Changeset 5358


Ignore:
Timestamp:
May 23, 2008, 10:30:53 AM (16 years ago)
Author:
jakeman
Message:

Added new unit tests for urs2sts

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

Legend:

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

    r5352 r5358  
    48414841
    48424842    file_params=-1*ones(3,Float)#[nsta,dt,nt]
    4843     write=1 #write txt files to current directory as well
     4843    write=0 #if true write txt files to current directory as well
    48444844    data=read_mux2(1,filenames,weights,file_params,write)
    48454845
     
    48664866    longitudes=zeros(data.shape[0],Float)
    48674867    elevation=zeros(data.shape[0],Float)
    4868     stage=zeros((data.shape[0],data.shape[1]-OFFSET),Float)
     4868    quantity=zeros((data.shape[0],data.shape[1]-OFFSET),Float)
    48694869    for i in range(0,data.shape[0]):
    48704870        latitudes[i]=data[i][data.shape[1]-OFFSET]
    48714871        longitudes[i]=data[i][data.shape[1]-OFFSET+1]
    48724872        elevation[i]=-data[i][data.shape[1]-OFFSET+2]
    4873         stage[i]=data[i][:-OFFSET]
    4874 
    4875     return times, latitudes, longitudes, elevation, stage
     4873        quantity[i]=data[i][:-OFFSET]
     4874
     4875    return times, latitudes, longitudes, elevation, quantity
    48764876
    48774877def mux2sww_time(mux_times, mint, maxt):
     
    49404940                basename_in + NORTH_VELOCITY_MUX2_LABEL]
    49414941    quantities = ['HA','UA','VA']
     4942
     4943    for file_in in files_in:
     4944        if (os.access(file_in, os.F_OK) == 0):
     4945            msg = 'File %s does not exist or is not accessible' %file_in
     4946            raise IOError, msg
    49424947
    49434948    #need to do this for velocity-e-mux2 and velocity-n-mux2 files as well
     
    49594964       
    49604965    if (minlat is not None) and (minlon is not None) and (maxlat is not None) and (maxlon is not None):
     4966        if verbose: print 'Cliiping urs data'
    49614967        latitudes = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),latitudes_urs)
    49624968        longitudes = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),longitudes_urs)
     
    49674973        times=times_urs
    49684974
     4975    msg='File is empty and or clipped region not in file region'
     4976    assert len(latitudes>0),msg
     4977
    49694978    number_of_points = latitudes.shape[0]
    49704979    number_of_times = times.shape[0]
     
    49965005        x[i] = easting
    49975006        y[i] = northing
    4998         #print zone,easting,northing
     5007        msg='all sts gauges need to be in the same zone'
     5008        assert zone==refzone,msg
    49995009
    50005010    if origin is None:
     
    50285038
    50295039    outfile.close()
    5030 
    50315040
    50325041class Write_sww:
  • anuga_core/source/anuga/shallow_water/test_data_manager.py

    r5347 r5358  
    55import unittest
    66import copy
    7 from Numeric import zeros, array, allclose, Float
     7from Numeric import zeros, array, allclose, Float, Int, ones,transpose
    88from anuga.utilities.numerical_tools import mean
    99import tempfile
     
    7676        domain.distribute_to_vertices_and_edges()               
    7777        self.initial_stage = copy.copy(domain.quantities['stage'].vertex_values)
    78 
    7978
    8079
     
    59775976        os.remove(sww_file)
    59785977       
    5979 
    59805978    def write_mux2(self,lat_long_points, time_step_count, time_step,
    5981                   depth=None, ha=None, ua=None, va=None
    5982                   ):
     5979                   first_tstep, last_tstep,
     5980                   depth=None, ha=None, ua=None, va=None):
    59835981        """
    59845982        This will write 3 non-gridded mux files, for testing.
     
    60026000                     EAST_VELOCITY_MUX2_LABEL,
    60036001                     NORTH_VELOCITY_MUX2_LABEL]
     6002
     6003        msg='first_tstep and last_step arrays must have same length as number of points'
     6004        assert len(first_tstep)==points_num,msg
     6005        assert len(last_tstep)==points_num,msg
     6006
     6007        if depth is not None:
     6008            depth=ensure_numeric(depth)
     6009            assert len(depth)==points_num
     6010        if ha is not None:
     6011            ha=ensure_numeric(ha)
     6012            assert ha.shape==(points_num,time_step_count)
     6013        if ua is not None:
     6014            ua=ensure_numeric(ua)
     6015            assert ua.shape==(points_num,time_step_count)
     6016        if va is not None:
     6017            va=ensure_numeric(va)
     6018            assert va.shape==(points_num,time_step_count)
     6019
    60046020        quantities_init = [[],[],[]]
    60056021        # urs binary is latitude fastest
    6006         for point in lat_long_points:
     6022        for i,point in enumerate(lat_long_points):
    60076023            lat = point[0]
    60086024            lon = point[1]
     
    60116027                this_depth = n
    60126028            else:
    6013                 this_depth = depth
     6029                this_depth = depth[i]
     6030            latlondeps.append([lat, lon, this_depth])
     6031
    60146032            if ha is None:
    60156033                this_ha = e
     6034                quantities_init[0].append(ones(time_step_count,Float)*this_ha) # HA
    60166035            else:
    6017                 this_ha = ha
     6036                quantities_init[0].append(ha[i])
    60186037            if ua is None:
    60196038                this_ua = n
     6039                quantities_init[1].append(ones(time_step_count,Float)*this_ua) # UA
    60206040            else:
    6021                 this_ua = ua
     6041                quantities_init[1].append(ua[i])
    60226042            if va is None:
    6023                 this_va = e   
     6043                this_va = e
     6044                quantities_init[2].append(ones(time_step_count,Float)*this_va) #
    60246045            else:
    6025                 this_va = va         
    6026             latlondeps.append([lat, lon, this_depth])
    6027             quantities_init[0].append(this_ha) # HA
    6028             quantities_init[1].append(this_ua) # UA
    6029             quantities_init[2].append(this_va) # VA
     6046                quantities_init[2].append(va[i])           
    60306047
    60316048        file_handle, base_name = tempfile.mkstemp("")
     
    60346051
    60356052        files = []       
    6036         for i,q in enumerate(quantities):
     6053        for i,q in enumerate(quantities):
     6054            q_time = zeros((time_step_count, points_num), Float)
    60376055            quantities_init[i] = ensure_numeric(quantities_init[i])
    6038             #print "HA_init", HA_init
    6039             q_time = zeros((time_step_count, points_num), Float)
    60406056            for time in range(time_step_count):
    6041                 q_time[time,:] = quantities_init[i] #* time * 4
     6057                q_time[time,:]=quantities_init[i][:,time]
    60426058
    60436059            #Write C files
     
    60696085                    f.write(pack('f',id))   
    60706086
    6071             first_tstep=1
    6072             last_tstep=time_step_count
    6073             for latlondep in latlondeps:
    6074                 f.write(pack('i',first_tstep))
    6075             for latlondep in latlondeps:
    6076                 f.write(pack('i',last_tstep))
    6077 
    6078             # Write quantity info
    6079 
    6080             for time in  range(time_step_count):
    6081                 #first timestep always assumed to be zero
    6082                 f.write(pack('f',0.0))
    6083                 for point_i in range(points_num):
    6084                     f.write(pack('f',q_time[time,point_i]))
    6085 
    6086             f.close()
     6087            #first_tstep=1
     6088            #last_tstep=time_step_count
     6089            for i,latlondep in enumerate(latlondeps):
     6090                f.write(pack('i',first_tstep[i]))
     6091            for i,latlondep in enumerate(latlondeps):
     6092                f.write(pack('i',last_tstep[i]))
     6093
     6094            # Find when first station starts recording
     6095            min_tstep = min(first_tstep)
     6096            # Find when all stations have stoped recording
     6097            max_tstep = max(last_tstep)
     6098
     6099            #for time in  range(time_step_count):
     6100            for time in range(min_tstep-1,max_tstep):
     6101                    f.write(pack('f',time*time_step))               
     6102                    for point_i in range(points_num):
     6103                        if time+1>=first_tstep[point_i] and time+1<=last_tstep[point_i]:
     6104                            f.write(pack('f',q_time[time,point_i]))
     6105
    60876106        return base_name, files
    60886107
    60896108    def test_read_mux2_py(self):
    6090         from Numeric import ones,Float
     6109        """Constant stage,momentum at each gauge
     6110        """
    60916111        tide = 1
    60926112        time_step_count = 3
    60936113        time_step = 2
    60946114        lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)]
    6095         depth=20
    6096         ha=2
    6097         ua=5
    6098         va=-10 #-ve added to take into account mux file format where south
    6099                # is positive.
     6115        n=len(lat_long_points)
     6116        first_tstep=ones(n,Int)
     6117        last_tstep=time_step_count*ones(n,Int)
     6118        depth=20*ones(n,Float)
     6119        ha=2*ones((n,time_step_count),Float)
     6120        ua=5*ones((n,time_step_count),Float)
     6121        va=-10*ones((n,time_step_count),Float)
     6122        #-ve added to take into account mux file format where south is positive.
    61006123        base_name, files = self.write_mux2(lat_long_points,
    6101                                      time_step_count, time_step,
    6102                                      depth=depth,
    6103                                      ha=ha,
    6104                                      ua=ua,
    6105                                      va=va)
     6124                                      time_step_count, time_step,
     6125                                      first_tstep, last_tstep,
     6126                                      depth=depth,
     6127                                      ha=ha,
     6128                                      ua=ua,
     6129                                      va=va)
    61066130
    61076131        weights=ones(1,Float)
    61086132        #ensure that files are indeed mux2 files
    61096133        times, latitudes, longitudes, elevation, stage=read_mux2_py([files[0]],weights)
     6134        ua_times, ua_latitudes, ua_longitudes, ua_elevation, xvelocity=read_mux2_py([files[1]],weights)
     6135        msg='ha and ua have different gauge meta data'
     6136        assert allclose(times,ua_times) and allclose(latitudes,ua_latitudes) and allclose(longitudes,ua_longitudes) and allclose(elevation,ua_elevation),msg
     6137        va_times, va_latitudes, va_longitudes, va_elevation, yvelocity=read_mux2_py([files[2]],weights)
     6138        msg='ha and va have different gauge meta data'
     6139        assert allclose(times,va_times) and allclose(latitudes,va_latitudes) and allclose(longitudes,va_longitudes) and allclose(elevation,va_elevation),msg
     6140
    61106141        self.delete_mux(files)
    61116142
     
    61176148        for i,point in enumerate(lat_long_points):
    61186149            assert allclose(latitudes[i],point[0]) and allclose(longitudes[i],point[1]),msg
     6150
    61196151        msg='Incorrect gauge depths returned'
    6120         assert allclose(elevation,-depth*ones(len(lat_long_points),Float)),msg
    6121         msg='incorrect gauge time series returned'
    6122         assert allclose(stage,ha*ones((len(lat_long_points),time_step_count),Float))
     6152        assert allclose(elevation,-depth),msg
     6153        msg='incorrect gauge height time series returned'
     6154        assert allclose(stage,ha)
     6155        msg='incorrect gauge ua time series returned'
     6156        assert allclose(xvelocity,ua)
     6157        msg='incorrect gauge va time series returned'
     6158        assert allclose(yvelocity,va)
     6159
     6160    def test_read_mux2_py2(self):
     6161        """Spatially varing stage
     6162        """
     6163        tide = 1
     6164        time_step_count = 3
     6165        time_step = 2
     6166        lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)]
     6167        n=len(lat_long_points)
     6168        first_tstep=ones(n,Int)
     6169        last_tstep=(time_step_count)*ones(n,Int)
     6170        depth=20*ones(n,Float)
     6171        ha=2*ones((n,time_step_count),Float)
     6172        ha[0]=arange(0,time_step_count)+1
     6173        ha[1]=time_step_count-arange(1,time_step_count+1)
     6174        ha[1]=arange(time_step_count,2*time_step_count)
     6175        ha[2]=arange(2*time_step_count,3*time_step_count)
     6176        ha[3]=arange(3*time_step_count,4*time_step_count)
     6177        ua=5*ones((n,time_step_count),Float)
     6178        va=-10*ones((n,time_step_count),Float)
     6179        #-ve added to take into account mux file format where south is positive.
     6180        base_name, files = self.write_mux2(lat_long_points,
     6181                                      time_step_count, time_step,
     6182                                      first_tstep, last_tstep,
     6183                                      depth=depth,
     6184                                      ha=ha,
     6185                                      ua=ua,
     6186                                      va=va)
     6187
     6188        weights=ones(1,Float)
     6189        #ensure that files are indeed mux2 files
     6190        times, latitudes, longitudes, elevation, stage=read_mux2_py([files[0]],weights)
     6191        ua_times, ua_latitudes, ua_longitudes, ua_elevation, xvelocity=read_mux2_py([files[1]],weights)
     6192        msg='ha and ua have different gauge meta data'
     6193        assert allclose(times,ua_times) and allclose(latitudes,ua_latitudes) and allclose(longitudes,ua_longitudes) and allclose(elevation,ua_elevation),msg
     6194        va_times, va_latitudes, va_longitudes, va_elevation, yvelocity=read_mux2_py([files[2]],weights)
     6195        msg='ha and va have different gauge meta data'
     6196        assert allclose(times,va_times) and allclose(latitudes,va_latitudes) and allclose(longitudes,va_longitudes) and allclose(elevation,va_elevation),msg
     6197
     6198
     6199        self.delete_mux(files)
     6200
     6201        msg='time array has incorrect length'
     6202        #assert times.shape[0]==time_step_count,msg
     6203        msg = 'time array is incorrect'
     6204        #assert allclose(times,time_step*arange(1,time_step_count+1)),msg
     6205        msg='Incorrect gauge positions returned'
     6206        for i,point in enumerate(lat_long_points):
     6207            assert allclose(latitudes[i],point[0]) and allclose(longitudes[i],point[1]),msg
     6208
     6209        msg='Incorrect gauge depths returned'
     6210        assert allclose(elevation,-depth),msg
     6211        msg='incorrect gauge height time series returned'
     6212        assert allclose(stage,ha)
     6213        msg='incorrect gauge ua time series returned'
     6214        assert allclose(xvelocity,ua)
     6215        msg='incorrect gauge va time series returned'
     6216        assert allclose(yvelocity,va)
     6217
     6218    def test_read_mux2_py3(self):
     6219        """Varying start and finsh times
     6220        """
     6221        tide = 1
     6222        time_step_count = 3
     6223        time_step = 2
     6224        lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)]
     6225        n=len(lat_long_points)
     6226        first_tstep=ones(n,Int)
     6227        first_tstep[0]+=1
     6228        first_tstep[2]+=1
     6229        last_tstep=(time_step_count)*ones(n,Int)
     6230        last_tstep[0]-=1
     6231
     6232        depth=20*ones(n,Float)
     6233        ha=2*ones((n,time_step_count),Float)
     6234        ha[0]=arange(0,time_step_count)
     6235        ha[1]=arange(time_step_count,2*time_step_count)
     6236        ha[2]=arange(2*time_step_count,3*time_step_count)
     6237        ha[3]=arange(3*time_step_count,4*time_step_count)
     6238        ua=5*ones((n,time_step_count),Float)
     6239        va=-10*ones((n,time_step_count),Float)
     6240        #-ve added to take into account mux file format where south is positive.
     6241        base_name, files = self.write_mux2(lat_long_points,
     6242                                      time_step_count, time_step,
     6243                                      first_tstep, last_tstep,
     6244                                      depth=depth,
     6245                                      ha=ha,
     6246                                      ua=ua,
     6247                                      va=va)
     6248
     6249        weights=ones(1,Float)
     6250        #ensure that files are indeed mux2 files
     6251        times, latitudes, longitudes, elevation, stage=read_mux2_py([files[0]],weights)
     6252        ua_times, ua_latitudes, ua_longitudes, ua_elevation, xvelocity=read_mux2_py([files[1]],weights)
     6253        msg='ha and ua have different gauge meta data'
     6254        assert allclose(times,ua_times) and allclose(latitudes,ua_latitudes) and allclose(longitudes,ua_longitudes) and allclose(elevation,ua_elevation),msg
     6255        va_times, va_latitudes, va_longitudes, va_elevation, yvelocity=read_mux2_py([files[2]],weights)
     6256        msg='ha and va have different gauge meta data'
     6257        assert allclose(times,va_times) and allclose(latitudes,va_latitudes) and allclose(longitudes,va_longitudes) and allclose(elevation,va_elevation),msg
     6258
     6259        self.delete_mux(files)
     6260
     6261        msg='time array has incorrect length'
     6262        #assert times.shape[0]==time_step_count,msg
     6263        msg = 'time array is incorrect'
     6264        #assert allclose(times,time_step*arange(1,time_step_count+1)),msg
     6265        msg='Incorrect gauge positions returned'
     6266        for i,point in enumerate(lat_long_points):
     6267            assert allclose(latitudes[i],point[0]) and allclose(longitudes[i],point[1]),msg
     6268
     6269
     6270        # Set original data used to write mux file to be zero when gauges are
     6271        #not recdoring
     6272        ha[0][0]=0.0
     6273        ha[0][time_step_count-1]=0.0;
     6274        ha[2][0]=0.0;
     6275        ua[0][0]=0.0
     6276        ua[0][time_step_count-1]=0.0;
     6277        ua[2][0]=0.0;
     6278        va[0][0]=0.0
     6279        va[0][time_step_count-1]=0.0;
     6280        va[2][0]=0.0;
     6281        msg='Incorrect gauge depths returned'
     6282        assert allclose(elevation,-depth),msg
     6283        msg='incorrect gauge height time series returned'
     6284        assert allclose(stage,ha)
     6285        msg='incorrect gauge ua time series returned'
     6286        assert allclose(xvelocity,ua)
     6287        msg='incorrect gauge va time series returned'
     6288        assert allclose(yvelocity,va)
    61236289
    61246290    def test_urs2sts(self):
    6125         from Numeric import ones,Float
    6126         tide = 1
     6291        """tide = 1
    61276292        time_step_count = 3
    61286293        time_step = 2
     
    61336298        va=-10 #-ve added to take into account mux file format where south
    61346299               # is positive.
    6135         base_name, files = self.write_mux2(lat_long,
    6136                                      time_step_count, time_step,
    6137                                      depth=depth,
    6138                                      ha=ha,
    6139                                      ua=ua,
    6140                                      va=va)
     6300        """
     6301        #tide = 1
     6302        tide=0
     6303        time_step_count = 3
     6304        time_step = 2
     6305        lat_long_points =[(-21.5,114.5),(-21,114.5),(-21.5,115), (-21.,115.)]
     6306        n=len(lat_long_points)
     6307        first_tstep=ones(n,Int)
     6308        first_tstep[0]+=1
     6309        first_tstep[2]+=1
     6310        last_tstep=(time_step_count)*ones(n,Int)
     6311        last_tstep[0]-=1
     6312
     6313        gauge_depth=20*ones(n,Float)
     6314        ha=2*ones((n,time_step_count),Float)
     6315        ha[0]=arange(0,time_step_count)
     6316        ha[1]=arange(time_step_count,2*time_step_count)
     6317        ha[2]=arange(2*time_step_count,3*time_step_count)
     6318        ha[3]=arange(3*time_step_count,4*time_step_count)
     6319        ua=5*ones((n,time_step_count),Float)
     6320        va=-10*ones((n,time_step_count),Float)
     6321        #-ve added to take into account mux file format where south is positive.
     6322        base_name, files = self.write_mux2(lat_long_points,
     6323                                      time_step_count, time_step,
     6324                                      first_tstep, last_tstep,
     6325                                      depth=gauge_depth,
     6326                                      ha=ha,
     6327                                      ua=ua,
     6328                                      va=va)
    61416329
    61426330        urs2sts(base_name,mean_stage=tide,verbose=False)
     
    61626350        #Check that first coordinate is correctly represented       
    61636351        #Work out the UTM coordinates for first point
    6164         zone, e, n = redfearn(lat_long[0][0], lat_long[0][1])
     6352        zone, e, n = redfearn(lat_long_points[0][0], lat_long_points[0][1])
    61656353        assert allclose([x[0],y[0]], [e,n])
    61666354
     
    61806368        ymomentum = fid.variables['ymomentum'][:]
    61816369        elevation = fid.variables['elevation'][:]
    6182         assert allclose(stage[0,0], ha+tide)  #Meters
    6183 
     6370
     6371        # Set original data used to write mux file to be zero when gauges are
     6372        #not recdoring
     6373        ha[0][0]=0.0
     6374        ha[0][time_step_count-1]=0.0;
     6375        ha[2][0]=0.0;
     6376        ua[0][0]=0.0
     6377        ua[0][time_step_count-1]=0.0;
     6378        ua[2][0]=0.0;
     6379        va[0][0]=0.0
     6380        va[0][time_step_count-1]=0.0;
     6381        va[2][0]=0.0;
     6382
     6383        assert allclose(transpose(ha),stage)  #Meters
    61846384
    61856385        #Check the momentums - ua
     
    61886388        #momentum = velocity_ua *(stage+depth)
    61896389
    6190         answer_x = ua*(ha+tide+depth)
    6191         actual_x = xmomentum[0,0]
    6192         #print "answer_x",answer_x
    6193         #print "actual_x",actual_x
    6194         assert allclose(answer_x, actual_x)  #Meters
     6390        depth=zeros((len(lat_long_points),time_step_count),Float)
     6391        for i in range(len(lat_long_points)):
     6392            depth[i]=gauge_depth[i]+tide+ha[i]
     6393        assert allclose(transpose(ua*depth),xmomentum)
    61956394
    61966395        #Check the momentums - va
     
    61996398        #momentum = velocity_va *(stage+depth)
    62006399
    6201         answer_y = va*(ha+tide+depth)
    6202         actual_y = ymomentum[0,0]
    6203         #print "answer_y",answer_y
    6204         #print "actual_y",actual_y
    6205         assert allclose(answer_y, actual_y)  #Meters
    6206 
    6207         # check the stage values, first time step.
    6208         assert allclose(stage[0], ha +tide)  #Meters
     6400        assert allclose(transpose(va*depth),ymomentum)
     6401
    62096402        # check the elevation values.
    62106403        # -ve since urs measures depth, sww meshers height,
    6211         # these arrays are equal since the northing values were used as
    6212         # the elevation
    6213         assert allclose(-elevation, depth)  #Meters
     6404        assert allclose(-elevation, gauge_depth)  #Meters
    62146405
    62156406        fid.close()
Note: See TracChangeset for help on using the changeset viewer.