Ignore:
Timestamp:
Jan 10, 2007, 4:50:53 PM (18 years ago)
Author:
duncan
Message:

removing read/write of .xya files from loadASCII.py

Location:
anuga_core/source/anuga/load_mesh
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/load_mesh/loadASCII.py

    r4099 r4165  
    7575##FIXME (DSG-DSG) Is the dict format mentioned above a list of a numeric array?
    7676#  Needs to be defined
    77 ##FIXME (DSG-DSG) if the ascii file isn't .xya give a better error message.
    78 
    79 # FIXME (Ole): Has this stuff been superseded by geospatial data?
    80 # Much of this code is also there
    8177
    8278
     
    973969###
    974970
    975 #FIXME (DSG):  These should be obsolete.  Use geospatial objects.
    976 def export_points_file(ofile, point_dict):
    977     """
    978     write a points file, ofile, as a text (.xya) or binary (.pts) file
    979 
    980     ofile is the file name, including the extension
    981 
    982     The point_dict is defined at the top of this file.
    983     """
    984     #this was done for all keys in the mesh file.
    985     #if not mesh_dict.has_key('points'):
    986     #    mesh_dict['points'] = []
    987     if (ofile[-4:] == ".xya"):
    988         _write_xya_file(ofile, point_dict)
    989     elif (ofile[-4:] == ".pts"):
    990         _write_pts_file(ofile, point_dict)
    991     else:
    992         msg = 'Unknown file type %s ' %ofile
    993         raise IOError, msg
    994                
    995 def import_points_file(ofile, delimiter = None, verbose = False):
    996     """
    997     load an .xya or .pts file
    998 
    999     Note: will throw an IOError if it can't load the file.
    1000     Catch these!
    1001     """
    1002    
    1003     if ofile[-4:]== ".xya":
    1004         try:
    1005             if delimiter == None:
    1006                 try:
    1007                     fd = open(ofile)
    1008                     points_dict = _read_xya_file(fd, ',')
    1009                 except TitleError: # this is catching the error thrown by geo_ref
    1010                     fd.close()
    1011                     fd = open(ofile)
    1012                     points_dict = _read_xya_file(fd, ' ')
    1013             else:
    1014                 fd = open(ofile)
    1015                 points_dict = _read_xya_file(fd, delimiter)
    1016             fd.close()
    1017         except (IndexError,ValueError,SyntaxError):
    1018             fd.close()
    1019             msg = 'Could not open file %s ' %ofile
    1020             raise IOError, msg
    1021         except TitleError:
    1022             print "reclassifying title error"
    1023             fd.close()
    1024             msg = 'Could not open file %s ' %ofile
    1025             raise IOError, msg
    1026         except IOError:
    1027             # Catch this to add an error message
    1028             msg = 'Could not open file %s ' %ofile
    1029             raise IOError, msg
    1030        
    1031     elif ofile[-4:]== ".pts":
    1032         try:
    1033             points_dict = _read_pts_file(ofile, verbose)       
    1034         except IOError, e:   
    1035             msg = 'Could not open file %s ' %ofile
    1036             raise IOError, msg
    1037     else:     
    1038         msg = 'Extension %s is unknown' %ofile[-4:]
    1039         raise IOError, msg
    1040     return points_dict
    1041 
    1042971def extent_point_atts(point_atts):
    1043972    """
     
    11211050    return dic.keys(), point_attributes
    11221051
    1123 def _read_pts_file(file_name, verbose = False):
    1124     """Read .pts NetCDF file
    1125    
    1126     Return a dic of array of points, and dic of array of attribute
    1127 
    1128     eg
    1129     dic['points'] = [[1.0,2.0],[3.0,5.0]]
    1130     dic['attributelist']['elevation'] = [[7.0,5.0]
    1131     """
    1132     #FIXME: (DSG)This format has issues.
    1133     # There can't be an attribute called points
    1134     # consider format change
    1135 
    1136 
    1137     if verbose: print 'Reading ', file_name
    1138 
    1139    
    1140     # see if the file is there.  Throw a QUIET IO error if it isn't
    1141     fd = open(file_name,'r')
    1142     fd.close()
    1143 
    1144     #throws prints to screen if file not present
    1145     fid = NetCDFFile(file_name, 'r')
    1146 
    1147     point_atts = {} 
    1148     # Get the variables
    1149     point_atts['pointlist'] = array(fid.variables['points'])
    1150     keys = fid.variables.keys()
    1151     if verbose: print 'Got %d variables: %s' %(len(keys), keys)
    1152     try:
    1153         keys.remove('points')
    1154     except IOError, e:       
    1155         fid.close()   
    1156         msg = 'Expected keyword "points" but could not find it'
    1157         raise IOError, msg
    1158 
    1159     attributes = {}
    1160     for key in keys:
    1161         if verbose: print "reading attribute '%s'" %key
    1162        
    1163         attributes[key] = array(fid.variables[key])
    1164        
    1165     point_atts['attributelist'] = attributes
    1166    
    1167     try:
    1168         geo_reference = Geo_reference(NetCDFObject=fid)
    1169         point_atts['geo_reference'] = geo_reference
    1170     except AttributeError, e:
    1171         #geo_ref not compulsory
    1172         point_atts['geo_reference'] = None
    1173    
    1174    
    1175     fid.close()
    1176    
    1177     #print "point_atts",point_atts
    1178     return point_atts
    1179 
    1180 def _write_pts_file(file_name, point_atts):
    1181     """Write .pts NetCDF file   
    1182 
    1183     WARNING: This function mangles the point_atts data structure
    1184     """
    1185     #FIXME: (DSG)This format has issues.
    1186     # There can't be an attribute called points
    1187     # consider format change
    1188 
    1189 
    1190     legal_keys = ['pointlist', 'attributelist', 'geo_reference']
    1191     for key in point_atts.keys():
    1192         msg = 'Key %s is illegal. Valid keys are %s' %(key, legal_keys)
    1193         assert key in legal_keys, msg
    1194    
    1195     point_atts2array(point_atts)
    1196     # NetCDF file definition
    1197     outfile = NetCDFFile(file_name, 'w')
    1198    
    1199     #Create new file
    1200     outfile.institution = 'Geoscience Australia'
    1201     outfile.description = 'NetCDF format for compact and portable storage ' +\
    1202                       'of spatial point data'
    1203    
    1204     # dimension definitions
    1205     shape = point_atts['pointlist'].shape[0]
    1206     outfile.createDimension('number_of_points', shape) 
    1207     outfile.createDimension('number_of_dimensions', 2) #This is 2d data
    1208    
    1209     # variable definition
    1210     outfile.createVariable('points', Float, ('number_of_points',
    1211                                              'number_of_dimensions'))
    1212 
    1213     #create variables 
    1214     outfile.variables['points'][:] = point_atts['pointlist'] #.astype(Float32)
    1215     for key in point_atts['attributelist'].keys():
    1216         outfile.createVariable(key, Float, ('number_of_points',))
    1217         outfile.variables[key][:] = point_atts['attributelist'][key] #.astype(Float32)
    1218        
    1219     if point_atts.has_key('geo_reference') and not point_atts['geo_reference'] == None:
    1220         point_atts['geo_reference'].write_NetCDF(outfile)
    1221        
    1222     outfile.close()
    1223    
    1224 def _read_xya_file(fd, delimiter):
    1225     #lines = fd.readlines()
    1226     points = []
    1227     pointattributes = []
    1228     #if len(lines) <= 1:
    1229     #    raise SyntaxError
    1230     title = fd.readline()
    1231     #title = lines.pop(0) # the first (title) line
    1232     att_names = clean_line(title,delimiter)
    1233 
    1234     att_dict = {}
    1235     line = fd.readline()
    1236     numbers = clean_line(line,delimiter)
    1237     #for line in lines:
    1238     while len(numbers) > 1:
    1239         #print "line >%s" %line
    1240         #numbers = clean_line(line,delimiter)
    1241         #print "numbers >%s<" %numbers
    1242         #if len(numbers) < 2 and numbers != []:
    1243            
    1244             # A line without two numbers
    1245             # or a bad delimiter
    1246             #FIXME dsg-dsg change error that is raised.
    1247             #raise SyntaxError
    1248         if numbers != []:
    1249             try:
    1250                 x = float(numbers[0])
    1251                 y = float(numbers[1])
    1252                 points.append([x,y])
    1253                 numbers.pop(0)
    1254                 numbers.pop(0)
    1255                 #attributes = []
    1256                 #print "att_names",att_names
    1257                 #print "numbers",numbers
    1258                 if len(att_names) != len(numbers):
    1259                     fd.close()
    1260                     # It might not be a problem with the title
    1261                     #raise TitleAmountError
    1262                     raise IOError
    1263                 for i,num in enumerate(numbers):
    1264                     num.strip()
    1265                     if num != '\n' and num != '':
    1266                         #attributes.append(float(num))
    1267                         att_dict.setdefault(att_names[i],[]).append(float(num))
    1268                    
    1269             except ValueError:
    1270                 raise SyntaxError
    1271         line = fd.readline()
    1272         numbers = clean_line(line,delimiter)
    1273        
    1274     if line == '':
    1275         # end of file
    1276         geo_reference = None
    1277     else:
    1278         geo_reference = Geo_reference(ASCIIFile=fd,read_title=line)
    1279    
    1280     xya_dict = {}
    1281     xya_dict['pointlist'] = array(points).astype(Float)
    1282    
    1283     for key in att_dict.keys():
    1284         att_dict[key] = array(att_dict[key]).astype(Float)
    1285     xya_dict['attributelist'] = att_dict
    1286     xya_dict['geo_reference'] = geo_reference
    1287     #print "xya_dict",xya_dict
    1288     return xya_dict
    1289 
    12901052#FIXME(dsg), turn this dict plus methods into a class?
    12911053def take_points(dict,indices_to_keep):
     
    13151077    combined['geo_reference'] = dict1['geo_reference']
    13161078    return combined
    1317                  
    1318 def _write_xya_file( file_name, xya_dict, delimiter = ','):
    1319     """
    1320     export a file, ofile, with the xya format
    1321    
    1322     """
    1323     points = xya_dict['pointlist']
    1324     pointattributes = xya_dict['attributelist']
    1325    
    1326     fd = open(file_name,'w')
    1327  
    1328     titlelist = ""
    1329     for title in pointattributes.keys():
    1330         titlelist = titlelist + title + delimiter
    1331     titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter
    1332     fd.write(titlelist+"\n")
    1333     #<vertex #> <x> <y> [attributes]
    1334     for i,vert in enumerate( points):
    1335        
    1336         attlist = ","
    1337         for att in pointattributes.keys():
    1338             attlist = attlist + str(pointattributes[att][i])+ delimiter
    1339         attlist = attlist[0:-len(delimiter)] # remove the last delimiter
    1340         attlist.strip()
    1341         fd.write( str(vert[0]) + delimiter
    1342                   + str(vert[1])
    1343                   + attlist + "\n")
    1344    
    1345     # geo_reference info
    1346     if xya_dict.has_key('geo_reference') and \
    1347            not xya_dict['geo_reference'] is None:
    1348         xya_dict['geo_reference'].write_ASCII(fd)
    1349     fd.close()
    13501079     
    13511080if __name__ == "__main__":
  • anuga_core/source/anuga/load_mesh/test_loadASCII.py

    r3514 r4165  
    1212from Numeric import array, allclose
    1313   
    14 from loadASCII import *
     14from anuga.load_mesh.loadASCII import *
    1515from anuga.coordinate_transforms.geo_reference import Geo_reference
    1616import loadASCII
     
    320320        #print fileName
    321321        try:
    322             dict = import_points_file(fileName,delimiter = ' ')
     322            dict = import_mesh_file(fileName)
    323323        except IOError:
    324324            pass
     
    341341        #print fileName
    342342        try:
    343             dict = import_points_file(fileName,delimiter = ' ')
     343            dict = import_mesh_file(fileName)
    344344        except IOError:
    345345            pass
     
    519519                        'imaginary file did not raise error!')
    520520           
    521     def test_import_msh_bad(self):
     521    def throws_error_2_screen_test_import_mesh_bad(self):
    522522        import os
    523523        import tempfile
     
    533533        #print fileName
    534534        try:
    535             dict = import_points_file(fileName,delimiter = ' ')
     535            dict = import_mesh_file(fileName)
    536536        except IOError:
    537537            pass
     
    540540                        'bad msh file did not raise error!')       
    541541        os.remove(fileName)         
    542    
    543   ###################### .XYA ##############################
    544        
    545     def test_export_xya_file(self):
    546         dict = {}
    547         att_dict = {}
    548         dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    549         att_dict['elevation'] = array([10.0, 0.0, 10.4])
    550         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    551         dict['attributelist'] = att_dict
    552         dict['geo_reference'] = Geo_reference(56,1.9,1.9)
    553        
    554        
    555         fileName = tempfile.mktemp(".xya")
    556         export_points_file(fileName, dict)
    557         dict2 = import_points_file(fileName)
    558         #print "fileName",fileName
    559         os.remove(fileName)
    560         #print "dict2",dict2
    561        
    562         assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    563         assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4])
    564         answer = [10.0, 0.0, 10.4]
    565         assert allclose(dict2['attributelist']['brightness'], answer)
    566         #print "dict2['geo_reference']",dict2['geo_reference']
    567         self.failUnless(dict['geo_reference'] == dict2['geo_reference'],
    568                          'test_writepts failed. Test geo_reference')
    569 
    570     def test_export_xya_file2(self):
    571         dict = {}
    572         att_dict = {}
    573         dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    574         att_dict['elevation'] = array([10.0, 0.0, 10.4])
    575         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    576         dict['attributelist'] = att_dict
    577        
    578        
    579         fileName = tempfile.mktemp(".xya")
    580         export_points_file(fileName, dict)
    581         dict2 = import_points_file(fileName)
    582         #print "fileName",fileName
    583         os.remove(fileName)
    584         #print "dict2",dict2
    585        
    586         assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    587         assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4])
    588         answer = [10.0, 0.0, 10.4]
    589         assert allclose(dict2['attributelist']['brightness'], answer)
    590 
    591        
    592     def test_loadxya(self):
    593         """
    594         comma delimited
    595         """
    596        
    597         fileName = tempfile.mktemp(".xya")
    598         file = open(fileName,"w")
    599         file.write("elevation  , speed \n\
    600 1.0, 0.0, 10.0, 0.0\n\
    601 0.0, 1.0, 0.0, 10.0\n\
    602 1.0, 0.0, 10.4, 40.0\n")
    603         file.close()
    604         #print fileName
    605         dict = import_points_file(fileName,delimiter = ',')
    606         os.remove(fileName)
    607         assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    608         assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
    609         assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
    610 
    611     #FIXME - redundant test?
    612     def test_loadxy(self):
    613         """
    614         To test the mesh side of loading xya files.
    615         Not the loading of xya files
    616         """
    617         import os
    618         import tempfile
    619        
    620         fileName = tempfile.mktemp(".xya")
    621         file = open(fileName,"w")
    622         file.write("elevation speed \n\
    623 1.0 0.0 10.0 0.0\n\
    624 0.0 1.0 0.0 10.0\n\
    625 1.0 0.0 10.4 40.0\n")
    626         file.close()
    627         #print fileName
    628         dict = import_points_file(fileName)
    629         os.remove(fileName)
    630         assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    631         assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
    632         assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
    633 
    634                
    635     def test_loadxya2(self):
    636         """
    637         space delimited
    638         """
    639         import os
    640         import tempfile
    641        
    642         fileName = tempfile.mktemp(".xya")
    643         file = open(fileName,"w")
    644         file.write("  elevation   speed \n\
    645 1.0 0.0 10.0 0.0\n\
    646 0.0 1.0 0.0 10.0\n\
    647 1.0 0.0 10.4 40.0\n")
    648         file.close()
    649         #print fileName
    650         dict = import_points_file(fileName,delimiter = ' ')
    651         os.remove(fileName)
    652         assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    653         assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
    654         assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
    655      
    656     def test_loadxya3(self):
    657         """
    658         space delimited
    659         """
    660         import os
    661         import tempfile
    662        
    663         fileName = tempfile.mktemp(".xya")
    664         file = open(fileName,"w")
    665         file.write("  elevation   speed \n\
    666 1.0 0.0 10.0 0.0\n\
    667 0.0 1.0 0.0 10.0\n\
    668 1.0 0.0 10.4 40.0\n\
    669 #geocrap\n\
    670 56\n\
    671 56.6\n\
    672 3\n")
    673         file.close()
    674         #print fileName
    675         dict = import_points_file(fileName,delimiter = ' ')
    676         os.remove(fileName)
    677         assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    678         assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
    679         assert allclose(dict['attributelist']['speed'], [0.0, 10.0, 40.0])
    680      
    681         geo_reference = Geo_reference(56, 56.6, 3.0)
    682        
    683         self.failUnless(geo_reference == dict['geo_reference'],
    684                          'test_writepts failed. Test geo_reference')
    685        
    686 ########################## BAD .XYA ##########################
    687  
    688     def test_loadxy_bad_no_file_xya(self):
    689         import os
    690         import tempfile
    691        
    692         fileName = tempfile.mktemp(".xya")
    693         #print fileName
    694         try:
    695             dict = import_points_file(fileName,delimiter = ' ')
    696         except IOError:
    697             pass
    698         else:
    699             self.failUnless(0 ==1,
    700                         'imaginary file did not raise error!')
    701  
    702     def test_read_write_points_file_bad(self):
    703         dict = self.tri_dict.copy()
    704         fileName = tempfile.mktemp(".xxx")
    705         try:
    706             export_points_file(fileName,dict)
    707         except IOError:
    708             pass
    709         else:
    710             self.failUnless(0 ==1,
    711                         'bad points file extension did not raise error!')
    712      
    713     def test_read_write_points_file_bad2(self):
    714         dict = {}
    715         att_dict = {}
    716         dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    717         att_dict['elevation'] = array([10.0, 0.0, 10.4])
    718         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    719         dict['attributelist'] = att_dict
    720         dict['geo_reference'] = Geo_reference(56,1.9,1.9)
    721         try:
    722             export_points_file("_???/yeah.xya",dict)
    723         except IOError:
    724             pass
    725         else:
    726             self.failUnless(0 ==1,
    727                         'bad points file extension did not raise error!')
    728                    
    729     def test_loadxy_bad(self):
    730         import os
    731         import tempfile
    732        
    733         fileName = tempfile.mktemp(".xya")
    734         file = open(fileName,"w")
    735         file.write("  elevation   \n\
    736 1.0 0.0 10.0 0.0\n\
    737 0.0 1.0 0.0 10.0\n\
    738 1.0 0.0 10.4 40.0\n")
    739         file.close()
    740         #print fileName
    741         try:
    742             dict = import_points_file(fileName,delimiter = ' ')
    743         except IOError:
    744             pass
    745         else:
    746             self.failUnless(0 ==1,
    747                         'bad xya file did not raise error!')
    748         os.remove(fileName)
    749        
    750     def test_loadxy_bad2(self):
    751         import os
    752         import tempfile
    753        
    754         fileName = tempfile.mktemp(".xya")
    755         file = open(fileName,"w")
    756         file.write("elevation\n\
    757 1.0 0.0 10.0 \n\
    758 0.0 1.0\n\
    759 1.0 \n")
    760         file.close()
    761         #print fileName
    762         try:
    763             dict = import_points_file(fileName,delimiter = ' ')
    764         except IOError:
    765             pass
    766         else:
    767             self.failUnless(0 ==1,
    768                         'bad xya file did not raise error!')   
    769         os.remove(fileName)
    770    
    771     def test_loadxy_bad3(self):
    772         """
    773         specifying wrong delimiter
    774         """
    775         import os
    776         import tempfile
    777        
    778         fileName = tempfile.mktemp(".xya")
    779         file = open(fileName,"w")
    780         file.write("  elevation  , speed \n\
    781 1.0, 0.0, 10.0, 0.0\n\
    782 0.0, 1.0, 0.0, 10.0\n\
    783 1.0, 0.0, 10.4, 40.0\n")
    784         file.close()
    785         try:
    786             dict = import_points_file(fileName,delimiter = ' ')
    787         except IOError:
    788             pass
    789         else:
    790             self.failUnless(0 ==1,
    791                         'bad xya file did not raise error!')   
    792         os.remove(fileName)
    793      
    794     def test_loadxy_bad4(self):
    795         """
    796         specifying wrong delimiter
    797         """
    798         import os
    799         import tempfile
    800        
    801         fileName = tempfile.mktemp(".xya")
    802         file = open(fileName,"w")
    803         file.write("  elevation   speed \n\
    804 1.0 0.0 10.0 0.0\n\
    805 0.0 1.0 0.0 10.0\n\
    806 1.0 0.0 10.4 40.0\n\
    807 yeah")
    808         file.close()
    809         try:
    810             dict = import_points_file(fileName,delimiter = ' ')
    811         except IOError:
    812             pass
    813         else:
    814             self.failUnless(0 ==1,
    815                         'bad xya file did not raise error!')   
    816         os.remove(fileName)
    817  
    818     def test_loadxy_bad4(self):
    819         """
    820         specifying wrong delimiter
    821         """
    822         import os
    823         import tempfile
    824        
    825         fileName = tempfile.mktemp(".xya")
    826         file = open(fileName,"w")
    827         file.write("  elevation   speed \n\
    828 1.0 0.0 10.0 0.0\n\
    829 0.0 1.0 0.0 10.0\n\
    830 1.0 0.0 10.4 40.0\n\
    831 #geocrap")
    832         file.close()
    833         try:
    834             dict = import_points_file(fileName,delimiter = ' ')
    835         except IOError:
    836             pass
    837         else:
    838             self.failUnless(0 ==1,
    839                         'bad xya file did not raise error!')   
    840         os.remove(fileName)
    841 
    842     def test_loadxy_bad5(self):
    843         """
    844         specifying wrong delimiter
    845         """
    846         import os
    847         import tempfile
    848        
    849         fileName = tempfile.mktemp(".xya")
    850         file = open(fileName,"w")
    851         file.write("  elevation   speed \n\
    852 1.0 0.0 10.0 0.0\n\
    853 0.0 1.0 0.0 10.0\n\
    854 1.0 0.0 10.4 40.0\n\
    855 #geocrap\n\
    856 crap")
    857         file.close()
    858         try:
    859             dict = import_points_file(fileName,delimiter = ' ')
    860         except IOError:
    861             pass
    862         else:
    863             self.failUnless(0 ==1,
    864                         'bad xya file did not raise error!')   
    865         os.remove(fileName)       
    866   ############### .PTS ##########
    867            
    868     def test_loadpts(self):
    869        
    870         from Scientific.IO.NetCDF import NetCDFFile
    871 
    872         fileName = tempfile.mktemp(".pts")
    873         # NetCDF file definition
    874         outfile = NetCDFFile(fileName, 'w')
    875        
    876         # dimension definitions
    877         outfile.createDimension('number_of_points', 3)   
    878         outfile.createDimension('number_of_dimensions', 2) #This is 2d data
    879    
    880         # variable definitions
    881         outfile.createVariable('points', Float, ('number_of_points',
    882                                                  'number_of_dimensions'))
    883         outfile.createVariable('elevation', Float, ('number_of_points',))
    884    
    885         # Get handles to the variables
    886         points = outfile.variables['points']
    887         elevation = outfile.variables['elevation']
    888  
    889         points[0, :] = [1.0,0.0]
    890         elevation[0] = 10.0
    891         points[1, :] = [0.0,1.0]
    892         elevation[1] = 0.0 
    893         points[2, :] = [1.0,0.0]
    894         elevation[2] = 10.4   
    895 
    896         outfile.close()
    897        
    898         dict = import_points_file(fileName)
    899         os.remove(fileName)
    900         answer =  [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]
    901         #print "dict['pointlist']",dict['pointlist']
    902         #print "answer",answer
    903         assert allclose(dict['pointlist'], [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    904         assert allclose(dict['attributelist']['elevation'], [10.0, 0.0, 10.4])
    905  
    906     def test_writepts(self):
    907         dict = {}
    908         att_dict = {}
    909         dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    910         att_dict['elevation'] = array([10.0, 0.0, 10.4])
    911         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    912         dict['attributelist'] = att_dict
    913         dict['geo_reference'] = Geo_reference(56,1.9,1.9)
    914        
    915        
    916         fileName = tempfile.mktemp(".pts")
    917         export_points_file(fileName, dict)
    918         dict2 = import_points_file(fileName)
    919         #print "fileName",fileName
    920         os.remove(fileName)
    921         #print "dict2",dict2
    922        
    923         assert allclose(dict2['pointlist'],[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    924         assert allclose(dict2['attributelist']['elevation'], [10.0, 0.0, 10.4])
    925         answer = [10.0, 0.0, 10.4]
    926         assert allclose(dict2['attributelist']['brightness'], answer)
    927 
    928         #print "dict['geo_reference'] ",dict['geo_reference']
    929         #print "dict2['geo_reference']",dict2['geo_reference']
    930        
    931         self.failUnless(dict['geo_reference'] == dict2['geo_reference'],
    932                          'test_writepts failed. Test geo_reference')
    933        
    934  ########################## BAD .PTS ##########################         
    935 
    936     def test_load_bad_no_file_pts(self):
    937         import os
    938         import tempfile
    939        
    940         fileName = tempfile.mktemp(".pts")
    941         #print fileName
    942         try:
    943             dict = import_points_file(fileName)
    944         except IOError:
    945             pass
    946         else:
    947             self.failUnless(0 ==1,
    948                         'imaginary file did not raise error!')
    949            
    950   ############### .PTS OTHER ##########
    951 
    952     def test_concatinate_attributelist(self):
    953         dic = {}
    954         dic['one'] = array([1,2])
    955         dic['2'] = array([2,7])
    956         dic['three'] = array([3,79])
    957         dic['4'] = array([4,47])
    958         dic['five'] = array([5,17])
    959         titles, block = concatinate_attributelist(dic)
    960         #print "titles", titles
    961         #print "array", block
    962         self.failUnless(titles  == ['4', '2', 'five', 'three', 'one'],
    963                          'test_concatinate_attributelist failed.')
    964         assert allclose(block, [[4,2,5,3,1],[47,7,17,79,2]])
    965 
    966     def test_half_pts(self):
    967         dict = {}
    968         att_dict = {}
    969         dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    970         att_dict['elevation'] = array([10.0, 0.0, 10.4])
    971         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    972         dict['attributelist'] = att_dict
    973         out_dict = half_pts(dict)
    974         assert allclose(out_dict['pointlist'],[[1.0, 0.0],[1.0, 0.0]])
    975         assert allclose(out_dict['attributelist']['elevation'], [10.0, 10.4])
    976         assert allclose(out_dict['attributelist']['brightness'], [10.0, 10.4])
    977  
    978     def test_extent_point_atts(self):
    979        
    980         dict = {}
    981         att_dict = {}
    982         dict['pointlist'] = array([[1.0, 10.0],[0.0, 1.0],[10.0, -10.0]])
    983         att_dict['elevation'] = array([30.0, 0.0, 10.4])
    984         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    985         dict['attributelist'] = att_dict
    986         out_dict = extent_point_atts(dict)
    987        
    988         #print "out_dict['pointlist']",out_dict #['pointlist']
    989         assert allclose(out_dict['pointlist'],[[0.0, -10.0],[10.0, -10.0],
    990                                                [10.0,10.0],[0.0, 10.0]])
    991 
    992         self.failUnless(dict['attributelist']  == {},
    993                          'test_extent_point_atts failed. Test 1')
    994        
    995     def test_reduce_pts(self):
    996         dict = {}
    997         att_dict = {}
    998         dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    999         att_dict['elevation'] = array([10.0, 0.0, 10.4])
    1000         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    1001         dict['attributelist'] = att_dict
    1002        
    1003         inFileName = tempfile.mktemp(".pts")
    1004         export_points_file(inFileName, dict)
    1005        
    1006         outFileName = tempfile.mktemp(".pts")
    1007        
    1008         dict2 = reduce_pts(inFileName,outFileName, 1 )
    1009         os.remove(inFileName)
    1010 
    1011         dict2 = import_points_file(outFileName)
    1012         os.remove(outFileName)
    1013         #print "dict2",dict2
    1014        
    1015         assert allclose(dict2['pointlist'],[[1.0, 0.0]])
    1016         assert allclose(dict2['attributelist']['elevation'], [10.0])
    1017         assert allclose(dict2['attributelist']['brightness'], [10.0])
    1018        
    1019     def test_produce_half_point_files(self):
    1020         dict = {}
    1021         att_dict = {}
    1022         dict['pointlist'] = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    1023         att_dict['elevation'] = array([10.0, 0.0, 10.4])
    1024         att_dict['brightness'] = array([10.0, 0.0, 10.4])
    1025         dict['attributelist'] = att_dict
    1026        
    1027         inFileName = tempfile.mktemp(".pts")
    1028         export_points_file(inFileName, dict)
    1029        
    1030         outFileName = tempfile.mktemp(".pts")
    1031         delimiter = '_p'
    1032         outfiles = produce_half_point_files(inFileName, 1,
    1033                                             delimiter, verbose = False )
    1034         os.remove(inFileName)
    1035         root, ext = splitext(inFileName)
    1036         outFileName = root + delimiter + ext
    1037         #print "outFileName",outfiles 
    1038         dict2 = import_points_file(outfiles[1])
    1039         for file in outfiles:
    1040             #print "del file",file
    1041             os.remove(file)
    1042        
    1043         assert allclose(dict2['pointlist'],[[1.0, 0.0]])
    1044         assert allclose(dict2['attributelist']['elevation'], [10.0])
    1045         assert allclose(dict2['attributelist']['brightness'], [10.0])
    1046                      
     542             
    1047543#-------------------------------------------------------------
    1048544if __name__ == "__main__":
     
    1050546    suite = unittest.makeSuite(loadASCIITestCase,'test')
    1051547    #suite = unittest.makeSuite(loadASCIITestCase,'test_writepts')
    1052     runner = unittest.TextTestRunner(verbosity=0)
     548    runner = unittest.TextTestRunner() #verbosity=0)
    1053549    runner.run(suite)
    1054550   
Note: See TracChangeset for help on using the changeset viewer.