example of reading a simple mixed ascii (text) file

From: Dennis Shea (shea AT XXXXXX)
Date: Thu Jun 17 2004 - 10:50:53 MDT


The following qustion has been asked several times so
I thought I would post a public answer

Consider a file (foo.ascii) that contains *mixed* type information
(here, integers and strings) in nice *consistent* columns:

       0 HTIC85 KWBC 121200 / 96 37 12 100 850 11
    4139 HTKC85 KWBC 121200 / 96 39 12 100 850 11
    8278 HTLC85 KWBC 121200 / 96 40 12 100 850 11
   12417 HTMC85 KWBC 121200 / 96 41 12 100 850 11
[SNIP]
36782845 HVLZ98 KWBK 121200 / 96 40 78 105 10 34
36786569 HTLZ98 KWBK 121200 / 96 40 2072078 105 2 15
36791155 HTLZ98 KWBK 121200 / 96 40 2072078 105 2 16
36795311 HTLZ98 KWBK 121200 / 96 40 78 105 2 11

===
Reading the header file could be accomplished several ways:
(1) asciiread [but it would require familiarity
               with how asciiread handles "mixed type"
               (ie, files with strings and integers)]
(2) use fortran or C and create a shared object
(3) use the unix 'cut' tool via NCL's "systemfunc"
    http://ngwww.ucar.edu/ngdoc/ng/ref/ncl/functions/systemfunc.html
    This is illustrated below.
===
method (3) is demonstrated
===
begin
  diri = "./"
  fili = "foo.ascii"

  col1 = stringtointeger( systemfunc("cut -c1-8 "+diri+fili) )
  col2 = systemfunc("cut -c10-15 "+diri+fili)
  col3 = systemfunc("cut -c17-20 "+diri+fili)
  col4 = stringtointeger( systemfunc("cut -c22-27 "+diri+fili) )
  col5 = stringtointeger( systemfunc("cut -c31-33 "+diri+fili) )
  col6 = stringtointeger( systemfunc("cut -c35-37 "+diri+fili) )
  col7 = stringtointeger( systemfunc("cut -c39-45 "+diri+fili) )
  col8 = stringtointeger( systemfunc("cut -c47-49 "+diri+fili) )
  col9 = stringtointeger( systemfunc("cut -c51-57 "+diri+fili) )
  col10 = stringtointeger( systemfunc("cut -c60-61 "+diri+fili) )

  print (col1+" "+col2+" "+col3+" "+col4+" "+col5+" "+col6 \
             +" "+col7+" "+col8+" "+col9+" "+col10 )
end

_______________________________________________
ncl-talk mailing list
ncl-talk AT ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk



This archive was generated by hypermail 2b29 : Thu Jun 17 2004 - 14:33:02 MDT