----------
X-Sun-Data-Type: text
X-Sun-Data-Description: text
X-Sun-Data-Name: text
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 60

Matt Jeglum,

I'm a little surprised that you have NetCDF files for the NCEP-NCAR
Reanalysis 1 data set that need a PDEF statement, or a Data Descriptor
File (DDF, often suffixed with .ctl) at all.  You should be able to
just do "sdfopen /path/to/file.nc" on each one.  The ones I made which
are available from ftp.cdc.noaa.gov don't require more than "sdfopen".

To do templating, it matters what version of GrADS you have.  In version 1.8,
you could do:
        sdfopen /path/to/first_file.nc TEMPLATE #of_time_steps
where "TEMPLATE" could either be a full path ending in a template, or
just the filename portion of the template (E.G., "air.%y4.nc").  The
final argument is the number of time steps represented in the template.
In version 1.9, the syntax was changed to require the TEMPLATE argument
be a full path.  In GrADS 2, it was decided that templating should
only occur in DDFs (be they for "xdfopen" or be they for "open").
I wrote a GrADS script called "sdfopent" that restores the syntax
from 1.8.  It works in both 1.9 and 2, but not in 1.8.  Ironically,
it requires a scripting function that was introduced in 1.9.  I
have attached this script.  Basically, it writes a DDF for your
for use with "xdfopen" (which means the DDF is shorter), and then
runs "xdfopen" on it.

-Hoop

