Changeset 5473
- Timestamp:
- Jul 7, 2008, 3:34:26 PM (15 years ago)
- Location:
- anuga_core/source/anuga/shallow_water
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/shallow_water/data_manager.py
r5470 r5473 4838 4838 NORTH_VELOCITY_MUX2_LABEL = '-n-mux2' 4839 4839 4840 def read_mux2_py(filenames,weights,verbose=False): 4841 4840 def read_mux2_py(filenames,weights,permutation=None,verbose=False): 4841 """ 4842 Access the mux files specified in the filenames list. Combine the 4843 data found therin as a weighted linear sum as specifed by the weights. 4844 If permutation is None extract timeseries data for all gauges within the 4845 files. 4846 4847 Input: 4848 filenames: List of filenames specifiying the file containing the 4849 timeseries data (mux2 format) for each source 4850 weights: Weights associated with each source 4851 permutation: Specifies the gauge numbers that for which data is to be 4852 extracted 4853 """ 4842 4854 from Numeric import ones,Float,compress,zeros,arange 4843 4855 from urs_ext import read_mux2 … … 4853 4865 verbose=0 4854 4866 4855 data=read_mux2(numSrc,filenames,weights,file_params, verbose)4867 data=read_mux2(numSrc,filenames,weights,file_params,permutation,verbose) 4856 4868 4857 4869 msg='File parameter values were not read in correctly from c file' … … 5034 5046 raise IOError, msg 5035 5047 5036 #need to do this for velocity-e-mux2 and velocity-n-mux2 files as well 5037 #for now set x_momentum and y_moentum quantities to zero 5048 if ordering_filename is not None: 5049 # Read ordering file 5050 5051 try: 5052 fid=open(ordering_filename, 'r') 5053 file_header=fid.readline().split(',') 5054 ordering_lines=fid.readlines() 5055 fid.close() 5056 except: 5057 msg = 'Cannot open %s'%ordering_filename 5058 raise Exception, msg 5059 5060 reference_header = 'index, longitude, latitude\n' 5061 reference_header_split = reference_header.split(',') 5062 for i in range(3): 5063 if not file_header[i] == reference_header_split[i]: 5064 msg = 'File must contain header: '+reference_header+'\n' 5065 raise Exception, msg 5066 5067 if len(ordering_lines)<2: 5068 msg = 'File must contain at least two points' 5069 raise Exception, msg 5070 5071 5072 5073 permutation = [int(line.split(',')[0]) for line in ordering_lines] 5074 else: 5075 permutation = None 5076 5038 5077 if (verbose): print 'reading mux2 file' 5039 5078 mux={} 5040 5079 for i,quantity in enumerate(quantities): 5041 5080 # For each quantity read the associated list of source mux2 file with extenstion associated with that quantity 5042 times, latitudes_urs, longitudes_urs, elevation, mux[quantity],starttime = read_mux2_py(files_in[i],weights, verbose=verbose)5081 times, latitudes_urs, longitudes_urs, elevation, mux[quantity],starttime = read_mux2_py(files_in[i],weights,permutation=permutation,verbose=verbose) 5043 5082 if quantity!=quantities[0]: 5044 5083 msg='%s, %s and %s have inconsitent gauge data'%(files_in[0],files_in[1],files_in[2]) … … 5053 5092 elevation_old=elevation 5054 5093 starttime_old=starttime 5055 5056 5094 5057 5095 if ordering_filename is not None: 5058 # Read ordering file 5059 5060 try: 5061 fid=open(ordering_filename, 'r') 5062 file_header=fid.readline().split(',') 5063 ordering_lines=fid.readlines() 5064 fid.close() 5065 except: 5066 msg = 'Cannot open %s'%ordering_filename 5067 raise Exception, msg 5068 5069 reference_header = 'index, longitude, latitude\n' 5070 reference_header_split = reference_header.split(',') 5071 for i in range(3): 5072 if not file_header[i] == reference_header_split[i]: 5073 msg = 'File must contain header: '+reference_header+'\n' 5074 raise Exception, msg 5075 5076 if len(ordering_lines)<2: 5077 msg = 'File must contain at least two points' 5078 raise Exception, msg 5079 5080 5081 5082 permutation = [int(line.split(',')[0]) for line in ordering_lines] 5083 5084 latitudes = take(latitudes_urs, permutation) 5085 longitudes = take(longitudes_urs, permutation) 5096 latitudes = latitudes_urs 5097 longitudes = longitudes_urs 5086 5098 5087 5099 # Self check - can be removed to improve speed … … 5116 5128 longitudes=longitudes_urs 5117 5129 permutation = range(latitudes.shape[0]) 5118 5119 5130 5120 5131 msg='File is empty and or clipped region not in file region' … … 5178 5189 if verbose: print 'Converting quantities' 5179 5190 for j in range(len(times)): 5180 for i , index in enumerate(permutation):5181 w = zscale*mux['HA'][i ndex,j] + mean_stage5182 h=w-elevation[i ndex]5191 for i in range(number_of_points): 5192 w = zscale*mux['HA'][i,j] + mean_stage 5193 h=w-elevation[i] 5183 5194 stage[j,i] = w 5184 5195 5185 xmomentum[j,i] = mux['UA'][i ndex,j]*h5186 ymomentum[j,i] = mux['VA'][i ndex,j]*h5196 xmomentum[j,i] = mux['UA'][i,j]*h 5197 ymomentum[j,i] = mux['VA'][i,j]*h 5187 5198 5188 5199 outfile.close() -
anuga_core/source/anuga/shallow_water/test_data_manager.py
r5466 r5473 6105 6105 return base_name, files 6106 6106 6107 def test_read_mux2_py I(self):6107 def test_read_mux2_py0(self): 6108 6108 """Constant stage,momentum at each gauge 6109 6109 """ … … 6543 6543 """Test multiple sources with ordering file 6544 6544 """ 6545 6546 6545 tide=0 6547 6546 time_step_count = 3 … … 6945 6944 fid.write(line) 6946 6945 fid.close() 6947 6946 6948 6947 sts_file=base_name 6949 6948 urs2sts(base_name, basename_out=sts_file, … … 8889 8888 if __name__ == "__main__": 8890 8889 8890 #suite = unittest.makeSuite(Test_Data_Manager,'test_read_mux2_py0') 8891 8891 suite = unittest.makeSuite(Test_Data_Manager,'test') 8892 8892 #suite = unittest.makeSuite(Test_Data_Manager,'test_export_gridII') -
anuga_core/source/anuga/shallow_water/urs_ext.c
r5471 r5473 23 23 char isdata(float); 24 24 void wrttxt(char *, float, int, float *, float, float, float, float, float, int, int, int); 25 PyArrayObject *_read_mux2(int, char **, float *, double *, int);25 PyArrayObject *_read_mux2(int, char **, float *, double *, PyObject *, int); 26 26 27 27 PyObject *read_mux2(PyObject *self, PyObject *args){ … … 29 29 30 30 Python call: 31 read_mux2(numSrc,filenames,weights,file_params, verbose)31 read_mux2(numSrc,filenames,weights,file_params,permutation,verbose) 32 32 33 33 NOTE: … … 38 38 PyArrayObject *pyweights,*file_params; 39 39 PyArrayObject *sts_data; 40 PyObject *permutation; 40 41 PyObject *fname; 41 42 … … 51 52 52 53 // Convert Python arguments to C 53 if (!PyArg_ParseTuple(args, "iOOO i",54 &numSrc,&filenames,&pyweights,&file_params,& verbose)) {54 if (!PyArg_ParseTuple(args, "iOOOOi", 55 &numSrc,&filenames,&pyweights,&file_params,&permutation,&verbose)) { 55 56 56 57 PyErr_SetString(PyExc_RuntimeError, … … 82 83 muxFileNameArray = (char **) malloc((int)numSrc*sizeof(char *)); 83 84 if (muxFileNameArray == NULL) { 84 printf("ERROR: Memory for muxFileNameArray could not be allocated.\n");85 exit(-1);85 PyErr_SetString(PyExc_RuntimeError,"Memory for muxFileNameArray could not be allocated"); 86 return NULL; 86 87 } 87 88 for (i=0;i<PyList_Size(filenames);i++){ 88 89 muxFileNameArray[i] = (char *) malloc((MAX_FILE_NAME_LENGTH+1)*sizeof(char)); 89 90 if (muxFileNameArray[i] == NULL) { 90 printf("ERROR: Memory for muxFileNameArray could not be allocated.\n");91 exit(-1);91 PyErr_SetString(PyExc_RuntimeError,"Memory for muxFileNameArray could not be allocated"); 92 return NULL; 92 93 } 93 94 fname=PyList_GetItem(filenames, i); 94 95 if (!PyString_Check(fname)) { 95 96 PyErr_SetString(PyExc_ValueError, "filename not a string"); 97 return NULL; 96 98 } 97 99 muxFileNameArray[i]=PyString_AsString(fname); … … 111 113 112 114 // Read in mux2 data from file 113 sts_data=_read_mux2((int)numSrc,muxFileNameArray,weights,(double*)file_params->data,(int)verbose); 114 115 sts_data=_read_mux2((int)numSrc,muxFileNameArray,weights,(double*)file_params->data,permutation,(int)verbose); 115 116 116 117 // Allocate space for return vector … … 125 126 } 126 127 127 PyArrayObject *_read_mux2(int numSrc, char **muxFileNameArray, float *weights, double *params, int verbose)128 PyArrayObject *_read_mux2(int numSrc, char **muxFileNameArray, float *weights, double *params, PyObject *permutation, int verbose) 128 129 { 129 130 FILE *fp; 130 int nsta, nsta0, i, t, isrc, ista;131 int nsta, nsta0, i, j, t, isrc, ista, N; 131 132 struct tgsrwg *mytgs, *mytgs0; 132 133 char *muxFileName; … … 144 145 float *temp_sts_data; 145 146 PyArrayObject *sts_data; 147 148 int permuation_dimensions[1]; 149 PyArrayObject *permutation_array; 146 150 147 151 long int offset; … … 174 178 /* Open the first muxfile */ 175 179 if((fp=fopen(muxFileNameArray[0],"r"))==NULL){ 176 fprintf(stderr, "cannot open file %s\n", muxFileNameArray[0]); 177 exit(-1); 180 fprintf(stderr,"Cannot open file %s\n", muxFileNameArray[0]); 181 PyErr_SetString(PyExc_RuntimeError,""); 182 return NULL; 178 183 } 179 184 … … 181 186 /* First read the number of stations*/ 182 187 fread(&nsta0,sizeof(int),1,fp); 188 189 if (permutation == Py_None){ 190 // if no permutation is specified return the times series of all the gauges in the mux file 191 permuation_dimensions[0]=nsta0; 192 permutation_array=(PyArrayObject *) PyArray_FromDims(1, permuation_dimensions, PyArray_INT); 193 for (ista=0; ista<nsta0; ista++){ 194 *(long *) (permutation_array -> data+ista*permutation_array->strides[0])=ista; 195 } 196 }else{ 197 // Specifies the gauge numbers that for which data is to be extracted 198 permutation_array=(PyArrayObject *) PyArray_ContiguousFromObject(permutation,PyArray_INT,1,1); 199 } 200 if(permutation_array == NULL){ 201 PyErr_SetString(PyExc_RuntimeError,"ERROR: Memory for permutation_array array could not be allocated"); 202 return NULL; 203 } 183 204 184 205 /*now allocate space for, and read in, the structures for each station*/ … … 203 224 if (numData < 0) { 204 225 fprintf(stderr,"Size of data block appears to be negative!\n"); 205 exit(-1); 226 PyErr_SetString(PyExc_RuntimeError,""); 227 return NULL; 206 228 } 207 229 fclose(fp); … … 229 251 { 230 252 fprintf(stderr, "cannot open file %s\n", muxFileName); 231 exit(-1); 253 PyErr_SetString(PyExc_RuntimeError,""); 254 return NULL; 232 255 } 233 256 … … 236 259 if(nsta != nsta0){ 237 260 fprintf(stderr,"%s has different number of stations to %s\n", muxFileName, muxFileNameArray[0]); 261 PyErr_SetString(PyExc_RuntimeError,""); 238 262 fclose(fp); 239 exit(-1);263 return NULL; 240 264 } 241 265 fread(&mytgs[0], nsta*sizeof(struct tgsrwg), 1, fp); … … 244 268 fprintf(stderr,"%s has different sampling rate to %s\n", muxFileName, muxFileNameArray[0]); 245 269 fclose(fp); 246 exit(-1);270 return NULL; 247 271 } 248 272 if(mytgs[ista].nt != num_ts){ 249 273 fprintf(stderr,"%s has different series length to %s\n", muxFileName, muxFileNameArray[0]); 274 PyErr_SetString(PyExc_RuntimeError,""); 250 275 fclose(fp); 251 exit(-1);276 return NULL; 252 277 } 253 278 } … … 263 288 if (numData < 0){ 264 289 fprintf(stderr,"Size of data block appears to be negative!\n"); 265 exit(-1); 290 PyErr_SetString(PyExc_RuntimeError,""); 291 return NULL; 266 292 } 267 293 fclose(fp); 268 294 } 269 params[0]=(double)nsta0; 295 N = permutation_array->dimensions[0]; 296 297 params[0]=(double)N; 270 298 params[1]=(double)mytgs0[0].dt; 271 params[2]=(double) mytgs0[0].nt;299 params[2]=(double)num_ts; 272 300 273 301 274 302 // Find min and max start times of all gauges 303 // FIXME: ONLY CHECK gauges in permutation for start and finsh times 275 304 start_tstep=num_ts+1; 276 305 finish_tstep=-1; … … 285 314 286 315 if ((start_tstep>num_ts) | (finish_tstep < 0)){ 287 printf("ERROR: Gauge data has incorrect start and finsh times\n");316 PyErr_SetString(PyExc_RuntimeError,"Gauge data has incorrect start and finsh times"); 288 317 return NULL; 289 318 } 290 319 291 320 if (start_tstep>=finish_tstep){ 292 printf("ERROR: Gauge data has non-postive_length\n");321 PyErr_SetString(PyExc_RuntimeError,"Gauge data has non-postive_length"); 293 322 return NULL; 294 323 } … … 296 325 /* Make array(s) to hold the demuxed data */ 297 326 len_sts_data=num_ts+POFFSET; 298 dimensions[0]= nsta0;327 dimensions[0]=N; 299 328 dimensions[1]=len_sts_data; 300 329 sts_data = (PyArrayObject *) PyArray_FromDims(2, dimensions, PyArray_DOUBLE); 301 330 if(sts_data == NULL){ 302 printf("ERROR: Memory for pydata array could not be allocated.\n");331 PyErr_SetString(PyExc_RuntimeError,"Memory for pydata array could not be allocated"); 303 332 return NULL; 304 333 } 305 334 306 335 /* Initialise sts data to zero */ 307 for (ista=0;ista<nsta0;ista++){ 336 for(i=0; i<N; i++){ 337 ista = *(long *) (permutation_array -> data+i*permutation_array->strides[0]); 308 338 for (t=0;t<num_ts;t++){ 309 *(double *) (sts_data -> data+i sta*sts_data->strides[0]+t*sts_data->strides[1])=0.0;310 } 311 *(double *) (sts_data -> data+i sta*sts_data->strides[0]+(num_ts)*sts_data->strides[1])=(float)mytgs0[ista].geolat;312 *(double *) (sts_data -> data+i sta*sts_data->strides[0]+(num_ts+1)*sts_data->strides[1])=(float)mytgs0[ista].geolon;313 *(double *) (sts_data -> data+i sta*sts_data->strides[0]+(num_ts+2)*sts_data->strides[1])=(float)mytgs0[ista].z;314 *(double *) (sts_data -> data+i sta*sts_data->strides[0]+(num_ts+3)*sts_data->strides[1])=(float)fros[ista];315 *(double *) (sts_data -> data+i sta*sts_data->strides[0]+(num_ts+4)*sts_data->strides[1])=(float)lros[ista];339 *(double *) (sts_data -> data+i*sts_data->strides[0]+t*sts_data->strides[1])=0.0; 340 } 341 *(double *) (sts_data -> data+i*sts_data->strides[0]+(num_ts)*sts_data->strides[1])=(float)mytgs0[ista].geolat; 342 *(double *) (sts_data -> data+i*sts_data->strides[0]+(num_ts+1)*sts_data->strides[1])=(float)mytgs0[ista].geolon; 343 *(double *) (sts_data -> data+i*sts_data->strides[0]+(num_ts+2)*sts_data->strides[1])=(float)mytgs0[ista].z; 344 *(double *) (sts_data -> data+i*sts_data->strides[0]+(num_ts+3)*sts_data->strides[1])=(float)fros[ista]; 345 *(double *) (sts_data -> data+i*sts_data->strides[0]+(num_ts+4)*sts_data->strides[1])=(float)lros[ista]; 316 346 } 317 347 … … 327 357 if((fp=fopen(muxFileName,"r"))==NULL) 328 358 { 329 //fprintf(stderr, "%s: cannot open file %s\n", av[0], muxFileName);330 fprintf(stderr, "cannot open file %s\n", muxFileName);331 exit(-1);359 fprintf(stderr, "Cannot open file %s\n", muxFileName); 360 PyErr_SetString(PyExc_RuntimeError,""); 361 return NULL; 332 362 } 333 363 … … 345 375 346 376 /* loop over stations */ 347 for(ista=0; ista<nsta0; ista++){ 377 for(j=0; j<N; j++){ 378 ista = *(long *) (permutation_array -> data+j*permutation_array->strides[0]); 379 //printf("%d\n",(int)*(long *) (permutation_array -> data+j*permutation_array->strides[0])); 348 380 349 381 /* fill the data0 array from the mux file, and weight it */ … … 351 383 352 384 /* weight appropriately and add */ 353 for( i=0; i<num_ts; i++){354 if((isdata(*(double *) (sts_data -> data+ ista*sts_data->strides[0]+i*sts_data->strides[1]))) && isdata(temp_sts_data[i])){355 *(double *) (sts_data -> data+ ista*sts_data->strides[0]+i*sts_data->strides[1]) += temp_sts_data[i] * weights[isrc];385 for(t=0; t<num_ts; t++){ 386 if((isdata(*(double *) (sts_data -> data+j*sts_data->strides[0]+t*sts_data->strides[1]))) && isdata(temp_sts_data[t])){ 387 *(double *) (sts_data -> data+j*sts_data->strides[0]+t*sts_data->strides[1]) += temp_sts_data[t] * weights[isrc]; 356 388 }else{ 357 *(double *) (sts_data -> data+ ista*sts_data->strides[0]+i*sts_data->strides[1]) = 0.0;389 *(double *) (sts_data -> data+j*sts_data->strides[0]+t*sts_data->strides[1]) = 0.0; 358 390 } 359 391 } 360 392 } 361 } 393 } 362 394 free(muxData); 363 395 free(temp_sts_data); 396 free(fros); 397 free(lros); 364 398 return sts_data; 365 399 }
Note: See TracChangeset
for help on using the changeset viewer.