Changeset 2643


Ignore:
Timestamp:
Mar 31, 2006, 4:18:50 PM (19 years ago)
Author:
nick
Message:

add comments to geospatial_data.py

Location:
inundation/geospatial_data
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/geospatial_data/geospatial_data.py

    r2624 r2643  
    2323                 absolute = True):
    2424
    25         """Create instance from data points and associated attributes
     25       
     26        """
     27        Create instance from data points and associated attributes
    2628
    2729
     
    4547        If None, the default is the 'first'
    4648       
    47         file_name: Name of input file.....
    48        
    49         absolute: is the data point absolute or relative to a xll and yll
    50         in a zone
    51        
    52         """
     49        file_name: Name of input netCDF file or xya file. netCDF file must
     50        have dimensions "points" etc.
     51        xya file is a CVS file with lats(x), longs(y) and elevation(a).
     52        first line must be attribute name eg elevation 
     53       
     54        absolute: Return data points that are absolute or relative to
     55        to a xll and yll in a zone
     56       
     57        """
     58       
    5359
    5460        if file_name is None:
     
    6066            self.set_default_attribute_name(default_attribute_name)
    6167            self.set_absolute(absolute)
    62            
    6368       
    6469        else:
     
    7176                # if file name then all provided info will be removed!
    7277                self.file_name = file_name
    73                 data = import_points_file(self.file_name)
     78                self.new_import_points_file(file_name)
    7479               
    75                 # print 'data: point in init', data['pointlist']
    76                 # print 'attrib: point in init', data['attributelist']
    77                 # print 'geo_ref: point in init', data['geo_reference']
    78                
    79                 data_points = data['pointlist']
    80                 attributes = data['attributelist']
    81                 geo_reference = data['geo_reference']
    82                
    83                 self.check_data_points(data_points)
    84                 self.set_attributes(attributes)
    85                 self.set_geo_reference(geo_reference)
     80#                print'data: point in init',self.data_points
     81#                print'attrib: point in init',self.attributes
     82#                print'geo_ref: point in init',self.geo_reference
     83                self.check_data_points(self.data_points)
     84                self.set_attributes(self.attributes)
     85                self.set_geo_reference(self.geo_reference)
    8686                self.set_default_attribute_name(default_attribute_name)
    8787
     
    366366        """
    367367       
    368         all_data = {}
     368        attributes = {}
    369369        if ofile[-4:]== ".xya":
    370370            try:
     
    372372                    try:
    373373                        fd = open(ofile)
    374                         all_data = _read_xya_file(fd, ',')
     374#                        all_data = _read_xya_file(fd, ',')
     375                        data_points, attributes, geo_reference = _new_read_xya_file(fd, ',')
    375376                    except SyntaxError:
    376377                        fd.close()
    377378                        fd = open(ofile)
    378                         all_data = _read_xya_file(fd, ' ')
     379#                        all_data = _read_xya_file(fd, ' ')
     380                        data_points, attributes, geo_reference = _new_read_xya_file(fd, ' ')
    379381                else:
    380382                    fd = open(ofile)
    381                     all_data = _read_xya_file(fd, delimiter)
     383#                    all_data = _read_xya_file(fd, delimiter)
     384                    data_points, attributes, geo_reference = _new_read_xya_file(fd, delimiter)
    382385                fd.close()
    383386            except (IndexError,ValueError,SyntaxError):
     
    393396            try:
    394397    #            print 'hi from import_points_file'
    395                 all_data = _read_pts_file(ofile, verbose)
     398#                all_data = _read_pts_file(ofile, verbose)
     399                data_points, attributes, geo_reference = _new_read_pts_file(ofile, verbose)
    396400    #            print 'hi1 from import_points_file', all_data
    397401            except IOError, e:   
     
    401405            msg = 'Extension %s is unknown' %ofile[-4:]
    402406            raise IOError, msg
    403    
    404         return all_data
     407       
     408#        print'in import data_points', data_points
     409#        print'in import attributes', attributes
     410#        print'in import data_points', geo_reference
     411        self.data_points = data_points
     412        self.attributes = attributes
     413        self.geo_reference = geo_reference
     414   
     415#        return all_data
    405416   
    406417    def new_export_points_file(self, ofile):
     
    491502        raise IOError, msg
    492503'''
    493 def _read_pts_file(file_name, verbose = False):
     504
     505
     506def _new_read_pts_file(file_name, verbose = False):
    494507    """Read .pts NetCDF file
    495508   
     
    516529    fid = NetCDFFile(file_name, 'r')
    517530   
    518     point_atts = {} 
     531#    point_atts = {} 
    519532        # Get the variables
    520     point_atts['pointlist'] = array(fid.variables['points'])
     533#    point_atts['pointlist'] = array(fid.variables['points'])
     534    pointlist = array(fid.variables['points'])
    521535    keys = fid.variables.keys()
    522536    if verbose: print 'Got %d variables: %s' %(len(keys), keys)
     
    534548        attributes[key] = array(fid.variables[key])
    535549   
    536     point_atts['attributelist'] = attributes
     550#    point_atts['attributelist'] = attributes
    537551   
    538552    try:
    539553        geo_reference = Geo_reference(NetCDFObject=fid)
    540         point_atts['geo_reference'] = geo_reference
     554#        point_atts['geo_reference'] = geo_reference
    541555    except AttributeError, e:
    542556        #geo_ref not compulsory
    543         point_atts['geo_reference'] = None
     557#        point_atts['geo_reference'] = None
     558        geo_reference = None
    544559   
    545560    fid.close()
    546     return point_atts
    547 
    548 
    549 def _read_xya_file( fd, delimiter):
     561   
     562    return pointlist, attributes, geo_reference
     563
     564
     565def _new_read_xya_file( fd, delimiter):
    550566#    print 'hello from read xya data'
    551567    points = []
     
    591607        geo_reference = Geo_reference(ASCIIFile=fd,read_title=line)
    592608   
     609#    xya_dict = {}
     610#    xya_dict['pointlist'] = array(points).astype(Float)
     611    pointlist = array(points).astype(Float)
     612   
     613    for key in att_dict.keys():
     614        att_dict[key] = array(att_dict[key]).astype(Float)
     615#    xya_dict['attributelist'] = att_dict
     616   
     617#    xya_dict['geo_reference'] = geo_reference
     618    #print "xya_dict",xya_dict
     619    return pointlist, att_dict, geo_reference
     620
     621
     622def _read_pts_file(file_name, verbose = False):
     623    """Read .pts NetCDF file
     624   
     625    Return a dic of array of points, and dic of array of attribute
     626    eg
     627    dic['points'] = [[1.0,2.0],[3.0,5.0]]
     628    dic['attributelist']['elevation'] = [[7.0,5.0]
     629    """   
     630    #FIXME: (DSG) This format has issues.
     631    # There can't be an attribute called points
     632    # consider format change
     633
     634#    print 'hi for read points file'
     635    from Scientific.IO.NetCDF import NetCDFFile
     636   
     637    if verbose: print 'Reading ', file_name
     638   
     639       
     640    # see if the file is there.  Throw a QUIET IO error if it isn't
     641    fd = open(file_name,'r')
     642    fd.close()
     643   
     644    #throws prints to screen if file not present
     645    fid = NetCDFFile(file_name, 'r')
     646   
     647    point_atts = {} 
     648        # Get the variables
     649    point_atts['pointlist'] = array(fid.variables['points'])
     650    keys = fid.variables.keys()
     651    if verbose: print 'Got %d variables: %s' %(len(keys), keys)
     652    try:
     653        keys.remove('points')
     654    except IOError, e:       
     655        fid.close()   
     656        msg = 'Expected keyword "points" but could not find it'
     657        raise IOError, msg
     658   
     659    attributes = {}
     660    for key in keys:
     661        if verbose: print "reading attribute '%s'" %key
     662           
     663        attributes[key] = array(fid.variables[key])
     664   
     665    point_atts['attributelist'] = attributes
     666   
     667    try:
     668        geo_reference = Geo_reference(NetCDFObject=fid)
     669        point_atts['geo_reference'] = geo_reference
     670    except AttributeError, e:
     671        #geo_ref not compulsory
     672        point_atts['geo_reference'] = None
     673   
     674    fid.close()
     675    return point_atts
     676
     677
     678def _read_xya_file( fd, delimiter):
     679#    print 'hello from read xya data'
     680    points = []
     681    pointattributes = []
     682    title = fd.readline()
     683    att_names = clean_line(title,delimiter)
     684   
     685    att_dict = {}
     686    line = fd.readline()
     687    numbers = clean_line(line,delimiter)
     688    #print 'read xya numbers', numbers
     689    while len(numbers) > 1:
     690        if numbers != []:
     691            try:
     692                x = float(numbers[0])
     693                y = float(numbers[1])
     694                points.append([x,y])
     695                numbers.pop(0)
     696                numbers.pop(0)
     697                #attributes = []
     698                #print "att_names",att_names
     699                #print "numbers",numbers
     700                if len(att_names) != len(numbers):
     701                    fd.close()
     702                    # It might not be a problem with the title
     703                    #raise TitleAmountError
     704                    raise IOError
     705                for i,num in enumerate(numbers):
     706                    num.strip()
     707                    if num != '\n' and num != '':
     708                        #attributes.append(float(num))
     709                        att_dict.setdefault(att_names[i],[]).append(float(num))
     710                       
     711            except ValueError:
     712                raise SyntaxError
     713        line = fd.readline()
     714        numbers = clean_line(line,delimiter)
     715   
     716    if line == '':
     717            # end of file
     718        geo_reference = None
     719    else:
     720        geo_reference = Geo_reference(ASCIIFile=fd,read_title=line)
     721   
    593722    xya_dict = {}
    594723    xya_dict['pointlist'] = array(points).astype(Float)
  • inundation/geospatial_data/test_geospatial_data.py

    r2624 r2643  
    337337        file.close()
    338338        #print fileName
     339        dict = import_points_file(fileName,delimiter = ',')
    339340        dict = import_points_file(fileName,delimiter = ',')
    340341        os.remove(fileName)
Note: See TracChangeset for help on using the changeset viewer.