irfpy.util.datafile

irfpy-standard data file.

Code author: Yoshifumi Futaana

See the tutorial on the data file Data file.

class irfpy.util.datafile.Datafile(version='1.0')[source]

Bases: object

A template file of data file.

All the irfpy-compatible data file has a very simple structure. It composes of header, data and trailer. Each can contain more than zero entries.

Header and trailer includes general information or meta values on the data file. Version number and the creation time is automatically added at the top. Header and trailer lines start with “#’. You can add any header and trailer by add_header() or add_trail() methods.

The data includes the data. You may add an entry of data by add_data(). Multiple data can be stored. datakey can be given. If the same datakey is called in add_data(), the data is appended in the end. You may also use continue_data() without specifying the data key. The last one is used in this case. To append the data, using str is highly recommended.

>>> df = Datafile(version="1.0")   # Create data file on memory.
>>> print(df.dumps())    
### HEADER : 2
# VERSION : 1.0
# CREATION_DATE : ...
### DATA : 0
### TRAIL : 0

You may add header/trail.

>>> df.add_header('SPACECRAFT', 'MEX')
>>> df.add_trail('LICENSE', 'BSD')
>>> print(df.dumps())   
### HEADER : 3
...
# SPACECRAFT : MEX
...
### TRAIL : 1
# LICENSE : BSD

Adding data is as follows.

>>> df.add_data('FLUX', '0 2009-01-03T18:05:33  2.8 1.9 4.1 7.3')
>>> df.add_data('FLUX', '1 2009-01-03T18:08:45  2.7 2.3 3.7 6.8')
>>> df.add_data('FLUX', '2 2009-01-03T18:11:57  2.6 1.9 2.8 9.0')
>>> print(df.dumps())   
### HEADER : 3
...
### DATA : 1
## DATA : FLUX : 3
0 2009-01-03T18:05:33  2.8 1.9 4.1 7.3
1 2009-01-03T18:08:45  2.7 2.3 3.7 6.8
2 2009-01-03T18:11:57  2.6 1.9 2.8 9.0
...

Reading data is as follows.

>>> from io import StringIO  # Prepare data file.
>>> fp = StringIO()
>>> b = fp.write('### HEADER : 2\n')
>>> b = fp.write('# VERSION : 1.0\n')
>>> b = fp.write('# CREATION_DATE : 2013-03-08T09:56:22\n')
>>> b = fp.write('### DATA : 1\n')
>>> b = fp.write('## DATA : FLUX : 3\n')
>>> b = fp.write('0 2009-01-03T18:05:33  2.8 1.9 4.1 7.3\n')
>>> b = fp.write('1 2009-01-03T18:08:45  2.7 2.3 3.7 6.8\n')
>>> b = fp.write('2 2009-01-03T18:11:57  2.6 1.9 2.8 9.0\n')
>>> b = fp.write('### TRAIL : 0\n')
>>> b = fp.seek(0)   # Faked data file ready. Usually, fp = open('fname.dat') is enough.

Here comes the way of reading.

>>> fp = open('fname.dat')   
>>> df = Datafile()
>>> df.readfile(fp)
>>> dfreader = DatafileReader(df)
>>> print(dfreader.get_header('VERSION'))
1.0
>>> print(dfreader.get_header('CREATION_DATE'))
2013-03-08T09:56:22
>>> print(dfreader.get_header('SOMETHING_NOT_THERE'))
None
>>> print(dfreader.get_data('FLUX')[2])
2 2009-01-03T18:11:57  2.6 1.9 2.8 9.0

Instnce a data file class with version

add_header(head_key, head_data)[source]
add_creation_path()[source]

Add a creation path to a header.

add_data(data_key, data)[source]
set_cdkey(data_key)[source]

Set the current data key.

continue_data(data)[source]
add_trail(trail_key, trail_data)[source]
dumps()[source]
dump(fp)[source]

Print to a file-like, fp

readfile(fp)[source]

Read the file from the file-like, fp

readheader(fp)[source]
readtrail(fp)[source]
readdata(fp)[source]
class irfpy.util.datafile.DatafileReader(data_file, missing_return=None)[source]

Bases: object

get_header(key)[source]
get_data(key)[source]
get_trail(key)[source]
irfpy.util.datafile.doctests()[source]