Changeset 4037


Ignore:
Timestamp:
Nov 28, 2006, 11:54:54 AM (18 years ago)
Author:
duncan
Message:

removed bug that broke maxlat and minlat in sww2ferret

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

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/shallow_water/data_manager.py

    r4031 r4037  
    6464
    6565from Numeric import concatenate, array, Float, Int, Int32, resize, sometrue, \
    66      searchsorted, zeros, allclose, around, reshape, transpose
     66     searchsorted, zeros, allclose, around, reshape, transpose, sort
    6767from Scientific.IO.NetCDF import NetCDFFile
    6868
     
    26862686        jmax = searchsorted(times, maxt)
    26872687
     2688    #print "latitudes[:]",latitudes[:]
     2689    #print "longitudes[:]",longitudes [:]
    26882690    kmin, kmax, lmin, lmax = _get_min_max_indexes(latitudes[:],
    26892691                                                  longitudes[:],
     
    26972699    longitudes = longitudes[lmin:lmax]
    26982700
     2701    #print "latitudes[:]",latitudes[:]
     2702    #print "longitudes[:]",longitudes [:]
    26992703
    27002704    if verbose: print 'cropping'
     
    38893893    outfile.close()
    38903894
    3891 def _get_min_max_indexes(latitudes,longitudes,
     3895def _get_min_max_indexes(latitudes_ref,longitudes_ref,
    38923896                        minlat=None, maxlat=None,
    38933897                        minlon=None, maxlon=None):
     
    39013905    has a section outside of the latitudes/longitudes area.)
    39023906
    3903     assume latitudess & longitudes are sorted,
     3907    asset longitudes are sorted,
    39043908    long - from low to high (west to east, eg 148 - 151)
    3905     lat - from high to low (north to south, eg -35 - -38)
    3906     """
    3907 
    3908     # reverse order of lat, so it's in ascending order
    3909     try:
    3910         latitudes = latitudes.tolist()
    3911     except:
    3912         pass
     3909    assert latitudes are sorted, ascending or decending
     3910    """
     3911    latitudes = latitudes_ref[:]
     3912    longitudes = longitudes_ref[:]
     3913
     3914    latitudes = ensure_numeric(latitudes)
     3915    longitudes = ensure_numeric(longitudes)
    39133916   
    3914     latitudes.reverse()
     3917    assert allclose(sort(longitudes), longitudes)
     3918   
     3919    lat_ascending = True
     3920    if not allclose(sort(latitudes), latitudes):
     3921        lat_ascending = False
     3922        # reverse order of lat, so it's in ascending order         
     3923        latitudes = latitudes[::-1]
     3924        assert allclose(sort(latitudes), latitudes)
     3925    #print "latitudes  in funct", latitudes
    39153926   
    39163927    largest_lat_index = len(latitudes)-1
     
    39433954        lon_max_index = searchsorted(longitudes, maxlon)
    39443955
    3945     #Take into account that the latitude list was reversed
    3946     latitudes.reverse() # Python passes by reference, need to swap back
    3947     lat_min_index, lat_max_index = largest_lat_index - lat_max_index , \
    3948                                    largest_lat_index - lat_min_index
     3956    # Reversing the indexes, if the lat array is decending
     3957    if lat_ascending is False:
     3958        lat_min_index, lat_max_index = largest_lat_index - lat_max_index , \
     3959                                       largest_lat_index - lat_min_index
    39493960    lat_max_index = lat_max_index + 1 # taking into account how slicing works
    39503961    lon_max_index = lon_max_index + 1 # taking into account how slicing works
  • anuga_core/source/anuga/shallow_water/test_data_manager.py

    r4026 r4037  
    23202320        # LON = 150.66667, 150.83334, 151, 151.16667
    23212321        # LAT = -34.5, -34.33333, -34.16667, -34 ;
    2322         # TIME = 0, 0.1, 0.6, 1.1, 1.6, 2.1 ;
    2323         #
    2324         # First value (index=0) in small_ha.nc is 0.3400644 cm,
    2325         # Fourth value (index==3) is -6.50198 cm
    2326 
    2327 
    2328 
     2322       
    23292323        #Read
    23302324        from anuga.coordinate_transforms.redfearn import redfearn
     
    23612355
    23622356
     2357    def test_ferret2sww_lat_longII(self):
     2358        # Test that min lat long works
     2359
     2360        #The test file has
     2361        # LON = 150.66667, 150.83334, 151, 151.16667
     2362        # LAT = -34.5, -34.33333, -34.16667, -34 ;
     2363       
     2364        #Read
     2365        from anuga.coordinate_transforms.redfearn import redfearn
     2366        fid = NetCDFFile(self.test_MOST_file + '_ha.nc')
     2367        first_value = fid.variables['HA'][:][0,0,0]
     2368        fourth_value = fid.variables['HA'][:][0,0,3]
     2369        fid.close()
     2370
     2371
     2372        #Call conversion (with zero origin)
     2373        #ferret2sww('small', verbose=False,
     2374        #           origin = (56, 0, 0))
     2375        ferret2sww(self.test_MOST_file, verbose=False,
     2376                   origin = (56, 0, 0), minlat=-34.4, maxlat=-34.2)
     2377
     2378        #Work out the UTM coordinates for first point
     2379        zone, e, n = redfearn(-34.5, 150.66667)
     2380        #print zone, e, n
     2381
     2382        #Read output file 'small.sww'
     2383        #fid = NetCDFFile('small.sww')
     2384        fid = NetCDFFile(self.test_MOST_file + '.sww')
     2385
     2386        x = fid.variables['x'][:]
     2387        y = fid.variables['y'][:]
     2388        #Check that first coordinate is correctly represented
     2389        assert 12 == len(x)
     2390
     2391        fid.close()
     2392
     2393        #Cleanup
     2394        import os
     2395        os.remove(self.test_MOST_file + '.sww')
     2396       
    23632397    def test_ferret2sww3(self):
    23642398        """Elevation included
     
    41714205
    41724206        self.failUnless(m2d == [[5,6],[9,10]],
     4207                         'failed')
     4208
     4209    def test_get_min_max_indexes_lat_ascending(self):
     4210        latitudes = [0,1,2,3]
     4211        longitudes = [0,10,20,30]
     4212
     4213        # k - lat
     4214        # l - lon
     4215        kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes(
     4216            latitudes,longitudes,
     4217            -10,4,-10,31)
     4218
     4219        #print "kmin",kmin;print "kmax",kmax
     4220        #print "lmin",lmin;print "lmax",lmax
     4221        latitudes_new = latitudes[kmin:kmax]
     4222        longitudes_news = longitudes[lmin:lmax]
     4223        #print "latitudes_new", latitudes_new
     4224        #print "longitudes_news",longitudes_news
     4225        self.failUnless(latitudes == latitudes_new and \
     4226                        longitudes == longitudes_news,
     4227                         'failed')
     4228
     4229        ## 3rd test
     4230        kmin, kmax, lmin, lmax = data_manager._get_min_max_indexes(\
     4231            latitudes,
     4232            longitudes,
     4233            1.1,1.9,12,17)
     4234        #print "kmin",kmin;print "kmax",kmax
     4235        #print "lmin",lmin;print "lmax",lmax
     4236        latitudes_new = latitudes[kmin:kmax]
     4237        longitudes_news = longitudes[lmin:lmax]
     4238        #print "latitudes_new", latitudes_new
     4239        #print "longitudes_news",longitudes_news
     4240
     4241        self.failUnless(latitudes_new == [1, 2] and \
     4242                        longitudes_news == [10, 20],
    41734243                         'failed')
    41744244
     
    48064876        self.delete_mux(files)
    48074877        os.remove(sww_file)
     4878 
     4879    def test_urs2sww_minmaxlatlong(self):
     4880       
     4881        #longitudes = [150.66667, 150.83334, 151., 151.16667]
     4882        #latitudes = [-34.5, -34.33333, -34.16667, -34]
     4883
     4884        tide = 1
     4885        base_name, files = self.create_mux()
     4886        urs2sww(base_name,
     4887                minlat=-34.5,
     4888                maxlat=-34,
     4889                minlon= 150.66667,
     4890                maxlon= 151.16667,
     4891                mean_stage=tide,
     4892                remove_nc_files=False
     4893                )
     4894        sww_file = base_name + '.sww'
     4895       
     4896        #Let's interigate the sww file
     4897        # Note, the sww info is not gridded.  It is point data.
     4898        fid = NetCDFFile(sww_file)
     4899       
     4900
     4901        # Make x and y absolute
     4902        x = fid.variables['x'][:]
     4903        y = fid.variables['y'][:]
     4904        geo_reference = Geo_reference(NetCDFObject=fid)
     4905        points = geo_reference.get_absolute(map(None, x, y))
     4906        points = ensure_numeric(points)
     4907        x = points[:,0]
     4908        y = points[:,1]
     4909       
     4910        #Check that first coordinate is correctly represented       
     4911        #Work out the UTM coordinates for first point
     4912        zone, e, n = redfearn(-34.5, 150.66667)
     4913        assert allclose([x[0],y[0]], [e,n])
     4914
     4915       
     4916        #Check first value
     4917        stage = fid.variables['stage'][:]
     4918        xmomentum = fid.variables['xmomentum'][:]
     4919        ymomentum = fid.variables['ymomentum'][:]
     4920        elevation = fid.variables['elevation'][:]
     4921        assert allclose(stage[0,0], e +tide)  #Meters
     4922
     4923        #Check the momentums - ua
     4924        #momentum = velocity*(stage-elevation)
     4925        #momentum = velocity*(stage+elevation)
     4926        # -(-elevation) since elevation is inverted in mux files
     4927        # = n*(e+tide+n) based on how I'm writing these files
     4928        answer = n*(e+tide+n)
     4929        actual = xmomentum[0,0]
     4930        assert allclose(answer, actual)  #Meters
     4931
     4932        # check the stage values, first time step.
     4933        # These arrays are equal since the Easting values were used as
     4934        # the stage
     4935        assert allclose(stage[0], x +tide)  #Meters
     4936
     4937        # check the elevation values.
     4938        # -ve since urs measures depth, sww meshers height,
     4939        # these arrays are equal since the northing values were used as
     4940        # the elevation
     4941        assert allclose(-elevation, y)  #Meters
     4942       
     4943        fid.close()
     4944        self.delete_mux(files)
     4945        os.remove(sww_file)
    48084946       
    48094947    def test_lon_lat2grid(self):
     
    48775015#-------------------------------------------------------------
    48785016if __name__ == "__main__":
    4879     #suite = unittest.makeSuite(Test_Data_Manager,'test_lon')
     5017    #suite = unittest.makeSuite(Test_Data_Manager,'test_urs2sww_m')
     5018    #suite = unittest.makeSuite(Test_Data_Manager,'test_get_min_max_indexes_lat_ascending')
    48805019    #suite = unittest.makeSuite(Test_Data_Manager,'test_ferret2sww_lat_long')
    48815020    suite = unittest.makeSuite(Test_Data_Manager,'test')
Note: See TracChangeset for help on using the changeset viewer.