> From owner-GRADSUSR@LIST.CINECA.IT  Tue Sep 16 16:05:19 2008
> Date:     Wed, 17 Sep 2008 00:04:16 +0200
> Reply-To: GRADSUSR@LIST.CINECA.IT
> From: Automatic digest processor <LISTSERV@LIST.CINECA.IT>
> Subject:  GRADSUSR Digest - 15 Sep 2008 to 16 Sep 2008 (#2008-253)
> To: Recipients of GRADSUSR digests <GRADSUSR@LIST.CINECA.IT>
>
> There are 3 messages totalling 311 lines in this issue.
>
> Topics of the day:
>
>   1. floor function for grads
>   2. google map tile creation & cutting with Gr
>   3. NCEP Reanalysis netCDF in traj
>
> ----------------------------------------------------------------------
>
> ------=_Part_9653_12785105.1221535368007--
>
> ------------------------------
>
> Date:    Tue, 16 Sep 2008 19:16:52 +0200
> From:    Matt Jeglum <m.jeglum@UTAH.EDU>
> Subject: NCEP Reanalysis netCDF in traj
>
> Hi all,
> I am attempting to use netCDF files from the NCEP/NCAR reanalysis to make trajectories with a
> modified version of traj.gs.  My impression is that I need to create a templted control file to make this
> work.  My main issue is with creating the control file, and in particular finding the necessary
> variables using an ncdump -h  to specifiy pdef projection, reference points and reference lat/lons.
> Any help would be appreciated.
> ------------------------------
> End of GRADSUSR Digest - 15 Sep 2008 to 16 Sep 2008 (#2008-253)
> ***************************************************************
----------
X-Sun-Data-Type: default
X-Sun-Data-Description: default
X-Sun-Data-Name: sdfopent.gs
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 226

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 format (E.G., 0z01jan1979)
* 5 tstep = size of time step (E.G., 6hr, 1dy, 1mo)
* 6 DDFname = filename of DDF file that this script writes for you
*****************************************************************************

**Parse command line args
dset = subwrd(args,1)
templ = subwrd(args,2)
ntimes = subwrd(args,3)
# time1 = subwrd(args,4)
# tstep = subwrd(args,5)
# out = subwrd(args,6)
out = subwrd(args,4)
if(out='')
out='out.ddf'
    say 'Defaulting optional DDF arg to out.ddf'
endif

**warn if args are blank
if (dset = '' | templ = '' | ntimes = '')
say 'Must include following args:'
say 'sdfopent netCDFpath Template NumTimes [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 '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

lastslsh=rindex(dset,'/')
dsetlen=strlen(dset)
fnamelen=dsetlen-lastslsh
fname=substr(dset,lastslsh+1,fnamelen)
dirname=substr(dset,1,lastslsh)
templchr1=substr(templ,1,1)
if (templchr1='/')
templtln=templ
else
templtln=dirname%templ
endif
'sdfopen 'dset
line2=sublin(result,2)
filenum=subwrd(line2,8)
'q dims'
line5=sublin(result,5)
time1=subwrd(line5,6)
'set t 1 2'
ymdh1=subwrd(result,4)
ymdh2=subwrd(result,5)
'close 'filenum

* parse first time
tempstr=ymdh1
* say 'Parsing 'tempstr'.'
templen=strlen(tempstr)
lastcolon=rindex(tempstr,':')
* say 'Length='templen'.'
coloncol=index(tempstr,':')
* say 'First colon found at position='coloncol'.'
yr1=substr(tempstr,1,coloncol-1)
say 'yr1='yr1'.'
tempstr=substr(tempstr,coloncol+1,templen-coloncol)
* say 'tempstr shrunk to='tempstr'.'
templen=templen - (strlen(yr1)+1)
* say 'New length='templen
coloncol=index(tempstr,':')
* say 'Next colon position='coloncol'.'
mon1=substr(tempstr,1,coloncol-1)
* say 'mon1='mon1'.'
tempstr=substr(tempstr,coloncol+1,templen-coloncol)
* say 'tempstr shrunk again to='tempstr'.'
templen=templen-(strlen(mon1)+1)
* say 'And now it has length='templen'.'
coloncol=index(tempstr,':')
* say 'Last colon position='coloncol'.'
dy1=substr(tempstr,1,coloncol-1)
* say 'dy1='dy1'.'
hr1=substr(tempstr,coloncol+1,templen-coloncol)
* say 'hr1='hr1'.'

* parse time2
tempstr=ymdh2
* say 'Parsing 'tempstr'.'
templen=strlen(tempstr)
* say 'Length='templen'.'
coloncol=index(tempstr,':')
* say 'First colon found at position='coloncol'.'
yr2=substr(tempstr,1,coloncol-1)
* say 'yr2='yr2'.'
tempstr=substr(tempstr,coloncol+1,templen-coloncol)
* say 'tempstr shrunk to='tempstr'.'
templen=templen - (strlen(yr2)+1)
* say 'New length='templen
coloncol=index(tempstr,':')
* say 'Next colon position='coloncol'.'
mon2=substr(tempstr,1,coloncol-1)
* say 'mon2='mon2'.'
tempstr=substr(tempstr,coloncol+1,templen-coloncol)
* say 'tempstr shrunk again to='tempstr'.'
templen=templen-(strlen(mon2)+1)
* say 'And now it has length='templen'.'
coloncol=index(tempstr,':')
* say 'Last colon position='coloncol'.'
dy2=substr(tempstr,1,coloncol-1)
* say 'dy2='dy2'.'
hr2=substr(tempstr,coloncol+1,templen-coloncol)
* say 'hr2='hr2'.'

if(yr1!=yr2)
    say 'Cannot handle differences in years'
    return
endif
deltamon=mon2-mon1
if (deltamon > 0)
    if (deltamon = 1)
tstep="1mo"
    else
        say 'Cannot handle differences in months greater than 1'
        return
    endif
else
deltady=dy2-dy1
    if (deltady > 0)
        if (deltady = 1)
tstep="1dy"
        else
            say 'Cannot handle differences in days greater than 1'
            return
        endif
    else
deltahr = hr2 - hr1
tstep=deltahr%'hr'
    endif
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

return

function index(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=1
while (i <= (biglen - ltllen + 1))
    newstr=substr(bigstr,i,ltllen)
* say 'newstr='newstr
    if (newstr=ltlstr)
        return(i)
    endif
    i=i+1
endwhile
return(0)

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)

