Re: Problem writing title while looping

From: Alessandra Giannini <alesall_at_nyahnyahspammersnyahnyah>
Date: Fri Dec 13 2013 - 06:44:28 MST

Hi Almami,

the reason you are stuck on the December label is that you are defining the label at the end of the first loop, if I understand correctly - here:

 diff@long_name = " "+month(i)

Then you plot in a second loop, but the long_name definition remains there, stuck at the end of the first loop.

You can do a couple of things to optimize your routine.

You can try changing the order of things as follows:

1) assuming they are the same for all 12 plots, put all the plotting resources first, at the top of your script, before the first loop.
If any of the resources change with month, only stick those in the do loop.

2) it looks to me like you are looping twice over the same do loop - why not use only one do loop and include the plotting command in it?
In that same loop you can read or compute what you want to plot, and then plot it.

That said, and keeping in mind that languages like ncl don't like loops, it seems to me the first loop is not necessary - just do the simple difference of two variables with the same dimensions, as such:

diff = f45 - pre

Then do the loop on plotting only, but remember to stick this line into it:

diff@long_name = " "+month(i)

Hope this helps!
best, alessandra

-- 
Alessandra Giannini
IRI for Climate and Society - The Earth Institute at Columbia University
P.O. Box 1000, Palisades NY 10964-8000
phone/fax: +1 845 680-4473/4864 - email: alesall@iri.columbia.edu
On Dec 13, 2013, at 7:21 AM, Almami Johnson <almamij@gmail.com> wrote:
> Dear all
> 
> I come across a problem which I don't know how to work it out. I have
> made this script (see below) that displays 12 subplots in a panel;
> each subplot represents a month (January, February ... December). I
> have done a do loop to do the calculation and at the same time
> attached each month with a long_name being respectively January,
> February ... December.  The problem is for all 12 subplots, the title
> comes out as "December". Could you please me on this? i.e. to have for
> each subplot the actual month: January .... December?
> 
> Thanks !
> 
> ****************The script
> **********************************************************************************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
> 
> ;*****************************************************************************************
> ; Creating Function to Read at once Precip and Convert Calendar to
> Human Readable YYYYMM
> ;*****************************************************************************************
> 
>   undef("getPrecip")
>   function getPrecip(fname[1]:string, vname[1]:string)
>   local x
> 
>   begin
>     f       = addfile(fname, "r")
>     x       = f->$vname$(0,{-10:35},{-30:40})  ; read specified time period
>   return(x)
>   end
> 
> 
> ;*****************************************************************************************
> ; Creating a new variable corresponding to all the 12 months and getting each
> ;*****************************************************************************************
> 
>   x = getPrecip("ensmean_pre_events_above_75P_01_mean_Correct.nc","moderate_wet_days_wrt_75th_percentile_of_reference_period")
> 
>   dim = dimsizes(x(:,:))
>   nlat = dim(0)
>   nlon = dim(1)
> 
>   pre = new((/12,nlat,nlon/), "float")
>   f45 = new((/12,nlat,nlon/), "float")
>   diff= new((/12,nlat,nlon/), "float")
> 
>   month = (/"January","February","March","April","May","June","July","August","September","October","November","December"/)
>   moncount = (/"01","02","03","04","05","06","07","08","09","10","11","12"/)
>   nplots = dimsizes(month)
> 
>   do i = 0,nplots-1
>   print("Getting data and preparing for " +month(i)+ " corresponding
> to " +moncount(i)+ " for both present-day and future")
> 
>   pre(i,:,:)  =
> getPrecip("ensmean_pre_events_above_75P_"+moncount(i)+"_mean_Correct.nc","moderate_wet_days_wrt_75th_percentile_of_reference_period")
>   printVarSummary(pre(i,:,:))
> 
>   f45(i,:,:)  =
> getPrecip("ensmean_f85_events_above_75P_"+moncount(i)+"_mean_Correct.nc","moderate_wet_days_wrt_75th_percentile_of_reference_period")
>   printVarSummary(f45(i,:,:))
> 
>   print("Now taking the difference between future and present for
> "+month(i)+ " corresponding to " +moncount(i))
> 
>   diff(i,:,:) = f45(i,:,:) - pre(i,:,:)
>   diff@long_name = "    "+month(i)
>   diff@units    = " "
>   copy_VarCoords(pre(i,:,:),diff(i,:,:))
>   printVarSummary (diff(i,:,:))             ; print a summary of the variable
> 
>   end do
> 
> ;************************************************************************************************************
> ; Create graphic array for panel plot and Open a Work Station
> ;************************************************************************************************************
> 
>   plots = new(nplots,graphic)                             ; Open a
> variale plots to store 4 Figures
>   wks = gsn_open_wks("ps","Events_Above_75P")             ; open a workstation
> 
> ;*************************************************************************************************************
> ; Create Colors
> ;*************************************************************************************************************
> 
> ;   colors = (/ (/255,255,255/), (/0,0,0/), (/255,255,255/),
> (/220,255,220/),(/200,235,200/), \
> ;            (/160,215,160/), (/120,195,120/), (/80,175,80/),
> (/40,155,40/), (/10,120,10/), \
> ;            (/0,100,0/), (/0,70,0/), (/0,40,0/), (/0,10,0/) /) * 1.0
> ; we multiply by 1 to make colors float
> 
> ;   colors = colors/255.
> ; normalize (required by NCL)
> 
> ;   colors = RGBtoCmap("green.txt")
>   gsn_define_colormap(wks, "precip_diff_12lev")
>             ; generate new color map
> 
> 
> ;*************************************************************************************************************
> ; Create plots options
> ;*************************************************************************************************************
> 
>   res                      = True               ; plot options desired
>   res@gsnSpreadColors      = True               ; span full colormap
>   res@gsnDraw              = False              ; don't draw
>   res@gsnFrame             = False              ; don't advance frame
>   res@cnFillOn             = True               ; turn on color fill
> 
>   res@cnLevelSelectionMode = "ExplicitLevels"
> ; set explicit contour levels
>   res@cnLevels             =
> (/-50.,-40.,-30.,-25.,-20.,-16.,-12.,-8.,-5.,-2.,-1.,1.,
> 2.,5.,8.,12.,16.,20.,25.,30.,40.,50./)
> 
>   res@cnLinesOn            = False              ; turn off contour lines
>   res@cnLineLabelsOn       = False              ; turn off the values
> that would be on the contours
>   res@mpFillOn             = False              ; turn off gray continents
>   res@gsnMaximize          = True               ; Use the full page
>   res@gsnPaperOrientation  = "landscape"         ; Change orientation
>   res@mpProjection         = "Mercator"
>   res@mpLimitMode          = "LatLon"
>   res@gsnAddCyclic         =  False             ; Regional data
> ;   res@mpMaxLatF           =  max(latitude)         ; Taking the whole domain
> ;   res@mpMinLatF           =  min(latitude)
> ;   res@mpMaxLonF           =  max(longitude)
> ;   res@mpMinLonF           =  min(longitude)
> 
>   res@mpMaxLatF            =   25.0
>   res@mpMinLatF            =  -5.0
>   res@mpMaxLonF            =   30.0
>   res@mpMinLonF            =  -30.0
> 
>   res@mpOutlineBoundarySets       = "National"        ; Display the
> conutries borders
>   res@mpOutlineOn = True                        ; Turn on the outline
> of the map
>   res@mpGeophysicalLineThicknessF = 3.0         ; Increase the
> thickness of map border
>   res@pmTickMarkDisplayMode       = "Always"          ; Turn on map
> tickmarks, i.e. writing lons and lats along the axes
>   res@lbLabelBarOn                = False               ; turn off
> individual colorbars
> ;*********************************************************************************************************************
> ; Changing the font seize
> ;*********************************************************************************************************************
>    res@tmXBLabelFontHeightF = 0.02                  ; resize tick
> labels for X axis
>    res@tmYLLabelFontHeightF = 0.02                  ; resize tick
> labels for Y axis
>                                                     ; change label
> spacing to avoid overlap
>   ;res@lbLabelStride    = 2                         ; and write every
> other label
>    res@lbLabelFontHeightF = 0.018                   ; change the size
> of the label bar
>   ;res@tiMainString         = "1st Plot Test"       ; Provide a main Tile
> 
>    res@gsnStringFontHeightF = 0.018                 ; Increase the
> title font size (long_name)
> 
> ;*******************************************************************************************************
> ; Displaying the Panel Plot
> ;*******************************************************************************************************
> 
>    do i = 0, nplots-1
> 
>    plots(i) = gsn_csm_contour_map(wks,diff(i,:,:),res)
> 
>    end do
> 
>    pnlres = True                                            ; Add panel options
>    pnlres@gsnFrame     = False                              ; Don't frame yet
>    pnlres@txString   = "RCP4.5 minus Historical" ; add main title for the panel
>    pnlres@gsnPanelLabelBar  = True                          ; Setting
> a common colorbar
>    pnlres@lbLabelFontHeightF  = 0.02                        ;
> Increase font size of the numbers in the label bar
>    pnlres@pmLabelBarWidthF    = 0.9                         ; label bar width
>    pnlres@pmLabelBarHeightF   = 0.08                        ; label bar height
>    pnlres@pmLabelBarOrthogonalPosF = 0.002                  ; move
> the labelbar up and down
>    pnlres@pmLabelBarParallelPosF   = 0.03                   ;move the
> labelbar left or right
>    pnlres@lbLabelAutoStride = True                          ; Optimal
> labels, i.e. avoid overlap
>    pnlres@gsnPanelDebug     = True                          ; To get
> information from panel
>    pnlres@gsnPanelFigureStrings=
> (/"a)","b)","c)","d)","e)","f)","g)","h)","i)","j)","k)","l)"/)   ;
> add strings to panel
>    pnlres@gsnPanelFigureStringsFontHeightF  = 0.018         ;
> Increase the size of these strings
>    pnlres@gsnPanelFigureStringsPerimOn              = False        ;
> Remove the box around these strings
>    pnlres@amJust   = "TopLeft"                              ; Where
> to put these strings
>    pnlres@gsnPanelYWhiteSpacePercent = 08.                  ; Adds
> the white space to the panel plot
>    pnlres@gsnPanelXWhiteSpacePercent = 05.                  ; Adds
> the white space to the panel plot
> ;   pnlres@gsnPanelTop                = 1.
> ;   pnlres@gsnPanelBottom             = 0.
>  gsn_panel(wks,plots,(/4,3/),pnlres)
> 
> end
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Dec 13 06:44:42 2013

This archive was generated by hypermail 2.1.8 : Fri Dec 13 2013 - 11:39:30 MST