[ncl-talk] Spatial trend plot for JJAS season

Buzan, Jonathan jbuzan at purdue.edu
Thu Apr 21 09:24:02 MDT 2022


Hi Lekshmi,

The month_to_annual function can be found in the:

$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl

I recommend looking at how the function behaves, and then creating your own function that mimics the month_to_annual but specifically for the season that you are focused on.

For example, a 3D variable, T(time,lat,lon) uses the rankx.eq.3:
contributed.ncl

   if (rankx.eq.3) then
       ny     = dimx(1)      ; nlat
       mx     = dimx(2)      ; mlon
       nyr    = -1
       xAnnual= new ( (/nyrs,ny,mx/), typeof(x), getFillValue(x)) ;  contributed.ncl
       do nt=0,ntim-1,NMOS
          xTemp = x(nt:nt+NMOS1,:,:) ; cleaner code only
          nyr   = nyr+1

          if (opt.eq.0) then
              xAnnual(nyr,:,:) = dim_sum_n ( xTemp, 0 )
          else
              xAnnual(nyr,:,:) = dim_avg_n ( xTemp, 0 )
          end if

          nMsg  = 0                             ; number of missing for current year (nyr)
          if (isatt(x,"_FillValue")) then
              nMsg = num (ismissing(xTemp) )    ; for all grid points
          end if

          if (nMsg.gt.0) then
              nGood            = dim_num_n(.not.ismissing(xTemp), 0 )          ; (lat,lon)
              xAnnual(nyr,:,:) = mask( xAnnual(nyr,:,:), nGood.ge.nmos, True)  ; ?False
              print("month_to_annual: some grid points have missing data: nt="+nt \
                   +" nyr="+nyr+"  num(nGood)="+num(nGood.gt.0))
          end if
       end do

       copy_VarAtts (x, xAnnual)
       xAnnual at NCL  = "month_to_annual"
       xAnnual!0    = "year"
       copy_VarCoords_skipDim0 (x, xAnnual)
       return(xAnnual)
   end if


Cheers,
-Jonathan



On Apr 21, 2022, at 16:52, Lekshmi M S via ncl-talk <ncl-talk at mailman.ucar.edu<mailto:ncl-talk at mailman.ucar.edu>> wrote:


I would like to modify the following script to spatially plot the JJAS precipitation trend plot for 70years. Instead of using "month_to_annual", I think I have to use "month_to _season" here. But JJAS is not defined as a season here. Please help me to do this for JJAS months.

;*************************************************
; regress_4.ncl
;
; Concepts illustrated:
;   - Drawing color-filled contours over a cylindrical equidistant map
;   - Calculating the regression coefficient (slope) at each grid point
;   - Copying attributes from one variable to another
;
;*************************************************
;
; These files are loaded by default in NCL V6.2.0 and newer
; 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

;************************************************
; Specify geographical region and time span (year-month start and end
;************************************************

  latS     = -90
  latN     =  90
  lonL     =   0
  lonR     = 360

  ymStrt   = 195101
  ymLast   = 201012

  pltTitle = "Globe: "+(ymStrt/100)+"-"+(ymLast/100)

;************************************************
; Read from netCDF file: variable is type short...unpack
;************************************************
   diri   = "./"
   fili   = "air.sig995.mon.mean.nc<http://air.sig995.mon.mean.nc/>"
   f      = addfile(diri+fili,"r")

   YYYYMM = cd_calendar( f->time, -1)

   iStrt  = ind(YYYYMM.eq.ymStrt)
   iLast  = ind(YYYYMM.eq.ymLast)

   x      = short2flt( f->air(iStrt:iLast,{latS:latN},{lonL:lonR}) )

   x      = x-273.15
   x at units= "degC"

   printVarSummary(x)                            ; [time| 720]x[lat| 91]x[lon| 180]

   yyyymm = cd_calendar(x&time, -1)
   yyyy   = yyyymm/100

   dimx = dimsizes(x)
   ntim = dimx(0)                ; all years and months
   nlat = dimx(1)
   mlon = dimx(2)

   year  = ispan(yyyy(0), yyyy(ntim-1), 1)
   nyrs  = dimsizes(year)

;************************************************
; Areal averages: cos(lat) is good enough
;************************************************

   xann  = month_to_annual(x , 1)                 ; [year| 60]x[lat| 91]x[lon| 180]
   xann&year  = year
   printVarSummary(xann)

;************************************************
; Calculate the regression coefficients (slopes)
;************************************************
   rc           = regCoef(year,xann(lat|:,lon|:,year|:))

   rc at long_name = "Trend"
   rc at units     = xann at units+"/year"
   copy_VarCoords(xann(0,:,:), rc)                ; copy lat,lon coords

   printVarSummary(rc)

;************************************************
; for illustration: change units to degC/period-used
;************************************************
   rc           = rc*nyrs                       ; (C/year)*(nyrs)
   rc at units     = "degC/"+nyrs+"_year"

;************************************************
; plotting parameters
;************************************************
   wks  = gsn_open_wks("png","regress")         ; send graphics to PNG file

   res                       = True
   res at gsnMaximize           = True             ; make large

   res at cnFillOn              = True             ; turn on color
   res at cnLinesOn             = False            ; turn off contour lines
   res at cnLineLabelsOn        = False            ; turn off contour line labels
 ;;res at cnFillMode            = "RasterFill"

   res at cnLevelSelectionMode  = "ManualLevels"   ; set manual contour levels
   res at cnMinLevelValF        =  -2.0            ; set min contour level
   res at cnMaxLevelValF        =   2.0            ; set max contour level
   res at cnLevelSpacingF       =   0.2            ; set contour interval

   res at mpFillOn              = False            ; turn off default background gray
  ;res at mpCenterLonF          = 180

   res at gsnCenterString       = year(0)+"<mailto:;setmanualcontourlevelsres at cnMinLevelValF=-2.0;setmincontourlevelres at cnMaxLevelValF=2.0;setmaxcontourlevelres at cnLevelSpacingF=0.2;setcontourintervalres at mpFillOn=False;turnoffdefaultbackgroundgray;res at mpCenterLonF=180res at gsnCenterString=year(0)+>-"+year(nyrs-1)

   res at tiMainString          = "20th Century Reanalysis: sig995"    ; fili
   plot = gsn_csm_contour_map_ce(wks,rc,res)
  end



_______________________________________________
ncl-talk mailing list
ncl-talk at mailman.ucar.edu<mailto:ncl-talk at mailman.ucar.edu>
List instructions, subscriber options, unsubscribe:
https://mailman.ucar.edu/mailman/listinfo/ncl-talk

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20220421/715127ed/attachment.html>


More information about the ncl-talk mailing list