[ncl-talk] Issue with Decimal Places and Rounding off

Melissa Lazenby M.Lazenby at sussex.ac.uk
Wed Sep 28 08:45:05 MDT 2022


Hi NCL Users

I am currently using the function decimalPlaces like this below:

Round = decimalPlaces(rcMaxLon,2,True)
 Round1 = decimalPlaces(rcMaxLon at yave,2,True)

 pr4 at long_name = ""+model(gg)+""
  pr4 at units     = "s="+Round+" lat="+Round1+""

However, when I plot up my 39 CMIP models there are 3 in which the rounding to 2 decimal places has not worked properly...any ideas why this would be and any suggestions to fix it will be highly appreciated.

[cid:12776569-4ae3-473e-8535-21eef4fc0956]
[cid:dbfe906c-9598-431d-b6de-970a9e9b1607]


Kindest Regards
Melissa

Full code below:

;*************************************************
; regline_panel plots.ncl
;

;*************************************************
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

begin
;************************************************
; Read in Precip Data.
;************************************************

;Models

model = (/"ACCESS1-0","ACCESS1-3","bcc-csm1-1","BNU-ESM","CanESM2","CCSM4","CESM1-BGC","CESM1-CAM5","CMCC-CM","CMCC-CMS","CNRM-CM5","CSIRO-Mk3-6-0","EC-EARTH","FGOALS-g2","FIO-ESM","GFDL-CM3","GFDL-ESM2G","GFDL-ESM2M","GISS-E2-H_p1","GISS-E2-H_p2","GISS-E2-H_p3","GISS-E2-R_p1","GISS-E2-R_p2","GISS-E2-R_p3","HadGEM2-AO","HadGEM2-CC","HadGEM2-ES","inmcm4","IPSL-CM5A-LR","IPSL-CM5A-MR","IPSL-CM5B-LR","MIROC5","MIROC-ESM","MIROC-ESM-CHEM","MPI-ESM-LR","MPI-ESM-MR","MRI-CGCM3","NorESM1-M","NorESM1-ME"/)

wks  = gsn_open_wks("X11","2022_PHD_IPCC_Model_regline_historical_overlay_1981-2010_25-50")              ; specifies a plot
gsn_define_colormap(wks,"BlueOrangeRed")  ; choose color map
gsn_reverse_colormap(wks)
plot = new (dimsizes(model),"graphic")
do gg = 0,dimsizes(model)-1
    in=addfile("/mnt/geog/ml382/ml382_sals000408_home_dir/melsicz/historicalregline1981_2010IPCC/pr_Amon_"+model(gg)+"_historical_safrica_climDJF2022.nc","r")
 lat   = in->lat                                      ; get lat
 lon   = in->lon                                      ; get lon
 time  = in->time                                     ; get time

   pr1 = in->pr(:,:,:)


  pr2 = pr1(time|0, {lat|-30:0}, {lon|25:50})

 dimpr2   = dimsizes(pr2)
    nlat     = dimpr2(0)
    mlon     = dimpr2(1)

 pr2MaxLon = new ( mlon, typeof(pr2), pr2 at _FillValue)

   do ml=0,mlon-1
      imax = maxind(pr2(:,ml))
      pr2MaxLon(ml) = dble2flt(lat(imax))
   end do

   print(pr2MaxLon)
   print(pr2&lon)

   print("-------------------------------")
   print("pr2MaxLon: "+pr2&lon+"   "+pr2MaxLon)

   ;Regression Line

   rcMaxLon = regline(pr2&lon,pr2MaxLon)
   print(rcMaxLon)

   print(rcMaxLon at yave)

   bMaxLon  = rcMaxLon at yintercept
   print(bMaxLon)

   xMaxLon  = pr2&lon
   print(xMaxLon)
   yMaxLon  = rcMaxLon*pr2&lon + bMaxLon
   print(yMaxLon)

   print("-------------------------------")
   print(xMaxLon+"   "+yMaxLon)

;************************************************
; create an array to hold both the original data
; and the calculated regression line
;************************************************

 data      = new ( (/2,dimsizes(pr2MaxLon)/), typeof(pr2MaxLon))

 data(1,:) = dble2flt(rcMaxLon)*(dble2flt(xMaxLon)-dble2flt(rcMaxLon at xave)) + dble2flt(rcMaxLon at yave)

;************************************************
; Read in Precip Data with larger domain.
;************************************************

f=addfile("/mnt/geog/ml382/ml382_sals000408_home_dir/melsicz/historicalregline1981_2010largeIPCC/pr_Amon_"+model(gg)+"_historical_safrica_climDJF2022.nc","r")

  lat1   = f->lat                                      ; get lat
  lon1   = f->lon                                      ; get lon
  time1  = f->time

  pr6 = f->pr(:,:,:)

  pr4 = pr6(time|0,{lat|-40:10}, {lon|-10:70})

