Changeset 2940


Ignore:
Timestamp:
May 22, 2006, 2:36:04 PM (18 years ago)
Author:
nick
Message:

fixed geospatial_data more

Location:
inundation/geospatial_data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/geospatial_data/geospatial_data.py

    r2935 r2940  
    8686            dic['attributelist']['elevation'] = [[7.0,5.0]
    8787               
    88         delimiter:
     88        delimiter: is the file delimiter that will be used when
     89            importing the file
    8990           
    9091        verbose:
     
    9596            # assume data point is really a file name
    9697            file_name = data_points
     98
     99        self.set_verbose(verbose)
    97100
    98101        if file_name is None:
     
    100103                msg = 'No file specified yet a delimiter is provided!'
    101104                raise ValueError, msg
    102             self.file_name = None
     105            file_name = None
    103106            self.check_data_points(data_points)
    104107            self.set_attributes(attributes)
     
    106109            self.set_default_attribute_name(default_attribute_name)
    107110
    108        
    109         else:
    110             if access(file_name, F_OK) == 0 :
    111                 msg = 'File %s does not exist or is not accessible' %file_name
    112                 raise IOError, msg
    113             else:
    114                 data = {}   
     111        else:
     112###                data = {}   
    115113                # watch for case where file name and points, attributes etc are provided!!
    116114                # if file name then all provided info will be removed!
    117                 self.file_name = file_name
    118                 self.delimiter = delimiter
    119                 self.import_points_file(file_name, delimiter)
     115                self.import_points_file(file_name, delimiter, verbose)
    120116               
    121117                self.check_data_points(self.data_points)
     
    183179        self.default_attribute_name = default_attribute_name
    184180
    185     def set_file_name(self, file_name = None):
    186         if file_name is None:
    187             self.file_name = None
    188         else:
    189             if access(file_name, F_OK) == 0 :
    190                 msg = 'Geospatial_data object needs have either data_points \n'
    191                 msg += ' or a file with data points'
    192                 raise msg
    193             else:   
    194                 self.file_name = file_name
    195                 print 'file name from set', self.file_name
    196    
    197181    def set_verbose(self, verbose = False):
    198182        if verbose is not False:
     
    319303    ###
    320304
    321     def import_points_file(self, ofile, delimiter = None, verbose = False):
     305    def import_points_file(self, file_name, delimiter = None, verbose = False):
    322306        """ load an .xya or .pts file
    323307        Note: will throw an IOError if it can't load the file.
     
    325309        """
    326310       
     311        if access(file_name, F_OK) == 0 :
     312            msg = 'File %s does not exist or is not accessible' %file_name
     313            raise IOError, msg
     314       
    327315        attributes = {}
    328         if ofile[-4:]== ".xya":
     316        if file_name[-4:]== ".xya":
    329317            try:
    330318                if delimiter == None:
    331319                    try:
    332                         fd = open(ofile)
     320                        fd = open(file_name)
    333321                        data_points, attributes, geo_reference = _read_xya_file(fd, ',')
    334322                    except SyntaxError:
    335323                        fd.close()
    336                         fd = open(ofile)
     324                        fd = open(file_name)
    337325                        data_points, attributes, geo_reference = _read_xya_file(fd, ' ')
    338326                else:
    339                     fd = open(ofile)
     327                    fd = open(file_name)
    340328                    data_points, attributes, geo_reference = _read_xya_file(fd, delimiter)
    341329                fd.close()
    342330            except (IndexError,ValueError,SyntaxError):
    343331                fd.close()   
    344                 msg = 'Could not open file %s ' %ofile
     332                msg = 'Could not open file %s ' %file_name
    345333                raise IOError, msg
    346334            except IOError, e:
    347335                # Catch this to add an error message
    348                 msg = 'Could not open file or incorrect file format %s:%s' %(ofile, e)
     336                msg = 'Could not open file or incorrect file format %s:%s' %(file_name, e)
    349337                raise IOError, msg
    350338               
    351         elif ofile[-4:]== ".pts":
     339        elif file_name[-4:]== ".pts":
    352340            try:
    353                 data_points, attributes, geo_reference = _read_pts_file(ofile, verbose)
     341                data_points, attributes, geo_reference = _read_pts_file(file_name, verbose)
    354342            except IOError, e:   
    355                 msg = 'Could not open file %s ' %ofile
     343                msg = 'Could not open file %s ' %file_name
    356344                raise IOError, msg       
    357345        else:     
    358             msg = 'Extension %s is unknown' %ofile[-4:]
     346            msg = 'Extension %s is unknown' %file_name[-4:]
    359347            raise IOError, msg
    360348       
     
    368356#        return all_data
    369357   
    370     def export_points_file(self, ofile, absolute=False):
    371        
    372         """
    373         write a points file, ofile, as a text (.xya) or binary (.pts) file
    374         ofile is the file name, including the extension
     358    def export_points_file(self, file_name, absolute=True):
     359       
     360        """
     361        write a points file, file_name, as a text (.xya) or binary (.pts) file
     362        file_name is the file name, including the extension
    375363        The point_dict is defined at the top of this file.
    376364       
     
    382370        """
    383371   
    384         if (ofile[-4:] == ".xya"):
     372        if (file_name[-4:] == ".xya"):
    385373            if absolute is True:         
    386                 _write_xya_file(ofile, self.get_data_points(absolute),
     374                _write_xya_file(file_name, self.get_data_points(absolute=True),
    387375                                   self.get_all_attributes())
    388376            else:
    389                 _write_xya_file(ofile, self.get_data_points(absolute),
     377                _write_xya_file(file_name, self.get_data_points(absolute=False),
    390378                                   self.get_all_attributes(),
    391379                                   self.get_geo_reference())
    392380                                   
    393         elif (ofile[-4:] == ".pts"):
     381        elif (file_name[-4:] == ".pts"):
    394382            if absolute is True:
    395                 _write_pts_file(ofile, self.get_data_points(absolute),
     383                _write_pts_file(file_name, self.get_data_points(absolute),
    396384                                   self.get_all_attributes())
    397385            else:
    398                 _write_pts_file(ofile, self.get_data_points(absolute),
     386                _write_pts_file(file_name, self.get_data_points(absolute),
    399387                                   self.get_all_attributes(),
    400388                                   self.get_geo_reference())
    401389        else:
    402             msg = 'Unknown file type %s ' %ofile
     390            msg = 'Unknown file type %s ' %file_name
    403391            raise IOError, msg
    404392   
     
    462450
    463451def _read_xya_file( fd, delimiter):
    464 #    print 'hello from read xya data'
    465452    points = []
    466453    pointattributes = []
    467454    title = fd.readline()
    468455    att_names = clean_line(title,delimiter)
    469    
    470456    att_dict = {}
    471457    line = fd.readline()
     
    480466                numbers.pop(0)
    481467                numbers.pop(0)
    482                 #attributes = []
    483                 #print "att_names",att_names
    484                 #print "numbers",numbers
    485468                if len(att_names) != len(numbers):
    486469                    fd.close()
     
    493476                        #attributes.append(float(num))
    494477                        att_dict.setdefault(att_names[i],[]).append(float(num))
    495                        
    496478            except ValueError:
    497479                raise SyntaxError
    498480        line = fd.readline()
    499481        numbers = clean_line(line,delimiter)
    500    
    501482    if line == '':
    502483            # end of file
    503484        geo_reference = None
     485       
    504486    else:
    505487        geo_reference = Geo_reference(ASCIIFile=fd,read_title=line)
    506    
    507 #    xya_dict = {}
    508 #    xya_dict['pointlist'] = array(points).astype(Float)
     488       
     489   
    509490    pointlist = array(points).astype(Float)
    510    
    511491    for key in att_dict.keys():
    512492        att_dict[key] = array(att_dict[key]).astype(Float)
    513 #    xya_dict['attributelist'] = att_dict
    514    
    515 #    xya_dict['geo_reference'] = geo_reference
    516     #print "xya_dict",xya_dict
     493   
    517494    return pointlist, att_dict, geo_reference
    518 
    519 
    520 def _old_read_pts_file(file_name, verbose = False):
    521     """Read .pts NetCDF file
    522    
    523     Return a dic of array of points, and dic of array of attribute
    524     eg
    525     dic['points'] = [[1.0,2.0],[3.0,5.0]]
    526     dic['attributelist']['elevation'] = [[7.0,5.0]
    527     """   
    528     #FIXME: (DSG) This format has issues.
    529     # There can't be an attribute called points
    530     # consider format change
    531 
    532 #    print 'hi for read points file'
    533     from Scientific.IO.NetCDF import NetCDFFile
    534    
    535     if verbose: print 'Reading ', file_name
    536    
    537        
    538     # see if the file is there.  Throw a QUIET IO error if it isn't
    539     fd = open(file_name,'r')
    540     fd.close()
    541    
    542     #throws prints to screen if file not present
    543     fid = NetCDFFile(file_name, 'r')
    544    
    545     point_atts = {} 
    546         # Get the variables
    547     point_atts['pointlist'] = array(fid.variables['points'])
    548     keys = fid.variables.keys()
    549     if verbose: print 'Got %d variables: %s' %(len(keys), keys)
    550     try:
    551         keys.remove('points')
    552     except IOError, e:       
    553         fid.close()   
    554         msg = 'Expected keyword "points" but could not find it'
    555         raise IOError, msg
    556    
    557     attributes = {}
    558     for key in keys:
    559         if verbose: print "reading attribute '%s'" %key
    560            
    561         attributes[key] = array(fid.variables[key])
    562    
    563     point_atts['attributelist'] = attributes
    564    
    565     try:
    566         geo_reference = Geo_reference(NetCDFObject=fid)
    567         point_atts['geo_reference'] = geo_reference
    568     except AttributeError, e:
    569         #geo_ref not compulsory
    570         point_atts['geo_reference'] = None
    571    
    572     fid.close()
    573     return point_atts
    574 
    575 
    576 def _old_read_xya_file( fd, delimiter):
    577     points = []
    578     pointattributes = []
    579     title = fd.readline()
    580     att_names = clean_line(title,delimiter)
    581    
    582     att_dict = {}
    583     line = fd.readline()
    584     numbers = clean_line(line,delimiter)
    585     #print 'read xya numbers', numbers
    586     while len(numbers) > 1:
    587         if numbers != []:
    588             try:
    589                 x = float(numbers[0])
    590                 y = float(numbers[1])
    591                 points.append([x,y])
    592                 numbers.pop(0)
    593                 numbers.pop(0)
    594                 #attributes = []
    595                 #print "att_names",att_names
    596                 #print "numbers",numbers
    597                 if len(att_names) != len(numbers):
    598                     fd.close()
    599                     # It might not be a problem with the title
    600                     #raise TitleAmountError
    601                     raise IOError
    602                 for i,num in enumerate(numbers):
    603                     num.strip()
    604                     if num != '\n' and num != '':
    605                         #attributes.append(float(num))
    606                         att_dict.setdefault(att_names[i],[]).append(float(num))
    607                        
    608             except ValueError:
    609                 raise SyntaxError
    610         line = fd.readline()
    611         numbers = clean_line(line,delimiter)
    612    
    613     if line == '':
    614             # end of file
    615         geo_reference = None
    616     else:
    617         geo_reference = Geo_reference(ASCIIFile=fd,read_title=line)
    618    
    619     xya_dict = {}
    620     xya_dict['pointlist'] = array(points).astype(Float)
    621    
    622     for key in att_dict.keys():
    623         att_dict[key] = array(att_dict[key]).astype(Float)
    624     xya_dict['attributelist'] = att_dict
    625    
    626     xya_dict['geo_reference'] = geo_reference
    627     #print "xya_dict",xya_dict
    628     return xya_dict
    629 
    630495
    631496def _write_pts_file(file_name, write_data_points,
     
    686551                                    delimiter = ','):
    687552    """
    688     export a file, ofile, with the xya format
     553    export a file, file_name, with the xya format
    689554   
    690555    """
     
    782647    return numbers
    783648           
    784 def add_points_files(add_file1, add_file2, results_file):
     649def xxx_add_points_files(add_file1, add_file2, results_file):
    785650    """ adds the points and attruibutes of 2 pts or xya files and
    786651    writes it to a pts file
  • inundation/geospatial_data/test_geospatial_data.py

    r2928 r2940  
    5252        attributes = [2, 4]
    5353        G = Geospatial_data(points, attributes,
    54                             geo_reference = Geo_reference(56, 100, 200))
     54                            geo_reference=Geo_reference(56, 100, 200))
    5555
    5656        assert G.geo_reference.zone == 56
     
    6464        attributes = [2, 4]
    6565        G = Geospatial_data(points, attributes,
    66                             geo_reference = Geo_reference(56, 100, 200))
    67 
    68 
    69         P = G.get_data_points(absolute = False)
     66                            geo_reference=Geo_reference(56, 100, 200))
     67
     68
     69        P = G.get_data_points(absolute=False)
    7070        assert allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
    7171
    72         P = G.get_data_points(absolute = True)
     72        P = G.get_data_points(absolute=True)
    7373        assert allclose(P, [[101.0, 202.1], [103.0, 205.3]])       
    7474
     
    8787        attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]}
    8888        G = Geospatial_data(points, attributes,
    89                             geo_reference = Geo_reference(56, 100, 200),
    90                             default_attribute_name = 'a1')
    91 
    92 
    93         P = G.get_data_points(absolute = False)
     89                            geo_reference=Geo_reference(56, 100, 200),
     90                            default_attribute_name='a1')
     91
     92
     93        P = G.get_data_points(absolute=False)
    9494        assert allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
    9595       
     
    122122        attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]}
    123123        G = Geospatial_data(points, attributes,
    124                             geo_reference = Geo_reference(56, 100, 200),
    125                             default_attribute_name = 'a1')
     124                            geo_reference=Geo_reference(56, 100, 200),
     125                            default_attribute_name='a1')
    126126
    127127
     
    165165        G = points_dictionary2geospatial_data(points_dict)
    166166
    167         P = G.get_data_points(absolute = False)
     167        P = G.get_data_points(absolute=False)
    168168        assert allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
    169169       
     
    342342        G1 = Geospatial_data(points, att_dict, geo_ref)
    343343
    344         G1.export_points_file(FN, absolute = True)
     344        G1.export_points_file(FN, absolute=True)
    345345
    346346        #Create object from file
    347347        G = Geospatial_data(file_name = FN)
    348348       
    349         assert allclose(G.get_data_points(absolute = True),
    350                         G1.get_data_points(absolute = True))
     349        assert allclose(G.get_data_points(absolute=True),
     350                        G1.get_data_points(absolute=True))
    351351        assert allclose(G.get_attributes('att1'), attributes)
    352352        assert allclose(G.get_attributes('att2'), array(attributes) + 1)
     
    3653651.0, 0.0, 10.4, 40.0\n")
    366366        file.close()
    367         results = Geospatial_data(fileName, delimiter = ',')
     367        results = Geospatial_data(fileName, delimiter=',')
    368368        os.remove(fileName)
    369369#        print 'data', results.get_data_points()
    370370        assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    371         assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4])
    372         assert allclose(results.get_attributes(attribute_name = 'speed'), [0.0, 10.0, 40.0])
     371        assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])
     372        assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0])
    373373
    374374    def test_loadxya2(self):
     
    386386        file.close()
    387387
    388         results = Geospatial_data(fileName, delimiter = ' ')
     388        results = Geospatial_data(fileName, delimiter=' ')
    389389
    390390        os.remove(fileName)
    391391
    392392        assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    393         assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4])
    394         assert allclose(results.get_attributes(attribute_name = 'speed'), [0.0, 10.0, 40.0])
     393        assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])
     394        assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0])
    395395     
    396396    def test_loadxya3(self):
     
    412412        file.close()
    413413
    414         results = Geospatial_data(fileName, delimiter = ' ')
    415 
    416         os.remove(fileName)
    417         assert allclose(results.get_data_points(absolute = False), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    418         assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4])
    419         assert allclose(results.get_attributes(attribute_name = 'speed'), [0.0, 10.0, 40.0])
     414        results = Geospatial_data(fileName, delimiter=' ')
     415
     416        os.remove(fileName)
     417        assert allclose(results.get_data_points(absolute=False), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     418        assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])
     419        assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0])
    420420
    421421    def test_read_write_points_file_bad2(self):
     
    424424        att_dict['elevation'] = array([10.0, 0.0, 10.4])
    425425        att_dict['brightness'] = array([10.0, 0.0, 10.4])
    426         geo_reference = Geo_reference(56,1.9,1.9)
     426        geo_reference=Geo_reference(56,1.9,1.9)
    427427       
    428428        G = Geospatial_data(pointlist, att_dict, geo_reference)
     
    451451        #print fileName
    452452        try:
    453             results = Geospatial_data(fileName, delimiter = ' ')
     453            results = Geospatial_data(fileName, delimiter=' ')
    454454        except IOError:
    455455            pass
     
    473473        #print fileName
    474474        try:
    475             results = Geospatial_data(fileName, delimiter = ' ')
     475            results = Geospatial_data(fileName, delimiter=' ')
    476476        except IOError:
    477477            pass
     
    495495        file.close()
    496496        try:
    497             results = Geospatial_data(fileName, delimiter = ' ')
     497            results = Geospatial_data(fileName, delimiter=' ')
    498498        except IOError:
    499499            pass
     
    505505    def test_loadxy_bad4(self):
    506506        """
    507         specifying wrong delimiter
     507         specifying wrong delimiter
    508508        """
    509509        import os
    510        
    511510        fileName = tempfile.mktemp(".xya")
    512511        file = open(fileName,"w")
     
    5155140.0 1.0 0.0 10.0\n\
    5165151.0 0.0 10.4 40.0\n\
    517 #geocrap")
     516#geo\n\
     51756\n\
     5181.1\n\
     5191.0\n")
     520
    518521        file.close()
    519522        try:
    520 #            dict = import_points_file(fileName,delimiter = ' ')
    521 #            results = Geospatial_data()
    522             results = Geospatial_data(fileName, delimiter = ',')
    523 #            results.import_points_file(fileName, delimiter = ' ')
     523            results = Geospatial_data(fileName, delimiter=',')
    524524        except IOError:
    525525            pass
     
    528528            raise msg
    529529
    530 #            self.failUnless(0 ==1,
    531 #                        'bad xya file did not raise error!')   
    532530        os.remove(fileName)
    533531
     
    548546        file.close()
    549547        try:
    550 #            dict = import_points_file(fileName,delimiter = ' ')
     548#            dict = import_points_file(fileName,delimiter=' ')
    551549#            results = Geospatial_data()
    552             results = Geospatial_data(fileName, delimiter = ' ')
    553 #            results.import_points_file(fileName, delimiter = ' ')
     550            results = Geospatial_data(fileName, delimiter=' ', verbose=True)
     551#            results.import_points_file(fileName, delimiter=' ')
    554552        except IOError:
    555553            pass
     
    567565        fileName = tempfile.mktemp(".xya")
    568566        try:
    569             results = Geospatial_data(fileName, delimiter = ' ')
     567            results = Geospatial_data(fileName, delimiter=' ')
    570568        except IOError:
    571569            pass
     
    590588        att_dict['brightness'] = array([10.0, 0.0, 10.4])
    591589#        dict['attributelist'] = att_dict
    592         geo_reference = Geo_reference(56,1.9,1.9)
     590        geo_reference=Geo_reference(56,1.9,1.9)
    593591       
    594592       
    595593        fileName = tempfile.mktemp(".xya")
    596594        G = Geospatial_data(pointlist, att_dict, geo_reference)
    597         G.export_points_file(fileName)
     595        G.export_points_file(fileName, False)
    598596
    599597#        dict2 = import_points_file(fileName)
     
    603601       
    604602        assert allclose(results.get_data_points(absolute=False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    605         assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4])
     603        assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])
    606604        answer = [10.0, 0.0, 10.4]
    607         assert allclose(results.get_attributes(attribute_name = 'brightness'), answer)
     605        assert allclose(results.get_attributes(attribute_name='brightness'), answer)
    608606        #print "dict2['geo_reference']",dict2['geo_reference']
    609607        self.failUnless(results.get_geo_reference() == geo_reference,
     
    637635        att_dict['elevation'] = array([10.0, 0.0, 10.4])
    638636        att_dict['brightness'] = array([10.0, 0.0, 10.4])
    639         geo_reference = Geo_reference(56,1.9,1.9)
     637        geo_reference=Geo_reference(56,1.9,1.9)
    640638       
    641639       
     
    650648        assert allclose(results.get_data_points(),
    651649                        [[2.9, 1.9],[1.9, 2.9],[2.9, 1.9]])
    652         assert allclose(results.get_attributes(attribute_name = 'elevation'),
     650        assert allclose(results.get_attributes(attribute_name='elevation'),
    653651                         [10.0, 0.0, 10.4])
    654652        answer = [10.0, 0.0, 10.4]
    655         assert allclose(results.get_attributes(attribute_name = 'brightness'), answer)
     653        assert allclose(results.get_attributes(attribute_name='brightness'), answer)
    656654        self.failUnless(results.get_geo_reference() == geo_reference,
    657655                         'test_writepts failed. Test geo_reference')                         
     
    676674       
    677675        assert allclose(results.get_data_points(),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    678         assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.1, 0.0, 10.4])
     676        assert allclose(results.get_attributes(attribute_name='elevation'), [10.1, 0.0, 10.4])
    679677        answer = [10.0, 1.0, 10.4]
    680         assert allclose(results.get_attributes(attribute_name = 'brightness'), answer)
     678        assert allclose(results.get_attributes(attribute_name='brightness'), answer)
    681679
    682680    def test_new_export_absolute_pts_file(self):
     
    698696       
    699697        assert allclose(results.get_data_points(), G.get_data_points(True))
    700         assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.1, 0.0, 10.4])
     698        assert allclose(results.get_attributes(attribute_name='elevation'), [10.1, 0.0, 10.4])
    701699        answer = [10.0, 1.0, 10.4]
    702         assert allclose(results.get_attributes(attribute_name = 'brightness'), answer)
     700        assert allclose(results.get_attributes(attribute_name='brightness'), answer)
    703701
    704702    def test_loadpts(self):
     
    736734        answer =  [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]
    737735        assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    738         assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4])
     736        assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])
    739737       
    740738    def test_writepts(self):
     
    743741        att_dict['elevation'] = array([10.0, 0.0, 10.4])
    744742        att_dict['brightness'] = array([10.0, 0.0, 10.4])
    745         geo_reference = Geo_reference(56,1.9,1.9)
     743        geo_reference=Geo_reference(56,1.9,1.9)
    746744       
    747745        fileName = tempfile.mktemp(".pts")
     
    749747        G = Geospatial_data(pointlist, att_dict, geo_reference)
    750748       
    751         G.export_points_file(fileName)
     749        G.export_points_file(fileName, False)
    752750       
    753751        results = Geospatial_data(file_name = fileName)
     
    837835        xll = 0.1
    838836        yll = 20
    839         geo_reference = Geo_reference(56, xll, yll)
     837        geo_reference=Geo_reference(56, xll, yll)
    840838        geo_reference.write_NetCDF(outfile)
    841839
     
    875873
    876874       
    877     def test_add_points_files(self):
    878         '''adds an xya and pts files and checks resulting file is correct
     875    def test_add_(self):
     876        '''adds an xya and pts files, reads the files and adds them
     877           checking results are correct
    879878        '''
    880879
     
    882881       
    883882        # create files
    884 #        dict1 = {}
    885883        att_dict1 = {}
    886884        pointlist1 = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    887885        att_dict1['elevation'] = array([-10.0, 0.0, 10.4])
    888886        att_dict1['brightness'] = array([10.0, 0.0, 10.4])
    889 #        dict1['attributelist'] = att_dict1
    890887        geo_reference1 = Geo_reference(56, 2.0, 1.0)
    891888       
    892 #        dict2 = {}
    893889        att_dict2 = {}
    894890        pointlist2 = array([[2.0, 1.0],[1.0, 2.0],[2.0, 1.0]])
    895891        att_dict2['elevation'] = array([1.0, 15.0, 1.4])
    896892        att_dict2['brightness'] = array([14.0, 1.0, -12.4])
    897 #        dict2['attributelist'] = att_dict2
    898893        geo_reference2 = Geo_reference(56, 1.0, 2.0) #FIXME (Ole): I changed this, why does it still pass
    899894        #OK - it fails now, after the fix revealed by test_create_from_pts_file_with_geo')
     
    918913       
    919914        #read results
    920         assert allclose(G.get_data_points(absolute = False),
    921                         [[2.0, 0.0],[1.0, 1.0],
    922                          [2.0, 0.0],[2.0, 2.0],
    923                          [1.0, 3.0],[2.0, 2.0]])
    924         assert allclose(G.get_attributes(attribute_name = 'elevation'),
     915#        print'res', G.get_data_points()
     916#        print'res1', G.get_data_points(False)
     917        assert allclose(G.get_data_points(),
     918                        [[ 3.0, 1.0], [ 2.0, 2.0],
     919                         [ 3.0, 1.0], [ 3.0, 3.0],
     920                         [ 2.0, 4.0], [ 3.0, 3.0]])
     921                         
     922        assert allclose(G.get_attributes(attribute_name='elevation'),
    925923                        [-10.0, 0.0, 10.4, 1.0, 15.0, 1.4])
    926924       
    927925        answer = [10.0, 0.0, 10.4, 14.0, 1.0, -12.4]
    928         assert allclose(G.get_attributes(attribute_name = 'brightness'), answer)
     926        assert allclose(G.get_attributes(attribute_name='brightness'), answer)
    929927       
    930928        self.failUnless(G.get_geo_reference() == geo_reference1,
     
    962960        #print "data_points",data_points     
    963961        new_points = ensure_absolute(data_points,
    964                                              geo_reference = mesh_origin)
     962                                             geo_reference=mesh_origin)
    965963        #print "new_points",new_points
    966964        #print "ab_points",ab_points
     
    972970        data_points = geo.change_points_geo_ref(ab_points)   
    973971        new_points = ensure_absolute(data_points,
    974                                              geo_reference = geo)
     972                                             geo_reference=geo)
    975973        #print "new_points",new_points
    976974        #print "ab_points",ab_points
     
    985983        #print "geo in points", points
    986984        G = Geospatial_data(points, attributes,
    987                             geo_reference = geo_reference)
     985                            geo_reference=geo_reference)
    988986         
    989987        new_points = ensure_absolute(G)
     
    10111009        results = Geospatial_data(fileName)
    10121010       
    1013         assert allclose(results.get_data_points(absolute = False), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    1014         assert allclose(results.get_attributes(attribute_name = 'elevation'), [10.0, 0.0, 10.4])
    1015         assert allclose(results.get_attributes(attribute_name = 'speed'), [0.0, 10.0, 40.0])
     1011        assert allclose(results.get_data_points(absolute=False), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1012        assert allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])
     1013        assert allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0])
    10161014
    10171015        os.remove(fileName)
     
    10201018       
    10211019        try:
    1022             G = Geospatial_data(delimiter = ',')
     1020            G = Geospatial_data(delimiter=',')
    10231021#            results = Geospatial_data(file_name = fileName)
    10241022#            dict = import_points_file(fileName)
     
    10421040            raise msg       
    10431041
     1042    def xxx_test_check_geo_reference(self):
     1043        """
     1044        checks geo reference details are OK. eg can be called '#geo reference'
     1045        if not throws a clear error message
     1046        """
     1047        import os
     1048        fileName = tempfile.mktemp(".xya")
     1049        file = open(fileName,"w")
     1050        file.write("  elevation  \n\
     10511.0 0.0 10.0\n\
     10520.0 1.0 0.0\n\
     10531.0 0.0 10.4\n\
     1054#geo reference\n\
     105556\n\
     10561.1\n\
     10571.0\n")
     1058
     1059        file.close()
     1060        results = Geospatial_data(fileName)
     1061        assert allclose(results.get_geo_reference().get_xllcorner(), 1.1)
     1062        assert allclose(results.get_geo_reference().get_yllcorner(), 1.0)
     1063
     1064    def xxx_test_check_geo_reference1(self):
     1065        """
     1066        checks geo reference details are OK. eg can be called '#geo reference'
     1067        if not throws a clear error message
     1068        """
     1069        import os
     1070        fileName = tempfile.mktemp(".xya")
     1071        file = open(fileName,"w")
     1072        file.write("  elevation  \n\
     10731.0 0.0 10.0\n\
     10740.0 1.0 0.0\n\
     10751.0 0.0 10.4\n\
     1076#geo reference\n\
     107756\n\
     10781.1\n"
     1079)
     1080        file.close()
     1081
     1082#        try:
     1083        results = Geospatial_data(fileName)
     1084#        except ValueError or SyntaxError:
     1085#            pass
     1086#        else:
     1087#            msg = 'Geo reference data format is incorrect'
     1088#            raise msg       
     1089
     1090
     1091
     1092        assert allclose(results.get_geo_reference().get_xllcorner(), 1.1)
     1093        assert allclose(results.get_geo_reference().get_yllcorner(), 1.0)
     1094       
    10441095
    10451096
Note: See TracChangeset for help on using the changeset viewer.