[ncl-talk] Calculating date after/before specified number of days of given date

Walter Kolczynski walter.kolczynski at noaa.gov
Wed May 23 01:16:52 MDT 2018


Dipti,

Convert the year-month-day to a single calendar unit with 
cd_inv_calendar 
<http://www.ncl.ucar.edu/Document/Functions/Built-in/cd_inv_calendar.shtml> 
(I usually use Unix epoch-seconds, but NCL is flexible), do the math in 
that space, and then use that to make a string (using cd_string 
<http://www.ncl.ucar.edu/Document/Functions/User_contributed/cd_string.shtml>) 
or recover the year-month-day as separate variables using cd_calendar 
<http://www.ncl.ucar.edu/Document/Functions/Built-in/cd_calendar.shtml>:

load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/cd_string.ncl"

start_year    = 2018
start_month   = 1
start_day     = 5
delta_days    = -7

start_time    = cd_inv_calendar( start_year, start_month, start_day, 0, 
0, 0, "seconds since 1970-1-1 00:00:00", 0 )
delta         = delta_days * 86400             ; Convert delta to 
seconds, since our calendar units are seconds
end_time      = start_time + delta
copy_VarAtts(start_time, end_time)             ; Copy calendar 
information to end_time
end_string    = cd_string(end_time, "%Y-%N-%D")
end_time_UT   = cd_calendar(end_time, -5)      ; -5 gives integer values 
instead of float
end_year      = end_time_UT(:,0)
end_month     = end_time_UT(:,1)
end_day       = end_time_UT(:,2)

print(end_string)
print(end_year)
print(end_month)
print(end_day)

**** OUTPUT ****

Variable: end_string
Type: string
Total Size: 8 bytes
             1 values
Number of Dimensions: 1
Dimensions and sizes:   [1]
Coordinates:
Number Of Attributes: 1
   _FillValue :  missing
(0)     2017-12-29

Variable: end_year
Type: integer
Total Size: 4 bytes
             1 values
Number of Dimensions: 1
Dimensions and sizes:   [1]
Coordinates:
Number Of Attributes: 1
   calendar :    standard
(0)     2017

Variable: end_month
Type: integer
Total Size: 4 bytes
             1 values
Number of Dimensions: 1
Dimensions and sizes:   [1]
Coordinates:
Number Of Attributes: 1
   calendar :    standard
(0)     12

Variable: end_day
Type: integer
Total Size: 4 bytes
             1 values
Number of Dimensions: 1
Dimensions and sizes:   [1]
Coordinates:
Number Of Attributes: 1
   calendar :    standard
(0)     29

- Walter


On 2018-05-23 02:44, Dipti Hingmire wrote:
> Dear All,
>
> I wan to plot lead lag composite plots of a set of events/dates.
>
> Is there any inbuilt function in ncl which will provide me date 
> after/before specified number of days of given date?
>
>
> e.g. If i give 2018-01-05 as input date and I want the date 7 days 
> before I should get 2017-12-29
>
> Thanks and Regards,
> Dipti
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180523/99e05035/attachment.html>


More information about the ncl-talk mailing list