Changeset 5471
- Timestamp:
- Jul 7, 2008, 1:23:52 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified anuga_core/source/anuga/shallow_water/urs_ext.c ¶
r5470 r5471 23 23 char isdata(float); 24 24 void wrttxt(char *, float, int, float *, float, float, float, float, float, int, int, int); 25 float**_read_mux2(int, char **, float *, double *, int);25 PyArrayObject *_read_mux2(int, char **, float *, double *, int); 26 26 27 27 PyObject *read_mux2(PyObject *self, PyObject *args){ … … 37 37 PyObject *filenames; 38 38 PyArrayObject *pyweights,*file_params; 39 PyArrayObject * pydata;39 PyArrayObject *sts_data; 40 40 PyObject *fname; 41 41 … … 45 45 long verbose; 46 46 47 float **cdata;48 int dimensions[2];49 47 int nsta0; 50 48 int nt; 51 49 double dt; 52 int i,j; 53 int start_tstep; 54 int finish_tstep; 55 int it,time; 56 int num_ts; 50 int i; 57 51 58 52 // Convert Python arguments to C … … 117 111 118 112 // Read in mux2 data from file 119 cdata=_read_mux2((int)numSrc,muxFileNameArray,weights,(double*)file_params->data,(int)verbose);113 sts_data=_read_mux2((int)numSrc,muxFileNameArray,weights,(double*)file_params->data,(int)verbose); 120 114 121 115 … … 125 119 nt=(int)*(double *) (file_params -> data+2*file_params->strides[0]); 126 120 127 128 // Find min and max start times of all gauges129 start_tstep=nt+1;130 finish_tstep=-1;131 for (i=0;i<nsta0;i++){132 if ((int)cdata[i][nt+3] < start_tstep){133 start_tstep=(int)cdata[i][nt+3];134 }135 if ((int)cdata[i][nt+4] > finish_tstep){136 finish_tstep=(int)cdata[i][nt+4];137 }138 }139 140 if ((start_tstep>nt) | (finish_tstep < 0)){141 printf("ERROR: Gauge data has incorrect start and finsh times\n");142 return NULL;143 }144 145 if (start_tstep>=finish_tstep){146 printf("ERROR: Gauge data has non-postive_length\n");147 return NULL;148 }149 150 num_ts=finish_tstep-start_tstep+1;151 dimensions[0]=nsta0;152 dimensions[1]=num_ts+POFFSET;153 pydata = (PyArrayObject *) PyArray_FromDims(2, dimensions, PyArray_DOUBLE);154 if(pydata == NULL){155 printf("ERROR: Memory for pydata array could not be allocated.\n");156 return NULL;157 }158 159 // Each gauge begins and ends recording at different times. When a gauge is160 // not recording but at least one other gauge is. Pad the non-recording gauge161 // array with zeros.162 for (i=0;i<nsta0;i++){163 time=0;164 for (it=0;it<finish_tstep;it++){165 if((it+1>=start_tstep)&&(it+1<=finish_tstep)){166 if (it+1>(int)cdata[i][nt+4]){167 //This gauge has stopped recording but others are still recording168 *(double *) (pydata -> data+i*pydata->strides[0]+time*pydata->strides[1])=0.0;169 }else{170 *(double *) (pydata -> data+i*pydata->strides[0]+time*pydata->strides[1])=cdata[i][it];171 }172 time++;173 }174 }175 // Pass back lat,lon,elevation176 for (j=0; j<POFFSET; j++){177 *(double *) (pydata -> data+i*pydata->strides[0]+(num_ts+j)*pydata->strides[1])=cdata[i][nt+j];178 }179 }180 181 121 free(weights); 182 122 free(muxFileNameArray); 183 free(cdata); 184 return PyArray_Return(pydata); 123 return PyArray_Return(sts_data); 185 124 186 125 } 187 126 188 float**_read_mux2(int numSrc, char **muxFileNameArray, float *weights, double *params, int verbose)127 PyArrayObject *_read_mux2(int numSrc, char **muxFileNameArray, float *weights, double *params, int verbose) 189 128 { 190 129 FILE *fp; 191 int nsta, nsta0, i, isrc, ista;130 int nsta, nsta0, i, t, isrc, ista; 192 131 struct tgsrwg *mytgs, *mytgs0; 193 132 char *muxFileName; … … 198 137 long numData; 199 138 200 139 int start_tstep; 140 int finish_tstep; 141 int num_ts; 142 int dimensions[2]; 201 143 int len_sts_data; 202 float **sts_data;203 144 float *temp_sts_data; 145 PyArrayObject *sts_data; 204 146 205 147 long int offset; … … 256 198 /* compute the size of the data block for source 0 */ 257 199 numData = getNumData(fros, lros, nsta0); 258 200 num_ts = mytgs0[0].nt; 201 259 202 /* Burbidge: Added a sanity check here */ 260 203 if (numData < 0) { … … 303 246 exit(-1); 304 247 } 305 if(mytgs[ista].nt != mytgs0[ista].nt){248 if(mytgs[ista].nt != num_ts){ 306 249 fprintf(stderr,"%s has different series length to %s\n", muxFileName, muxFileNameArray[0]); 307 250 fclose(fp); … … 327 270 params[1]=(double)mytgs0[0].dt; 328 271 params[2]=(double)mytgs0[0].nt; 329 330 /* make array(s) to hold the demuxed data */ 331 //FIXME: This can be reduced to only contain stations in order file 332 sts_data = (float **)malloc (nsta0 * sizeof(float *)); 333 334 if (sts_data == NULL){ 335 printf("ERROR: Memory for sts_data could not be allocated.\n"); 336 exit(-1); 337 } 338 339 len_sts_data=mytgs0[0].nt + POFFSET; 340 for (ista=0; ista<nsta0; ista++){ 341 // Initialise sts_data to zero 342 sts_data[ista] = (float *)calloc(len_sts_data, sizeof(float) ); 343 if (sts_data[ista] == NULL){ 344 printf("ERROR: Memory for sts_data could not be allocated.\n"); 345 exit(-1); 346 } 347 sts_data[ista][mytgs0[0].nt]=(float)mytgs0[ista].geolat; 348 sts_data[ista][mytgs0[0].nt+1]=(float)mytgs0[ista].geolon; 349 sts_data[ista][mytgs0[0].nt+2]=(float)mytgs0[ista].z; 350 sts_data[ista][mytgs0[0].nt+3]=(float)fros[ista]; 351 sts_data[ista][mytgs0[0].nt+4]=(float)lros[ista]; 352 } 272 273 274 // Find min and max start times of all gauges 275 start_tstep=num_ts+1; 276 finish_tstep=-1; 277 for (ista=0;ista<nsta0;ista++){ 278 if (fros[ista]< start_tstep){ 279 start_tstep=fros[ista]; 280 } 281 if (lros[0+nsta0*ista] > finish_tstep){ 282 finish_tstep=lros[ista]; 283 } 284 } 285 286 if ((start_tstep>num_ts) | (finish_tstep < 0)){ 287 printf("ERROR: Gauge data has incorrect start and finsh times\n"); 288 return NULL; 289 } 290 291 if (start_tstep>=finish_tstep){ 292 printf("ERROR: Gauge data has non-postive_length\n"); 293 return NULL; 294 } 295 296 /* Make array(s) to hold the demuxed data */ 297 len_sts_data=num_ts+POFFSET; 298 dimensions[0]=nsta0; 299 dimensions[1]=len_sts_data; 300 sts_data = (PyArrayObject *) PyArray_FromDims(2, dimensions, PyArray_DOUBLE); 301 if(sts_data == NULL){ 302 printf("ERROR: Memory for pydata array could not be allocated.\n"); 303 return NULL; 304 } 305 306 /* Initialise sts data to zero */ 307 for (ista=0;ista<nsta0;ista++){ 308 for (t=0;t<num_ts;t++){ 309 *(double *) (sts_data -> data+ista*sts_data->strides[0]+t*sts_data->strides[1])=0.0; 310 } 311 *(double *) (sts_data -> data+ista*sts_data->strides[0]+(num_ts)*sts_data->strides[1])=(float)mytgs0[ista].geolat; 312 *(double *) (sts_data -> data+ista*sts_data->strides[0]+(num_ts+1)*sts_data->strides[1])=(float)mytgs0[ista].geolon; 313 *(double *) (sts_data -> data+ista*sts_data->strides[0]+(num_ts+2)*sts_data->strides[1])=(float)mytgs0[ista].z; 314 *(double *) (sts_data -> data+ista*sts_data->strides[0]+(num_ts+3)*sts_data->strides[1])=(float)fros[ista]; 315 *(double *) (sts_data -> data+ista*sts_data->strides[0]+(num_ts+4)*sts_data->strides[1])=(float)lros[ista]; 316 } 353 317 354 318 temp_sts_data = (float *)calloc(len_sts_data, sizeof(float) ); … … 384 348 385 349 /* fill the data0 array from the mux file, and weight it */ 386 fillDataArray(ista, nsta0, mytgs0[ista].nt, mytgs0[ista].ig, fros+isrc*nsta0, lros+isrc*nsta0, temp_sts_data, &istart, &istop, muxData);350 fillDataArray(ista, nsta0, num_ts, mytgs0[ista].ig, fros+isrc*nsta0, lros+isrc*nsta0, temp_sts_data, &istart, &istop, muxData); 387 351 388 352 /* weight appropriately and add */ 389 for(i=0; i<mytgs0[ista].nt; i++){ 390 if((isdata(sts_data[ista][i])) && isdata(temp_sts_data[i])){ 391 sts_data[ista][i] += temp_sts_data[i] * weights[isrc]; 392 //printf("%d,%d,%f\n",ista,i,sts_data[ista][i]); 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]; 393 356 }else{ 394 sts_data[ista][i] = NODATA;357 *(double *) (sts_data -> data+ista*sts_data->strides[0]+i*sts_data->strides[1]) = 0.0; 395 358 } 396 359 }
Note: See TracChangeset
for help on using the changeset viewer.