Changeset 5462 for anuga_core/source/anuga/shallow_water/data_manager.py
- Timestamp:
- Jul 4, 2008, 11:48:37 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/shallow_water/data_manager.py
r5452 r5462 4903 4903 def urs2sts(basename_in, basename_out = None, weights=None, 4904 4904 verbose = False, origin = None, 4905 mean_stage=0.0,zscale=1.0, 4905 mean_stage=0.0, zscale=1.0, 4906 ordering_filename = None, 4906 4907 minlat = None, maxlat = None, 4907 4908 minlon = None, maxlon = None): … … 4913 4914 origin is a 3-tuple with geo referenced 4914 4915 UTM coordinates (zone, easting, northing) 4916 4917 input: 4918 basename_in: list of source file prefixes 4919 4920 These are combined with the extensions: 4921 WAVEHEIGHT_MUX2_LABEL = '_waveheight-z-mux2' for stage 4922 EAST_VELOCITY_MUX2_LABEL = '_velocity-e-mux2' xmomentum 4923 NORTH_VELOCITY_MUX2_LABEL = '_velocity-n-mux2' and ymomentum 4924 4925 to create a 2D list of mux2 file. The rows are associated with each quantity and must have the above extensions 4926 the columns are the list of file prefixes. 4927 4928 ordering: a .txt file name specifying which mux2 gauge points are sto be stored. This is indicated by the index of 4929 the gauge in the ordering file. 4930 4931 ordering file format: 4932 header 4933 index,longitude,latitude 4934 4935 header='index,longitude,latitude\n' 4936 If ordering is None all points are taken in the order they appear in the mux2 file subject to rectangular clipping box (see below). 4937 4938 4939 minlat, minlon, maxlat, maxlon define a rectangular clipping box and can be used to extract gauge information in a rectangular box. This is useful for extracting 4940 information at gauge points not on the boundary. 4941 if ordering_filename is specified clipping box cannot be used 4942 4943 4944 output: 4945 baename_out: name of sts file in which mux2 data is stored. 4946 4947 4915 4948 """ 4916 4949 import os … … 4944 4977 precision = Float 4945 4978 4979 if ordering_filename is not None: 4980 msg = 'If ordering_filename is specified,' 4981 msg += ' rectangular clipping box cannot be specified as well. ' 4982 assert minlat is None and maxlat is None and\ 4983 minlon is None and maxlon is None, msg 4984 4985 4946 4986 msg = 'Must use latitudes and longitudes for minlat, maxlon etc' 4947 4948 4987 if minlat != None: 4949 4988 assert -90 < minlat < 90 , msg … … 4959 4998 assert maxlon > minlon 4960 4999 5000 5001 # Check output filename 4961 5002 if basename_out is None: 4962 stsname = basename_in[0] + '.sts' 4963 else: 4964 stsname = basename_out + '.sts' 5003 msg = 'STS filename must be specified' 5004 raise Exception, msg 5005 5006 stsname = basename_out + '.sts' 4965 5007 4966 5008 files_in=[[],[],[]] … … 4991 5033 mux={} 4992 5034 for i,quantity in enumerate(quantities): 4993 times_urs, latitudes_urs, longitudes_urs, elevation, mux[quantity],starttime = read_mux2_py(files_in[i],weights) 5035 # For each quantity read the associated list of source mux2 file with extenstion associated with that quantity 5036 times, latitudes_urs, longitudes_urs, elevation, mux[quantity],starttime = read_mux2_py(files_in[i],weights) 4994 5037 if quantity!=quantities[0]: 4995 5038 msg='%s, %s and %s have inconsitent gauge data'%(files_in[0],files_in[1],files_in[2]) 4996 assert allclose(times _urs,times_urs_old),msg5039 assert allclose(times,times_old),msg 4997 5040 assert allclose(latitudes_urs,latitudes_urs_old),msg 4998 5041 assert allclose(longitudes_urs,longitudes_urs_old),msg 4999 5042 assert allclose(elevation,elevation_old),msg 5000 5043 assert allclose(starttime,starttime_old) 5001 times_ urs_old=times_urs5044 times_old=times 5002 5045 latitudes_urs_old=latitudes_urs 5003 5046 longitudes_urs_old=longitudes_urs 5004 5047 elevation_old=elevation 5005 5048 starttime_old=starttime 5006 5007 if (minlat is not None) and (minlon is not None) and (maxlat is not None) and (maxlon is not None): 5049 5050 5051 if ordering_filename is not None: 5052 # Read ordering file 5053 5054 try: 5055 fid=open(ordering_filename, 'r') 5056 file_header=fid.readline().split(',') 5057 ordering_lines=fid.readlines() 5058 fid.close() 5059 except: 5060 msg = 'Cannot open %s'%ordering_filename 5061 raise Exception, msg 5062 5063 reference_header = 'index, longitude, latitude\n'.split(',') 5064 for i in range(3): 5065 if not file_header[i] == reference_header[i]: 5066 msg = 'File must contain header\n'+header+'\n' 5067 raise Exception, msg 5068 5069 if len(ordering_lines)<2: 5070 msg = 'File must contain at least two points' 5071 raise Exception, msg 5072 5073 5074 5075 permutation = [int(line.split(',')[0]) for line in ordering_lines] 5076 5077 latitudes = take(latitudes_urs, permutation) 5078 longitudes = take(longitudes_urs, permutation) 5079 5080 # Self check - can be removed to improve speed 5081 ref_longitudes = [float(line.split(',')[1]) for line in ordering_lines] 5082 ref_latitudes = [float(line.split(',')[2]) for line in ordering_lines] 5083 5084 msg = 'Longitudes specified in ordering file do not match those found in mux files' 5085 msg += 'I got %s instead of %s (only beginning shown)'\ 5086 %(str(longitudes[:10]) + '...', 5087 str(ref_longitudes[:10]) + '...') 5088 assert allclose(longitudes, ref_longitudes), msg 5089 5090 msg = 'Latitudes specified in ordering file do not match those found in mux files. ' 5091 msg += 'I got %s instead of %s (only beginning shown)'\ 5092 %(str(latitudes[:10]) + '...', 5093 str(ref_latitudes[:10]) + '...') 5094 assert allclose(latitudes, ref_latitudes), msg 5095 5096 elif (minlat is not None) and (minlon is not None) and (maxlat is not None) and (maxlon is not None): 5097 #FIXME - this branch is probably not working 5008 5098 if verbose: print 'Cliping urs data' 5009 5099 latitudes = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),latitudes_urs) 5010 5100 longitudes = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),longitudes_urs) 5011 times = compress((latitudes_urs>=minlat)&(latitudes_urs<=maxlat)&(longitudes_urs>=minlon)&(longitudes_urs<=maxlon),times_urs) 5101 5102 permutation = range(latitudes.shape[0]) 5103 5104 msg = 'Clipping is not done yet' 5105 raise Exception, msg 5106 5012 5107 else: 5013 5108 latitudes=latitudes_urs 5014 5109 longitudes=longitudes_urs 5015 times=times_urs 5110 permutation = range(latitudes.shape[0]) 5111 5016 5112 5017 5113 msg='File is empty and or clipped region not in file region' … … 5032 5128 #starttime = times[0] 5033 5129 sts = Write_sts() 5034 sts.store_header(outfile, times+starttime,number_of_points, description=description, 5035 verbose=verbose,sts_precision=Float) 5130 sts.store_header(outfile, 5131 times+starttime, 5132 number_of_points, 5133 description=description, 5134 verbose=verbose, 5135 sts_precision=Float) 5036 5136 5037 5137 # Store … … 5067 5167 stage = outfile.variables['stage'] 5068 5168 xmomentum = outfile.variables['xmomentum'] 5069 ymomentum = outfile.variables['ymomentum']5169 ymomentum = outfile.variables['ymomentum'] 5070 5170 5071 5171 if verbose: print 'Converting quantities' 5072 5172 for j in range(len(times)): 5073 for i in range(number_of_points):5074 w = zscale*mux['HA'][i ,j] + mean_stage5075 h=w-elevation[i ]5173 for i, index in enumerate(permutation): 5174 w = zscale*mux['HA'][index,j] + mean_stage 5175 h=w-elevation[index] 5076 5176 stage[j,i] = w 5077 #delete following two lines once velcotu files are read in. 5078 xmomentum[j,i] = mux['UA'][i ,j]*h5079 ymomentum[j,i] = mux['VA'][i ,j]*h5177 5178 xmomentum[j,i] = mux['UA'][index,j]*h 5179 ymomentum[j,i] = mux['VA'][index,j]*h 5080 5180 5081 5181 outfile.close() … … 5119 5219 xllcorner = fid.xllcorner[0] 5120 5220 yllcorner = fid.yllcorner[0] 5121 #Points stored in sts file are norma ilsed to [xllcorner,yllcorner] but5221 #Points stored in sts file are normalised to [xllcorner,yllcorner] but 5122 5222 #we cannot assume that boundary polygon will be. At least the 5123 5223 #additional points specified by the user after this function is called … … 5132 5232 yllcorner = fid.yllcorner[0] 5133 5233 5234 # Walk through ordering file 5134 5235 boundary_polygon=[] 5135 5236 for line in lines: … … 5139 5240 zone,easting,northing=redfearn(float(fields[1]),float(fields[2])) 5140 5241 if not zone == fid.zone[0]: 5141 msg = 'Inconsi tent zones'5242 msg = 'Inconsistent zones' 5142 5243 raise Exception, msg 5143 5244 else: 5144 5245 easting = float(fields[1]) 5145 5246 northing = float(fields[2]) 5247 5146 5248 if not allclose(array([easting,northing]),sts_points[index]): 5147 5249 msg = "Points in sts file do not match those in the"+\ 5148 ".txt file sp cifying the order of the boundary points"5250 ".txt file specifying the order of the boundary points" 5149 5251 raise Exception, msg 5150 5252 boundary_polygon.append(sts_points[index].tolist())
Note: See TracChangeset
for help on using the changeset viewer.