Changeset 3414 for nappy


Ignore:
Timestamp:
14/02/08 12:36:06 (11 years ago)
Author:
astephen
Message:

Tidied up _fixHeaderLength in na_file.py.
Changed write interface so self.write(na_dict, delimiter, float_format)
is a separate call to creating the instance.
Fixed unit tests to cope with this.

BUT: broke nappy_api.openNAFile(<file>, "w") with these changes.
Hence need to think about changes being made to openNAFile() so that it
can predict (or not) the FFI.

Only 1001 unit test (worked on by CK) is broken at present.

Location:
nappy/trunk/nappy
Files:
1 added
18 edited

Legend:

Unmodified
Added
Removed
  • nappy/trunk/nappy/na_file/na_file.py

    r3413 r3414  
    3535    functionality for the NASA Ames format. 
    3636 
    37     When a sub-class of NAFile is called with a read ('r' - default) or 
    38     write ('w') mode the header in the file is automatically read or written. 
    39     However, the user must explicitly read or write the data section with a 
    40     call to the 'readData' or 'writeData' methods. 
     37    When a sub-class of NAFile is called with a read ('r' - default)  
     38    mode the header in the file is automatically read. To read the data 
     39    section the user must call the 'readData' method. 
     40       
     41    When a sub-class of NAFile is called with the write ('w') mode 
     42    then the file is opened but nothing is written. The user must then  
     43    send an 'na_dict' object to the 'write' method to write the output. 
     44    The output file is then flushed to ensure the data is written even if  
     45    the user forgets to close it. 
    4146    """ 
    4247 
    43     def __init__(self, filename, mode="r", na_dict={}, delimiter="    ", float_format="%8.3f"): 
     48    def __init__(self, filename, mode="r"):  
    4449        """ 
    4550        Initialization of class, decides if user wishes to read or write 
    4651        NASA Ames file. 
    4752        """ 
    48         print "delimiter is", delimiter 
    4953        nappy.na_file.na_core.NACore.__init__(self) 
    50         self.na_dict = na_dict 
    5154        self.filename = filename 
    5255        self._open(mode) 
    53         self.delimiter = delimiter 
    54         self.float_format = float_format 
    55  
    56         if mode == "r": 
     56        self.mode = mode 
     57 
     58        if self.mode == "r": 
    5759            self._normalized_X = True 
    5860            self.readHeader() 
    59         elif mode == "w": 
    60             self._parseDictionary() 
    61             self.header = StringIO.StringIO() 
    62             self.writeHeader() 
    63             self.writeData() 
     61        elif self.mode == "w": 
     62            # Self flag to check if data written 
     63            self.data_written = False 
    6464        else: 
    65             raise "Unknown file mode '%s'." % mode 
    66         self.close() 
     65            raise "Unknown file mode '%s'." % self.mode 
     66 
    6767         
    6868    def _open(self, mode): 
    6969        "Wrapper to builtin open file function." 
    7070        self.file = open(self.filename, mode) 
    71  
     71        self.is_open = True 
     72 
     73    def write(self, na_dict, delimiter="    ", float_format="%8.3f"): 
     74        """ 
     75        Writes an na_dict to the file and then flushes it to ensure data not  
     76        being buffered. 
     77        """ 
     78        self.delimiter = delimiter 
     79        self.float_format = float_format 
     80        # Raise errors if dangerous behaviour 
     81        if self.mode != "w": 
     82            raise Exception("WARNING: Cannot write to read-only file. Can only write to NA file object when mode='w'.") 
     83 
     84        if self.data_written == True: 
     85            raise Exception("WARNING: Cannot write multiple NASA Ames dictionaries to a single file. Please open a new NASA Ames file instance to write new data to.") 
     86 
     87        if self.is_open == False: 
     88            raise Exception("WARNING: NASA Ames file instance is closed and cannot be written to.") 
     89    
     90        # Parse na_dict then write header and data 
     91        self._parseDictionary(na_dict) 
     92        self.header = StringIO.StringIO() 
     93        self.writeHeader() 
     94        self.writeData() 
     95        self.file.flush() 
     96         
     97        # Set flag to make sure cannot try and write more data 
     98        self.data_written = True 
     99          
     100      
    72101    def close(self): 
    73102        "Wrapper to builtin close file function." 
    74103        self.file.close() 
    75  
    76     def _parseDictionary(self): 
     104        self.is_open = False 
     105 
     106    def _parseDictionary(self, na_dict): 
    77107        """ 
    78108        Parser for the optional na_dict argument containing a dictionary 
     
    80110        with the name used in the NASA Ames documentation. 
    81111        """ 
     112        self.na_dict = na_dict 
    82113        for i in self.na_dict.keys(): 
    83114            setattr(self, i, self.na_dict[i]) 
     
    122153        return rtlines 
    123154 
     155 
    124156    def _readCommonHeader(self): 
    125157        """ 
     
    226258        self.header.write("%s\n" * self.NNCOML % tuple(self.NCOM)) 
    227259 
    228     def _fixHeaderLength(self, header): 
    229         """ 
    230         Takes the header StringIO object and counts the number of lines to 
    231         return a list of lines with the correct number of header lines in  
    232         the top line. 
    233         """ 
    234         header.seek(0) 
    235         lines = header.readlines() 
     260    def _fixHeaderLength(self): 
     261        """ 
     262        Takes the self.header StringIO object and counts the number of lines 
     263        and corrects the NLHEAD value in the header line. 
     264        Resets to start of self.header. 
     265        """ 
     266        self.header.seek(0) 
     267        lines = self.header.readlines() 
    236268        headlength = len(lines) 
    237269        lines[0] = "%s%s%s\n" % (headlength, self.delimiter, self.FFI) 
    238         return lines 
     270        self.header = StringIO.StringIO("".join(lines)) 
     271        self.header.seek(0)  
    239272 
    240273    def _readSpecialComments(self): 
  • nappy/trunk/nappy/na_file/na_file_1001.py

    r3413 r3414  
    4242        self._writeVariablesHeaderSection() 
    4343        self._writeComments() 
    44         lines = self._fixHeaderLength(self.header) 
    45         self.file.writelines(lines) 
     44        self._fixHeaderLength() 
     45        self.file.write(self.header.read()) 
    4646 
    4747    def _setupArrays(self): 
  • nappy/trunk/nappy/na_file/na_file_1010.py

    r3356 r3414  
    4646        self._writeAuxVariablesHeaderSection()                                   
    4747        self._writeComments()                                                    
    48         lines = self._fixHeaderLength(self.header) 
    49         self.file.writelines(lines)       
     48        self._fixHeaderLength() 
     49        self.file.write(self.header.read()) 
    5050 
    5151    def _setupArrays(self): 
  • nappy/trunk/nappy/na_file/na_file_1020.py

    r3404 r3414  
    7777        self._writeAuxVariablesHeaderSection()                                                   
    7878        self._writeComments()                                                            
    79         lines = self._fixHeaderLength(self.header) 
    80         self.file.writelines(lines) 
     79        self._fixHeaderLength() 
     80        self.file.write(self.header.read()) 
    8181 
    8282    def writeData(self):                                                                                 
  • nappy/trunk/nappy/na_file/na_file_2010.py

    r3383 r3414  
    7575        self._writeAuxVariablesHeaderSection() 
    7676        self._writeComments()          
    77         lines = self._fixHeaderLength(self.header) 
    78         self.file.writelines(lines) 
     77        self._fixHeaderLength() 
     78        self.file.write(self.header.read()) 
     79 
    7980 
    8081    def _setupArrays(self): 
  • nappy/trunk/nappy/na_file/na_file_2110.py

    r3356 r3414  
    4848        self._writeAuxVariablesHeaderSection() 
    4949        self._writeComments() 
    50         lines = self._fixHeaderLength(self.header) 
    51         self.file.writelines(lines) 
     50        self._fixHeaderLength() 
     51        self.file.write(self.header.read()) 
     52 
    5253 
    5354    def _setupArrays(self): 
  • nappy/trunk/nappy/na_file/na_file_2160.py

    r3404 r3414  
    5050        self._writeAuxVariablesHeaderSection() 
    5151        self._writeComments() 
    52         lines = self._fixHeaderLength(self.header) 
    53         self.file.writelines(lines)      
     52        self._fixHeaderLength() 
     53        self.file.write(self.header.read()) 
    5454 
    5555    def _writeAuxVariablesHeaderSection(self): 
  • nappy/trunk/nappy/na_file/na_file_2310.py

    r3356 r3414  
    4848        self._writeAuxVariablesHeaderSection() 
    4949        self._writeComments() 
    50         lines = self._fixHeaderLength(self.header) 
    51         self.file.writelines(lines) 
     50        self._fixHeaderLength() 
     51        self.file.write(self.header.read()) 
    5252 
    5353    def _readData1(self, datalines, ivar_count): 
  • nappy/trunk/nappy/nappy_api.py

    r3383 r3414  
    3131            na_dict['FFI'] = ffi 
    3232            print "\nFormat identified as:", ffi     
    33         return apply(getNAFileClass(ffi), (filename,), {"mode":mode, "na_dict":na_dict, "delimiter":delimiter, "float_format":float_format}) 
     33        return apply(getNAFileClass(ffi), (filename,), {"mode":mode, "na_dict":na_dict}) #, "delimiter":delimiter, "float_format":float_format}) 
    3434    else: 
    3535        raise Exception("File mode not recognised '" + mode + "'.") 
  • nappy/trunk/nappy/unit_tests/test_na_file_1001.py

    r3389 r3414  
    1313 
    1414# Import local modules 
    15 base_dir = os.environ.get("NAPPY_BASE_DIR", "..\\..") 
     15base_dir = os.environ.get("NAPPY_BASE_DIR", "../..") 
    1616sys.path.append(base_dir) 
    1717 
    1818import nappy 
    19 import nappy.na_file.na_file 
     19import nappy.na_file.na_file_1001 
    2020 
    2121class NAFile1001_TestCase(unittest.TestCase): 
     
    3434    def test_write1001(self): 
    3535        "Tests writing FFI 1001." 
    36         self.fobj = nappy.openNAFile(self.outfile, mode="w", na_dict=self.na_dict)               
     36        self.fobj = nappy.na_file.na_file_1001.NAFile1001(self.outfile, mode="w")                
     37        self.fobj.write(na_dict=self.na_dict) 
    3738        self.failUnless(isinstance(self.fobj, nappy.na_file.na_file.NAFile)) 
    3839 
     
    4142    unittest.main() 
    4243 
     44 
  • nappy/trunk/nappy/unit_tests/test_na_file_1010.py

    r3355 r3414  
    1313 
    1414# Import local modules 
    15 base_dir = os.environ.get("NAPPY_BASE_DIR", "..\\..") 
     15base_dir = os.environ.get("NAPPY_BASE_DIR", "../..") 
    1616sys.path.append(base_dir) 
    1717 
    1818import nappy 
    19 import nappy.na_file.na_file 
     19import nappy.na_file.na_file_1010 
    2020 
    2121class NAFile1010_TestCase(unittest.TestCase): 
     
    3434    def test_write1010(self): 
    3535        "Tests writing FFI 1010." 
    36         self.fobj = nappy.openNAFile(self.outfile, mode="w", na_dict=self.na_dict)               
     36        self.fobj = nappy.na_file.na_file_1010.NAFile1010(self.outfile, mode="w")                
     37        self.fobj.write(na_dict=self.na_dict) 
    3738        self.failUnless(isinstance(self.fobj, nappy.na_file.na_file.NAFile)) 
    3839 
     
    4142    unittest.main() 
    4243 
     44 
  • nappy/trunk/nappy/unit_tests/test_na_file_1020.py

    r3355 r3414  
    1313 
    1414# Import local modules 
    15 base_dir = os.environ.get("NAPPY_BASE_DIR", "..\\..") 
     15base_dir = os.environ.get("NAPPY_BASE_DIR", "../..") 
    1616sys.path.append(base_dir) 
    1717 
    1818import nappy 
    19 import nappy.na_file.na_file 
     19import nappy.na_file.na_file_1020 
    2020 
    2121class NAFile1020_TestCase(unittest.TestCase): 
     
    3434    def test_write1020(self): 
    3535        "Tests writing FFI 1020." 
    36         self.fobj = nappy.openNAFile(self.outfile, mode="w", na_dict=self.na_dict)               
     36        self.fobj = nappy.na_file.na_file_1020.NAFile1020(self.outfile, mode="w")                
     37        self.fobj.write(na_dict=self.na_dict) 
    3738        self.failUnless(isinstance(self.fobj, nappy.na_file.na_file.NAFile)) 
    3839 
     
    4142    unittest.main() 
    4243 
     44 
  • nappy/trunk/nappy/unit_tests/test_na_file_2010.py

    r3355 r3414  
    1313 
    1414# Import local modules 
    15 base_dir = os.environ.get("NAPPY_BASE_DIR", "..\\..") 
     15base_dir = os.environ.get("NAPPY_BASE_DIR", "../..") 
    1616sys.path.append(base_dir) 
    1717 
    1818import nappy 
    19 import nappy.na_file.na_file 
     19import nappy.na_file.na_file_2010 
    2020 
    2121class NAFile2010_TestCase(unittest.TestCase): 
     
    3434    def test_write2010(self): 
    3535        "Tests writing FFI 2010." 
    36         self.fobj = nappy.openNAFile(self.outfile, mode="w", na_dict=self.na_dict)               
     36        self.fobj = nappy.na_file.na_file_2010.NAFile2010(self.outfile, mode="w")                
     37        self.fobj.write(na_dict=self.na_dict) 
    3738        self.failUnless(isinstance(self.fobj, nappy.na_file.na_file.NAFile)) 
    3839 
     
    4142    unittest.main() 
    4243 
     44 
  • nappy/trunk/nappy/unit_tests/test_na_file_2110.py

    r3355 r3414  
    1313 
    1414# Import local modules 
    15 base_dir = os.environ.get("NAPPY_BASE_DIR", "..\\..") 
     15base_dir = os.environ.get("NAPPY_BASE_DIR", "../..") 
    1616sys.path.append(base_dir) 
    1717 
    1818import nappy 
    19 import nappy.na_file.na_file 
     19import nappy.na_file.na_file_2110 
    2020 
    2121class NAFile2110_TestCase(unittest.TestCase): 
     
    3434    def test_write2110(self): 
    3535        "Tests writing FFI 2110." 
    36         self.fobj = nappy.openNAFile(self.outfile, mode="w", na_dict=self.na_dict)               
     36        self.fobj = nappy.na_file.na_file_2110.NAFile2110(self.outfile, mode="w")                
     37        self.fobj.write(na_dict=self.na_dict) 
    3738        self.failUnless(isinstance(self.fobj, nappy.na_file.na_file.NAFile)) 
    3839 
     
    4142    unittest.main() 
    4243 
     44 
  • nappy/trunk/nappy/unit_tests/test_na_file_2160.py

    r3355 r3414  
    1313 
    1414# Import local modules 
    15 base_dir = os.environ.get("NAPPY_BASE_DIR", "..\\..") 
     15base_dir = os.environ.get("NAPPY_BASE_DIR", "../..") 
    1616sys.path.append(base_dir) 
    1717 
    1818import nappy 
    19 import nappy.na_file.na_file 
     19import nappy.na_file.na_file_2160 
    2020 
    2121class NAFile2160_TestCase(unittest.TestCase): 
     
    3434    def test_write2160(self): 
    3535        "Tests writing FFI 2160." 
    36         self.fobj = nappy.openNAFile(self.outfile, mode="w", na_dict=self.na_dict)               
     36        self.fobj = nappy.na_file.na_file_2160.NAFile2160(self.outfile, mode="w")                
     37        self.fobj.write(na_dict=self.na_dict) 
    3738        self.failUnless(isinstance(self.fobj, nappy.na_file.na_file.NAFile)) 
    3839 
     
    4142    unittest.main() 
    4243 
     44 
  • nappy/trunk/nappy/unit_tests/test_na_file_2310.py

    r3355 r3414  
    1313 
    1414# Import local modules 
    15 base_dir = os.environ.get("NAPPY_BASE_DIR", "..\\..") 
     15base_dir = os.environ.get("NAPPY_BASE_DIR", "../..") 
    1616sys.path.append(base_dir) 
    1717 
    1818import nappy 
    19 import nappy.na_file.na_file 
     19import nappy.na_file.na_file_2310 
    2020 
    2121class NAFile2310_TestCase(unittest.TestCase): 
     
    3434    def test_write2310(self): 
    3535        "Tests writing FFI 2310." 
    36         self.fobj = nappy.openNAFile(self.outfile, mode="w", na_dict=self.na_dict)               
     36        self.fobj = nappy.na_file.na_file_2310.NAFile2310(self.outfile, mode="w")                
     37        self.fobj.write(na_dict=self.na_dict) 
    3738        self.failUnless(isinstance(self.fobj, nappy.na_file.na_file.NAFile)) 
    3839 
     
    4142    unittest.main() 
    4243 
     44 
  • nappy/trunk/nappy/unit_tests/test_na_file_3010.py

    r3355 r3414  
    1313 
    1414# Import local modules 
    15 base_dir = os.environ.get("NAPPY_BASE_DIR", "..\\..") 
     15base_dir = os.environ.get("NAPPY_BASE_DIR", "../..") 
    1616sys.path.append(base_dir) 
    1717 
    1818import nappy 
    19 import nappy.na_file.na_file 
     19import nappy.na_file.na_file_3010 
    2020 
    2121class NAFile3010_TestCase(unittest.TestCase): 
     
    3434    def test_write3010(self): 
    3535        "Tests writing FFI 3010." 
    36         self.fobj = nappy.openNAFile(self.outfile, mode="w", na_dict=self.na_dict)               
     36        self.fobj = nappy.na_file.na_file_3010.NAFile3010(self.outfile, mode="w")                
     37        self.fobj.write(na_dict=self.na_dict) 
    3738        self.failUnless(isinstance(self.fobj, nappy.na_file.na_file.NAFile)) 
    3839 
     
    4142    unittest.main() 
    4243 
     44 
  • nappy/trunk/nappy/unit_tests/test_na_file_4010.py

    r3355 r3414  
    1313 
    1414# Import local modules 
    15 base_dir = os.environ.get("NAPPY_BASE_DIR", "..\\..") 
     15base_dir = os.environ.get("NAPPY_BASE_DIR", "../..") 
    1616sys.path.append(base_dir) 
    1717 
    1818import nappy 
    19 import nappy.na_file.na_file 
     19import nappy.na_file.na_file_4010 
    2020 
    2121class NAFile4010_TestCase(unittest.TestCase): 
     
    3434    def test_write4010(self): 
    3535        "Tests writing FFI 4010." 
    36         self.fobj = nappy.openNAFile(self.outfile, mode="w", na_dict=self.na_dict)               
     36        self.fobj = nappy.na_file.na_file_4010.NAFile4010(self.outfile, mode="w")                
     37        self.fobj.write(na_dict=self.na_dict) 
    3738        self.failUnless(isinstance(self.fobj, nappy.na_file.na_file.NAFile)) 
    3839 
     
    4142    unittest.main() 
    4243 
     44 
Note: See TracChangeset for help on using the changeset viewer.