Chapters:
1: Introduction
2: Simple example
3: Invocation
4: Finer Control
5: X-Y Plots
6: Contour Plots
7: Image Plots
8: Examples
9: Gri Commands
10: Programming
11: Environment
12: Emacs Mode
13: History
14: Installation
15: Gri Bugs
16: Test Suite
17: Gri in Press
18: Acknowledgments
19: License
Indices:
Concepts
Commands
Variables

### 9.3.33: The ``read`' commands

There are several varieties of ``read`' command. Those commands used for reading numerical information (e.g. ``read columns`') are able to decode variables and synonyms as well as simple numbers.

- Read Colornames: Read colornames
- Read Columns: Read (x,y,...) columnar data
- Read Grid: Read grid for contouring
- Read Image Colorscale: Read colormap for color image
- Read Image Grayscale: Read colormap for gray image
- Read Image Mask: Read mask for image
- Read Image: Read image
- Read From: Change which open file looked at
- Read Synonym or Variable: Read individual synonym or variable
- Read Line: Read whole line

#### 9.3.33.1: ``read colornames`'

    `read colornames from RGB "\filename"'

Read colornames from named file, which is in the X11 format. This format has 4 or more columns, the first three giving the red, green and blue values in the range 0 to 255, and the last columns giving the colorname (which may have more than one word). You can create colors yourself or read an X11 color file. In many cases you will want to ``read colornames from RGB "/usr/lib/X11/rgb.txt"`'. Full filenames must be used; the '~' syntax is not permitted. Once you have read in a colorname table, the named colors may be used as builtin colors (see Set Color). To view the colors available on your particular system, use the Unix command ``xcolors`' or ``excolors`'; to see the RGB values of all colors on your X11 system, use the ``showrgb`' unix command.. To view the names and RGB values of the colors Gri knows, including builtin ones and ones from ``read colornames`', use ``show colornames`'.

This command is akin to ``set colorname`' (see Set Colorname), except that the latter uses the Gri notation of color constituents being in the range from 0 to 1, whereas for ``read colornames`' uses an X11 database, so that the color constitutents range from 0 to 255.

#### 9.3.33.2: ``read columns`'

    `read columns ...'

Read numbers into columns. These columns have predefined meanings and names. For example, ``read columns x y`' instructs Gri to read data into columns called ``x`' and ``y`'; it is these data that Gri will use if you tell it to ``draw curve`'. Other columns are: ``z`', used for contouring a function ``z=z(x,y)`'; ``weight`', used for weighting data points; ``u`' and ``v`', used for arrow (vector) plots.

If the keyword ``appending`' is given as the last word on the ``read columns`' line, then the new data will be appended to any existing columnar data; otherwise they will overwrite any existing data.

As a special case, if the ``x`' column is not indicated (e.g. ``read columns y`') then Gri creates x-values automatically, in the sequence 0, 1, 2, etc.

- ``read columns x y`' Read ``x`' in column 1, ``y`' in column 2 until blank-line found. Only the first two numbers on each line will be read; any extra numbers (or words) on the line will be ignored.

- ``read columns * y * * x`' Read ``x`' in column 5, ``y`' in column 2. The ``*`' character is a spacer. It instructs Gri to skip the first, third, and fourth words on the data line. These words need not be numbers. This example illustrates a general mechanism of using the ``*`' character to skip over unwanted items in the data file. Note that there is no need to supply ``*`' characters for trailing extraneous words; Gri will skip them anywary. Finally, note that any order of ``x`' and ``y`' (and the other columns; see below) is allowed.

