Okay, so as always we will need to start by opening the data file. For this tutorial, I will use the monthly mean 2d reanalysis product.
Before we do anything else, lets see what our variable looks like. Since this dataset spans a large range of time, you can pick a time to look at; I am going to choose February,2000 as my time value. I do this by using the 'set time' (as opposed to 'set t') command. Once the time is set, simply display your variable. The resultant image is shown below.
'set time 00Z01FEB2000'
'set gxout shaded'
|Average 2 meter temperature for Feb 2000|
The first thing we want to do is determine what data we would like to save, and how we would like to organize it. I have found that when writing 2d data to a text file, I like to include the x/y grid location, the lat/lon value at each grid point, and the actual temperature data. So, our data file will have five columns to contain these data. I like to know which column is which, so the first thing we will do is write header to start off our data file. This is done by using the "write" function in GrADS.
write('NCEP.txt', 'X Y LON LAT TEMP')
Once this command is executed in GrADS the file NCEP.txt will be created in your folder with the text "X Y LON LAT TEMP" on top.
Now that we have our data file started, it's time to start filling it up with data. To do this, the first thing that we need to do is tell GrADS to write our data out to the screen instead of plot it on a map. This is done by setting the output to print. In addition to that, we need to tell GrADS how to format our printed data. This is done with the 'prnopts' command.
'set gxout print'
'set prnopts %6.2f 1 1' ;*%6.2f: c format, 1: values to plot on each line, ;space between values
So, GrADS is set to print to the screen, and our printing options are set, we are ready to save the data. The way we are going to this is one grid-point at a time using a nested loop, looping first though x, then through y grid points, saving the information into rows as we go. To begin, we will first need to set limits on our domain. We do this by the use of the 'query' function
xline=sublin(result,2) ;* 2nd line
yline=sublin(result,3) ;* 3rd line
xmax=subwrd(xline,13) ;*13th word on xline
ymax=subwrd(yline,13) ;*13th word on yline
say 'X grid-points: 'xmax
say 'Y grid-points: 'ymax
This brick of code should print out the following to the screen:
X grid-points: 193
Y grid-points: 94
Now we know our domain dimensions. We can set up our loop. Again, we will use a simple nested loop configuration to go through the data. All we do in the loop, is print out the temperature data at each grid-point, get the lat/lon values using the 'q dims' command, and write the data to a new row in the NCEP.txt file. The code is as follows:
'set x 'x
'set y 'y
* NOTE: It may be useful to test this to find out where the data is contained with in the result
* It just so happens that in this case, the data is the 1st word of the 2nd line, this is not always true
*Get Lat/Lon Data
*Save data to file
*Note the "append", so to add to the file instead of overwriting it
write('NCEP.txt', x' 'y' 'lon' 'lat' 'tmp, append)
That's actually all there is to it. This script will take a few moments to loop through all of the grid-points, and as it does so you will likely see the words "unknown command 0" printed a bunch of times on your screen. I have often found it helpful to include a "say" command inside the loop, so to know where I am in the saving process. When this script finishes you will have a lot of data saved into a .txt file that can be read into any plotting or statistical program.
As always, I included a downloadable script for this tutorial: Download this script