formats Package

visual Module

exception pyaavso.formats.visual.FormatException

Raised when the data does not conform to AAVSO format specification.

class pyaavso.formats.visual.VisualFormatReader(fp)

A class to read observations from file in AAVSO Visual File Format.

The reader API is also based on csv Python module. You create a reader instance by passing a file-like object in the constructor. This will read all the data and validate required headers. Then the reader object can be used to iterate over observation data.

A short example:

>>> with open('data.txt', 'rb') as fp:
...     reader = VisualFormatReader(fp)
...     for observation in reader:
...         print '%(name)s %(magnitude)s' % observation
SS Cyg 10.0
RZ Cas 6.4

Creates the reader instance and reads file headers.

Raises FormatException when any of the required headers could not be found in input. The following header parameters are required:

  • TYPE - always ‘Visual’, yet must be specified in file
  • OBSCODE - official AAVSO-assigned observer code
  • DATE - date format, must be one of ‘JD’ or ‘Excel’

Other headers described in AAVSO specification have reasonable default values, eg. the default delimiter is a comma, when not specified in headers. Without the OBSTYPE header, observations are assumed to be visual.

Parameters:fp – a file-like object from which data will be read
classmethod row_to_dict(row)

Converts a raw input record to a dictionary of observation data.

Parameters:
  • cls – current class
  • row – a single observation as a list or tuple
class pyaavso.formats.visual.VisualFormatWriter(fp, observer_code, delimiter=', ', date_format=u'JD', obstype=u'Visual')

A class responsible for writing observation data in AAVSO Visual File Format.

The API here mimics the csv module in Python standard library.

To write your observations into the data file, you first need to create the writer, passing to it the destination file and your observer code. Then call writerow() for every single observation, for example:

>>> with open('data.txt', 'wb') as fp:
...     writer = VisualFormatWriter(fp, 'XYZ')
...     writer.writerow({
...         'name': 'SS CYG',
...         'date': '2450702.1234',
...         'magnitude': '<11.1',
...         'comment_code': '',
...         'comp1': '110',
...         'comp2': '113',
...         'chart': '070613',
...         'notes': 'This is a test',
...     })

Creates the writer which will write observations into the file-like object given in first parameter. The only other required parameter is the official AAVSO-assigned observer code.

Parameters:
  • fp – file-like object to write observations into
  • observer_code – AAVSO observer code
  • delimiter – field delimiter (set as DELIM header)
  • date_format – observation date format (one of JD or Excel)
  • obstype – observation type (Visual or PTG)
classmethod dict_to_row(observation_data)

Takes a dictionary of observation data and converts it to a list of fields according to AAVSO visual format specification.

Parameters:
  • cls – current class
  • observation_data – a single observation as a dictionary
writerow(observation_data)

Writes a single observation to the output file.

If the observation_data parameter is a dictionary, it is converted to a list to keep a consisted field order (as described in format specification). Otherwise it is assumed that the data is a raw record ready to be written to file.

Parameters:observation_data – a single observation as a dictionary or list