00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <time.h>
00026 #include <sys/time.h>
00027 #include <math.h>
00028 #include <string.h>
00029 #include "jsoc_main.h"
00030 #include "astro.h"
00031 #include "cartography_cgem.c"
00032 #include "finterpolate.h"
00033 #include <complex.h>
00034 #include <fftw3.h>
00035 #include "flct4jsoc.c"
00036
00037 #define PI (M_PI)
00038 #define RADSINDEG (PI/180.)
00039 #define RAD2ARCSEC (648000./M_PI)
00040 #define SECINDAY (86400.)
00041 #define FOURK (4096)
00042 #define FOURK2 (16777216)
00043
00044
00045 #define INTERPOPT (0) // linear interpolation
00046
00047 #define ARRLENGTH(ARR) (sizeof(ARR) / sizeof(ARR[0]))
00048
00049 #define TEST 1
00050
00051
00052 #ifndef MIN
00053 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
00054 #endif
00055 #ifndef MAX
00056 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
00057 #endif
00058
00059 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
00060 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00061
00062 #define kNotSpecified "Not Specified"
00063 #define dpath "/home/jsoc/cvs/Development/JSOC"
00064
00065
00066
00067
00068 #define PIX_X(wx,wy) ((((wx-crval1)*cosa + (wy-crval1)*sina)/cdelt)+crpix1)
00069 #define PIX_Y(wx,wy) ((((wy-crval2)*cosa - (wx-crval2)*sina)/cdelt)+crpix2)
00070 #define WX(pix_x,pix_y) (((pix_x-crpix1)*cosa - (pix_y-crpix2)*sina)*cdelt+crval1)
00071 #define WY(pix_x,pix_y) (((pix_y-crpix2)*cosa + (pix_x-crpix1)*sina)*cdelt+crval2)
00072
00073
00074 struct ephemeris {
00075 double crln_obs, crlt_obs;
00076 double rSun, asd, pa;
00077 double crval1, crval2, crpix1, crpix2, cdelt1, cdelt2;
00078 TIME t_rec, t_obs;
00079 char *ctype1, *ctype2;
00080 };
00081
00082
00083 struct reqInfo {
00084 double xc, yc;
00085 int ncol, nrow;
00086 double dx, dy;
00087 int proj;
00088 };
00089
00090
00091
00092 char *mapName[] = {"PlateCarree", "Cassini-Soldner", "Mercator",
00093 "LambertCylindrical", "Sanson-Flamsteed", "gnomonic", "Postel",
00094 "stereographic", "orthographic", "LambertAzimuthal"};
00095 char *wcsCode[] = {"CAR", "CAS", "MER", "CEA", "GLS", "TAN", "ARC", "STG",
00096 "SIN", "ZEA"};
00097
00098
00099
00100
00101 int PC2Mercator(DRMS_Record_t *inRec0, DRMS_Record_t *inRec1, char *segQuery,
00102 int *dims_mer, double **bz0_mer_ptr, double **bz1_mer_ptr);
00103
00104 void pc2m(int *dims, float *bz, struct ephemeris *ephem,
00105 int *dims_mer, float *bz_mer);
00106
00107
00108 void scaleflct(DRMS_Record_t *inRec, double dt, int *dims_mer, double *v);
00109
00110
00111 int Mercator2PC(DRMS_Record_t *inRec, int *dims_mer, double *vx_mer, double *vy_mer, double *vm_mer,
00112 int *dims, float *vx, float *vy, float *vm);
00113
00114 void m2pc(int *dims_mer, float *map_mer, struct ephemeris *ephem,
00115 int *dims, float *map);
00116
00117
00118 int writeV(DRMS_Record_t *inRec, DRMS_Record_t *outRec,
00119 int *dims, float *vx, float *vy, float *vm);
00120
00121 int writeSupp(DRMS_Record_t *inRec, DRMS_Record_t *outRec, int *dims_mer,
00122 double *vx_mer, double *vy_mer, double *vm_mer, double *bz0_mer, double *bz1_mer);
00123
00124
00125 int getEphemeris(DRMS_Record_t *inRec, struct ephemeris *ephem);
00126
00127
00128 void performSampling(float *inData, float *outData, float *u, float *v, int *dims_in, int *dims_out);
00129
00130
00131 float nnb (float *f, int nx, int ny, float x, float y);
00132
00133
00134
00135
00136 char *module_name = "cgem_flct";
00137
00138 ModuleArgs_t module_args[] =
00139 {
00140 {ARG_STRING, "in", kNotSpecified, "Input series."},
00141 {ARG_STRING, "out", kNotSpecified, "Output series."},
00142 {ARG_STRING, "seg", "Bz", "Iutput segment."},
00143 {ARG_DOUBLE, "sigma", "5.", "For FLCT. Images modulated by gaussian of width sigma pixels."},
00144 {ARG_FLAG, "t", "", "For FLCT, evoke threshold below."},
00145 {ARG_FLAG, "k", "", "For FLCT, evoke filtering below."},
00146 {ARG_DOUBLE, "thresh", "0.1", "For FLCT. Pixels below threshold ignored. In Gauss if greater than 1, relative value of max value if between 0 and 1."},
00147 {ARG_DOUBLE, "kr", "0.25", "For FLCT. Filter subimages w/gaussian w/ roll-off wavenumber kr."},
00148 {ARG_FLAG, "a", "", "Force threshold to be absolute value"},
00149 {ARG_FLAG, "q", "", "Quiet mode for FLCT"},
00150 {ARG_END}
00151 };
00152
00153 int DoIt(void)
00154 {
00155
00156 int status = DRMS_SUCCESS;
00157
00158
00159
00160 char *inQuery = NULL, *segQuery = NULL;
00161 char *outQuery = NULL;
00162
00163 inQuery = (char *) params_get_str(&cmdparams, "in");
00164 segQuery = (char *) params_get_str(&cmdparams, "seg");
00165 outQuery = (char *) params_get_str(&cmdparams, "out");
00166
00167 if (!strcmp(inQuery, kNotSpecified) ||
00168 !strcmp(segQuery, kNotSpecified) ||
00169 !strcmp(outQuery, kNotSpecified)) {
00170 DIE("Input/output not available");
00171 }
00172
00173 double sigma = params_get_double(&cmdparams, "sigma");
00174 int useThresh = params_isflagset(&cmdparams, "t");
00175 int doFilter = params_isflagset(&cmdparams, "k");
00176 double thresh = params_get_double(&cmdparams, "thresh");
00177 double kr = params_get_double(&cmdparams, "kr");
00178 int quiet = params_isflagset(&cmdparams, "q");
00179 int absthresh = params_isflagset(&cmdparams, "a");
00180
00181
00182
00183 DRMS_RecordSet_t *inRS = drms_open_records(drms_env, inQuery, &status);
00184 int nrecs = inRS->n;
00185 if (status || nrecs <= 2 || !inRS) DIE("Input data series error");
00186 DRMS_Segment_t *inSeg = drms_segment_lookup(inRS->records[0], segQuery);
00187 if (!inSeg) {
00188 drms_close_records(inRS, DRMS_FREE_RECORD);
00189 DIE("Requested segment not available\n");
00190 }
00191
00192
00193
00194 DRMS_RecordSet_t *outRS = drms_create_records(drms_env, nrecs - 2, outQuery, DRMS_PERMANENT, &status);
00195 if (status || !outRS) {
00196 drms_close_records(outRS, DRMS_FREE_RECORD);
00197 DIE("Error creating output series\n");
00198 }
00199
00200
00201
00202 for (int irec = 0; irec < nrecs - 2; irec++) {
00203
00204 DRMS_Record_t *inRec0 = inRS->records[irec];
00205 DRMS_Record_t *inRec1 = inRS->records[irec + 2];
00206 DRMS_Record_t *inRec_target = inRS->records[irec + 1];
00207 DRMS_Record_t *outRec = outRS->records[irec];
00208 printf("==============\nProcessing rec pairs #%d of %d, ", irec + 1, nrecs - 2); fflush(stdout);
00209
00210
00211
00212 TIME tobs0 = drms_getkey_time(inRec0, "T_OBS", &status);
00213 TIME tobs1 = drms_getkey_time(inRec1, "T_OBS", &status);
00214 TIME trec = drms_getkey_time(inRec_target, "T_REC", &status);
00215 double dt = tobs1 - tobs0;
00216 char trec_str[100];
00217 sprint_time(trec_str, trec, "TAI", 0);
00218 printf("TREC=[%s], dt=%f... ", trec_str, dt);
00219
00220
00221
00222 DRMS_Segment_t *inSeg0 = NULL, *inSeg1 = NULL;
00223 inSeg0 = drms_segment_lookup(inRec0, segQuery);
00224 inSeg1 = drms_segment_lookup(inRec1, segQuery);
00225 if (!inSeg0 || !inSeg1) {
00226 SHOW("\nSegment check error, frames skipped.\n");
00227 continue;
00228 }
00229 if ((inSeg0->axis[0] != inSeg1->axis[0]) ||
00230 (inSeg0->axis[1] != inSeg1->axis[1])) {
00231 SHOW("\nImage dimensions do not match, frames skipped.\n");
00232 continue;
00233 }
00234 int nx = inSeg0->axis[0], ny = inSeg0->axis[1], nxny = nx * ny;
00235 int dims[2] = {nx, ny};
00236
00237
00238
00239 int dims_mer[2];
00240 double *bz0_mer = NULL, *bz1_mer = NULL;
00241
00242 if (PC2Mercator(inRec0, inRec1, segQuery, dims_mer, &bz0_mer, &bz1_mer)) {
00243 if (bz0_mer) free(bz0_mer); if (bz1_mer) free(bz1_mer);
00244 SHOW("Carree to Mercator mapping error, frames skipped.\n");
00245 continue;
00246 }
00247
00248
00249
00250 int nx_mer = dims_mer[0], ny_mer = dims_mer[1], nxny_mer = nx_mer * ny_mer;
00251 double *vx_mer = (double *) (malloc(nxny_mer * sizeof(double)));
00252 double *vy_mer = (double *) (malloc(nxny_mer * sizeof(double)));
00253 double *vm_mer = (double *) (malloc(nxny_mer * sizeof(double)));
00254
00255 if (flct4jsoc(dims_mer, bz0_mer, bz1_mer,
00256 1.0, 1.0, sigma, useThresh, thresh, doFilter, kr,
00257 quiet, absthresh,
00258 vx_mer, vy_mer, vm_mer)) {
00259 if (bz0_mer) free(bz0_mer); if (bz1_mer) free(bz1_mer);
00260 free(vx_mer); free(vy_mer); free(vm_mer);
00261 SHOW("FLCT error, frames skipped.\n");
00262 continue;
00263 }
00264
00265 #ifndef TEST
00266 if (bz0_mer) free(bz0_mer); if (bz1_mer) free(bz1_mer);
00267 #endif
00268
00269
00270
00271
00272 #ifndef TEST
00273 scaleflct(inRec_target, dt, dims_mer, vx_mer);
00274 scaleflct(inRec_target, dt, dims_mer, vy_mer);
00275 #endif
00276
00277 float *vx = (float *) (malloc(nxny * sizeof(float)));
00278 float *vy = (float *) (malloc(nxny * sizeof(float)));
00279 float *vm = (float *) (malloc(nxny * sizeof(float)));
00280
00281 if (Mercator2PC(inRec1, dims_mer, vx_mer, vy_mer, vm_mer, dims, vx, vy, vm)) {
00282 if (bz0_mer) free(bz0_mer); if (bz1_mer) free(bz1_mer);
00283 free(vx_mer); free(vy_mer); free(vm_mer); free(vx); free(vy); free(vm);
00284 SHOW("Mercator to Carree mapping error, frames skipped.\n");
00285 continue;
00286 }
00287
00288 #ifndef TEST
00289 free(vx_mer); free(vy_mer); free(vm_mer);
00290 #endif
00291
00292
00293
00294 if (writeV(inRec_target, outRec, dims, vx, vy, vm)) {
00295 SHOW("Output error, frames skipped.\n");
00296 continue;
00297 }
00298
00299 #ifdef TEST
00300 if (writeSupp(inRec1, outRec, dims_mer,
00301 vx_mer, vy_mer, vm_mer, bz0_mer, bz1_mer)) {
00302 SHOW("Output intermediate data error, carrying on.\n");
00303 }
00304 #endif
00305
00306 SHOW("done.\n")
00307
00308 }
00309
00310 drms_close_records(inRS, DRMS_FREE_RECORD);
00311 drms_close_records(outRS, DRMS_INSERT_RECORD);
00312
00313 return DRMS_SUCCESS;
00314
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 int PC2Mercator(DRMS_Record_t *inRec0, DRMS_Record_t *inRec1, char *segQuery,
00326 int *dims_mer, double **bz0_mer_ptr, double **bz1_mer_ptr)
00327 {
00328 int status = 0;
00329
00330
00331
00332 struct ephemeris ephem0, ephem1;
00333 if (getEphemeris(inRec0, &ephem0) || getEphemeris(inRec1, &ephem1)) {
00334 SHOW("\nEphemeris error... ");
00335 return 1;
00336 }
00337 if (strstr(ephem0.ctype1, "CAR") == NULL ||
00338 strstr(ephem0.ctype2, "CAR") == NULL ||
00339 strstr(ephem1.ctype1, "CAR") == NULL ||
00340 strstr(ephem1.ctype2, "CAR") == NULL) {
00341 SHOW("\nMust be in Plate Carree ");
00342 return 1;
00343 }
00344
00345
00346
00347 DRMS_Segment_t *inSeg0 = NULL, *inSeg1 = NULL;
00348 inSeg0 = drms_segment_lookup(inRec0, segQuery);
00349 inSeg1 = drms_segment_lookup(inRec1, segQuery);
00350 int nx = inSeg0->axis[0], ny = inSeg0->axis[1];
00351 int nxny = nx * ny, dims[2] = {nx, ny};
00352
00353
00354
00355
00356 double yc_mer = ephem0.crval2 * RADSINDEG;
00357 double dy_mer = ephem0.cdelt2 * RADSINDEG;
00358 int nx_mer = nx;
00359 double th_i = yc_mer - dy_mer * ny / 2.;
00360 double th_f = yc_mer + dy_mer * ny / 2.;
00361 int ny_mer = ceil(log((tan(th_f) + 1. / cos(th_f)) / (tan(th_i) + 1. / cos(th_i))) / dy_mer);
00362 int nxny_mer = nx_mer * ny_mer;
00363 dims_mer[0] = nx_mer; dims_mer[1] = ny_mer;
00364
00365
00366
00367
00368 DRMS_Array_t *inArray0 = drms_segment_read(inSeg0, DRMS_TYPE_FLOAT, &status);
00369 if (status) {
00370 SHOW("\nArray read error... ");
00371 return 1;
00372 }
00373 float *bz0 = (float *)inArray0->data;
00374 float *bz0_mer = (float *) (malloc(nxny_mer * sizeof(float)));
00375
00376 pc2m(dims, bz0, &ephem0, dims_mer, bz0_mer);
00377
00378 *bz0_mer_ptr = (double *) (malloc(nxny_mer * sizeof(double)));
00379 for (int i = 0; i < nxny_mer; i++) {
00380 (*bz0_mer_ptr)[i] = bz0_mer[i];
00381 }
00382 drms_free_array(inArray0); free(bz0_mer);
00383
00384
00385
00386 DRMS_Array_t *inArray1 = drms_segment_read(inSeg1, DRMS_TYPE_FLOAT, &status);
00387 if (status) {
00388 SHOW("\nArray read error... ");
00389 free(*bz0_mer_ptr);
00390 return 1;
00391 }
00392 float *bz1 = (float *)inArray1->data;
00393 float *bz1_mer = (float *) (malloc(nxny_mer * sizeof(float)));
00394
00395 pc2m(dims, bz1, &ephem1, dims_mer, bz1_mer);
00396
00397 *bz1_mer_ptr = (double *) (malloc(nxny_mer * sizeof(double)));
00398 for (int i = 0; i < nxny_mer; i++) {
00399 (*bz1_mer_ptr)[i] = bz1_mer[i];
00400 }
00401 drms_free_array(inArray1); free(bz1_mer);
00402
00403
00404
00405 return 0;
00406 }
00407
00408
00409
00410
00411
00412
00413
00414
00415 void pc2m(int *dims, float *bz, struct ephemeris *ephem,
00416 int *dims_mer, float *bz_mer)
00417 {
00418
00419 int nx = dims[0], ny = dims[1], nxny = nx * ny;
00420 int nx_mer = dims_mer[0], ny_mer = dims_mer[1], nxny_mer = nx_mer * ny_mer;
00421
00422 int isCarr = (strstr(ephem->ctype1, "CRLN") != NULL) ? 1 : 0;
00423
00424 double xc = ((isCarr) ? (ephem->crval1 - ephem->crln_obs) : (ephem->crval1)) * RADSINDEG;
00425 double yc = ephem->crval2 * RADSINDEG;
00426 double dx = ephem->cdelt1 * RADSINDEG;
00427 double dy = ephem->cdelt2 * RADSINDEG;
00428 double colc = ephem->crpix1 - 1;
00429 double rowc = ephem->crpix1 - 1;
00430
00431
00432 double xc_mer = xc, yc_mer = yc, dx_mer = dx, dy_mer = dy;
00433 double rowc_mer = (ny_mer - 1.) / 2., colc_mer = (nx_mer - 1.) / 2.;
00434
00435
00436
00437 float *col_work = (float *) (malloc(nxny_mer * sizeof(float)));
00438 float *row_work = (float *) (malloc(nxny_mer * sizeof(float)));
00439
00440
00441
00442 double x, y, x_mer, y_mer, lat, lon;
00443 int idx;
00444 for (int row_mer = 0; row_mer < ny_mer; row_mer++) {
00445 y_mer = (row_mer - rowc_mer) * dy_mer;
00446 for (int col_mer = 0; col_mer < nx_mer; col_mer++) {
00447 x_mer = (col_mer - colc_mer) * dx_mer;
00448 idx = row_mer * nx_mer + col_mer;
00449 if (plane2sphere(x_mer, y_mer, yc_mer, xc_mer, &lat, &lon, MERCATOR)) {
00450 col_work[idx] = DRMS_MISSING_FLOAT;
00451 row_work[idx] = DRMS_MISSING_FLOAT;
00452 continue;
00453 }
00454 if (sphere2plane(lat, lon, yc, xc, &x, &y, RECTANGULAR)) {
00455 col_work[idx] = DRMS_MISSING_FLOAT;
00456 row_work[idx] = DRMS_MISSING_FLOAT;
00457 continue;
00458 }
00459 col_work[idx] = x / dx + colc;
00460 row_work[idx] = y / dy + rowc;
00461 }
00462 }
00463
00464
00465
00466 performSampling(bz, bz_mer, col_work, row_work, dims, dims_mer);
00467
00468 free(col_work); free(row_work);
00469
00470 }
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481 void scaleflct(DRMS_Record_t *inRec, double dt, int *dims_mer, double *v)
00482 {
00483
00484 int status = 0;
00485
00486 struct ephemeris ephem;
00487 status = getEphemeris(inRec, &ephem);
00488
00489 double rSun_ref = drms_getkey_float(inRec, "RSUN_REF", &status);
00490 double ds = ephem.cdelt1 * RADSINDEG * rSun_ref;
00491 double v0 = ds / dt;
00492 printf("cd1=%f, ds=%f, dt=%f, v0=%f\n", ephem.cdelt1, ds, dt, v0);
00493
00494
00495
00496 int nx_mer = dims_mer[0], ny_mer = dims_mer[1], nxny_mer = nx_mer * ny_mer;
00497 double rowc_mer = (ny_mer - 1.) / 2., colc_mer = (nx_mer - 1.) / 2.;
00498 double dx_mer = ephem.cdelt1 * RADSINDEG;
00499 double dy_mer = ephem.cdelt2 * RADSINDEG;
00500 int isCarr = (strstr(ephem.ctype1, "CRLN") != NULL) ? 1 : 0;
00501 double xc_mer = ((isCarr) ? (ephem.crval1 - ephem.crln_obs) : (ephem.crval1)) * RADSINDEG;
00502 double yc_mer = ephem.crval2 * RADSINDEG;
00503
00504 double x_mer, y_mer, lat, lon;
00505 int idx;
00506 for (int row_mer = 0; row_mer < ny_mer; row_mer++) {
00507 y_mer = (row_mer - rowc_mer) * dy_mer;
00508 for (int col_mer = 0; col_mer < nx_mer; col_mer++) {
00509 x_mer = (col_mer - colc_mer) * dx_mer;
00510 idx = row_mer * nx_mer + col_mer;
00511 if (plane2sphere(x_mer, y_mer, yc_mer, xc_mer, &lat, &lon, MERCATOR)) {
00512 v[idx] = DRMS_MISSING_DOUBLE;
00513 continue;
00514 }
00515 v[idx] *= (v0 * cos(lat));
00516 }
00517 }
00518
00519 }
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529 int Mercator2PC(DRMS_Record_t *inRec, int *dims_mer, double *vx_mer, double *vy_mer, double *vm_mer,
00530 int *dims, float *vx, float *vy, float *vm)
00531 {
00532 int status = 0;
00533
00534
00535
00536 struct ephemeris ephem;
00537 if (getEphemeris(inRec, &ephem)) {
00538 SHOW("\nEphemeris error... ");
00539 return 1;
00540 }
00541
00542
00543
00544 int nx_mer = dims_mer[0], ny_mer = dims_mer[1], nxny_mer = nx_mer * ny_mer;
00545
00546 float *vx_mer_t = (float *) (malloc(nxny_mer * sizeof(float)));
00547 float *vy_mer_t = (float *) (malloc(nxny_mer * sizeof(float)));
00548 float *vm_mer_t = (float *) (malloc(nxny_mer * sizeof(float)));
00549 for (int i = 0; i < nxny_mer; i++) {
00550 vx_mer_t[i] = vx_mer[i];
00551 vy_mer_t[i] = vy_mer[i];
00552 vm_mer_t[i] = vm_mer[i];
00553 }
00554
00555 m2pc(dims_mer, vx_mer_t, &ephem, dims, vx);
00556 m2pc(dims_mer, vy_mer_t, &ephem, dims, vy);
00557 m2pc(dims_mer, vm_mer_t, &ephem, dims, vy);
00558
00559 free(vx_mer_t); free(vy_mer_t); free(vm_mer_t);
00560
00561 return 0;
00562 }
00563
00564
00565
00566
00567
00568
00569
00570
00571 void m2pc(int *dims_mer, float *map_mer, struct ephemeris *ephem,
00572 int *dims, float *map)
00573 {
00574
00575 int nx = dims[0], ny = dims[1], nxny = nx * ny;
00576 int nx_mer = dims_mer[0], ny_mer = dims_mer[1], nxny_mer = nx_mer * ny_mer;
00577
00578 int isCarr = (strstr(ephem->ctype1, "CRLN") != NULL) ? 1 : 0;
00579
00580 double xc = ((isCarr) ? (ephem->crval1 - ephem->crln_obs) : (ephem->crval1)) * RADSINDEG;
00581 double yc = ephem->crval2 * RADSINDEG;
00582 double dx = ephem->cdelt1 * RADSINDEG;
00583 double dy = ephem->cdelt2 * RADSINDEG;
00584 double colc = ephem->crpix1 - 1;
00585 double rowc = ephem->crpix2 - 1;
00586
00587 double xc_mer = xc, yc_mer = yc, dx_mer = dx, dy_mer = dy;
00588 double rowc_mer = (ny_mer - 1.) / 2., colc_mer = (nx_mer - 1.) / 2.;
00589
00590
00591
00592 float *col_work = (float *) (malloc(nxny * sizeof(float)));
00593 float *row_work = (float *) (malloc(nxny * sizeof(float)));
00594
00595
00596
00597 double x, y, x_mer, y_mer, lat, lon;
00598 int idx;
00599 for (int row = 0; row < ny; row++) {
00600 y = (row - rowc) * dy;
00601 for (int col = 0; col < nx; col++) {
00602 x = (col - colc) * dx;
00603 idx = row * nx + col;
00604 if (plane2sphere(x, y, yc, xc, &lat, &lon, RECTANGULAR)) {
00605 col_work[idx] = DRMS_MISSING_FLOAT;
00606 row_work[idx] = DRMS_MISSING_FLOAT;
00607 continue;
00608 }
00609 if (sphere2plane(lat, lon, yc_mer, xc_mer, &x_mer, &y_mer, MERCATOR)) {
00610 col_work[idx] = DRMS_MISSING_FLOAT;
00611 row_work[idx] = DRMS_MISSING_FLOAT;
00612 continue;
00613 }
00614 col_work[idx] = x_mer / dx_mer + colc_mer;
00615 row_work[idx] = y_mer / dy_mer + rowc_mer;
00616 }
00617 }
00618
00619
00620
00621
00622 performSampling(map_mer, map, col_work, row_work, dims_mer, dims);
00623
00624 free(col_work); free(row_work);
00625
00626 }
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636 int writeV(DRMS_Record_t *inRec, DRMS_Record_t *outRec,
00637 int *dims, float *vx, float *vy, float *vm)
00638 {
00639 int status = 0;
00640
00641 DRMS_Segment_t *outSeg;
00642 DRMS_Array_t *outArray;
00643
00644 int nx = dims[0], ny = dims[1], nxny = nx * ny;
00645
00646
00647
00648 outSeg = drms_segment_lookup(outRec, "Vx");
00649 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, dims, vx, &status);
00650 if (status) {
00651 SHOW("\nOutput vx error... ");
00652 free(vx); free(vy); free(vm);
00653 return status;
00654 }
00655 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
00656 outArray->israw = 0;
00657 outArray->bzero = outSeg->bzero;
00658 outArray->bscale = outSeg->bscale;
00659 status = drms_segment_write(outSeg, outArray, 0);
00660 if (status) {
00661 SHOW("\nOutput vx write error... ");
00662 free(vx); free(vy); free(vm);
00663 return status;
00664 }
00665 drms_free_array(outArray);
00666
00667
00668
00669 for (int i = 0; i < nxny; i++) {
00670 vy[i] *= (-1.);
00671 }
00672 outSeg = drms_segment_lookup(outRec, "Vy");
00673 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, dims, vy, &status);
00674 if (status) {
00675 SHOW("\nOutput vy error... ");
00676 free(vy); free(vm);
00677 return status;
00678 }
00679 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
00680 outArray->israw = 0;
00681 outArray->bzero = outSeg->bzero;
00682 outArray->bscale = outSeg->bscale;
00683 status = drms_segment_write(outSeg, outArray, 0);
00684 if (status) {
00685 SHOW("\nOutput vy write error... ");
00686 free(vy); free(vm);
00687 return status;
00688 }
00689 drms_free_array(outArray);
00690
00691
00692
00693 outSeg = drms_segment_lookup(outRec, "Vm");
00694 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, dims, vm, &status);
00695 if (status) {
00696 SHOW("\nOutput vm error... ");
00697 free(vm);
00698 return status;
00699 }
00700 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
00701 outArray->israw = 0;
00702 outArray->bzero = outSeg->bzero;
00703 outArray->bscale = outSeg->bscale;
00704 status = drms_segment_write(outSeg, outArray, 0);
00705 if (status) {
00706 SHOW("\nOutput vm write error... ");
00707 free(vm);
00708 return status;
00709 }
00710 drms_free_array(outArray);
00711
00712
00713
00714 drms_copykeys(outRec, inRec, 0, kDRMS_KeyClass_Explicit);
00715
00716 TIME val, trec, tnow, UNIX_epoch = -220924792.000;
00717 tnow = (double)time(NULL);
00718 tnow += UNIX_epoch;
00719 drms_setkey_time(outRec, "DATE", tnow);
00720
00721 return 0;
00722 }
00723
00724
00725
00726
00727
00728
00729
00730
00731 int writeSupp(DRMS_Record_t *inRec, DRMS_Record_t *outRec, int *dims_mer,
00732 double *vx_mer, double *vy_mer, double *vm_mer, double *bz0_mer, double *bz1_mer)
00733 {
00734 int status = 0;
00735
00736 DRMS_Segment_t *outSeg;
00737 DRMS_Array_t *outArray;
00738
00739 int nx_mer = dims_mer[0], ny_mer = dims_mer[1], nxny_mer = nx_mer * ny_mer;
00740
00741
00742
00743 outSeg = drms_segment_lookup(outRec, "Vx_mer");
00744 outArray = drms_array_create(DRMS_TYPE_DOUBLE, 2, dims_mer, vx_mer, &status);
00745 if (status) {
00746 SHOW("\nOutput vx_mer error... ");
00747 free(vx_mer); free(vy_mer); free(vm_mer); free(bz0_mer); free(bz1_mer);
00748 return status;
00749 }
00750 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
00751 outArray->israw = 0;
00752 outArray->bzero = outSeg->bzero;
00753 outArray->bscale = outSeg->bscale;
00754 status = drms_segment_write(outSeg, outArray, 0);
00755 if (status) {
00756 SHOW("\nOutput vx_mer write error... ");
00757 free(vx_mer); free(vy_mer); free(vm_mer); free(bz0_mer); free(bz1_mer);
00758 return status;
00759 }
00760 drms_free_array(outArray);
00761
00762
00763
00764 outSeg = drms_segment_lookup(outRec, "Vy_mer");
00765 outArray = drms_array_create(DRMS_TYPE_DOUBLE, 2, dims_mer, vy_mer, &status);
00766 if (status) {
00767 SHOW("\nOutput vy_mer error... ");
00768 free(vy_mer); free(vm_mer); free(bz0_mer); free(bz1_mer);
00769 return status;
00770 }
00771 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
00772 outArray->israw = 0;
00773 outArray->bzero = outSeg->bzero;
00774 outArray->bscale = outSeg->bscale;
00775 status = drms_segment_write(outSeg, outArray, 0);
00776 if (status) {
00777 SHOW("\nOutput vy_mer write error... ");
00778 free(vy_mer); free(vm_mer); free(bz0_mer); free(bz1_mer);
00779 return status;
00780 }
00781 drms_free_array(outArray);
00782
00783
00784
00785 outSeg = drms_segment_lookup(outRec, "Vm_mer");
00786 outArray = drms_array_create(DRMS_TYPE_DOUBLE, 2, dims_mer, vm_mer, &status);
00787 if (status) {
00788 SHOW("\nOutput vm_mer error... ");
00789 free(vm_mer); free(bz0_mer); free(bz1_mer);
00790 return status;
00791 }
00792 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
00793 outArray->israw = 0;
00794 outArray->bzero = outSeg->bzero;
00795 outArray->bscale = outSeg->bscale;
00796 status = drms_segment_write(outSeg, outArray, 0);
00797 if (status) {
00798 SHOW("\nOutput vm_mer write error... ");
00799 free(vm_mer); free(bz0_mer); free(bz1_mer);
00800 return status;
00801 }
00802 drms_free_array(outArray);
00803
00804
00805
00806 outSeg = drms_segment_lookup(outRec, "Bz0_mer");
00807 outArray = drms_array_create(DRMS_TYPE_DOUBLE, 2, dims_mer, bz0_mer, &status);
00808 if (status) {
00809 SHOW("\nOutput bz0_mer error... ");
00810 free(bz0_mer); free(bz1_mer);
00811 return status;
00812 }
00813 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
00814 outArray->israw = 0;
00815 outArray->bzero = outSeg->bzero;
00816 outArray->bscale = outSeg->bscale;
00817 status = drms_segment_write(outSeg, outArray, 0);
00818 if (status) {
00819 SHOW("\nOutput bz0_mer write error... ");
00820 free(bz0_mer); free(bz1_mer);
00821 return status;
00822 }
00823 drms_free_array(outArray);
00824
00825
00826
00827 outSeg = drms_segment_lookup(outRec, "Bz1_mer");
00828 outArray = drms_array_create(DRMS_TYPE_DOUBLE, 2, dims_mer, bz1_mer, &status);
00829 if (status) {
00830 SHOW("\nOutput bz1_mer error... ");
00831 free(bz1_mer);
00832 return status;
00833 }
00834 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
00835 outArray->israw = 0;
00836 outArray->bzero = outSeg->bzero;
00837 outArray->bscale = outSeg->bscale;
00838 status = drms_segment_write(outSeg, outArray, 0);
00839 if (status) {
00840 SHOW("\nOutput bz1_mer write error... ");
00841 free(bz1_mer);
00842 return status;
00843 }
00844 drms_free_array(outArray);
00845
00846 return 0;
00847 }
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857 int getEphemeris(DRMS_Record_t *inRec, struct ephemeris *ephem)
00858 {
00859
00860 int status = 0;
00861
00862 double crota2 = drms_getkey_float(inRec, "CROTA2", &status);
00863 double sina = sin(crota2 * RADSINDEG);
00864 double cosa = cos(crota2 * RADSINDEG);
00865
00866 ephem->pa = - crota2 * RADSINDEG;
00867 ephem->crlt_obs = drms_getkey_float(inRec, "CRLT_OBS", &status);
00868 ephem->crln_obs = drms_getkey_float(inRec, "CRLN_OBS", &status);
00869
00870 ephem->crval1 = drms_getkey_double(inRec, "CRVAL1", &status);
00871 ephem->crval2 = drms_getkey_double(inRec, "CRVAl2", &status);
00872 ephem->crpix1 = drms_getkey_double(inRec, "CRPIX1", &status);
00873 ephem->crpix2 = drms_getkey_double(inRec, "CRPIX2", &status);
00874 ephem->cdelt1 = drms_getkey_double(inRec, "CDELT1", &status);
00875 ephem->cdelt2 = drms_getkey_double(inRec, "CDELT2", &status);
00876
00877 ephem->asd = drms_getkey_double(inRec, "RSUN_OBS", &status);
00878 if (status) {
00879 double dSun = drms_getkey_float(inRec, "DSUN_OBS", &status);
00880 double rSun_ref = drms_getkey_float(inRec, "RSUN_REF", &status);
00881 if (status) rSun_ref = 6.96e8;
00882 ephem->asd = asin(rSun_ref / dSun);
00883 }
00884 ephem->rSun = ephem->asd * RAD2ARCSEC / ephem->cdelt1;
00885
00886 ephem->t_rec = drms_getkey_time(inRec, "T_REC", &status);
00887 ephem->t_obs = drms_getkey_time(inRec, "T_OBS", &status);
00888
00889 ephem->ctype1 = drms_getkey_string(inRec, "CTYPE1", &status);
00890 ephem->ctype2 = drms_getkey_string(inRec, "CTYPE2", &status);
00891
00892 return 0;
00893
00894 }
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905 void performSampling(float *inData, float *outData, float *u, float *v,
00906 int *dims_in, int *dims_out)
00907 {
00908
00909 struct fint_struct pars;
00910
00911 switch (INTERPOPT) {
00912 case 0:
00913 init_finterpolate_wiener(&pars, 6, 1, 6, 2, 1, 1, NULL, dpath);
00914 break;
00915 case 1:
00916 init_finterpolate_cubic_conv(&pars, 1., 3.);
00917 break;
00918 case 2:
00919 init_finterpolate_linear(&pars, 1.);
00920 break;
00921 case 3:
00922 default:
00923 break;
00924 }
00925
00926 int ind;
00927 int ncol = dims_out[0], nrow = dims_out[1];
00928 if (INTERPOPT == 3) {
00929 for (int row = 0; row < nrow; row++) {
00930 for (int col = 0; col < ncol; col++) {
00931 ind = row * ncol + col;
00932 outData[ind] = nnb(inData, dims_in[0], dims_in[1], u[ind], v[ind]);
00933 }
00934 }
00935 } else {
00936
00937 finterpolate(&pars, inData, u, v, outData,
00938 dims_in[0], dims_in[1], dims_in[0], ncol, nrow, ncol, DRMS_MISSING_FLOAT);
00939 }
00940
00941 }
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951 float nnb (float *f, int nx, int ny, float x, float y)
00952 {
00953
00954 if (x <= -0.5 || y <= -0.5 || x > nx - 0.5 || y > ny - 0.5)
00955 return DRMS_MISSING_FLOAT;
00956 int ilow = floor (x);
00957 int jlow = floor (y);
00958 int i = ((x - ilow) > 0.5) ? ilow + 1 : ilow;
00959 int j = ((y - jlow) > 0.5) ? jlow + 1 : jlow;
00960 return f[j * nx + i];
00961
00962 }