MadrigalCedarFile is an object that allows the creation and editting of Cedar files.
This class emulates a python list, and so users may treat it just like a python list. The
restriction enforced is that all items in the list must be either MadrigalCatalogRecords,
MadrigalHeaderRecords, or MadrigalDataRecords (all also defined in the madrigal.cedar module).
Each of these three classes supports the method getType(), which returns catalog , header ,
and data , respectively.
Usage example:
# the following example inserts a catalog record at the beginning of an existing file
import madrigal.cedar.MadrigalCedarFile, time
cedarObj = madrigal.cedar.MadrigalCedarFile('/opt/madrigal/experiments/1998/mlh/20jan98/mil980120g.003')
startTime = time.mktime((1998,1,20,0,0,0,0,0,0)) - time.timezone
endTime = time.mktime((1998,1,21,23,59,59,0,0,0)) - time.timezone
# catLines is a list of 80 character lines to be included in catalog record
catObj = madrigal.cedar.MadrigalCatalogRecord(31, 1000, 1998,1,20,0,0,0,0,
1998,1,21,23,59,59,99, catLines)
cedarObj.insert(0, catObj)
cedarObj.write()
Non-standard Python modules used: None
Change history:
Written by Bill Rideout April. 6, 2005
Methods
|
|
|
|
__contains__
|
__contains__ ( self, other )
|
|
__delitem__
|
__delitem__ ( self, key )
|
|
__delslice__
|
__delslice__ (
self,
i,
j,
)
|
|
__getitem__
|
__getitem__ ( self, key )
|
|
__getslice__
|
__getslice__ (
self,
i,
j,
)
|
|
__init__
|
__init__ (
self,
fullFilename,
createFlag=False,
startDatetime=None,
endDatetime=None,
)
__init__ initializes MadrigalCedarFile by reading in existing file, if any.
Inputs:
fullFilename - either the existing Cedar file (in any allowed Cedar format),
or a file to be created.
createFlag - tells whether this is a file to be created. If False and
fullFilename cannot be read, an error is raised. If True and
fullFilename already exists, or fullFilename cannot be created,
an error is raised.
startDatetime - if not None (the default), reject all input records where
record end time < startDatetime (datetime.datetime object)
endDatetime - if not None (the default), reject all input records where
record start time > endDatetime (datetime.datetime object)
Affects: populates self.__privList if file exists, sets self.__fullFilename
Returns: void
Exceptions
|
|
ValueError, 'in MadrigalCedarFile, createFlag must be either True or False'
ValueError, 'in MadrigalCedarFile, endDatetime %s must be datetime' %(str( endDatetime ) )
ValueError, 'in MadrigalCedarFile, fullFilename %s already exists' %(str( fullFilename ) )
ValueError, 'in MadrigalCedarFile, fullFilename %s cannot be created' %(str( fullFilename ) )
ValueError, 'in MadrigalCedarFile, fullFilename %s does not exist' %(str( fullFilename ) )
ValueError, 'in MadrigalCedarFile, startDatetime %s must be datetime' %(str( startDatetime ) )
|
|
|
__iter__
|
__iter__ ( self )
|
|
__len__
|
__len__ ( self )
|
|
__parseFile
|
__parseFile ( self )
__parseFile reads an existing Cedar file, and populates self.__privList with MadrigalCatalogRecords,
MadrigalHeaderRecords, or MadrigalDataRecords.
Exceptions
|
|
'Illegal return value'
IOError, 'Error parsing Cedar file %s' %( self.__fullFilename )
|
|
|
__setitem__
|
__setitem__ (
self,
key,
value,
)
Exceptions
|
|
ValueError, 'In MadrigalCedarFile, can only add MadrigalCatalogRecord, MadrigalHeaderRecord, or MadrigalDataRecord'
|
|
|
__setslice__
|
__setslice__ (
self,
i,
j,
seq,
)
Exceptions
|
|
ValueError, 'In MadrigalCedarFile, can only add MadrigalCatalogRecord, MadrigalHeaderRecord, or MadrigalDataRecord'
|
|
|
__str__
|
__str__ ( self )
|
|
append
|
append ( self, item )
Exceptions
|
|
ValueError, 'In MadrigalCedarFile, can only add MadrigalCatalogRecord, MadrigalHeaderRecord, or MadrigalDataRecord'
|
|
|
count
|
count ( self, other )
|
|
createCatalogTimeSection
|
createCatalogTimeSection ( self )
createCatalogTimeSection will return all the lines in the catalog record that
describe the start and end time of the data records.
Inputs: None
Returns: a tuple with three items 1) a string in the format of the time section of a
catalog record, 2) earliest datetime, 3) latest datetime
|
|
createHeaderTimeSection
|
createHeaderTimeSection ( self, dataRecList=None )
createHeaderTimeSection will return all the lines in the header record that
describe the start and end time of the data records.
Inputs:
dataRecList - if given, examine only those MadrigalDataRecords in dataRecList.
If None (the default), examine all MadrigalDataRecords in this
MadrigalCedarFile
Returns: a tuple with three items 1) a string in the format of the time section of a
header record, 2) earliest datetime, 3) latest datetime
|
|
dump
|
dump ( self, format='UnblockedBinary' )
dump appends all the present records in MadrigalCedarFile to file, and removes present records from MadrigalCedarFile.
Can be used to append records to a file. Only works with file formats UnblockedBinary and Ascii, since other formats
do not have clear record -> file mapping.
Inputs:
format - a format to save the file in. For now, the allowed values are UnblockedBinary and Ascii .
Defaults to UnblockedBinary. If dump was previously called with other format, exception raised.
Outputs: None
Affects: writes a MadrigalCedarFile to file
Exceptions
|
|
ValueError, 'Format must be UnblockedBinary or Ascii for dump, not %s' %(str( format ) )
ValueError, 'Previous dump format was %s, cannot now use %s' %(str( self.__format ), str( format ) )
|
|
|
index
|
index ( self, other )
|
|
insert
|
insert (
self,
i,
x,
)
|
|
pop
|
pop ( self, i )
|
|
remove
|
remove ( self, x )
|
|
reverse
|
reverse ( self )
|
|
write
|
write (
self,
format='Madrigal',
newFilename=None,
)
write persists a MadrigalCedarFile to file.
Inputs:
format - a format to save the file in. For now, the allowed values are Madrigal , BlockedBinary ,
UnblockedBinary , Cbf , and Ascii . Defaults to Madrigal.
newFilename - a filename to save to. Defaults to self.__fullFilename passed into initializer if not given.
Outputs: None
Affects: writes a MadrigalCedarFile to file
Exceptions
|
|
ValueError, 'Cannot call write method after calling dump method'
|
|
|