Changeset 5358
- Timestamp:
- May 23, 2008, 10:30:53 AM (17 years ago)
- 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 4841 4841 4842 4842 file_params=-1*ones(3,Float)#[nsta,dt,nt] 4843 write= 1 #write txt files to current directory as well4843 write=0 #if true write txt files to current directory as well 4844 4844 data=read_mux2(1,filenames,weights,file_params,write) 4845 4845 … … 4866 4866 longitudes=zeros(data.shape[0],Float) 4867 4867 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) 4869 4869 for i in range(0,data.shape[0]): 4870 4870 latitudes[i]=data[i][data.shape[1]-OFFSET] 4871 4871 longitudes[i]=data[i][data.shape[1]-OFFSET+1] 4872 4872 elevation[i]=-data[i][data.shape[1]-OFFSET+2] 4873 stage[i]=data[i][:-OFFSET]4874 4875 return times, latitudes, longitudes, elevation, stage4873 quantity[i]=data[i][:-OFFSET] 4874 4875 return times, latitudes, longitudes, elevation, quantity 4876 4876 4877 4877 def mux2sww_time(mux_times, mint, maxt): … … 4940 4940 basename_in + NORTH_VELOCITY_MUX2_LABEL] 4941 4941 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 4942 4947 4943 4948 #need to do this for velocity-e-mux2 and velocity-n-mux2 files as well … … 4959 4964 4960 4965 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' 4961 4967 latitudes = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),latitudes_urs) 4962 4968 longitudes = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),longitudes_urs) … … 4967 4973 times=times_urs 4968 4974 4975 msg='File is empty and or clipped region not in file region' 4976 assert len(latitudes>0),msg 4977 4969 4978 number_of_points = latitudes.shape[0] 4970 4979 number_of_times = times.shape[0] … … 4996 5005 x[i] = easting 4997 5006 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 4999 5009 5000 5010 if origin is None: … … 5028 5038 5029 5039 outfile.close() 5030 5031 5040 5032 5041 class Write_sww: -
anuga_core/source/anuga/shallow_water/test_data_manager.py
r5347 r5358 5 5 import unittest 6 6 import copy 7 from Numeric import zeros, array, allclose, Float 7 from Numeric import zeros, array, allclose, Float, Int, ones,transpose 8 8 from anuga.utilities.numerical_tools import mean 9 9 import tempfile … … 76 76 domain.distribute_to_vertices_and_edges() 77 77 self.initial_stage = copy.copy(domain.quantities['stage'].vertex_values) 78 79 78 80 79 … … 5977 5976 os.remove(sww_file) 5978 5977 5979 5980 5978 def write_mux2(self,lat_long_points, time_step_count, time_step, 5981 depth=None, ha=None, ua=None, va=None5982 ):5979 first_tstep, last_tstep, 5980 depth=None, ha=None, ua=None, va=None): 5983 5981 """ 5984 5982 This will write 3 non-gridded mux files, for testing. … … 6002 6000 EAST_VELOCITY_MUX2_LABEL, 6003 6001 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 6004 6020 quantities_init = [[],[],[]] 6005 6021 # urs binary is latitude fastest 6006 for point in lat_long_points:6022 for i,point in enumerate(lat_long_points): 6007 6023 lat = point[0] 6008 6024 lon = point[1] … … 6011 6027 this_depth = n 6012 6028 else: 6013 this_depth = depth 6029 this_depth = depth[i] 6030 latlondeps.append([lat, lon, this_depth]) 6031 6014 6032 if ha is None: 6015 6033 this_ha = e 6034 quantities_init[0].append(ones(time_step_count,Float)*this_ha) # HA 6016 6035 else: 6017 this_ha = ha6036 quantities_init[0].append(ha[i]) 6018 6037 if ua is None: 6019 6038 this_ua = n 6039 quantities_init[1].append(ones(time_step_count,Float)*this_ua) # UA 6020 6040 else: 6021 this_ua = ua6041 quantities_init[1].append(ua[i]) 6022 6042 if va is None: 6023 this_va = e 6043 this_va = e 6044 quantities_init[2].append(ones(time_step_count,Float)*this_va) # 6024 6045 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]) 6030 6047 6031 6048 file_handle, base_name = tempfile.mkstemp("") … … 6034 6051 6035 6052 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) 6037 6055 quantities_init[i] = ensure_numeric(quantities_init[i]) 6038 #print "HA_init", HA_init6039 q_time = zeros((time_step_count, points_num), Float)6040 6056 for time in range(time_step_count): 6041 q_time[time,:] = quantities_init[i] #* time * 46057 q_time[time,:]=quantities_init[i][:,time] 6042 6058 6043 6059 #Write C files … … 6069 6085 f.write(pack('f',id)) 6070 6086 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 6087 6106 return base_name, files 6088 6107 6089 6108 def test_read_mux2_py(self): 6090 from Numeric import ones,Float 6109 """Constant stage,momentum at each gauge 6110 """ 6091 6111 tide = 1 6092 6112 time_step_count = 3 6093 6113 time_step = 2 6094 6114 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. 6100 6123 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) 6106 6130 6107 6131 weights=ones(1,Float) 6108 6132 #ensure that files are indeed mux2 files 6109 6133 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 6110 6141 self.delete_mux(files) 6111 6142 … … 6117 6148 for i,point in enumerate(lat_long_points): 6118 6149 assert allclose(latitudes[i],point[0]) and allclose(longitudes[i],point[1]),msg 6150 6119 6151 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) 6123 6289 6124 6290 def test_urs2sts(self): 6125 from Numeric import ones,Float 6126 tide = 1 6291 """tide = 1 6127 6292 time_step_count = 3 6128 6293 time_step = 2 … … 6133 6298 va=-10 #-ve added to take into account mux file format where south 6134 6299 # 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) 6141 6329 6142 6330 urs2sts(base_name,mean_stage=tide,verbose=False) … … 6162 6350 #Check that first coordinate is correctly represented 6163 6351 #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]) 6165 6353 assert allclose([x[0],y[0]], [e,n]) 6166 6354 … … 6180 6368 ymomentum = fid.variables['ymomentum'][:] 6181 6369 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 6184 6384 6185 6385 #Check the momentums - ua … … 6188 6388 #momentum = velocity_ua *(stage+depth) 6189 6389 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) 6195 6394 6196 6395 #Check the momentums - va … … 6199 6398 #momentum = velocity_va *(stage+depth) 6200 6399 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 6209 6402 # check the elevation values. 6210 6403 # -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 6214 6405 6215 6406 fid.close()
Note: See TracChangeset
for help on using the changeset viewer.