00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #include <stdio.h>
00070 #include <stdlib.h>
00071 #include <time.h>
00072 #include <sys/time.h>
00073 #include <math.h>
00074 #include <string.h>
00075 #include "jsoc_main.h"
00076 #include "astro.h"
00077 #include "fstats.h"
00078 #include "cartography.c"
00079 #include "fresize.h"
00080 #include "finterpolate.h"
00081 #include "img2helioVector.c"
00082 #include "copy_me_keys.c"
00083 #include "errorprop.c"
00084
00085 #include <mkl_blas.h>
00086 #include <mkl_service.h>
00087 #include <mkl_lapack.h>
00088 #include <mkl_vml_functions.h>
00089 #include <omp.h>
00090
00091 #define PI (M_PI)
00092 #define RADSINDEG (PI/180.)
00093 #define RAD2ARCSEC (648000./M_PI)
00094 #define SECINDAY (86400.)
00095 #define FOURK (4096)
00096 #define FOURK2 (16777216)
00097
00098
00099 #define NYQVIST (0.015)
00100
00101 #ifndef MIN
00102 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
00103 #endif
00104 #ifndef MAX
00105 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
00106 #endif
00107
00108 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
00109 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00110
00111
00112
00113
00114 #define PIX_X(wx,wy) ((((wx-crvalx)*cosa + (wy-crvaly)*sina)/cdelt)+crpix1)
00115 #define PIX_Y(wx,wy) ((((wy-crvaly)*cosa - (wx-crvalx)*sina)/cdelt)+crpix2)
00116 #define WX(pix_x,pix_y) (((pix_x-crpix1)*cosa - (pix_y-crpix2)*sina)*cdelt+crvalx)
00117 #define WY(pix_x,pix_y) (((pix_y-crpix2)*cosa + (pix_x-crpix1)*sina)*cdelt+crvaly)
00118
00119 #define kDpath "dpath"
00120 #define kDpathDef "/home/jsoc/cvs/Development/JSOC"
00121
00122 #define kNotSpecified "Not Specified"
00123
00124
00125 #define a0 (0.0734)
00126 #define a2 (-0.412)
00127 #define a4 (-0.300)
00128
00129
00130
00131
00132
00133 struct mapInfo {
00134
00135 int cutOut, localOpt, globalOpt, fullDisk;
00136 int localCart, spheric;
00137 int latlon, noDisamb, fillNan;
00138 int autoTrack, autoSize, diffTrack, useHarp;
00139 int verbose;
00140 int ambweak;
00141 int doerr, covar, rice;
00142 int nbin, gauss;
00143 int mapOpt, repOpt, interpOpt;
00144 float ref_lon, ref_lat;
00145 float xscale, yscale;
00146 int ref_nrow, ref_ncol;
00147
00148 float xc, yc;
00149 int nrow, ncol;
00150 TIME tref;
00151 };
00152
00153
00154
00155 char *mapName[] = {"PlateCarree", "Cassini-Soldner", "Mercator",
00156 "LambertCylindrical", "Sanson-Flamsteed", "gnomonic", "Postel",
00157 "stereographic", "orthographic", "LambertAzimuthal"};
00158 char *wcsCode[] = {"CAR", "CAS", "MER", "CEA", "GLS", "TAN", "ARC", "STG",
00159 "SIN", "ZEA"};
00160 char *repName[] = {"Local Bx, By, Bz", "Field, inclination, azimuth",
00161 "Blos, Btrans, azimuth"};
00162 char *segName[] = { "bx", "by", "bz",
00163 "field", "inclination", "azimuth",
00164 "blon", "btrans", "azimuth"};
00165 char *interpName[] = {"Wiener", "Cubic Convolution", "Bilinear", "Nearest Neighbor"};
00166
00167
00168
00169
00170
00171 int findPosition(DRMS_Record_t *inRec, struct mapInfo *mInfo);
00172
00173
00174
00175 int readFullDisk(DRMS_Record_t *inRec, struct mapInfo *mInfo, float *bx_img, float *by_img, float *bz_img);
00176
00177
00178
00179 int performMapping(DRMS_Record_t *inRec, struct mapInfo *mInfo,
00180 float *bx_img, float *by_img, float *bz_img,
00181 float *bx_map, float *by_map, float *bz_map, const char *dpath);
00182
00183
00184
00185 int getError(DRMS_Record_t *inRec, struct mapInfo *mInfo,
00186 float *b1_err, float *b2_err, float *b3_err,
00187 float *cc_fi, float *cc_fa, float *cc_ai,
00188 int *qual_map, char *confid_map);
00189
00190
00191
00192 int localVectorTransform(DRMS_Record_t *inRec, struct mapInfo *mInfo,
00193 float *bx_map, float *by_map, float *bz_map);
00194
00195
00196
00197
00198 void repVectorTransform(float *bx_map, float *by_map, float *bz_map,
00199 float *b1, float *b2, float *b3,
00200 struct mapInfo *mInfo);
00201
00202
00203
00204 int writeMap(DRMS_Record_t *outRec, struct mapInfo *mInfo,
00205 float *b1, float *b2, float *b3);
00206
00207
00208
00209 int writeError(DRMS_Record_t *outRec, struct mapInfo *mInfo,
00210 float *b1_err, float *b2_err, float *b3_err,
00211 float *cc_fi, float *cc_fa, float *cc_ai,
00212 int *qual_map, char *confid_map);
00213
00214
00215
00216 void outputLatLon(DRMS_Record_t *outRec, DRMS_Record_t *inRec, struct mapInfo *mInfo);
00217
00218
00219
00220 void setKeys(DRMS_Record_t *outRec, DRMS_Record_t *inRec, struct mapInfo *mInfo);
00221
00222
00223
00224
00225 int getEphemeris(DRMS_Record_t *inRec, double *disk_lonc, double *disk_latc,
00226 double *disk_xc, double *disk_yc, double *rSun, double *asd, double *pa);
00227
00228
00229
00230 float nnb (float *f, int nx, int ny, double x, double y);
00231
00232
00233
00234 void frebin (float *image_in, float *image_out, int nx, int ny, int nbin, int gauss);
00235
00236
00237
00238
00239 char *module_name = "bmap";
00240 char *version_id = "2012 Feb 28";
00241
00242 ModuleArgs_t module_args[] =
00243 {
00244 {ARG_STRING, "in", kNotSpecified, "Input vector image series."},
00245 {ARG_STRING, "out", kNotSpecified, "Output map series."},
00246 {ARG_NUME, "map", "carree", "Projetion method, carree by default.",
00247 "carree, Cassini, Mercator, cyleqa, sineqa, gnomonic, Postel, stereographic, orthographic, Lambert"},
00248 {ARG_NUME, "rep", "xyz", "Vector representation, xyz by default.",
00249 "xyz, fia, lta"},
00250 {ARG_NUME, "interp", "wiener", "Interpolation method, higher order polynomial by default.",
00251 "wiener, cubic, bilinear, nearest"},
00252
00253 {ARG_FLOAT, "lon", "0.", "Reference longitude for output map center."},
00254 {ARG_FLOAT, "lat", "0.", "Reference latitude for output map center."},
00255 {ARG_FLOAT, "xscale", "0.03", "Scale in x direction at map center, in degrees."},
00256 {ARG_FLOAT, "yscale", "0.03", "Scale in x direction at map center, in degrees."},
00257 {ARG_INT, "cols", "0", "Columns in output map"},
00258 {ARG_INT, "rows", "0", "Rows in output map"},
00259 {ARG_INT, "nbin", "3", "Oversampling rate for anti-aliasing. See documentation."},
00260 {ARG_INT, "doerr", "1", "Perform error propagation, use near neighbor so far."},
00261 {ARG_INT, "covar", "0", "Out put covariances for field, only work for cutout now."},
00262 {ARG_INT, "rice", "1", "Use Rice compression."},
00263 {ARG_INT, "ambweak", "0", "Ambiguity resolution for weak field. 0: potential; 1: random; 2: radial acute"},
00264 {ARG_FLAG, "c", "", "Simple full pixel cutout without interpolation, override all mapping options."},
00265 {ARG_FLAG, "l", "", "Force vector representation in local tangent coordinates, for cutout mode only."},
00266 {ARG_FLAG, "g", "", "Force vector representation in plane of sky coordinates, for remap mode only."},
00267 {ARG_FLAG, "a", "", "Automatic tracking, override lon/lat."},
00268 {ARG_FLAG, "b", "", "Boxcar smoothing instead of Gaussian."},
00269 {ARG_FLAG, "f", "", "Full-disk mode, no mapping allowed."},
00270 {ARG_FLAG, "r", "", "Use original resolution, suppressing oversampling."},
00271 {ARG_FLAG, "e", "", "Output latitude, longitude."},
00272 {ARG_FLAG, "z", "", "Skip disambiguation solution."},
00273 {ARG_FLAG, "v", "", "Verbose mode."},
00274 {ARG_FLAG, "x", "", "Uses patch center direction for vector decomposition"},
00275 {ARG_FLAG, "q", "", "Fill regions outside HARP defined area as NaN"},
00276 {ARG_FLAG, "s", "", "Spherical option, flip By as Btheta"},
00277 {ARG_FLAG, "d", "", "Enable differential rotation tracking"},
00278 {ARG_FLAG, "h", "", "Force direct cutout same as specified HARP, overrides -a, -d, lon/lat, etc."},
00279 {ARG_STRING, kDpath, kDpathDef, "path to the source code tree (the JSOC root directory)"},
00280 {ARG_INT, "harpnum", "0", "HARP number"},
00281 {ARG_END}
00282 };
00283
00284 int DoIt(void)
00285 {
00286
00287 int status = DRMS_SUCCESS;
00288 int nrecs, irec;
00289
00290 struct mapInfo mInfo;
00291
00292 char *inQuery, *outQuery;
00293
00294 DRMS_RecordSet_t *inRS = NULL, *outRS = NULL;
00295 DRMS_Record_t *inRec = NULL, *outRec = NULL;
00296 const char *dpath = params_get_str(&cmdparams, kDpath);
00297
00298 int hn = params_get_int(&cmdparams, "harpnum");
00299
00300
00301
00302 inQuery = (char *) params_get_str(&cmdparams, "in");
00303 outQuery = (char *) params_get_str(&cmdparams, "out");
00304
00305 mInfo.mapOpt = params_get_int(&cmdparams, "map");
00306 mInfo.repOpt = params_get_int(&cmdparams, "rep");
00307 mInfo.interpOpt = params_get_int(&cmdparams, "interp");
00308
00309 mInfo.ref_lon = params_get_float(&cmdparams, "lon");
00310 mInfo.ref_lat = params_get_float(&cmdparams, "lat");
00311
00312 mInfo.xscale = params_get_float(&cmdparams, "xscale");
00313 mInfo.yscale = params_get_float(&cmdparams, "yscale");
00314 mInfo.nbin = params_get_int(&cmdparams, "nbin");
00315
00316 mInfo.doerr = params_get_int(&cmdparams, "doerr");
00317 mInfo.covar = params_get_int(&cmdparams, "covar");
00318 mInfo.rice = params_get_int(&cmdparams, "rice");
00319
00320 mInfo.ambweak = params_get_int(&cmdparams, "ambweak");
00321
00322 if ((mInfo.xscale / mInfo.nbin) > NYQVIST || (mInfo.yscale / mInfo.nbin) > NYQVIST)
00323 mInfo.nbin = MAX((round(mInfo.xscale / NYQVIST)),(round(mInfo.yscale / NYQVIST)));
00324 if (mInfo.xscale <= NYQVIST && mInfo.yscale <= NYQVIST)
00325 mInfo.nbin = 1;
00326 mInfo.nbin = mInfo.nbin / 2 * 2 + 1;
00327 if (params_isflagset(&cmdparams, "r")) mInfo.nbin = 1;
00328
00329
00330
00331 mInfo.ref_ncol = params_get_int(&cmdparams, "cols");
00332 mInfo.ref_nrow = params_get_int(&cmdparams, "rows");
00333 if (mInfo.ref_ncol <= 0 || mInfo.ref_nrow <= 0)
00334 mInfo.autoSize = 1;
00335 else
00336 mInfo.autoSize = 0;
00337
00338 mInfo.localCart = params_isflagset(&cmdparams, "x");
00339 mInfo.cutOut = params_isflagset(&cmdparams, "c");
00340 mInfo.localOpt = params_isflagset(&cmdparams, "l");
00341 mInfo.globalOpt = params_isflagset(&cmdparams, "g");
00342
00343 mInfo.autoTrack = params_isflagset(&cmdparams, "a");
00344 mInfo.useHarp = params_isflagset(&cmdparams, "h");
00345 mInfo.gauss = !(params_isflagset(&cmdparams, "b"));
00346
00347 mInfo.diffTrack = params_isflagset(&cmdparams, "d");
00348 if (mInfo.globalOpt || mInfo.autoTrack) mInfo.diffTrack = 0;
00349
00350 mInfo.fullDisk = params_isflagset(&cmdparams, "f");
00351 if (mInfo.fullDisk) {
00352 mInfo.cutOut = 1;
00353 mInfo.ref_ncol = mInfo.ref_nrow = mInfo.nrow = mInfo.ncol = FOURK;
00354 }
00355
00356 mInfo.latlon = params_isflagset(&cmdparams, "e");
00357 mInfo.noDisamb = params_isflagset(&cmdparams, "z");
00358 mInfo.verbose = params_isflagset(&cmdparams, "v");
00359 mInfo.fillNan = params_isflagset(&cmdparams, "q");
00360 mInfo.spheric = params_isflagset(&cmdparams, "s");
00361
00362
00363
00364 if (mInfo.verbose) {
00365 printf("==============\nMapping %s:\n", inQuery);
00366 if (mInfo.cutOut) {
00367 printf("Full pixel cutout\n");
00368 if (mInfo.localOpt) printf("Use local tangent plane as reference\n");
00369 } else {
00370 printf("Mapping: %s\n", mapName[mInfo.mapOpt]);
00371 printf("Interpolation: %s\n", interpName[mInfo.interpOpt]);
00372 printf("Scale: %f, %f deg\n", mInfo.xscale, mInfo.yscale);
00373 if (mInfo.globalOpt) printf("Use plane of sky as reference\n");
00374 }
00375 printf("Representation: %s\n", repName[mInfo.repOpt]);
00376 if (mInfo.autoTrack) {
00377 printf("Automatic centered\n");
00378 } else {
00379 printf("Use reference lon/lat: %f, %f\n", mInfo.ref_lon, mInfo.ref_lat);
00380 }
00381 if (mInfo.autoSize) {
00382 printf("Automatic sized\n");
00383 } else {
00384 printf("Map size: %d x %d\n", mInfo.ref_ncol, mInfo.ref_nrow);
00385 }
00386 if (mInfo.fullDisk) printf("Full disk mode\n");
00387 if (mInfo.latlon) printf("Output latitude/longitude\n");
00388 if (mInfo.noDisamb) printf("No disambiguation\n");
00389 }
00390 if (! mInfo.cutOut) mInfo.covar = 0;
00391
00392
00393
00394 inRS = drms_open_records(drms_env, inQuery, &status);
00395 if (status || inRS->n == 0) DIE("No input data found");
00396 nrecs = inRS->n;
00397
00398 if (mInfo.diffTrack) {
00399 mInfo.tref = (drms_getkey_time(inRS->records[0], "T_OBS", &status) +
00400 drms_getkey_time(inRS->records[nrecs-1], "T_OBS", &status))/2.;
00401 }
00402
00403
00404
00405 outRS = drms_create_records(drms_env, nrecs, outQuery, DRMS_PERMANENT, &status);
00406 if (status) DIE("Output recordset not created");
00407
00408
00409
00410 printf("==============\nStart mapping, %d image(s) in total.\n", nrecs);
00411
00412
00413 for (irec = 0; irec < nrecs; irec++) {
00414
00415 inRec = inRS->records[irec];
00416 TIME trec = drms_getkey_time(inRec, "T_REC", &status);
00417
00418 if (mInfo.verbose) {
00419 printf("==============\nImage #%d: ", irec);
00420 int harpnum = drms_getkey_int(inRec, "HARPNUM", &status);
00421 if (!mInfo.fullDisk && !status) printf("[%d]", harpnum);
00422 char trec_str[40];
00423 sprint_time(trec_str, trec, "TAI", 0);
00424 printf("[%s]\n", trec_str);
00425 }
00426
00427
00428
00429 if (mInfo.verbose) SHOW("Determine geometry...");
00430
00431 if (!mInfo.fullDisk &&
00432 findPosition(inRec, &mInfo)) {
00433 printf(" erroneous geometry, image skipped.\n");
00434 continue;
00435 }
00436
00437 if (mInfo.verbose) printf(" done.\n");
00438
00439
00440
00441 float *bx_img = NULL, *by_img = NULL, *bz_img = NULL;
00442
00443 bx_img = (float *) malloc(FOURK * FOURK * sizeof(float));
00444 by_img = (float *) malloc(FOURK * FOURK * sizeof(float));
00445 bz_img = (float *) malloc(FOURK * FOURK * sizeof(float));
00446
00447 if (mInfo.verbose) SHOW("Read input data...");
00448
00449 if (readFullDisk(inRec, &mInfo, bx_img, by_img, bz_img)) {
00450 if (mInfo.verbose) printf(" segment read error, image skipped.\n");
00451 if (bx_img) free(bx_img); if (by_img) free(by_img); if (bz_img) free(bz_img);
00452 continue;
00453 }
00454
00455 if (mInfo.verbose) printf(" done.\n");
00456
00457
00458
00459 float *bx_map = NULL, *by_map = NULL, *bz_map = NULL;
00460 int mapsize = mInfo.ncol * mInfo.nrow;
00461
00462 if (!mInfo.fullDisk) {
00463 bx_map = (float *) malloc(mapsize * sizeof(float));
00464 by_map = (float *) malloc(mapsize * sizeof(float));
00465 bz_map = (float *) malloc(mapsize * sizeof(float));
00466 } else {
00467 bx_map = bx_img;
00468 by_map = by_img;
00469 bz_map = bz_img;
00470 }
00471
00472 if (mInfo.verbose) SHOW("Mapping...");
00473
00474 if (!mInfo.fullDisk &&
00475 performMapping(inRec, &mInfo,
00476 bx_img, by_img, bz_img,
00477 bx_map, by_map, bz_map, dpath)) {
00478 printf("Image #%d skipped.", irec);
00479 if (mInfo.verbose) printf(": mapping error\n"); else printf("\n");
00480 if (bx_img) free(bx_img); if (by_img) free(by_img); if (bz_img) free(bz_img);
00481 if (bx_map) free(bx_map); if (by_map) free(by_map); if (bz_map) free(bz_map);
00482 continue;
00483 }
00484
00485 if (mInfo.verbose) printf(" done.\n");
00486
00487
00488
00489 float *b1_err = NULL, *b2_err = NULL, *b3_err = NULL;
00490 float *cc_fi = NULL, *cc_fa = NULL, *cc_ai = NULL;
00491 int *qual_map = NULL; char *confid_map = NULL;
00492
00493 if (mInfo.doerr) {
00494 if (mInfo.verbose) SHOW("Getting error estimation...");
00495
00496 if (!mInfo.fullDisk) {
00497 b1_err = (float *) malloc(mapsize * sizeof(float));
00498 b2_err = (float *) malloc(mapsize * sizeof(float));
00499 b3_err = (float *) malloc(mapsize * sizeof(float));
00500 qual_map = (int *) malloc(mapsize * sizeof(int));
00501 confid_map = (char *) malloc(mapsize * sizeof(char));
00502 if (mInfo.covar) {
00503 cc_fi = (float *) malloc(mapsize * sizeof(float));
00504 cc_fa = (float *) malloc(mapsize * sizeof(float));
00505 cc_ai = (float *) malloc(mapsize * sizeof(float));
00506 }
00507 } else {
00508 b1_err = (float *) malloc(FOURK * FOURK * sizeof(float));
00509 b2_err = (float *) malloc(FOURK * FOURK * sizeof(float));
00510 b3_err = (float *) malloc(FOURK * FOURK * sizeof(float));
00511 qual_map = (int *) malloc(FOURK * FOURK * sizeof(int));
00512 confid_map = (char *) malloc(FOURK * FOURK * sizeof(char));
00513 if (mInfo.covar) {
00514 cc_fi = (float *) malloc(FOURK * FOURK * sizeof(float));
00515 cc_fa = (float *) malloc(FOURK * FOURK * sizeof(float));
00516 cc_ai = (float *) malloc(FOURK * FOURK * sizeof(float));
00517 }
00518 }
00519
00520 if (getError(inRec, &mInfo, b1_err, b2_err, b3_err, cc_fi, cc_fa, cc_ai, qual_map, confid_map)) {
00521 printf("Image #%d skipped.", irec);
00522 if (mInfo.verbose) printf(": error estimation error\n"); else printf("\n");
00523 continue;
00524 }
00525
00526 if (mInfo.verbose) printf(" done.\n");
00527 }
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544 if (mInfo.verbose) SHOW("Vector transformation...");
00545
00546 if (localVectorTransform(inRec, &mInfo, bx_map, by_map, bz_map)) {
00547 printf("Image #%d skipped.", irec);
00548 if (mInfo.verbose) printf(": vector transformation error\n"); else printf("\n");
00549 if (bx_img) free(bx_img); if (by_img) free(by_img); if (bz_img) free(bz_img);
00550 if (bx_map) free(bx_map); if (by_map) free(by_map); if (bz_map) free(bz_map);
00551 continue;
00552 }
00553
00554
00555
00556 float *b1 = NULL, *b2 = NULL, *b3 = NULL;
00557
00558 b1 = (float *) malloc(mapsize * sizeof(float));
00559 b2 = (float *) malloc(mapsize * sizeof(float));
00560 b3 = (float *) malloc(mapsize * sizeof(float));
00561
00562 repVectorTransform(bx_map, by_map, bz_map, b1, b2, b3, &mInfo);
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573 if (mInfo.verbose) printf(" done.\n");
00574
00575
00576
00577 if (bx_img) free(bx_img); if (by_img) free(by_img); if (bz_img) free(bz_img);
00578 if (!mInfo.fullDisk) {
00579 if (bx_map) free(bx_map); if (by_map) free(by_map); if (bz_map) free(bz_map);
00580 }
00581
00582
00583
00584
00585 outRec = outRS->records[irec];
00586
00587 if (mInfo.verbose) SHOW("Output...");
00588
00589 if (writeMap(outRec, &mInfo, b1, b2, b3)) {
00590 printf("Image #%d skipped.", irec);
00591 if (mInfo.verbose) printf(": output error\n"); else printf("\n");
00592 if (b1) free(b1); if (b2) free(b2); if (b3) free(b3);
00593 continue;
00594 }
00595
00596 if (mInfo.verbose) printf(" done.\n");
00597
00598
00599
00600 if (mInfo.doerr) {
00601 if (mInfo.verbose) SHOW("Output error estimation...");
00602 if (writeError(outRec, &mInfo, b1_err, b2_err, b3_err, cc_fi, cc_fa, cc_ai, qual_map, confid_map)) {
00603 printf("Image #%d skipped.", irec);
00604 if (mInfo.verbose) printf(": error estimation output error\n"); else printf("\n");
00605 if (b1_err) free(b1_err); if (b2_err) free(b2_err); if (b3_err) free(b3_err);
00606 continue;
00607 }
00608 if (mInfo.verbose) printf(" done.\n");
00609 }
00610
00611
00612
00613 if (mInfo.latlon) {
00614 if (mInfo.verbose) SHOW("Output lat/lon...");
00615 outputLatLon(outRec, inRec, &mInfo);
00616 if (mInfo.verbose) printf(" done.\n");
00617 }
00618
00619
00620
00621 drms_copykey(outRec, inRec, "T_REC");
00622 if (!hn) drms_copykey(outRec, inRec, "HARPNUM"); else drms_setkey_int(outRec, "HARPNUM", hn);
00623 drms_copykey(outRec, inRec, "RUNNUM");
00624
00625 setKeys(outRec, inRec, &mInfo);
00626
00627
00628
00629 printf("Image #%d done.\n", irec);
00630
00631 }
00632
00633 drms_close_records(inRS, DRMS_FREE_RECORD);
00634
00635 drms_close_records(outRS, DRMS_INSERT_RECORD);
00636
00637 return 0;
00638
00639 }
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653 int findPosition(DRMS_Record_t *inRec, struct mapInfo *mInfo)
00654 {
00655 int status = 0;
00656
00657
00658
00659 double disk_latc, disk_lonc, disk_xc, disk_yc, rSun, asd, pa;
00660
00661 if (getEphemeris(inRec, &disk_lonc, &disk_latc, &disk_xc, &disk_yc, &rSun, &asd, &pa))
00662 return 1;
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 double lonc, latc;
00673
00674 float minlon, maxlon, minlat, maxlat;
00675
00676 if (mInfo->autoTrack || mInfo->autoSize) {
00677 minlon = drms_getkey_float(inRec, "LON_MIN", &status); if (status) return 1;
00678 maxlon = drms_getkey_float(inRec, "LON_MAX", &status); if (status) return 1;
00679 minlat = drms_getkey_float(inRec, "LAT_MIN", &status); if (status) return 1;
00680 maxlat = drms_getkey_float(inRec, "LAT_MAX", &status); if (status) return 1;
00681 }
00682
00683 if (mInfo->autoTrack) {
00684
00685
00686
00687 latc = (maxlat + minlat) / 2.;
00688 lonc = (maxlon + minlon) / 2. + disk_lonc / RADSINDEG;
00689
00690 } else {
00691
00692
00693
00694 latc = mInfo->ref_lat;
00695 lonc = mInfo->ref_lon;
00696
00697
00698
00699 if (mInfo->diffTrack) {
00700 TIME t_obs = drms_getkey_time(inRec, "T_OBS", &status);
00701 TIME delta_t = t_obs - mInfo->tref;
00702 double sinlat = sin(latc * RADSINDEG);
00703 double sinlat2 = sinlat * sinlat;
00704 double delta_rot = a0 + sinlat2 * (a2 + a4 * sinlat2);
00705 double delta_lon = delta_rot * 1.0e-6 * delta_t;
00706 lonc += delta_lon / RADSINDEG;
00707 printf("dlon=%f\n",delta_lon / RADSINDEG);
00708 }
00709
00710
00711
00712
00713 if ((lonc * RADSINDEG - disk_lonc) >= PI) lonc -= 360.;
00714 if ((lonc * RADSINDEG - disk_lonc) <= -PI) lonc += 360.;
00715
00716 }
00717
00718 double xi, zeta;
00719
00720 if (mInfo->cutOut) {
00721
00722 if (mInfo->useHarp) {
00723
00724 float x0 = drms_getkey_float(inRec, "CRPIX1", &status) - 1;
00725 float y0 = drms_getkey_float(inRec, "CRPIX2", &status) - 1;
00726 DRMS_Segment_t *inSeg = drms_segment_lookup(inRec, "disambig");
00727 mInfo->xc = x0 + (inSeg->axis[0] - 1.) / 2.;
00728 mInfo->yc = y0 + (inSeg->axis[1] - 1.) / 2.;
00729
00730 } else {
00731
00732 if (sphere2img (latc * RADSINDEG, lonc * RADSINDEG, disk_latc, disk_lonc, &xi, &zeta,
00733 disk_xc/rSun, disk_yc/rSun, 1.0, pa, 0., 0., 0., 0.))
00734 return 1;
00735 xi *= rSun;
00736 zeta *= rSun;
00737
00738 if (!mInfo->autoSize) {
00739 if (mInfo->ncol % 2 == 0) mInfo->xc = (int) xi + 0.5; else mInfo->xc = round(xi);
00740 if (mInfo->nrow % 2 == 0) mInfo->yc = (int) zeta + 0.5; else mInfo->yc = round(zeta);
00741 } else {
00742 mInfo->xc = round(xi);
00743 mInfo->yc = round(zeta);
00744 }
00745
00746 }
00747
00748 } else {
00749
00750 mInfo->xc = lonc;
00751 mInfo->yc = latc;
00752
00753 }
00754
00755
00756
00757 if (mInfo->autoSize) {
00758
00759 if (mInfo->cutOut) {
00760
00761 DRMS_Segment_t *inSeg = drms_segment_lookup(inRec, "disambig");
00762 if (mInfo->useHarp) {
00763 mInfo->ncol = inSeg->axis[0];
00764 mInfo->nrow = inSeg->axis[1];
00765 } else {
00766 mInfo->ncol = inSeg->axis[0] / 2 * 2 + 1;
00767 mInfo->nrow = inSeg->axis[1] / 2 * 2 + 1;
00768 }
00769
00770 } else {
00771
00772 mInfo->ncol = round((maxlon - minlon) / mInfo->xscale);
00773 mInfo->nrow = round((maxlat - minlat) / mInfo->yscale);
00774
00775 }
00776
00777 } else {
00778
00779 mInfo->ncol = mInfo->ref_ncol;
00780 mInfo->nrow = mInfo->ref_nrow;
00781
00782 }
00783
00784
00785
00786 return 0;
00787
00788 }
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800 int readFullDisk(DRMS_Record_t *inRec, struct mapInfo *mInfo, float *bx_img, float *by_img, float *bz_img)
00801 {
00802
00803 int status = 0;
00804
00805 DRMS_Segment_t *inSeg;
00806 DRMS_Array_t *inArray_ambig;
00807 DRMS_Array_t *inArray_bTotal, *inArray_bAzim, *inArray_bIncl, *inArray_bFill;
00808
00809 char *ambig;
00810 float *bTotal, *bAzim, *bIncl, *bFill;
00811
00812
00813
00814 inSeg = drms_segment_lookup(inRec, "disambig");
00815 inArray_ambig = drms_segment_read(inSeg, DRMS_TYPE_CHAR, &status);
00816 if (status) return 1;
00817 ambig = (char *)inArray_ambig->data;
00818
00819 inSeg = drms_segment_lookup(inRec, "field");
00820 inArray_bTotal = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00821 if (status) return 1;
00822 bTotal = (float *)inArray_bTotal->data;
00823
00824 inSeg = drms_segment_lookup(inRec, "azimuth");
00825 inArray_bAzim = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00826 if (status) return 1;
00827 bAzim = (float *)inArray_bAzim->data;
00828
00829 inSeg = drms_segment_lookup(inRec, "inclination");
00830 inArray_bIncl = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00831 if (status) return 1;
00832 bIncl = (float *)inArray_bIncl->data;
00833
00834 inSeg = drms_segment_lookup(inRec, "alpha_mag");
00835 inArray_bFill = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00836 if (status) return 1;
00837 bFill = (float *)inArray_bFill->data;
00838
00839
00840
00841 int llx, lly;
00842 int bmx, bmy;
00843
00844 int fullDisk = 0;
00845 if (inArray_ambig->axis[0] == FOURK && inArray_ambig->axis[1] == FOURK)
00846 fullDisk = 1;
00847
00848 llx = (int)(drms_getkey_float(inRec, "CRPIX1", &status)) - 1;
00849 lly = (int)(drms_getkey_float(inRec, "CRPIX2", &status)) - 1;
00850
00851 bmx = inArray_ambig->axis[0];
00852 bmy = inArray_ambig->axis[1];
00853
00854 int kx, ky, kOff;
00855 int jy = 0, yOff = 0, iData = 0;
00856 int ix = 0;
00857 int xDim = FOURK, yDim = FOURK;
00858
00859 int amb = (int)(pow(2,mInfo->ambweak));
00860
00861 for (jy = 0; jy < yDim; jy++)
00862 {
00863 ix = 0;
00864 yOff = jy * xDim;
00865 ky = jy - lly;
00866 for (ix = 0; ix < xDim; ix++)
00867 {
00868 iData = yOff + ix;
00869 kx = ix - llx;
00870
00871
00872 bx_img[iData] = - bTotal[iData] * bFill[iData] * sin(bIncl[iData] * RADSINDEG) * sin(bAzim[iData] * RADSINDEG);
00873 by_img[iData] = bTotal[iData] * bFill[iData] * sin(bIncl[iData] * RADSINDEG) * cos(bAzim[iData] * RADSINDEG);
00874 bz_img[iData] = bTotal[iData] * bFill[iData] * cos(bIncl[iData] * RADSINDEG);
00875
00876
00877
00878 if (mInfo->noDisamb) continue;
00879
00880 if (mInfo->fullDisk || fullDisk) {
00881 if ((ambig[iData] / amb) % 2) {
00882 bx_img[iData] *= -1.; by_img[iData] *= -1.;
00883 }
00884 continue;
00885 }
00886
00887 if (kx < 0 || kx >= bmx || ky < 0 || ky >= bmy) {
00888 continue;
00889 } else {
00890 kOff = ky * bmx + kx;
00891 if ((ambig[kOff] / amb) % 2) {
00892 bx_img[iData] *= -1.; by_img[iData] *= -1.;
00893 }
00894 }
00895
00896 }
00897 }
00898
00899
00900
00901 drms_free_array(inArray_ambig);
00902 drms_free_array(inArray_bTotal); drms_free_array(inArray_bAzim);
00903 drms_free_array(inArray_bIncl); drms_free_array(inArray_bFill);
00904
00905 return 0;
00906
00907 }
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922 int performMapping(DRMS_Record_t *inRec, struct mapInfo *mInfo,
00923 float *bx_img, float *by_img, float *bz_img,
00924 float *bx_map, float *by_map, float *bz_map, const char *dpath)
00925 {
00926
00927 int status = 0;
00928
00929 if (mInfo->cutOut) {
00930
00931 int nrow = mInfo->nrow, ncol = mInfo->ncol;
00932 float xc = mInfo->xc, yc = mInfo->yc;
00933
00934
00935
00936 int ind_map, ind_img;
00937 int x, y;
00938
00939 for (int row = 0; row < nrow; row++) {
00940 for (int col = 0; col < ncol; col++) {
00941
00942 ind_map = row * ncol + col;
00943 x = round(col + xc + 0.5 - ncol / 2.0);
00944 y = round(row + yc + 0.5 - nrow / 2.0);
00945
00946 if (x < 0 || x >= FOURK || y < 0 || y >= FOURK) {
00947 bx_map[ind_map] = DRMS_MISSING_FLOAT;
00948 by_map[ind_map] = DRMS_MISSING_FLOAT;
00949 bz_map[ind_map] = DRMS_MISSING_FLOAT;
00950 } else {
00951 ind_img = y * FOURK + x;
00952 bx_map[ind_map] = bx_img[ind_img];
00953 by_map[ind_map] = by_img[ind_img];
00954 bz_map[ind_map] = bz_img[ind_img];
00955
00956 }
00957
00958 }
00959 }
00960
00961
00962 if (mInfo->fillNan) {
00963 int col0 = drms_getkey_float(inRec, "CRPIX1", &status) - 1.;
00964 int row0 = drms_getkey_float(inRec, "CRPIX2", &status) - 1.;
00965 DRMS_Segment_t *inSeg = drms_segment_lookup(inRec, "disambig");
00966 int col1 = col0 + inSeg->axis[0];
00967 int row1 = row0 + inSeg->axis[1];
00968 for (int row = 0; row < nrow; row++) {
00969 for (int col = 0; col < ncol; col++) {
00970 ind_map = row * ncol + col;
00971 x = round(col + xc + 0.5 - ncol / 2.0);
00972 y = round(row + yc + 0.5 - nrow / 2.0);
00973 if (x < col0 || x >= col1 || y < row0 || y >= row1) {
00974 bx_map[ind_map] = DRMS_MISSING_FLOAT;
00975 by_map[ind_map] = DRMS_MISSING_FLOAT;
00976 bz_map[ind_map] = DRMS_MISSING_FLOAT;
00977 }
00978 }
00979 }
00980
00981 }
00982
00983 } else {
00984
00985
00986
00987 double disk_latc, disk_lonc, disk_xc, disk_yc, rSun, asd, pa;
00988
00989 if (getEphemeris(inRec, &disk_lonc, &disk_latc, &disk_xc, &disk_yc, &rSun, &asd, &pa))
00990 return 1;
00991
00992
00993
00994 int ncol0, nrow0;
00995 float xscale0, yscale0;
00996
00997 ncol0 = mInfo->ncol * mInfo->nbin + (mInfo->nbin / 2) * 2;
00998 nrow0 = mInfo->nrow * mInfo->nbin + (mInfo->nbin / 2) * 2;
00999 xscale0 = mInfo->xscale / mInfo->nbin;
01000 yscale0 = mInfo->yscale / mInfo->nbin;
01001
01002
01003
01004 float *bx_map0 = NULL, *by_map0 = NULL, *bz_map0 = NULL;
01005
01006 bx_map0 = (float *) malloc(ncol0 * nrow0 * sizeof(float));
01007 by_map0 = (float *) malloc(ncol0 * nrow0 * sizeof(float));
01008 bz_map0 = (float *) malloc(ncol0 * nrow0 * sizeof(float));
01009
01010 double lonc = mInfo->xc, latc = mInfo->yc;
01011
01012 float *xi_out, *zeta_out;
01013
01014 xi_out = (float *) malloc(ncol0 * nrow0 * sizeof(float));
01015 zeta_out = (float *) malloc(ncol0 * nrow0 * sizeof(float));
01016
01017 double x, y;
01018 double lat, lon;
01019 double xi, zeta;
01020
01021 int ind_map;
01022
01023
01024
01025
01026
01027
01028 for (int row0 = 0; row0 < nrow0; row0++) {
01029 for (int col0 = 0; col0 < ncol0; col0++) {
01030
01031 ind_map = row0 * ncol0 + col0;
01032
01033 x = (col0 + 0.5 - ncol0/2.) * xscale0;
01034 y = (row0 + 0.5 - nrow0/2.) * yscale0;
01035
01036
01037
01038
01039
01040
01041
01042 if (plane2sphere (x * RADSINDEG, y * RADSINDEG, latc * RADSINDEG, lonc * RADSINDEG,
01043 &lat, &lon, mInfo->mapOpt)) {
01044 xi_out[ind_map] = -1;
01045 zeta_out[ind_map] = -1;
01046 continue;
01047 }
01048
01049
01050
01051
01052
01053 if (sphere2img (lat, lon, disk_latc, disk_lonc, &xi, &zeta,
01054 disk_xc/rSun, disk_yc/rSun, 1.0, pa, 0., 0., 0., 0.)) {
01055 xi_out[ind_map] = -1;
01056 zeta_out[ind_map] = -1;
01057 continue;
01058 }
01059
01060 xi_out[ind_map] = xi * rSun;
01061 zeta_out[ind_map] = zeta * rSun;
01062
01063 }
01064 }
01065
01066
01067
01068 struct fint_struct pars;
01069
01070 switch (mInfo->interpOpt) {
01071 default:
01072 case 0:
01073 init_finterpolate_wiener(&pars, 6, 1, 6, 2, 1, 1, NULL, dpath);
01074 break;
01075 case 1:
01076 init_finterpolate_cubic_conv(&pars, 1., 3.);
01077 break;
01078 case 2:
01079 init_finterpolate_linear(&pars, 1.);
01080 break;
01081 case 3:
01082 break;
01083 }
01084
01085 if (mInfo->interpOpt == 3) {
01086 for (int row0 = 0; row0 < nrow0; row0++) {
01087 for (int col0 = 0; col0 < ncol0; col0++) {
01088 ind_map = row0 * ncol0 + col0;
01089 bx_map0[ind_map] = nnb(bx_img, FOURK, FOURK, xi_out[ind_map], zeta_out[ind_map]);
01090 by_map0[ind_map] = nnb(by_img, FOURK, FOURK, xi_out[ind_map], zeta_out[ind_map]);
01091 bz_map0[ind_map] = nnb(bz_img, FOURK, FOURK, xi_out[ind_map], zeta_out[ind_map]);
01092 }
01093 }
01094 } else {
01095 finterpolate(&pars, bx_img, xi_out, zeta_out, bx_map0,
01096 FOURK, FOURK, FOURK, ncol0, nrow0, ncol0, DRMS_MISSING_FLOAT);
01097 finterpolate(&pars, by_img, xi_out, zeta_out, by_map0,
01098 FOURK, FOURK, FOURK, ncol0, nrow0, ncol0, DRMS_MISSING_FLOAT);
01099 finterpolate(&pars, bz_img, xi_out, zeta_out, bz_map0,
01100 FOURK, FOURK, FOURK, ncol0, nrow0, ncol0, DRMS_MISSING_FLOAT);
01101 }
01102
01103
01104
01105 frebin(bx_map0, bx_map, ncol0, nrow0, mInfo->nbin, mInfo->gauss);
01106 frebin(by_map0, by_map, ncol0, nrow0, mInfo->nbin, mInfo->gauss);
01107 frebin(bz_map0, bz_map, ncol0, nrow0, mInfo->nbin, mInfo->gauss);
01108
01109
01110
01111 free(bx_map0); free(by_map0); free(bz_map0);
01112 free(xi_out); free(zeta_out);
01113
01114 }
01115
01116 return 0;
01117
01118 }
01119
01120
01121
01122
01123
01124 int getError(DRMS_Record_t *inRec, struct mapInfo *mInfo,
01125 float *b1_err, float *b2_err, float *b3_err,
01126 float *cc_fi, float *cc_fa, float *cc_ai,
01127 int *qual_map, char *confid_map)
01128 {
01129 int status = 0;
01130
01131 int prop = (mInfo->cutOut && mInfo->localOpt == 1) ||
01132 (!mInfo->cutOut && mInfo->globalOpt == 0);
01133
01134 DRMS_Segment_t *inSeg;
01135
01136 DRMS_Array_t *inArray_bTotal, *inArray_bAzim, *inArray_bIncl;
01137 DRMS_Array_t *inArray_qual, *inArray_confid;
01138 DRMS_Array_t *inArray_errbT, *inArray_errbAz, *inArray_errbIn;
01139 DRMS_Array_t *inArray_bTbI, *inArray_bTbA, *inArray_bAbI;
01140
01141 float *bTotal, *bAzim, *bIncl;
01142 int *qual; char *confid;
01143 float *errbT0, *errbAz0, *errbIn0;
01144 float *errbT, *errbAz, *errbIn;
01145 float *errbTbI0, *errbTbA0, *errbAbI0;
01146 float *errbTbI, *errbTbA, *errbAbI;
01147
01148
01149
01150 inSeg = drms_segment_lookup(inRec, "field");
01151 inArray_bTotal = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
01152 if (status) return 1;
01153 bTotal = (float *)inArray_bTotal->data;
01154
01155 inSeg = drms_segment_lookup(inRec, "azimuth");
01156 inArray_bAzim = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
01157 if (status) return 1;
01158 bAzim = (float *)inArray_bAzim->data;
01159
01160 inSeg = drms_segment_lookup(inRec, "inclination");
01161 inArray_bIncl = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
01162 if (status) return 1;
01163 bIncl = (float *)inArray_bIncl->data;
01164
01165
01166
01167 inSeg = drms_segment_lookup(inRec, "info_map");
01168 inArray_qual = drms_segment_read(inSeg, DRMS_TYPE_INT, &status);
01169 if (status) return 1;
01170 qual = (int *)inArray_qual->data;
01171
01172 inSeg = drms_segment_lookup(inRec, "confid_map");
01173 inArray_confid = drms_segment_read(inSeg, DRMS_TYPE_CHAR, &status);
01174 if (status) return 1;
01175 confid = (char *)inArray_confid->data;
01176
01177
01178
01179 inSeg = drms_segment_lookup(inRec, "field_err");
01180 inArray_errbT = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
01181 if (status) return 1;
01182 errbT0 = (float *)inArray_errbT->data;
01183
01184 inSeg = drms_segment_lookup(inRec, "azimuth_err");
01185 inArray_errbAz = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
01186 if (status) return 1;
01187 errbAz0 = (float *)inArray_errbAz->data;
01188
01189 inSeg = drms_segment_lookup(inRec, "inclination_err");
01190 inArray_errbIn = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
01191 if (status) return 1;
01192 errbIn0 = (float *)inArray_errbIn->data;
01193
01194 errbT = (float *) (malloc(FOURK2 * sizeof(float)));
01195 errbAz = (float *) (malloc(FOURK2 * sizeof(float)));
01196 errbIn = (float *) (malloc(FOURK2 * sizeof(float)));
01197
01198 for (int i = 0; i < FOURK2; i++) {
01199 errbT[i] = errbT0[i] * errbT0[i];
01200 if (fabs(errbAz0[i]) > 180.) errbAz0[i] = 180.;
01201 if (fabs(errbIn0[i]) > 180.) errbIn0[i] = 180.;
01202 errbAz[i] = errbAz0[i] * errbAz0[i] * RADSINDEG * RADSINDEG;
01203 errbIn[i] = errbIn0[i] * errbIn0[i] * RADSINDEG * RADSINDEG;
01204 }
01205
01206
01207
01208 if (prop || mInfo->covar) {
01209
01210 inSeg = drms_segment_lookup(inRec, "field_inclination_err");
01211 inArray_bTbI = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
01212 if (status) return 1;
01213 errbTbI0 = (float *)inArray_bTbI->data;
01214
01215 inSeg = drms_segment_lookup(inRec, "field_az_err");
01216 inArray_bTbA = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
01217 if (status) return 1;
01218 errbTbA0 = (float *)inArray_bTbA->data;
01219
01220 inSeg = drms_segment_lookup(inRec, "inclin_azimuth_err");
01221 inArray_bAbI = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
01222 if (status) return 1;
01223 errbAbI0 = (float *)inArray_bAbI->data;
01224
01225 if (prop) {
01226 errbTbI = (float *) (malloc(FOURK2 * sizeof(float)));
01227 errbTbA = (float *) (malloc(FOURK2 * sizeof(float)));
01228 errbAbI = (float *) (malloc(FOURK2 * sizeof(float)));
01229
01230 for (int i = 0; i < FOURK2; i++) {
01231 errbTbI[i] = errbTbI0[i] * errbT0[i] * errbIn0[i] * RADSINDEG;
01232 errbTbA[i] = errbTbA0[i] * errbT0[i] * errbAz0[i] * RADSINDEG;
01233 errbAbI[i] = errbAbI0[i] * errbAz0[i] * errbIn0[i] * RADSINDEG * RADSINDEG;
01234 }
01235 }
01236 }
01237
01238
01239
01240 double disk_latc, disk_lonc, disk_xc, disk_yc, rSun, asd, pa;
01241
01242 if (getEphemeris(inRec, &disk_lonc, &disk_latc, &disk_xc, &disk_yc, &rSun, &asd, &pa))
01243 return 1;
01244
01245
01246
01247 double x, y;
01248 double lat, lon;
01249 double xi, zeta;
01250
01251 int nrow = mInfo->nrow, ncol = mInfo->ncol;
01252 float xc = mInfo->xc, yc = mInfo->yc;
01253 float xscale = mInfo->xscale, yscale = mInfo->yscale;
01254 double lonc = mInfo->xc, latc = mInfo->yc;
01255
01256 int ind_map, ind_img;
01257
01258 int dx0, dy0, ind_img0;
01259 inSeg = drms_segment_lookup(inRec, "disambig");
01260 int col0 = drms_getkey_float(inRec, "CRPIX1", &status) - 1.;
01261 int row0 = drms_getkey_float(inRec, "CRPIX2", &status) - 1.;
01262 int nx0 = inSeg->axis[0];
01263 int ny0 = inSeg->axis[1];
01264 int col1 = col0 + nx0;
01265 int row1 = row0 + ny0;
01266
01267 double btSigma2, bpSigma2, brSigma2;
01268
01269 if (mInfo->verbose) {
01270 if (prop) SHOW("\nError propagation...") else SHOW("\nCopy variance...");
01271 }
01272
01273 for (int row = 0; row < nrow; row++) {
01274 for (int col = 0; col < ncol; col++) {
01275
01276 ind_map = row * ncol + col;
01277
01278
01279
01280 if (mInfo->cutOut) {
01281
01282 xi = round(col + xc + 0.5 - ncol / 2.0);
01283 zeta = round(row + yc + 0.5 - nrow / 2.0);
01284
01285 } else {
01286
01287 x = (col + 0.5 - ncol/2.) * xscale;
01288 y = (row + 0.5 - nrow/2.) * yscale;
01289
01290 if (plane2sphere (x * RADSINDEG, y * RADSINDEG, latc * RADSINDEG, lonc * RADSINDEG,
01291 &lat, &lon, mInfo->mapOpt)) {
01292 qual_map[ind_map] = DRMS_MISSING_CHAR;
01293 confid_map[ind_map] = DRMS_MISSING_CHAR;
01294 b1_err[ind_map] = DRMS_MISSING_FLOAT;
01295 b2_err[ind_map] = DRMS_MISSING_FLOAT;
01296 b3_err[ind_map] = DRMS_MISSING_FLOAT;
01297 continue;
01298 }
01299
01300 if (sphere2img (lat, lon, disk_latc, disk_lonc, &xi, &zeta,
01301 disk_xc/rSun, disk_yc/rSun, 1.0, pa, 0., 0., 0., 0.)) {
01302 qual_map[ind_map] = DRMS_MISSING_CHAR;
01303 confid_map[ind_map] = DRMS_MISSING_CHAR;
01304 b1_err[ind_map] = DRMS_MISSING_FLOAT;
01305 b2_err[ind_map] = DRMS_MISSING_FLOAT;
01306 b3_err[ind_map] = DRMS_MISSING_FLOAT;
01307 continue;
01308 }
01309
01310 xi *= rSun; xi = round(xi);
01311 zeta *= rSun; zeta = round(zeta);
01312
01313 }
01314
01315
01316
01317 dx0 = xi - col0; dy0 = zeta - row0;
01318 if (dx0 < 0 || dy0 < 0 || dx0 >= nx0 || dy0 >= ny0) {
01319 qual_map[ind_map] = DRMS_MISSING_INT;
01320 confid_map[ind_map] = DRMS_MISSING_CHAR;
01321 } else {
01322 ind_img0 = dy0 * nx0 + dx0;
01323 qual_map[ind_map] = qual[ind_img0];
01324 confid_map[ind_map] = confid[ind_img0];
01325 }
01326
01327
01328
01329
01330 ind_img = round(zeta * FOURK + xi);
01331
01332
01333
01334 if (prop) {
01335
01336 if (errorprop (bTotal, bAzim, bIncl,
01337 errbT, errbAz, errbIn, errbTbA, errbTbI, errbAbI,
01338 lon, lat, disk_lonc, disk_latc, pa, FOURK, FOURK, xi, zeta,
01339 &btSigma2, &bpSigma2, &brSigma2)) {
01340 b1_err[ind_map] = DRMS_MISSING_FLOAT;
01341 b2_err[ind_map] = DRMS_MISSING_FLOAT;
01342 b3_err[ind_map] = DRMS_MISSING_FLOAT;
01343
01344 continue;
01345 }
01346
01347 b1_err[ind_map] = sqrt(bpSigma2);
01348 b2_err[ind_map] = sqrt(btSigma2);
01349 b3_err[ind_map] = sqrt(brSigma2);
01350
01351 } else {
01352
01353 b1_err[ind_map] = errbT0[ind_img];
01354 b2_err[ind_map] = errbIn0[ind_img];
01355 b3_err[ind_map] = errbAz0[ind_img];
01356 if (mInfo->covar) {
01357 cc_fi[ind_map] = errbTbI0[ind_img];
01358 cc_fa[ind_map] = errbTbA0[ind_img];
01359 cc_ai[ind_map] = errbAbI0[ind_img];
01360 }
01361
01362 }
01363
01364 }
01365 }
01366
01367
01368 if (mInfo->cutOut && !mInfo->localOpt && mInfo->fillNan) {
01369 for (int row = 0; row < nrow; row++) {
01370 for (int col = 0; col < ncol; col++) {
01371 ind_map = row * ncol + col;
01372 int x1 = round(col + xc + 0.5 - ncol / 2.0);
01373 int y1 = round(row + yc + 0.5 - nrow / 2.0);
01374 if (x1 < col0 || x1 >= col1 || y1 < row0 || y1 >= row1) {
01375 b1_err[ind_map] = DRMS_MISSING_FLOAT;
01376 b2_err[ind_map] = DRMS_MISSING_FLOAT;
01377 b3_err[ind_map] = DRMS_MISSING_FLOAT;
01378 if (mInfo->covar) {
01379 cc_fi[ind_map] = DRMS_MISSING_FLOAT;
01380 cc_fa[ind_map] = DRMS_MISSING_FLOAT;
01381 cc_ai[ind_map] = DRMS_MISSING_FLOAT;
01382 }
01383 }
01384 }
01385 }
01386 }
01387
01388
01389
01390 free(errbT); free(errbAz); free(errbIn);
01391 drms_free_array(inArray_bTotal); drms_free_array(inArray_bAzim);
01392 drms_free_array(inArray_bIncl);
01393 drms_free_array(inArray_qual); drms_free_array(inArray_confid);
01394 drms_free_array(inArray_errbT); drms_free_array(inArray_errbAz);
01395 drms_free_array(inArray_errbIn);
01396 if (prop || mInfo->covar) {
01397 if (prop) {
01398 free(errbTbI); free(errbTbA); free(errbAbI);
01399 }
01400 drms_free_array(inArray_bTbI); drms_free_array(inArray_bTbA);
01401 drms_free_array(inArray_bAbI);
01402 }
01403
01404 return 0;
01405
01406 }
01407
01408
01409
01410
01411
01412
01413
01414
01415 int localVectorTransform(DRMS_Record_t *inRec, struct mapInfo *mInfo,
01416 float *bx_map, float *by_map, float *bz_map)
01417 {
01418
01419 if (mInfo->cutOut && (! mInfo->localOpt))
01420 return 0;
01421
01422 if ((! mInfo->cutOut) && mInfo->globalOpt)
01423 return 0;
01424
01425
01426
01427 double disk_latc, disk_lonc, disk_xc, disk_yc, rSun, asd, pa;
01428
01429 if (getEphemeris(inRec, &disk_lonc, &disk_latc, &disk_xc, &disk_yc, &rSun, &asd, &pa))
01430 return 1;
01431
01432
01433
01434
01435 int mapsize = mInfo->ncol * mInfo->nrow;
01436 int ind_map;
01437
01438 double x, y;
01439 double lat, lon;
01440 double latc = mInfo->yc, lonc = mInfo->xc;
01441
01442 double bx_tmp, by_tmp, bz_tmp;
01443 double rho, sinlat, coslat, sig, mu, chi;
01444
01445
01446
01447
01448 if (mInfo->repOpt == 0 && mInfo->localCart == 1) {
01449
01450 if (mInfo->cutOut) {
01451 x = (int) mInfo->xc;
01452 y = (int) mInfo->yc;
01453 x = (x - disk_xc) / rSun;
01454 y = (y - disk_yc) / rSun;
01455 img2sphere(x, y, asd, disk_latc, disk_lonc, pa,
01456 &rho, &lat, &lon, &sinlat, &coslat, &sig, &mu, &chi);
01457 } else {
01458 lat = latc * RADSINDEG;
01459 lon = lonc * RADSINDEG;
01460 }
01461 }
01462
01463
01464
01465
01466 for (int row = 0; row < mInfo->nrow; row++) {
01467 for (int col = 0; col < mInfo->ncol; col++) {
01468
01469 ind_map = row * mInfo->ncol + col;
01470
01471
01472
01473 if (mInfo->repOpt != 0 || mInfo->localCart == 0) {
01474
01475 if (mInfo->cutOut) {
01476
01477 if (!mInfo->fullDisk) {
01478 x = (int)(col + mInfo->xc + 0.5 - mInfo->ncol / 2.0);
01479 y = (int)(row + mInfo->yc + 0.5 - mInfo->nrow / 2.0);
01480 x = (x - disk_xc) / rSun;
01481 y = (y - disk_yc) / rSun;
01482 } else {
01483 x = (col - disk_xc) / rSun;
01484 y = (row - disk_yc) / rSun;
01485 }
01486
01487
01488 if (img2sphere(x, y, asd, disk_latc, disk_lonc, pa,
01489 &rho, &lat, &lon, &sinlat, &coslat, &sig, &mu, &chi)) {
01490 bx_map[ind_map] = DRMS_MISSING_FLOAT;
01491 by_map[ind_map] = DRMS_MISSING_FLOAT;
01492 bz_map[ind_map] = DRMS_MISSING_FLOAT;
01493 continue;
01494 }
01495
01496 } else {
01497
01498 x = (col + 0.5 - mInfo->ncol / 2.) * mInfo->xscale;
01499 y = (row + 0.5 - mInfo->nrow / 2.) * mInfo->yscale;
01500
01501 if (plane2sphere (x * RADSINDEG, y * RADSINDEG, latc * RADSINDEG, lonc * RADSINDEG,
01502 &lat, &lon, mInfo->mapOpt)) {
01503 bx_map[ind_map] = DRMS_MISSING_FLOAT;
01504 by_map[ind_map] = DRMS_MISSING_FLOAT;
01505 bz_map[ind_map] = DRMS_MISSING_FLOAT;
01506 continue;
01507 }
01508 }
01509
01510 }
01511
01512
01513
01514
01515
01516 bx_tmp = by_tmp = bz_tmp = 0;
01517
01518 img2helioVector (bx_map[ind_map], by_map[ind_map], bz_map[ind_map],
01519 &bx_tmp, &by_tmp, &bz_tmp,
01520 lon, lat, disk_lonc, disk_latc, pa);
01521
01522 bx_map[ind_map] = bx_tmp;
01523 by_map[ind_map] = by_tmp;
01524 bz_map[ind_map] = bz_tmp;
01525
01526 }
01527 }
01528
01529 return 0;
01530
01531 }
01532
01533
01534
01535
01536
01537 void repVectorTransform(float *bx_map, float *by_map, float *bz_map,
01538 float *b1, float *b2, float *b3,
01539 struct mapInfo *mInfo)
01540 {
01541
01542 int mapsize = mInfo->ncol * mInfo->nrow;
01543 float k = 1;
01544
01545 switch (mInfo->repOpt) {
01546 default:
01547 case 0:
01548 if ((!mInfo->cutOut && !mInfo->localCart && mInfo->spheric) ||
01549 (mInfo->cutOut && mInfo->localOpt && !mInfo->localCart && mInfo->spheric))
01550 k = -1;
01551 for (int i = 0; i < mapsize; i++) {
01552 b1[i] = bx_map[i];
01553 b2[i] = by_map[i] * k;
01554 b3[i] = bz_map[i];
01555 }
01556 break;
01557 case 1:
01558 for (int i = 0; i < mapsize; i++) {
01559 b1[i] = sqrt(bx_map[i] * bx_map[i] + by_map[i] * by_map[i] + bz_map[i] * bz_map[i]);
01560 b2[i] = acos(bz_map[i] / b1[i]) / RADSINDEG;
01561 b3[i] = atan2(-bx_map[i], by_map[i]) / RADSINDEG;
01562 if (b3[i] < 0.) b3[i] += 360.;
01563 }
01564 break;
01565 case 2:
01566 for (int i = 0; i < mapsize; i++) {
01567 b1[i] = bz_map[i];
01568 b2[i] = sqrt(bx_map[i] * bx_map[i] + by_map[i] * by_map[i]);
01569 b3[i] = atan2(-bx_map[i], by_map[i]) / RADSINDEG;
01570 if (b3[i] < 0.) b3[i] += 360.;
01571 }
01572 break;
01573 }
01574
01575 }
01576
01577
01578
01579
01580
01581
01582 int writeMap(DRMS_Record_t *outRec, struct mapInfo *mInfo,
01583 float *b1, float *b2, float *b3)
01584 {
01585
01586 int status = 0;
01587
01588 int outDims[2];
01589 outDims[0] = mInfo->ncol; outDims[1] = mInfo->nrow;
01590
01591 DRMS_Segment_t *outSeg;
01592 DRMS_Array_t *outArray;
01593
01594
01595 outSeg = drms_segment_lookupnum(outRec, 0);
01596 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, b1, &status);
01597 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01598
01599 if (mInfo->rice) {
01600 outArray->israw = 0;
01601 outArray->bzero = 0;
01602 outArray->bscale = 0.01;
01603 }
01604 status = drms_segment_write(outSeg, outArray, 0);
01605 if (status) return 1;
01606 drms_free_array(outArray);
01607
01608
01609 outSeg = drms_segment_lookupnum(outRec, 1);
01610 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, b2, &status);
01611 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01612
01613 if (mInfo->rice) {
01614 outArray->israw = 0;
01615 outArray->bzero = 0;
01616 outArray->bscale = 0.01;
01617 }
01618 status = drms_segment_write(outSeg, outArray, 0);
01619 if (status) return 1;
01620 drms_free_array(outArray);
01621
01622
01623 outSeg = drms_segment_lookupnum(outRec, 2);
01624 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, b3, &status);
01625 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01626
01627 if (mInfo->rice) {
01628 outArray->israw = 0;
01629 outArray->bzero = 0;
01630 outArray->bscale = 0.01;
01631 }
01632 status = drms_segment_write(outSeg, outArray, 0);
01633 if (status) return 1;
01634 drms_free_array(outArray);
01635
01636 return 0;
01637
01638 }
01639
01640
01641
01642
01643
01644 int writeError(DRMS_Record_t *outRec, struct mapInfo *mInfo,
01645 float *b1_err, float *b2_err, float *b3_err,
01646 float *cc_fi, float *cc_fa, float *cc_ai,
01647 int *qual_map, char *confid_map)
01648 {
01649
01650 int status = 0;
01651
01652 int outDims[2];
01653 outDims[0] = mInfo->ncol; outDims[1] = mInfo->nrow;
01654
01655 DRMS_Segment_t *outSeg;
01656 DRMS_Array_t *outArray;
01657
01658
01659 outSeg = drms_segment_lookupnum(outRec, 3);
01660 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, b1_err, &status);
01661 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01662
01663 if (mInfo->rice) {
01664 outArray->israw = 0;
01665 outArray->bzero = 0;
01666 outArray->bscale = 0.001;
01667 }
01668 status = drms_segment_write(outSeg, outArray, 0);
01669 if (status) return 1;
01670 drms_free_array(outArray);
01671
01672
01673 outSeg = drms_segment_lookupnum(outRec, 4);
01674 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, b2_err, &status);
01675 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01676
01677 if (mInfo->rice) {
01678 outArray->israw = 0;
01679 outArray->bzero = 0;
01680 outArray->bscale = 0.001;
01681 }
01682 status = drms_segment_write(outSeg, outArray, 0);
01683 if (status) return 1;
01684 drms_free_array(outArray);
01685
01686
01687 outSeg = drms_segment_lookupnum(outRec, 5);
01688 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, b3_err, &status);
01689 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01690
01691 if (mInfo->rice) {
01692 outArray->israw = 0;
01693 outArray->bzero = 0;
01694 outArray->bscale = 0.001;
01695 }
01696 status = drms_segment_write(outSeg, outArray, 0);
01697 if (status) return 1;
01698 drms_free_array(outArray);
01699
01700 if (! mInfo->covar || ! cc_fi || ! cc_fa || ! cc_ai) return 0;
01701
01702
01703 outSeg = drms_segment_lookup(outRec, "info_map");
01704 outArray = drms_array_create(DRMS_TYPE_INT, 2, outDims, qual_map, &status);
01705 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01706 outArray->parent_segment = outSeg;
01707 status = drms_segment_write(outSeg, outArray, 0);
01708 if (status) return 1;
01709 drms_free_array(outArray);
01710
01711
01712 outSeg = drms_segment_lookup(outRec, "confid_map");
01713 outArray = drms_array_create(DRMS_TYPE_CHAR, 2, outDims, confid_map, &status);
01714 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01715 outArray->parent_segment = outSeg;
01716 status = drms_segment_write(outSeg, outArray, 0);
01717 if (status) return 1;
01718 drms_free_array(outArray);
01719
01720
01721
01722 outSeg = drms_segment_lookup(outRec, "field_inclination_err");
01723 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, cc_fi, &status);
01724 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01725 outArray->parent_segment = outSeg;
01726 if (mInfo->rice) {
01727 outArray->israw = 0;
01728 outArray->bzero = 0;
01729 outArray->bscale = 0.0001;
01730 }
01731 status = drms_segment_write(outSeg, outArray, 0);
01732 if (status) return 1;
01733 drms_free_array(outArray);
01734
01735
01736 outSeg = drms_segment_lookup(outRec, "field_az_err");
01737 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, cc_fa, &status);
01738 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01739 outArray->parent_segment = outSeg;
01740 if (mInfo->rice) {
01741 outArray->israw = 0;
01742 outArray->bzero = 0;
01743 outArray->bscale = 0.0001;
01744 }
01745 status = drms_segment_write(outSeg, outArray, 0);
01746 if (status) return 1;
01747 drms_free_array(outArray);
01748
01749
01750 outSeg = drms_segment_lookup(outRec, "inclin_azimuth_err");
01751 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, cc_ai, &status);
01752 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01753 outArray->parent_segment = outSeg;
01754 if (mInfo->rice) {
01755 outArray->israw = 0;
01756 outArray->bzero = 0;
01757 outArray->bscale = 0.0001;
01758 }
01759 status = drms_segment_write(outSeg, outArray, 0);
01760 if (status) return 1;
01761 drms_free_array(outArray);
01762
01763
01764 return 0;
01765
01766 }
01767
01768
01769
01770
01771
01772
01773 void outputLatLon(DRMS_Record_t *outRec, DRMS_Record_t *inRec, struct mapInfo *mInfo)
01774 {
01775
01776 if (!mInfo->latlon) return;
01777
01778
01779
01780 double disk_latc, disk_lonc, disk_xc, disk_yc, rSun, asd, pa;
01781
01782 if (getEphemeris(inRec, &disk_lonc, &disk_latc, &disk_xc, &disk_yc, &rSun, &asd, &pa))
01783 return;
01784
01785
01786
01787 float *carrLat = NULL, *carrLon = NULL;
01788 int mapsize = mInfo->nrow * mInfo->ncol;
01789
01790 carrLat = (float *) malloc(mapsize * sizeof(float));
01791 carrLon = (float *) malloc(mapsize * sizeof(float));
01792
01793
01794
01795
01796
01797
01798 int ind_map;
01799
01800 double x, y;
01801 double lat, lon;
01802 double latc = mInfo->yc, lonc = mInfo->xc;
01803
01804 double bx_tmp, by_tmp, bz_tmp;
01805 double rho, sinlat, coslat, sig, mu, chi;
01806
01807
01808
01809
01810
01811
01812
01813 for (int row = 0; row < mInfo->nrow; row++) {
01814 for (int col = 0; col < mInfo->ncol; col++) {
01815
01816 ind_map = row * mInfo->ncol + col;
01817
01818
01819
01820 if (mInfo->cutOut) {
01821
01822 if (!mInfo->fullDisk) {
01823 x = (int)(col + mInfo->xc + 0.5 - mInfo->ncol / 2.0);
01824 y = (int)(row + mInfo->yc + 0.5 - mInfo->nrow / 2.0);
01825 } else {
01826 x = col;
01827 y = row;
01828 }
01829
01830 x = (x - disk_xc) / rSun;
01831 y = (y - disk_yc) / rSun;
01832
01833 if (img2sphere(x, y, asd, disk_latc, disk_lonc, pa,
01834 &rho, &lat, &lon, &sinlat, &coslat, &sig, &mu, &chi)) {
01835 carrLat[ind_map] = DRMS_MISSING_FLOAT;
01836 carrLon[ind_map] = DRMS_MISSING_FLOAT;
01837 continue;
01838 }
01839
01840 } else {
01841
01842 x = (col + 0.5 - mInfo->ncol / 2.) * mInfo->xscale;
01843 y = (row + 0.5 - mInfo->nrow / 2.) * mInfo->yscale;
01844
01845 if (plane2sphere (x * RADSINDEG, y * RADSINDEG, latc * RADSINDEG, lonc * RADSINDEG,
01846 &lat, &lon, mInfo->mapOpt)) {
01847 carrLat[ind_map] = DRMS_MISSING_FLOAT;
01848 carrLon[ind_map] = DRMS_MISSING_FLOAT;
01849 continue;
01850 }
01851
01852 }
01853
01854 carrLat[ind_map] = lat / RADSINDEG;
01855 carrLon[ind_map] = lon / RADSINDEG;
01856
01857 }
01858 }
01859
01860
01861
01862 int status = 0;
01863
01864 int outDims[2];
01865 outDims[0] = mInfo->ncol; outDims[1] = mInfo->nrow;
01866
01867 DRMS_Segment_t *outSeg = NULL;
01868 DRMS_Array_t *outArray;
01869
01870
01871 outSeg = drms_segment_lookup(outRec, "lat");
01872 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, carrLat, &status);
01873 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01874 outArray->parent_segment = outSeg;
01875 status = drms_segment_write(outSeg, outArray, 0);
01876 drms_free_array(outArray);
01877
01878
01879 outSeg = drms_segment_lookup(outRec, "lon");
01880 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, carrLon, &status);
01881 outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
01882 outArray->parent_segment = outSeg;
01883 status = drms_segment_write(outSeg, outArray, 0);
01884 drms_free_array(outArray);
01885
01886 }
01887
01888
01889
01890
01891
01892
01893 void setKeys(DRMS_Record_t *outRec, DRMS_Record_t *inRec, struct mapInfo *mInfo)
01894 {
01895 int status = 0;
01896 char key[64];
01897
01898
01899 drms_setkey_string(outRec, "BLD_VERS", jsoc_version);
01900 drms_setkey_time(outRec, "DATE", CURRENT_SYSTEM_TIME);
01901
01902
01903 copy_me_keys(inRec, outRec);
01904 copy_patch_keys(inRec, outRec);
01905 copy_ambig_keys(inRec, outRec);
01906
01907
01908
01909 if (mInfo->fullDisk) {
01910 copy_geo_keys(inRec, outRec);
01911 return;
01912 }
01913
01914 double disk_latc, disk_lonc, disk_xc, disk_yc, rSun, asd, pa;
01915
01916 if (getEphemeris(inRec, &disk_lonc, &disk_latc, &disk_xc, &disk_yc, &rSun, &asd, &pa))
01917 return;
01918
01919 drms_copykey(outRec, inRec, "CAR_ROT");
01920 drms_copykey(outRec, inRec, "CDELT1");
01921 drms_copykey(outRec, inRec, "CDELT2");
01922 drms_copykey(outRec, inRec, "IMCRPIX1");
01923 drms_copykey(outRec, inRec, "IMCRPIX2");
01924 drms_copykey(outRec, inRec, "IMCRVAL1");
01925 drms_copykey(outRec, inRec, "IMCRVAL2");
01926
01927 switch (mInfo->repOpt) {
01928 default:
01929 case 0:
01930 drms_setkey_string(outRec, "BUNIT_000", "Gauss");
01931 drms_setkey_string(outRec, "BUNIT_001", "Gauss");
01932 drms_setkey_string(outRec, "BUNIT_002", "Gauss");
01933 break;
01934 case 1:
01935 drms_setkey_string(outRec, "BUNIT_000", "Gauss");
01936 drms_setkey_string(outRec, "BUNIT_001", "degree");
01937 drms_setkey_string(outRec, "BUNIT_002", "degree");
01938 break;
01939 case 2:
01940 drms_setkey_string(outRec, "BUNIT_000", "Gauss");
01941 drms_setkey_string(outRec, "BUNIT_001", "Gauss");
01942 drms_setkey_string(outRec, "BUNIT_002", "degree");
01943 break;
01944 }
01945
01946 if (mInfo->cutOut) {
01947
01948 drms_setkey_float(outRec, "CRPIX1", disk_xc - mInfo->xc + (mInfo->ncol - 1.) / 2. + 1.);
01949 drms_setkey_float(outRec, "CRPIX2", disk_yc - mInfo->yc + (mInfo->nrow - 1.) / 2. + 1.);
01950
01951 drms_setkey_float(outRec, "CRVAL1", 0);
01952 drms_setkey_float(outRec, "CRVAL2", 0);
01953
01954 drms_copykey(outRec, inRec, "CDELT1");
01955 drms_copykey(outRec, inRec, "CDELT2");
01956 drms_copykey(outRec, inRec, "CUNIT1");
01957 drms_copykey(outRec, inRec, "CUNIT2");
01958 drms_copykey(outRec, inRec, "CTYPE1");
01959 drms_copykey(outRec, inRec, "CTYPE2");
01960 drms_copykey(outRec, inRec, "CROTA2");
01961
01962 drms_setkey_string(outRec, "PROJECTION", "Cutout");
01963
01964
01965 drms_setkey_string(outRec, "BUNIT_003", "Gauss");
01966 drms_setkey_string(outRec, "BUNIT_004", "degree");
01967 drms_setkey_string(outRec, "BUNIT_005", "degree");
01968 drms_setkey_string(outRec, "BUNIT_006", " ");
01969 drms_setkey_string(outRec, "BUNIT_007", " ");
01970 drms_setkey_string(outRec, "BUNIT_008", " ");
01971 drms_setkey_string(outRec, "BUNIT_009", " ");
01972 drms_setkey_string(outRec, "BUNIT_010", " ");
01973 drms_setkey_string(outRec, "BUNIT_011", "degree");
01974 drms_setkey_string(outRec, "BUNIT_012", "degree");
01975
01976 } else {
01977
01978 drms_setkey_float(outRec, "CRPIX1", mInfo->ncol/2. + 0.5);
01979 drms_setkey_float(outRec, "CRPIX2", mInfo->nrow/2. + 0.5);
01980
01981 drms_setkey_float(outRec, "CRVAL1", mInfo->xc);
01982 drms_setkey_float(outRec, "CRVAL2", mInfo->yc);
01983 drms_setkey_float(outRec, "CDELT1", mInfo->xscale);
01984 drms_setkey_float(outRec, "CDELT2", mInfo->yscale);
01985 drms_setkey_string(outRec, "CUNIT1", "degree");
01986 drms_setkey_string(outRec, "CUNIT2", "degree");
01987
01988 snprintf (key, 64, "CRLN-%s", wcsCode[mInfo->mapOpt]);
01989 drms_setkey_string(outRec, "CTYPE1", key);
01990 snprintf (key, 64, "CRLT-%s", wcsCode[mInfo->mapOpt]);
01991 drms_setkey_string(outRec, "CTYPE2", key);
01992 drms_setkey_float(outRec, "CROTA2", 0.0);
01993
01994 drms_setkey_string(outRec, "PROJECTION", mapName[mInfo->mapOpt]);
01995
01996
01997 drms_setkey_string(outRec, "BUNIT_003", "Gauss");
01998 drms_setkey_string(outRec, "BUNIT_004", "Gauss");
01999 drms_setkey_string(outRec, "BUNIT_005", "Gauss");
02000 drms_setkey_string(outRec, "BUNIT_006", " ");
02001 drms_setkey_string(outRec, "BUNIT_007", " ");
02002 drms_setkey_string(outRec, "BUNIT_008", "degree");
02003 drms_setkey_string(outRec, "BUNIT_009", "degree");
02004
02005 }
02006
02007
02008 }
02009
02010
02011
02012
02013
02014
02015
02016 int getEphemeris(DRMS_Record_t *inRec, double *disk_lonc, double *disk_latc,
02017 double *disk_xc, double *disk_yc, double *rSun, double *asd, double *pa)
02018 {
02019
02020 int status = 0;
02021
02022 float dSun, rSun_ref, cdelt;
02023 float crvalx, crvaly, crpix1, crpix2, crota2;
02024 double sina, cosa;
02025
02026 dSun = drms_getkey_float(inRec, "DSUN_OBS", &status);
02027 rSun_ref = drms_getkey_float(inRec, "RSUN_REF", &status);
02028 if (status) rSun_ref = 6.96e8;
02029 cdelt = drms_getkey_float(inRec, "CDELT1", &status);
02030 *asd = asin(rSun_ref/dSun);
02031
02032 *rSun = asin(rSun_ref / dSun) * RAD2ARCSEC / cdelt;
02033
02034 float testFulldisk = drms_getkey_float(inRec, "IMCRPIX1", &status);
02035
02036 if (status) {
02037 crvalx = drms_getkey_float(inRec, "CRVAL1", &status);
02038 crvaly = drms_getkey_float(inRec, "CRVAL2", &status);
02039 crpix1 = drms_getkey_float(inRec, "CRPIX1", &status);
02040 crpix2 = drms_getkey_float(inRec, "CRPIX2", &status);
02041 } else {
02042 crvalx = drms_getkey_float(inRec, "IMCRVAL1", &status);
02043 crvaly = drms_getkey_float(inRec, "IMCRVAL2", &status);
02044 crpix1 = drms_getkey_float(inRec, "IMCRPIX1", &status);
02045 crpix2 = drms_getkey_float(inRec, "IMCRPIX2", &status);
02046 }
02047
02048 crota2 = drms_getkey_float(inRec, "CROTA2", &status);
02049 sina = sin(crota2 * RADSINDEG);
02050 cosa = cos(crota2 * RADSINDEG);
02051
02052 *disk_xc = PIX_X(0.0,0.0) - 1.0;
02053 *disk_yc = PIX_Y(0.0,0.0) - 1.0;
02054
02055 *disk_latc = drms_getkey_float(inRec, "CRLT_OBS", &status) * RADSINDEG;
02056 *disk_lonc = drms_getkey_float(inRec, "CRLN_OBS", &status) * RADSINDEG;
02057 *pa = - crota2 * RADSINDEG;
02058
02059 return 0;
02060
02061 }
02062
02063
02064
02065 float nnb (float *f, int nx, int ny, double x, double y)
02066 {
02067
02068 if (x <= -0.5 || y <= -0.5 || x > nx - 0.5 || y > ny - 0.5)
02069 return DRMS_MISSING_FLOAT;
02070 int ilow = floor (x);
02071 int jlow = floor (y);
02072 int i = ((x - ilow) > 0.5) ? ilow + 1 : ilow;
02073 int j = ((y - jlow) > 0.5) ? jlow + 1 : jlow;
02074 return f[j * nx + i];
02075
02076 }
02077
02078
02079
02080 void frebin (float *image_in, float *image_out, int nx, int ny, int nbin, int gauss)
02081 {
02082
02083 struct fresize_struct fresizes;
02084 int nxout, nyout, xoff, yoff;
02085 int nlead = nx;
02086
02087 nxout = nx / nbin; nyout = ny / nbin;
02088 if (gauss && nbin != 1)
02089 init_fresize_gaussian(&fresizes, (nbin / 2), (nbin / 2 * 2), nbin);
02090 else
02091 init_fresize_bin(&fresizes, nbin);
02092 xoff = nbin / 2 + nbin / 2;
02093 yoff = nbin / 2 + nbin / 2;
02094 fresize(&fresizes, image_in, image_out, nx, ny, nlead, nxout, nyout, nxout, xoff, yoff, DRMS_MISSING_FLOAT);
02095
02096 }