Reading ASCII: columnar data separated by delimiters
Reading data that contains mixed types
If you have a data file in which the first row contains the name of each field separated by a delimiter, and the rest of the file contains the values of each field separated by the same delimiter, then you can download the NCL script below to read it in.
Here's the start of a sample file with 6 fields, each separated by a comma;
ID,LAT,LON,ELEV,SOURCE,FLAGS 4,-27.75,152.45,-999,ADAM,0 5,-27.03,152.02,-999,MARY,0 . . .
Note: it is not generally recommended to read in complex ASCII files with NCL, but this example shows that it can be done.
The script below also writes the ASCII data back out to a netCDF file with same name as the input file, with ".nc" appended. The script is rather lengthy; this is because it requires string parsing which is not one of NCL's strong suits. Also, there's a bit of checking involved to allow multiple types to be read in.
You will need to modify the script to indicate the input ASCII file name, the number of fields, the delimiter, and the type of each field. The name of the script is ascii_delim.ncl. First search for the lines:
;============================================================ ; Main code ;============================================================and the lines you need to modify follow shortly:
filename = "asc.dat" ; ASCII file to read. nfields = 6 ; # of fields delimiter = "," ; field delimiter var_types = new(nfields,string) var_strlens = new(nfields,integer) ; var to hold strlens, just in case. . . . var_types = "integer" ; Default to int. var_types(4) = "character" ; Corresponds to field 4. var_types(1:2) = "float"The allowable variable types are "integer", "float", "double", "string", or "character". Note that if you read in a variable as a string, it won't get written to the netCDF file because only character arrays can be written to a netCDF file.
Below is an example "asc.dat" data set with six fields, each separated by a comma (",") as a delimiter.
ID,LAT,LON,ELEV,SOURCE,FLAGS 4,-27.75,152.45,-999,ADAM,0 5,-27.03,152.02,-999,MARY,0 6,-26.76,148.82,-999,DAVE,1 7,-26.58,148.77,-999,RICK,0 8,-26.48,148.68,1000,DENNIS,0 9,-26.30,148.52,900,TIM,0 10,-26.25,148.41,-999,SPONGEBOB,0You can run the above script on this file; call it "asc.dat". Some output will be echoed to the screen, and it will create a netCDF file called "asc.dat.nc".