Ignore:
Timestamp:
Feb 10, 2009, 11:11:04 AM (15 years ago)
Author:
rwilson
Message:

Initial commit of numpy changes. Still a long way to go.

Location:
branches/numpy
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/numpy/anuga/geospatial_data/geospatial_data.py

    r6166 r6304  
    1313from Scientific.IO.NetCDF import NetCDFFile
    1414
    15 import Numeric as num
     15import numpy as num
    1616
    1717from anuga.coordinate_transforms.lat_long_UTM_conversion import UTMtoLL
     
    2323from anuga.config import points_file_block_line_size as MAX_READ_LINES
    2424from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a
     25from anuga.config import netcdf_float
    2526
    2627DEFAULT_ATTRIBUTE = 'elevation'
     
    5657                 load_file_now=True,
    5758                 verbose=False):
    58         """
    59         Create instance from data points and associated attributes
     59        """Create instance from data points and associated attributes
    6060
    6161        data_points: x,y coordinates in meters. Type must be either a
    62         sequence of 2-tuples or an Mx2 Numeric array of floats.  A file name
     62        sequence of 2-tuples or an Mx2 numeric array of floats.  A file name
    6363        with extension .txt, .cvs or .pts can also be passed in here.
    6464
     
    131131
    132132        verbose:
    133 
    134133        """
    135134
    136135        if isinstance(data_points, basestring):
    137             # assume data point is really a file name
     136            # assume data_points is really a file name
    138137            file_name = data_points
    139138
    140139        self.set_verbose(verbose)
    141         self.geo_reference = None #create the attribute
     140        self.geo_reference = None # create the attribute
    142141        self.file_name = file_name
    143142
     
    157156                                        data_points=data_points,
    158157                                        points_are_lats_longs=\
    159                                             points_are_lats_longs)
     158                                        points_are_lats_longs)
    160159            self.check_data_points(data_points)
    161160            self.set_attributes(attributes)
     
    188187                        # This message was misleading.
    189188                        # FIXME (Ole): Are we blocking here or not?
    190                         #print 'ASCII formats are not that great for '
    191                         #print 'blockwise reading. Consider storing this'
    192                         #print 'data as a pts NetCDF format'
     189                        # print 'ASCII formats are not that great for '
     190                        # print 'blockwise reading. Consider storing this'
     191                        # print 'data as a pts NetCDF format'
    193192
    194193    ##
     
    203202
    204203    ##
    205     # @brief Save points data in instance.
    206     # @param data_points Points data to store in instance and check.
     204    # @brief Check data points.
     205    # @param data_points Points data to check and store in instance.
    207206    # @note Throws ValueError exception if no data.
    208207    def check_data_points(self, data_points):
    209         """Checks data points
    210         """
     208        """Checks data points"""
    211209
    212210        if data_points is None:
     
    217215        else:
    218216            self.data_points = ensure_numeric(data_points)
     217            return
     218
     219            print 'self.data_points=%s' % str(self.data_points)
     220            print 'self.data_points.shape=%s' % str(self.data_points.shape)
    219221            if not (0,) == self.data_points.shape:
    220222                assert len(self.data_points.shape) == 2
     
    226228    # @note Throws exception if unable to convert dict keys to numeric.
    227229    def set_attributes(self, attributes):
    228         """Check and assign attributes dictionary
    229         """
     230        """Check and assign attributes dictionary"""
    230231
    231232        if attributes is None:
     
    234235
    235236        if not isinstance(attributes, DictType):
    236             #Convert single attribute into dictionary
     237            # Convert single attribute into dictionary
    237238            attributes = {DEFAULT_ATTRIBUTE: attributes}
    238239
    239         #Check input attributes
     240        # Check input attributes
    240241        for key in attributes.keys():
    241242            try:
    242243                attributes[key] = ensure_numeric(attributes[key])
    243244            except:
    244                 msg = 'Attribute %s could not be converted' %key
    245                 msg += 'to a numeric vector'
    246                 raise msg
     245                msg = ("Attribute '%s' (%s) could not be converted to a"
     246                       "numeric vector" % (str(key), str(attributes[key])))
     247                raise Exception, msg
    247248
    248249        self.attributes = attributes
    249250
    250     #def set_geo_reference(self, geo_reference):
    251     #    # FIXME (Ole): Backwards compatibility - deprecate
    252     #    self.setgeo_reference(geo_reference)
    253 
    254251    ##
    255252    # @brief Set the georeference of geospatial data.
    256     # @param geo_reference The georeference data    to set.
     253    # @param geo_reference The georeference data to set.
    257254    # @note Will raise exception if param not instance of Geo_reference.
    258255    def set_geo_reference(self, geo_reference):
     
    277274            msg = 'Argument geo_reference must be a valid Geo_reference '
    278275            msg += 'object or None.'
    279             raise msg
     276            raise Expection, msg
    280277
    281278        # If a geo_reference already exists, change the point data according to
     
    511508                        raise Exception, msg
    512509        else:
    513             #other is None:
     510            # other is None:
    514511            new_points = self.get_data_points(absolute=True)
    515512            new_attributes = self.attributes
     
    525522    # @return The new geospatial object.
    526523    def __radd__(self, other):
    527         """Handle cases like None + Geospatial_data(...)
    528         """
     524        """Handle cases like None + Geospatial_data(...)"""
    529525
    530526        return self + other
     
    542538    def import_points_file(self, file_name, delimiter=None, verbose=False):
    543539        """ load an .txt, .csv or .pts file
     540
    544541        Note: will throw an IOError/SyntaxError if it can't load the file.
    545542        Catch these!
     
    571568            except SyntaxError, e:
    572569                # This should only be if there is a format error
    573                 msg = 'Could not open file %s. \n' %file_name
     570                msg = 'Problem with format of file %s. \n' %file_name
    574571                msg += Error_message['IOError']
    575572                raise SyntaxError, msg
     
    591588                           as_lat_long=False, isSouthHemisphere=True):
    592589
    593         """
    594         write a points file, file_name, as a text (.csv) or binary (.pts) file
     590        """write a points file as a text (.csv) or binary (.pts) file
     591
    595592        file_name is the file name, including the extension
    596593        The point_dict is defined at the top of this file.
     
    659656        """
    660657
    661         #FIXME: add the geo_reference to this
     658        # FIXME: add the geo_reference to this
    662659        points = self.get_data_points()
    663660        sampled_points = num.take(points, indices)
     
    679676    # @note Points in each result object are selected randomly.
    680677    def split(self, factor=0.5, seed_num=None, verbose=False):
    681         """Returns two
    682         geospatial_data object, first is the size of the 'factor'
     678        """Returns two geospatial_data object, first is the size of the 'factor'
    683679        smaller the original and the second is the remainder. The two
    684         new object are disjoin set of each other.
     680        new objects are disjoint sets of each other.
    685681
    686682        Points of the two new object have selected RANDOMLY.
     
    707703        if verbose: print "make unique random number list and get indices"
    708704
    709         total=num.array(range(self_size), num.Int)     #array default#
     705        total=num.array(range(self_size))
    710706        total_list = total.tolist()
    711707
     
    731727        random_num = random_num.tolist()
    732728
    733         #need to sort and reverse so the pop() works correctly
     729        # need to sort and reverse so the pop() works correctly
    734730        random_num.sort()
    735731        random_num.reverse()
     
    748744            random_list.append(remainder_list.pop(i))
    749745            j += 1
    750             #prints progress
     746            # prints progress
    751747            if verbose and round(random_num_len/10*k) == j:
    752748                print '(%s/%s)' % (j, random_num_len)
     
    779775        """
    780776        from Scientific.IO.NetCDF import NetCDFFile
    781         #FIXME - what to do if the file isn't there
     777        # FIXME - what to do if the file isn't there
    782778
    783779        # FIXME (Ole): Shouldn't this go into the constructor?
     
    941937                        data_points,
    942938                        points_are_lats_longs):
    943     """
    944     if the points has lat long info, assume it is in (lat, long) order.
    945     """
     939    """If the points has lat long info, assume it is in (lat, long) order."""
    946940
    947941    if geo_reference is not None:
     
    10551049                                                 header,
    10561050                                                 max_read_lines=1e30)
    1057                                     #If the file is bigger that this, block..
     1051                                    # If the file is bigger that this, block..
    10581052                                    # FIXME (Ole) What's up here?
    10591053    except ANUGAError:
     
    10821076    """
    10831077
    1084     line = file_pointer.readline()
     1078    line = file_pointer.readline().strip()
    10851079    header = clean_line(line, delimiter)
    10861080
     
    10951089# @param verbose True if this function is to be verbose.
    10961090# @note Will throw IndexError, SyntaxError exceptions.
    1097 def _read_csv_file_blocking(file_pointer, header,
     1091def _read_csv_file_blocking(file_pointer,
     1092                            header,
    10981093                            delimiter=CSV_DELIMITER,
    10991094                            max_read_lines=MAX_READ_LINES,
     
    11501145        raise StopIteration
    11511146
    1152     pointlist = num.array(points, num.Float)
     1147    pointlist = num.array(points, num.float)
    11531148    for key in att_dict.keys():
    1154         att_dict[key] = num.array(att_dict[key], num.Float)
     1149        att_dict[key] = num.array(att_dict[key], num.float)
    11551150
    11561151    # Do stuff here so the info is in lat's and longs
     
    11831178# @note Will throw IOError and AttributeError exceptions.
    11841179def _read_pts_file_header(fid, verbose=False):
    1185     """Read the geo_reference and number_of_points from a .pts file
    1186     """
     1180    """Read the geo_reference and number_of_points from a .pts file"""
    11871181
    11881182    keys = fid.variables.keys()
     
    12121206# @return Tuple of (pointlist, attributes).
    12131207def _read_pts_file_blocking(fid, start_row, fin_row, keys):
    1214     """Read the body of a .csv file.
    1215     """
     1208    """Read the body of a .csv file."""
    12161209
    12171210    pointlist = num.array(fid.variables['points'][start_row:fin_row])
     
    12391232
    12401233    WARNING: This function mangles the point_atts data structure
    1241     #F??ME: (DSG)This format has issues.
     1234    # F??ME: (DSG)This format has issues.
    12421235    # There can't be an attribute called points
    12431236    # consider format change
     
    12641257    shape = write_data_points.shape[0]
    12651258    outfile.createDimension('number_of_points', shape)
    1266     outfile.createDimension('number_of_dimensions', 2) #This is 2d data
     1259    outfile.createDimension('number_of_dimensions', 2) # This is 2d data
    12671260
    12681261    # Variable definition
    1269     outfile.createVariable('points', num.Float, ('number_of_points',
    1270                                                  'number_of_dimensions'))
    1271 
    1272     #create variables
    1273     outfile.variables['points'][:] = write_data_points #.astype(Float32)
     1262    outfile.createVariable('points', netcdf_float, ('number_of_points',
     1263                                                    'number_of_dimensions'))
     1264
     1265    # create variables
     1266    outfile.variables['points'][:] = write_data_points
    12741267
    12751268    if write_attributes is not None:
    12761269        for key in write_attributes.keys():
    1277             outfile.createVariable(key, num.Float, ('number_of_points',))
    1278             outfile.variables[key][:] = write_attributes[key] #.astype(Float32)
     1270            outfile.createVariable(key, netcdf_float, ('number_of_points',))
     1271            outfile.variables[key][:] = write_attributes[key]
    12791272
    12801273    if write_geo_reference is not None:
     
    12961289                    as_lat_long=False,
    12971290                    delimiter=','):
    1298     """Write a .csv file.
    1299     """
     1291    """Write a .csv file."""
    13001292
    13011293    points = write_data_points
     
    13611353# @return ??
    13621354def _point_atts2array(point_atts):
    1363     point_atts['pointlist'] = num.array(point_atts['pointlist'], num.Float)
     1355    point_atts['pointlist'] = num.array(point_atts['pointlist'], num.float)
    13641356
    13651357    for key in point_atts['attributelist'].keys():
    13661358        point_atts['attributelist'][key] = \
    1367                 num.array(point_atts['attributelist'][key], num.Float)
     1359                num.array(point_atts['attributelist'][key], num.float)
    13681360
    13691361    return point_atts
     
    13751367# @return A points dictionary.
    13761368def geospatial_data2points_dictionary(geospatial_data):
    1377     """Convert geospatial data to points_dictionary
    1378     """
     1369    """Convert geospatial data to points_dictionary"""
    13791370
    13801371    points_dictionary = {}
     
    13961387# @param points_dictionary A points dictionary to convert.
    13971388def points_dictionary2geospatial_data(points_dictionary):
    1398     """Convert points_dictionary to geospatial data object
    1399     """
     1389    """Convert points_dictionary to geospatial data object"""
    14001390
    14011391    msg = 'Points dictionary must have key pointlist'
     
    14171407##
    14181408# @brief Split a string into 'clean' fields.
    1419 # @param line The string to process.
     1409# @param str The string to process.
    14201410# @param delimiter The delimiter string to split 'line' with.
    14211411# @return A list of 'cleaned' field strings.
    14221412# @note Any fields that were initially zero length will be removed.
    1423 def clean_line(line,delimiter):
    1424     """Remove whitespace
    1425     """
    1426 
    1427     line = line.strip()                 # probably unnecessary RW
    1428     numbers = line.split(delimiter)
    1429 
    1430     i = len(numbers) - 1
    1431     while i >= 0:
    1432         if numbers[i] == '':
    1433             numbers.pop(i)
    1434         else:
    1435             numbers[i] = numbers[i].strip()
    1436         i += -1
    1437 
    1438     return numbers
     1413# @note If a field contains '\n' it isn't zero length.
     1414def clean_line(str, delimiter):
     1415    """Split string on given delimiter, remove whitespace from each field."""
     1416
     1417    return [x.strip() for x in str.split(delimiter) if x != '']
    14391418
    14401419
     
    14621441    # Input check
    14631442    if isinstance(points, basestring):
    1464         #It's a string - assume it is a point file
     1443        # It's a string - assume it is a point file
    14651444        points = Geospatial_data(file_name=points)
    14661445
     
    14701449        assert geo_reference == None, msg
    14711450    else:
    1472         points = ensure_numeric(points, num.Float)
     1451        points = ensure_numeric(points, num.float)
    14731452
    14741453    # Sort of geo_reference and convert points
    14751454    if geo_reference is None:
    1476         geo = None #Geo_reference()
     1455        geo = None # Geo_reference()
    14771456    else:
    14781457        if isinstance(geo_reference, Geo_reference):
     
    14931472# @return A geospatial object.
    14941473def ensure_geospatial(points, geo_reference=None):
    1495     """
    1496     This function inputs several formats and
    1497     outputs one format. - a geospatial_data instance.
     1474    """Convert various data formats to a geospatial_data instance.
    14981475
    14991476    Inputed formats are;
     
    15141491    else:
    15151492        # List or numeric array of absolute points
    1516         points = ensure_numeric(points, num.Float)
     1493        points = ensure_numeric(points, num.float)
    15171494
    15181495    # Sort out geo reference
     
    15631540                                     cache=False,
    15641541                                     verbose=False):
    1565     """
    1566     Removes a small random sample of points from 'data_file'. Then creates
    1567     models with different alpha values from 'alpha_list' and cross validates
    1568     the predicted value to the previously removed point data. Returns the
    1569     alpha value which has the smallest covariance.
     1542    """Removes a small random sample of points from 'data_file'.
     1543    Then creates models with different alpha values from 'alpha_list' and
     1544    cross validates the predicted value to the previously removed point data.
     1545    Returns the alpha value which has the smallest covariance.
    15701546
    15711547    data_file: must not contain points outside the boundaries defined
     
    16281604        if no_boundary is True:
    16291605            msg = 'All boundaries must be defined'
    1630             raise msg
     1606            raise Expection, msg
    16311607
    16321608        poly_topo = [[east_boundary,south_boundary],
     
    16451621
    16461622    else: # if mesh file provided
    1647         #test mesh file exists?
     1623        # test mesh file exists?
    16481624        if verbose: "reading from file: %s" % mesh_file
    16491625        if access(mesh_file,F_OK) == 0:
     
    16511627            raise IOError, msg
    16521628
    1653     #split topo data
     1629    # split topo data
    16541630    if verbose: print 'Reading elevation file: %s' % data_file
    16551631    G = Geospatial_data(file_name = data_file)
     
    16681644        alphas = alpha_list
    16691645
    1670     #creates array with columns 1 and 2 are x, y. column 3 is elevation
    1671     #4 onwards is the elevation_predicted using the alpha, which will
    1672     #be compared later against the real removed data
    1673     data = num.array([], typecode=num.Float)
     1646    # creates array with columns 1 and 2 are x, y. column 3 is elevation
     1647    # 4 onwards is the elevation_predicted using the alpha, which will
     1648    # be compared later against the real removed data
     1649    data = num.array([], dtype=num.float)
    16741650
    16751651    data=num.resize(data, (len(points), 3+len(alphas)))
    16761652
    1677     #gets relative point from sample
     1653    # gets relative point from sample
    16781654    data[:,0] = points[:,0]
    16791655    data[:,1] = points[:,1]
     
    16811657    data[:,2] = elevation_sample
    16821658
    1683     normal_cov=num.array(num.zeros([len(alphas), 2]), typecode=num.Float)
     1659    normal_cov=num.array(num.zeros([len(alphas), 2]), dtype=num.float)
    16841660
    16851661    if verbose: print 'Setup computational domains with different alphas'
    16861662
    16871663    for i, alpha in enumerate(alphas):
    1688         #add G_other data to domains with different alphas
     1664        # add G_other data to domains with different alphas
    16891665        if verbose:
    16901666            print '\n Calculating domain and mesh for Alpha = ', alpha, '\n'
     
    17001676        points_geo = Geospatial_data(points, domain.geo_reference)
    17011677
    1702         #returns the predicted elevation of the points that were "split" out
    1703         #of the original data set for one particular alpha
     1678        # returns the predicted elevation of the points that were "split" out
     1679        # of the original data set for one particular alpha
    17041680        if verbose: print 'Get predicted elevation for location to be compared'
    17051681        elevation_predicted = \
     
    17071683                    get_values(interpolation_points=points_geo)
    17081684
    1709         #add predicted elevation to array that starts with x, y, z...
     1685        # add predicted elevation to array that starts with x, y, z...
    17101686        data[:,i+3] = elevation_predicted
    17111687
     
    18341810        if no_boundary is True:
    18351811            msg = 'All boundaries must be defined'
    1836             raise msg
     1812            raise Expection, msg
    18371813
    18381814        poly_topo = [[east_boundary,south_boundary],
     
    18511827
    18521828    else: # if mesh file provided
    1853         #test mesh file exists?
     1829        # test mesh file exists?
    18541830        if access(mesh_file,F_OK) == 0:
    18551831            msg = "file %s doesn't exist!" % mesh_file
    18561832            raise IOError, msg
    18571833
    1858     #split topo data
     1834    # split topo data
    18591835    G = Geospatial_data(file_name=data_file)
    18601836    if verbose: print 'start split'
     
    18631839    points = G_small.get_data_points()
    18641840
    1865     #FIXME: Remove points outside boundary polygon
     1841    # FIXME: Remove points outside boundary polygon
    18661842#    print 'new point',len(points)
    18671843#
    18681844#    new_points=[]
    1869 #    new_points=array([],typecode=Float)
     1845#    new_points=array([],dtype=float)
    18701846#    new_points=resize(new_points,(len(points),2))
    18711847#    print "BOUNDARY", boundary_poly
     
    18901866
    18911867    for alpha in alphas:
    1892         #add G_other data to domains with different alphas
     1868        # add G_other data to domains with different alphas
    18931869        if verbose:
    18941870            print '\n Calculating domain and mesh for Alpha = ', alpha, '\n'
     
    19021878        domains[alpha] = domain
    19031879
    1904     #creates array with columns 1 and 2 are x, y. column 3 is elevation
    1905     #4 onwards is the elevation_predicted using the alpha, which will
    1906     #be compared later against the real removed data
    1907     data = num.array([], typecode=num.Float)
     1880    # creates array with columns 1 and 2 are x, y. column 3 is elevation
     1881    # 4 onwards is the elevation_predicted using the alpha, which will
     1882    # be compared later against the real removed data
     1883    data = num.array([], dtype=num.float)
    19081884
    19091885    data = num.resize(data, (len(points), 3+len(alphas)))
    19101886
    1911     #gets relative point from sample
     1887    # gets relative point from sample
    19121888    data[:,0] = points[:,0]
    19131889    data[:,1] = points[:,1]
     
    19151891    data[:,2] = elevation_sample
    19161892
    1917     normal_cov = num.array(num.zeros([len(alphas), 2]), typecode=num.Float)
     1893    normal_cov = num.array(num.zeros([len(alphas), 2]), dtype=num.float)
    19181894
    19191895    if verbose:
     
    19231899
    19241900        points_geo = domains[alpha].geo_reference.change_points_geo_ref(points)
    1925         #returns the predicted elevation of the points that were "split" out
    1926         #of the original data set for one particular alpha
     1901        # returns the predicted elevation of the points that were "split" out
     1902        # of the original data set for one particular alpha
    19271903        elevation_predicted = \
    19281904                domains[alpha].quantities[attribute_smoothed].\
    19291905                        get_values(interpolation_points=points_geo)
    19301906
    1931         #add predicted elevation to array that starts with x, y, z...
     1907        # add predicted elevation to array that starts with x, y, z...
    19321908        data[:,i+3] = elevation_predicted
    19331909
Note: See TracChangeset for help on using the changeset viewer.