Ignore:
Timestamp:
Jun 30, 2009, 2:07:41 PM (16 years ago)
Author:
ole
Message:

Merged numpy branch back into the trunk.

In ~/sandpit/anuga/anuga_core/source
svn merge -r 6246:HEAD ../../branches/numpy .

In ~/sandpit/anuga/anuga_validation
svn merge -r 6417:HEAD ../branches/numpy_anuga_validation .

In ~/sandpit/anuga/misc
svn merge -r 6809:HEAD ../branches/numpy_misc .

For all merges, I used numpy version where conflicts existed

The suites test_all.py (in source/anuga) and validate_all.py passed using Python2.5 with numpy on my Ubuntu Linux box.

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

Legend:

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

    r6438 r7276  
    1 """Class Geospatial_data - Manipulation of locations on the planet and
    2 associated attributes.
    3 
     1"""Class Geospatial_data
     2
     3Manipulation of locations on the planet and associated attributes.
    44"""
    55
     
    99from warnings import warn
    1010from string import lower
    11 from RandomArray import randint, seed, get_seed
    1211from copy import deepcopy
     12import copy
     13
    1314from Scientific.IO.NetCDF import NetCDFFile
    14 
    15 import Numeric as num
     15import numpy as num
     16from numpy.random import randint, seed
    1617
    1718from anuga.coordinate_transforms.lat_long_UTM_conversion import UTMtoLL
     
    2021     TitleError, DEFAULT_ZONE, ensure_geo_reference, write_NetCDF_georeference
    2122from anuga.coordinate_transforms.redfearn import convert_from_latlon_to_utm
     23from anuga.utilities.system_tools import clean_line
    2224from anuga.utilities.anuga_exceptions import ANUGAError
    2325from anuga.config import points_file_block_line_size as MAX_READ_LINES
    2426from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a
     27from anuga.config import netcdf_float
     28
    2529
    2630DEFAULT_ATTRIBUTE = 'elevation'
     
    5660                 load_file_now=True,
    5761                 verbose=False):
    58         """
    59         Create instance from data points and associated attributes
     62        """Create instance from data points and associated attributes
    6063
    6164        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
     65        sequence of 2-tuples or an Mx2 numeric array of floats.  A file name
    6366        with extension .txt, .cvs or .pts can also be passed in here.
    6467
     
    131134
    132135        verbose:
    133 
    134136        """
    135137
    136138        if isinstance(data_points, basestring):
    137             # assume data point is really a file name
     139            # assume data_points is really a file name
    138140            file_name = data_points
    139141
    140142        self.set_verbose(verbose)
    141         self.geo_reference = None #create the attribute
     143        self.geo_reference = None
    142144        self.file_name = file_name
    143145
     
    148150
    149151        if file_name is None:
    150             if latitudes is not None \
    151                or longitudes is not None \
    152                or points_are_lats_longs:
     152            if (latitudes is not None or longitudes is not None
     153                    or points_are_lats_longs):
    153154                data_points, geo_reference = \
    154155                    _set_using_lat_long(latitudes=latitudes,
     
    156157                                        geo_reference=geo_reference,
    157158                                        data_points=data_points,
    158                                         points_are_lats_longs=\
     159                                        points_are_lats_longs=
    159160                                            points_are_lats_longs)
    160161            self.check_data_points(data_points)
     
    188189                        # This message was misleading.
    189190                        # 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'
     191                        # print 'ASCII formats are not that great for '
     192                        # print 'blockwise reading. Consider storing this'
     193                        # print 'data as a pts NetCDF format'
    193194
    194195    ##
     
    203204
    204205    ##
    205     # @brief Save points data in instance.
    206     # @param data_points Points data to store in instance and check.
     206    # @brief Check data points.
     207    # @param data_points Points data to check and store in instance.
    207208    # @note Throws ValueError exception if no data.
    208209    def check_data_points(self, data_points):
    209         """Checks data points
    210         """
     210        """Checks data points"""
    211211
    212212        if data_points is None:
     
    214214            msg = 'There is no data or file provided!'
    215215            raise ValueError, msg
    216 
    217216        else:
    218217            self.data_points = ensure_numeric(data_points)
     
    226225    # @note Throws exception if unable to convert dict keys to numeric.
    227226    def set_attributes(self, attributes):
    228         """Check and assign attributes dictionary
    229         """
     227        """Check and assign attributes dictionary"""
    230228
    231229        if attributes is None:
     
    234232
    235233        if not isinstance(attributes, DictType):
    236             #Convert single attribute into dictionary
     234            # Convert single attribute into dictionary
    237235            attributes = {DEFAULT_ATTRIBUTE: attributes}
    238236
    239         #Check input attributes
     237        # Check input attributes
    240238        for key in attributes.keys():
    241239            try:
    242240                attributes[key] = ensure_numeric(attributes[key])
    243241            except:
    244                 msg = 'Attribute %s could not be converted' %key
    245                 msg += 'to a numeric vector'
    246                 raise msg
     242                msg = ("Attribute '%s' (%s) could not be converted to a"
     243                       "numeric vector" % (str(key), str(attributes[key])))
     244                raise Exception, msg
    247245
    248246        self.attributes = attributes
    249247
    250     #def set_geo_reference(self, geo_reference):
    251     #    # FIXME (Ole): Backwards compatibility - deprecate
    252     #    self.setgeo_reference(geo_reference)
    253 
    254248    ##
    255249    # @brief Set the georeference of geospatial data.
    256     # @param geo_reference The georeference data    to set.
     250    # @param geo_reference The georeference data to set.
    257251    # @note Will raise exception if param not instance of Geo_reference.
    258252    def set_geo_reference(self, geo_reference):
     
    263257        """
    264258
    265         from anuga.coordinate_transforms.geo_reference import Geo_reference
    266 
    267259        if geo_reference is None:
    268260            # Use default - points are in absolute coordinates
     
    275267            # FIXME (Ole): This exception will be raised even
    276268            # if geo_reference is None. Is that the intent Duncan?
    277             msg = 'Argument geo_reference must be a valid Geo_reference '
    278             msg += 'object or None.'
    279             raise msg
     269            msg = ('Argument geo_reference must be a valid Geo_reference '
     270                   'object or None.')
     271            raise Expection, msg
    280272
    281273        # If a geo_reference already exists, change the point data according to
     
    384376    # @param isSouthHemisphere If True, return lat/lon points in S.Hemi.
    385377    # @return A set of data points, in appropriate form.
    386     def get_data_points(self, absolute=True, geo_reference=None,
    387                         as_lat_long=False, isSouthHemisphere=True):
     378    def get_data_points(self,
     379                        absolute=True,
     380                        geo_reference=None,
     381                        as_lat_long=False,
     382                        isSouthHemisphere=True):
    388383        """Get coordinates for all data points as an Nx2 array
    389384
     
    466461    # @return The new geospatial object.
    467462    def __add__(self, other):
    468         """Returns the addition of 2 geospatical objects,
     463        """Returns the addition of 2 geospatial objects,
    469464        objects are concatencated to the end of each other
    470465
     
    494489            if self.attributes is None:
    495490                if other.attributes is not None:
    496                     msg = 'Geospatial data must have the same '
    497                     msg += 'attributes to allow addition.'
     491                    msg = ('Geospatial data must have the same '
     492                           'attributes to allow addition.')
    498493                    raise Exception, msg
    499494
     
    505500                        attrib1 = self.attributes[x]
    506501                        attrib2 = other.attributes[x]
    507                         new_attributes[x] = num.concatenate((attrib1, attrib2))
     502                        new_attributes[x] = num.concatenate((attrib1, attrib2),
     503                                                            axis=0) #??default#
    508504                    else:
    509                         msg = 'Geospatial data must have the same \n'
    510                         msg += 'attributes to allow addition.'
     505                        msg = ('Geospatial data must have the same '
     506                               'attributes to allow addition.')
    511507                        raise Exception, msg
    512508        else:
    513             #other is None:
     509            # other is None:
    514510            new_points = self.get_data_points(absolute=True)
    515511            new_attributes = self.attributes
     
    525521    # @return The new geospatial object.
    526522    def __radd__(self, other):
    527         """Handle cases like None + Geospatial_data(...)
    528         """
     523        """Handle cases like None + Geospatial_data(...)"""
    529524
    530525        return self + other
    531526
    532     ###
    533     #  IMPORT/EXPORT POINTS FILES
    534     ###
     527################################################################################
     528#  IMPORT/EXPORT POINTS FILES
     529################################################################################
    535530
    536531    ##
     
    542537    def import_points_file(self, file_name, delimiter=None, verbose=False):
    543538        """ load an .txt, .csv or .pts file
     539
    544540        Note: will throw an IOError/SyntaxError if it can't load the file.
    545541        Catch these!
     
    553549
    554550        attributes = {}
    555         if file_name[-4:]== ".pts":
     551        if file_name[-4:] == ".pts":
    556552            try:
    557553                data_points, attributes, geo_reference = \
     
    560556                msg = 'Could not open file %s ' % file_name
    561557                raise IOError, msg
    562         elif file_name[-4:]== ".txt" or file_name[-4:]== ".csv":
     558        elif file_name[-4:] == ".txt" or file_name[-4:]== ".csv":
    563559            try:
    564560                data_points, attributes, geo_reference = \
     
    566562            except IOError, e:
    567563                # This should only be if a file is not found
    568                 msg = 'Could not open file %s. ' % file_name
    569                 msg += 'Check the file location.'
     564                msg = ('Could not open file %s. Check the file location.'
     565                       % file_name)
    570566                raise IOError, msg
    571567            except SyntaxError, e:
    572568                # This should only be if there is a format error
    573                 msg = 'Could not open file %s. \n' %file_name
    574                 msg += Error_message['IOError']
     569                msg = ('Problem with format of file %s.\n%s'
     570                       % (file_name, Error_message['IOError']))
    575571                raise SyntaxError, msg
    576572        else:
    577             msg = 'Extension %s is unknown' %file_name[-4:]
     573            msg = 'Extension %s is unknown' % file_name[-4:]
    578574            raise IOError, msg
    579575
     
    590586    def export_points_file(self, file_name, absolute=True,
    591587                           as_lat_long=False, isSouthHemisphere=True):
    592 
    593         """
    594         write a points file, file_name, as a text (.csv) or binary (.pts) file
     588        """write a points file as a text (.csv) or binary (.pts) file
     589
    595590        file_name is the file name, including the extension
    596591        The point_dict is defined at the top of this file.
     
    621616                                self.get_all_attributes(),
    622617                                self.get_geo_reference())
    623 
    624618        elif file_name[-4:] == ".txt" or file_name[-4:] == ".csv":
    625619            msg = "ERROR: trying to write a .txt file with relative data."
     
    628622                            self.get_data_points(absolute=True,
    629623                                                 as_lat_long=as_lat_long,
    630                                   isSouthHemisphere=isSouthHemisphere),
     624                                           isSouthHemisphere=isSouthHemisphere),
    631625                            self.get_all_attributes(),
    632626                            as_lat_long=as_lat_long)
    633 
    634627        elif file_name[-4:] == ".urs" :
    635628            msg = "ERROR: Can not write a .urs file as a relative file."
     
    637630            _write_urs_file(file_name,
    638631                            self.get_data_points(as_lat_long=True,
    639                                   isSouthHemisphere=isSouthHemisphere))
    640 
     632                                           isSouthHemisphere=isSouthHemisphere))
    641633        else:
    642634            msg = 'Unknown file type %s ' %file_name
     
    659651        """
    660652
    661         #FIXME: add the geo_reference to this
     653        # FIXME: add the geo_reference to this
    662654        points = self.get_data_points()
    663         sampled_points = num.take(points, indices)
     655        sampled_points = num.take(points, indices, axis=0)
    664656
    665657        attributes = self.get_all_attributes()
     
    668660        if attributes is not None:
    669661            for key, att in attributes.items():
    670                 sampled_attributes[key] = num.take(att, indices)
     662                sampled_attributes[key] = num.take(att, indices, axis=0)
    671663
    672664        return Geospatial_data(sampled_points, sampled_attributes)
     
    679671    # @note Points in each result object are selected randomly.
    680672    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'
     673        """Returns two geospatial_data object, first is the size of the 'factor'
    683674        smaller the original and the second is the remainder. The two
    684         new object are disjoin set of each other.
     675        new objects are disjoint sets of each other.
    685676
    686677        Points of the two new object have selected RANDOMLY.
     
    698689        """
    699690
    700         i=0
     691        i = 0
    701692        self_size = len(self)
    702693        random_list = []
    703694        remainder_list = []
    704         new_size = round(factor*self_size)
     695        new_size = round(factor * self_size)
    705696
    706697        # Find unique random numbers
    707698        if verbose: print "make unique random number list and get indices"
    708699
    709         total=num.array(range(self_size), num.Int)     #array default#
     700        total = num.array(range(self_size), num.int)    #array default#
    710701        total_list = total.tolist()
    711702
     
    721712        # Set seed if provided, mainly important for unit test!
    722713        # plus recalcule seed when no seed provided.
    723         if seed_num != None:
    724             seed(seed_num, seed_num)
     714        if seed_num is not None:
     715            seed(seed_num)
    725716        else:
    726717            seed()
    727718
    728         if verbose: print "seed:", get_seed()
     719        #if verbose: print "seed:", get_seed()
    729720
    730721        random_num = randint(0, self_size-1, (int(new_size),))
    731722        random_num = random_num.tolist()
    732723
    733         #need to sort and reverse so the pop() works correctly
     724        # need to sort and reverse so the pop() works correctly
    734725        random_num.sort()
    735726        random_num.reverse()
     
    737728        if verbose: print "make random number list and get indices"
    738729
    739         j=0
    740         k=1
     730        j = 0
     731        k = 1
    741732        remainder_list = total_list[:]
    742733
    743734        # pops array index (random_num) from remainder_list
    744         # (which starts as the
    745         # total_list and appends to random_list
     735        # (which starts as the total_list and appends to random_list)
    746736        random_num_len = len(random_num)
    747737        for i in random_num:
    748738            random_list.append(remainder_list.pop(i))
    749739            j += 1
    750             #prints progress
     740            # prints progress
    751741            if verbose and round(random_num_len/10*k) == j:
    752742                print '(%s/%s)' % (j, random_num_len)
     
    754744
    755745        # FIXME: move to tests, it might take a long time
    756         # then create an array of random lenght between 500 and 1000,
     746        # then create an array of random length between 500 and 1000,
    757747        # and use a random factor between 0 and 1
    758748        # setup for assertion
     
    760750        test_total.extend(remainder_list)
    761751        test_total.sort()
    762         msg = 'The two random lists made from the original list when added ' \
    763               'together DO NOT equal the original list'
     752        msg = ('The two random lists made from the original list when added '
     753               'together DO NOT equal the original list')
    764754        assert total_list == test_total, msg
    765755
     
    778768        file pointer position
    779769        """
    780         from Scientific.IO.NetCDF import NetCDFFile
    781         #FIXME - what to do if the file isn't there
     770
     771        # FIXME - what to do if the file isn't there
    782772
    783773        # FIXME (Ole): Shouldn't this go into the constructor?
     
    807797            self.number_of_blocks = self.number_of_points/self.max_read_lines
    808798            # This computes the number of full blocks. The last block may be
    809             # smaller and won't be ircluded in this estimate.
     799            # smaller and won't be included in this estimate.
    810800
    811801            if self.verbose is True:
    812                 print 'Reading %d points (in ~%d blocks) from file %s. ' \
    813                       % (self.number_of_points,
    814                          self.number_of_blocks,
    815                          self.file_name),
    816                 print 'Each block consists of %d data points' \
    817                       % self.max_read_lines
    818 
     802                print ('Reading %d points (in ~%d blocks) from file %s. '
     803                       % (self.number_of_points, self.number_of_blocks,
     804                          self.file_name)),
     805                print ('Each block consists of %d data points'
     806                       % self.max_read_lines)
    819807        else:
    820808            # Assume the file is a csv file
     
    847835
    848836            if self.verbose is True:
    849                 if self.show_verbose >= self.start_row \
    850                    and self.show_verbose < fin_row:
    851                     print 'Reading block %d (points %d to %d) out of %d'\
    852                           %(self.block_number,
    853                             self.start_row,
    854                             fin_row,
    855                             self.number_of_blocks)
     837                if (self.show_verbose >= self.start_row
     838                    and self.show_verbose < fin_row):
     839                    print ('Reading block %d (points %d to %d) out of %d'
     840                           % (self.block_number, self.start_row,
     841                              fin_row, self.number_of_blocks))
    856842
    857843                    self.show_verbose += max(self.max_read_lines,
    858844                                             self.verbose_block_size)
    859 
    860845
    861846            # Read next block
     
    869854
    870855            self.block_number += 1
    871 
    872856        else:
    873857            # Assume the file is a csv file
     
    876860                 att_dict,
    877861                 geo_ref,
    878                  self.file_pointer) = \
    879                         _read_csv_file_blocking(self.file_pointer,
    880                                                 self.header[:],
    881                                                 max_read_lines=\
    882                                                     self.max_read_lines,
    883                                                 verbose=self.verbose)
     862                 self.file_pointer) = _read_csv_file_blocking(self.file_pointer,
     863                                                              self.header[:],
     864                                                              max_read_lines=
     865                                                           self.max_read_lines,
     866                                                              verbose=
     867                                                                  self.verbose)
    884868
    885869                # Check that the zones haven't changed.
     
    888872                    self.blocking_georef = geo_ref
    889873                elif self.blocking_georef is not None:
    890                     msg = 'Geo reference given, then not given.'
    891                     msg += ' This should not happen.'
     874                    msg = ('Geo reference given, then not given.'
     875                           ' This should not happen.')
    892876                    raise ValueError, msg
    893877                geo = Geospatial_data(pointlist, att_dict, geo_ref)
     
    907891                del self.file_pointer
    908892                # This should only be if there is a format error
    909                 msg = 'Could not open file %s. \n' % self.file_name
    910                 msg += Error_message['IOError']
     893                msg = ('Could not open file %s.\n%s'
     894                       % (self.file_name, Error_message['IOError']))
    911895                raise SyntaxError, msg
    912896        return geo
    913 
    914897
    915898##################### Error messages ###########
    916899Error_message = {}
    917900Em = Error_message
    918 Em['IOError'] = "NOTE: The format for a comma separated .txt/.csv file is:\n"
    919 Em['IOError'] += "        1st line:     [column names]\n"
    920 Em['IOError'] += "        other lines:  [x value], [y value], [attributes]\n"
    921 Em['IOError'] += "\n"
    922 Em['IOError'] += "           for example:\n"
    923 Em['IOError'] += "           x, y, elevation, friction\n"
    924 Em['IOError'] += "           0.6, 0.7, 4.9, 0.3\n"
    925 Em['IOError'] += "           1.9, 2.8, 5, 0.3\n"
    926 Em['IOError'] += "           2.7, 2.4, 5.2, 0.3\n"
    927 Em['IOError'] += "\n"
    928 Em['IOError'] += "The first two columns are assumed to be x, y coordinates.\n"
    929 Em['IOError'] += "The attribute values must be numeric.\n"
     901Em['IOError'] = ('NOTE: The format for a comma separated .txt/.csv file is:\n'
     902                 '        1st line:     [column names]\n'
     903                 '        other lines:  [x value], [y value], [attributes]\n'
     904                 '\n'
     905                 '           for example:\n'
     906                 '           x, y, elevation, friction\n'
     907                 '           0.6, 0.7, 4.9, 0.3\n'
     908                 '           1.9, 2.8, 5, 0.3\n'
     909                 '           2.7, 2.4, 5.2, 0.3\n'
     910                 '\n'
     911                 'The first two columns are assumed to be x, y coordinates.\n'
     912                 'The attribute values must be numeric.\n')
    930913
    931914##
     
    936919# @param data_points ??
    937920# @param points_are_lats_longs ??
     921# @note IS THIS USED???
    938922def _set_using_lat_long(latitudes,
    939923                        longitudes,
     
    941925                        data_points,
    942926                        points_are_lats_longs):
    943     """
    944     if the points has lat long info, assume it is in (lat, long) order.
    945     """
     927    """If the points has lat long info, assume it is in (lat, long) order."""
    946928
    947929    if geo_reference is not None:
    948         msg = "A georeference is specified yet latitude and longitude " \
    949               "are also specified!"
     930        msg = ('A georeference is specified yet latitude and longitude '
     931               'are also specified!')
    950932        raise ValueError, msg
    951933
    952934    if data_points is not None and not points_are_lats_longs:
    953         msg = "Data points are specified yet latitude and longitude are " \
    954               "also specified."
     935        msg = ('Data points are specified yet latitude and longitude are '
     936               'also specified.')
    955937        raise ValueError, msg
    956938
     
    988970    """Read .pts NetCDF file
    989971
    990     Return a dic of array of points, and dic of array of attribute
     972    Return a (dict_points, dict_attribute, geo_ref)
    991973    eg
    992     dic['points'] = [[1.0,2.0],[3.0,5.0]]
    993     dic['attributelist']['elevation'] = [[7.0,5.0]]
     974    dict['points'] = [[1.0,2.0],[3.0,5.0]]
     975    dict['attributelist']['elevation'] = [[7.0,5.0]]
    994976    """
    995 
    996     from Scientific.IO.NetCDF import NetCDFFile
    997977
    998978    if verbose: print 'Reading ', file_name
     
    10551035                                                 header,
    10561036                                                 max_read_lines=1e30)
    1057                                     #If the file is bigger that this, block..
     1037                                    # If the file is bigger that this, block..
    10581038                                    # FIXME (Ole) What's up here?
    10591039    except ANUGAError:
     
    10951075# @param verbose True if this function is to be verbose.
    10961076# @note Will throw IndexError, SyntaxError exceptions.
    1097 def _read_csv_file_blocking(file_pointer, header,
     1077def _read_csv_file_blocking(file_pointer,
     1078                            header,
    10981079                            delimiter=CSV_DELIMITER,
    10991080                            max_read_lines=MAX_READ_LINES,
     
    11361117            if len(header) != len(numbers):
    11371118                file_pointer.close()
    1138                 msg = "File load error. " \
    1139                       "There might be a problem with the file header."
     1119                msg = ('File load error. '
     1120                       'There might be a problem with the file header.')
    11401121                raise SyntaxError, msg
    11411122            for i,n in enumerate(numbers):
    11421123                n.strip()
    11431124                if n != '\n' and n != '':
    1144                     #attributes.append(float(n))
    11451125                    att_dict.setdefault(header[i],[]).append(float(n))
    11461126        except ValueError:
     
    11501130        raise StopIteration
    11511131
    1152     pointlist = num.array(points, num.Float)
     1132    pointlist = num.array(points, num.float)
    11531133    for key in att_dict.keys():
    1154         att_dict[key] = num.array(att_dict[key], num.Float)
     1134        att_dict[key] = num.array(att_dict[key], num.float)
    11551135
    11561136    # Do stuff here so the info is in lat's and longs
     
    11831163# @note Will throw IOError and AttributeError exceptions.
    11841164def _read_pts_file_header(fid, verbose=False):
    1185     """Read the geo_reference and number_of_points from a .pts file
    1186     """
     1165    '''Read the geo_reference and number_of_points from a .pts file'''
    11871166
    11881167    keys = fid.variables.keys()
     
    12051184
    12061185##
    1207 # @brief Read the body of a .csf file, with blocking.
     1186# @brief Read the body of a .pts file, with blocking.
    12081187# @param fid Handle to already open file.
    12091188# @param start_row Start row index of points to return.
     
    12121191# @return Tuple of (pointlist, attributes).
    12131192def _read_pts_file_blocking(fid, start_row, fin_row, keys):
    1214     """Read the body of a .csv file.
    1215     """
     1193    '''Read the body of a .pts file.'''
    12161194
    12171195    pointlist = num.array(fid.variables['points'][start_row:fin_row])
     
    12391217
    12401218    WARNING: This function mangles the point_atts data structure
    1241     #F??ME: (DSG)This format has issues.
     1219    # F??ME: (DSG)This format has issues.
    12421220    # There can't be an attribute called points
    12431221    # consider format change
     
    12511229    """
    12521230
    1253     from Scientific.IO.NetCDF import NetCDFFile
    1254 
    12551231    # NetCDF file definition
    12561232    outfile = NetCDFFile(file_name, netcdf_mode_w)
     
    12581234    # Create new file
    12591235    outfile.institution = 'Geoscience Australia'
    1260     outfile.description = 'NetCDF format for compact and portable storage ' \
    1261                           'of spatial point data'
     1236    outfile.description = ('NetCDF format for compact and portable storage '
     1237                           'of spatial point data')
    12621238
    12631239    # Dimension definitions
    12641240    shape = write_data_points.shape[0]
    12651241    outfile.createDimension('number_of_points', shape)
    1266     outfile.createDimension('number_of_dimensions', 2) #This is 2d data
     1242    outfile.createDimension('number_of_dimensions', 2) # This is 2d data
    12671243
    12681244    # 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)
     1245    outfile.createVariable('points', netcdf_float,
     1246                           ('number_of_points', 'number_of_dimensions'))
     1247
     1248    # create variables
     1249    outfile.variables['points'][:] = write_data_points
    12741250
    12751251    if write_attributes is not None:
    12761252        for key in write_attributes.keys():
    1277             outfile.createVariable(key, num.Float, ('number_of_points',))
    1278             outfile.variables[key][:] = write_attributes[key] #.astype(Float32)
     1253            outfile.createVariable(key, netcdf_float, ('number_of_points',))
     1254            outfile.variables[key][:] = write_attributes[key]
    12791255
    12801256    if write_geo_reference is not None:
     
    12961272                    as_lat_long=False,
    12971273                    delimiter=','):
    1298     """Write a .csv file.
    1299     """
     1274    """Write a .csv file."""
    13001275
    13011276    points = write_data_points
     
    13611336# @return ??
    13621337def _point_atts2array(point_atts):
    1363     point_atts['pointlist'] = num.array(point_atts['pointlist'], num.Float)
     1338    point_atts['pointlist'] = num.array(point_atts['pointlist'], num.float)
    13641339
    13651340    for key in point_atts['attributelist'].keys():
    13661341        point_atts['attributelist'][key] = \
    1367                 num.array(point_atts['attributelist'][key], num.Float)
     1342                num.array(point_atts['attributelist'][key], num.float)
    13681343
    13691344    return point_atts
     
    13751350# @return A points dictionary.
    13761351def geospatial_data2points_dictionary(geospatial_data):
    1377     """Convert geospatial data to points_dictionary
    1378     """
     1352    """Convert geospatial data to points_dictionary"""
    13791353
    13801354    points_dictionary = {}
     
    13961370# @param points_dictionary A points dictionary to convert.
    13971371def points_dictionary2geospatial_data(points_dictionary):
    1398     """Convert points_dictionary to geospatial data object
    1399     """
    1400 
    1401     msg = 'Points dictionary must have key pointlist'
     1372    """Convert points_dictionary to geospatial data object"""
     1373
     1374    msg = "Points dictionary must have key 'pointlist'"
    14021375    assert points_dictionary.has_key('pointlist'), msg
    14031376
    1404     msg = 'Points dictionary must have key attributelist'
     1377    msg = "Points dictionary must have key 'attributelist'"
    14051378    assert points_dictionary.has_key('attributelist'), msg
    14061379
     
    14121385    return Geospatial_data(points_dictionary['pointlist'],
    14131386                           points_dictionary['attributelist'],
    1414                            geo_reference = geo)
    1415 
    1416 
    1417 ##
    1418 # @brief Split a string into 'clean' fields.
    1419 # @param line The string to process.
    1420 # @param delimiter The delimiter string to split 'line' with.
    1421 # @return A list of 'cleaned' field strings.
    1422 # @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
     1387                           geo_reference=geo)
    14391388
    14401389
     
    14601409    """
    14611410
    1462     import copy
    14631411    # Input check
    14641412    if isinstance(points, basestring):
    1465         #It's a string - assume it is a point file
     1413        # It's a string - assume it is a point file
    14661414        points = Geospatial_data(file_name=points)
    14671415
     
    14711419        assert geo_reference == None, msg
    14721420    else:
    1473         points = ensure_numeric(copy.copy(points), num.Float)
     1421        points = ensure_numeric(copy.copy(points), num.float)
    14741422
    14751423    # Sort of geo_reference and convert points
    14761424    if geo_reference is None:
    1477         geo = None #Geo_reference()
     1425        geo = None    # Geo_reference()
    14781426    else:
    14791427        if isinstance(geo_reference, Geo_reference):
     
    14941442# @return A geospatial object.
    14951443def ensure_geospatial(points, geo_reference=None):
    1496     """
    1497     This function inputs several formats and
    1498     outputs one format. - a geospatial_data instance.
     1444    """Convert various data formats to a geospatial_data instance.
    14991445
    15001446    Inputed formats are;
     
    15081454    """
    15091455
    1510     import copy
    15111456    # Input check
    15121457    if isinstance(points, Geospatial_data):
     
    15161461    else:
    15171462        # List or numeric array of absolute points
    1518         points = ensure_numeric(points, num.Float)
     1463        points = ensure_numeric(points, num.float)
    15191464
    15201465    # Sort out geo reference
     
    15651510                                     cache=False,
    15661511                                     verbose=False):
    1567     """
    1568     Removes a small random sample of points from 'data_file'. Then creates
    1569     models with different alpha values from 'alpha_list' and cross validates
    1570     the predicted value to the previously removed point data. Returns the
    1571     alpha value which has the smallest covariance.
     1512    """Removes a small random sample of points from 'data_file'.
     1513    Then creates models with different alpha values from 'alpha_list' and
     1514    cross validates the predicted value to the previously removed point data.
     1515    Returns the alpha value which has the smallest covariance.
    15721516
    15731517    data_file: must not contain points outside the boundaries defined
    1574                and it either a pts, txt or csv file.
     1518               and it must be either a pts, txt or csv file.
    15751519
    15761520    alpha_list: the alpha values to test in a single list
     
    16151559    from anuga.fit_interpolate.benchmark_least_squares import mem_usage
    16161560
    1617 
    1618     attribute_smoothed='elevation'
     1561    attribute_smoothed = 'elevation'
    16191562
    16201563    if mesh_file is None:
     
    16221565        mesh_file = 'temp.msh'
    16231566
    1624         if north_boundary is None or south_boundary is None \
    1625            or east_boundary is None or west_boundary is None:
     1567        if (north_boundary is None or south_boundary is None
     1568            or east_boundary is None or west_boundary is None):
    16261569            no_boundary = True
    16271570        else:
     
    16301573        if no_boundary is True:
    16311574            msg = 'All boundaries must be defined'
    1632             raise msg
    1633 
    1634         poly_topo = [[east_boundary,south_boundary],
    1635                      [east_boundary,north_boundary],
    1636                      [west_boundary,north_boundary],
    1637                      [west_boundary,south_boundary]]
     1575            raise Expection, msg
     1576
     1577        poly_topo = [[east_boundary, south_boundary],
     1578                     [east_boundary, north_boundary],
     1579                     [west_boundary, north_boundary],
     1580                     [west_boundary, south_boundary]]
    16381581
    16391582        create_mesh_from_regions(poly_topo,
     
    16471590
    16481591    else: # if mesh file provided
    1649         #test mesh file exists?
     1592        # test mesh file exists?
    16501593        if verbose: "reading from file: %s" % mesh_file
    16511594        if access(mesh_file,F_OK) == 0:
     
    16531596            raise IOError, msg
    16541597
    1655     #split topo data
     1598    # split topo data
    16561599    if verbose: print 'Reading elevation file: %s' % data_file
    16571600    G = Geospatial_data(file_name = data_file)
     
    16651608    if alpha_list == None:
    16661609        alphas = [0.001,0.01,100]
    1667         #alphas = [0.000001, 0.00001, 0.0001, 0.001, 0.01,\
    1668          #         0.1, 1.0, 10.0, 100.0,1000.0,10000.0]
     1610        #alphas = [0.000001, 0.00001, 0.0001, 0.001, 0.01,
     1611        #          0.1, 1.0, 10.0, 100.0,1000.0,10000.0]
    16691612    else:
    16701613        alphas = alpha_list
    16711614
    1672     #creates array with columns 1 and 2 are x, y. column 3 is elevation
    1673     #4 onwards is the elevation_predicted using the alpha, which will
    1674     #be compared later against the real removed data
    1675     data = num.array([], typecode=num.Float)
    1676 
    1677     data=num.resize(data, (len(points), 3+len(alphas)))
    1678 
    1679     #gets relative point from sample
     1615    # creates array with columns 1 and 2 are x, y. column 3 is elevation
     1616    # 4 onwards is the elevation_predicted using the alpha, which will
     1617    # be compared later against the real removed data
     1618    data = num.array([], dtype=num.float)
     1619
     1620    data = num.resize(data, (len(points), 3+len(alphas)))
     1621
     1622    # gets relative point from sample
    16801623    data[:,0] = points[:,0]
    16811624    data[:,1] = points[:,1]
     
    16831626    data[:,2] = elevation_sample
    16841627
    1685     normal_cov=num.array(num.zeros([len(alphas), 2]), typecode=num.Float)
     1628    normal_cov = num.array(num.zeros([len(alphas), 2]), dtype=num.float)
    16861629
    16871630    if verbose: print 'Setup computational domains with different alphas'
    16881631
    16891632    for i, alpha in enumerate(alphas):
    1690         #add G_other data to domains with different alphas
     1633        # add G_other data to domains with different alphas
    16911634        if verbose:
    1692             print '\n Calculating domain and mesh for Alpha = ', alpha, '\n'
     1635            print '\nCalculating domain and mesh for Alpha =', alpha, '\n'
    16931636        domain = Domain(mesh_file, use_cache=cache, verbose=verbose)
    16941637        if verbose: print domain.statistics()
     
    17021645        points_geo = Geospatial_data(points, domain.geo_reference)
    17031646
    1704         #returns the predicted elevation of the points that were "split" out
    1705         #of the original data set for one particular alpha
     1647        # returns the predicted elevation of the points that were "split" out
     1648        # of the original data set for one particular alpha
    17061649        if verbose: print 'Get predicted elevation for location to be compared'
    17071650        elevation_predicted = \
     
    17091652                    get_values(interpolation_points=points_geo)
    17101653
    1711         #add predicted elevation to array that starts with x, y, z...
     1654        # add predicted elevation to array that starts with x, y, z...
    17121655        data[:,i+3] = elevation_predicted
    17131656
    17141657        sample_cov = cov(elevation_sample)
    17151658        ele_cov = cov(elevation_sample - elevation_predicted)
    1716         normal_cov[i,:] = [alpha,ele_cov / sample_cov]
     1659        normal_cov[i,:] = [alpha, ele_cov / sample_cov]
    17171660
    17181661        if verbose:
     
    17221665
    17231666    normal_cov0 = normal_cov[:,0]
    1724     normal_cov_new = num.take(normal_cov, num.argsort(normal_cov0))
     1667    normal_cov_new = num.take(normal_cov, num.argsort(normal_cov0), axis=0)
    17251668
    17261669    if plot_name is not None:
     
    17371680        print 'Final results:'
    17381681        for i, alpha in enumerate(alphas):
    1739             print 'covariance for alpha %s = %s ' \
    1740                   % (normal_cov[i][0], normal_cov[i][1])
    1741         print '\n Optimal alpha is: %s ' \
    1742               % normal_cov_new[(num.argmin(normal_cov_new, axis=0))[1], 0]
     1682            print ('covariance for alpha %s = %s '
     1683                   % (normal_cov[i][0], normal_cov[i][1]))
     1684        print ('\nOptimal alpha is: %s '
     1685               % normal_cov_new[(num.argmin(normal_cov_new, axis=0))[1], 0])
    17431686
    17441687    # covariance and optimal alpha
     
    18221765    from anuga.fit_interpolate.benchmark_least_squares import mem_usage
    18231766
    1824 
    18251767    attribute_smoothed = 'elevation'
    18261768
     
    18281770        mesh_file = 'temp.msh'
    18291771
    1830         if north_boundary is None or south_boundary is None \
    1831            or east_boundary is None or west_boundary is None:
     1772        if (north_boundary is None or south_boundary is None
     1773            or east_boundary is None or west_boundary is None):
    18321774            no_boundary = True
    18331775        else:
     
    18361778        if no_boundary is True:
    18371779            msg = 'All boundaries must be defined'
    1838             raise msg
    1839 
    1840         poly_topo = [[east_boundary,south_boundary],
    1841                      [east_boundary,north_boundary],
    1842                      [west_boundary,north_boundary],
    1843                      [west_boundary,south_boundary]]
     1780            raise Expection, msg
     1781
     1782        poly_topo = [[east_boundary, south_boundary],
     1783                     [east_boundary, north_boundary],
     1784                     [west_boundary, north_boundary],
     1785                     [west_boundary, south_boundary]]
    18441786
    18451787        create_mesh_from_regions(poly_topo,
     
    18531795
    18541796    else: # if mesh file provided
    1855         #test mesh file exists?
     1797        # test mesh file exists?
    18561798        if access(mesh_file,F_OK) == 0:
    18571799            msg = "file %s doesn't exist!" % mesh_file
    18581800            raise IOError, msg
    18591801
    1860     #split topo data
     1802    # split topo data
    18611803    G = Geospatial_data(file_name=data_file)
    18621804    if verbose: print 'start split'
     
    18651807    points = G_small.get_data_points()
    18661808
    1867     #FIXME: Remove points outside boundary polygon
    1868 #    print 'new point',len(points)
    1869 #
    1870 #    new_points=[]
    1871 #    new_points=array([],typecode=Float)
    1872 #    new_points=resize(new_points,(len(points),2))
    1873 #    print "BOUNDARY", boundary_poly
    1874 #    for i,point in enumerate(points):
    1875 #        if is_inside_polygon(point,boundary_poly, verbose=True):
    1876 #            new_points[i] = point
    1877 #            print"WOW",i,new_points[i]
    1878 #    points = new_points
    1879 
    18801809    if verbose: print "Number of points in sample to compare: ", len(points)
    18811810
    18821811    if alpha_list == None:
    18831812        alphas = [0.001,0.01,100]
    1884         #alphas = [0.000001, 0.00001, 0.0001, 0.001, 0.01,\
    1885          #         0.1, 1.0, 10.0, 100.0,1000.0,10000.0]
     1813        #alphas = [0.000001, 0.00001, 0.0001, 0.001, 0.01,
     1814        #          0.1, 1.0, 10.0, 100.0,1000.0,10000.0]
    18861815    else:
    18871816        alphas = alpha_list
     
    18921821
    18931822    for alpha in alphas:
    1894         #add G_other data to domains with different alphas
     1823        # add G_other data to domains with different alphas
    18951824        if verbose:
    1896             print '\n Calculating domain and mesh for Alpha = ', alpha, '\n'
     1825            print '\nCalculating domain and mesh for Alpha =', alpha, '\n'
    18971826        domain = Domain(mesh_file, use_cache=cache, verbose=verbose)
    18981827        if verbose: print domain.statistics()
     
    19041833        domains[alpha] = domain
    19051834
    1906     #creates array with columns 1 and 2 are x, y. column 3 is elevation
    1907     #4 onwards is the elevation_predicted using the alpha, which will
    1908     #be compared later against the real removed data
    1909     data = num.array([], typecode=num.Float)
     1835    # creates array with columns 1 and 2 are x, y. column 3 is elevation
     1836    # 4 onwards is the elevation_predicted using the alpha, which will
     1837    # be compared later against the real removed data
     1838    data = num.array([], dtype=num.float)
    19101839
    19111840    data = num.resize(data, (len(points), 3+len(alphas)))
    19121841
    1913     #gets relative point from sample
     1842    # gets relative point from sample
    19141843    data[:,0] = points[:,0]
    19151844    data[:,1] = points[:,1]
     
    19171846    data[:,2] = elevation_sample
    19181847
    1919     normal_cov = num.array(num.zeros([len(alphas), 2]), typecode=num.Float)
     1848    normal_cov = num.array(num.zeros([len(alphas), 2]), dtype=num.float)
    19201849
    19211850    if verbose:
    19221851        print 'Determine difference between predicted results and actual data'
     1852
    19231853    for i, alpha in enumerate(domains):
    19241854        if verbose: print'Alpha =', alpha
    19251855
    19261856        points_geo = domains[alpha].geo_reference.change_points_geo_ref(points)
    1927         #returns the predicted elevation of the points that were "split" out
    1928         #of the original data set for one particular alpha
     1857        # returns the predicted elevation of the points that were "split" out
     1858        # of the original data set for one particular alpha
    19291859        elevation_predicted = \
    19301860                domains[alpha].quantities[attribute_smoothed].\
    19311861                        get_values(interpolation_points=points_geo)
    19321862
    1933         #add predicted elevation to array that starts with x, y, z...
     1863        # add predicted elevation to array that starts with x, y, z...
    19341864        data[:,i+3] = elevation_predicted
    19351865
     
    19411871
    19421872    normal_cov0 = normal_cov[:,0]
    1943     normal_cov_new = num.take(normal_cov, num.argsort(normal_cov0))
     1873    normal_cov_new = num.take(normal_cov, num.argsort(normal_cov0), axis=0)
    19441874
    19451875    if plot_name is not None:
  • anuga_core/source/anuga/geospatial_data/test_geospatial_data.py

    r7012 r7276  
    11#!/usr/bin/env python
    2 
    32
    43import unittest
    54import os
     5import tempfile
    66from math import sqrt, pi
    7 import tempfile
    8 
    9 import Numeric as num
     7
     8import numpy as num
    109
    1110from anuga.geospatial_data.geospatial_data import *
     
    1514from anuga.utilities.system_tools import get_host_name
    1615from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a
     16from anuga.config import netcdf_float
     17
    1718
    1819class Test_Geospatial_data(unittest.TestCase):
     
    2324        pass
    2425
    25 
    2626    def test_0(self):
    2727        #Basic points
    2828        from anuga.coordinate_transforms.geo_reference import Geo_reference
    29        
     29
    3030        points = [[1.0, 2.1], [3.0, 5.3]]
    3131        G = Geospatial_data(points)
    32 
    3332        assert num.allclose(G.data_points, [[1.0, 2.1], [3.0, 5.3]])
    3433
     
    3736        rep = `G`
    3837        ref = '[[ 1.   2.1]\n [ 3.   5.3]]'
    39 
    40         msg = 'Representation %s is not equal to %s' %(rep, ref)
     38        msg = 'Representation %s is not equal to %s' % (rep, ref)
    4139        assert rep == ref, msg
    4240
    4341        #Check getter
    4442        assert num.allclose(G.get_data_points(), [[1.0, 2.1], [3.0, 5.3]])
    45        
     43
    4644        #Check defaults
    4745        assert G.attributes is None
    48        
    4946        assert G.geo_reference.zone == Geo_reference().zone
    5047        assert G.geo_reference.xllcorner == Geo_reference().xllcorner
    5148        assert G.geo_reference.yllcorner == Geo_reference().yllcorner
    52        
    5349
    5450    def test_1(self):
    5551        points = [[1.0, 2.1], [3.0, 5.3]]
    5652        attributes = [2, 4]
    57         G = Geospatial_data(points, attributes)       
     53        G = Geospatial_data(points, attributes)
    5854        assert G.attributes.keys()[0] == DEFAULT_ATTRIBUTE
    5955        assert num.allclose(G.attributes.values()[0], [2, 4])
    60        
    6156
    6257    def test_2(self):
    6358        from anuga.coordinate_transforms.geo_reference import Geo_reference
     59
    6460        points = [[1.0, 2.1], [3.0, 5.3]]
    6561        attributes = [2, 4]
     
    7167        assert G.geo_reference.yllcorner == 200
    7268
    73 
    7469    def test_get_attributes_1(self):
    7570        from anuga.coordinate_transforms.geo_reference import Geo_reference
     71
    7672        points = [[1.0, 2.1], [3.0, 5.3]]
    7773        attributes = [2, 4]
     
    7975                            geo_reference=Geo_reference(56, 100, 200))
    8076
    81 
    8277        P = G.get_data_points(absolute=False)
    83         assert num.allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
     78        assert num.allclose(P, [[1.0, 2.1], [3.0, 5.3]])
    8479
    8580        P = G.get_data_points(absolute=True)
    86         assert num.allclose(P, [[101.0, 202.1], [103.0, 205.3]])       
     81        assert num.allclose(P, [[101.0, 202.1], [103.0, 205.3]])
    8782
    8883        V = G.get_attributes() #Simply get them
     
    9489    def test_get_attributes_2(self):
    9590        #Multiple attributes
    96        
    97        
    9891        from anuga.coordinate_transforms.geo_reference import Geo_reference
     92
    9993        points = [[1.0, 2.1], [3.0, 5.3]]
    10094        attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]}
     
    10397                            default_attribute_name='a1')
    10498
    105 
    10699        P = G.get_data_points(absolute=False)
    107         assert num.allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
    108        
     100        assert num.allclose(P, [[1.0, 2.1], [3.0, 5.3]])
     101
    109102        V = G.get_attributes() #Get default attribute
    110103        assert num.allclose(V, [2, 4])
     
    119112        assert num.allclose(V, [79.4, -7])
    120113
     114                # FIXME: use failUnlessRaises()
    121115        try:
    122116            V = G.get_attributes('hdnoatedu') #Invalid
     
    124118            pass
    125119        else:
    126             raise 'Should have raised exception'
     120            raise Exception, 'Should have raised exception'
    127121
    128122    def test_get_data_points(self):
    129         points_ab = [[12.5,34.7],[-4.5,-60.0]]
     123        points_ab = [[12.5, 34.7], [-4.5, -60.0]]
    130124        x_p = -10
    131125        y_p = -40
    132126        geo_ref = Geo_reference(56, x_p, y_p)
    133127        points_rel = geo_ref.change_points_geo_ref(points_ab)
    134        
     128
    135129        spatial = Geospatial_data(points_rel, geo_reference=geo_ref)
    136 
    137130        results = spatial.get_data_points(absolute=False)
    138        
    139131        assert num.allclose(results, points_rel)
    140        
     132
    141133        x_p = -1770
    142134        y_p = 4.01
    143135        geo_ref = Geo_reference(56, x_p, y_p)
    144136        points_rel = geo_ref.change_points_geo_ref(points_ab)
    145         results = spatial.get_data_points \
    146                   ( geo_reference=geo_ref)
    147        
     137        results = spatial.get_data_points(geo_reference=geo_ref)
     138
    148139        assert num.allclose(results, points_rel)
    149140
    150  
    151141    def test_get_data_points_lat_long(self):
    152         # lat long [-30.],[130]
    153         #Zone:   52   
    154         #Easting:  596450.153  Northing: 6680793.777
    155         # lat long [-32.],[131]
    156         #Zone:   52   
    157         #Easting:  688927.638  Northing: 6457816.509
    158        
    159         points_Lat_long = [[-30.,130], [-32,131]]
    160        
    161         spatial = Geospatial_data(latitudes=[-30, -32.],
    162                                   longitudes=[130, 131])
    163 
     142        # lat long [-30.], [130]
     143        # Zone:   52
     144        # Easting:  596450.153  Northing: 6680793.777
     145        # lat long [-32.], [131]
     146        # Zone:   52
     147        # Easting:  688927.638  Northing: 6457816.509
     148
     149        points_Lat_long = [[-30., 130], [-32, 131]]
     150
     151        spatial = Geospatial_data(latitudes=[-30, -32.], longitudes=[130, 131])
    164152        results = spatial.get_data_points(as_lat_long=True)
    165         #print "test_get_data_points_lat_long - results", results
    166         #print "points_Lat_long",points_Lat_long
    167153        assert num.allclose(results, points_Lat_long)
    168      
     154
    169155    def test_get_data_points_lat_longII(self):
    170156        # x,y  North,east long,lat
    171157        boundary_polygon = [[ 250000, 7630000]]
    172158        zone = 50
    173        
     159
    174160        geo_reference = Geo_reference(zone=zone)
    175         geo = Geospatial_data(boundary_polygon,geo_reference=geo_reference)
     161        geo = Geospatial_data(boundary_polygon ,geo_reference=geo_reference)
    176162        seg_lat_long = geo.get_data_points(as_lat_long=True)
    177         lat_result = degminsec2decimal_degrees(-21,24,54)
    178         long_result = degminsec2decimal_degrees(114,35,17.89)
    179         #print "seg_lat_long", seg_lat_long [0][0]
    180         #print "lat_result",lat_result
     163        lat_result = degminsec2decimal_degrees(-21, 24, 54)
     164        long_result = degminsec2decimal_degrees(114, 35, 17.89)
     165        assert num.allclose(seg_lat_long[0][0], lat_result)     #lat
     166        assert num.allclose(seg_lat_long[0][1], long_result)    #long
     167
     168    def test_get_data_points_lat_longIII(self):
     169        # x,y  North,east long,lat
     170        # for northern hemisphere
     171        boundary_polygon = [[419944.8, 918642.4]]
     172        zone = 47
     173
     174        geo_reference = Geo_reference(zone=zone)
     175        geo = Geospatial_data(boundary_polygon, geo_reference=geo_reference)
     176        seg_lat_long = geo.get_data_points(as_lat_long=True,
     177                                           isSouthHemisphere=False)
     178        lat_result = degminsec2decimal_degrees(8.31, 0, 0)
     179        long_result = degminsec2decimal_degrees(98.273, 0, 0)
    181180        assert num.allclose(seg_lat_long[0][0], lat_result)#lat
    182181        assert num.allclose(seg_lat_long[0][1], long_result)#long
    183182
    184 
    185     def test_get_data_points_lat_longIII(self):
    186         # x,y  North,east long,lat
    187         #for northern hemisphere
    188         boundary_polygon = [[419944.8, 918642.4]]
    189         zone = 47
    190        
    191         geo_reference = Geo_reference(zone=zone)
    192         geo = Geospatial_data(boundary_polygon,
    193                               geo_reference=geo_reference)
    194                              
    195         seg_lat_long = geo.get_data_points(as_lat_long=True,
    196                                            isSouthHemisphere=False)
    197                                            
    198         lat_result = degminsec2decimal_degrees(8.31,0,0)
    199         long_result = degminsec2decimal_degrees(98.273,0,0)
    200         #print "seg_lat_long", seg_lat_long [0]
    201         #print "lat_result",lat_result
    202         assert num.allclose(seg_lat_long[0][0], lat_result)#lat
    203         assert num.allclose(seg_lat_long[0][1], long_result)#long
    204 
    205 
    206              
    207183    def test_set_geo_reference(self):
    208         """test_set_georeference
    209        
    210         Test that georeference can be changed without changing the 
     184        '''test_set_georeference
     185
     186        Test that georeference can be changed without changing the
    211187        absolute values.
    212         """
    213            
    214         points_ab = [[12.5,34.7],[-4.5,-60.0]]
     188        '''
     189
     190        points_ab = [[12.5, 34.7], [-4.5, -60.0]]
    215191        x_p = -10
    216192        y_p = -40
    217193        geo_ref = Geo_reference(56, x_p, y_p)
    218194        points_rel = geo_ref.change_points_geo_ref(points_ab)
    219        
     195
    220196        # Create without geo_ref properly set
    221         G = Geospatial_data(points_rel)       
     197        G = Geospatial_data(points_rel)
    222198        assert not num.allclose(points_ab, G.get_data_points(absolute=True))
    223        
     199
    224200        # Create the way it should be
    225201        G = Geospatial_data(points_rel, geo_reference=geo_ref)
    226202        assert num.allclose(points_ab, G.get_data_points(absolute=True))
    227        
     203
    228204        # Change georeference and check that absolute values are unchanged.
    229205        x_p = 10
     
    231207        new_geo_ref = Geo_reference(56, x_p, y_p)
    232208        G.set_geo_reference(new_geo_ref)
    233         assert num.allclose(points_ab, G.get_data_points(absolute=True))
    234        
    235 
    236                
    237        
     209        msg = ('points_ab=\n%s\nbut G.get_data_points(absolute=True)=\n%s'
     210               % (str(points_ab), str(G.get_data_points(absolute=True))))
     211        assert num.allclose(points_ab, G.get_data_points(absolute=True)), msg
     212
    238213    def test_conversions_to_points_dict(self):
    239214        #test conversions to points_dict
    240        
    241        
    242215        from anuga.coordinate_transforms.geo_reference import Geo_reference
     216
    243217        points = [[1.0, 2.1], [3.0, 5.3]]
    244218        attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]}
     
    247221                            default_attribute_name='a1')
    248222
    249 
    250223        points_dict = geospatial_data2points_dictionary(G)
    251224
    252225        assert points_dict.has_key('pointlist')
    253         assert points_dict.has_key('attributelist')       
     226        assert points_dict.has_key('attributelist')
    254227        assert points_dict.has_key('geo_reference')
    255228
     
    259232        assert A.has_key('a0')
    260233        assert A.has_key('a1')
    261         assert A.has_key('a2')       
     234        assert A.has_key('a2')
    262235
    263236        assert num.allclose( A['a0'], [0, 0] )
    264         assert num.allclose( A['a1'], [2, 4] )       
     237        assert num.allclose( A['a1'], [2, 4] )
    265238        assert num.allclose( A['a2'], [79.4, -7] )
    266 
    267239
    268240        geo = points_dict['geo_reference']
    269241        assert geo is G.geo_reference
    270242
    271 
    272243    def test_conversions_from_points_dict(self):
    273         """test conversions from points_dict
    274         """
     244        '''test conversions from points_dict'''
    275245
    276246        from anuga.coordinate_transforms.geo_reference import Geo_reference
    277        
     247
    278248        points = [[1.0, 2.1], [3.0, 5.3]]
    279249        attributes = {'a0': [0, 0], 'a1': [2, 4], 'a2': [79.4, -7]}
     
    283253        points_dict['attributelist'] = attributes
    284254        points_dict['geo_reference'] = Geo_reference(56, 100, 200)
    285        
    286255
    287256        G = points_dictionary2geospatial_data(points_dict)
    288 
    289257        P = G.get_data_points(absolute=False)
    290         assert num.allclose(P, [[1.0, 2.1], [3.0, 5.3]])       
    291        
    292         #V = G.get_attribute_values() #Get default attribute
    293         #assert allclose(V, [2, 4])
     258        assert num.allclose(P, [[1.0, 2.1], [3.0, 5.3]])
    294259
    295260        V = G.get_attributes('a0') #Get by name
     
    303268
    304269    def test_add(self):
    305         """ test the addition of two geospatical objects
    306             no geo_reference see next test
    307         """
     270        '''test the addition of two geospatical objects
     271        no geo_reference see next test
     272        '''
     273
    308274        points = [[1.0, 2.1], [3.0, 5.3]]
    309         attributes = {'depth':[2, 4], 'elevation':[6.1, 5]}
    310         attributes1 = {'depth':[2, 4], 'elevation':[2.5, 1]}
    311         G1 = Geospatial_data(points, attributes)       
    312         G2 = Geospatial_data(points, attributes1)
    313        
    314 #        g3 = geospatial_data2points_dictionary(G1)
    315 #        print 'g3=', g3
    316        
     275        attributes = {'depth': [2, 4], 'elevation': [6.1, 5]}
     276        attributes1 = {'depth': [2, 4], 'elevation': [2.5, 1]}
     277        G1 = Geospatial_data(points, attributes)
     278        G2 = Geospatial_data(points, attributes1)
     279
    317280        G = G1 + G2
    318281
     
    325288
    326289    def test_addII(self):
    327         """ test the addition of two geospatical objects
    328             no geo_reference see next test
    329         """
     290        '''test the addition of two geospatical objects
     291        no geo_reference see next test
     292        '''
     293
    330294        points = [[1.0, 2.1], [3.0, 5.3]]
    331         attributes = {'depth':[2, 4]}
    332         G1 = Geospatial_data(points, attributes) 
    333        
     295        attributes = {'depth': [2, 4]}
     296        G1 = Geospatial_data(points, attributes)
     297
    334298        points = [[5.0, 2.1], [3.0, 50.3]]
    335         attributes = {'depth':[200, 400]}
     299        attributes = {'depth': [200, 400]}
    336300        G2 = Geospatial_data(points, attributes)
    337        
    338 #        g3 = geospatial_data2points_dictionary(G1)
    339 #        print 'g3=', g3
    340        
     301
    341302        G = G1 + G2
    342303
    343         assert G.attributes.has_key('depth') 
     304        assert G.attributes.has_key('depth')
    344305        assert G.attributes.keys(), ['depth']
    345306        assert num.allclose(G.attributes['depth'], [2, 4, 200, 400])
    346307        assert num.allclose(G.get_data_points(), [[1.0, 2.1], [3.0, 5.3],
    347308                                                  [5.0, 2.1], [3.0, 50.3]])
     309
    348310    def test_add_with_geo (self):
    349         """
    350         Difference in Geo_reference resolved
    351         """
     311        '''Difference in Geo_reference resolved'''
     312
    352313        points1 = [[1.0, 2.1], [3.0, 5.3]]
    353314        points2 = [[5.0, 6.1], [6.0, 3.3]]
     
    361322                                 projection='UTM',
    362323                                 units='m')
    363                                
     324
    364325        G1 = Geospatial_data(points1, attributes1, geo_ref1)
    365326        G2 = Geospatial_data(points2, attributes2, geo_ref2)
     
    370331
    371332        P2 = G2.get_data_points(absolute=True)
    372         assert num.allclose(P2, [[5.1, 9.1], [6.1, 6.3]])       
    373        
     333        assert num.allclose(P2, [[5.1, 9.1], [6.1, 6.3]])
     334
    374335        G = G1 + G2
    375336
     
    380341        P = G.get_data_points(absolute=True)
    381342
    382         #P_relative = G.get_data_points(absolute=False)
    383         #
    384         #assert allclose(P_relative, P - [0.1, 2.0])
    385 
    386         assert num.allclose(P, num.concatenate( (P1,P2) ))
     343        assert num.allclose(P, num.concatenate((P1,P2), axis=0))    #??default#
     344        msg = ('P=\n%s\nshould be close to\n'
     345               '[[2.0, 4.1], [4.0, 7.3],\n'
     346               ' [5.1, 9.1], [6.1, 6.3]]'
     347               % str(P))
    387348        assert num.allclose(P, [[2.0, 4.1], [4.0, 7.3],
    388                                 [5.1, 9.1], [6.1, 6.3]])
    389        
    390 
    391 
    392        
     349                                [5.1, 9.1], [6.1, 6.3]]), msg
    393350
    394351    def test_add_with_geo_absolute (self):
    395         """
    396         Difference in Geo_reference resolved
    397         """
     352        '''Difference in Geo_reference resolved'''
     353
    398354        points1 = num.array([[2.0, 4.1], [4.0, 7.3]])
    399         points2 = num.array([[5.1, 9.1], [6.1, 6.3]])       
     355        points2 = num.array([[5.1, 9.1], [6.1, 6.3]])
    400356        attributes1 = [2, 4]
    401357        attributes2 = [5, 76]
     
    403359        geo_ref2 = Geo_reference(55, 2.0, 3.0)
    404360
    405        
    406                                
    407         G1 = Geospatial_data(points1 - [geo_ref1.get_xllcorner(), geo_ref1.get_yllcorner()],
     361        G1 = Geospatial_data(points1 - [geo_ref1.get_xllcorner(),
     362                                        geo_ref1.get_yllcorner()],
    408363                             attributes1, geo_ref1)
    409        
    410         G2 = Geospatial_data(points2 - [geo_ref2.get_xllcorner(), geo_ref2.get_yllcorner()],
     364
     365        G2 = Geospatial_data(points2 - [geo_ref2.get_xllcorner(),
     366                                        geo_ref2.get_yllcorner()],
    411367                             attributes2, geo_ref2)
    412368
     
    416372
    417373        P1 = G1.get_data_points(absolute=False)
    418         assert num.allclose(P1, points1 - [geo_ref1.get_xllcorner(), geo_ref1.get_yllcorner()])       
     374        msg = ('P1=\n%s\nbut points1 - <...>=\n%s'
     375               % (str(P1),
     376                  str(points1 - [geo_ref1.get_xllcorner(),
     377                                 geo_ref1.get_yllcorner()])))
     378        assert num.allclose(P1, points1 - [geo_ref1.get_xllcorner(),
     379                                           geo_ref1.get_yllcorner()]), msg
    419380
    420381        P2 = G2.get_data_points(absolute=True)
     
    422383
    423384        P2 = G2.get_data_points(absolute=False)
    424         assert num.allclose(P2, points2 - [geo_ref2.get_xllcorner(), geo_ref2.get_yllcorner()])               
    425        
     385        assert num.allclose(P2, points2 - [geo_ref2.get_xllcorner(),
     386                                           geo_ref2.get_yllcorner()])
     387
    426388        G = G1 + G2
    427        
    428         #assert allclose(G.get_geo_reference().get_xllcorner(), 1.0)
    429         #assert allclose(G.get_geo_reference().get_yllcorner(), 2.0)
    430 
    431389        P = G.get_data_points(absolute=True)
    432390
    433         #P_relative = G.get_data_points(absolute=False)
    434         #
    435         #assert allclose(P_relative, [[1.0, 2.1], [3.0, 5.3], [4.1, 7.1], [5.1, 4.3]])
    436 
    437         assert num.allclose(P, num.concatenate( (points1,points2) ))
    438 
     391        assert num.allclose(P, num.concatenate((points1, points2), axis=0)) #??default#
    439392
    440393    def test_add_with_None(self):
    441         """ test that None can be added to a geospatical objects
    442         """
    443        
     394        '''test that None can be added to a geospatical objects'''
     395
    444396        points1 = num.array([[2.0, 4.1], [4.0, 7.3]])
    445         points2 = num.array([[5.1, 9.1], [6.1, 6.3]])       
     397        points2 = num.array([[5.1, 9.1], [6.1, 6.3]])
    446398
    447399        geo_ref1= Geo_reference(55, 1.0, 2.0)
     
    452404                                 projection='UTM',
    453405                                 units='m')
    454        
    455 
    456         attributes1 = {'depth':[2, 4.7], 'elevation':[6.1, 5]}
    457         attributes2 = {'depth':[-2.3, 4], 'elevation':[2.5, 1]}
    458 
     406
     407        attributes1 = {'depth': [2, 4.7], 'elevation': [6.1, 5]}
     408        attributes2 = {'depth': [-2.3, 4], 'elevation': [2.5, 1]}
    459409
    460410        G1 = Geospatial_data(points1, attributes1, geo_ref1)
     
    464414        assert G1.attributes.has_key('elevation')
    465415        assert num.allclose(G1.attributes['depth'], [2, 4.7])
    466         assert num.allclose(G1.attributes['elevation'], [6.1, 5])       
    467        
     416        assert num.allclose(G1.attributes['elevation'], [6.1, 5])
     417
    468418        G2 = Geospatial_data(points2, attributes2, geo_ref2)
    469419        assert num.allclose(G2.get_geo_reference().get_xllcorner(), 0.1)
     
    472422        assert G2.attributes.has_key('elevation')
    473423        assert num.allclose(G2.attributes['depth'], [-2.3, 4])
    474         assert num.allclose(G2.attributes['elevation'], [2.5, 1])       
     424        assert num.allclose(G2.attributes['elevation'], [2.5, 1])
    475425
    476426        #Check that absolute values are as expected
     
    479429
    480430        P2 = G2.get_data_points(absolute=True)
    481         assert num.allclose(P2, [[5.2, 12.1], [6.2, 9.3]])       
     431        assert num.allclose(P2, [[5.2, 12.1], [6.2, 9.3]])
    482432
    483433        # Normal add
    484434        G = G1 + None
    485 
    486435        assert G.attributes.has_key('depth')
    487436        assert G.attributes.has_key('elevation')
    488437        assert num.allclose(G.attributes['depth'], [2, 4.7])
    489         assert num.allclose(G.attributes['elevation'], [6.1, 5])       
     438        assert num.allclose(G.attributes['elevation'], [6.1, 5])
    490439
    491440        # Points are now absolute.
    492441        assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0)
    493442        assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0)
    494         P = G.get_data_points(absolute=True)       
    495         assert num.allclose(P, [[3.0, 6.1], [5.0, 9.3]])
    496 
     443        P = G.get_data_points(absolute=True)
     444        msg = 'P=\n%s' % str(P)
     445        assert num.allclose(P, [[3.0, 6.1], [5.0, 9.3]]), msg
    497446
    498447        G = G2 + None
     
    500449        assert G.attributes.has_key('elevation')
    501450        assert num.allclose(G.attributes['depth'], [-2.3, 4])
    502         assert num.allclose(G.attributes['elevation'], [2.5, 1])       
    503 
     451        assert num.allclose(G.attributes['elevation'], [2.5, 1])
    504452        assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0)
    505453        assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0)
    506         P = G.get_data_points(absolute=True)       
     454
     455        P = G.get_data_points(absolute=True)
    507456        assert num.allclose(P, [[5.2, 12.1], [6.2, 9.3]])
    508        
    509 
    510457
    511458        # Reverse add
    512459        G = None + G1
    513 
    514460        assert G.attributes.has_key('depth')
    515461        assert G.attributes.has_key('elevation')
    516462        assert num.allclose(G.attributes['depth'], [2, 4.7])
    517         assert num.allclose(G.attributes['elevation'], [6.1, 5])       
     463        assert num.allclose(G.attributes['elevation'], [6.1, 5])
    518464
    519465        # Points are now absolute.
    520466        assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0)
    521467        assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0)
    522         P = G.get_data_points(absolute=True)       
    523         assert num.allclose(P, [[3.0, 6.1], [5.0, 9.3]])       
    524 
     468
     469        P = G.get_data_points(absolute=True)
     470        assert num.allclose(P, [[3.0, 6.1], [5.0, 9.3]])
    525471
    526472        G = None + G2
     
    528474        assert G.attributes.has_key('elevation')
    529475        assert num.allclose(G.attributes['depth'], [-2.3, 4])
    530         assert num.allclose(G.attributes['elevation'], [2.5, 1])       
     476        assert num.allclose(G.attributes['elevation'], [2.5, 1])
    531477
    532478        assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0)
    533479        assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0)
    534         P = G.get_data_points(absolute=True)       
     480
     481        P = G.get_data_points(absolute=True)
    535482        assert num.allclose(P, [[5.2, 12.1], [6.2, 9.3]])
    536483
    537        
    538 
    539        
    540                            
    541        
    542484    def test_clip0(self):
    543         """test_clip0(self):
    544        
     485        '''test_clip0(self):
     486
    545487        Test that point sets can be clipped by a polygon
    546         """
    547        
     488        '''
     489
    548490        from anuga.coordinate_transforms.geo_reference import Geo_reference
    549        
     491
    550492        points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3],
    551493                  [0, 0], [2.4, 3.3]]
    552494        G = Geospatial_data(points)
    553495
    554         # First try the unit square   
    555         U = [[0,0], [1,0], [1,1], [0,1]]
    556         assert num.allclose(G.clip(U).get_data_points(), [[0.2, 0.5], [0.4, 0.3], [0, 0]])
     496        # First try the unit square
     497        U = [[0,0], [1,0], [1,1], [0,1]]
     498        assert num.allclose(G.clip(U).get_data_points(),
     499                            [[0.2, 0.5], [0.4, 0.3], [0, 0]])
    557500
    558501        # Then a more complex polygon
    559502        polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    560         points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
     503        points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0],
     504                  [0.5, 1.5], [0.5, -0.5]]
    561505        G = Geospatial_data(points)
    562506
    563507        assert num.allclose(G.clip(polygon).get_data_points(),
    564                         [[0.5, 0.5], [1, -0.5], [1.5, 0]])
     508                            [[0.5, 0.5], [1, -0.5], [1.5, 0]])
    565509
    566510    def test_clip0_with_attributes(self):
    567         """test_clip0_with_attributes(self):
    568        
     511        '''test_clip0_with_attributes(self):
     512
    569513        Test that point sets with attributes can be clipped by a polygon
    570         """
    571        
     514        '''
     515
    572516        from anuga.coordinate_transforms.geo_reference import Geo_reference
    573        
     517
    574518        points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3],
    575519                  [0, 0], [2.4, 3.3]]
     
    578522        att_dict = {'att1': attributes,
    579523                    'att2': num.array(attributes)+1}
    580        
     524
    581525        G = Geospatial_data(points, att_dict)
    582526
    583         # First try the unit square   
    584         U = [[0,0], [1,0], [1,1], [0,1]]
    585         assert num.allclose(G.clip(U).get_data_points(), [[0.2, 0.5], [0.4, 0.3], [0, 0]])
     527        # First try the unit square
     528        U = [[0,0], [1,0], [1,1], [0,1]]
     529        assert num.allclose(G.clip(U).get_data_points(),
     530                            [[0.2, 0.5], [0.4, 0.3], [0, 0]])
    586531        assert num.allclose(G.clip(U).get_attributes('att1'), [-4, 76, 0.1])
    587         assert num.allclose(G.clip(U).get_attributes('att2'), [-3, 77, 1.1])               
     532        assert num.allclose(G.clip(U).get_attributes('att2'), [-3, 77, 1.1])
    588533
    589534        # Then a more complex polygon
    590535        polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    591         points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
     536        points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0],
     537                  [0.5, 1.5], [0.5, -0.5]]
    592538
    593539        # This time just one attribute
    594540        attributes = [2, -4, 5, 76, -2, 0.1]
    595541        G = Geospatial_data(points, attributes)
    596 
    597542        assert num.allclose(G.clip(polygon).get_data_points(),
    598                         [[0.5, 0.5], [1, -0.5], [1.5, 0]])
     543                            [[0.5, 0.5], [1, -0.5], [1.5, 0]])
    599544        assert num.allclose(G.clip(polygon).get_attributes(), [-4, 5, 76])
    600        
    601545
    602546    def test_clip1(self):
    603         """test_clip1(self):
    604        
     547        '''test_clip1(self):
     548
    605549        Test that point sets can be clipped by a polygon given as
    606550        another Geospatial dataset
    607         """
    608        
     551        '''
     552
    609553        from anuga.coordinate_transforms.geo_reference import Geo_reference
    610        
     554
    611555        points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3],
    612556                  [0, 0], [2.4, 3.3]]
     
    615559                    'att2': num.array(attributes)+1}
    616560        G = Geospatial_data(points, att_dict)
    617        
    618         # First try the unit square   
    619         U = Geospatial_data([[0,0], [1,0], [1,1], [0,1]]) 
     561
     562        # First try the unit square
     563        U = Geospatial_data([[0,0], [1,0], [1,1], [0,1]])
    620564        assert num.allclose(G.clip(U).get_data_points(),
    621                         [[0.2, 0.5], [0.4, 0.3], [0, 0]])
    622 
     565                            [[0.2, 0.5], [0.4, 0.3], [0, 0]])
    623566        assert num.allclose(G.clip(U).get_attributes('att1'), [-4, 76, 0.1])
    624         assert num.allclose(G.clip(U).get_attributes('att2'), [-3, 77, 1.1])                       
    625        
     567        assert num.allclose(G.clip(U).get_attributes('att2'), [-3, 77, 1.1])
     568
    626569        # Then a more complex polygon
    627         points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
    628         attributes = [2, -4, 5, 76, -2, 0.1]       
     570        points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0],
     571                  [0.5, 1.5], [0.5, -0.5]]
     572        attributes = [2, -4, 5, 76, -2, 0.1]
    629573        G = Geospatial_data(points, attributes)
    630         polygon = Geospatial_data([[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]])
    631        
     574        polygon = Geospatial_data([[0,0], [1,0], [0.5,-1], [2, -1],
     575                                   [2,1], [0,1]])
    632576
    633577        assert num.allclose(G.clip(polygon).get_data_points(),
    634578                            [[0.5, 0.5], [1, -0.5], [1.5, 0]])
    635579        assert num.allclose(G.clip(polygon).get_attributes(), [-4, 5, 76])
    636        
    637580
    638581    def test_clip0_outside(self):
    639         """test_clip0_outside(self):
    640        
     582        '''test_clip0_outside(self):
     583
    641584        Test that point sets can be clipped outside of a polygon
    642         """
    643        
     585        '''
     586
    644587        from anuga.coordinate_transforms.geo_reference import Geo_reference
    645        
     588
    646589        points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3],
    647590                  [0, 0], [2.4, 3.3]]
    648         attributes = [2, -4, 5, 76, -2, 0.1, 3]       
     591        attributes = [2, -4, 5, 76, -2, 0.1, 3]
    649592        G = Geospatial_data(points, attributes)
    650593
    651         # First try the unit square   
     594        # First try the unit square
    652595        U = [[0,0], [1,0], [1,1], [0,1]]
    653596        assert num.allclose(G.clip_outside(U).get_data_points(),
    654597                            [[-1, 4], [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]])
    655         #print G.clip_outside(U).get_attributes()
    656         assert num.allclose(G.clip_outside(U).get_attributes(), [2, 5, -2, 3])       
    657        
     598        assert num.allclose(G.clip_outside(U).get_attributes(), [2, 5, -2, 3])
    658599
    659600        # Then a more complex polygon
    660601        polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    661         points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
    662         attributes = [2, -4, 5, 76, -2, 0.1]       
     602        points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0],
     603                  [0.5, 1.5], [0.5, -0.5]]
     604        attributes = [2, -4, 5, 76, -2, 0.1]
    663605        G = Geospatial_data(points, attributes)
    664 
    665606        assert num.allclose(G.clip_outside(polygon).get_data_points(),
    666607                            [[0.5, 1.4], [0.5, 1.5], [0.5, -0.5]])
    667         assert num.allclose(G.clip_outside(polygon).get_attributes(), [2, -2, 0.1])               
    668 
     608        assert num.allclose(G.clip_outside(polygon).get_attributes(),
     609                            [2, -2, 0.1])
    669610
    670611    def test_clip1_outside(self):
    671         """test_clip1_outside(self):
    672        
     612        '''test_clip1_outside(self):
     613
    673614        Test that point sets can be clipped outside of a polygon given as
    674615        another Geospatial dataset
    675         """
    676        
     616        '''
     617
    677618        from anuga.coordinate_transforms.geo_reference import Geo_reference
    678        
     619
    679620        points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3],
    680621                  [0, 0], [2.4, 3.3]]
    681         attributes = [2, -4, 5, 76, -2, 0.1, 3]       
    682         G = Geospatial_data(points, attributes)       
    683 
    684         # First try the unit square   
    685         U = Geospatial_data([[0,0], [1,0], [1,1], [0,1]]) 
     622        attributes = [2, -4, 5, 76, -2, 0.1, 3]
     623        G = Geospatial_data(points, attributes)
     624
     625        # First try the unit square
     626        U = Geospatial_data([[0,0], [1,0], [1,1], [0,1]])
    686627        assert num.allclose(G.clip_outside(U).get_data_points(),
    687628                            [[-1, 4], [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]])
    688         assert num.allclose(G.clip(U).get_attributes(), [-4, 76, 0.1])       
     629        assert num.allclose(G.clip(U).get_attributes(), [-4, 76, 0.1])
    689630
    690631        # Then a more complex polygon
    691         points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
    692         attributes = [2, -4, 5, 76, -2, 0.1]       
     632        points = [[0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0],
     633                  [0.5, 1.5], [0.5, -0.5]]
     634        attributes = [2, -4, 5, 76, -2, 0.1]
    693635        G = Geospatial_data(points, attributes)
    694 
    695         polygon = Geospatial_data([[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]])
    696        
    697 
     636        polygon = Geospatial_data([[0, 0], [1, 0], [0.5, -1], [2, -1],
     637                                   [2, 1], [0, 1]])
    698638        assert num.allclose(G.clip_outside(polygon).get_data_points(),
    699639                            [[0.5, 1.4], [0.5, 1.5], [0.5, -0.5]])
    700         assert num.allclose(G.clip_outside(polygon).get_attributes(), [2, -2, 0.1])
    701        
    702 
     640        assert num.allclose(G.clip_outside(polygon).get_attributes(),
     641                            [2, -2, 0.1])
    703642
    704643    def test_clip1_inside_outside(self):
    705         """test_clip1_inside_outside(self):
    706        
     644        '''test_clip1_inside_outside(self):
     645
    707646        Test that point sets can be clipped outside of a polygon given as
    708647        another Geospatial dataset
    709         """
    710        
     648        '''
     649
    711650        from anuga.coordinate_transforms.geo_reference import Geo_reference
    712        
     651
    713652        points = [[-1, 4], [0.2, 0.5], [1.0, 2.1], [0.4, 0.3], [3.0, 5.3],
    714653                  [0, 0], [2.4, 3.3]]
    715         attributes = [2, -4, 5, 76, -2, 0.1, 3]       
     654        attributes = [2, -4, 5, 76, -2, 0.1, 3]
    716655        G = Geospatial_data(points, attributes)
    717656
    718         # First try the unit square   
    719         U = Geospatial_data([[0,0], [1,0], [1,1], [0,1]]) 
     657        # First try the unit square
     658        U = Geospatial_data([[0,0], [1,0], [1,1], [0,1]])
    720659        G1 = G.clip(U)
    721         assert num.allclose(G1.get_data_points(),[[0.2, 0.5], [0.4, 0.3], [0, 0]])
     660        assert num.allclose(G1.get_data_points(),
     661                            [[0.2, 0.5], [0.4, 0.3], [0, 0]])
    722662        assert num.allclose(G.clip(U).get_attributes(), [-4, 76, 0.1])
    723        
    724663        G2 = G.clip_outside(U)
    725664        assert num.allclose(G2.get_data_points(),[[-1, 4], [1.0, 2.1],
    726665                                                  [3.0, 5.3], [2.4, 3.3]])
    727         assert num.allclose(G.clip_outside(U).get_attributes(), [2, 5, -2, 3])               
    728 
    729        
     666        assert num.allclose(G.clip_outside(U).get_attributes(), [2, 5, -2, 3])
     667
    730668        # New ordering
    731         new_points = [[0.2, 0.5], [0.4, 0.3], [0, 0]] +\
    732                      [[-1, 4], [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]]
    733 
    734         new_attributes = [-4, 76, 0.1, 2, 5, -2, 3]                 
    735        
     669        new_points = [[0.2, 0.5], [0.4, 0.3], [0, 0], [-1, 4],
     670                      [1.0, 2.1], [3.0, 5.3], [2.4, 3.3]]
     671        new_attributes = [-4, 76, 0.1, 2, 5, -2, 3]
     672
    736673        assert num.allclose((G1+G2).get_data_points(), new_points)
    737674        assert num.allclose((G1+G2).get_attributes(), new_attributes)
     
    741678        G.export_points_file(FN)
    742679
    743 
    744680        # Read it back in
    745681        G3 = Geospatial_data(FN)
    746682
    747 
    748683        # Check result
    749         assert num.allclose(G3.get_data_points(), new_points)       
    750         assert num.allclose(G3.get_attributes(), new_attributes)       
    751        
     684        assert num.allclose(G3.get_data_points(), new_points)
     685        assert num.allclose(G3.get_attributes(), new_attributes)
     686
    752687        os.remove(FN)
    753688
    754        
    755689    def test_load_csv(self):
    756        
    757         import os
    758         import tempfile
    759        
    760         fileName = tempfile.mktemp(".csv")
    761         file = open(fileName,"w")
    762         file.write("x,y,elevation speed \n\
     690        fileName = tempfile.mktemp('.csv')
     691        file = open(fileName,'w')
     692        file.write('x,y,elevation speed \n\
    7636931.0 0.0 10.0 0.0\n\
    7646940.0 1.0 0.0 10.0\n\
    765 1.0 0.0 10.4 40.0\n")
     6951.0 0.0 10.4 40.0\n')
    766696        file.close()
    767         #print fileName
     697
    768698        results = Geospatial_data(fileName)
    769699        os.remove(fileName)
    770 #        print 'data', results.get_data_points()
    771         assert num.allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],
    772                                                         [1.0, 0.0]])
     700        assert num.allclose(results.get_data_points(),
     701                            [[1.0, 0.0],[0.0, 1.0], [1.0, 0.0]])
    773702        assert num.allclose(results.get_attributes(attribute_name='elevation'),
    774703                            [10.0, 0.0, 10.4])
     
    776705                            [0.0, 10.0, 40.0])
    777706
    778 
    779   ###################### .CSV ##############################
     707################################################################################
     708# Test CSV files
     709################################################################################
    780710
    781711    def test_load_csv_lat_long_bad_blocking(self):
    782         """
    783         test_load_csv_lat_long_bad_blocking(self):
     712        '''test_load_csv_lat_long_bad_blocking(self):
    784713        Different zones in Geo references
    785         """
    786         fileName = tempfile.mktemp(".csv")
    787         file = open(fileName,"w")
    788         file.write("Lati,LONG,z \n\
     714        '''
     715
     716        fileName = tempfile.mktemp('.csv')
     717        file = open(fileName, 'w')
     718        file.write('Lati,LONG,z \n\
    789719-25.0,180.0,452.688000\n\
    790 -34,150.0,459.126000\n")
     720-34,150.0,459.126000\n')
    791721        file.close()
    792        
     722
    793723        results = Geospatial_data(fileName, max_read_lines=1,
    794724                                  load_file_now=False)
    795        
    796         #for i in results:
    797         #    pass
     725
    798726        try:
    799727            for i in results:
     
    803731        else:
    804732            msg = 'Different zones in Geo references not caught.'
    805             raise msg       
    806        
    807         os.remove(fileName)
    808        
     733            raise Exception, msg
     734
     735        os.remove(fileName)
     736
    809737    def test_load_csv(self):
    810        
    811         fileName = tempfile.mktemp(".txt")
    812         file = open(fileName,"w")
    813         file.write(" x,y, elevation ,  speed \n\
    814 1.0, 0.0, 10.0, 0.0\n\
    815 0.0, 1.0, 0.0, 10.0\n\
    816 1.0, 0.0 ,10.4, 40.0\n")
     738        fileName = tempfile.mktemp('.txt')
     739        file = open(fileName, 'w')
     740        file.write(' x,y, elevation ,  speed \n\
     7411.0, 0.0, 10.0, 0.0\n\
     7420.0, 1.0, 0.0, 10.0\n\
     7431.0, 0.0 ,10.4, 40.0\n')
    817744        file.close()
    818745
    819746        results = Geospatial_data(fileName, max_read_lines=2)
    820747
    821 
    822         assert num.allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    823         assert num.allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])
    824         assert num.allclose(results.get_attributes(attribute_name='speed'), [0.0, 10.0, 40.0])
     748        assert num.allclose(results.get_data_points(),
     749                            [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     750        assert num.allclose(results.get_attributes(attribute_name='elevation'),
     751                            [10.0, 0.0, 10.4])
     752        assert num.allclose(results.get_attributes(attribute_name='speed'),
     753                            [0.0, 10.0, 40.0])
    825754
    826755        # Blocking
     
    828757        for i in results:
    829758            geo_list.append(i)
    830            
     759
    831760        assert num.allclose(geo_list[0].get_data_points(),
    832761                            [[1.0, 0.0],[0.0, 1.0]])
    833 
    834762        assert num.allclose(geo_list[0].get_attributes(attribute_name='elevation'),
    835763                            [10.0, 0.0])
    836764        assert num.allclose(geo_list[1].get_data_points(),
    837                             [[1.0, 0.0]])       
     765                            [[1.0, 0.0]])
    838766        assert num.allclose(geo_list[1].get_attributes(attribute_name='elevation'),
    839767                            [10.4])
    840            
    841         os.remove(fileName)         
    842        
     768
     769        os.remove(fileName)
     770
    843771    def test_load_csv_bad(self):
    844         """ test_load_csv_bad(self):
     772        '''test_load_csv_bad(self):
    845773        header column, body column missmatch
    846774        (Format error)
    847         """
    848         import os
    849        
    850         fileName = tempfile.mktemp(".txt")
    851         file = open(fileName,"w")
    852         file.write(" elevation ,  speed \n\
    853 1.0, 0.0, 10.0, 0.0\n\
    854 0.0, 1.0, 0.0, 10.0\n\
    855 1.0, 0.0 ,10.4, 40.0\n")
     775        '''
     776
     777        fileName = tempfile.mktemp('.txt')
     778        file = open(fileName, 'w')
     779        file.write(' elevation ,  speed \n\
     7801.0, 0.0, 10.0, 0.0\n\
     7810.0, 1.0, 0.0, 10.0\n\
     7821.0, 0.0 ,10.4, 40.0\n')
    856783        file.close()
    857784
     
    861788        # Blocking
    862789        geo_list = []
    863         #for i in results:
    864         #    geo_list.append(i)
    865790        try:
    866791            for i in results:
     
    869794            pass
    870795        else:
    871             msg = 'bad file did not raise error!'
    872             raise msg
     796            msg = 'Bad file did not raise error!'
     797            raise Exception, msg
    873798        os.remove(fileName)
    874799
    875800    def test_load_csv_badII(self):
    876         """ test_load_csv_bad(self):
     801        '''test_load_csv_bad(self):
    877802        header column, body column missmatch
    878803        (Format error)
    879         """
    880         import os
    881        
    882         fileName = tempfile.mktemp(".txt")
    883         file = open(fileName,"w")
    884         file.write(" x,y,elevation ,  speed \n\
     804        '''
     805
     806        fileName = tempfile.mktemp('.txt')
     807        file = open(fileName, 'w')
     808        file.write(' x,y,elevation ,  speed \n\
    8858091.0, 0.0, 10.0, 0.0\n\
    8868100.0, 1.0, 0.0, 10\n\
    887 1.0, 0.0 ,10.4 yeah\n")
     8111.0, 0.0 ,10.4 yeah\n')
    888812        file.close()
    889813
     
    893817        # Blocking
    894818        geo_list = []
    895         #for i in results:
    896         #    geo_list.append(i)
    897819        try:
    898820            for i in results:
     
    901823            pass
    902824        else:
    903             msg = 'bad file did not raise error!'
    904             raise msg
     825            msg = 'Bad file did not raise error!'
     826            raise Exception, msg
    905827        os.remove(fileName)
    906828
    907829    def test_load_csv_badIII(self):
    908         """ test_load_csv_bad(self):
     830        '''test_load_csv_bad(self):
    909831        space delimited
    910         """
    911         import os
    912        
    913         fileName = tempfile.mktemp(".txt")
    914         file = open(fileName,"w")
    915         file.write(" x y elevation   speed \n\
     832        '''
     833
     834        fileName = tempfile.mktemp('.txt')
     835        file = open(fileName, 'w')
     836        file.write(' x y elevation   speed \n\
    9168371. 0.0 10.0 0.0\n\
    9178380.0 1.0 0.0 10.0\n\
    918 1.0 0.0 10.4 40.0\n")
     8391.0 0.0 10.4 40.0\n')
    919840        file.close()
    920841
     
    925846            pass
    926847        else:
    927             msg = 'bad file did not raise error!'
    928             raise msg
    929         os.remove(fileName)
    930        
     848            msg = 'Bad file did not raise error!'
     849            raise Exception, msg
     850        os.remove(fileName)
     851
    931852    def test_load_csv_badIV(self):
    932         """ test_load_csv_bad(self):
     853        ''' test_load_csv_bad(self):
    933854        header column, body column missmatch
    934855        (Format error)
    935         """
    936         import os
    937        
    938         fileName = tempfile.mktemp(".txt")
    939         file = open(fileName,"w")
    940         file.write(" x,y,elevation ,  speed \n\
     856        '''
     857
     858        fileName = tempfile.mktemp('.txt')
     859        file = open(fileName, 'w')
     860        file.write(' x,y,elevation ,  speed \n\
    9418611.0, 0.0, 10.0, wow\n\
    9428620.0, 1.0, 0.0, ha\n\
    943 1.0, 0.0 ,10.4, yeah\n")
     8631.0, 0.0 ,10.4, yeah\n')
    944864        file.close()
    945865
     
    949869        # Blocking
    950870        geo_list = []
    951         #for i in results:
    952          #   geo_list.append(i)
    953871        try:
    954872            for i in results:
     
    957875            pass
    958876        else:
    959             msg = 'bad file did not raise error!'
    960             raise msg
     877            msg = 'Bad file did not raise error!'
     878            raise Exception, msg
    961879        os.remove(fileName)
    962880
    963881    def test_load_pts_blocking(self):
    964882        #This is pts!
    965        
    966         import os
    967        
    968         fileName = tempfile.mktemp(".txt")
    969         file = open(fileName,"w")
    970         file.write(" x,y, elevation ,  speed \n\
    971 1.0, 0.0, 10.0, 0.0\n\
    972 0.0, 1.0, 0.0, 10.0\n\
    973 1.0, 0.0 ,10.4, 40.0\n")
     883        fileName = tempfile.mktemp('.txt')
     884        file = open(fileName, 'w')
     885        file.write(' x,y, elevation ,  speed \n\
     8861.0, 0.0, 10.0, 0.0\n\
     8870.0, 1.0, 0.0, 10.0\n\
     8881.0, 0.0 ,10.4, 40.0\n')
    974889        file.close()
    975890
    976         pts_file = tempfile.mktemp(".pts")
    977        
     891        pts_file = tempfile.mktemp('.pts')
     892
    978893        convert = Geospatial_data(fileName)
    979894        convert.export_points_file(pts_file)
    980895        results = Geospatial_data(pts_file, max_read_lines=2)
    981896
    982         assert num.allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],
    983                                                         [1.0, 0.0]])
     897        assert num.allclose(results.get_data_points(),
     898                            [[1.0, 0.0],[0.0, 1.0], [1.0, 0.0]])
    984899        assert num.allclose(results.get_attributes(attribute_name='elevation'),
    985900                            [10.0, 0.0, 10.4])
     
    990905        geo_list = []
    991906        for i in results:
    992             geo_list.append(i) 
     907            geo_list.append(i)
    993908        assert num.allclose(geo_list[0].get_data_points(),
    994909                            [[1.0, 0.0],[0.0, 1.0]])
     
    996911                            [10.0, 0.0])
    997912        assert num.allclose(geo_list[1].get_data_points(),
    998                             [[1.0, 0.0]])       
     913                            [[1.0, 0.0]])
    999914        assert num.allclose(geo_list[1].get_attributes(attribute_name='elevation'),
    1000915                            [10.4])
    1001            
    1002         os.remove(fileName) 
    1003         os.remove(pts_file)               
     916
     917        os.remove(fileName)
     918        os.remove(pts_file)
    1004919
    1005920    def verbose_test_load_pts_blocking(self):
    1006        
    1007         import os
    1008        
    1009         fileName = tempfile.mktemp(".txt")
    1010         file = open(fileName,"w")
    1011         file.write(" x,y, elevation ,  speed \n\
    1012 1.0, 0.0, 10.0, 0.0\n\
    1013 0.0, 1.0, 0.0, 10.0\n\
    1014 1.0, 0.0, 10.0, 0.0\n\
    1015 0.0, 1.0, 0.0, 10.0\n\
    1016 1.0, 0.0, 10.0, 0.0\n\
    1017 0.0, 1.0, 0.0, 10.0\n\
    1018 1.0, 0.0, 10.0, 0.0\n\
    1019 0.0, 1.0, 0.0, 10.0\n\
    1020 1.0, 0.0, 10.0, 0.0\n\
    1021 0.0, 1.0, 0.0, 10.0\n\
    1022 1.0, 0.0, 10.0, 0.0\n\
    1023 0.0, 1.0, 0.0, 10.0\n\
    1024 1.0, 0.0, 10.0, 0.0\n\
    1025 0.0, 1.0, 0.0, 10.0\n\
    1026 1.0, 0.0, 10.0, 0.0\n\
    1027 0.0, 1.0, 0.0, 10.0\n\
    1028 1.0, 0.0, 10.0, 0.0\n\
    1029 0.0, 1.0, 0.0, 10.0\n\
    1030 1.0, 0.0, 10.0, 0.0\n\
    1031 0.0, 1.0, 0.0, 10.0\n\
    1032 1.0, 0.0, 10.0, 0.0\n\
    1033 0.0, 1.0, 0.0, 10.0\n\
    1034 1.0, 0.0, 10.0, 0.0\n\
    1035 0.0, 1.0, 0.0, 10.0\n\
    1036 1.0, 0.0, 10.0, 0.0\n\
    1037 0.0, 1.0, 0.0, 10.0\n\
    1038 1.0, 0.0, 10.0, 0.0\n\
    1039 0.0, 1.0, 0.0, 10.0\n\
    1040 1.0, 0.0, 10.0, 0.0\n\
    1041 0.0, 1.0, 0.0, 10.0\n\
    1042 1.0, 0.0, 10.0, 0.0\n\
    1043 0.0, 1.0, 0.0, 10.0\n\
    1044 1.0, 0.0, 10.0, 0.0\n\
    1045 0.0, 1.0, 0.0, 10.0\n\
    1046 1.0, 0.0, 10.0, 0.0\n\
    1047 0.0, 1.0, 0.0, 10.0\n\
    1048 1.0, 0.0, 10.0, 0.0\n\
    1049 0.0, 1.0, 0.0, 10.0\n\
    1050 1.0, 0.0, 10.0, 0.0\n\
    1051 0.0, 1.0, 0.0, 10.0\n\
    1052 1.0, 0.0, 10.0, 0.0\n\
    1053 0.0, 1.0, 0.0, 10.0\n\
    1054 1.0, 0.0, 10.0, 0.0\n\
    1055 0.0, 1.0, 0.0, 10.0\n\
    1056 1.0, 0.0, 10.0, 0.0\n\
    1057 0.0, 1.0, 0.0, 10.0\n\
    1058 1.0, 0.0 ,10.4, 40.0\n")
     921        fileName = tempfile.mktemp('.txt')
     922        file = open(fileName, 'w')
     923        file.write(' x,y, elevation ,  speed \n\
     9241.0, 0.0, 10.0, 0.0\n\
     9250.0, 1.0, 0.0, 10.0\n\
     9261.0, 0.0, 10.0, 0.0\n\
     9270.0, 1.0, 0.0, 10.0\n\
     9281.0, 0.0, 10.0, 0.0\n\
     9290.0, 1.0, 0.0, 10.0\n\
     9301.0, 0.0, 10.0, 0.0\n\
     9310.0, 1.0, 0.0, 10.0\n\
     9321.0, 0.0, 10.0, 0.0\n\
     9330.0, 1.0, 0.0, 10.0\n\
     9341.0, 0.0, 10.0, 0.0\n\
     9350.0, 1.0, 0.0, 10.0\n\
     9361.0, 0.0, 10.0, 0.0\n\
     9370.0, 1.0, 0.0, 10.0\n\
     9381.0, 0.0, 10.0, 0.0\n\
     9390.0, 1.0, 0.0, 10.0\n\
     9401.0, 0.0, 10.0, 0.0\n\
     9410.0, 1.0, 0.0, 10.0\n\
     9421.0, 0.0, 10.0, 0.0\n\
     9430.0, 1.0, 0.0, 10.0\n\
     9441.0, 0.0, 10.0, 0.0\n\
     9450.0, 1.0, 0.0, 10.0\n\
     9461.0, 0.0, 10.0, 0.0\n\
     9470.0, 1.0, 0.0, 10.0\n\
     9481.0, 0.0, 10.0, 0.0\n\
     9490.0, 1.0, 0.0, 10.0\n\
     9501.0, 0.0, 10.0, 0.0\n\
     9510.0, 1.0, 0.0, 10.0\n\
     9521.0, 0.0, 10.0, 0.0\n\
     9530.0, 1.0, 0.0, 10.0\n\
     9541.0, 0.0, 10.0, 0.0\n\
     9550.0, 1.0, 0.0, 10.0\n\
     9561.0, 0.0, 10.0, 0.0\n\
     9570.0, 1.0, 0.0, 10.0\n\
     9581.0, 0.0, 10.0, 0.0\n\
     9590.0, 1.0, 0.0, 10.0\n\
     9601.0, 0.0, 10.0, 0.0\n\
     9610.0, 1.0, 0.0, 10.0\n\
     9621.0, 0.0, 10.0, 0.0\n\
     9630.0, 1.0, 0.0, 10.0\n\
     9641.0, 0.0, 10.0, 0.0\n\
     9650.0, 1.0, 0.0, 10.0\n\
     9661.0, 0.0, 10.0, 0.0\n\
     9670.0, 1.0, 0.0, 10.0\n\
     9681.0, 0.0, 10.0, 0.0\n\
     9690.0, 1.0, 0.0, 10.0\n\
     9701.0, 0.0 ,10.4, 40.0\n')
    1059971        file.close()
    1060972
    1061         pts_file = tempfile.mktemp(".pts")
    1062        
     973        pts_file = tempfile.mktemp('.pts')
    1063974        convert = Geospatial_data(fileName)
    1064975        convert.export_points_file(pts_file)
     
    1068979        geo_list = []
    1069980        for i in results:
    1070             geo_list.append(i) 
     981            geo_list.append(i)
    1071982        assert num.allclose(geo_list[0].get_data_points(),
    1072                             [[1.0, 0.0],[0.0, 1.0]])
     983                            [[1.0, 0.0], [0.0, 1.0]])
    1073984        assert num.allclose(geo_list[0].get_attributes(attribute_name='elevation'),
    1074985                            [10.0, 0.0])
    1075986        assert num.allclose(geo_list[1].get_data_points(),
    1076                             [[1.0, 0.0],[0.0, 1.0] ])       
     987                            [[1.0, 0.0],[0.0, 1.0] ])
    1077988        assert num.allclose(geo_list[1].get_attributes(attribute_name='elevation'),
    1078989                            [10.0, 0.0])
    1079            
    1080         os.remove(fileName) 
     990
     991        os.remove(fileName)
    1081992        os.remove(pts_file)
    1082        
    1083        
    1084993
    1085994    def test_new_export_pts_file(self):
     
    1088997        att_dict['elevation'] = num.array([10.1, 0.0, 10.4])
    1089998        att_dict['brightness'] = num.array([10.0, 1.0, 10.4])
    1090        
    1091         fileName = tempfile.mktemp(".pts")
    1092        
     999
     1000        fileName = tempfile.mktemp('.pts')
    10931001        G = Geospatial_data(pointlist, att_dict)
    1094        
    10951002        G.export_points_file(fileName)
    1096 
    10971003        results = Geospatial_data(file_name = fileName)
    1098 
    1099         os.remove(fileName)
    1100        
    1101         assert num.allclose(results.get_data_points(),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    1102         assert num.allclose(results.get_attributes(attribute_name='elevation'), [10.1, 0.0, 10.4])
     1004        os.remove(fileName)
     1005
     1006        assert num.allclose(results.get_data_points(),
     1007                            [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
     1008        assert num.allclose(results.get_attributes(attribute_name='elevation'),
     1009                            [10.1, 0.0, 10.4])
    11031010        answer = [10.0, 1.0, 10.4]
    1104         assert num.allclose(results.get_attributes(attribute_name='brightness'), answer)
     1011        assert num.allclose(results.get_attributes(attribute_name='brightness'),
     1012                            answer)
    11051013
    11061014    def test_new_export_absolute_pts_file(self):
    11071015        att_dict = {}
    1108         pointlist = num.array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1016        pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
    11091017        att_dict['elevation'] = num.array([10.1, 0.0, 10.4])
    11101018        att_dict['brightness'] = num.array([10.0, 1.0, 10.4])
    11111019        geo_ref = Geo_reference(50, 25, 55)
    1112        
    1113         fileName = tempfile.mktemp(".pts")
    1114        
     1020
     1021        fileName = tempfile.mktemp('.pts')
    11151022        G = Geospatial_data(pointlist, att_dict, geo_ref)
    1116        
    11171023        G.export_points_file(fileName, absolute=True)
    1118 
    11191024        results = Geospatial_data(file_name = fileName)
    1120 
    1121         os.remove(fileName)
    1122        
    1123         assert num.allclose(results.get_data_points(), G.get_data_points(True))
    1124         assert num.allclose(results.get_attributes(attribute_name='elevation'), [10.1, 0.0, 10.4])
     1025        os.remove(fileName)
     1026
     1027        msg = ('results.get_data_points()=\n%s\nbut G.get_data_points(True)=\n%s'
     1028               % (str(results.get_data_points()), str(G.get_data_points(True))))
     1029        assert num.allclose(results.get_data_points(),
     1030                            G.get_data_points(True)), msg
     1031        msg = ("results.get_attributes(attribute_name='elevation')=%s"
     1032               % str(results.get_attributes(attribute_name='elevation')))
     1033        assert num.allclose(results.get_attributes(attribute_name='elevation'),
     1034                            [10.1, 0.0, 10.4]), msg
    11251035        answer = [10.0, 1.0, 10.4]
    1126         assert num.allclose(results.get_attributes(attribute_name='brightness'), answer)
     1036        msg = ("results.get_attributes(attribute_name='brightness')=%s, "
     1037               'answer=%s' %
     1038               (str(results.get_attributes(attribute_name='brightness')),
     1039                str(answer)))
     1040        assert num.allclose(results.get_attributes(attribute_name='brightness'),
     1041                            answer), msg
    11271042
    11281043    def test_loadpts(self):
    1129        
    11301044        from Scientific.IO.NetCDF import NetCDFFile
    11311045
    1132         fileName = tempfile.mktemp(".pts")
     1046        fileName = tempfile.mktemp('.pts')
    11331047        # NetCDF file definition
    11341048        outfile = NetCDFFile(fileName, netcdf_mode_w)
    1135        
     1049
    11361050        # dimension definitions
    1137         outfile.createDimension('number_of_points', 3)   
    1138         outfile.createDimension('number_of_dimensions', 2) #This is 2d data
    1139    
     1051        outfile.createDimension('number_of_points', 3)
     1052        outfile.createDimension('number_of_dimensions', 2) # This is 2d data
     1053
    11401054        # variable definitions
    1141         outfile.createVariable('points', num.Float, ('number_of_points',
    1142                                                      'number_of_dimensions'))
    1143         outfile.createVariable('elevation', num.Float, ('number_of_points',))
    1144    
     1055        outfile.createVariable('points', netcdf_float, ('number_of_points',
     1056                                                        'number_of_dimensions'))
     1057        outfile.createVariable('elevation', netcdf_float, ('number_of_points',))
     1058
    11451059        # Get handles to the variables
    11461060        points = outfile.variables['points']
    11471061        elevation = outfile.variables['elevation']
    1148  
     1062
    11491063        points[0, :] = [1.0,0.0]
    1150         elevation[0] = 10.0 
     1064        elevation[0] = 10.0
    11511065        points[1, :] = [0.0,1.0]
    1152         elevation[1] = 0.0 
     1066        elevation[1] = 0.0
    11531067        points[2, :] = [1.0,0.0]
    1154         elevation[2] = 10.4   
     1068        elevation[2] = 10.4
    11551069
    11561070        outfile.close()
    1157        
     1071
    11581072        results = Geospatial_data(file_name = fileName)
    11591073        os.remove(fileName)
    1160         answer =  [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]
    1161         assert num.allclose(results.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    1162         assert num.allclose(results.get_attributes(attribute_name='elevation'), [10.0, 0.0, 10.4])
    1163        
     1074
     1075        answer = [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]]
     1076        assert num.allclose(results.get_data_points(),
     1077                            [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
     1078        assert num.allclose(results.get_attributes(attribute_name='elevation'),
     1079                            [10.0, 0.0, 10.4])
     1080
    11641081    def test_writepts(self):
    1165         #test_writepts: Test that storage of x,y,attributes works
    1166        
     1082        '''Test that storage of x,y,attributes works'''
     1083
    11671084        att_dict = {}
    1168         pointlist = num.array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1085        pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
    11691086        att_dict['elevation'] = num.array([10.0, 0.0, 10.4])
    11701087        att_dict['brightness'] = num.array([10.0, 0.0, 10.4])
    1171         geo_reference=Geo_reference(56,1.9,1.9)
     1088        geo_reference=Geo_reference(56, 1.9, 1.9)
    11721089
    11731090        # Test pts format
    1174         fileName = tempfile.mktemp(".pts")
     1091        fileName = tempfile.mktemp('.pts')
    11751092        G = Geospatial_data(pointlist, att_dict, geo_reference)
    11761093        G.export_points_file(fileName, False)
     
    11781095        os.remove(fileName)
    11791096
    1180         assert num.allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    1181         assert num.allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4])
     1097        assert num.allclose(results.get_data_points(False),
     1098                            [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
     1099        assert num.allclose(results.get_attributes('elevation'),
     1100                            [10.0, 0.0, 10.4])
    11821101        answer = [10.0, 0.0, 10.4]
    11831102        assert num.allclose(results.get_attributes('brightness'), answer)
    11841103        self.failUnless(geo_reference == geo_reference,
    1185                          'test_writepts failed. Test geo_reference')
     1104                        'test_writepts failed. Test geo_reference')
    11861105
    11871106    def test_write_csv_attributes(self):
    1188         #test_write : Test that storage of x,y,attributes works
    1189        
     1107        '''Test that storage of x,y,attributes works'''
     1108
    11901109        att_dict = {}
    1191         pointlist = num.array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1110        pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
    11921111        att_dict['elevation'] = num.array([10.0, 0.0, 10.4])
    11931112        att_dict['brightness'] = num.array([10.0, 0.0, 10.4])
    1194         geo_reference=Geo_reference(56,0,0)
     1113        geo_reference=Geo_reference(56, 0, 0)
     1114
    11951115        # Test txt format
    1196         fileName = tempfile.mktemp(".txt")
     1116        fileName = tempfile.mktemp('.txt')
    11971117        G = Geospatial_data(pointlist, att_dict, geo_reference)
    11981118        G.export_points_file(fileName)
    1199         #print "fileName", fileName
    12001119        results = Geospatial_data(file_name=fileName)
    12011120        os.remove(fileName)
    1202         assert num.allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    1203         assert num.allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4])
     1121
     1122        assert num.allclose(results.get_data_points(False),
     1123                            [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
     1124        assert num.allclose(results.get_attributes('elevation'),
     1125                            [10.0, 0.0, 10.4])
    12041126        answer = [10.0, 0.0, 10.4]
    12051127        assert num.allclose(results.get_attributes('brightness'), answer)
    1206        
    1207  
     1128
    12081129    def test_write_csv_attributes_lat_long(self):
    1209         #test_write : Test that storage of x,y,attributes works
    1210        
     1130        '''Test that storage of x,y,attributes works'''
     1131
    12111132        att_dict = {}
    1212         pointlist = num.array([[-21.5,114.5],[-21.6,114.5],[-21.7,114.5]])
     1133        pointlist = num.array([[-21.5, 114.5], [-21.6, 114.5], [-21.7, 114.5]])
    12131134        att_dict['elevation'] = num.array([10.0, 0.0, 10.4])
    12141135        att_dict['brightness'] = num.array([10.0, 0.0, 10.4])
     1136
    12151137        # Test txt format
    1216         fileName = tempfile.mktemp(".txt")
     1138        fileName = tempfile.mktemp('.txt')
    12171139        G = Geospatial_data(pointlist, att_dict, points_are_lats_longs=True)
    12181140        G.export_points_file(fileName, as_lat_long=True)
    1219         #print "fileName", fileName
    12201141        results = Geospatial_data(file_name=fileName)
    12211142        os.remove(fileName)
     1143
    12221144        assert num.allclose(results.get_data_points(False, as_lat_long=True),
    1223                         pointlist)
    1224         assert num.allclose(results.get_attributes('elevation'), [10.0, 0.0, 10.4])
     1145                            pointlist)
     1146        assert num.allclose(results.get_attributes('elevation'),
     1147                            [10.0, 0.0, 10.4])
    12251148        answer = [10.0, 0.0, 10.4]
    12261149        assert num.allclose(results.get_attributes('brightness'), answer)
    1227        
     1150
    12281151    def test_writepts_no_attributes(self):
    1229 
    1230         #test_writepts_no_attributes: Test that storage of x,y alone works
    1231        
     1152        '''Test that storage of x,y alone works'''
     1153
    12321154        att_dict = {}
    1233         pointlist = num.array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    1234         geo_reference=Geo_reference(56,1.9,1.9)
     1155        pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
     1156        geo_reference=Geo_reference(56, 1.9, 1.9)
    12351157
    12361158        # Test pts format
    1237         fileName = tempfile.mktemp(".pts")
     1159        fileName = tempfile.mktemp('.pts')
    12381160        G = Geospatial_data(pointlist, None, geo_reference)
    12391161        G.export_points_file(fileName, False)
     
    12411163        os.remove(fileName)
    12421164
    1243         assert num.allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1165        assert num.allclose(results.get_data_points(False),
     1166                            [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
    12441167        self.failUnless(geo_reference == geo_reference,
    1245                          'test_writepts failed. Test geo_reference')
    1246        
    1247        
     1168                        'test_writepts failed. Test geo_reference')
     1169
    12481170    def test_write_csv_no_attributes(self):
    1249         #test_write txt _no_attributes: Test that storage of x,y alone works
    1250        
     1171        '''Test that storage of x,y alone works'''
     1172
    12511173        att_dict = {}
    1252         pointlist = num.array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1174        pointlist = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
    12531175        geo_reference=Geo_reference(56,0,0)
     1176
    12541177        # Test format
    1255         fileName = tempfile.mktemp(".txt")
     1178        fileName = tempfile.mktemp('.txt')
    12561179        G = Geospatial_data(pointlist, None, geo_reference)
    12571180        G.export_points_file(fileName)
    12581181        results = Geospatial_data(file_name=fileName)
    12591182        os.remove(fileName)
    1260         assert num.allclose(results.get_data_points(False),[[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
    1261 
    1262          
    1263        
    1264  ########################## BAD .PTS ##########################         
     1183
     1184        assert num.allclose(results.get_data_points(False),
     1185                            [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
     1186
     1187################################################################################
     1188# Check bad PTS files.
     1189################################################################################
    12651190
    12661191    def test_load_bad_no_file_pts(self):
    1267         import os
    1268         import tempfile
    1269        
    1270         fileName = tempfile.mktemp(".pts")
    1271         #print fileName
     1192        fileName = tempfile.mktemp('.pts')
    12721193        try:
    1273             results = Geospatial_data(file_name = fileName)
    1274 #            dict = import_points_file(fileName)
     1194            results = Geospatial_data(file_name=fileName)
    12751195        except IOError:
    12761196            pass
    12771197        else:
    12781198            msg = 'imaginary file did not raise error!'
    1279             raise msg
    1280 #            self.failUnless(0 == 1,
    1281 #                        'imaginary file did not raise error!')
    1282 
     1199            raise Exception, msg
    12831200
    12841201    def test_create_from_pts_file(self):
    1285        
    12861202        from Scientific.IO.NetCDF import NetCDFFile
    12871203
    1288 #        fileName = tempfile.mktemp(".pts")
     1204        # NetCDF file definition
    12891205        FN = 'test_points.pts'
    1290         # NetCDF file definition
    12911206        outfile = NetCDFFile(FN, netcdf_mode_w)
    1292        
     1207
    12931208        # dimension definitions
    1294         outfile.createDimension('number_of_points', 3)   
    1295         outfile.createDimension('number_of_dimensions', 2) #This is 2d data
    1296    
     1209        outfile.createDimension('number_of_points', 3)
     1210        outfile.createDimension('number_of_dimensions', 2) # This is 2d data
     1211
    12971212        # variable definitions
    1298         outfile.createVariable('points', num.Float, ('number_of_points',
    1299                                                      'number_of_dimensions'))
    1300         outfile.createVariable('elevation', num.Float, ('number_of_points',))
    1301    
     1213        outfile.createVariable('points', netcdf_float, ('number_of_points',
     1214                                                        'number_of_dimensions'))
     1215        outfile.createVariable('elevation', netcdf_float, ('number_of_points',))
     1216
    13021217        # Get handles to the variables
    13031218        points = outfile.variables['points']
    13041219        elevation = outfile.variables['elevation']
    1305  
     1220
    13061221        points[0, :] = [1.0,0.0]
    1307         elevation[0] = 10.0 
     1222        elevation[0] = 10.0
    13081223        points[1, :] = [0.0,1.0]
    1309         elevation[1] = 0.0 
     1224        elevation[1] = 0.0
    13101225        points[2, :] = [1.0,0.0]
    1311         elevation[2] = 10.4   
     1226        elevation[2] = 10.4
    13121227
    13131228        outfile.close()
     
    13171232        assert num.allclose(G.get_geo_reference().get_xllcorner(), 0.0)
    13181233        assert num.allclose(G.get_geo_reference().get_yllcorner(), 0.0)
    1319 
    1320         assert num.allclose(G.get_data_points(), [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1234        assert num.allclose(G.get_data_points(),
     1235                            [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
    13211236        assert num.allclose(G.get_attributes(), [10.0, 0.0, 10.4])
    13221237        os.remove(FN)
    13231238
    13241239    def test_create_from_pts_file_with_geo(self):
    1325         """This test reveals if Geospatial data is correctly instantiated from a pts file.
    1326         """
    1327        
     1240        '''Test if Geospatial data is correctly instantiated from a pts file.'''
     1241
    13281242        from Scientific.IO.NetCDF import NetCDFFile
    13291243
     1244        # NetCDF file definition
    13301245        FN = 'test_points.pts'
    1331         # NetCDF file definition
    13321246        outfile = NetCDFFile(FN, netcdf_mode_w)
    13331247
     
    13391253
    13401254        # dimension definitions
    1341         outfile.createDimension('number_of_points', 3)   
    1342         outfile.createDimension('number_of_dimensions', 2) #This is 2d data
    1343    
     1255        outfile.createDimension('number_of_points', 3)
     1256        outfile.createDimension('number_of_dimensions', 2) # This is 2d data
     1257
    13441258        # variable definitions
    1345         outfile.createVariable('points', num.Float, ('number_of_points',
    1346                                                      'number_of_dimensions'))
    1347         outfile.createVariable('elevation', num.Float, ('number_of_points',))
    1348    
     1259        outfile.createVariable('points', netcdf_float, ('number_of_points',
     1260                                                        'number_of_dimensions'))
     1261        outfile.createVariable('elevation', netcdf_float, ('number_of_points',))
     1262
    13491263        # Get handles to the variables
    13501264        points = outfile.variables['points']
     
    13521266
    13531267        points[0, :] = [1.0,0.0]
    1354         elevation[0] = 10.0 
     1268        elevation[0] = 10.0
    13551269        points[1, :] = [0.0,1.0]
    1356         elevation[1] = 0.0 
     1270        elevation[1] = 0.0
    13571271        points[2, :] = [1.0,0.0]
    1358         elevation[2] = 10.4   
     1272        elevation[2] = 10.4
    13591273
    13601274        outfile.close()
     
    13641278        assert num.allclose(G.get_geo_reference().get_xllcorner(), xll)
    13651279        assert num.allclose(G.get_geo_reference().get_yllcorner(), yll)
    1366 
    13671280        assert num.allclose(G.get_data_points(), [[1.0+xll, 0.0+yll],
    13681281                                                  [0.0+xll, 1.0+yll],
    13691282                                                  [1.0+xll, 0.0+yll]])
    1370        
    13711283        assert num.allclose(G.get_attributes(), [10.0, 0.0, 10.4])
     1284
    13721285        os.remove(FN)
    13731286
    1374        
    13751287    def test_add_(self):
    13761288        '''test_add_(self):
    13771289        adds an txt and pts files, reads the files and adds them
    1378            checking results are correct
     1290        checking results are correct
    13791291        '''
     1292
    13801293        # create files
    13811294        att_dict1 = {}
    1382         pointlist1 = num.array([[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1295        pointlist1 = num.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
    13831296        att_dict1['elevation'] = num.array([-10.0, 0.0, 10.4])
    13841297        att_dict1['brightness'] = num.array([10.0, 0.0, 10.4])
    13851298        geo_reference1 = Geo_reference(56, 2.0, 1.0)
    1386        
     1299
    13871300        att_dict2 = {}
    1388         pointlist2 = num.array([[2.0, 1.0],[1.0, 2.0],[2.0, 1.0]])
     1301        pointlist2 = num.array([[2.0, 1.0], [1.0, 2.0], [2.0, 1.0]])
    13891302        att_dict2['elevation'] = num.array([1.0, 15.0, 1.4])
    13901303        att_dict2['brightness'] = num.array([14.0, 1.0, -12.4])
    1391         geo_reference2 = Geo_reference(56, 1.0, 2.0) 
     1304        geo_reference2 = Geo_reference(56, 1.0, 2.0)
    13921305
    13931306        G1 = Geospatial_data(pointlist1, att_dict1, geo_reference1)
    13941307        G2 = Geospatial_data(pointlist2, att_dict2, geo_reference2)
    1395        
    1396         fileName1 = tempfile.mktemp(".txt")
    1397         fileName2 = tempfile.mktemp(".pts")
    1398 
    1399         #makes files
     1308
     1309        fileName1 = tempfile.mktemp('.txt')
     1310        fileName2 = tempfile.mktemp('.pts')
     1311
     1312        # makes files
    14001313        G1.export_points_file(fileName1)
    14011314        G2.export_points_file(fileName2)
    1402        
     1315
    14031316        # add files
    1404        
    1405         G3 = Geospatial_data(file_name = fileName1)
    1406         G4 = Geospatial_data(file_name = fileName2)
    1407        
     1317        G3 = Geospatial_data(file_name=fileName1)
     1318        G4 = Geospatial_data(file_name=fileName2)
    14081319        G = G3 + G4
    14091320
    1410        
    14111321        #read results
    1412 #        print'res', G.get_data_points()
    1413 #        print'res1', G.get_data_points(False)
    14141322        assert num.allclose(G.get_data_points(),
    14151323                            [[ 3.0, 1.0], [ 2.0, 2.0],
    14161324                             [ 3.0, 1.0], [ 3.0, 3.0],
    14171325                             [ 2.0, 4.0], [ 3.0, 3.0]])
    1418                          
    14191326        assert num.allclose(G.get_attributes(attribute_name='elevation'),
    14201327                            [-10.0, 0.0, 10.4, 1.0, 15.0, 1.4])
    1421        
    14221328        answer = [10.0, 0.0, 10.4, 14.0, 1.0, -12.4]
    1423         assert num.allclose(G.get_attributes(attribute_name='brightness'), answer)
    1424        
     1329        assert num.allclose(G.get_attributes(attribute_name='brightness'),
     1330                            answer)
    14251331        self.failUnless(G.get_geo_reference() == geo_reference1,
    1426                          'test_writepts failed. Test geo_reference')
    1427                          
     1332                        'test_writepts failed. Test geo_reference')
     1333
    14281334        os.remove(fileName1)
    14291335        os.remove(fileName2)
    1430        
     1336
    14311337    def test_ensure_absolute(self):
    1432         points = [[2.0, 0.0],[1.0, 1.0],
    1433                          [2.0, 0.0],[2.0, 2.0],
    1434                          [1.0, 3.0],[2.0, 2.0]]
     1338        points = [[2.0, 0.0], [1.0, 1.0],
     1339                  [2.0, 0.0], [2.0, 2.0],
     1340                  [1.0, 3.0], [2.0, 2.0]]
    14351341        new_points = ensure_absolute(points)
    1436        
     1342
    14371343        assert num.allclose(new_points, points)
    14381344
    1439         points = num.array([[2.0, 0.0],[1.0, 1.0],
    1440                             [2.0, 0.0],[2.0, 2.0],
    1441                             [1.0, 3.0],[2.0, 2.0]])
     1345        points = num.array([[2.0, 0.0], [1.0, 1.0],
     1346                            [2.0, 0.0], [2.0, 2.0],
     1347                            [1.0, 3.0], [2.0, 2.0]])
    14421348        new_points = ensure_absolute(points)
    1443        
     1349
    14441350        assert num.allclose(new_points, points)
    1445        
    1446         ab_points = num.array([[2.0, 0.0],[1.0, 1.0],
    1447                                [2.0, 0.0],[2.0, 2.0],
    1448                                [1.0, 3.0],[2.0, 2.0]])
    1449        
    1450         mesh_origin = (56, 290000, 618000) #zone, easting, northing
    1451 
    1452         data_points = num.zeros((ab_points.shape), num.Float)
     1351
     1352        ab_points = num.array([[2.0, 0.0], [1.0, 1.0],
     1353                               [2.0, 0.0], [2.0, 2.0],
     1354                               [1.0, 3.0], [2.0, 2.0]])
     1355
     1356        mesh_origin = (56, 290000, 618000) # zone, easting, northing
     1357        data_points = num.zeros((ab_points.shape), num.float)
     1358
    14531359        #Shift datapoints according to new origins
    14541360        for k in range(len(ab_points)):
    14551361            data_points[k][0] = ab_points[k][0] - mesh_origin[1]
    14561362            data_points[k][1] = ab_points[k][1] - mesh_origin[2]
    1457         #print "data_points",data_points     
    1458         new_points = ensure_absolute(data_points,
    1459                                      geo_reference=mesh_origin)
    1460         #print "new_points",new_points
    1461         #print "ab_points",ab_points
    1462            
     1363        new_points = ensure_absolute(data_points, geo_reference=mesh_origin)
     1364
    14631365        assert num.allclose(new_points, ab_points)
    14641366
    14651367        geo = Geo_reference(56,67,-56)
    1466 
    1467         data_points = geo.change_points_geo_ref(ab_points)   
    1468         new_points = ensure_absolute(data_points,
    1469                                      geo_reference=geo)
    1470         #print "new_points",new_points
    1471         #print "ab_points",ab_points
    1472            
     1368        data_points = geo.change_points_geo_ref(ab_points)
     1369        new_points = ensure_absolute(data_points, geo_reference=geo)
     1370
    14731371        assert num.allclose(new_points, ab_points)
    1474 
    14751372
    14761373        geo_reference = Geo_reference(56, 100, 200)
     
    14781375        points = geo_reference.change_points_geo_ref(ab_points)
    14791376        attributes = [2, 4]
    1480         #print "geo in points", points
    1481         G = Geospatial_data(points, attributes,
    1482                             geo_reference=geo_reference)
    1483          
     1377        G = Geospatial_data(points, attributes, geo_reference=geo_reference)
    14841378        new_points = ensure_absolute(G)
    1485         #print "new_points",new_points
    1486         #print "ab_points",ab_points
    1487            
     1379
    14881380        assert num.allclose(new_points, ab_points)
    14891381
    1490 
    1491        
    14921382    def test_ensure_geospatial(self):
    1493         points = [[2.0, 0.0],[1.0, 1.0],
    1494                          [2.0, 0.0],[2.0, 2.0],
    1495                          [1.0, 3.0],[2.0, 2.0]]
     1383        points = [[2.0, 0.0], [1.0, 1.0],
     1384                  [2.0, 0.0], [2.0, 2.0],
     1385                  [1.0, 3.0], [2.0, 2.0]]
    14961386        new_points = ensure_geospatial(points)
    1497        
    1498         assert num.allclose(new_points.get_data_points(absolute = True), points)
    1499 
    1500         points = num.array([[2.0, 0.0],[1.0, 1.0],
    1501                             [2.0, 0.0],[2.0, 2.0],
    1502                             [1.0, 3.0],[2.0, 2.0]])
     1387        assert num.allclose(new_points.get_data_points(absolute=True), points)
     1388
     1389        points = num.array([[2.0, 0.0], [1.0, 1.0],
     1390                            [2.0, 0.0], [2.0, 2.0],
     1391                            [1.0, 3.0], [2.0, 2.0]])
    15031392        new_points = ensure_geospatial(points)
    1504        
    1505         assert num.allclose(new_points.get_data_points(absolute = True), points)
    1506        
     1393        assert num.allclose(new_points.get_data_points(absolute=True), points)
     1394
    15071395        ab_points = num.array([[2.0, 0.0],[1.0, 1.0],
    15081396                               [2.0, 0.0],[2.0, 2.0],
    15091397                               [1.0, 3.0],[2.0, 2.0]])
    1510        
    1511         mesh_origin = (56, 290000, 618000) #zone, easting, northing
    1512 
    1513         data_points = num.zeros((ab_points.shape), num.Float)
     1398        mesh_origin = (56, 290000, 618000)      # zone, easting, northing
     1399        data_points = num.zeros((ab_points.shape), num.float)
     1400
    15141401        #Shift datapoints according to new origins
    15151402        for k in range(len(ab_points)):
    15161403            data_points[k][0] = ab_points[k][0] - mesh_origin[1]
    15171404            data_points[k][1] = ab_points[k][1] - mesh_origin[2]
    1518         #print "data_points",data_points     
    15191405        new_geospatial = ensure_geospatial(data_points,
    1520                                              geo_reference=mesh_origin)
     1406                                           geo_reference=mesh_origin)
    15211407        new_points = new_geospatial.get_data_points(absolute=True)
    1522         #print "new_points",new_points
    1523         #print "ab_points",ab_points
    1524            
    15251408        assert num.allclose(new_points, ab_points)
    15261409
    1527         geo = Geo_reference(56,67,-56)
    1528 
    1529         data_points = geo.change_points_geo_ref(ab_points)   
    1530         new_geospatial = ensure_geospatial(data_points,
    1531                                              geo_reference=geo)
     1410        geo = Geo_reference(56, 67, -56)
     1411        data_points = geo.change_points_geo_ref(ab_points)
     1412        new_geospatial = ensure_geospatial(data_points, geo_reference=geo)
    15321413        new_points = new_geospatial.get_data_points(absolute=True)
    1533         #print "new_points",new_points
    1534         #print "ab_points",ab_points
    1535            
    1536         assert num.allclose(new_points, ab_points)
    1537 
     1414        msg = ('new_points=\n%s\nab_points=\n%s'
     1415               % (str(new_points), str(ab_points)))
     1416        assert num.allclose(new_points, ab_points), msg
    15381417
    15391418        geo_reference = Geo_reference(56, 100, 200)
     
    15411420        points = geo_reference.change_points_geo_ref(ab_points)
    15421421        attributes = [2, 4]
    1543         #print "geo in points", points
    1544         G = Geospatial_data(points, attributes,
    1545                             geo_reference=geo_reference)
    1546          
    1547         new_geospatial  = ensure_geospatial(G)
     1422        G = Geospatial_data(points, attributes, geo_reference=geo_reference)
     1423        new_geospatial = ensure_geospatial(G)
    15481424        new_points = new_geospatial.get_data_points(absolute=True)
    1549         #print "new_points",new_points
    1550         #print "ab_points",ab_points
    1551            
    15521425        assert num.allclose(new_points, ab_points)
    1553        
     1426
    15541427    def test_isinstance(self):
    1555 
    1556         import os
    1557        
    1558         fileName = tempfile.mktemp(".csv")
    1559         file = open(fileName,"w")
    1560         file.write("x,y,  elevation ,  speed \n\
    1561 1.0, 0.0, 10.0, 0.0\n\
    1562 0.0, 1.0, 0.0, 10.0\n\
    1563 1.0, 0.0, 10.4, 40.0\n")
     1428        fileName = tempfile.mktemp('.csv')
     1429        file = open(fileName, 'w')
     1430        file.write('x,y,  elevation ,  speed \n\
     14311.0, 0.0, 10.0, 0.0\n\
     14320.0, 1.0, 0.0, 10.0\n\
     14331.0, 0.0, 10.4, 40.0\n')
    15641434        file.close()
    15651435
    15661436        results = Geospatial_data(fileName)
    15671437        assert num.allclose(results.get_data_points(absolute=True),
    1568                             [[1.0, 0.0],[0.0, 1.0],[1.0, 0.0]])
     1438                            [[1.0, 0.0], [0.0, 1.0], [1.0, 0.0]])
    15691439        assert num.allclose(results.get_attributes(attribute_name='elevation'),
    15701440                            [10.0, 0.0, 10.4])
     
    15731443
    15741444        os.remove(fileName)
    1575        
    15761445
    15771446    def test_no_constructors(self):
    1578        
    15791447        try:
    15801448            G = Geospatial_data()
    1581 #            results = Geospatial_data(file_name = fileName)
    1582 #            dict = import_points_file(fileName)
    15831449        except ValueError:
    15841450            pass
    15851451        else:
    15861452            msg = 'Instance must have a filename or data points'
    1587             raise msg       
     1453            raise Exception, msg
    15881454
    15891455    def test_load_csv_lat_long(self):
    1590         """
    1591         comma delimited
    1592 
    1593         """
    1594         fileName = tempfile.mktemp(".csv")
    1595         file = open(fileName,"w")
    1596         file.write("long,lat, elevation, yeah \n\
     1456        '''comma delimited'''
     1457
     1458        fileName = tempfile.mktemp('.csv')
     1459        file = open(fileName, 'w')
     1460        file.write('long,lat, elevation, yeah \n\
    15971461150.916666667,-34.50,452.688000, 10\n\
    1598 150.0,-34,459.126000, 10\n")
     1462150.0,-34,459.126000, 10\n')
    15991463        file.close()
     1464
    16001465        results = Geospatial_data(fileName)
    16011466        os.remove(fileName)
    16021467        points = results.get_data_points()
    1603        
     1468
    16041469        assert num.allclose(points[0][0], 308728.009)
    16051470        assert num.allclose(points[0][1], 6180432.601)
    16061471        assert num.allclose(points[1][0],  222908.705)
    16071472        assert num.allclose(points[1][1], 6233785.284)
    1608        
    1609      
     1473
    16101474    def test_load_csv_lat_longII(self):
    1611         """
    1612         comma delimited
    1613 
    1614         """
    1615         fileName = tempfile.mktemp(".csv")
    1616         file = open(fileName,"w")
    1617         file.write("Lati,LONG,z \n\
     1475        '''comma delimited'''
     1476
     1477        fileName = tempfile.mktemp('.csv')
     1478        file = open(fileName, 'w')
     1479        file.write('Lati,LONG,z \n\
    16181480-34.50,150.916666667,452.688000\n\
    1619 -34,150.0,459.126000\n")
     1481-34,150.0,459.126000\n')
    16201482        file.close()
     1483
    16211484        results = Geospatial_data(fileName)
    16221485        os.remove(fileName)
    16231486        points = results.get_data_points()
    1624        
     1487
    16251488        assert num.allclose(points[0][0], 308728.009)
    16261489        assert num.allclose(points[0][1], 6180432.601)
    1627         assert num.allclose(points[1][0],  222908.705)
     1490        assert num.allclose(points[1][0], 222908.705)
    16281491        assert num.allclose(points[1][1], 6233785.284)
    16291492
    1630          
    16311493    def test_load_csv_lat_long_bad(self):
    1632         """
    1633         comma delimited
    1634 
    1635         """
    1636         fileName = tempfile.mktemp(".csv")
    1637         file = open(fileName,"w")
    1638         file.write("Lati,LONG,z \n\
     1494        '''comma delimited'''
     1495
     1496        fileName = tempfile.mktemp('.csv')
     1497        file = open(fileName, 'w')
     1498        file.write('Lati,LONG,z \n\
    16391499-25.0,180.0,452.688000\n\
    1640 -34,150.0,459.126000\n")
     1500-34,150.0,459.126000\n')
    16411501        file.close()
     1502
    16421503        try:
    16431504            results = Geospatial_data(fileName)
     
    16461507        else:
    16471508            msg = 'Different zones in Geo references not caught.'
    1648             raise msg       
    1649        
    1650         os.remove(fileName)
    1651        
     1509            raise Exception, msg
     1510
     1511        os.remove(fileName)
     1512
    16521513    def test_lat_long(self):
    1653         lat_gong = degminsec2decimal_degrees(-34,30,0.)
    1654         lon_gong = degminsec2decimal_degrees(150,55,0.)
    1655        
    1656         lat_2 = degminsec2decimal_degrees(-34,00,0.)
    1657         lon_2 = degminsec2decimal_degrees(150,00,0.)
    1658        
     1514        lat_gong = degminsec2decimal_degrees(-34, 30, 0.)
     1515        lon_gong = degminsec2decimal_degrees(150, 55, 0.)
     1516
     1517        lat_2 = degminsec2decimal_degrees(-34, 00, 0.)
     1518        lon_2 = degminsec2decimal_degrees(150, 00, 0.)
     1519
    16591520        lats = [lat_gong, lat_2]
    16601521        longs = [lon_gong, lon_2]
     
    16621523
    16631524        points = gsd.get_data_points(absolute=True)
    1664        
     1525
    16651526        assert num.allclose(points[0][0], 308728.009)
    16661527        assert num.allclose(points[0][1], 6180432.601)
    1667         assert num.allclose(points[1][0],  222908.705)
     1528        assert num.allclose(points[1][0], 222908.705)
    16681529        assert num.allclose(points[1][1], 6233785.284)
    16691530        self.failUnless(gsd.get_geo_reference().get_zone() == 56,
    16701531                        'Bad zone error!')
    1671        
     1532
     1533        # use self.failUnlessRaises(ValueError, Geospatial_data(latitudes=lats))
    16721534        try:
    16731535            results = Geospatial_data(latitudes=lats)
     
    16751537            pass
    16761538        else:
    1677             self.failUnless(0 ==1,  'Error not thrown error!')
     1539            self.fail('Error not thrown error!')
    16781540        try:
    16791541            results = Geospatial_data(latitudes=lats)
     
    16811543            pass
    16821544        else:
    1683             self.failUnless(0 ==1,  'Error not thrown error!')
     1545            self.fail('Error not thrown error!')
    16841546        try:
    16851547            results = Geospatial_data(longitudes=lats)
     
    16871549            pass
    16881550        else:
    1689             self.failUnless(0 ==1, 'Error not thrown error!')
     1551            self.fail('Error not thrown error!')
    16901552        try:
    16911553            results = Geospatial_data(latitudes=lats, longitudes=longs,
     
    16941556            pass
    16951557        else:
    1696             self.failUnless(0 ==1,  'Error not thrown error!')
    1697            
     1558            self.fail('Error not thrown error!')
     1559
    16981560        try:
    16991561            results = Geospatial_data(latitudes=lats, longitudes=longs,
     
    17021564            pass
    17031565        else:
    1704             self.failUnless(0 ==1,  'Error not thrown error!')
     1566            self.fail('Error not thrown error!')
    17051567
    17061568    def test_lat_long2(self):
    1707         lat_gong = degminsec2decimal_degrees(-34,30,0.)
    1708         lon_gong = degminsec2decimal_degrees(150,55,0.)
    1709        
    1710         lat_2 = degminsec2decimal_degrees(-34,00,0.)
    1711         lon_2 = degminsec2decimal_degrees(150,00,0.)
    1712        
     1569        lat_gong = degminsec2decimal_degrees(-34, 30, 0.)
     1570        lon_gong = degminsec2decimal_degrees(150, 55, 0.)
     1571
     1572        lat_2 = degminsec2decimal_degrees(-34, 00, 0.)
     1573        lon_2 = degminsec2decimal_degrees(150, 00, 0.)
     1574
    17131575        points = [[lat_gong, lon_gong], [lat_2, lon_2]]
    17141576        gsd = Geospatial_data(data_points=points, points_are_lats_longs=True)
    17151577
    17161578        points = gsd.get_data_points(absolute=True)
    1717        
     1579
    17181580        assert num.allclose(points[0][0], 308728.009)
    17191581        assert num.allclose(points[0][1], 6180432.601)
    1720         assert num.allclose(points[1][0],  222908.705)
     1582        assert num.allclose(points[1][0], 222908.705)
    17211583        assert num.allclose(points[1][1], 6233785.284)
    17221584        self.failUnless(gsd.get_geo_reference().get_zone() == 56,
     
    17281590            pass
    17291591        else:
    1730             self.failUnless(0 ==1,  'Error not thrown error!')
    1731 
     1592            self.fail('Error not thrown error!')
    17321593
    17331594    def test_write_urs_file(self):
    1734         lat_gong = degminsec2decimal_degrees(-34,00,0)
    1735         lon_gong = degminsec2decimal_degrees(150,30,0.)
    1736        
    1737         lat_2 = degminsec2decimal_degrees(-34,00,1)
    1738         lon_2 = degminsec2decimal_degrees(150,00,0.)
     1595        lat_gong = degminsec2decimal_degrees(-34, 00, 0)
     1596        lon_gong = degminsec2decimal_degrees(150, 30, 0.)
     1597
     1598        lat_2 = degminsec2decimal_degrees(-34, 00, 1)
     1599        lon_2 = degminsec2decimal_degrees(150, 00, 0.)
    17391600        p1 = (lat_gong, lon_gong)
    17401601        p2 = (lat_2, lon_2)
     
    17421603        gsd = Geospatial_data(data_points=list(points),
    17431604                              points_are_lats_longs=True)
    1744        
    1745         fn = tempfile.mktemp(".urs")
     1605
     1606        fn = tempfile.mktemp('.urs')
    17461607        gsd.export_points_file(fn)
    1747         #print "fn", fn
    17481608        handle = open(fn)
    17491609        lines = handle.readlines()
    1750         assert lines[0],'2'
    1751         assert lines[1],'-34.0002778 150.0 0'
    1752         assert lines[2],'-34.0 150.5 1'
     1610        assert lines[0], '2'
     1611        assert lines[1], '-34.0002778 150.0 0'
     1612        assert lines[2], '-34.0 150.5 1'
    17531613        handle.close()
    17541614        os.remove(fn)
    1755        
     1615
    17561616    def test_lat_long_set(self):
    1757         lat_gong = degminsec2decimal_degrees(-34,30,0.)
    1758         lon_gong = degminsec2decimal_degrees(150,55,0.)
    1759        
    1760         lat_2 = degminsec2decimal_degrees(-34,00,0.)
    1761         lon_2 = degminsec2decimal_degrees(150,00,0.)
     1617        lat_gong = degminsec2decimal_degrees(-34, 30, 0.)
     1618        lon_gong = degminsec2decimal_degrees(150, 55, 0.)
     1619
     1620        lat_2 = degminsec2decimal_degrees(-34, 00, 0.)
     1621        lon_2 = degminsec2decimal_degrees(150, 00, 0.)
    17621622        p1 = (lat_gong, lon_gong)
    17631623        p2 = (lat_2, lon_2)
     
    17671627
    17681628        points = gsd.get_data_points(absolute=True)
    1769         #print "points[0][0]", points[0][0]
    17701629        #Note the order is unknown, due to using sets
    17711630        # and it changes from windows to linux
     
    17731632            assert num.allclose(points[1][0], 308728.009)
    17741633            assert num.allclose(points[1][1], 6180432.601)
    1775             assert num.allclose(points[0][0],  222908.705)
     1634            assert num.allclose(points[0][0], 222908.705)
    17761635            assert num.allclose(points[0][1], 6233785.284)
    17771636        except AssertionError:
    17781637            assert num.allclose(points[0][0], 308728.009)
    17791638            assert num.allclose(points[0][1], 6180432.601)
    1780             assert num.allclose(points[1][0],  222908.705)
     1639            assert num.allclose(points[1][0], 222908.705)
    17811640            assert num.allclose(points[1][1], 6233785.284)
    1782            
     1641
    17831642        self.failUnless(gsd.get_geo_reference().get_zone() == 56,
    17841643                        'Bad zone error!')
    17851644        points = gsd.get_data_points(as_lat_long=True)
    1786         #print "test_lat_long_set points", points
    17871645        try:
    17881646            assert num.allclose(points[0][0], -34)
     
    17931651
    17941652    def test_len(self):
    1795        
    17961653        points = [[1.0, 2.1], [3.0, 5.3]]
    17971654        G = Geospatial_data(points)
    1798         self.failUnless(2 ==len(G), 'Len error!')
    1799        
     1655        self.failUnless(2 == len(G), 'Len error!')
     1656
    18001657        points = [[1.0, 2.1]]
    18011658        G = Geospatial_data(points)
    1802         self.failUnless(1 ==len(G), 'Len error!')
     1659        self.failUnless(1 == len(G), 'Len error!')
    18031660
    18041661        points = [[1.0, 2.1], [3.0, 5.3], [3.0, 5.3], [3.0, 5.3]]
    18051662        G = Geospatial_data(points)
    1806         self.failUnless(4 ==len(G), 'Len error!')
    1807        
     1663        self.failUnless(4 == len(G), 'Len error!')
     1664
    18081665    def test_split(self):
    18091666        """test if the results from spilt are disjoin sets"""
    1810        
    1811         #below is a work around until the randint works on cyclones compute nodes
    1812         if get_host_name()[8:9]!='0':
    1813                
    1814            
     1667
     1668        # below is a workaround until randint works on cyclones compute nodes
     1669        if get_host_name()[8:9] != '0':
    18151670            points = [[1.0, 1.0], [1.0, 2.0],[1.0, 3.0], [1.0, 4.0], [1.0, 5.0],
    18161671                      [2.0, 1.0], [2.0, 2.0],[2.0, 3.0], [2.0, 4.0], [2.0, 5.0],
     
    18181673                      [4.0, 1.0], [4.0, 2.0],[4.0, 3.0], [4.0, 4.0], [4.0, 5.0],
    18191674                      [5.0, 1.0], [5.0, 2.0],[5.0, 3.0], [5.0, 4.0], [5.0, 5.0]]
    1820             attributes = {'depth':[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
    1821                           14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
    1822                           'speed':[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
    1823                           14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]}
     1675            attributes = {'depth': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
     1676                                    11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
     1677                                    21, 22, 23, 24, 25],
     1678                          'speed': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
     1679                                    11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
     1680                                    21, 22, 23, 24, 25]}
    18241681            G = Geospatial_data(points, attributes)
    1825    
     1682
    18261683            factor = 0.21
    1827    
    1828             #will return G1 with 10% of points and G2 with 90%
    1829             G1, G2  = G.split(factor,100)
    1830            
     1684
     1685            # will return G1 with 10% of points and G2 with 90%
     1686            G1, G2  = G.split(factor, 100)
    18311687            assert num.allclose(len(G), len(G1)+len(G2))
    18321688            assert num.allclose(round(len(G)*factor), len(G1))
    1833    
     1689
    18341690            P = G1.get_data_points(absolute=False)
    1835             assert num.allclose(P, [[5.0,4.0],[4.0,3.0],[4.0,2.0],[3.0,1.0],[2.0,3.0]])
    1836    
     1691            expected = [[5.,4.], [4.,1.], [2.,4.], [2.,3.], [1.,4.]]
     1692            #            expected = [[5.0, 4.0], [4.0, 3.0], [4.0, 2.0],
     1693                                     #            [3.0, 1.0], [2.0, 3.0]]
     1694            msg = 'Expected %s, but\nP=%s' % (str(expected), str(P))
     1695            assert num.allclose(P, expected), msg
     1696
    18371697            A = G1.get_attributes()
    1838             assert num.allclose(A,[24, 18, 17, 11, 8])
    1839        
     1698            expected = [24, 16, 9, 8, 4]
     1699            msg = 'expected=%s, but A=%s' % (str(expected), str(A))
     1700            assert num.allclose(A, expected), msg
     1701
    18401702    def test_split1(self):
    1841         """test if the results from spilt are disjoin sets"""
    1842         #below is a work around until the randint works on cyclones compute nodes
    1843         if get_host_name()[8:9]!='0':
    1844 
    1845             from RandomArray import randint,seed
    1846             seed(100,100)
    1847             a_points = randint(0,999999,(10,2))
     1703        """test if the results from split are disjoint sets"""
     1704
     1705        # below is a workaround until randint works on cyclones compute nodes
     1706        if get_host_name()[8:9] != '0':
     1707            from numpy.random import randint, seed
     1708
     1709            seed((100, 100))
     1710            a_points = randint(0, 999999, (10,2))
    18481711            points = a_points.tolist()
    1849     #        print points
    1850    
     1712
    18511713            G = Geospatial_data(points)
    1852    
     1714
    18531715            factor = 0.1
    1854    
    1855             #will return G1 with 10% of points and G2 with 90%
    1856             G1, G2  = G.split(factor,100)
    1857            
    1858     #        print 'G1',G1
     1716
     1717            # will return G1 with 10% of points and G2 with 90%
     1718            G1, G2  = G.split(factor, 100)
    18591719            assert num.allclose(len(G), len(G1)+len(G2))
    18601720            assert num.allclose(round(len(G)*factor), len(G1))
    1861    
     1721
    18621722            P = G1.get_data_points(absolute=False)
    1863             assert num.allclose(P, [[982420.,28233.]])
    1864 
    1865  
     1723            expected = [[425835., 137518.]]
     1724            msg = 'expected=%s, but\nP=%s' % (str(expected), str(P))
     1725            assert num.allclose(P, expected), msg
     1726
    18661727    def test_find_optimal_smoothing_parameter(self):
    18671728        """
    1868         Creates a elevation file represting hill (sort of) and runs
     1729        Creates a elevation file representing a hill (sort of) and runs
    18691730        find_optimal_smoothing_parameter for 3 different alphas,
    1870        
     1731
    18711732        NOTE the random number seed is provided to control the results
    18721733        """
     1734
    18731735        from cmath import cos
    18741736
    1875         #below is a work around until the randint works on cyclones compute nodes
     1737        # below is a workaround until randint works on cyclones compute nodes
    18761738        if get_host_name()[8:9]!='0':
    1877 
    1878             filename = tempfile.mktemp(".csv")
    1879             file = open(filename,"w")
    1880             file.write("x,y,elevation \n")
    1881    
    1882             for i in range(-5,6):
    1883                 for j in range(-5,6):
    1884                     #this equation made surface like a circle ripple
     1739            filename = tempfile.mktemp('.csv')
     1740            file = open(filename, 'w')
     1741            file.write('x,y,elevation \n')
     1742
     1743            for i in range(-5, 6):
     1744                for j in range(-5, 6):
     1745                    # this equation makes a surface like a circle ripple
    18851746                    z = abs(cos(((i*i) + (j*j))*.1)*2)
    1886     #                print 'x,y,f',i,j,z
    1887                     file.write("%s, %s, %s\n" %(i, j, z))
    1888                    
     1747                    file.write("%s, %s, %s\n" % (i, j, z))
     1748
    18891749            file.close()
    1890      
    1891             value, alpha = find_optimal_smoothing_parameter(data_file=filename, 
     1750
     1751            value, alpha = find_optimal_smoothing_parameter(data_file=filename,
    18921752                                                 alpha_list=[0.0001, 0.01, 1],
    18931753                                                 mesh_file=None,
     
    19001760                                                 seed_num=100000,
    19011761                                                 verbose=False)
    1902    
     1762
    19031763            os.remove(filename)
    1904            
     1764
    19051765            # print value, alpha
    1906             assert (alpha==0.01)
     1766            assert(alpha == 0.01)
    19071767
    19081768    def test_find_optimal_smoothing_parameter1(self):
    19091769        """
    1910         Creates a elevation file represting hill (sort of) and
     1770        Creates a elevation file representing  a hill (sort of) and
    19111771        Then creates a mesh file and passes the mesh file and the elevation
    19121772        file to find_optimal_smoothing_parameter for 3 different alphas,
    1913        
     1773
    19141774        NOTE the random number seed is provided to control the results
    19151775        """
    1916         #below is a work around until the randint works on cyclones compute nodes
     1776
     1777        # below is a workaround until randint works on cyclones compute nodes
    19171778        if get_host_name()[8:9]!='0':
    1918 
    19191779            from cmath import cos
    19201780            from anuga.pmesh.mesh_interface import create_mesh_from_regions
    1921            
    1922             filename = tempfile.mktemp(".csv")
    1923             file = open(filename,"w")
    1924             file.write("x,y,elevation \n")
    1925    
    1926             for i in range(-5,6):
    1927                 for j in range(-5,6):
    1928                     #this equation made surface like a circle ripple
     1781
     1782            filename = tempfile.mktemp('.csv')
     1783            file = open(filename, 'w')
     1784            file.write('x,y,elevation \n')
     1785
     1786            for i in range(-5 ,6):
     1787                for j in range(-5, 6):
     1788                    # this equation makes a surface like a circle ripple
    19291789                    z = abs(cos(((i*i) + (j*j))*.1)*2)
    1930     #                print 'x,y,f',i,j,z
    1931                     file.write("%s, %s, %s\n" %(i, j, z))
    1932                    
     1790                    file.write('%s, %s, %s\n' % (i, j, z))
     1791
    19331792            file.close()
    1934             poly=[[5,5],[5,-5],[-5,-5],[-5,5]]
    1935             internal_poly=[[[[1,1],[1,-1],[-1,-1],[-1,1]],.5]]
    1936             mesh_filename= tempfile.mktemp(".msh")
    1937            
     1793
     1794            poly=[[5,5], [5,-5], [-5,-5], [-5,5]]
     1795            internal_poly=[[[[1,1], [1,-1], [-1,-1], [-1,1]], .5]]
     1796            mesh_filename= tempfile.mktemp('.msh')
     1797
    19381798            create_mesh_from_regions(poly,
    1939                                  boundary_tags={'back': [2],
    1940                                                 'side': [1,3],
    1941                                                 'ocean': [0]},
    1942                              maximum_triangle_area=3,
    1943                              interior_regions=internal_poly,
    1944                              filename=mesh_filename,
    1945                              use_cache=False,
    1946                              verbose=False)
    1947      
    1948             value, alpha = find_optimal_smoothing_parameter(data_file=filename, 
     1799                                     boundary_tags={'back': [2],
     1800                                                    'side': [1,3],
     1801                                                    'ocean': [0]},
     1802                                     maximum_triangle_area=3,
     1803                                     interior_regions=internal_poly,
     1804                                     filename=mesh_filename,
     1805                                     use_cache=False,
     1806                                     verbose=False)
     1807
     1808            value, alpha = find_optimal_smoothing_parameter(data_file=filename,
    19491809                                                 alpha_list=[0.0001, 0.01, 1],
    19501810                                                 mesh_file=mesh_filename,
     
    19521812                                                 seed_num=174,
    19531813                                                 verbose=False)
    1954    
     1814
    19551815            os.remove(filename)
    19561816            os.remove(mesh_filename)
    1957            
    1958     #        print value, alpha
    1959             assert (alpha==0.01)
     1817
     1818            msg = 'alpha=%s' % str(alpha)
     1819            # 0.01 was expected with Numeric.RandomArray RNG
     1820            assert alpha==1.0, msg
    19601821
    19611822    def test_find_optimal_smoothing_parameter2(self):
    1962         """
    1963         Tests requirement that mesh file must exist or IOError is thrown
    1964        
     1823        '''Tests requirement that mesh file must exist or IOError is thrown
     1824
    19651825        NOTE the random number seed is provided to control the results
    1966         """
     1826        '''
     1827
    19671828        from cmath import cos
    19681829        from anuga.pmesh.mesh_interface import create_mesh_from_regions
    1969        
    1970         filename = tempfile.mktemp(".csv")
    1971         mesh_filename= tempfile.mktemp(".msh")
    1972        
     1830
     1831        filename = tempfile.mktemp('.csv')
     1832        mesh_filename= tempfile.mktemp('.msh')
     1833
    19731834        try:
    1974             value, alpha = find_optimal_smoothing_parameter(data_file=filename, 
     1835            value, alpha = find_optimal_smoothing_parameter(data_file=filename,
    19751836                                             alpha_list=[0.0001, 0.01, 1],
    19761837                                             mesh_file=mesh_filename,
     
    19811842            pass
    19821843        else:
    1983             self.failUnless(0 ==1,  'Error not thrown error!')
    1984        
    1985          
     1844            self.fail('Error not thrown error!')
     1845
     1846################################################################################
     1847
    19861848if __name__ == "__main__":
    1987 
    1988     #suite = unittest.makeSuite(Test_Geospatial_data, 'test_write_csv_attributes_lat_long')
    1989     suite = unittest.makeSuite(Test_Geospatial_data, 'test_find_optimal_smoothing_parameter')
    1990     #suite = unittest.makeSuite(Test_Geospatial_data, 'test_split1')
    1991     #suite = unittest.makeSuite(Test_Geospatial_data, 'test')
     1849    suite = unittest.makeSuite(Test_Geospatial_data, 'test')
    19921850    runner = unittest.TextTestRunner() #verbosity=2)
    19931851    runner.run(suite)
    19941852
    1995    
     1853
Note: See TracChangeset for help on using the changeset viewer.