[ncl-talk] Error with date format
Rick Brownrigg
brownrig at ucar.edu
Sun Sep 1 08:15:55 MDT 2019
HI,
You might try the string formatting functions sprintf/sprinti; perhaps
something like:
ncl 0> y = 2019
ncl 1> m = 8
ncl 2> d = 31
ncl 3> ymd = sprinti("%04d",y) + "-" + sprinti("%02d",m) + "-" +
sprinti("%02d",d)
ncl 4> print(ymd)
Variable: ymd
Type: string
Total Size: 8 bytes
1 values
Number of Dimensions: 1
Dimensions and sizes: [1]
Coordinates:
(0) 2019-08-31
Rick
On Sun, Sep 1, 2019 at 2:02 AM Lyndz via ncl-talk <ncl-talk at ucar.edu> wrote:
> Dear NCL-experts,
>
> *Details:*
> I am converting dates with YYYYMMDD format to YYYY-MM-DD format (separated
> by a hyphen). For example, I have the following data *20140423*. If the
> month and day are both less than 10, a 0 should precede it. However, I
> encountered an error below:
>
> -------------------------------------------------------------------------------------------------------------
> *What I have so far:*
> I have the following function from a previous email:
>
> undef("add_hyphen")
> function add_hyphen(yyyymmdd[*]:integer)
> local yyyy, mmdd, mm, dd, a, b, c, d
> begin
> yyyy = yyyymmdd/10000
> mmdd = yyyymmdd-(yyyy*10000)
> mm = mmdd/100
>
> if(mm.lt.10)
> a = tostring(mm)
> b = 0+a
> else
> b = mm
> end if
>
> dd = mmdd-(mm*100)
> if(dd.lt.10)
> c = tostring(dd)
> d = 0+c
> else
> d = dd
> end if
> return(yyyy+"-"+b+"-"+d)
> end
>
> yymd = add_hyphen(ymd)
> print(yymd)
>
> -------------------------------------------------------------------------------------------------------------
> *Error:*
> fatal: Conditional statements (if and do while) require SCALAR logical
> values, see all and any functions
>
> -------------------------------------------------------------------------------------------------------------
> *What I want:*
> The input has no "units" so I can't use the cd_string. Any suggestions on
> how I can do this correctly in NCL?
> I'll appreciate any help.
>
>
> Sincerely,
> Lyndz
>
>
>
> _______________________________________________
> 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/20190901/7ed3d3d1/attachment.html>
More information about the ncl-talk
mailing list