Data File Reading

August, 2001


The VisAD Library contains a family of data file readers (FITS, GIF, text, netCDF, DODS, ADDE, etc) which create VisAD Data objects that you can use.

The loadl() method that is provided will attempt to invoke the correct file reader based on one or more characteristics of the filename or it's form. For example, files with an extension ".dods" will be read using the DODS File Adapter.

You may, of course, also use Jython's File I/O to read data, and then create your own VisAD Data object.

The load() method

This method takes one argument: the name of the data file from which you want to read data. As you saw in the introduction, it's as simple as it sounds.
a = load("mydata.txt")
would invoke the VisAD Text Adapter to read the file "mydata.txt" and create a VisAD Data object, which is then referred to by the symbol a. Whereas,
a = load("")
would invoke the VisAD netCDF (Plain) Adapter to read the file "" and create a VisAD Data object.

If you are reading a file that you are not familiar with, you might want to take a look at the VisAD structure associated with this. If you command: dumpType(name) you will get a listing of the entire structure. See Using data types functions for more details.

Using VisAD Data Adapters directly

The load() method may not always do exactly what you need, and so you might need to resort to using VisAD Data Adapters (file readers) directly. It is beyond the scope of ths document to discuss those, and you are referred to the VisAD Programmer's Guide for that.

It is important, however, to document the following: If you need to access any class in the package (or its sub-packages), you must first do: "from visad import *" in order to the gain access to the desired File Adapter class. For example, to read Point data via ADDE, I'd need to use:

from visad import *
from import PointDataAdapter

Reading data from a file using Jython I/O

Jython provides for basic I/O that allows you to read data values from text or binary files. In order to illustrate this, we first need a data let's create one:
import math
output = open('myfile.txt','w')
for x in range(0,360,10):
Running this Jython script creates a file named myfile.txt with the 37 values of the sin() function, every 10 degrees. Now, let's read this data into a Jython list of floating point values named "z":
for v in m:
At this point if you just enter z on the command line, you'll see the values listed. Next, we need to turn these values into a VisAD Data object. Since this is a one-dimensional set of values, linearly spaced, we can simply use the field() function:
f = field(z)
To show this, of course, just use the plot() function:
It will look like this:

There are several variations on the field function that are outlined in that section (hyperlink).

If you need to use binary files, Jython provides a 'b' option on the open() method to supress most character translation (for example, newline), plus you will probably need to use the struct module to translate between character/byte streams and internal/binary formats. See Python documentation on struct module.

Here's a short example:

import struct
f = open("OUTLUSAM", "rb")
g =
h = struct.unpack("3i", g)
print h
(857, 800000, 801490)
The "12" in the read() says read 12 bytes. The "3i" format in the unpack() says to translate the 12 bytes in g as 3 integers. The struct formatting has many options, including byte-order swapping if you need it.

Back to the home page