;************************************************
; Read in Precip Data.
;************************************************
 ;Obs
 cin  = addfile("/mnt/geog/ml382/melphd/regressionline/SIOCZ/pr_CMAP_historical_safrica_climDJF_small.nc","r")
 prcmap = cin->precip(:,:,:)
 pr5 = prcmap(time|0, {lat|-30:0}, {lon|25:50})


;*************************************************************
;Calculations of max precip for lat and lon values observations (CMAP)
;**************************************************************

    dimpr5   = dimsizes(pr5)
    nlat     = dimpr5(0)
    mlon     = dimpr5(1)

 pr5MaxLon = new ( mlon, typeof(pr5), pr5 at _FillValue)

   do ml=0,mlon-1
      imax = maxind(pr5(:,ml))
      pr5MaxLon(ml) = dble2flt(lat(imax))
   end do

   print(pr5MaxLon)
   print(pr5&lon)

   print("-------------------------------")
   print("pr5MaxLon: "+pr5&lon+"   "+pr5MaxLon)

   ;Regression Line

   regcMaxLon = regline(pr5&lon,pr5MaxLon)
   print(regcMaxLon)

   print(regcMaxLon at yave)

   bMaxLon  = regcMaxLon at yintercept
   print(bMaxLon)

   xMaxLon  = pr5&lon
   print(xMaxLon)
   yMaxLon  = regcMaxLon*pr5&lon + bMaxLon
   print(yMaxLon)

   print("-------------------------------")
   print(xMaxLon+"   "+yMaxLon)

;************************************************
; create an array to hold both the original data
; and the calculated regression line
;************************************************

 datac      = new ( (/2,dimsizes(pr5MaxLon)/), typeof(pr5MaxLon))

 datac(1,:) = dble2flt(regcMaxLon)*(dble2flt(xMaxLon)-dble2flt(regcMaxLon at xave)) + dble2flt(regcMaxLon at yave)


   ; resource lists

 res                     = True               ; plot mods desired
 res at gsnAddCyclic        = False
 res at cnFillOn            = True               ; color on
 res at lbLabelStride       = 2                  ; every other label
 res at gsnSpreadColors     = True               ; use full range of color map
 res at cnLineLabelsOn      = False              ; no contour line labels
 res at lbLabelBarOn        = False
 res at gsnDraw             = False              ; do not draw the plot
 res at gsnFrame            = False


;pr4   = pr6(0,{-40:10},{-10:70})

dimpr4   = dimsizes(pr4)
latn     = dimpr4(0)
lonm     = dimpr4(1)


res at mpLimitMode       = "Corners"  ;
res at mpLeftCornerLonF  = lon1(0)
res at mpRightCornerLonF = lon1(lonm-1)
res at mpLeftCornerLatF  = lat1(0)
res at mpRightCornerLatF = lat1(latn-1)


 res at tmXBMode      = "Explicit"     ; Define own tick mark labels.
 res at tmXBValues    = (/ -9.,0.,10.,20.,30.,40.,50.,60.,69 /)
 res at tmXBLabels    = (/ "10W","0","10E","20E","30E","40E","50E","60E","70E" /)

 res at tmYLMode      = "Explicit"     ; Define own tick mark labels.
 res at tmYLValues    = (/  -39.,-30.,-20.,-10.,-0.,9 /)
 res at tmYLLabels    = (/"40S","30S","20S","10S","0","10N" /)

 res at vpXF             = 0.12               ; default is 0.2 change aspect
 res at vpYF             = 0.8                ; default is 0.8 ration
 res at vpHeightF        = 0.4                ; default is 0.6
 res at vpWidthF         = 0.8                ; default is 0.6

 res at cnLevelSelectionMode = "ManualLevels"  ; manual levels
 res at cnMinLevelValF       =  0              ; min level
 res at cnMaxLevelValF       =  400            ; max level
 res at cnLevelSpacingF      =  50           ; contour spacing

  res at tmXBLabelFontHeightF = 0.022                ; resize tick labels
  res at tmYLLabelFontHeightF = 0.022
  res at pmLabelBarOrthogonalPosF = .25           ; move whole thing down


;************************************************
; plotting parameters
;************************************************
 sres                      = True                   ; plot mods desired
 sres at gsnMaximize          = True                   ; maximize plot in frame
 sres at xyMarkLineModes      = (/"Markers","Lines"/)  ; choose which have markers
 sres at xyMarkers            = 16                     ; choose type of marker
 sres at xyLineColors         = (/"blue","black"/)
 sres at xyMonoDashPattern    = True
 sres at gsnLineDashPattern    = 1
 sres at xyDashPattern        = 16                     ; solid line
 sres at xyLineThicknesses    = (/1,3/)                ; set second line to 2
 sres at gsnDraw = False                        ; do not draw the plot
 sres at gsnFrame = False


 ; reverse the first two colors
  setvalues wks
    "wkColorMap"        : "BlueOrangeRed" ;"BkBlAqGrYeOrReViWh200"
    "wkForegroundColor" : (/0.,0.,0./)
    "wkBackgroundColor" : (/1.,1.,1./)
  end setvalues

