Changeset 4175


Ignore:
Timestamp:
Jan 15, 2007, 12:10:20 PM (18 years ago)
Author:
duncan
Message:

added blocking on netcdf files & a fix in using verbose

Location:
anuga_core/source/anuga
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/fit_interpolate/fit.py

    r4174 r4175  
    320320         
    321321        """
    322 
    323322        # use blocking to load in the point info
    324323        if type(point_coordinates_or_filename) == types.StringType:
     
    328327            for i,geo_block in  enumerate(Geospatial_data(filename,
    329328                                              max_read_lines=max_read_lines,
    330                                               load_file_now=False)):
     329                                              load_file_now=False,
     330                                              verbose=verbose)):
    331331                if verbose is True and 0 == i%200: # round every 5 minutes
    332332                    print 'Block %i' %i
  • anuga_core/source/anuga/fit_interpolate/test_fit.py

    r4174 r4175  
    241241        os.remove(fileName)
    242242
    243     def test_fit_to_mesh(self):
    244 
    245         a = [-1.0, 0.0]
    246         b = [3.0, 4.0]
    247         c = [4.0,1.0]
    248         d = [-3.0, 2.0] #3
    249         e = [-1.0,-2.0]
    250         f = [1.0, -2.0] #5
    251 
    252         vertices = [a, b, c, d,e,f]
    253         triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
    254 
    255 
    256         fileName = tempfile.mktemp(".ddd")
    257         file = open(fileName,"w")
    258         file.write(" x,y, elevation \n\
    259 -2.0, 2.0, 0.\n\
    260 -1.0, 1.0, 0.\n\
    261 0.0, 2.0 , 2.\n\
    262 1.0, 1.0 , 2.\n\
    263  2.0,  1.0 ,3. \n\
    264  0.0,  0.0 , 0.\n\
    265  1.0,  0.0 , 1.\n\
    266  0.0,  -1.0, -1.\n\
    267  -0.2, -0.5, -0.7\n\
    268  -0.9, -1.5, -2.4\n\
    269  0.5,  -1.9, -1.4\n\
    270  3.0,  1.0 , 4.\n")
    271         file.close()
    272        
    273         f = fit_to_mesh(vertices, triangles,fileName,
    274                                 alpha=0.0, max_read_lines=2)
    275                         #use_cache=True, verbose=True)
    276         answer = linear_function(vertices)
    277         #print "f\n",f
    278         #print "answer\n",answer
    279         assert allclose(f, answer)
    280        
    281         os.remove(fileName)
    282243   
    283244    def test_fit_to_mesh_pts(self):
     
    314275        geo.export_points_file(fileName_pts)
    315276        f = fit_to_mesh(vertices, triangles,fileName_pts,
    316                                 alpha=0.0, max_read_lines=2)
     277                                alpha=0.0, max_read_lines=2) #,
    317278                        #use_cache=True, verbose=True)
    318279        answer = linear_function(vertices)
  • anuga_core/source/anuga/geospatial_data/geospatial_data.py

    r4174 r4175  
    1313#from MA import tolist
    1414
     15from Scientific.IO.NetCDF import NetCDFFile   
     16   
    1517from anuga.utilities.numerical_tools import ensure_numeric
    1618from anuga.coordinate_transforms.geo_reference import Geo_reference, TitleError
     
    586588            msg += 'Text file format is moving to comma seperated .txt files.'
    587589            warn(msg, DeprecationWarning)
    588             error(msg, DeprecationWarning)
    589590
    590591        if (file_name[-4:] == ".xya"):
     
    699700
    700701    def __iter__(self):
    701         # read in the header and save the file pointer position
    702 
     702        """
     703        read in the header and save the file pointer position
     704        """
     705
     706        from Scientific.IO.NetCDF import NetCDFFile
     707       
    703708        #FIXME - what to do if the file isn't there
    704709
    705         #FIXME - give warning if the file format is .xya
    706         if self.file_name[-4:]== ".xya" or self.file_name[-4:]== ".pts":
     710        if self.file_name[-4:]== ".xya":
    707711            #let's just read it all
    708712            pass
     713        elif self.file_name[-4:]== ".pts":
     714           
     715            # see if the file is there.  Throw a QUIET IO error if it isn't
     716            fd = open(self.file_name,'r')
     717            fd.close()
     718   
     719            #throws prints to screen if file not present
     720            self.fid = NetCDFFile(self.file_name, 'r')
     721           
     722            self.blocking_georef, self.blocking_keys, self.last_row = \
     723                     _read_pts_file_header(self.fid, self.verbose)
     724            self.start_row=0
    709725        else:
    710726            file_pointer = open(self.file_name)
     
    712728                         _read_csv_file_header(file_pointer)
    713729
    714             if self.max_read_lines is None:
    715                 self.max_read_lines = MAX_READ_LINES
     730        if self.max_read_lines is None:
     731            self.max_read_lines = MAX_READ_LINES
    716732        return self
    717733   
    718734    def next(self):
    719735        # read a block, instanciate a new geospatial and return it
    720         if self.file_name[-4:]== ".xya" or self.file_name[-4:]== ".pts":
     736        if self.file_name[-4:]== ".xya" :
    721737            if not hasattr(self,'finished_reading') or \
    722738                   self.finished_reading is False:
     
    728744                self.finished_reading = False
    729745               
     746        elif self.file_name[-4:]== ".pts":
     747            if self.start_row == self.last_row:
     748                # read the end of the file last iteration
     749                # FIXME clean up, remove blocking atts eg
     750                #self.max_read_lines
     751                #self.blocking_georef,self.last_row
     752                #self.start_row
     753                ### self.blocking_keys
     754                self.fid.close()
     755                raise StopIteration
     756            fin_row = self.start_row + self.max_read_lines
     757            if fin_row > self.last_row:
     758                fin_row = self.last_row
     759
     760            #call stuff
     761            pointlist, att_dict, = \
     762                   _read_pts_file_blocking( self.fid,
     763                                            self.start_row,
     764                                            fin_row,
     765                                            self.blocking_keys
     766                                            )
     767            geo = Geospatial_data(pointlist, att_dict, self.blocking_georef)
     768            self.start_row = fin_row
     769           
    730770        else:
    731771            try:
    732772                pointlist, att_dict, self.file_pointer = \
    733773                   _read_csv_file_blocking( self.file_pointer,
    734                                          self.header[:],
    735                                          max_read_lines=self.max_read_lines)
     774                                            self.header[:],
     775                                            max_read_lines=self.max_read_lines,
     776                                            verbose=self.verbose)
    736777                geo = Geospatial_data(pointlist, att_dict)
    737778            except StopIteration:
     
    761802    fid = NetCDFFile(file_name, 'r')
    762803   
    763 #    point_atts = {} 
    764         # Get the variables
    765 #    point_atts['pointlist'] = array(fid.variables['points'])
    766804    pointlist = array(fid.variables['points'])
    767805    keys = fid.variables.keys()
     
    780818        attributes[key] = array(fid.variables[key])
    781819   
    782 #    point_atts['attributelist'] = attributes
    783820   
    784821    try:
    785822        geo_reference = Geo_reference(NetCDFObject=fid)
    786 #        point_atts['geo_reference'] = geo_reference
    787823    except AttributeError, e:
    788         #geo_ref not compulsory
    789 #        point_atts['geo_reference'] = None
    790824        geo_reference = None
    791825   
     
    895929    return pointlist, att_dict,file_pointer
    896930
     931def _read_pts_file_header(fid, verbose=False):
     932
     933    """
     934    Read the geo_reference of a .pts file
     935    """
     936   
     937    keys = fid.variables.keys()
     938    try:
     939        keys.remove('points')
     940    except IOError, e:       
     941        fid.close()   
     942        msg = 'Expected keyword "points" but could not find it'
     943        raise IOError, msg
     944    if verbose: print 'Got %d variables: %s' %(len(keys), keys)
     945   
     946    try:
     947        geo_reference = Geo_reference(NetCDFObject=fid)
     948    except AttributeError, e:
     949        geo_reference = None
     950
     951    return geo_reference, keys, fid.dimensions['number_of_points']
     952
     953def _read_pts_file_blocking(fid, start_row, fin_row, keys):
     954                            #verbose=False):
     955   
     956
     957    """
     958    Read the body of a .csv file.
     959    header: The list header of the csv file, with the x and y labels.
     960    """
     961   
     962    pointlist = array(fid.variables['points'][start_row:fin_row])
     963   
     964    attributes = {}
     965    for key in keys:
     966        attributes[key] = array(fid.variables[key][start_row:fin_row])
     967
     968    return pointlist, attributes
     969   
     970   
    897971def _read_xya_file(fd, delimiter):
    898972    points = []
  • anuga_core/source/anuga/geospatial_data/test_geospatial_data.py

    r4165 r4175  
    1616warnings.filterwarnings(action = 'ignore',
    1717                        message='.xya format is deprecated.  Please use .txt.',
     18                        category=DeprecationWarning)
     19
     20warnings.filterwarnings(action = 'ignore',
     21                        message='The text file values must be ab',
    1822                        category=DeprecationWarning)
    1923
     
    974978                        [10.4])
    975979           
    976         os.remove(fileName)            
     980        os.remove(fileName)         
    977981       
    978982    def test_load_csv_bad(self):
     
    10041008            raise msg
    10051009        os.remove(fileName)
    1006        
    1007     def depreciated_test_export_xya_file(self):
     1010
     1011       
     1012    def test_load_pts_blocking(self):
     1013        """ test_load_csv(self):
     1014        space delimited
     1015        """
     1016        import os
     1017       
     1018        fileName = tempfile.mktemp(".txt")
     1019        file = open(fileName,"w")
     1020        file.write(" x,y, elevation ,  speed \n\
     10211.0, 0.0, 10.0, 0.0\n\
     10220.0, 1.0, 0.0, 10.0\n\
     10231.0, 0.0 ,10.4, 40.0\n")
     1024        file.close()
     1025
     1026        pts_file = tempfile.mktemp(".pts")
     1027       
     1028        convert = Geospatial_data(fileName)
     1029        convert.export_points_file(pts_file)
     1030        results = Geospatial_data(pts_file, max_read_lines=2)
     1031
     1032        assert allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],
     1033                                                    [1.0, 0.0]])
     1034        assert allclose(results.get_attributes(attribute_name='elevation'),
     1035                        [10.0, 0.0, 10.4])
     1036        assert allclose(results.get_attributes(attribute_name='speed'),
     1037                        [0.0, 10.0, 40.0])
     1038
     1039        # Blocking
     1040        geo_list = []
     1041        for i in results:
     1042            geo_list.append(i)
     1043        assert allclose(geo_list[0].get_data_points(),
     1044                        [[1.0, 0.0],[0.0, 1.0]])
     1045        assert allclose(geo_list[0].get_attributes(attribute_name='elevation'),
     1046                        [10.0, 0.0])
     1047        assert allclose(geo_list[1].get_data_points(),
     1048                        [[1.0, 0.0]])       
     1049        assert allclose(geo_list[1].get_attributes(attribute_name='elevation'),
     1050                        [10.4])
     1051           
     1052        os.remove(fileName) 
     1053        os.remove(pts_file)               
     1054
     1055       
     1056    def test_export_xya_file(self):
     1057        # depreciated since it's testing using geo refs in a text file
    10081058#        dict = {}
    10091059        att_dict = {}
Note: See TracChangeset for help on using the changeset viewer.