[gradsusr] How to use template option for .nc file (there is no .ctl, file)

Hoop hoop at colorado.edu
Fri Apr 8 00:27:40 EDT 2016


Mano,

Here's another alternative.  I've written a different version of
sdfopent.gs, called sdfopent2.gs, for the one time step per file
case.  It takes more arguments:

sdfopent netCDFpath Template NumTimes FirstTime TimeStepSize [DDF]
Where:
netCDFpath is full path to netcdf file
Template is the template (full path or relative)
NumTimes is the number of timesteps in the netCDF file
FirstTime is the first time value; E.G., 00z01Jan1979
TimeStepSize is the size of the time step, E.G., 1hr or 1dy or 1mo
DDF is optional name of DDF file created.

I've attached this script.

-Hoop

On 04/07/16 18:13, lpasmanoranjan wrote:
> Dear Hoop,
> Thank you very much for providing a possible solution. I will go into it
> and tell you.
>
> I had tried a lot by changing script and writing descriptor file etc..
> But could not solve that. But I had thought to convert each .nc file to
> grads form.
>
> I will come back here soon.
>
> Thanks and kind regards,
>
> On Friday 8 April 2016, Hoop <hoop at colorado.edu> wrote:
>
>     I've not heard back from Mano, but an outside-of-GrADS solution
>     has occurred to me, although I'm unsure of its practicality.  One
>     could use the ncrcat command from the NetCDF operators (nco.sf.net)
>     to combine the files into one for GrADS.
>
>     -Hoop
>
>     On 04/05/16 00:08, Hoop wrote:
>
>         Mano,
>
>         I'm not sure, but I think it might be the one time step in each file
>         thing.  If I recall correctly (and it hasn't changed), GrADS kind of
>         depends (at least, in the sdfopen case) on there being at least two
>         time steps in each file.  It reads (or read, at least) the first two
>         time steps in a file to learn what time step size is.  But, there's
>         hope, even it turns out I'm right about this point.
>
>         So, the sdfopent.gs script wrote a DDF
>         (out.ddf, apparently).  We
>         just need to make sure the TDEF line in that file is correct.
>         Please
>         share that part of out.ddf, and I may need to suggest a modification
>         to it.  If that's the case, then you could just re-start GrADS after
>         modifying it, and then entering "xdfopen out.ddf" at the initial
>         GrADS prompt.
>
>         Thanks,
>         -Hoop
>
>         On 04/04/16 21:09, lpasmanoranjan wrote:
>
>             Dear Hoop,
>             Thank you so much for your prompt reply. I did a mistake
>             when I copied
>             to gmail. But the message was same.  ncdump gives the following
>
>             ga-> !ncdump -h  /home/data/pre/1995/01/pre_1995_01_01_00.nc
>             netcdf pre_1995_01_01_00.nc {
>             dimensions:
>                   LON = 3223 ;
>                   LAT = 4943 ;
>                   TIME = 1 ;
>             variables:
>                   double LON(LON) ;
>                       LON:units = "degrees_east" ;
>                       LON:long_name = "Longitude" ;
>                   double LAT(LAT) ;
>                       LAT:units = "degrees_north" ;
>                       LAT:long_name = "Latitude" ;
>                   double TIME(TIME) ;
>                       TIME:units = "seconds since 1970-01-01 00:00:00
>             +0:00" ;
>                       TIME:long_name = "Time" ;
>                   short PRE(TIME, LAT, LON) ;
>                       PRE:long_name = "60-minutes precipitation" ;
>                       PRE:units = "1e-3 meter" ;
>                       PRE:missing_value = -32768s ;
>                       PRE:scale_factor = 0.01f ;
>                       PRE:add_offset = 0.f ;
>
>
>             On 5 April 2016 at 11:48, Hoop <hoop at colorado.edu> wrote:
>
>                  Mano,
>
>                  I'm confused.  Why does the path to your first file
>             have a space inside
>                  of it?  You have:
>
>                  /home/data/pre/1995/01/ pre_1995_01_01.nc
>
>                  Shouldn't it be:
>
>                  /home/data/pre/1995/01/pre_1995_01_01.nc
>
>                  If that doesn't solve it, please send the output from
>             running
>                  "ncdump -ht " on the first file.  If you are not
>             familiar with
>                  ncdump, it is part of the standard NetCDF suite from
>             Unidata:
>
>             http://www.unidata.ucar.edu/software/netcdf/
>
>                  Thanks,
>                  -Hoop
>
>
>                  On 04/04/16 20:20, lpasmanoranjan wrote:
>
>                      Dear Hoop,
>                      Thank you very much for your reply. I tried it for
>             2 files after
>                      saving
>             sdfopent.gs in my scripts directory. But
>                      getting
>                      the following error.
>
>
>                      My data directory: /home/data/pre/1995/01/
>                      Number of Times: 2 (pre_1995_01_01.nc and
>             pre_1995_01_02.nc)
>                      (Kindly Note: each file has only one time data, so
>             I changed the
>                      deltahr
>                      = 1 in the sdfopent.gs script
>             at line 150)
>
>                      Now I used the following command and got the
>             subquent messages.
>
>                      ga-> sdfopent /home/data/pre/1995/01/
>             pre_1995_01_01.nc
>                      pre_1995_01_%h2.nc 2
>                      Defaulting optional DDF arg to out.ddf
>                      yr1=1995.
>                      netCDFfile= /home/data/pre/1995/01/
>             pre_1995_01_01.nc
>                      Template= pre_1995_01_%h2.nc
>                      NumTimes= 2
>                      Time1= 00Z01JAN1995
>                      Tstep= 1hr
>                      DDF= out.ddf
>                      gadsdf: Time dimension time is not an SDF dimension.
>                      SDF Descriptor file out.ddf was not successfully
>             opened & parsed.
>                      Scanning Descriptor File:  out.ddf
>                      gadsdf: Time dimension time is not an SDF dimension.
>                      SDF Descriptor file out.ddf was not successfully
>             opened & parsed.
>
>                      SET Error:  No files open yet
>                      SET Error:  No files open yet
>
>                      I must be doing some mistake, can anybody please
>             help me to sort
>                      this
>                      problem. Thank you so much.
>
>                      On 5 April 2016 at 02:23, Hoop <hoop at colorado.edu>
>             wrote:
>
>                           Mano,
>
>                           The standard script library:
>
>             http://cola.gmu.edu/grads/gadoc/library.html
>
>                           includes the script sdfopent.gs.  It
>                      takes two additional
>                           arguments beyond the one for sdfopen.  The
>             second argument
>                           is a template, like "pre_%y4_%m2_%d2.nc",
>                      and the
>                           third is
>                           a count of time steps in the entire series.
>             The first argument
>                           would be the first path in the time series.  See:
>
>             ftp://cola.gmu.edu/grads/scripts/sdfopent.gs
>
>                           -Hoop
>
>                           On 04/04/16 10:00, gradsusr-request at gradsusr.org>
>                      wrote:
>                            > Date: Mon, 4 Apr 2016 18:27:49 +0900
>                            > From: lpasmanoranjan <lpasmanoranjan at gmail.com>
>                            > Subject: [gradsusr] How to use template
>             option for .nc
>                      file (there is
>                            >       no      .ctl file)
>                            > To: GrADS Users Forum <gradsusr at gradsusr.org>
>                            >
>                            > Dear Users,
>                            > I am having a problem in viewing the
>             following dataset as
>                           template option.
>                            > I have hourly dataset in .nc as following:
>                            >
>                            > 1995/01/pre_1995_01_01.nc
>                            > 1995/01/pre_1995_01_02.nc
>                            > 1995/01/pre_1995_01_03.nc
>                            > 1995/01/pre_1995_01_04.nc
>                            > 1995/01/pre_1995_01_05.nc
>                            > 1995/01/pre_1995_01_06.nc
>                            > and so on for 10 years
>                            >
>                            > *All individual datasets can be viewed in
>             grads with
>                      sdfopen command*
>                            >
>                            > Can anybody please help me how to use
>             template option.
>                            >
>                            > Thank you for any kind of help or
>             suggestion in this regard.
>                            >
>                            > Kind Regards
>                            > Mano
>                           _______________________________________________
>                           gradsusr mailing list
>             gradsusr at gradsusr.org
>             http://gradsusr.org/mailman/listinfo/gradsusr
>                      --
>                      Kind Regards,
>                      Mano
>             --
>             Kind Regards,
>             Mano
>             --
>             Kind Regards,
>             Mano
> --
> Kind Regards,
> Mano
-------------- next part --------------
function sdfopent(args)
* This function simulates the old 3-arg sdfopen for templating, but needs 5.
* It is based on narropen.gs, originally by Jamie Scott, and heavily
* modified by me. -Hoop.   2008/02/14
*
**args are:
* 1 dset = full path of netCDF data file
* 2 the template, full path or just the filename portion
* 3 ntimes = number of timesteps in the time series
* 4 time1 = first time in GrADS standard format HHzDDMonYYYY
* 5 tstep = time step size in GrADS standard format Nhr/Ndy/Nmo
* 6 DDFname = filename of DDF file that this script writes for you (optional)
*****************************************************************************

**Parse command line args  
dset = subwrd(args,1)
templ = subwrd(args,2)
ntimes = subwrd(args,3)
time1 = subwrd(args,4)
tstep = subwrd(args,5)
gradsvers=getvers()
* say 'gradsvers='gradsvers'.'
if(gradsvers="v1.8")
* short-circuit to the built-in, which this script emulates, 'cuz no strlen()
'sdfopen ' dset ' ' templ ' ' ntimes
else
out = subwrd(args,6)
if(out='')
out='out.ddf'
   say 'Defaulting optional DDF arg to out.ddf'
endif

**warn if args are blank
if (dset = '' | templ = '' | ntimes = '' | time1 = '' | tstep = '')
say 'Must include following args:'
say 'sdfopent netCDFpath Template NumTimes FirstTime TimeStepSize [DDF]'
say 'Where:'
say 'netCDFpath is full path to netcdf file'
say 'Template is the template (full path or relative)'
say 'NumTimes is the number of timesteps in the netCDF file'
say 'FirstTime is the first time value; E.G., 00z01Jan1979'
say 'TimeStepSize is the size of the time step, E.G., 1hr or 1dy or 1mo'
say 'DDF is optional name of DDF file created.'
* say 'Time1 is the first time in GrADS format (E.G., 0z01Jan1979)'
* say 'Tstep is the size of a time step (E.G., 6hr, 1dy, 1mo)'
* say 'DDFname is the filename of the DDF file that this script writes for you'
exit
endif

* Remember, the path args may not have slashes
lastslsh=rindex(dset,'/')
dsetlen=strlen(dset)
fnamelen=dsetlen-lastslsh
fname=substr(dset,lastslsh+1,fnamelen)
if (lastslsh=0)
dirname=''
else
dirname=substr(dset,1,lastslsh)
endif
templchr1=substr(templ,1,1)
if (templchr1='/')
templtln=templ
else
templtln=dirname%templ
endif
* say 'Got dset='dset', templ='templ', ntimes='ntimes', time1='time1',
* tstep='tstep', DDFname='out'.'

say 'netCDFfile= 'dset
say 'Template= 'templ
say 'NumTimes= 'ntimes
say 'Time1= 'time1
say 'Tstep= 'tstep
say 'DDF= 'out

**construct default ddf*
line = 'DSET 'templtln
line2= 'options template'
* problem - xdfopen requires full tdef line when templating, so 2 more args
line3 ='tdef time 'ntimes' linear 'time1' 'tstep

*write out ddf
rc = write(out,line)
rc = write(out,line2)
rc = write(out,line3)
rc = close(out)

*open ddf
'xdfopen 'out
say result
'set t 1'
say result
endif
* This endif is from the beginning of the script where we short-circuit
* to the built-in sdfopen command if we have version 1.8, as 1.8 doesn't
* have "strlen".  This is OK, because this script is meant to restore the
* functionality in the 1.8 version of sdfopen.
return

function rindex(arg1, arg2)
bigstr=subwrd(arg1,1)
ltlstr=subwrd(arg2,1)
* say 'bigstr='bigstr' , ltlstr='ltlstr'.'
biglen=strlen(bigstr)
ltllen=strlen(ltlstr)
* say 'biglen='biglen
* say 'ltllen='ltllen
if(ltllen < 1)
   return(0)
endif
if(ltllen > biglen)
   return(0)
endif
i=(biglen -ltllen + 1)
while (i >= 1)
   newstr=substr(bigstr,i,ltllen)
* say 'newstr='newstr
   if (newstr=ltlstr)
       return(i)
   endif
   i=i-1
endwhile
return(0)

function getvers(args)
'q config'
line1=sublin(result,1)
word2l1=subwrd(line1,2)
tempstr=substr(word2l1,1,4)
return(tempstr)


More information about the gradsusr mailing list