[ncl-talk] Converting time to datestring

Zilore Mumba zmumba at gmail.com
Thu Feb 4 15:27:12 MST 2021


Thank you very much Dave. With the changes you have suggested, all is
working very well now. I appreciate greatly.

On Thu, Feb 4, 2021 at 11:31 PM Dave Allured - NOAA Affiliate <
dave.allured at noaa.gov> wrote:

> Okay.  Comment out these two lines.  You might need to put them back
> later, but they do not do what you think.  @ttmValues is only to select the
> date positions to have visible printed labels.  In other words it is to
> select a subset of dates across the whole axis.
>
>     cd_date = cd_calendar(times, -2)
>     restick at ttmValues = cd_date
>
> Then change these two lines to read as follows.  These functions need the *original time
> coordinates*, not the *decoded date/time values* that you made in
> *cd_date*.
>
>     time_axis_labels (*time*, res, restick)
>     plot = gsn_csm_xy (wks, *time*, y, res)
>
> Pay close attention to the details in the function documentation for
> *time_axis_labels*.  Note that most of the *"ttm"* input attributes for
> that procedure are optional.  You only use them to get certain special
> effects if you want them.
>
>
> On Thu, Feb 4, 2021 at 1:58 PM Zilore Mumba via ncl-talk <
> ncl-talk at mailman.ucar.edu> wrote:
>
>> I have made a lot of progress with:
>>    times  = f->time                         ; get times in file
>>    cd_date = cd_calendar(times, -2)
>> ;   print(cd_date) This prints all the dates correctly as yyyymmdd
>>
>> Then I have:
>>    res                      =  True
>>    restick                  =  True
>>    restick at ttmValues        =  cd_date
>>    restick at ttmFormat        = "%D/%N/%Y"
>>    res at xyLineThicknessF     =  3.0                        ; set line
>> thickness
>>    res at xyLineColor          =  "red"                      ; set line
>> color
>>    res at xyDashPatterns       =  0                          ; solid line
>>    res at tmXBLabelFontHeightF = 0.010
>>    res at tiYAxisString        =  "Daily " + " " + y at standard_name +"  "+
>> "at 15S/30E"              ; y-axis string
>>
>> ; create the plot
>> ;-----------------------------------------------
>>    time_axis_labels(cd_date,res,restick)
>>           ; call the formatting procedure
>>    plot = gsn_csm_xy(wks,cd_date,y,res)
>>         ; should produce bottom x-axis ticks as dd/mm/yyyy (01/01/1971).
>>
>> I am able to have yyyy0000 on the x-axis. What I am trying to figure out
>> now is how to use "time_axis_labels".
>>
>> On Thu, Feb 4, 2021 at 10:17 PM Barry Lynn <barry.h.lynn at gmail.com>
>> wrote:
>>
>>> Hi:
>>>
>>> You have to define year, month, etc from your data.
>>>
>>> Can you do that?
>>>
>>>
>>> On Thu, 4 Feb 2021 at 20:39 Zilore Mumba <zmumba at gmail.com> wrote:
>>>
>>>> Hello Lynn,
>>>> Sorry I have had no success with the code you sent me. On the line
>>>> new_time_array = cd_inv_calendar( year(it,1,1),  month(it,1,1),
>>>> day(it,1,1), hour(it,1,1), minute(it,1,1), 0, new_time_units,0)
>>>> I get the error: fatal:Undefined identifier: (year) is undefined, can't
>>>> continue
>>>>
>>>> On Thu, Feb 4, 2021 at 7:55 AM Barry Lynn <barry.h.lynn at gmail.com>
>>>> wrote:
>>>>
>>>>> Hi:
>>>>>
>>>>> Try adjusting the third line here:
>>>>>
>>>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/cd_string.ncl"
>>>>>
>>>>> begin code...
>>>>>
>>>>> new_time_units = "hours since 1800-01-01 00:00"
>>>>> new_time_units = new_time_units
>>>>> new_time_array = cd_inv_calendar( year(it,1,1),  month(it,1,1),
>>>>> day(it,1,1), hour(it,1,1), minute(it,1,1), 0, new_time_units,0)
>>>>> print("new_time_array = " + new_time_array)
>>>>> printVarSummary(new_time_array)
>>>>> format = "" ;; defaults to "%H%M IST %d %c %Y"
>>>>>  format = "%H%M IST %d %c %Y"
>>>>>  new_time_array = new_time_array + 2.01
>>>>> ;format = "%H%M IDT %d %c %Y"
>>>>> ;new_time_array = new_time_array + 3.01
>>>>> time_var=cd_string(new_time_array,format)
>>>>> print("time_var = " + time_var)
>>>>>
>>>>> On Thu, Feb 4, 2021 at 7:17 AM Zilore Mumba via ncl-talk <
>>>>> ncl-talk at mailman.ucar.edu> wrote:
>>>>>
>>>>>> Hello,
>>>>>> I hope I can get quick help to set me off as it is taking me days to
>>>>>> make sense of what I have to do.
>>>>>> I have historical (1971-200) and model simulation (2021-2050) data. I
>>>>>> have adapted an ncl script from the DKRZ website for plotting a time series.
>>>>>>
>>>>>> With print(times) I get the time range 7701.5 to 18658.5.
>>>>>> When I plot the time series at a point I get the x-axis range 6000 60
>>>>>> 20000
>>>>>> ncdump -h on the file indicates that time:units = "days since
>>>>>> 1949-12-01 00:00:00"
>>>>>> my data has format yyyymmdd (yearmothday). I want the x-axis to show
>>>>>> dates.
>>>>>>
>>>>>> I have been looking in the ncl documentation how to do this. This may
>>>>>> take me days or weeks.
>>>>>> I would appreciate any help in this.
>>>>>> The script I have is below
>>>>>>
>>>>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRF_contributed.ncl"
>>>>>> begin
>>>>>>
>>>>>>    diri = "./"                             ; directory where to find
>>>>>> the data
>>>>>>    fili =
>>>>>> "pr_AFR-44_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_SMHI-RCA4_v1_day_19710101_20001231Reg.nc"
>>>>>>             ; file name
>>>>>>
>>>>>>    f   = addfile(diri+fili,"r")             ; open file
>>>>>>
>>>>>>    t1 = f->pr(:,{-15},{30})                 ; read precip time series
>>>>>> at 15S/30E
>>>>>>
>>>>>>    times  = f->time                         ; get times in file
>>>>>>    cd_date = cd_calendar(times, -2)
>>>>>>
>>>>>> ; open a workstation
>>>>>> ;----------------------------------------------------------------
>>>>>>    wks_type = "X11"                         ; graphics output type
>>>>>>    wks_type at wkWidth   =  1024               ; graphics output width
>>>>>> in pixels
>>>>>>    wks_type at wkHeight  =  1048               ; graphics output height
>>>>>> in pixels
>>>>>>
>>>>>>    wks = gsn_open_wks(wks_type, "plot_xy_multiple_lines_axis")
>>>>>>
>>>>>> ; resources for variable t1, y-axis
>>>>>> ;----------------------------------------------------------
>>>>>>    resT1                      =  True
>>>>>>    resT1 at ttmValues            =  cd_date
>>>>>>    resT1 at xyLineThicknessF     =  3.0                        ; set
>>>>>> line thickness
>>>>>>    resT1 at xyLineColor          =  "red"                      ; set
>>>>>> line color
>>>>>>    resT1 at xyDashPatterns       =  0                          ; solid
>>>>>> line
>>>>>>    resT1 at tmXBLabelFontHeightF = 0.010
>>>>>>    resT1 at tiYAxisString        =  "Daily " + " " + t1 at standard_name
>>>>>> +"  "+ "at 15S/30E"              ; y-axis string
>>>>>>
>>>>>> ; create the plot
>>>>>> ;-----------------------------------------------
>>>>>>    plot = gsn_csm_xy(wks, times&time, t1, resT1)
>>>>>>
>>>>>> end
>>>>>>
>>>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20210205/8d59cc99/attachment.html>


More information about the ncl-talk mailing list