Next / Previous / Contents / Shipman's homepage

64. class MawsDateField: MAWS date field

In MAWS data sets, the year to be written to the output encounter record is the year of the actual encounter. In October, November, and December, the field season year in the file name is one greater than the year of the encounter.

Therefore, this class, derived from DateField, handles that year change. Only the .scanField() method need be replaced.

baseclasses.py
# - - - - -   c l a s s   M a w s D a t e F i e l d   - - - - -

class MawsDateField(DateField):
    '''Represents the date in MAWS data sets.

      Exports:  As inherited.
    '''

64.1. MawsDateField.scanField()

This method is a variant of DateField.scanField() that handles the special case where we have to subtract one from the field year to get the year of the actual encounter.

baseclasses.py
# - - -   M a w s D a t e F i e l d . s c a n F i e l d   - - -

    @staticmethod
    def scanField(encounter, scan, fieldName):
        '''Scan an MMDD-type date, adding the year from context.
        '''

        #-- 1 --
        # [ if the line in scan starts with a pattern that matches
        #   self.MMDD_PAT ->
        #     scan  :=  scan advanced past that pattern
        #     rawMMDD  :=  the matching pattern
        m = scan.tabReMatch(DateField.MMDD_PAT)
        if  m is None:
            scan.syntax("Expecting a date in MMDD format.")
        else:
            rawMMDD = m.group()

        #-- 2 --
        # [ if rawMMDD describes a valid month/day date ->
        #     I
        #   else ->
        #     Log()  +:=  error message
        #     raise SyntaxError ]
        mm = rawMMDD[:2]
        dd = rawMMDD[2:]
        if  not (1 <= int(mm) <= 12):
            scan.syntax("Month '%s' is invalid." % mm)
        if  not (0 <= int(dd) <= 31):
            scan.syntax("Date '%s' is invalid." % dd)

This next prime is the only part of this method different from the same method in the base class.

baseclasses.py
        #-- 3 --
        if  int(mm) >= 10:
            year = str(int(encounter.compiler.year) - 1)
        else:
            year = encounter.compiler.year

        #-- 4 --
        # [ encounter.(fieldName)  :=  encounter.compiler.year +
        #                              rawMMDD ]
        setattr(encounter, fieldName,
                DateField(encounter, year + rawMMDD))