- ``read columns y=2 x=5`' or ``read columns x=5 y=2`' As above; read ``x`' in column 5 and ``y`' in column 2. The column number may be specified in this manner for all the named column variables. No spaces are allowed before or after the ``=`' sign. The first column is called column 1. Whether this format is used or the ``*`' format is a matter of choice, except that numbered format also permits using a given number to fill several variables (for example ``read columns x=1 y=2 u=1 v=2`').

- ``read columns x="netCDF_name" ...`' If the file is a ``netCDF`' file, opened by e.g. ``open myfile.nc netCDF`', then the ``netCDF`' variables for the columns, e.g.

      open latlon.nc netCDF
      read columns x="longitude" y="latitude"

  Note: the data must be stored as the ``netCDF`' ``float'' type.

  For more information on netCDF format, see

  ``http://www.unidata.ucar.edu/packages/netcdf/index.html`' here .

- ``read columns * y z * x`' Read ``x`' in column 5, ``y`' in column 2, and ``z`' in column 3. The ``z`' column is used for contouring.

- ``read columns x y u v`' Read ``x`' and ``y`' in first two columns, and the ``arrow'' data ``u`' and ``v`' as third and fourth columns.

- ``read columns .rows. x y`' Read ``.rows.`' rows of column data.

Sometimes you'll have ``x`' in one file and ``y`' in another. In that case, use the operating system or an editor to put the columns in one file. In unix, the easy way is

    open "paste file_with_x file_with_y |"
    read columns x y

NOTE FOR BINARY FILES: For ascii files, Gri will proceed to a new line after it has read the items requested; it skips any words appearing on the data line after the last object of interest. Thus ``read columns x y`' will read the first two columns and ignore any other columns that might be present. But for binary files, Gri has no way of knowing how to "skip" to the next line (see ``skip`' command), so you will have to flesh out the ``read columns`' command with as many spacers as are present in your data. For example, if you have four numbers in each data record and want to interpret the first two as ``x`' and ``y`', you would use ``read columns x y * *`' to read the data.

RETURN VALUE: Sets ``\.return_value`' to ``N rows N non-missing N inside-clip-region`'

#### 9.3.33.3: ``read grid`'

``read grid`' commands read grid characteristics. (The ``grid'' is the object that is contoured.)

For normal ascii or binary files, the commands to read the grid's x-locations, y-locations and data are:

    `read grid x [.rows.]'
    `read grid y [.rows.]'
    `read grid data [spacers] \
      [.rows. .cols.] [spacers] [bycolumns]'

For ``netCDF`' files, the commands are as follows (note that it is not possible to specify the number of data to read, nor to read the grid by columns).

    `read grid x = "variable_name"'
    `read grid y = "variable_name"'
    `read grid data = "variable_name"'

The ordering of the y-grid data is the same as if they were read from a normal file: the first number is considered to be at the top of the plot.

For more information on netCDF format, see

``http://www.unidata.ucar.edu/packages/netcdf/index.html`' here .

Details of the non-netCDF commands:

- ``read grid x [.cols.]`' Read the ``x`' locations of the grid points, one number per line. If ``.cols.`' is supplied, then that many values will be read; otherwise, reading will stop at end-of-file or blank-line.

- ``read grid y [.rows.]`' As above, but for y grid; ``.rows.`' is the number of rows. The first number to be read corresponds to the location of the top edge of the grid. Thus, if you were to view the column of numbers with a text editor, they would be oriented the same way as the corresponding elements will appear on the page.

