Changeset 4295


Ignore:
Timestamp:
Mar 6, 2007, 10:56:39 AM (17 years ago)
Author:
duncan
Message:

Adding mint, maxt to new URS2sww

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

Legend:

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

    r4292 r4295  
    45874587   
    45884588def urs_ungridded2sww(basename_in='o', basename_out=None, verbose=False,
    4589             #mint=None, maxt=None,
     4589            mint=None, maxt=None,
    45904590            mean_stage=0,
    45914591            origin = None,
     
    45944594    """
    45954595    parameters not used!
    4596     origin
    45974596    #mint=None, maxt=None,
    45984597    """
     
    46354634
    46364635    #mesh.export_mesh_file(basename_in + '.tsh')
    4637     times = []
     4636    # These are the times of the mux file
     4637    mux_times = []
    46384638    for i in range(a_mux.time_step_count):
    4639         times.append(a_mux.time_step * i)
     4639        mux_times.append(a_mux.time_step * i) 
     4640    mux_times_start_i, mux_times_fin_i = mux2sww_time(mux_times, mint, maxt)
     4641    times = mux_times[mux_times_start_i:mux_times_fin_i]
     4642   
     4643    if mux_times_start_i == mux_times_fin_i:
     4644        # Close the mux files
     4645        for quantity, file in map(None, quantities, files_in):
     4646            mux[quantity].close()
     4647        msg="Due to mint and maxt there's no time info in the boundary SWW."
     4648        assert 1==0 , msg #Happy to know a better way of doing this..
    46404649       
     4650    # If this raise is removed there is currently no downstream errors
     4651           
    46414652    points_utm=ensure_numeric(points_utm)
    46424653    #print "mesh_dic['generatedpointlist']", mesh_dic['generatedpointlist']
     
    46544665
    46554666    outfile = NetCDFFile(swwname, 'w')
    4656     # For a different way of doing this, check out tsh2sww
     4667    # For a different way of doing this, check out tsh2sww
     4668    # work out sww_times and the index range this covers
    46574669    write_sww_header(outfile, times, len(volumes), len(points_utm))
     4670    outfile.mean_stage = mean_stage
     4671    outfile.zscale = zscale
    46584672    write_sww_triangulation(outfile, points_utm, volumes,
    46594673                            elevation, zone,  origin=origin, verbose=verbose)
     
    46624676    j = 0
    46634677    # Read in a time slice from each mux file and write it to the sww file
    4664     for HA, UA, VA in map(None, mux['HA'], mux['UA'], mux['VA']):           
    4665         write_sww_time_slice(outfile, HA, UA, VA,  elevation, j,
    4666                               mean_stage=mean_stage, zscale=zscale,
    4667                               verbose=verbose)
     4678    for HA, UA, VA in map(None, mux['HA'], mux['UA'], mux['VA']):
     4679        if j >= mux_times_start_i and j < mux_times_fin_i:
     4680            write_sww_time_slice(outfile, HA, UA, VA,  elevation,
     4681                                 j - mux_times_start_i,
     4682                                 mean_stage=mean_stage, zscale=zscale,
     4683                                 verbose=verbose)
    46684684        j += 1
    46694685    outfile.close()
    46704686    #
    46714687    # Do some conversions while writing the sww file
     4688def mux2sww_time(mux_times, mint, maxt):
     4689    """
     4690    """
     4691
     4692    if mint == None:
     4693        mux_times_start_i = 0
     4694    else:
     4695        mux_times_start_i = searchsorted(mux_times, mint)
     4696       
     4697    if maxt == None:
     4698        mux_times_fin_i = len(mux_times)
     4699    else:
     4700        maxt += 0.5 # so if you specify a time where there is
     4701                    # data that time will be included
     4702        mux_times_fin_i = searchsorted(mux_times, maxt)
     4703
     4704    return mux_times_start_i, mux_times_fin_i
    46724705
    46734706def write_sww_header(outfile, times, number_of_volumes, number_of_points ):
     
    46794712    number_of_times = len(times)
    46804713   
    4681     #Create new file
    46824714    outfile.institution = 'Geoscience Australia'
    46834715    outfile.description = 'Converted from XXX'
     
    46894721
    46904722    #Start time in seconds since the epoch (midnight 1/1/1970)
    4691     outfile.starttime = starttime = times[0]
     4723    if len(times) == 0:
     4724        outfile.starttime = starttime = 0
     4725    else:
     4726        outfile.starttime = starttime = times[0]
    46924727
    46934728
     
    47754810
    47764811
    4777     # FIXME: Don't store the whole speeds and stage in  memory.
    4778     # block it?
    47794812def write_sww_time_slices(outfile, has, uas, vas, elevation,
    47804813                         mean_stage=0, zscale=1,
     
    47964829        j += 1
    47974830
    4798  
    47994831def write_sww_time_slice(outfile, ha, ua, va, elevation, slice_index,
    48004832                         mean_stage=0, zscale=1,
     
    48044836    xmomentum = outfile.variables['xmomentum']
    48054837    ymomentum = outfile.variables['ymomentum']
    4806 
    4807    
     4838   
    48084839    w = zscale*ha + mean_stage
    48094840    stage[slice_index] = w
     
    48114842    xmomentum[slice_index] = ua*h
    48124843    ymomentum[slice_index] = va*h
     4844
    48134845       
    48144846class Urs_points:
  • anuga_core/source/anuga/shallow_water/test_data_manager.py

    r4284 r4295  
    54365436        os.remove(sww_file)
    54375437       
     5438    def test_urs_ungridded2sww_mint_maxt (self):
     5439       
     5440        #Zone:   50   
     5441        #Easting:  240992.578  Northing: 7620442.472
     5442        #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
     5443        lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
     5444        time_step_count = 6
     5445        time_step = 100
     5446        tide = 9000000
     5447        base_name, files = self.write_mux(lat_long,
     5448                                          time_step_count, time_step)
     5449        urs_ungridded2sww(base_name, mean_stage=tide, origin =(50,23432,4343),
     5450                          mint=101, maxt=500)
     5451       
     5452        # now I want to check the sww file ...
     5453        sww_file = base_name + '.sww'
     5454       
     5455        #Let's interigate the sww file
     5456        # Note, the sww info is not gridded.  It is point data.
     5457        fid = NetCDFFile(sww_file)
     5458       
     5459        # Make x and y absolute
     5460        x = fid.variables['x'][:]
     5461        y = fid.variables['y'][:]
     5462        geo_reference = Geo_reference(NetCDFObject=fid)
     5463        points = geo_reference.get_absolute(map(None, x, y))
     5464        points = ensure_numeric(points)
     5465        x = points[:,0]
     5466        y = points[:,1]
     5467       
     5468        #Check that first coordinate is correctly represented       
     5469        #Work out the UTM coordinates for first point
     5470        zone, e, n = redfearn(lat_long[0][0], lat_long[0][1])
     5471        assert allclose([x[0],y[0]], [e,n])
     5472
     5473        #Check the time vector
     5474        times = fid.variables['time'][:]
     5475       
     5476        times_actual = [200,300,400,500]
     5477       
     5478        assert allclose(ensure_numeric(times),
     5479                        ensure_numeric(times_actual))
     5480       
     5481               #Check first value
     5482        stage = fid.variables['stage'][:]
     5483        xmomentum = fid.variables['xmomentum'][:]
     5484        ymomentum = fid.variables['ymomentum'][:]
     5485        elevation = fid.variables['elevation'][:]
     5486        assert allclose(stage[0,0], e +tide)  #Meters
     5487
     5488        #Check the momentums - ua
     5489        #momentum = velocity*(stage-elevation)
     5490        #momentum = velocity*(stage+elevation)
     5491        # -(-elevation) since elevation is inverted in mux files
     5492        # = n*(e+tide+n) based on how I'm writing these files
     5493        answer = n*(e+tide+n)
     5494        actual = xmomentum[0,0]
     5495        assert allclose(answer, actual)  #Meters
     5496
     5497        # check the stage values, first time step.
     5498        # These arrays are equal since the Easting values were used as
     5499        # the stage
     5500        assert allclose(stage[0], x +tide)  #Meters
     5501        # check the elevation values.
     5502        # -ve since urs measures depth, sww meshers height,
     5503        # these arrays are equal since the northing values were used as
     5504        # the elevation
     5505        assert allclose(-elevation, y)  #Meters
     5506       
     5507        fid.close()
     5508        self.delete_mux(files)
     5509        os.remove(sww_file)
     5510       
     5511    def test_urs_ungridded2sww_mint_maxtII (self):
     5512       
     5513        #Zone:   50   
     5514        #Easting:  240992.578  Northing: 7620442.472
     5515        #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
     5516        lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
     5517        time_step_count = 6
     5518        time_step = 100
     5519        tide = 9000000
     5520        base_name, files = self.write_mux(lat_long,
     5521                                          time_step_count, time_step)
     5522        urs_ungridded2sww(base_name, mean_stage=tide, origin =(50,23432,4343),
     5523                          mint=0, maxt=100000)
     5524       
     5525        # now I want to check the sww file ...
     5526        sww_file = base_name + '.sww'
     5527       
     5528        #Let's interigate the sww file
     5529        # Note, the sww info is not gridded.  It is point data.
     5530        fid = NetCDFFile(sww_file)
     5531       
     5532        # Make x and y absolute
     5533        geo_reference = Geo_reference(NetCDFObject=fid)
     5534        points = geo_reference.get_absolute(map(None, fid.variables['x'][:],
     5535                                                fid.variables['y'][:]))
     5536        points = ensure_numeric(points)
     5537        x = points[:,0]
     5538       
     5539        #Check the time vector
     5540        times = fid.variables['time'][:]
     5541       
     5542        times_actual = [0,100,200,300,400,500]
     5543        assert allclose(ensure_numeric(times),
     5544                        ensure_numeric(times_actual))
     5545       
     5546        #Check first value
     5547        stage = fid.variables['stage'][:]
     5548        assert allclose(stage[0], x +tide)
     5549       
     5550        fid.close()
     5551        self.delete_mux(files)
     5552        os.remove(sww_file)
     5553       
     5554    def test_urs_ungridded2sww_mint_maxtIII (self):
     5555       
     5556        #Zone:   50   
     5557        #Easting:  240992.578  Northing: 7620442.472
     5558        #Latitude:   -21  30 ' 0.00000 ''  Longitude: 114  30 ' 0.00000 ''
     5559        lat_long = [[-21.5,114.5],[-21,114.5],[-21,115]]
     5560        time_step_count = 6
     5561        time_step = 100
     5562        tide = 9000000
     5563        base_name, files = self.write_mux(lat_long,
     5564                                          time_step_count, time_step)
     5565        try:
     5566            urs_ungridded2sww(base_name, mean_stage=tide,
     5567                          origin =(50,23432,4343),
     5568                          mint=301, maxt=399)
     5569        except:
     5570            pass
     5571        else:
     5572            self.failUnless(0 ==1,  'Bad input did not throw exception error!')
     5573
     5574        self.delete_mux(files)
     5575       
    54385576    def test_URS_points_needed_and_urs_ungridded2sww(self):
    54395577        # This doesn't actually check anything
Note: See TracChangeset for help on using the changeset viewer.