Re: convert date string,

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Thu Jan 30 2014 - 10:40:37 MST

On Jan 27, 2014, at 4:13 AM, Xi Chang <xi.chang01@gmail.com> wrote:

> I have the output of my codes in *txt something like this:
>
> 17 Jan 1960
> 28 Jan 1963
> 16 Dec 1965
> 23 Feb 1966
> 7 Jan 1968
> 28 Nov 1968
> 13 Mar 1969
>
> anybody know, what's the appropiate ncl function to convert these date
> to the form YYYYMMDD, thus the output will be:
>
> 19600117
> 19630128
> …

There's no single function for this.

You can use the str_xxx functions to parse the individual elements, and sprinti to format them into a new format:

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

;---Unadvertised function that returns array (/"Jan","Feb",.../)
  mname = month_name(0)

;---Time values to convert
  time = (/"17 Jan 1960","28 Jan 1963","16 Dec 1965","23 Feb 1966",\
            " 7 Jan 1968","28 Nov 1968","13 Mar 1969"/)
  ntime = dimsizes(time)

;---Arrays to hold time elements
  newtime = new(ntime,integer)

  do i=0,ntime-1
    day = toint(str_get_field(time(i),1," "))
    smon = str_get_field(time(i),2," ")
    year = str_get_field(time(i),3," ")
    mon = ind(smon.eq.mname)+1 ; 1-12
;
; Convert to YYYYMMDD. Use "%02i" to force a leading '0'
; for month and day if needed.
;
    newtime(i) = toint(year + sprinti("%02i",mon) + sprinti("%02i",day))
  end do

  print(time + " = " + newtime)

Note: the above is not the only way to do it. You could have also used "str_split" to split each string into three elements, instead of using str_get_field.

--Mary

>
>
> thanks a lot.
> Chang
> _______________________________________________
> 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 Thu Jan 30 10:40:48 2014

This archive was generated by hypermail 2.1.8 : Fri Feb 07 2014 - 16:39:11 MST