Next / Previous / Contents / Shipman's homepage

19.4. BaseCompiler.parseFileName(): Parse the batch file name

This method examines the name of the input file and verifies that it conforms to the batch file naming scheme. Assuming that it does, it stores away the year in self.year, and sets either self.location or self.station, depending on whether the name starts with a digit or not (digit implies station number). It sets the other attribute to None, so that for example if the file name is 16205-2004, self.station is set to the Station object for station 16205 and self.location is set to none. Conversely, a file name of yose-2007-5 would cause self.location to be set to the Location object for Yosemite, while self.station would be set to None.

baseclasses.py
# - - -   B a s e C o m p i l e r . p a r s e F i l e N a m e   - - -

    def parseFileName(self):
        '''Process the name of a batch file.

          [ if self.fileName conforms to the batch file naming scheme and
            represents a multi-station location in self.stationSet ->
              self.location  :=  a Location object representing that
                                 location
              self.station   :=  None
              self.year      :=  year from self.fileName
            else if self.fileName conforms to the batch file naming
            scheme and represents a single-station location in
            self.stationSet ->
              self.location  :=  None
              self.station   :=  a Station object representing that
                                 station
              self.year      :=  year from self.fileName
            else ->
              Log()  +:=  error message(s)
              raise ValueError ]
        '''

We'll use the compiled regular expressions LOCATION_PAT and STA_NO_PAT to check to see if the file name starts with either a location code or a station number.

baseclasses.py
        #-- 1 --
        self.location = self.station = None

See Section 19.5, “BaseCompiler.parseFileLocality(): Find the location or station code in a batch file name”.

baseclasses.py
        #-- 2 - 
        # [ if self.fileName starts with a string that matches
        #   LOCATION_PAT and is a location code defined in
        #   self.stationSet ->
        #     rest  :=  fileName past that string
        #     self.location  :=  the Location with that location code
        #                        from self.stationSet
        #   else if self.fileName starts with a string that matches
        #   STA_NO_PAT and is a station number in self.stationSet ->
        #     rest  :=  fileName past that string
        #     self.station  :=  the Station with that station number
        #                       from self.stationSet
        #   else ->
        #     Log()  +:=  error message
        #     raise ValueError ]
        rest = self.parseFileLocality()

A hyphen must separate the location or station code and the batch's four-digit year.

baseclasses.py
        #-- 3 --
        # [ if rest starts with "-" ->
        #     rest  :=  rest without its first character
        #   else ->
        #     Log()  +:=  error message
        #     raise ValueError ]
        if ((len(rest) == 0) or
            (rest[0] != '-')):
          message  = ("A hyphen must follow the location or station "
                        "of the file name.")
          Log().message(message)
          raise ValueError, message
        else:
          rest = rest[1:]

The four-digit year must be next. We ignore any part of the file name past the year.

baseclasses.py
        #-- 4 --
        # [ if rest starts with YYYY_PAT ->
        #     self.year  :=  the part that matched YYYY_PAT
        #   else ->
        #     Log()  +:=  error message
        #     raise ValueError ]
        m = YYYY_PAT.match(rest)
        if  m is None:
            message  = ("A four-digit year must follow the hyphen "
                "after the location or station code.")
            Log().error(message)
            raise ValueError, message
        else:
            self.year = m.group()