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