ascii

Eric DeWeaver deweaver at AOS.WISC.EDU
Thu Feb 3 13:06:33 EST 2005


Hi Mike,

One way to convert ascii data into a grads-readable binary file is
to use a perl script.  I'm sending you a script that I use
to read an ascii file with monthly NAO index values.

This is the beginning of Jim Hurrell's ascii NAO index, as it
appears on the html page, http://www.cgd.ucar.edu/~jhurrell/nao.html.

========== Copy this text to a file called nao.txt ================

Monthly index of the NAO based on the difference of normalized sea
level pressures (SLP) between Ponta Delgada, Azores and
Stykkisholmur/Reykjavik, Iceland since 1865.
</p>
<P>
<PRE>
        J     F     M     A     M     J     J     A     S     O     N     D

1865  -0.5  -1.6   0.0   0.0   0.1   0.1   0.0   1.3   3.1  -3.2  -0.9   1.5
1866   0.8   0.9  -0.9  -2.6  -2.3   1.6  -1.1   0.0   3.6  -0.2  -0.5   0.6
1867  -4.7   1.4  -5.1   2.1  -5.4  -0.2  -3.6   3.0   2.0   2.8  -4.7  -0.2
1868   0.9   3.8   4.4   2.1   3.6   4.5   1.8   1.8  -4.4   4.6  -2.7   0.6
1869   2.0   3.1  -0.3  -0.2  -3.6  -2.4  -0.1  -1.9  -0.5  -2.4   1.4  -0.3

============= End Copy ============================================

Here's a perl script that can read these numbers and write them out to a
direct access binary file in grads-readable form:

========== copy this text to a file nao.pl =======================

#!/usr/bin/perl
while(<STDIN>){
  if (/^\s*(\d\d\d\d)\s*(.*)$/){
    @year = split(/\s+/, $2);
    foreach(@year){ print pack("f",$_) }
#   foreach(@year){ print " ",$_," " }  <- uncomment to check data
  }
}

============= End Copy ============================================

The perl script assumes that each line in the ascii file begins with
a 4-digit year (\d\d\d\d means 4 digits), and that the data values
are space delimited.  The script will skip over any lines that don't
begin with a 4-digit number, including the "J F M ..." line at the
beginning.  "pack" means convert to binary.

After doing "chmod u+x nao.pl" you can do

./nao.pl < nao.txt > nao.dat

Then you need a grads control file:

============== copy this text to a file nao.ctl =================

DSET    ^nao.dat
UNDEF   -999.
Title Iceland/Azores NAO index from www.cgd.ucar.edu/~jhurrell/nao.html
XDEF 1  LINEAR  0 5.0
YDEF 1 LINEAR   -90 2.5
ZDEF 1 LINEAR 1 1000
TDEF 10000 LINEAR  JAN1865 1MO
VARS 1
nao  0    99  nao index
ENDVARS

============= End Copy ============================================

Hopefully this will work for you.  At least it works for me.

Good luck,

Eric





On Thu, 3 Feb 2005, Jennifer Adams wrote:

> On Feb 3, 2005, at 12:07 PM, Mike Notaro wrote:
>
> > Thanks Jean Pierre.  I wrote a script containing the following
> > to read a simple ascii formatted dataset:
> >
> > ret = read('/tmp/file.dat')
> > date = subwrd(ret , 1)
> > var1 = subwrd(ret , 2)
> > var2 = subwrd(ret , 3)
> >
> > and when I tried "exec script.gs", I get an error
> > that says "Define error: No file open yet. Exec error..."
> >
> > What am I doing wrong?
>
> Mike,
> GrADS can read ascii data into a script and create script variables,
> but it doesn't read ascii data as GrADS data variables that you can
> display and analyze. You'll have to write a C or Fortran program to
> read your ascii data and rewrite it as a gridded binary file that you
> can describe with a GrADS descriptor file.
>
> > My other question is if it is possible to read a netcdf file containing
> > variable(time), without lat or lon.  I get the error regarding the
> > absence of a discernable X coordinate.
>
> You'll have to write a descriptor file to read this kind of netcdf
> file. There are two types -- a simpler one to use with 'xdfopen'
> command, or a complete descriptor with 'dtype netcdf' that is used with
> the 'open' command. Take a look at the documentation:
> http://grads.iges.org/grads/gadoc/gradcomdxdfopen.html
> http://grads.iges.org/grads/gadoc/descriptorfile.html
>
> Jennifer
>
> --
> Jennifer Miletta Adams
> IGES/COLA
> 4041 Powder Mill Rd., Suite 302
> Calverton, MD 20705
>



More information about the gradsusr mailing list