Ignore:
Timestamp:
May 23, 2006, 9:11:57 AM (19 years ago)
Author:
duncan
Message:

added more tests to geo_ref. It throws a different error as well now. Updated cornell room

Location:
inundation/coordinate_transforms
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/coordinate_transforms/geo_reference.py

    r2608 r2941  
    1010from utilities.numerical_tools import ensure_numeric
    1111from utilities.anuga_exceptions import ANUGAError
     12
     13import exceptions
     14class TitleError(exceptions.IOError): pass
    1215
    1316DEFAULT_ZONE = -1
     
    3437        ASCIIFile - a handle to the text file
    3538        read_title - the title of the georeference text, if it was read in.
     39         If the function that calls this has already read the title line,
     40         it can't unread it, so this info has to be passed.
     41         If you know of a way to unread this info, then tell us.
    3642        """
    3743
     
    106112        #print "gr TITLE[0:2].upper()",TITLE[0:2].upper()
    107113        if read_title[0:2].upper() != TITLE[0:2].upper():
    108             raise SyntaxError
     114            msg = 'File error.  Expecting line: %s.  Got this line: %s' \
     115                  %(TITLE, read_title)
     116            raise TitleError, msg
    109117        self.zone = int(fd.readline())
    110118        self.xllcorner = float(fd.readline())
  • inundation/coordinate_transforms/test_geo_reference.py

    r2594 r2941  
    7272        self.failUnless(g == new_g, 'test_read_write_ASCII failed')
    7373       
    74     def test_read_write_ASCII2(self):
     74    def test_read_write_ASCII3(self):
    7575        from Scientific.IO.NetCDF import NetCDFFile
    7676        g = Geo_reference(56,1.9,1.9)
     
    8686            fd.close()
    8787            os.remove(file_name)
    88         except SyntaxError:
     88        except TitleError:
    8989            fd.close()
    9090            os.remove(file_name)
     
    254254            msg = 'Should have raised an exception'
    255255            raise msg
    256        
    257        
    258        
    259        
    260        
    261        
     256 
     257    def test_bad_ASCII_title(self):     
     258 # create an .xya file
     259        point_file = tempfile.mktemp(".xya")
     260        fd = open(point_file,'w')
     261        fd.write("# hey! \n")
     262        fd.close()
     263       
     264        fd = open(point_file,'r')
     265        #
     266        #new_g = Geo_reference(ASCIIFile=fd)
     267        try:
     268            new_g = Geo_reference(ASCIIFile=fd)
     269            fd.close()
     270            os.remove(point_file)
     271        except TitleError:
     272            fd.close()
     273            os.remove(point_file)
     274        else:
     275            self.failUnless(0 ==1,
     276                        'bad text file did not raise error!')
     277            os.remove(point_file)
     278
     279    def test_read_write_ASCII_test_and_fail(self):
     280        from Scientific.IO.NetCDF import NetCDFFile
     281
     282        # This is to test a fail
     283        g = Geo_reference(56,1.9,1.9)
     284        file_name = tempfile.mktemp(".geo_referenceTest")
     285        fd = open(file_name,'w')
     286        g.write_ASCII(fd)
     287        fd.close()       
     288        fd = open(file_name,'r')
     289        line = fd.readline()
     290        line = " #Geo"
     291        try:
     292            new_g = Geo_reference(ASCIIFile=fd, read_title=line)
     293            fd.close()
     294            os.remove(file_name)
     295        except TitleError:
     296            fd.close()
     297            os.remove(file_name)
     298        else:
     299            self.failUnless(0 ==1,
     300                        'bad text file did not raise error!')
     301
     302        # this tests a pass
     303        g = Geo_reference(56,1.9,1.9)
     304        file_name = tempfile.mktemp(".geo_referenceTest")
     305        fd = open(file_name,'w')
     306        g.write_ASCII(fd)
     307        fd.close()
     308       
     309        fd = open(file_name,'r')
     310        line = fd.readline()
     311        line = "#geo_yeah"
     312        new_g = Geo_reference(ASCIIFile=fd, read_title=line)
     313        fd.close()
     314        os.remove(file_name)
     315
     316        self.failUnless(g == new_g, 'test_read_write_ASCII failed')
     317       
     318        # this tests a pass
     319        g = Geo_reference(56,1.9,1.9)
     320        file_name = tempfile.mktemp(".geo_referenceTest")
     321        fd = open(file_name,'w')
     322        g.write_ASCII(fd)
     323        fd.close()
     324       
     325        fd = open(file_name,'r')
     326        line = fd.readline()
     327        line = "#geo crap"
     328        new_g = Geo_reference(ASCIIFile=fd, read_title=line)
     329        fd.close()
     330        os.remove(file_name)
     331
     332        self.failUnless(g == new_g, 'test_read_write_ASCII failed')
     333       
     334    def xxtest_good_title(self):     
     335 # create an .xya file
     336        point_file = tempfile.mktemp(".xya")
     337        fd = open(point_file,'w')
     338        fd.write("#Geo crap \n 56\n ")
     339        fd.close()
     340       
     341        fd = open(point_file,'r')
     342        #
     343        #new_g = Geo_reference(ASCIIFile=fd)
     344        try:
     345            new_g = Geo_reference(ASCIIFile=fd)
     346            fd.close()
     347            os.remove(point_file)
     348        except TitleError:
     349            fd.close()
     350            os.remove(point_file)
     351        else:
     352            self.failUnless(0 ==1,
     353                        'bad text file did not raise error!')
     354            os.remove(point_file)
     355         
    262356#-------------------------------------------------------------
    263357if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.