Reading ASCII data in CDAT
Goal: Learn how to read ASCII data in CDAT.
- Use Python
“string” Module
- Use VCDAT
- In general use genutil.ASCII.readAscii
genutil.ASCII.readAscii( text_file ,header=0, ids=None, shape=None,
next='------',separators=[';',',',':'])
Reads data from an ascii file
Usage :::
vars = genutil.ASCII.readAscii( text_file ,header=0, ids=None, shape=None, next='------',separators=[';',',',':'])
:::
Options :::
text_file :: ASCII File to read from.
header :: (0) Number of header lines, these lines will be skipped.
ids :: (None) use the values in this list as variable ids (1 per variable returned)
shape :: (None) use the tuple/list in this list as the final shape of the variable read.
next :: ('------') character string marking separation between variables
separators :: ([';',',', ':']) List of character recognized as column separator
Output :::
vars :: List containing transient variable possibly named after ids and reshaped from the 'shape' option.
- If data are in columns use genutil.ASCII.read_col
genutil.ASCII.read_col( text_file ,header=0, cskip=0,
cskip_type='columns', axis=0, ids=None, idrow=0,
separators=[';',',', ':'])
Reads column-stored data from ASCII files
Usage:::
vars = genutil.ASCII.read_col( text_file ,header=0, cskip=0, cskip_type='columns', axis=False, ids=None, idrow=False, separators=[';',',', ':'])
Options :::
text_file :: ASCII File to read from.
header :: (0) Number of header lines, these lines will be skipped.
cskip :: (0) Number of 'column'/'character' to skip (dummy column)
cskip_type :: ('column') is 'cskip' a number of 'column' or 'character' to skip?
axis :: (False) Use as the values for the first column as variable axis (x values in y(x))
idrow :: (False) Is the first row representing the ids of var generated ?
ids :: (None) use the values in this list as variable ids (1 per column returned)
separators :: ([';',',', ':']) List of character recognized as column separator
Output :::
vars :: List containing 1 transient varialbe per column in the files.
Variable ids are optionaly determined by first row.
Variable axis may be the first column
Python "string" example:
import sys,MV
# First of all we need to open the ASCII file
# For this we used the Python built-in command "open"
f=open(sys.prefix+'/sample_data/test_col.asc')
# Now we need to read its content
# To read all of its content we use the "readlines" command
# This returns a list of strings, each element of the list represents
# one line in the file
lines=f.readlines()
# Note to read one line at a time (inside a loop for example, if the ascii file is too big
# You can also f.readline()
# Now we can loop through the lines and look at the content
data1=[]
data2=[]
for line in lines:
# Splits the line into a list of string with seprartion
# when it finds space or tabs or return
sp=line.split()
# Now try to see if the first element is a number, if not skip
# we are only interested in the 2nd and third column here
try:
val1=float(sp[1]) # second column
val2=float(sp[2]) # third column
data1.append(val1)
data2.append(val2)
except:
pass # we didn't have 2 float at the begining of this line
# Now converts the 2 datasets to MV for use in other CDAT Packages
data1=MV.array(data1,id='dataset1')
data2=MV.array(data2,id='dataset2')
# Just for fun prints the average of data1
print MV.average(data1)