Re: Given a string time stamp need to add hours (donna Cote)

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Fri Feb 11 2011 - 09:16:27 MST

I don't know if I'm missing something here, but I believe you could use str_sub_str.
You will need to have at least version 5.1.1:

timestring = "2010-06-01T00:00"
nhours = 12

; Nicely format hour string. Prepend "T", postpend ":"
hstr = "T" + sprinti("%02i:",nhours)

; Construct new string
new_timestring = str_sub_str(timestring,"T",hstr)
print(new_timestring)

On Feb 10, 2011, at 4:22 PM, Peter Q. Olsson wrote:

> All-
>
> Re: Given a string time stamp need to add hours (donna Cote)
>
> Sounds like Donna got her immediate problem solved (see below) though I am not sure what
> the solution was. But the general issue of strings in NCL is an ongoing one, what with
> the very limited string-handling capabilities that NCL currently possesses. (No new-line
> character sequence...\n what is up with that?)
>
> So... I have found that it is MUCH cleaner to incorporate a tool-box set that has what I
> need: perl used from systemfunc() in NCL. Getting the raw materials out of NCL is often
> the hardest part. (Of course, this is easy for me to say, since I having been using perl
> for 15 years...). It is possible, but painful, to get your perl program imbedded in the
> NCL code. It is MUCH EASIER to write a small script external to NCL and call it through
> systemfunc. Just don't lose the perl script! ( With short scripts I will often embed
> them as a comment in the NCL script, just to be sure.
>
> I have addressed Donna's problem with strings and times below using both the internal
> and eternal method of scripting. You can see by comparing the "perl_command" string with
> the commented script that a lot of coercing is necessary to get by the NCL parser. The
> big problem is with double quotes ("), loved by perl and coveted closely by NCL. It took
> about 3 minutes the write the perl code snippet and a couple of hours to find the
> correct approach to get NCL to parse the string. And of course the "new
> line"/systemfunc() issue. I am sure that it would be much faster next time.
>
> Finally, I will point out that perl, through its extensive line of add-on packages
> available through CPAN (www.cpan.org), offers a huge diversity of easy-to-use tools for
> dealing with times: time objects, time/date strings, computing new times/elapsed times/
> old times... My favorite is the Date::Calc module.
>
> Here is the script:
>
> ; stringer.ncl <-------------------- An example of mixing ncl and perl via system calls
>
> timestring = "2010-06-01T00:00"
> time = 12
> nl = str_get_nl;
>
> perl_command = "perl -e '$time=sprintf qw(%02d),$ARGV[1];"+\
> "$ARGV[0]=~s/T00:/T$time:/;"+\
> "print $ARGV[0].qq/:00\n/'"
>
> print (perl_command) ; see comand as the shell will
>
> new_time_string = systemfunc(perl_command+" "+timestring+" "+time)
> print(new_time_string)
>
> ;;; MUCH CLEANER TO DO IT IMO
>
> ; #!/usr/local/bin/perl # <----------------- path might need to be adjusted to point to
> YOUR perl
>
> ; $time = sprintf "%02d",$ARGV[1]; # <-- make sure that value from 2nd command-line arg
> becomes
> ; # a zero-padded 2 digit integer
>
> ; $ARGV[0] =~ s/T00:/T$time:/; # <-- substitute pattern "T00:" with "T$time:".$time
> = 12 in this example
> ; print "$ARGV[0]:00\n"; # <-- print the desired string format with new time
> and ":00" (seconds)
> ; # column added
> ;
> ; # NOTE: AN IRRITATING FEATURE OF systemfunc() IS THAT THE RETURNED TEXT FROM THE
> SYSTEM CALL YOU ARE
> ; # MAKING MUST END IN A NEW-LINE, OR ELSE NOTHING IS RETURNED. SO... YOU HAVE TO STRIP
> THE NEW-LINE FROM
> ; # "new_time_string"/"another_time_string" BEFORE THEY CAN BE USED. Grr.
>
> ;;;;;;;;;; Put the above 4 lines of code in a file, say, "stringer.pl", make it
> executable, and then use
> ;;;;;;;;;;
>
> another_time_string = systemfunc("./stringer.pl " + timestring +" "+time);
> print(another_time_string)
>
> ;;;;;;;; So... the systemfunc call is similar, BUT, you do not need the hideous
> perl_command that
> ;;;;;;;; needs to be jiggered horribly to avoid offending quotes and double qoutes.
> ;;;;;;;;
> ;;;;;;;;Besides, by keeping your NCL code clean by externalzing the perl code, it is
> MUCH easier to maintain.
> ;;;;;;;; Also, the little external sript can provide a good jumping-off point when you
> inevitably develop
> ;;;;;;;; greater epectations for your time stamps.
>
> ;;;;;;; Many will argue that the awk/sed tool box can do this more cleanly... I used to
> myself.
> ;;;;;;; That may be true, but perl is SO MUCH MORE extensible than those tools. And the
> slightly greater
> ;;;;;;; overhead is negligible these days.
>
> ;;;;;;; Just don't misplace that perl code!
>
> Message: 1
> Date: Wed, 09 Feb 2011 14:41:18 -0600
> From: donna Cote <d-cote@tamu.edu>
> Subject: Re: Given a string time stamp need to add hours
> To: ncl-talk@ucar.edu
> Message-ID: <4D52FBEE.3060406@tamu.edu>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Carl, thanks a bunch! I don't yet have 5.2.0, or latest, so I don't have
> ut_string. Still, I don't need it and have this script working now!
>
> I'm so happy! -- Donna
>
> On 2/9/11 11:16 AM, Carl Schreck wrote:
>> There may be a simpler way, but you can do it with ut_inv_calendar and
>> ut_string. See the attached example.
>>
>> Hope you keep the sleet down there. We've had more than our fair share
>> of winter here in NC!
>>
>> Cheers,
>> Carl
>>
>> On Wed, Feb 9, 2011 at 11:45 AM, donna Cote <d-cote@tamu.edu
>> <mailto:d-cote@tamu.edu>> wrote:
>>
>> I could use help with this. It's sleeting outside and my brain seems to
>> be just about as frozen as my car's windshield!
>>
>> I have a time stamp, in a string:
>>
>> Variable: timestring
>> Type: string
>> Total Size: 4 bytes
>> 1 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [1]
>> Coordinates:
>> 2010-06-01T00:00
>>
>> And I have a number of hours, which, in this example = 12
>>
>> (0) Variable Name: Time
>> (0) 1 Dimensions:
>> (0) 0) Time: 1
>> (0) long_name: Elapse Time
>> (0) units: hour
>> (0) FORTRAN_format: i8
>> (0) start: 2010-06-01 00:00:00
>> (0) FORM: YYYY-MM-DD hh:mm:ss
>>
>> How do I get a timestamp created that will give me
>> 2010-06-01T12:00:00 (timestring + Time(0)) ??
>>
>> Thanks, Donna
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>>
>>
>> --
>> Carl J. Schreck III, PhD
>> Postdoctoral Research Associate
>> Cooperative Institute for Climate and Satellites (CICS-NC)
>> NOAA's National Climatic Data Center
>> 151 Patton Avenue
>> Asheville, NC 28801
>> Tel: 828-257-3140
>> carl.schreck@noaa.gov <mailto:carl.schreck@noaa.gov>
>> http://www.atmos.albany.edu/student/carl/
>
>
> ___________________________________________________________
> |
> | Dr. Peter Q. Olsson,
> | Alaska State Climatologist, Alaska State Climate Center
> | Chief Scientist, Alaska Experimental Forecast Facility
> | University of Alaska Anchorage
> | 2811 Merrill Field Drive
> | Anchorage, AK 99501
> | voice: (907) 786-7234, fax (907) 786-7237|
>
>
> From: "Peter Q. Olsson" <olsson@aeff.uaa.alaska.edu>
> Date: February 10, 2011 3:15:47 PM MST
> To: ncar-talk@ucar.edu, d-cote@tamu.edu
> Subject: Given a string time stamp need to add hours (donna Cote)
> Reply-To: olsson@aeff.uaa.alaska.edu
>
>
> All-
>
> Re: Given a string time stamp need to add hours (donna Cote)
>
> Sounds like Donna got her immediate problem solved (see below) though I am not sure what
> the solution was. But the general issue of strings in NCL is an ongoing one, what with
> the very limited string-handling capabilities that NCL currently possesses. (No new-line
> character sequence...\n what is up with that?)
>
> So... I have found that it is MUCH cleaner to incorporate a tool-box set that has what I
> need: perl used from systemfunc() in NCL. Getting the raw materials out of NCL is often
> the hardest part. (Of course, this is easy for me to say, since I having been using perl
> for 15 years...). It is possible, but painful, to get your perl program imbedded in the
> NCL code. It is MUCH EASIER to write a small script external to NCL and call it through
> systemfunc. Just don't lose the perl script! ( With short scripts I will often embed
> them as a comment in the NCL script, just to be sure.
>
> I have addressed Donna's problem with strings and times below using both the internal
> and eternal method of scripting. You can see by comparing the "perl_command" string with
> the commented script that a lot of coercing is necessary to get by the NCL parser. The
> big problem is with double quotes ("), loved by perl and coveted closely by NCL. It took
> about 3 minutes the write the perl code snippet and a couple of hours to find the
> correct approach to get NCL to parse the string. And of course the "new
> line"/systemfunc() issue. I am sure that it would be much faster next time.
>
> Finally, I will point out that perl, through its extensive line of add-on packages
> available through CPAN (www.cpan.org), offers a huge diversity of easy-to-use tools for
> dealing with times: time objects, time/date strings, computing new times/elapsed times/
> old times... My favorite is the Date::Calc module.
>
> Here is the script:
>
> ; stringer.ncl <-------------------- An example of mixing ncl and perl via system calls
>
> timestring = "2010-06-01T00:00"
> time = 12
> nl = str_get_nl;
>
> perl_command = "perl -e '$time=sprintf qw(%02d),$ARGV[1];"+\
> "$ARGV[0]=~s/T00:/T$time:/;"+\
> "print $ARGV[0].qq/:00\n/'"
>
> print (perl_command) ; see comand as the shell will
>
> new_time_string = systemfunc(perl_command+" "+timestring+" "+time)
> print(new_time_string)
>
> ;;; MUCH CLEANER TO DO IT IMO
>
>
> ; #!/usr/local/bin/perl # <----------------- path might need to be adjusted to point to
> YOUR perl
>
> ; $time = sprintf "%02d",$ARGV[1]; # <-- make sure that value from 2nd command-line arg
> becomes
> ; # a zero-padded 2 digit integer
>
> ; $ARGV[0] =~ s/T00:/T$time:/; # <-- substitute pattern "T00:" with "T$time:".$time
> = 12 in this example
> ; print "$ARGV[0]:00\n"; # <-- print the desired string format with new time
> and ":00" (seconds)
> ; # column added
> ;
> ; # NOTE: AN IRRITATING FEATURE OF systemfunc() IS THAT THE RETURNED TEXT FROM THE
> SYSTEM CALL YOU ARE
> ; # MAKING MUST END IN A NEW-LINE, OR ELSE NOTHING IS RETURNED. SO... YOU HAVE TO STRIP
> THE NEW-LINE FROM
> ; # "new_time_string"/"another_time_string" BEFORE THEY CAN BE USED. Grr.
>
> ;;;;;;;;;; Put the above 4 lines of code in a file, say, "stringer.pl", make it
> executable, and then use
> ;;;;;;;;;;
>
> another_time_string = systemfunc("./stringer.pl " + timestring +" "+time);
> print(another_time_string)
>
>
> ;;;;;;;; So... the systemfunc call is similar, BUT, you do not need the hideous
> perl_command that
> ;;;;;;;; needs to be jiggered horribly to avoid offending quotes and double qoutes.
> ;;;;;;;;
> ;;;;;;;;Besides, by keeping your NCL code clean by externalzing the perl code, it is
> MUCH easier to maintain.
> ;;;;;;;; Also, the little external sript can provide a good jumping-off point when you
> inevitably develop
> ;;;;;;;; greater epectations for your time stamps.
>
> ;;;;;;; Many will argue that the awk/sed tool box can do this more cleanly... I used to
> myself.
> ;;;;;;; That may be true, but perl is SO MUCH MORE extensible than those tools. And the
> slightly greater
> ;;;;;;; overhead is negligible these days.
>
> ;;;;;;; Just don't misplace that perl code!
>
>
>
>
>
>
> Message: 1
> Date: Wed, 09 Feb 2011 14:41:18 -0600
> From: donna Cote <d-cote@tamu.edu>
> Subject: Re: Given a string time stamp need to add hours
> To: ncl-talk@ucar.edu
> Message-ID: <4D52FBEE.3060406@tamu.edu>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Carl, thanks a bunch! I don't yet have 5.2.0, or latest, so I don't have
> ut_string. Still, I don't need it and have this script working now!
>
> I'm so happy! -- Donna
>
> On 2/9/11 11:16 AM, Carl Schreck wrote:
>> There may be a simpler way, but you can do it with ut_inv_calendar and
>> ut_string. See the attached example.
>>
>> Hope you keep the sleet down there. We've had more than our fair share
>> of winter here in NC!
>>
>> Cheers,
>> Carl
>>
>> On Wed, Feb 9, 2011 at 11:45 AM, donna Cote <d-cote@tamu.edu
>> <mailto:d-cote@tamu.edu>> wrote:
>>
>> I could use help with this. It's sleeting outside and my brain seems to
>> be just about as frozen as my car's windshield!
>>
>> I have a time stamp, in a string:
>>
>> Variable: timestring
>> Type: string
>> Total Size: 4 bytes
>> 1 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [1]
>> Coordinates:
>> 2010-06-01T00:00
>>
>> And I have a number of hours, which, in this example = 12
>>
>> (0) Variable Name: Time
>> (0) 1 Dimensions:
>> (0) 0) Time: 1
>> (0) long_name: Elapse Time
>> (0) units: hour
>> (0) FORTRAN_format: i8
>> (0) start: 2010-06-01 00:00:00
>> (0) FORM: YYYY-MM-DD hh:mm:ss
>>
>> How do I get a timestamp created that will give me
>> 2010-06-01T12:00:00 (timestring + Time(0)) ??
>>
>> Thanks, Donna
>>
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>
>>
>>
>> --
>> Carl J. Schreck III, PhD
>> Postdoctoral Research Associate
>> Cooperative Institute for Climate and Satellites (CICS-NC)
>> NOAA's National Climatic Data Center
>> 151 Patton Avenue
>> Asheville, NC 28801
>> Tel: 828-257-3140
>> carl.schreck@noaa.gov <mailto:carl.schreck@noaa.gov>
>> http://www.atmos.albany.edu/student/carl/
>
> ___________________________________________________________
> |
> | Dr. Peter Q. Olsson,
> | Alaska State Climatologist, Alaska State Climate Center
> | Chief Scientist, Alaska Experimental Forecast Facility
> | University of Alaska Anchorage
> | 2811 Merrill Field Drive
> | Anchorage, AK 99501
> | voice: (907) 786-7234, fax (907) 786-7237|
>
>
>
>
> _______________________________________________
> 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 Fri Feb 11 09:16:35 2011

This archive was generated by hypermail 2.1.8 : Fri Feb 11 2011 - 16:11:42 MST