[ncl-talk] can not create year labels for x axis

Dennis Shea shea at ucar.edu
Thu Jan 5 13:35:43 MST 2017


Well, I think you have to look at your times. Please carefully read:
   http://www.ncl.ucar.edu/Document/Functions/Built-in/cd_calendar.shtml

ncl 0> time = (/697594.5d, 1021954.5d/)   ; min and max provided by user
ncl 1> time!0 = "time"
ncl 2> time at units = "hours since 1900-1-1 00:00:00"    ; CDO created units
ncl 3> ymdhms = cd_calendar(time, 0)
ncl 4> print(ymdhms)


Variable: ymdhms
Type: float
Total Size: 48 bytes
            12 values
Number of Dimensions: 2
Dimensions and sizes:   [2] x [6]   . two time steps, 6 elements
Coordinates:
Number Of Attributes: 1
  calendar :    standard
(0,0)   1979                      year
(0,1)    8                           month
(0,2)    1                           day
(0,3)   10                          hour
(0,4)   30                          minute
(0,5)    0                           second

(1,0)   2016
(1,1)    8
(1,2)    1
(1,3)   10
(1,4)   30
(1,5)    0

So, your time variable  spans 197908 to 201608.
You wrote:
  "My data file contains single value for every year (JJAS season .."
So,

  197908
  198008
  198108
   ..........
   201508
   201608


I think you should create:

ncl 5> year = ispan(1979,2016,1)
ncl 6> print(year)

This (year) should be your 'time' (x) axis.

======
Also, you said you used the following and it failed.
Did you read the documentation for either function?
A rule: (1) read documentation; (2) then, use the function

http://www.ncl.ucar.edu/Document/Functions/Contributed/yyyymm_time.shtml
http://www.ncl.ucar.edu/Document/Functions/Contributed/yyyymm_to_yyyyfrac.shtml

ncl 9>     tstart = 1979
ncl 10>   tend   = 2016
ncl 11>   yyyymm   = yyyymm_time(tstart, tend, "integer")
ncl 12>   yyyyfrac = yyyymm_to_yyyyfrac(yyyymm,0.0)

ncl 13> print(yyyymm+"   "+yyyyfrac)

(0)     197901   1979         ; Note: the default print used 2 decimal
place but there are more.
(1)     197902   1979.08
(2)     197903   1979.17
(3)     197904   1979.25
(4)     197905   1979.33
(5)     197906   1979.42
(6)     197907   1979.5
(7)     197908   1979.58
[snip]
(448)   201605   2016.33
(449)   201606   2016.42
(450)   201607   2016.5
(451)   201608   2016.58
(452)   201609   2016.67
(453)   201610   2016.75
(454)   201611   2016.83
(455)   201612   2016.92
(448)   201605   2016.33
(449)   201606   2016.42
(450)   201607   2016.5
(451)   201608   2016.58
(452)   201609   2016.67
(453)   201610   2016.75
(454)   201611   2016.83
(455)   201612   2016.92

you write: "which gave the label in years but not for the full series of
time"
I do not understand. What is wrong with these numbers?











On Thu, Jan 5, 2017 at 10:29 AM, Dhirendra Kumar <dhirendra.cub at gmail.com>
wrote:

