Reading ascii data representing a grid
Question:
How can I read my ASCII data file which lists the longitudes and latitudes of a grid, and the data values?
Explanation:
If the data looks something like this:
219.0 11.00 -4916. 221.0 11.00 -4872. 223.0 11.00 -4923. 225.0 11.00 -4900. 227.0 11.00 -4818. 219.0 13.00 -4864. 221.0 13.00 -4855. 223.0 13.00 -4832. 225.0 13.00 -4846. 227.0 13.00 -4825. 219.0 15.00 -5010. 221.0 15.00 -4849. 223.0 15.00 -4785. 225.0 15.00 -4815. 227.0 15.00 -4959.
with the columns representing longitude, latitude, variable. If we try just these commands:
yes? FILE/VAR=xpoints,ypoints,bathy myfile.txt yes? DEFINE AXIS/X/UNITS=degrees_east xpoints **ERROR: improper grid or axis definition: data for DEFINE AXIS/FROM_VARIABLE is not monotonic
The data in the first two columns represent the 2D grid, so they repeat points. We need a bit more analysis to get the right definitions for the axes.
Solution:
1) If we're just doing one file, and if the grid is regularly-spaced grid, we can just look at the file and define the correct axes:
yes? DEFINE AXIS/X=221:227:2/UNITS=degrees_east xaxis yes? DEFINE AXIS/Y=11:15:2/UNITS=degrees_north yaxis (using whatever the true ranges and delta-values are in the file) yes? ! Define the grid and read, skipping the first two values in each input record. yes? DEFINE GRID/X=xaxis/Y=yaxis xygrid yes? FILE/VAR="dummy1,dummy2,bathy" myfile.txt
2) For a more general script, or if the spacing of the grid is not even, then you'll need to first use the data in the file to define the axes and then define a grid to read the data field. (Try LIST or PLOT commands with these different variables to see what we're doing here.)
yes? ! First read just the points defining the grid. yes? FILE/VAR="xpoints,ypoints" myfile.txt yes? ! The first N points of the variable xpoints will define the x axis. yes? ! Let's find where xpoints starts to repeat itself. yes? ! Where does the first derivative cross zero? yes? LET xdif = xpoints[I=@DDF] yes? LET xcross = xdif[I=@EVNT:0] yes? ! use LIST commands to check what we've done yes? LIST/I=1:15 xpoints, xdif, xcross DATA SET: ./myfile.txt X: 0.5 to 15.5 Column 1: XPOINTS Column 2: XDIF is XPOINTS[I=@DDF] Column 3: XCROSS is XDIF[I=@EVNT:0] XPOINTS XDIF XCROSS 1 / 1: 219.0 2.000 0.000 2 / 2: 221.0 2.000 0.000 3 / 3: 223.0 2.000 0.000 4 / 4: 225.0 2.000 0.000 5 / 5: 227.0 -8.000 1.000 6 / 6: 219.0 2.000 2.000 7 / 7: 221.0 2.000 2.000 8 / 8: 223.0 2.000 2.000 9 / 9: 225.0 2.000 2.000 10 / 10: 227.0 -8.000 3.000 11 / 11: 219.0 2.000 4.000 12 / 12: 221.0 2.000 4.000 13 / 13: 223.0 2.000 4.000 14 / 14: 225.0 2.000 4.000 15 / 15: 227.0 .... 4.000 yes? ! The @LOC tranform tells where the xdif variable crosses 0 yes? ! This is the length of the x axis yes? LET npts = `xcross[I=@LOC:1]` !-> DEFINE VARIABLE npts = 5 yes? ! Now use the first npts of the data from the file yse? ! to define the longitude axis yes? DEFINE AXIS/X/UNITS=degrees_east xaxis = xpoints[I=1:`npts`]  !-> DEFINE AXIS/X/UNITS=degrees_east xaxis = xpoints[I=1:5] yes? ! Now for the y axis we want the first value of ypoints, yes? ! then one point every `npts` after that yes? LET total = `ypoints,RETURN=isize` ! total points read from the file !-> DEFINE VARIABLE total = 15 yes? DEFINE AXIS/Y/UNITS=degrees_north yaxis=ypoints[I=1:`total`:`npts`] !-> DEFINE AXIS/Y/UNITS=degrees_north yaxis=ypoints[I=1:15:5] yes? ! Now define the grid and re-open the file to read the variable. yes? CANCEL DATA myfile.txt yes? DEFINE GRID/X=xaxis/Y=yaxis xygrid yes? FILE/VAR="dummy1,dummy2,bathy"/GRID=xygrid myfile.txt