how obtain the date from a NetCdf file

Matthias Fripp matthias.fripp at ECI.OX.AC.UK
Thu Nov 5 07:09:01 EST 2009


Sorry, I just noticed an error in how I formatted the numbers in my  
earlier reply. A corrected version of the script is below...

-Matthias

====== revised message ===========

To write out multiple columns of data, you would use something like  
this:

*write a header in the file
fname="output_file.csv"
rc = write(fname,"date,u,v,temp,hgt")
if (rc != 0)
   say "Error writing to file " fname
   return
endif

*find the time range set by the user
*you may need to adjust this depending on your version of grads
'query dims'
tmin = subwrd(result, 51)
tmax = subwrd(result, 53)

*loop through all times, writing out data
timestep = tmin
while timestep <= tmax
   'set t ' timestep
   'q time'
   datetime = subwrd(result, 3)

* get the data for this time step
* note: change all the "?_var" variables to match whatever is in your  
grib file

   'd u_var'
   u = subwrd(result,4)

   'd v_var'
   v = subwrd(result,4)

   'd temp_var'
   temp = subwrd(result,4)

   'd hgt_var'
   hgt = subwrd(result,4)

* compose a line of text and write it to the output file
   outstr = datetime ',' u ',' v ',' temp ',' hgt
   rc = write(fname,outstr,append)
   if (rc != 0)
     say "Error writing to file " fname
     break
   endif
   timestep = timestep + 1
endwhile
close(fname)
'set t ' tmin ' ' tmax

This script assumes you have previously selected a range of times with  
"set time" or "set t", and a single point with set lat, set lon, set  
x, set y, set z, etc. It can be extended pretty easily to loop across  
a range of x-y-z values if you want to do that instead.

If you are working with remote data (e.g., in gradsdods), it will  
probably be much faster if you use the "define" statement to read in  
large chunks of data at once, and then loop through them locally. In  
that case, you may also want to use 'q defval' to read numeric values  
directly from the defined variable into your script variables, and  
then use math_format() to convert the numeric values to text before  
composing the output string.

This creates a text file with commas between the values. I'm sure  
there's some way to specify tabs instead, but I'm not sure how.

I hope that helps.

Matthias

On Nov 5, 2009, at 9:13 AM, Javier Peña wrote:

> Thank you very much Matthias:
>
> It works!
> I have another question . I wish if I get several variables, these  
> appear in columns in the text file. Is this possible?
> I mean, I'd like a file.txt in which appears something similar to:
>
>    Fecha                   u             v              temp          
> hgt
> 00Z01JUN2009   -1.9065     0.225504     288.06        9385.06
> 12Z01JUN2009   1.02317    -1.33784      295.778      9385.06
> 00Z02JUN2009   -1.15094   -3.90728      290.077      9385.06
> 12Z02JUN2009   -0.65753  -1.19813       295.153      9385.06
> Thanks. Best  Regards:
>
> Javier Peña
>
> 2009/11/4 Matthias Fripp <matthias.fripp at eci.ox.ac.uk>
> you could use something like this:
>
> 'q time'
> datetime = subwrd(result, 3)
>
> That will give you the date and time in grads format, e.g.,  
> "00Z01JAN1979". If you want, you could go further, with something  
> like this:
>
> formatted_time = substr(datetime, 9, 4) '/' substr(datetime, 6, 3)  
> '/' substr(datetime, 4, 2) ' ' substr(datetime, 1, 2) ':00'
> say "formatted time: " formatted_time
>
> Unfortunately, I don't know any way to convert "JAN" to "01" within  
> grads. So you may need to use awk (or similar) to convert the dates  
> after-the-fact, in which case you might as well just write the  
> datetime directly to the file, and then convert that later.
>
> Matthias
>
> On Nov 4, 2009, at 4:26 PM, Javier Peña wrote:
>
>> Hello everyone,
>>
>> I have a netcdf file from which I can obtain any of the variables  
>> with the following script (for example to get u wind component):
>>
>> * takes the values of the component or in a column
>> 'reinit'
>> 'sdfopen prueba.nc'
>> 'set lat 41'
>> 'set lon 359'
>> 'set lev 1'
>> to = 1
>> * Write the variable to a file
>> * 'set gxout vector'
>> * 'set fwrite z: \ dummy.dat'
>> while (to <5)
>> 'set t' to
>> tmp =''
>> tmp = subwrd (rerult, 4)
>> 'd u', tmp = tmp%''subwrd (result, 4)
>> rrc = write ( 'c: \ prueba_c \ pruebas_archivo_ecmwf \ new_u.txt',  
>> tmp)
>> to = to + 1
>> endwhile
>>
>> but my question is, can I get the date which belongs each variable  
>> value? That is, if I have values for the velocity component u: 2.3,  
>> 3.4, 5.6, 3.9, can I get a. txt what next?
>>
>> Date                         U (m / s)
>> 2009/12/06 00:00          2.3
>> 2009/12/06 06:00          3.4
>> 2009/12/06 18:00          5.6
>> 2009/12/07 00:00          3.9
>>
>> Thank you very much. Best regards.
>>
>> Javier Peña
>
>
>
>
> -- 
> Javier Peña Álvarez
> Energías Renovables
> javier.p at ereda.com
> EREDA
>
>                            Tel:  +34 91 5014755
>                            Fax: +34 91 5014756
>                  c/ Téllez 26, 28007 MADRID
>                                    www.ereda.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://gradsusr.org/pipermail/gradsusr/attachments/20091105/d113456d/attachment.html 


More information about the gradsusr mailing list