NCL Home> Application examples> File IO || Data files for examples > read ascii

Reading ASCII data using "cut" to parse the data before reading

This method does not use the asciiread function at all, but a combination of the NCL function systemfunc and the UNIX "cut" command to read in only part of the file. If the file had missing columns, this technique would be required.

For an example, assume the data file is called "foo.ascii" and has the following lines:

200306130209   0.38   25.28 10088 233.95   6  92  9.99  9.99 99999.0   0.0 -9.99 167.9 p p p 1782  BOS  ATL 3              
200306130209   0.38   25.28 10088 233.95   6  92  9.99  9.99 99999.0   0.0 -9.99 167.9 p p p 1782  ORD  ATL 3              
200306122341 -45.10  168.70   914 279.35   4 272  9.99  9.99     1.1   0.0  0.01 -99.9 p p p 4552  DTW  MSP 3              
200306122341 -45.10  168.70   914 279.35   4 272  9.99  9.99     1.1   0.0  0.01 -99.9 p p p 4552  SDF  MHR 3  
To read off the year, month, day, hour, minutes, and stations from each line, you could use:

  fname  = "foo.ascii"
  year   = stringtofloat(systemfunc("cut -c1-4 " + fname))
  month  = stringtofloat(systemfunc("cut -c5-6 " + fname))
  day    = stringtofloat(systemfunc("cut -c7-8 " + fname))
  hour   = stringtofloat(systemfunc("cut -c9-10 " + fname))
  minute = stringtofloat(systemfunc("cut -c11-12 " + fname))
  sta    = systemfunc("cut -c100-102 " + fname)
Note that the first character is considered to be column 1. Also, you cannot use stringtointeger to convert the numbers like "09" to "9", because the preceding "0" causes NCL to treat the number as an octal value, and "9" is not a valid octal value.