Changeset 3735


Ignore:
Timestamp:
Oct 10, 2006, 4:54:34 PM (18 years ago)
Author:
ole
Message:

This fixes ticket:96 plus a few cosmetic things

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

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/geospatial_data/geospatial_data.py

    r3723 r3735  
    511511        if (file_name[-4:] == ".xya"):
    512512            if absolute is True:         
    513                 _write_xya_file(file_name, self.get_data_points(absolute=True),
    514                                    self.get_all_attributes())
     513                _write_xya_file(file_name,
     514                                self.get_data_points(absolute=True),
     515                                self.get_all_attributes())
    515516            else:
    516                 _write_xya_file(file_name, self.get_data_points(absolute=False),
    517                                    self.get_all_attributes(),
    518                                    self.get_geo_reference())
     517                _write_xya_file(file_name,
     518                                self.get_data_points(absolute=False),
     519                                self.get_all_attributes(),
     520                                self.get_geo_reference())
    519521                                   
    520522        elif (file_name[-4:] == ".pts"):
    521523            if absolute is True:
    522                 _write_pts_file(file_name, self.get_data_points(absolute),
    523                                    self.get_all_attributes())
     524                _write_pts_file(file_name,
     525                                self.get_data_points(absolute),
     526                                self.get_all_attributes())
    524527            else:
    525                 _write_pts_file(file_name, self.get_data_points(absolute),
    526                                    self.get_all_attributes(),
    527                                    self.get_geo_reference())
     528                _write_pts_file(file_name,
     529                                self.get_data_points(absolute),
     530                                self.get_all_attributes(),
     531                                self.get_geo_reference())
    528532        else:
    529533            msg = 'Unknown file type %s ' %file_name
     
    632636    return pointlist, att_dict, geo_reference
    633637
    634 def _write_pts_file(file_name, write_data_points,
    635                                    write_attributes,
    636                                    write_geo_reference = None):
     638def _write_pts_file(file_name,
     639                    write_data_points,
     640                    write_attributes=None,
     641                    write_geo_reference=None):
    637642    """
    638643    Write .pts NetCDF file   
     
    659664    outfile.institution = 'Geoscience Australia'
    660665    outfile.description = 'NetCDF format for compact and portable storage ' +\
    661                       'of spatial point data'
     666                          'of spatial point data'
    662667   
    663668    # dimension definitions
     
    673678    outfile.variables['points'][:] = write_data_points #.astype(Float32)
    674679
    675     for key in write_attributes.keys():
    676         outfile.createVariable(key, Float, ('number_of_points',))
    677         outfile.variables[key][:] = write_attributes[key] #.astype(Float32)
     680    if write_attributes is not None:
     681        for key in write_attributes.keys():
     682            outfile.createVariable(key, Float, ('number_of_points',))
     683            outfile.variables[key][:] = write_attributes[key] #.astype(Float32)
    678684       
    679685    if write_geo_reference is not None:
     
    684690
    685691
    686 def _write_xya_file( file_name, write_data_points,
    687                                     write_attributes,
    688                                     write_geo_reference = None,
    689                                     delimiter = ','):
     692def _write_xya_file(file_name,
     693                    write_data_points,
     694                    write_attributes=None,
     695                    write_geo_reference=None,
     696                    delimiter = ','):
    690697    """
    691698    export a file, file_name, with the xya format
     
    697704    fd = open(file_name,'w')
    698705    titlelist = ""
    699     for title in pointattributes.keys():
    700         titlelist = titlelist + title + delimiter
    701     titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter
     706    if pointattributes is not None:   
     707        for title in pointattributes.keys():
     708            titlelist = titlelist + title + delimiter
     709        titlelist = titlelist[0:-len(delimiter)] # remove the last delimiter
    702710    fd.write(titlelist+"\n")
     711   
    703712    #<vertex #> <x> <y> [attributes]
    704     for i,vert in enumerate( points):
    705        
    706         attlist = ","
    707         for att in pointattributes.keys():
    708             attlist = attlist + str(pointattributes[att][i])+ delimiter
    709         attlist = attlist[0:-len(delimiter)] # remove the last delimiter
    710         attlist.strip()
    711         fd.write( str(vert[0]) + delimiter
    712                   + str(vert[1])
    713                   + attlist + "\n")
     713    for i, vert in enumerate( points):
     714
     715
     716        if pointattributes is not None:           
     717            attlist = ","
     718            for att in pointattributes.keys():
     719                attlist = attlist + str(pointattributes[att][i])+ delimiter
     720            attlist = attlist[0:-len(delimiter)] # remove the last delimiter
     721            attlist.strip()
     722        else:
     723            attlist = ''
     724
     725        fd.write(str(vert[0]) + delimiter +
     726                 str(vert[1]) + attlist + "\n")
    714727
    715728    if  write_geo_reference is not None:
  • anuga_core/source/anuga/geospatial_data/test_geospatial_data.py

    r3733 r3735  
    476476
    477477
    478     def no_test_clip1_inside_outside(self):
     478    def test_clip1_inside_outside(self):
    479479        """test_clip1_inside_outside(self):
    480480       
     
    509509        FN = 'test_combine.pts'
    510510        G.export_points_file(FN)
     511
     512
     513        # Read it back in
     514        G3 = Geospatial_data(FN)
     515
     516
     517        # Check result
     518        assert allclose(G3.get_data_points(), new_points)       
     519       
     520       
    511521        os.remove(FN)
    512522
     
    972982       
    973983    def test_writepts(self):
     984        """test_writepts: Test that storage of x,y,attributes works
     985        """
    974986        att_dict = {}
    975987        pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     
    977989        att_dict['brightness'] = array([10.0, 0.0, 10.4])
    978990        geo_reference=Geo_reference(56,1.9,1.9)
    979        
     991
     992        # Test pts format
    980993        fileName = tempfile.mktemp(".pts")
    981        
    982994        G = Geospatial_data(pointlist, att_dict, geo_reference)
    983        
    984995        G.export_points_file(fileName, False)
    985        
    986         results = Geospatial_data(file_name = fileName)
    987 
     996        results = Geospatial_data(file_name=fileName)
    988997        os.remove(fileName)
    989998
     
    9921001        answer = [10.0, 0.0, 10.4]
    9931002        assert allclose(results.get_attributes('brightness'), answer)
    994 
    995        
    9961003        self.failUnless(geo_reference == geo_reference,
    9971004                         'test_writepts failed. Test geo_reference')
     1005
     1006        # Test xya format
     1007        fileName = tempfile.mktemp(".xya")
     1008        G = Geospatial_data(pointlist, att_dict, geo_reference)
     1009        G.export_points_file(fileName, False)
     1010        results = Geospatial_data(file_name=fileName)
     1011        os.remove(fileName)
     1012        assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1013        assert allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4])
     1014        answer = [10.0, 0.0, 10.4]
     1015        assert allclose(results.get_attributes('brightness'), answer)
     1016        self.failUnless(geo_reference == geo_reference,
     1017                         'test_writepts failed. Test geo_reference')
     1018
     1019    def test_writepts_no_attributes(self):
     1020        """test_writepts_no_attributes: Test that storage of x,y alone works
     1021        """
     1022        att_dict = {}
     1023        pointlist = array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1024        geo_reference=Geo_reference(56,1.9,1.9)
     1025
     1026        # Test pts format
     1027        fileName = tempfile.mktemp(".pts")
     1028        G = Geospatial_data(pointlist, None, geo_reference)
     1029        G.export_points_file(fileName, False)
     1030        results = Geospatial_data(file_name=fileName)
     1031        os.remove(fileName)
     1032
     1033        assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1034        self.failUnless(geo_reference == geo_reference,
     1035                         'test_writepts failed. Test geo_reference')
     1036
     1037        # Test xya format
     1038        fileName = tempfile.mktemp(".xya")
     1039        G = Geospatial_data(pointlist, None, geo_reference)
     1040        G.export_points_file(fileName, False)
     1041        results = Geospatial_data(file_name=fileName)
     1042        os.remove(fileName)
     1043        assert allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1044        self.failUnless(geo_reference == geo_reference,
     1045                         'test_writepts failed. Test geo_reference')
     1046
     1047       
    9981048       
    9991049 ########################## BAD .PTS ##########################         
Note: See TracChangeset for help on using the changeset viewer.