[5516] | 1 | /* |
---|
| 2 | gcc -fPIC -c urs_ext.c -I/usr/include/python2.5 -o urs_ext.o -Wall -O |
---|
| 3 | gcc -shared urs_ext.o -o urs_ext.so |
---|
| 4 | */ |
---|
| 5 | |
---|
| 6 | /* |
---|
| 7 | This file was reverted from changeset:5484 to changeset:5470 on 10th July |
---|
| 8 | by Ole. |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | #include "Python.h" |
---|
| 12 | #include "Numeric/arrayobject.h" |
---|
| 13 | #include "structure.h" |
---|
| 14 | #include "math.h" |
---|
| 15 | #include <stdio.h> |
---|
| 16 | #include <float.h> |
---|
| 17 | #include <time.h> |
---|
| 18 | |
---|
| 19 | #define MAX_FILE_NAME_LENGTH 128 |
---|
| 20 | #define NODATA 99.0 |
---|
| 21 | #define EPSILON 0.00001 |
---|
| 22 | |
---|
| 23 | #define DEBUG 0 |
---|
| 24 | |
---|
| 25 | #define POFFSET 5 //Number of site_params |
---|
| 26 | |
---|
| 27 | void fillDataArray(int, int, int, int, int *, int *, float *, int *, int *, float *); |
---|
| 28 | long getNumData(const int *fros, const int *lros, const int nsta); |
---|
| 29 | char isdata(float); |
---|
[5537] | 30 | float** _read_mux2(int numSrc, |
---|
| 31 | char **muxFileNameArray, |
---|
| 32 | float *weights, |
---|
| 33 | double *params, |
---|
[5539] | 34 | int *number_of_stations, |
---|
[5537] | 35 | int *permutation, |
---|
| 36 | int verbose); |
---|
[5516] | 37 | |
---|
| 38 | PyObject *read_mux2(PyObject *self, PyObject *args){ |
---|
| 39 | /*Read in mux 2 file |
---|
| 40 | |
---|
| 41 | Python call: |
---|
[5537] | 42 | read_mux2(numSrc,filenames,weights,file_params,permutation,verbose) |
---|
[5516] | 43 | |
---|
| 44 | NOTE: |
---|
[5539] | 45 | A Python int is equivalent to a C long (this becomes really important on 64 bit architectures) |
---|
[5516] | 46 | A Python double corresponds to a C double |
---|
| 47 | */ |
---|
[5539] | 48 | |
---|
[5516] | 49 | PyObject *filenames; |
---|
| 50 | PyArrayObject *pyweights; |
---|
| 51 | PyArrayObject *file_params; |
---|
[5537] | 52 | PyArrayObject *permutation; |
---|
[5516] | 53 | PyArrayObject *pydata; |
---|
| 54 | PyObject *fname; |
---|
| 55 | |
---|
| 56 | char **muxFileNameArray; |
---|
| 57 | float **cdata; |
---|
| 58 | float *weights; |
---|
| 59 | int dimensions[2]; |
---|
[5530] | 60 | int numSrc; |
---|
| 61 | int verbose; |
---|
[5516] | 62 | int nsta0; |
---|
[5537] | 63 | int number_of_selected_stations; |
---|
[5516] | 64 | int nt; |
---|
| 65 | double dt; |
---|
| 66 | int i; |
---|
| 67 | int j; |
---|
| 68 | int start_tstep; |
---|
| 69 | int finish_tstep; |
---|
| 70 | int it; |
---|
| 71 | int time; |
---|
| 72 | int num_ts; |
---|
[5527] | 73 | |
---|
[5516] | 74 | // Convert Python arguments to C |
---|
[5537] | 75 | if (!PyArg_ParseTuple(args, "iOOOOi", |
---|
| 76 | &numSrc, &filenames, &pyweights, &file_params, &permutation, &verbose)) |
---|
[5516] | 77 | { |
---|
| 78 | PyErr_SetString(PyExc_RuntimeError, |
---|
| 79 | "Input arguments to read_mux2 failed"); |
---|
| 80 | return NULL; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | if(!PyList_Check(filenames)) |
---|
| 84 | { |
---|
| 85 | PyErr_SetString(PyExc_TypeError, "get_first_elem expects a list"); |
---|
| 86 | return NULL; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if(PyList_Size(filenames) == 0) |
---|
| 90 | { |
---|
| 91 | PyErr_SetString(PyExc_ValueError, "empty lists not allowed"); |
---|
| 92 | return NULL; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | if (pyweights->nd != 1 || pyweights->descr->type_num != PyArray_DOUBLE) |
---|
| 96 | { |
---|
| 97 | PyErr_SetString(PyExc_ValueError, |
---|
| 98 | "pyweights must be one-dimensional and of type double"); |
---|
| 99 | return NULL; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if(PyList_Size(filenames) != pyweights->dimensions[0]) |
---|
| 103 | { |
---|
| 104 | PyErr_SetString(PyExc_ValueError, "Must specify one weight for each filename"); |
---|
| 105 | return NULL; |
---|
| 106 | } |
---|
| 107 | |
---|
[5530] | 108 | muxFileNameArray = (char**)malloc(numSrc*sizeof(char*)); |
---|
[5516] | 109 | if (muxFileNameArray == NULL) |
---|
| 110 | { |
---|
[5539] | 111 | PyErr_SetString(PyExc_ValueError, "ERROR: Memory for muxFileNameArray could not be allocated."); |
---|
[5530] | 112 | return NULL; |
---|
[5516] | 113 | } |
---|
| 114 | |
---|
| 115 | for (i = 0; i < numSrc; i++) |
---|
| 116 | { |
---|
[5530] | 117 | |
---|
[5516] | 118 | fname = PyList_GetItem(filenames, i); |
---|
[5530] | 119 | if (!fname) |
---|
[5516] | 120 | { |
---|
| 121 | PyErr_SetString(PyExc_ValueError, "filename not a string"); |
---|
[5530] | 122 | return NULL; |
---|
| 123 | } |
---|
[5539] | 124 | |
---|
[5516] | 125 | muxFileNameArray[i] = PyString_AsString(fname); |
---|
[5528] | 126 | if (muxFileNameArray[i] == NULL) |
---|
| 127 | { |
---|
[5537] | 128 | PyErr_SetString(PyExc_ValueError, |
---|
| 129 | "ERROR: Memory for muxFileNameArray could not be allocated.\n"); |
---|
[5529] | 130 | return NULL; |
---|
[5528] | 131 | } |
---|
[5516] | 132 | } |
---|
| 133 | |
---|
| 134 | if (file_params->nd != 1 || file_params->descr->type_num != PyArray_DOUBLE) |
---|
| 135 | { |
---|
| 136 | PyErr_SetString(PyExc_ValueError, |
---|
[5537] | 137 | "file_params must be one-dimensional and of type double"); |
---|
[5516] | 138 | return NULL; |
---|
| 139 | } |
---|
| 140 | |
---|
[5539] | 141 | |
---|
[5516] | 142 | // Create array for weights which are passed to read_mux2 |
---|
[5530] | 143 | weights = (float*) malloc(numSrc*sizeof(float)); |
---|
| 144 | for (i = 0; i < numSrc; i++) |
---|
[5516] | 145 | { |
---|
| 146 | weights[i] = (float)(*(double*)(pyweights->data + i*pyweights->strides[0])); |
---|
| 147 | } |
---|
| 148 | |
---|
[5537] | 149 | number_of_selected_stations = (int) permutation->dimensions[0]; |
---|
[5539] | 150 | |
---|
[5516] | 151 | // Read in mux2 data from file |
---|
[5530] | 152 | cdata = _read_mux2(numSrc, |
---|
| 153 | muxFileNameArray, |
---|
| 154 | weights, |
---|
[5537] | 155 | (double*)file_params->data, |
---|
[5539] | 156 | &number_of_selected_stations, // Desired number of stations |
---|
[5537] | 157 | (int*) permutation->data, // Ordering of selected stations |
---|
[5530] | 158 | verbose); |
---|
[5516] | 159 | |
---|
[5537] | 160 | if (!cdata) |
---|
| 161 | { |
---|
| 162 | PyErr_SetString(PyExc_ValueError, "No STS_DATA returned"); |
---|
| 163 | return NULL; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
[5516] | 167 | // Allocate space for return vector |
---|
| 168 | nsta0 = (int)*(double*)(file_params->data + 0*file_params->strides[0]); |
---|
| 169 | dt = *(double*)(file_params->data + 1*file_params->strides[0]); |
---|
| 170 | nt = (int)*(double*)(file_params->data + 2*file_params->strides[0]); |
---|
| 171 | |
---|
[5537] | 172 | |
---|
[5516] | 173 | // Find min and max start times of all gauges |
---|
| 174 | start_tstep = nt + 1; |
---|
| 175 | finish_tstep = -1; |
---|
[5537] | 176 | for (i = 0; i < number_of_selected_stations; i++) |
---|
[5516] | 177 | { |
---|
[5539] | 178 | //printf("cdata[%d] start = %f\n", i, (double) cdata[i][nt+3]); |
---|
| 179 | // printf("cdata[%d] finish = %f\n", i, (double) cdata[i][nt+4]); |
---|
[5537] | 180 | |
---|
[5516] | 181 | if ((int)cdata[i][nt + 3] < start_tstep) |
---|
| 182 | { |
---|
| 183 | start_tstep = (int)cdata[i][nt + 3]; |
---|
| 184 | } |
---|
| 185 | if ((int)cdata[i][nt + 4] > finish_tstep) |
---|
| 186 | { |
---|
| 187 | finish_tstep = (int)cdata[i][nt + 4]; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | if ((start_tstep > nt) | (finish_tstep < 0)) |
---|
| 192 | { |
---|
[5537] | 193 | printf("ERROR: Gauge data has incorrect start and finish times:\n"); |
---|
| 194 | printf(" start_tstep = %d, max_number_of_steps = %d\n", start_tstep, nt); |
---|
| 195 | printf(" finish_tstep = %d, min_number_of_steps = %d\n", finish_tstep, 0); |
---|
| 196 | PyErr_SetString(PyExc_ValueError, "Incorrect start and finish times"); |
---|
[5516] | 197 | return NULL; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if (start_tstep >= finish_tstep) |
---|
| 201 | { |
---|
[5537] | 202 | PyErr_SetString(PyExc_ValueError, "ERROR: Gauge data has non-postive_length"); |
---|
[5516] | 203 | return NULL; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | num_ts = finish_tstep - start_tstep + 1; |
---|
[5537] | 207 | dimensions[0] = number_of_selected_stations; |
---|
[5516] | 208 | dimensions[1] = num_ts + POFFSET; |
---|
| 209 | pydata = (PyArrayObject*)PyArray_FromDims(2, dimensions, PyArray_DOUBLE); |
---|
| 210 | if(pydata == NULL) |
---|
| 211 | { |
---|
[5537] | 212 | PyErr_SetString(PyExc_ValueError, "ERROR: Memory for pydata array could not be allocated."); |
---|
[5516] | 213 | return NULL; |
---|
| 214 | } |
---|
| 215 | |
---|
[5539] | 216 | |
---|
[5516] | 217 | // Each gauge begins and ends recording at different times. When a gauge is |
---|
| 218 | // not recording but at least one other gauge is. Pad the non-recording gauge |
---|
| 219 | // array with zeros. |
---|
[5537] | 220 | for (i = 0; i < number_of_selected_stations; i++) |
---|
[5516] | 221 | { |
---|
| 222 | time = 0; |
---|
| 223 | for (it = 0; it < finish_tstep; it++) |
---|
| 224 | { |
---|
| 225 | if ((it + 1 >= start_tstep) && (it + 1 <= finish_tstep)) |
---|
| 226 | { |
---|
| 227 | if (it + 1 > (int)cdata[i][nt + 4]) |
---|
| 228 | { |
---|
[5539] | 229 | // This gauge has stopped recording but others are still recording |
---|
[5516] | 230 | *(double*)(pydata->data + i*pydata->strides[0] + time*pydata->strides[1]) = 0.0; |
---|
| 231 | } |
---|
| 232 | else |
---|
| 233 | { |
---|
| 234 | *(double*)(pydata->data + i*pydata->strides[0] + time*pydata->strides[1]) = cdata[i][it]; |
---|
| 235 | } |
---|
| 236 | time++; |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | // Pass back lat,lon,elevation |
---|
| 240 | for (j = 0; j < POFFSET; j++) |
---|
| 241 | { |
---|
| 242 | *(double*)(pydata->data + i*pydata->strides[0] + (num_ts + j)*pydata->strides[1]) = cdata[i][nt + j]; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | free(weights); |
---|
| 247 | |
---|
[5529] | 248 | // Free filename array, but not independent Python strings |
---|
| 249 | // FIXME(Ole): Do we need to update a reference counter in this case? |
---|
[5516] | 250 | free(muxFileNameArray); |
---|
| 251 | |
---|
[5537] | 252 | for (i = 0; i < number_of_selected_stations; ++i) |
---|
[5516] | 253 | { |
---|
| 254 | free(cdata[i]); |
---|
| 255 | } |
---|
| 256 | free(cdata); |
---|
| 257 | |
---|
| 258 | return PyArray_Return(pydata); |
---|
| 259 | } |
---|
| 260 | |
---|
[5530] | 261 | float** _read_mux2(int numSrc, |
---|
| 262 | char **muxFileNameArray, |
---|
| 263 | float *weights, |
---|
| 264 | double *params, |
---|
[5539] | 265 | int *number_of_stations, |
---|
[5537] | 266 | int *permutation, |
---|
[5530] | 267 | int verbose) |
---|
[5516] | 268 | { |
---|
| 269 | FILE *fp; |
---|
[5531] | 270 | int nsta, nsta0, i, isrc, ista, k; |
---|
[5516] | 271 | struct tgsrwg *mytgs=0, *mytgs0=0; |
---|
| 272 | char *muxFileName; |
---|
| 273 | int istart, istop; |
---|
| 274 | int *fros=0, *lros=0; |
---|
[5539] | 275 | int number_of_selected_stations; |
---|
[5516] | 276 | char susMuxFileName; |
---|
| 277 | float *muxData; |
---|
| 278 | long numData; |
---|
| 279 | |
---|
| 280 | int len_sts_data; |
---|
| 281 | float **sts_data; |
---|
| 282 | float *temp_sts_data; |
---|
| 283 | |
---|
| 284 | long int offset; |
---|
| 285 | |
---|
| 286 | /* Allocate space for the names and the weights and pointers to the data*/ |
---|
| 287 | |
---|
| 288 | /* Check that the input files have mux2 extension*/ |
---|
| 289 | susMuxFileName = 0; |
---|
| 290 | for(isrc = 0; isrc < numSrc; isrc++) |
---|
| 291 | { |
---|
| 292 | muxFileName = muxFileNameArray[isrc]; |
---|
| 293 | if(!susMuxFileName && strcmp(muxFileName + strlen(muxFileName) - 4, "mux2") != 0) |
---|
| 294 | { |
---|
| 295 | susMuxFileName = 1; |
---|
| 296 | break; |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | if(susMuxFileName) |
---|
| 301 | { |
---|
| 302 | printf("\n**************************************************************************\n"); |
---|
| 303 | printf(" WARNING: This program operates only on multiplexed files in mux2 format\n"); |
---|
| 304 | printf(" At least one input file name does not end with mux2\n"); |
---|
| 305 | printf(" Check your results carefully!\n"); |
---|
| 306 | printf("**************************************************************************\n\n"); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | if (verbose) |
---|
| 310 | { |
---|
| 311 | printf("Reading mux header information\n"); |
---|
| 312 | } |
---|
| 313 | |
---|
[5537] | 314 | /* Loop over all sources, read headers and check compatibility */ |
---|
[5531] | 315 | for (isrc = 0; isrc < numSrc; isrc++) |
---|
[5516] | 316 | { |
---|
| 317 | muxFileName = muxFileNameArray[isrc]; |
---|
| 318 | |
---|
[5537] | 319 | /* open the mux file */ |
---|
[5516] | 320 | if((fp = fopen(muxFileName, "r")) == NULL) |
---|
| 321 | { |
---|
| 322 | fprintf(stderr, "cannot open file %s\n", muxFileName); |
---|
[5537] | 323 | return NULL; |
---|
[5516] | 324 | } |
---|
| 325 | |
---|
| 326 | if (!isrc) |
---|
| 327 | { |
---|
[5530] | 328 | fread(&nsta0, sizeof(int), 1, fp); |
---|
| 329 | |
---|
| 330 | fros = (int*)malloc(nsta0*numSrc*sizeof(int)); |
---|
| 331 | lros = (int*)malloc(nsta0*numSrc*sizeof(int)); |
---|
[5516] | 332 | |
---|
[5530] | 333 | mytgs0 = (struct tgsrwg*)malloc(nsta0*sizeof(struct tgsrwg)); |
---|
| 334 | mytgs = (struct tgsrwg*)malloc(nsta0*sizeof(struct tgsrwg)); |
---|
[5516] | 335 | |
---|
[5530] | 336 | fread(mytgs0, nsta0*sizeof(struct tgsrwg), 1, fp); |
---|
[5516] | 337 | } |
---|
| 338 | else |
---|
| 339 | { |
---|
| 340 | /* check that the mux files are compatible */ |
---|
| 341 | fread(&nsta, sizeof(int), 1, fp); |
---|
| 342 | if(nsta != nsta0) |
---|
| 343 | { |
---|
[5537] | 344 | fprintf(stderr,"%s has different number of stations to %s\n", |
---|
| 345 | muxFileName, |
---|
| 346 | muxFileNameArray[0]); |
---|
[5516] | 347 | fclose(fp); |
---|
[5537] | 348 | return NULL; |
---|
[5516] | 349 | } |
---|
| 350 | |
---|
| 351 | fread(mytgs, nsta*sizeof(struct tgsrwg), 1, fp); |
---|
| 352 | |
---|
| 353 | for (ista = 0; ista < nsta; ista++) |
---|
| 354 | { |
---|
| 355 | if (mytgs[ista].dt != mytgs0[ista].dt) |
---|
| 356 | { |
---|
[5537] | 357 | fprintf(stderr,"%s has different sampling rate to %s\n", |
---|
| 358 | muxFileName, |
---|
| 359 | muxFileNameArray[0]); |
---|
[5516] | 360 | fclose(fp); |
---|
[5537] | 361 | return NULL; |
---|
[5516] | 362 | } |
---|
| 363 | if (mytgs[ista].nt != mytgs0[ista].nt) |
---|
| 364 | { |
---|
[5537] | 365 | fprintf(stderr,"%s has different series length to %s\n", |
---|
| 366 | muxFileName, |
---|
| 367 | muxFileNameArray[0]); |
---|
[5516] | 368 | fclose(fp); |
---|
[5537] | 369 | return NULL; |
---|
[5516] | 370 | } |
---|
| 371 | } |
---|
| 372 | } |
---|
| 373 | |
---|
[5537] | 374 | /* Read the start and stop times for this source */ |
---|
[5516] | 375 | fread(fros + isrc*nsta0, nsta0*sizeof(int), 1, fp); |
---|
| 376 | fread(lros + isrc*nsta0, nsta0*sizeof(int), 1, fp); |
---|
| 377 | |
---|
[5537] | 378 | /* Compute the size of the data block for this source */ |
---|
[5516] | 379 | numData = getNumData(fros + isrc*nsta0, lros + isrc*nsta0, nsta0); |
---|
| 380 | |
---|
[5537] | 381 | /* Sanity check */ |
---|
[5516] | 382 | if (numData < 0) |
---|
| 383 | { |
---|
| 384 | fprintf(stderr,"Size of data block appears to be negative!\n"); |
---|
[5537] | 385 | return NULL; |
---|
[5516] | 386 | } |
---|
| 387 | |
---|
| 388 | fclose(fp); |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | params[0] = (double)nsta0; |
---|
| 392 | params[1] = (double)mytgs0[0].dt; |
---|
| 393 | params[2] = (double)mytgs0[0].nt; |
---|
| 394 | |
---|
[5537] | 395 | // Apply rule that an empty permutation file means 'take all stations' |
---|
| 396 | // We can change this later by passing in None instead of the empty permutation. |
---|
[5539] | 397 | |
---|
| 398 | |
---|
| 399 | number_of_selected_stations = *number_of_stations; |
---|
[5537] | 400 | if (number_of_selected_stations == 0) |
---|
| 401 | { |
---|
| 402 | number_of_selected_stations = nsta0; |
---|
[5539] | 403 | *number_of_stations = nsta0; // Return possibly updated number of stations |
---|
[5537] | 404 | |
---|
| 405 | // Create the Identity permutation vector |
---|
| 406 | permutation = (int *) malloc(number_of_selected_stations*sizeof(int)); |
---|
| 407 | for (i = 0; i < number_of_selected_stations; i++) |
---|
| 408 | { |
---|
| 409 | permutation[i] = i; |
---|
| 410 | } |
---|
| 411 | } |
---|
| 412 | |
---|
[5539] | 413 | |
---|
| 414 | |
---|
[5537] | 415 | /* Make array(s) to hold demuxed data for stations given in the permutation file */ |
---|
| 416 | sts_data = (float**)malloc(number_of_selected_stations*sizeof(float*)); |
---|
[5516] | 417 | if (sts_data == NULL) |
---|
| 418 | { |
---|
| 419 | printf("ERROR: Memory for sts_data could not be allocated.\n"); |
---|
[5537] | 420 | return NULL; |
---|
[5516] | 421 | } |
---|
| 422 | |
---|
[5537] | 423 | // For each selected station, allocate space for its data |
---|
| 424 | len_sts_data = mytgs0[0].nt + POFFSET; // Max length of each timeseries (I think) |
---|
| 425 | for (i = 0; i < number_of_selected_stations; i++) |
---|
[5516] | 426 | { |
---|
| 427 | // Initialise sts_data to zero |
---|
[5537] | 428 | sts_data[i] = (float*)calloc(len_sts_data, sizeof(float)); |
---|
| 429 | if (sts_data[i] == NULL) |
---|
[5516] | 430 | { |
---|
| 431 | printf("ERROR: Memory for sts_data could not be allocated.\n"); |
---|
[5537] | 432 | return NULL; |
---|
[5516] | 433 | } |
---|
| 434 | |
---|
[5537] | 435 | ista = permutation[i]; // Get global index into mux data |
---|
| 436 | |
---|
| 437 | sts_data[i][mytgs0[0].nt] = (float)mytgs0[ista].geolat; |
---|
| 438 | sts_data[i][mytgs0[0].nt + 1] = (float)mytgs0[ista].geolon; |
---|
| 439 | sts_data[i][mytgs0[0].nt + 2] = (float)mytgs0[ista].z; |
---|
| 440 | sts_data[i][mytgs0[0].nt + 3] = (float)fros[ista]; |
---|
| 441 | sts_data[i][mytgs0[0].nt + 4] = (float)lros[ista]; |
---|
[5516] | 442 | } |
---|
[5539] | 443 | |
---|
[5516] | 444 | temp_sts_data = (float*)calloc(len_sts_data, sizeof(float)); |
---|
| 445 | |
---|
| 446 | /* Loop over all sources */ |
---|
| 447 | //FIXME: remove istart and istop they are not used. |
---|
| 448 | istart = -1; |
---|
| 449 | istop = -1; |
---|
| 450 | for (isrc = 0; isrc < numSrc; isrc++) |
---|
| 451 | { |
---|
| 452 | /* Read in data block from mux2 file */ |
---|
| 453 | muxFileName = muxFileNameArray[isrc]; |
---|
| 454 | if((fp = fopen(muxFileName, "r")) == NULL) |
---|
| 455 | { |
---|
| 456 | fprintf(stderr, "cannot open file %s\n", muxFileName); |
---|
[5537] | 457 | return NULL; |
---|
[5516] | 458 | } |
---|
| 459 | |
---|
| 460 | if (verbose){ |
---|
[5537] | 461 | printf("Reading mux file %s\n", muxFileName); |
---|
[5516] | 462 | } |
---|
| 463 | |
---|
| 464 | offset = sizeof(int) + nsta0*(sizeof(struct tgsrwg) + 2*sizeof(int)); |
---|
| 465 | fseek(fp, offset, 0); |
---|
| 466 | |
---|
| 467 | numData = getNumData(fros + isrc*nsta0, lros + isrc*nsta0, nsta0); |
---|
| 468 | muxData = (float*)malloc(numData*sizeof(float)); |
---|
| 469 | fread(muxData, numData*sizeof(float), 1, fp); |
---|
| 470 | fclose(fp); |
---|
| 471 | |
---|
[5537] | 472 | // loop over stations present in the permutation array |
---|
[5531] | 473 | // use ista with mux data |
---|
| 474 | // use i with the processed data to be returned |
---|
| 475 | |
---|
[5537] | 476 | for(i = 0; i < number_of_selected_stations; i++) |
---|
[5516] | 477 | { |
---|
[5537] | 478 | |
---|
| 479 | ista = permutation[i]; // Get global index into mux data |
---|
| 480 | |
---|
[5516] | 481 | /* fill the data0 array from the mux file, and weight it */ |
---|
[5537] | 482 | fillDataArray(ista, |
---|
| 483 | nsta0, |
---|
| 484 | mytgs0[ista].nt, |
---|
| 485 | mytgs0[ista].ig, |
---|
| 486 | fros + isrc*nsta0, |
---|
| 487 | lros + isrc*nsta0, |
---|
| 488 | temp_sts_data, |
---|
| 489 | &istart, |
---|
| 490 | &istop, |
---|
| 491 | muxData); |
---|
[5516] | 492 | |
---|
| 493 | /* weight appropriately and add */ |
---|
[5531] | 494 | for(k = 0; k < mytgs0[ista].nt; k++) |
---|
[5516] | 495 | { |
---|
[5537] | 496 | if((isdata(sts_data[i][k])) && isdata(temp_sts_data[k])) |
---|
[5516] | 497 | { |
---|
[5537] | 498 | sts_data[i][k] += temp_sts_data[k] * weights[isrc]; |
---|
[5516] | 499 | } |
---|
| 500 | else |
---|
| 501 | { |
---|
[5537] | 502 | sts_data[i][k] = NODATA; |
---|
[5516] | 503 | } |
---|
| 504 | } |
---|
| 505 | } |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | free(muxData); |
---|
| 509 | free(temp_sts_data); |
---|
| 510 | free(fros); |
---|
| 511 | free(lros); |
---|
| 512 | free(mytgs0); |
---|
| 513 | free(mytgs); |
---|
| 514 | |
---|
| 515 | return sts_data; |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | |
---|
| 519 | /* thomas */ |
---|
| 520 | void fillDataArray(int ista, int nsta, int nt, int ig, int *nst, int *nft, float *data, int *istart_p, int *istop_p, float *muxData) |
---|
| 521 | { |
---|
| 522 | int it, last_it, jsta; |
---|
| 523 | long int offset=0; |
---|
| 524 | |
---|
| 525 | |
---|
| 526 | last_it = -1; |
---|
| 527 | /* make arrays of starting and finishing time steps for the tide gauges */ |
---|
| 528 | /* and fill them from the file */ |
---|
| 529 | |
---|
| 530 | /* update start and stop timesteps for this gauge */ |
---|
| 531 | if (nst[ista]!= -1) |
---|
| 532 | { |
---|
| 533 | if(*istart_p == -1) |
---|
| 534 | { |
---|
| 535 | *istart_p = nst[ista]; |
---|
| 536 | } |
---|
| 537 | else |
---|
| 538 | { |
---|
| 539 | *istart_p = ((nst[ista] < *istart_p) ? nst[ista] : *istart_p); |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | if (nft[ista] != -1) |
---|
| 543 | { |
---|
| 544 | if (*istop_p == -1) |
---|
| 545 | { |
---|
| 546 | *istop_p = nft[ista]; |
---|
| 547 | } |
---|
| 548 | else |
---|
| 549 | { |
---|
| 550 | *istop_p = ((nft[ista] < *istop_p) ? nft[ista] : *istop_p); |
---|
| 551 | } |
---|
| 552 | } |
---|
| 553 | if (ig == -1 || nst[ista] == -1) /* currently ig==-1 => nst[ista]==-1 */ |
---|
| 554 | { |
---|
| 555 | /* gauge never started recording, or was outside of all grids, fill array with 0 */ |
---|
| 556 | for(it = 0; it < nt; it++) |
---|
| 557 | { |
---|
| 558 | data[it] = 0.0; |
---|
| 559 | } |
---|
| 560 | } |
---|
| 561 | else |
---|
| 562 | { |
---|
| 563 | for(it = 0; it < nt; it++) |
---|
| 564 | { |
---|
| 565 | last_it = it; |
---|
| 566 | /* skip t record of data block */ |
---|
| 567 | offset++; |
---|
| 568 | /* skip records from earlier tide gauges */ |
---|
| 569 | for(jsta = 0; jsta < ista; jsta++) |
---|
| 570 | if(it + 1 >= nst[jsta] && it + 1 <= nft[jsta]) |
---|
| 571 | offset++; |
---|
| 572 | |
---|
| 573 | /* deal with the tide gauge at hand */ |
---|
| 574 | if(it + 1 >= nst[ista] && it + 1 <= nft[ista]) |
---|
| 575 | /* gauge is recording at this time */ |
---|
| 576 | { |
---|
| 577 | memcpy(data + it, muxData + offset, sizeof(float)); |
---|
| 578 | offset++; |
---|
| 579 | } |
---|
| 580 | else if (it + 1 < nst[ista]) |
---|
| 581 | { |
---|
| 582 | /* gauge has not yet started recording */ |
---|
| 583 | data[it] = 0.0; |
---|
| 584 | } |
---|
| 585 | else |
---|
| 586 | /* gauge has finished recording */ |
---|
| 587 | { |
---|
| 588 | data[it] = NODATA; |
---|
| 589 | break; |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | /* skip records from later tide gauges */ |
---|
| 593 | for(jsta = ista + 1; jsta < nsta; jsta++) |
---|
| 594 | if(it + 1 >= nst[jsta] && it+1 <= nft[jsta]) |
---|
| 595 | offset++; |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | if(last_it < nt - 1) |
---|
| 599 | /* the loop was exited early because the gauge had finished recording */ |
---|
| 600 | for(it = last_it+1; it < nt; it++) |
---|
| 601 | data[it] = NODATA; |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | char isdata(float x) |
---|
| 606 | { |
---|
| 607 | //char value; |
---|
| 608 | if(x < NODATA + EPSILON && NODATA < x + EPSILON) |
---|
| 609 | { |
---|
| 610 | return 0; |
---|
| 611 | } |
---|
| 612 | else |
---|
| 613 | { |
---|
| 614 | return 1; |
---|
| 615 | } |
---|
| 616 | } |
---|
| 617 | |
---|
| 618 | |
---|
| 619 | long getNumData(const int *fros, const int *lros, const int nsta) |
---|
| 620 | /* calculates the number of data in the data block of a mux file */ |
---|
| 621 | /* based on the first and last recorded output steps for each gauge */ |
---|
| 622 | { |
---|
| 623 | int ista, last_output_step; |
---|
| 624 | long numData = 0; |
---|
| 625 | |
---|
| 626 | last_output_step = 0; |
---|
| 627 | for(ista = 0; ista < nsta; ista++) |
---|
| 628 | if(*(fros + ista) != -1) |
---|
| 629 | { |
---|
| 630 | numData += *(lros + ista) - *(fros + ista) + 1; |
---|
| 631 | last_output_step = (last_output_step < *(lros+ista) ? *(lros+ista):last_output_step); |
---|
| 632 | } |
---|
| 633 | numData += last_output_step*nsta; /* these are the t records */ |
---|
| 634 | return numData; |
---|
| 635 | } |
---|
| 636 | |
---|
| 637 | |
---|
| 638 | |
---|
| 639 | //------------------------------- |
---|
| 640 | // Method table for python module |
---|
| 641 | //------------------------------- |
---|
| 642 | static struct PyMethodDef MethodTable[] = { |
---|
| 643 | {"read_mux2", read_mux2, METH_VARARGS, "Print out"}, |
---|
| 644 | {NULL, NULL} |
---|
| 645 | }; |
---|
| 646 | |
---|
| 647 | // Module initialisation |
---|
| 648 | void initurs_ext(void){ |
---|
| 649 | Py_InitModule("urs_ext", MethodTable); |
---|
| 650 | |
---|
| 651 | import_array(); // Necessary for handling of NumPY structures |
---|
| 652 | } |
---|