;************************************************
; plotting parameters
;************************************************
 cres                     = True                   ; plot mods desired
 cres at gsnMaximize         = True                   ; maximize plot in frame
 cres at xyMarkLineModes     = "Lines"                ; choose which have markers
 cres at xyDashPatterns      = 0                      ; solid line
 cres at xyLineThicknesses   = (/1,3/)               ; set second line to 2
 cres at gsnDraw = False                        ; do not draw the plot
 cres at gsnFrame = False


  ; reverse the first two colors
  setvalues wks
    "wkColorMap"        : "BlueOrangeRed" ;"BkBlAqGrYeOrReViWh200"
    "wkForegroundColor" : (/0.,0.,0./)
    "wkBackgroundColor" : (/1.,1.,1./)
  end setvalues

Round = decimalPlaces(rcMaxLon,2,True)
 Round1 = decimalPlaces(rcMaxLon at yave,2,True)

 pr4 at long_name = ""+model(gg)+""
   ;pr4 at units     = "s="+rcMaxLon+" lat="+rcMaxLon at yave+""
 pr4 at units     = "s="+Round+" lat="+Round1+""


 print(rcMaxLon)
 print(Round)
 print(Round1)

  res at tmXBLabelsOn         = False              ; do not draw bottom labels
  res at tmXBOn               = False              ; no bottom tickmarks
  res at tmYLLabelsOn         = False              ; no right labels
  res at tmYLOn               = False              ; no right tickmarks

 if((gg.eq.36) .or. (gg.eq.37) .or. (gg.eq.38)) then

       res at tmXBLabelsOn         = True              ; do not draw bottom labels
       res at tmXBOn               = True              ; no bottom tickmarks
    else
      res at tmXBLabelsOn         = False              ; do not draw bottom labels
      res at tmXBOn               = False              ; no bottom tickmarks
  end if

 if((gg.eq.0) .or. (gg.eq.4) .or. (gg.eq.8) .or. (gg.eq.12) .or. (gg.eq.16) .or. (gg.eq.20) .or.(gg.eq.24) .or. (gg.eq.28) .or. (gg.eq.32) .or. (gg.eq.36)) then
       res at tmYLLabelsOn         = True              ; do not draw bottom labels
       res at tmYLOn               = True              ; no bottom tickmarks
    else
      res at tmYLLabelsOn         = False              ; do not draw bottom labels
      res at tmYLOn               = False              ; no bottom tickmarks
  end if





 plot(gg)  = gsn_csm_contour_map(wks,pr4({-40:10},{-10:70}),res)        ; create plot
 plot2  = gsn_csm_xy(wks,pr2&lon,data,sres)        ; create plot
 plot3  = gsn_csm_xy(wks,pr5&lon,datac,cres)        ; create plot
 overlay(plot(gg),plot2)
 overlay(plot(gg),plot3)

  delete(pr4)
end do

; panel resource list  (if necessary)

panres                = True              ; mod panel plot
panres at gsnPanelLabelBar    = True                ; add common colorbar
panres at lbLabelFontHeightF =.012                 ; make labels larger
panres at gsnMaximize = True ; Maximize plot in frame.

gsn_panel(wks,plot(:23),(/6,4/),panres)    ; panel 16 models at a time.
gsn_panel(wks,plot(24:),(/6,4/),panres)
;gsn_panel(wks,plot(32:),(/4,4/),panres)
;gsn_panel(wks,plot(25:32),(/4,4/),panres)
;gsn_panel(wks,plot(33:),(/4,2/),panres)
;gsn_panel(wks,plot(41:),(/4,2/),panres)
;gsn_panel(wks,plot(::),(/4,2/),panres)
end



Dr. Melissa Lazenby
Lecturer in Climate Change
Department of Geography
Chichester 1 C150
University of Sussex

"Education is the most powerful weapon which you can use to change the world" Nelson Mandela

[cid:32ebdb21-ea12-42a7-874a-c8cd6971e615]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20220928/ec334044/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 583899 bytes
Desc: image.png
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20220928/ec334044/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 378613 bytes
Desc: image.png
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20220928/ec334044/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Outlook-yklt2iha.png
Type: image/png
Size: 24937 bytes
Desc: Outlook-yklt2iha.png
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20220928/ec334044/attachment-0005.png>


More information about the ncl-talk mailing list