- ``read grid data [.rows. .cols.]`' Read data for a grid having ``.rows.`' and ``.cols.`' columns. (If ``.rows.`' and ``.cols.`' are not supplied, but the grid already exists, then those pre-existing values are used. If they are specified here, then they are checked for consistency with the pre-existing values if they exist.) Gri will read ``.rows.`' lines, each containing ``.cols.`' numbers. (Extra information in the file can be skipped; see discussion of the ``*`' keyword below.) Gri will interpret the first line it reads as the grid data corresponding to a value of y equal to ``y[.rows.]`'. Thus, file should be arranged like this:

      f(x[1], y[.rows.])  ...  f(x[.cols.], y[.rows.])
              .
              .
              .
      f(x[1], y[3])       ...  f(x[.cols], y[3])
      f(x[1], y[2])       ...  f(x[.cols], y[2])
      f(x[1], y[1])       ...  f(x[.cols], y[1])

- ``read grid data [.rows. .cols.] bycolumns`' As above, but the ``bycolumns`' keyword tells Gri to read the data one column at a time, instead of one row at a time. Each line is expected to contain ``.rows.`' numbers (as opposed to ``.cols.`' numbers, as in the format where the ``bycolumns`' keyword is not present). (Extra information in the file can be skipped; see discussion of the ``*`' keyword below). The first line of the data file contains the first column of the gridded data, corresponding to x equal to ``x[1]`'). The file should look like this:

      f(x[1], y[1])     ...     f(x[1], y[.cols.])
      f(x[2], y[1])     ...     f(x[2], y[.cols.])
      f(x[3], y[1])     ...     f(x[3], y[.cols.])
              .
              .
              .
      f(x[.rows.],y[1]) ...  f(x[.rows.], y[.cols.])

- ``read grid data * * [.rows. .cols.]`' As ``read grid data .rows. .cols.`' except that the first two words on each line are skipped. As usual, trailing extraneous numbers are also skipped.

See also ``set x grid`', ``set y grid`'

RETURN VALUE:

``read grid x`' sets ``\.return_value`' to ``N cols`'

``read grid y`' sets ``\.return_value`' to ``N rows`'

``read grid data`' sets ``\.return_value`' to ``N rows N cols`'

#### 9.3.33.4: ``read image colorscale`'

    `read image colorscale [rgb|hsb]'

Read colorscale for image, from 256 lines each containing values for Red, Green, and Blue (or Hue, Saturation and Brightness), separated by whitespace. The values are expected to be in the range 0 to 1, and are clipped to these limits if not.

For hints on how to create such an input file, see Read Image Grayscale. If the example given there has the following code instead,

    open "awk 'BEGIN {               \
         for(i=0;i<256;i++) {        \
           print((i - 50)/50, 1, 1)  \
         }                           \
       }' |"
    read image colorscale hsb

then a linear full-color spectrum running from red at 10C to magenta at 15C is achieved.

#### 9.3.33.5: ``read image grayscale`'

    `read image grayscale'

Read grayscale for an image, from 256 lines each containing a single value. The values are expected to be in the range 0 to 1, and are clipped to these limits if not. For 8-bit images, Gri multiplies these values by 255, and uses this list for the grayscale mapping. Such a list is created by ``write image grayscale`'.

As an example, consider the code fragment (see Images).

    set image range 5 30.5
    set image grayscale black 10 white 15

is equivalent to

    set image range 5 30.5
    open "awk 'BEGIN {\
      for(i=0;i<256;i++) {\
        print(1-(i-50)/50)\
      } \
    }' |"
    read image grayscale
    close

because the image formula is

> Temperature = 5C + 0.1C * pixelvalue

where the pixelvalue ranges from 0 to 255. Therefore, a temperature of 10C is a pixelvalue of 50, and 15C is 100. To get a grayscale ranging between these values, therefore, we create a linear function which maps the 50th pixelvalue into grayvalue 1, and the 100th pixelvalue into grayvalue 0. That is what the awk line does; to see the actual numbers, you could insert the line ``write image grayscale to TMP`' and look at the file ``TMP`' (bear in mind that Gri will clip the values to the range 0 to 1).

Sometimes you will have a file, say named ``map.dat`', with RGB numbers in the range 0-255, rather than 0-1 as Gri requires. To read them, use the operating system to convert the numbers for you (see Open).

    open "cat map.dat \
      | awk '{print(($1+$2+$3)/3/255)}' |"
    read image grayscale
    close

#### 9.3.33.6: ``read image mask`'

    `read image mask rasterfile|{.rows. .cols.}'

Read image mask. The mask is associated with the image read in by the ``read image`' command in the following way. When computing image histograms, Gri ignores any pixels in the image for which the corresponding pixel in the mask is set to ``1`'.

- ``read image mask rasterfile`' The image size is specified in the rasterfile file itself, so it is not specified.

- ``read image mask .rows. .cols.`' The file must contain ``.rows.*.cols.`' binary data. Pixel order is the same as for images.

#### 9.3.33.7: ``read image`'

There are several types of ``read image`' commands, depending on the file format. If the file is "raw", with no embedded information about things like the width and height, then we need to specify everything, as in the first format given below. The other formats make use of the header information in, e.g. PGM files.

Headerless images

    `read image .rows. .cols. \
      [box .xleft. .ybottom. .xright. .ytop.] \
      [bycolumns]'

With no options specified (``read image .rows. .cols.`'), read binary data defining an ``image`'. The image range must have previously have been set by ``set image range`'. The data are as written by "unsigned char" format in C.

When the ``box`' option is specified, the geometry of the image, in user coordinates, is specified in terms of the cartesian coordinates of the lower-left corner (``.xleft.`', ``.ybottom.`') and upper-right corner (``.xright.`', ``.ytop.`'). If the ``box`' option is not specified, this geometry can be specified with either ``read x grid`' or ``set x grid`', plus either ``read y grid`' or ``set y grid`'.

With the ``bycolumns`' keyword present, the image is read sweeping from top-to-bottom, then left-to-right, instead of the usual order.

Sun rasterfile images

    `read image rasterfile [box .xleft. .ybottom. .xright. .ytop.]'

Read image in Sun rasterfile format. Image geometry is inferred from the header, so ``.rows.`' and ``.cols`' parameters are not given.

PGM images

    `read image pgm [box .xleft. .ybottom. .xright. .ytop.]'

Read image in PGM (Portable Gray Map) format. Image geometry is inferred from the header, so ``.rows.`' and ``.cols`' parameters are not given. Both ascii and binary PGM formats are supported (that is, files with magic characters of P2 and P5).

NOTE that there are many image formats and Gri doesn't try to deal with them all. The idea is to use another program to convert images to a file format that Gri understands. In the future Gri may support PNG and other popular formats, especially in the Linux versions, for which libraries exist to ease the input.

#### 9.3.33.8: ``read from \filename`'

    `read from \filename'

Cause future ``read`' commands to read from the indicated file. If that file is not open, an error message will result. Use ``read from \filename`' to shuffle reading among several open files.

Gri can look up filenames for ``read from`' in terms of their full pathnames or their local pathnames. Thus, a local file called ``a.dat`' in the directory ``/home/gri`' can be referred to by ``read from a.dat`' or by ``read from /home/gri/a.dat`', which comes in handy if you need to work with two files of the same name, in other directories. However, since Gri has the ability to search for files in a "path" (see Set Path To), you may not have specified an exact path name; this is why the ``open`' command provides a return value which names the full pathname (see Opening Simple Files).

#### 9.3.33.9: ``read`' synonym/variable

    `read [* [*...]] \synonym|.variable. [\synonym|.variable. [...]]}'

Read one or more items from the next line of the input file. These items may be synonyms or variables. The token ``*`' indicates that the word in the datafile should be skipped. As usual, the datafile may be embedded in the commandfile, providing the last data line is blank.

Normally one would use synonyms for words, and variables for numbers. The items are separated by one or more "whitespace" characters (e.g. space or TAB). Thus, if a file contained the line

    Temperature 10.3

then the line

    read \var_name .var_value.

would have the same result as

    \var_name = "Temperature"
    .var_value. = 10.3

This command ignores any trailing items on the line. That is, the next ``read`' command will start on the next line of the file. In a sense then, you get just one shot at analysing the input line in your datafile. If you need flexibility, you may wish to read the whole contents of the line into a synonym, which may be done using the ``read line`' command instead, to read it in as a string. (see Read Line).

If the input file is in the netCDF format, the indicated item will be read. For example, ``read \time:_MissingValue`' reads the missing value for the variable called ``time`'. This conveniently allows your data file to dictate axes names, units, missing values, etc. Example:

    # Plot profile of TU81N (age-corrected tritium)
    open profile.nc netCDF
    read columns x="TU81N" y="z"
    read \{z:_FillValue}		# assume same for all
    read \{z:long_name}
    read \{z:units}
    read \{TU81N:long_name}
    read \{TU81N:units}
    close
    set missing value \{z:_FillValue}
    set x name "\{TU81N:long_name} (\{TU81N:units)}"
    set y name "\{z:long_name} (\{z:units)}"
    set y axis decreasing
    draw curve

For more information on netCDF format, see ``http://www.unidata.ucar.edu/packages/netcdf/index.html`' here .

#### 9.3.33.10: ``read line`'

    `read line \synonym'

Read the next line of the datafile (or commandfile), trim off a trailing comment if there is one, and then store the next line of datafile into the named synonym.