> Mary
>
> Thank you for such a prompt reply.
>
> My data file contains single value for every year (JJAS season), and it
> was processed through CDO. The PrintMinMax gives the output:
>
> Number Of Attributes: 6
>   standard_name :       time
>   long_name :   time
>   bounds :      time_bnds
>   units :       hours since 1900-1-1 00:00:00
>   calendar :    standard
>   axis :        T
> (0)     time: min=697594.5   max=1021954.5
>
> You were correct about the date. So that was the reason, it did not work.
> As I am a newbie here; could you please guide me further to correct the
> dates for the plotting with the same file. I have also tried using the
> function mentioned in the mail but it give labels starting from 7000.
> Then I had tried adding the following line in the code:
>
>  tstart = 1979
>   tend   = 2016
>   yyyymm   = yyyymm_time(tstart, tend, "integer")
>   yyyyfrac = yyyymm_to_yyyyfrac(yyyymm,0.0)   ; create fractional years for
>                                             ; plotting purposes
>
> which gave the label in years but not for the full series of time.
>
> Looking forward for your input.
>
> Thanks
>
>
> On Thu, Jan 5, 2017 at 9:38 PM, Mary Haley <haley at ucar.edu> wrote:
>
>> Dhirendra,
>>
>> First, a couple of NCL tips:
>>
>> 1)
>> ​You don't need to use a "do" loop to do simple array arithmetic.  You
>> can replace the following code:
>>
>>
>>
>> ​  ​
>> dateF   = new (dimDate, float)
>>    do n=0,dimDate-1
>>     yyyy     = date(n)/100
>>     mon      = date(n)-yyyy*100
>>     dateF(n) = tofloat(yyyy + (mon-1)/12.)
>>   end do
>>
>> ​with:
>>
>>
>>     yyyy  = date/100
>>     mon   = date-yyyy*100
>>     dateF = tofloat(yyyy + (mon-1)/12.)
>>
>> 2) We have a function that converts YYYYMM to YYYY.frac, so the above
>> code can be replaced with
>> ​ one line:​
>>
>>
>>   dateF = yyyymm_to_yyyyfrac(date,0.0)
>>
>>
>> The values on your X axis look suspicious to me, as they go from around
>> 7000 to over 10000. Are these really your year values?  Did you look at the
>> original "date" variable to make sure these values are actually YYYYMM?
>>
>> printVarSummary(date)
>> printMinMax(date)
>>
>> You should also do:
>>
>> print(date)
>>
>> to see what all the numbers look like.
>>
>>
>> To get nice labels on a time axis, I highly recommend the use of the
>> time_axis_labels function:
>>
>> http://www.ncl.ucar.edu/Document/Functions/User_contributed/
>> time_axis_labels.shtml
>>
>> You have lots of options for how to format your labels.
>>
>> You can see some examples of how to use this at:
>>
>> http://www.ncl.ucar.edu/Applications/time_labels.shtml
>>
>> In particular, look at time_labels_4.ncl. This shows how to plot time
>> data that is in a YYYYMM format. I'm not sure your time is in this format,
>> but hopefully the above page will give you enough information to format
>> your X labels the way you want them.
>>
>> --Mary
>>
>>
>>
>>
>> On Thu, Jan 5, 2017 at 8:09 AM, Dhirendra Kumar <dhirendra.cub at gmail.com>
>> wrote:
>>
>>> Hi
>>>
>>> I am trying to plot an nc file having anomaly time series for the JJAS
>>> season. I have tried to follow the sample scripts over the NCL web
>>> resource, but could not succeed in getting the correct labels of time
>>> (year) on the X axis.
>>>
>>> Would appreciate if anybody could help...
>>> the script is as following-
>>>
>>> begin
>>>  f = addfile ("std_tim_anomaly.nc", "r")      ; add file
>>>  date  = f->time
>>>  printVarSummary(date)
>>>  precip= f->tp(:,0,0)
>>>  printVarSummary(precip)
>>>
>>>  dimDate = dimsizes(date)         ; number of dates
>>>
>>> ; convert integer YYYYMM to float
>>>
>>>  dateF   = new (dimDate, float)
>>>    do n=0,dimDate-1
>>>     yyyy      = date(n)/100
>>>     mon       = date(n)-yyyy*100
>>>     dateF(n) = tofloat(yyyy + (mon-1)/12.)
>>>   end do
>>>
>>> ;*********************************
>>> ; create plot
>>> ;********************************
>>>   wks   = gsn_open_wks ("ps", "bar" ) ; send graphics to PS file
>>>   res          = True             ; plot mods desired
>>>   res at gsnScale = True
>>>
>>> ; these four resources allow the user to stretch the plot size, and
>>> ; decide exactly where on the page to draw it.
>>>
>>>   res at vpXF      = 0.10            ; In page coordinates, where to start
>>>   res at vpYF      = 0.75            ; the plot
>>>   res at vpHeightF = 0.43            ; Changes the aspect ratio
>>>   res at vpWidthF  = 0.85
>>>
>>>   res at trYMinF   = -3.0            ; min value on y-axis
>>>   res at trYMaxF   =  3.0            ; max value on y-axis
>>>
>>>   res at tiYAxisString = "Anomalies" ; y-axis label
>>> ; this resource changes a regular line plot to a bar chart
>>>   res at gsnXYBarChart         = True ; create bar chart
>>> ; this resource is required to get bars above and below a reference line
>>>   res at gsnYRefLine           = 0. ; reference line
>>>   res at gsnXYBarChart         = True            ; create bar chart
>>>   res at gsnAboveYRefLineColor = "red"           ; above ref line fill red
>>>   res at gsnBelowYRefLineColor = "blue"          ; below ref line fill blue
>>>   res at gsnXYBarChartBarWidth = 24.0
>>>  plot = gsn_csm_xy (wks,dateF(::),precip,res)
>>> end
>>>
>>> [image: Inline image 1]this is the plot which is being created.
>>>
>>> Thanks in anticipation.
>>>
>>> --
>>> ========================================
>>> Dhirendra Kumar
>>> PhD Student
>>> School of Environmental Sciences, JNU
>>> New Delhi (India)
>>> Contact: +91 9910778043 <+91%2099107%2078043>
>>> Alternate email: dhirendra.cub at outlook.com
>>> =========================================
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> ncl-talk at ucar.edu
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>>
>>
>
>
> --
> ========================================
> Dhirendra Kumar
> PhD Student
> School of Environmental Sciences, JNU
> New Delhi (India)
> Contact: +91 9910778043 <+91%2099107%2078043>
> Alternate email: dhirendra.cub at outlook.com
> =========================================
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170105/ebe676e8/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 13157 bytes
Desc: not available
Url : http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170105/ebe676e8/attachment.png 


More information about the ncl-talk mailing list