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 #include <jsoc_main.h>
00069 #include <stdio.h>
00070 #include <stdlib.h>
00071 #include <unistd.h>
00072 #include <math.h>
00073 #include <sys/time.h>
00074 #include <sys/resource.h>
00075 #include <unistd.h>
00076 #include "copy_me_keys.c"
00077 #include "cartography.c"
00078 #include "timing.c"
00079 #include "fstats.h"
00080
00081
00082
00083 #include "noisemaskimag4twindow.c"
00084 #include "obstime2maskid.c"
00085
00086 #define PI (M_PI)
00087 #define DTOR (PI / 180.)
00088 #define Rad2arcsec (3600. * 180. / PI)
00089 #define FOURK (4096)
00090 #define FOURK2 (16777216)
00091
00092 #define ARRLENGTH(ARR) (sizeof(ARR) / sizeof(ARR[0]))
00093 #define DIE(msg) {fflush(stdout); fprintf(stderr, "%s, status=%d\n", msg, status); return(status);}
00094 #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
00095
00096 #define BB(bx,by,bz) (sqrt(bx*bx+by*by+bz*bz))
00097 #define BH(bx,by) (sqrt(bx*bx+by*by))
00098
00099
00100
00101
00102 #define PIX_X(wx,wy) ((((wx-crvalx)*cosa + (wy-crvaly)*sina)/cdelt)+crpix1)
00103 #define PIX_Y(wx,wy) ((((wy-crvaly)*cosa - (wx-crvalx)*sina)/cdelt)+crpix2)
00104 #define WX(pix_x,pix_y) (((pix_x-crpix1)*cosa - (pix_y-crpix2)*sina)*cdelt+crvalx)
00105 #define WY(pix_x,pix_y) (((pix_y-crpix2)*cosa + (pix_x-crpix1)*sina)*cdelt+crvaly)
00106
00107
00108
00109
00110
00111
00112 #define ambCode0 0x0000 // 000, not disambiguated
00113 #define ambCode1 0x0001 // 001, weak not annealed (pf/radial acute/random)
00114 #define ambCode2 0x0003 // 011, weak annealed
00115 #define ambCode3 0x0007 // 111, strong annealed
00116
00117
00118 #define QUAL_ISSTARGET (0x00004000) //the ISS loop was OPEN for one or several filtergrams used to produce the observable
00119 #define QUAL_POORQUALITY (0x20) //poor quality: careful when using these observables due to eclipse, or lunar transit, or thermal recovery, or open ISS, or other issues...
00120
00121
00122 #define QUALMAP 1 // QUALMAP not updated if 0
00123
00124
00125 #define NOISYB (140.)
00126
00127
00128
00129
00130
00131
00132
00133 int getAmbCode (char prob)
00134 {
00135 if (prob < 11)
00136 return ambCode0;
00137 else if (prob < 51)
00138 return ambCode1;
00139 else if (prob < 61)
00140 return ambCode2;
00141 else
00142 return ambCode3;
00143 }
00144
00145
00146
00147
00148
00149 int getEphemeris(DRMS_Record_t *inRec, float *radius, float *pix_x, float *pix_y);
00150
00151
00152
00153 int getGeometry(DRMS_Record_t *inRec, int npad, int *geometry,
00154 int *ll, int *ur, int *ll0, int *ur0,
00155 float *radius, float *xcen, float *ycen);
00156
00157
00158
00159 int getData(DRMS_Record_t *inRec, float *Bx, float *By, float *Bz, float *dBt,
00160 int *ll, int *ur, int *ll0, int *ur0);
00161
00162
00163
00164 int getBitmap(DRMS_Record_t *inRec, int *bitmap, float *Bx, float *By, float *Bz,
00165 int geometry, int *useMask, int nrt, int *offset, int nerode, int ngrow,
00166 int *ll, int *ur, int *ll0, int *ur0, float bthresh0, float bthresh1, char *maskQuery);
00167
00168
00169
00170 int getNoiseMask(DRMS_Record_t *inRec, float *noiseMask, int *ll, int *ur, char *maskQuery);
00171
00172
00173
00174 int getLinearMask(DRMS_Record_t *inRec, float *noiseMask, int *ll, int *ur,
00175 float bthresh0, float bthresh1);
00176
00177
00178
00179 void getRadialAcute(DRMS_Record_t *inRec, char *ambig_r, float *Bx, float *By, float *Bz,
00180 int *ll, int *ur, int *ll0, int *ur0);
00181
00182
00183
00184 void erodeAndGrow(DRMS_Record_t *inRec, int *bitmap, int nerode, int ngrow,
00185 int *ll, int *ur, int *ll0, int *ur0);
00186
00187
00188
00189 int writeData(DRMS_Record_t *outRec, DRMS_Record_t *inRec,
00190 float *Bx, float *probBa, char *ambig_r,
00191 int *ll, int *ur, int *ll0, int *ur0, int harpflag);
00192
00193
00194
00195 int writeAuxData(DRMS_Record_t *outRec, DRMS_Record_t *inRec,
00196 float *Bx, float *By, float *Bz, float *probBa, float *dBt, int *bitmap,
00197 int *ll, int *ur);
00198
00199
00200
00201 void set_Bunit(DRMS_Record_t *outRec);
00202
00203
00204
00205 void set_stats(DRMS_Record_t *outRec);
00206
00207
00208
00209 static int AcquireLock(int fd)
00210 {
00211 int ret = -1;
00212 int natt = 0;
00213
00214
00215 while ((ret = lockf(fd, F_TLOCK, 0)) != 0 && natt < 10)
00216 {
00217 sleep(1);
00218 natt++;
00219 }
00220
00221 return (ret == 0);
00222 }
00223
00224 static void ReleaseLock(int fd)
00225 {
00226 lockf(fd, F_ULOCK, 0);
00227 }
00228
00229
00230
00231
00232
00233 #ifdef OLD
00234 extern void ambig_(int *geometry,
00235 int *weak,
00236 int *nx, int *ny,
00237 int *npad,
00238 float *xcen, float *ycen,
00239 int *verb,
00240 float *lambda,
00241 int *neq, float *tfactr, float *tfac0,
00242 int *seed,
00243 int *ntx, int *nty,
00244 float *Bx, float *By, float *Bz, float *dBt, float *probBa,
00245 int *bitmap,
00246 float *radius,
00247 int *nap, float *bthresh0, float *bthresh1);
00248 #else
00249 extern void ambig_(int *geometry,
00250 int *weak,
00251 int *nx, int *ny,
00252 int *npad,
00253 float *xcen, float *ycen,
00254 int *verb,
00255 float *lambda,
00256 int *neq, float *tfactr, float *tfac0,
00257 int *seed,
00258 int *ntx, int *nty,
00259 float *Bx, float *By, float *Bz, float *probBa,
00260 int *bitmap,
00261 float *radius,
00262 int *nap);
00263 #endif
00264
00265
00266
00267 char *module_name = "disambig_v3";
00268 char *version_id = "2013 Dec 06";
00269
00270 #ifdef OLD
00271 char *segName[] = {"field", "inclination", "azimuth", "alpha_mag",
00272 "field_err", "inclination_err", "alpha_err",
00273 "field_inclination_err", "field_alpha_err", "inclination_alpha_err"};
00274 #else
00275 char *segName[] = {"field", "inclination", "azimuth", "alpha_mag"};
00276 #endif
00277
00278
00279 char *BSegName[] = {"inclination", "azimuth", "disambig", "field", "vlos_mag", "dop_width",
00280 "eta_0", "damping", "src_continuum", "src_grad", "alpha_mag", "chisq", "conv_flag",
00281 "inclination_err", "azimuth_err", "field_err", "vlos_err", "alpha_err",
00282 "field_inclination_err", "field_az_err", "inclin_azimuth_err", "field_alpha_err", "inclination_alpha_err",
00283 "azimuth_alpha_err", "conf_disambig", "info_map", "confid_map", "bitmap"};
00284 char *Bunit[] = {"degree", "degree", " ", "Mx/cm^2", "cm/s", "mA",
00285 " ", "length units", "DN/s", "DN/s", " ", " ", " ",
00286 "degree", "degree", "Mx/cm^2", "cm/s", " ",
00287 " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
00288
00289
00290 ModuleArgs_t module_args[] =
00291 {
00292 {ARG_STRING, "in", NULL, "Input data series."},
00293 {ARG_STRING, "out", NULL, "Output data series."},
00294 {ARG_INT, "VERB", "1", "Level of verbosity: 0=errors/warnings; 1=minimal messages; 2=all messages"},
00295 {ARG_FLAG, "l", "", "Flag to use linear noise threshold (AMBTHRn)"},
00296 {ARG_FLAG, "n", "", "Flag for nrt series"},
00297 {ARG_INT, "OFFSET", "20", "Constant added to the noise mask"},
00298 {ARG_INT, "AMBGMTRY", "0", "0 for automatic selection; 1 for planar; 2 for spherical."},
00299 {ARG_INT, "AMBWEAK", "1", "0 for random; 1 for potential field; 2 for most radial."},
00300 {ARG_INT, "AMBNEROD", "1", "Number of pixels by which to erode map of above threshold pixels."},
00301 {ARG_INT, "AMBNGROW", "5", "Number of pixels by which to grow eroded map."},
00302 {ARG_INT, "AMBNPAD", "20", "Pixel number to pad with zeros for potential field."},
00303 {ARG_INT, "AMBNAP", "10", "Pixel number to apodize for potential field."},
00304 {ARG_INT, "AMBNTX", "20", "Tile number in x (lon) direction for pf on a sphere."},
00305 {ARG_INT, "AMBNTY", "20", "Tile number in y (lat) direction for pf on a sphere."},
00306 {ARG_FLOAT, "AMBBTHR0", "2.0e2", "Threshold field strength."},
00307 {ARG_FLOAT, "AMBBTHR1", "4.0e2", "Threshold field strength."},
00308 {ARG_INT, "AMBSEED", "1", "Input random number seed (seed>0)."},
00309 {ARG_INT, "AMBNEQ", "20", "Num of reconfigurations attempted at each temperature setting."},
00310 {ARG_FLOAT, "AMBLMBDA", "1.", "Weighting factor between div. and vert. current density."},
00311 {ARG_FLOAT, "AMBTFCT0", "2.", "Input factor to scale initial temperature (tfac0>0)."},
00312 {ARG_FLOAT, "AMBTFCTR", "0.990", "Input factor to reduce temperature (0<tfactr<1)."},
00313 {ARG_STRING, "errlog", "/home/jsoc/locks/disambig_errlog.txt", "Error log for skipped full disk disambiguation"},
00314 {ARG_FLAG, "r", "", "Flag to suppress quality checking and error log"},
00315 {ARG_END}
00316 };
00317
00318
00319
00320 int DoIt(void)
00321 {
00322
00323 int status = DRMS_SUCCESS;
00324
00325
00326
00327 double wt0, wt1, wt;
00328 double ut0, ut1, ut;
00329 double st0, st1, st;
00330 double ct0, ct1, ct;
00331 wt0 = getwalltime();
00332 ct0 = getcputime(&ut0, &st0);
00333
00334
00335
00336 char ambcodev[50];
00337 sprintf(ambcodev,"%s %s", module_name, version_id);
00338
00339
00340
00341 char *inQuery = (char *)params_get_str(&cmdparams, "in");
00342 char *outQuery = (char *)params_get_str(&cmdparams, "out");
00343 int verbflag = params_get_int(&cmdparams, "VERB");
00344 int geometry = params_get_int(&cmdparams, "AMBGMTRY");
00345 int useMask = !(params_isflagset(&cmdparams, "l"));
00346 int nrt = params_isflagset(&cmdparams, "n");
00347 int offset = params_get_int(&cmdparams, "OFFSET");
00348
00349 int weak = params_get_int(&cmdparams, "AMBWEAK");
00350 int nerode = params_get_int(&cmdparams, "AMBNEROD");
00351 int ngrow = params_get_int(&cmdparams, "AMBNGROW");
00352 int npad = params_get_int(&cmdparams, "AMBNPAD");
00353 int nap = params_get_int(&cmdparams, "AMBNAP");
00354 int ntx = params_get_int(&cmdparams, "AMBNTX");
00355 int nty = params_get_int(&cmdparams, "AMBNTY");
00356 int seed = params_get_int(&cmdparams, "AMBSEED");
00357 int neq = params_get_int(&cmdparams, "AMBNEQ");
00358 float bthresh0 = params_get_float(&cmdparams, "AMBBTHR0");
00359 float bthresh1 = params_get_float(&cmdparams, "AMBBTHR1");
00360 float lambda = params_get_float(&cmdparams, "AMBLMBDA");
00361 float tfac0 = params_get_float(&cmdparams, "AMBTFCT0");
00362 float tfactr = params_get_float(&cmdparams, "AMBTFCTR");
00363
00364
00365 int noQualCheck = params_isflagset(&cmdparams, "r");
00366 const char *errLog = params_get_str(&cmdparams, "errlog");
00367
00368
00369
00370
00371 if (nap > npad) {nap = npad; SHOW("nap set to npad\n");}
00372 if (seed <= 0) {seed = 1; SHOW("seed set to 1\n");}
00373 if (lambda < 0) {lambda = 1.0; SHOW("lambda set to 1.0\n");}
00374 if (tfac0 < 0) {tfac0 = 2.; SHOW("tfac0 set to 2.0\n");}
00375 if (neq <= 0) {neq = 20; SHOW("neq set to 20\n");}
00376 if ((tfactr < 0) || (tfactr > 1)) {tfactr = 0.995; SHOW("tfactr set to 0.995\n");}
00377 if (geometry < 0 || geometry > 2) {DIE("Unknown geometry flag");}
00378 if (weak < 0 || weak > 2) {DIE("Unknown weak flag");}
00379 if (nerode <= 0) {nerode = 1; SHOW("nerode set to 1\n");}
00380 if (ngrow <= 0) {ngrow = 5; SHOW("ngrow set to 5\n");}
00381
00382
00383
00384 DRMS_RecordSet_t *inRS = drms_open_records(drms_env, inQuery, &status);
00385 if (status || inRS->n == 0) DIE("No input data found");
00386 int nrecs = inRS->n;
00387
00388
00389 for (int irec = 0; irec < nrecs; irec++) {
00390
00391
00392
00393 if (verbflag) {
00394 wt1 = getwalltime();
00395 ct1 = getcputime(&ut1, &st1);
00396 printf("processing record %d of %d...\n", irec, nrecs);
00397 }
00398
00399
00400
00401 DRMS_Record_t *inRec = inRS->records[irec];
00402 TIME t_rec = drms_getkey_time(inRec, "T_REC", &status);
00403 int harpnum = drms_getkey_int(inRec, "HARPNUM", &status);
00404 int harpflag = (harpnum == DRMS_MISSING_INT) ? 0 : 1;
00405 if (harpflag) ngrow = 0;
00406 int useMask_t = useMask;
00407 int geometry_t = geometry;
00408 char t_rec_str[100];
00409 sprint_time(t_rec_str, t_rec, "TAI", 0);
00410
00411 if (verbflag) {
00412 printf("T_REC=%s\n", t_rec_str);
00413 }
00414
00415
00416
00417 int skipRec = 0;
00418 if (!noQualCheck && !harpflag) {
00419 int qual = drms_getkey_int(inRec, "QUALITY", &status);
00420 float meanB = drms_getkey_float(inRec, "DATAMEAN_002", &status);
00421 if ((qual & QUAL_POORQUALITY) || (qual & QUAL_ISSTARGET) || (qual && (fabs(meanB) > NOISYB))) {
00422 printf("T_REC=%s, QUALITY=0x%x, DATAMEAN_002=%f\n", t_rec_str, qual, meanB);
00423 skipRec = 1;
00424 int lockfd = open(errLog, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG);
00425 if (lockfd >= 0) {
00426 if (AcquireLock(lockfd)) {
00427 FILE *fp = NULL;
00428
00429 if (fp = fdopen(lockfd, "a")) {
00430 fprintf(fp, "T_REC=%s, QUALITY=0x%x, DATAMEAN_002=%f\n", t_rec_str, qual, meanB);
00431 ReleaseLock(lockfd);
00432 fclose(fp);
00433 }
00434 else {
00435 close(lockfd);
00436 fprintf(stderr, "Unable to write into %s.\n", errLog);
00437 }
00438 } else {
00439 close(lockfd);
00440 fprintf(stderr, "Unable to acquire lock on %s.\n", errLog);
00441 }
00442 } else {
00443 fprintf(stderr, "Unable to open lock file for appending to: %s.\n", errLog);
00444 }
00445 }
00446 }
00447 if (skipRec) continue;
00448
00449
00450
00451 int ll[2], ur[2];
00452 int ll0[2], ur0[2];
00453 float radius, xcen, ycen;
00454
00455 if (getGeometry(inRec, npad, &geometry_t,
00456 ll, ur, ll0, ur0,
00457 &radius, &xcen, &ycen)) {
00458 printf("Getting geometry failed, image #%d skipped.\n", irec);
00459 continue;
00460 }
00461
00462 int nx = ur[0] - ll[0] + 1;
00463 int ny = ur[1] - ll[1] + 1;
00464 int nx0 = ur0[0] - ll0[0] + 1;
00465 int ny0 = ur0[1] - ll0[1] + 1;
00466 int nxny = nx * ny, nxny0 = nx0 * ny0;
00467
00468 printf("ll=[%d,%d], ur=[%d,%d], nx=%d, ny=%d\n", ll[0], ll[1], ur[0], ur[1], nx, ny);
00469 printf("ll0=[%d,%d], ur0=[%d,%d], nx0=%d, ny0=%d\n", ll0[0], ll0[1], ur0[0], ur0[1], nx0, ny0);
00470 printf("radius=%f, xcen=%f, ycen=%f\n", radius, xcen, ycen);
00471
00472 printf("Geometry determined. \n");
00473
00474
00475
00476 float *Bx = (float *) (malloc(nxny * sizeof(float)));
00477 float *By = (float *) (malloc(nxny * sizeof(float)));
00478 float *Bz = (float *) (malloc(nxny * sizeof(float)));
00479 float *dBt = (float *) (malloc(nxny * sizeof(float)));
00480
00481 if (getData(inRec, Bx, By, Bz, dBt, ll, ur, ll0, ur0)) {
00482 free(Bx); free(By); free(Bz); free(dBt);
00483 printf("Getting data failed, image #%d skipped.\n", irec);
00484 continue;
00485 }
00486
00487 printf("Arrays read. \n");
00488
00489
00490
00491 int *bitmap = (int *) (calloc(nxny, sizeof(int)));
00492
00493 int offset_t = offset;
00494 char maskQuery[100];
00495
00496 if (getBitmap(inRec, bitmap, Bx, By, Bz,
00497 geometry, &useMask_t, nrt, &offset_t, nerode, ngrow,
00498 ll, ur, ll0, ur0, bthresh0, bthresh1, maskQuery)) {
00499 free(bitmap);
00500 free(Bx); free(By); free(Bz);
00501 printf("Creating bitmap failed, image #%d skipped.\n", irec);
00502 continue;
00503 }
00504
00505 printf("Bitmap created. \n");
00506
00507
00508
00509 char *ambig_r = NULL;
00510 if (!harpflag) {
00511 ambig_r = (char *) calloc(nxny0, sizeof(char));
00512 getRadialAcute(inRec, ambig_r, Bx, By, Bz, ll, ur, ll0, ur0);
00513 printf("Radial acute solution determined\n");
00514 }
00515
00516
00517
00518 printf("start...\n");
00519
00520 float *probBa = (float *) (malloc(nxny * sizeof(float)));
00521
00522 printf("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n",
00523 geometry_t, nx, ny, weak, npad, seed, neq, ntx, nty, nap);
00524 printf("%f, %f, %f, %f, %f, %f, %f, %f, %f\n",
00525 xcen, ycen, radius, lambda, tfactr, tfac0, radius, bthresh0, bthresh1);
00526
00527 #ifndef NO_DISAMBIG
00528 #ifdef OLD
00529 ambig_(&geometry_t,
00530 &weak,
00531 &nx, &ny,
00532 &npad,
00533 &xcen, &ycen,
00534 &verbflag,
00535 &lambda,
00536 &neq, &tfactr, &tfac0,
00537 &seed,
00538 &ntx, &nty,
00539 Bx, By, Bz, probBa,
00540 dBt, bitmap,
00541 &radius,
00542 &nap, &bthresh0, &bthresh1);
00543 #else
00544 ambig_(&geometry_t,
00545 &weak,
00546 &nx, &ny,
00547 &npad,
00548 &xcen, &ycen,
00549 &verbflag,
00550 &lambda,
00551 &neq, &tfactr, &tfac0,
00552 &seed,
00553 &ntx, &nty,
00554 Bx, By, Bz, probBa,
00555 bitmap,
00556 &radius,
00557 &nap);
00558 #endif
00559 #endif
00560
00561 printf("done\n");
00562
00563
00564
00565
00566 printf("Creating output record...\n");
00567 DRMS_Record_t *outRec = drms_create_record(drms_env, outQuery, DRMS_PERMANENT, &status);
00568 if (status) {
00569 printf("Creating output record failed, image #%d skipped.\n", irec);
00570 continue;
00571 }
00572
00573 #ifdef WRITEBITMAP // output bitmap, etc. for testing purpose
00574
00575 if (writeAuxData(outRec, inRec, Bx, By, Bz, probBa, dBt, bitmap, ll, ur)) {
00576 printf("Output test arrays error, record skipped\n");
00577 continue;
00578 }
00579
00580 #else
00581
00582 if (writeData(outRec, inRec, Bx, probBa, ambig_r, ll, ur, ll0, ur0, harpflag)) {
00583 printf("Output error, record skipped\n");
00584 free(Bx); free(By); free(Bz);
00585 free(bitmap); free(probBa); free(ambig_r);
00586 continue;
00587 }
00588
00589 #endif
00590
00591
00592
00593
00594
00595 DRMS_Link_t *link = NULL;
00596 if (harpflag) {
00597 link = hcon_lookup_lower(&outRec->links, "PATCH");
00598 if (link) drms_link_set("PATCH", outRec, inRec);
00599 } else {
00600 link = hcon_lookup_lower(&outRec->links, "MDATA");
00601 if (link) drms_link_set("MDATA", outRec, inRec);
00602 }
00603
00604
00605
00606
00607 printf("Set keywords\n");
00608 drms_copykey(outRec, inRec, "T_REC");
00609 if (harpflag) drms_copykey(outRec, inRec, "HARPNUM");
00610
00611 copy_me_keys(inRec, outRec);
00612 copy_geo_keys(inRec, outRec);
00613 set_Bunit(outRec);
00614 drms_copykey(outRec, inRec, "QUALITY");
00615 if (harpflag) {
00616 copy_patch_keys(inRec, outRec);
00617 } else {
00618 drms_setkey_time(outRec, "DATE_ME", drms_getkey_time(inRec, "DATE", &status));
00619 drms_setkey_int(outRec, "QUAL_ME", drms_getkey_int(inRec, "QUALITY", &status));
00620
00621 set_stats(outRec);
00622 }
00623 drms_setkey_int(outRec, "DOFFSET", offset_t);
00624
00625 drms_setkey_string(outRec, "BLD_VERS", jsoc_version);
00626 char timebuf[1024];
00627 double UNIX_epoch = -220924792.0;
00628 sprint_time(timebuf, (double)time(NULL) + UNIX_epoch, "ISO", 0);
00629 drms_setkey_string(outRec, "DATE", timebuf);
00630
00631 drms_setkey_int(outRec, "AMBPATCH", harpflag);
00632 drms_setkey_int(outRec, "AMBGMTRY", geometry_t);
00633 drms_setkey_int(outRec, "AMBWEAK", weak);
00634 drms_setkey_int(outRec, "AMBNEROD", nerode);
00635 drms_setkey_int(outRec, "AMBNGROW", ngrow);
00636 drms_setkey_int(outRec, "AMBNPAD", npad);
00637 drms_setkey_int(outRec, "AMBNAP", nap);
00638 drms_setkey_int(outRec, "AMBNTX", ntx);
00639 drms_setkey_int(outRec, "AMBNTY", nty);
00640 drms_setkey_int(outRec, "AMBSEED", seed);
00641 drms_setkey_int(outRec, "AMBNEQ", neq);
00642 drms_setkey_float(outRec, "AMBBTHR0", bthresh0);
00643 drms_setkey_float(outRec, "AMBBTHR1", bthresh1);
00644 drms_setkey_float(outRec, "AMBLMBDA", lambda);
00645 drms_setkey_float(outRec, "AMBTFCT0", tfac0);
00646 drms_setkey_float(outRec, "AMBTFCTR", tfactr);
00647
00648 drms_setkey_string(outRec, "CODEVER5", "$Id: disambig_v3.c,v 1.18 2015/04/23 22:05:51 xudong Exp $");
00649 drms_setkey_string(outRec, "AMBCODEV", ambcodev);
00650
00651 if (useMask_t) {
00652 drms_setkey_int(outRec, "NOISEMASK", 1);
00653 drms_setkey_string(outRec, "MASKINFO", maskQuery);
00654 } else {
00655 drms_setkey_int(outRec, "NOISEMASK", 0);
00656 drms_setkey_string(outRec, "MASKINFO", "Linear");
00657 }
00658
00659 drms_setkey_string(outRec, "CODENAME", module_name);
00660
00661
00662
00663
00664 drms_close_record(outRec, DRMS_INSERT_RECORD);
00665
00666 #ifndef WRITEBITMAP
00667 free(Bx); free(By); free(Bz); free(dBt);
00668 free(probBa); free(bitmap);
00669 #endif
00670
00671 if (harpflag) free(ambig_r);
00672
00673
00674
00675 if (verbflag) {
00676 wt = getwalltime();
00677 ct = getcputime(&ut, &st);
00678 printf("record %d done, %.2f ms wall time, %.2f ms cpu time\n",
00679 irec, wt - wt1, ct - ct1);
00680 }
00681
00682 }
00683
00684 drms_close_records(inRS, DRMS_FREE_RECORD);
00685
00686 if (verbflag) {
00687 wt = getwalltime();
00688 ct = getcputime(&ut, &st);
00689 printf("total time spent: %.2f ms wall time, %.2f ms cpu time\n",
00690 wt - wt0, ct - ct0);
00691 }
00692
00693 return 0;
00694
00695 }
00696
00697
00698
00699
00700
00701
00702 int getEphemeris(DRMS_Record_t *inRec, float *radius, float *pix_x, float *pix_y)
00703 {
00704
00705 int status = 0;
00706
00707 int harpnum = drms_getkey_int(inRec, "HARPNUM", &status);
00708 int harpflag = (harpnum == DRMS_MISSING_INT) ? 0 : 1;
00709
00710 float crota2 = drms_getkey_float(inRec, "CROTA2", &status);
00711 float sina = sin(crota2 * DTOR);
00712 float cosa = cos(crota2 * DTOR);
00713 float cdelt = drms_getkey_float(inRec, "CDELT1", &status);
00714 float rsun_ref = drms_getkey_double(inRec, "RSUN_REF", &status);
00715 if (status) rsun_ref = 6.96e8;
00716 float dsun_obs = drms_getkey_double(inRec, "DSUN_OBS", &status);
00717
00718 *radius = asin(rsun_ref / dsun_obs) * Rad2arcsec / cdelt;
00719
00720 float crvalx, crvaly, crpix1, crpix2;
00721 if (harpflag) {
00722 crvalx = drms_getkey_float(inRec, "IMCRVAL1", &status);
00723 crvaly = drms_getkey_float(inRec, "IMCRVAL2", &status);
00724 crpix1 = drms_getkey_float(inRec, "IMCRPIX1", &status);
00725 crpix2 = drms_getkey_float(inRec, "IMCRPIX2", &status);
00726 } else {
00727 crvalx = drms_getkey_float(inRec, "CRVAL1", &status);
00728 crvaly = drms_getkey_float(inRec, "CRVAL2", &status);
00729 crpix1 = drms_getkey_float(inRec, "CRPIX1", &status);
00730 crpix2 = drms_getkey_float(inRec, "CRPIX2", &status);
00731 }
00732
00733 *pix_x = PIX_X(0.0,0.0);
00734 *pix_y = PIX_Y(0.0,0.0);
00735
00736 return 0;
00737
00738 }
00739
00740
00741
00742
00743
00744 int getGeometry(DRMS_Record_t *inRec, int npad, int *geometry,
00745 int *ll, int *ur, int *ll0, int *ur0,
00746 float *radius, float *xcen, float *ycen)
00747 {
00748
00749 int status = 0;
00750 int nx, ny, nx0, ny0;
00751
00752
00753
00754 float pix_x, pix_y;
00755 if (getEphemeris(inRec, radius, &pix_x, &pix_y)) {
00756 SHOW("Getting ephemeris error\n");
00757 return 1;
00758 }
00759
00760
00761
00762 int harpnum = drms_getkey_int(inRec, "HARPNUM", &status);
00763 int harpflag = (harpnum == DRMS_MISSING_INT) ? 0 : 1;
00764
00765 if (harpflag) {
00766
00767
00768
00769 if (*geometry == 0) {
00770 float minlon = drms_getkey_float(inRec, "LON_MIN", &status);
00771 float minlat = drms_getkey_float(inRec, "LAT_MIN", &status);
00772 float maxlon = drms_getkey_float(inRec, "LON_MAX", &status);
00773 float maxlat = drms_getkey_float(inRec, "LAT_MAX", &status);
00774 if (fabs(maxlon - minlon) < 20. && fabs(maxlat - minlat) < 20.) {
00775 *geometry = 1;
00776 printf("Geometry selection: using planar geometry\n");
00777 } else {
00778 *geometry = 2;
00779 printf("Geometry selection: using spherical geometry\n");
00780 }
00781 }
00782
00783
00784
00785 DRMS_Segment_t *maskSeg = drms_segment_lookup(inRec, "bitmap");
00786 nx0 = maskSeg->axis[0]; ny0 = maskSeg->axis[1];
00787
00788
00789
00790 ll0[0] = drms_getkey_int(inRec, "CRPIX1", &status) - 1;
00791 ll0[1] = drms_getkey_int(inRec, "CRPIX2", &status) - 1;
00792 ur0[0] = ll0[0] + nx0 - 1;
00793 ur0[1] = ll0[1] + ny0 - 1;
00794
00795
00796
00797 nx = nx0 + 2 * npad;
00798 ny = ny0 + 2 * npad;
00799
00800 ll[0] = ll0[0] - npad;
00801 ll[1] = ll0[1] - npad;
00802 ur[0] = ur0[0] + npad;
00803 ur[1] = ur0[1] + npad;
00804
00805
00806
00807
00808 if (ll[0] < 0) {
00809 nx += ll[0];
00810 ll[0] = 0;
00811 }
00812 if (ll[1] < 0) {
00813 ny += ll[1];
00814 ll[1] = 0;
00815 }
00816 if (ur[0] > 4095) {
00817 nx -= (ur[0] - 4095);
00818 ur[0] = 4095;
00819 }
00820 if (ur[1] > 4095) {
00821 ny -= (ur[1] - 4095);
00822 ur[1] = 4095;
00823 }
00824
00825
00826
00827 if (*geometry == 1) {
00828 *xcen = (float)ll0[0] + 0.5 * ((float)(nx0) - 1.) - (pix_x - 1.);
00829 *ycen = (float)ll0[1] + 0.5 * ((float)(ny0) - 1.) - (pix_y - 1.);
00830 }
00831
00832
00833 if (*geometry == 2) {
00834 *xcen = pix_x - (float)ll[0];
00835 *ycen = pix_y - (float)ll[1];
00836 }
00837
00838
00839 } else {
00840
00841 *geometry = 2;
00842
00843 *xcen = pix_x;
00844 *ycen = pix_y;
00845
00846 nx = ny = nx0 = ny0 = FOURK;
00847 ll[0] = ll[1] = 0;
00848 ur[0] = ur[1] = FOURK - 1;
00849 ll0[0] = ll0[1] = 0;
00850 ur0[0] = ur0[1] = FOURK - 1;
00851
00852 }
00853
00854
00855
00856 return 0;
00857
00858 }
00859
00860
00861
00862
00863
00864
00865 int getData(DRMS_Record_t *inRec, float *Bx, float *By, float *Bz, float *dBt,
00866 int *ll, int *ur, int *ll0, int *ur0)
00867 {
00868
00869 int status = 0;
00870
00871 int nx = ur[0] - ll[0] + 1;
00872 int ny = ur[1] - ll[1] + 1;
00873 int nx0 = ur0[0] - ll0[0] + 1;
00874 int ny0 = ur0[1] - ll0[1] + 1;
00875 int nxny = nx * ny, nxny0 = nx0 * ny0;
00876
00877
00878
00879 int nseg = ARRLENGTH(segName);
00880 DRMS_Segment_t *inSeg;
00881 DRMS_Array_t *inArray[nseg], *inArray_qual, *inArray_conf;
00882 float *inData[nseg];
00883 int *inData_qual;
00884 char *inData_conf;
00885
00886 int harpnum = drms_getkey_int(inRec, "HARPNUM", &status);
00887 int harpflag = (harpnum == DRMS_MISSING_INT) ? 0 : 1;
00888
00889 if (harpflag) {
00890
00891 for (int i = 0; i < nseg; i++) {
00892 DRMS_Segment_t *inSeg = drms_segment_lookup(inRec, segName[i]);
00893 inArray[i] = drms_segment_readslice(inSeg, DRMS_TYPE_FLOAT, ll, ur, &status);
00894 if (status) {
00895 for (int j = 0; j < i; j++) drms_free_array(inArray[i]);
00896 printf("Segment reading error\n");
00897 return 1;
00898 }
00899 inData[i] = (float *) inArray[i]->data;
00900 }
00901
00902 } else {
00903
00904 for (int i = 0; i < nseg; i++) {
00905 inSeg = drms_segment_lookup(inRec, segName[i]);
00906 inArray[i] = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00907 if (status) {
00908 for (int j = 0; j <= i; j++) drms_free_array(inArray[i]);
00909 printf("Segment reading error\n");
00910 return 1;
00911 }
00912 inData[i] = (float *) inArray[i]->data;
00913 }
00914
00915 }
00916
00917
00918
00919
00920 double Bmag, Binc, Bazm, Bfil;
00921 double BmagBfil, CosBinc, SinBinc;
00922
00923 double varBmag, varBinc, varBfil;
00924 double varBmagBinc, varBfilBmag, varBfilBinc, BmagBfilSinBinc;
00925
00926 for (int i = 0; i < nxny; i++) {
00927
00928 Bmag = inData[0][i];
00929 Binc = inData[1][i] * DTOR;
00930 Bazm = inData[2][i] * DTOR;
00931 Bfil = inData[3][i];
00932
00933 BmagBfil = Bmag * Bfil;
00934 CosBinc = cos(Binc);
00935 SinBinc = sin(Binc);
00936 BmagBfilSinBinc = BmagBfil * SinBinc;
00937
00938 if (isnan(Bmag) || isnan(Binc) || isnan(Bazm) || isnan(Bfil)) {
00939
00940 Bx[i] = 0.; By[i] = 0.; Bz[i] = 0.;
00941
00942 } else {
00943
00944 Bx[i] = - BmagBfilSinBinc * sin(Bazm);
00945 By[i] = BmagBfilSinBinc * cos(Bazm);
00946 Bz[i] = BmagBfil * CosBinc;
00947
00948 }
00949
00950 #ifdef OLD
00951
00952
00953 varBmag = inData[4][i];
00954 varBinc = inData[5][i];
00955
00956 varBfil = 0.;
00957
00958
00959 varBmagBinc = inData[7][i];
00960 varBfilBmag = inData[8][i];
00961 varBfilBinc = inData[9][i];
00962
00963 if (isnan(Bmag) || isnan(Binc) || isnan(Bazm) || isnan(Bfil)) {
00964 dBt[i] = 1.E9;
00965 } else {
00966 dBt[i] = (BmagBfil==0) ? 1.E9 :
00967 (BmagBfil *
00968 sqrt (SinBinc * SinBinc * (varBfil / (Bfil * Bfil) + varBmag / (Bmag * Bmag)
00969 + 2.*varBfilBmag/BmagBfil)
00970 + varBinc * CosBinc * CosBinc
00971 + (varBfilBinc / Bfil + varBmagBinc / Bmag) * SinBinc * CosBinc));
00972 }
00973
00974
00975
00976 #endif
00977
00978 }
00979
00980 for (int i = 0; i < nseg; i++) drms_free_array(inArray[i]);
00981
00982
00983
00984 return 0;
00985
00986 }
00987
00988
00989
00990
00991
00992 int getBitmap(DRMS_Record_t *inRec, int *bitmap, float *Bx, float *By, float *Bz,
00993 int geometry, int *useMask, int nrt, int *offset, int nerode, int ngrow,
00994 int *ll, int *ur, int *ll0, int *ur0, float bthresh0, float bthresh1, char *maskQuery)
00995 {
00996
00997 int status = 0;
00998
00999 int harpnum = drms_getkey_int(inRec, "HARPNUM", &status);
01000 int harpflag = (harpnum == DRMS_MISSING_INT) ? 0 : 1;
01001
01002 int nx = ur[0] - ll[0] + 1;
01003 int ny = ur[1] - ll[1] + 1;
01004 int nx0 = ur0[0] - ll0[0] + 1;
01005 int ny0 = ur0[1] - ll0[1] + 1;
01006 int nxny = nx * ny, nxny0 = nx0 * ny0;
01007
01008 int i0 = ll0[0] - ll[0];
01009 int j0 = ll0[1] - ll[1];
01010
01011 float *noiseMask = (float *) (malloc(nxny * sizeof(float)));
01012
01013 float offset_t;
01014
01015 #ifdef OLDBITMAP
01016
01017 printf("%d, %d, %d, %d\n", i0, j0, i0 + nx0, j0 + ny0);
01018
01019 int idx;
01020 if (harpflag) {
01021 for (int i = i0; i < (i0 + nx0); i++) {
01022 for (int j = j0; j < (j0 + ny0); j++) {
01023 idx = j * nx + i;
01024 bitmap[idx] = 1;
01025 }
01026 }
01027 } else {
01028
01029 }
01030
01031 #else
01032
01033
01034
01035 if (*useMask &&
01036 !(getNoiseMask(inRec, noiseMask, ll, ur, maskQuery))) {
01037
01038 offset_t = *offset;
01039 SHOW("Using noise mask estimate\n");
01040
01041 } else {
01042
01043 *useMask = 0;
01044
01045 if (getLinearMask(inRec, noiseMask, ll, ur,
01046 bthresh0, bthresh1)) {
01047 SHOW("Error getting error estimate\n");
01048 return 1;
01049 }
01050
01051 offset_t = 0;
01052 SHOW("Using linear noise estimate\n");
01053
01054 }
01055
01056
01057
01058 printf("offset=%f\n", offset_t);
01059
01060 for (int i = 0; i < nxny; i++) {
01061 if (isnan(noiseMask[i])) {
01062 bitmap[i] = 0;
01063 } else {
01064 bitmap[i] = ((BH(Bx[i],By[i])) > (noiseMask[i] + offset_t)) ? 2 : 0;
01065 }
01066 }
01067
01068
01069
01070 erodeAndGrow(inRec, bitmap, nerode, ngrow, ll, ur, ll0, ur0);
01071
01072 #endif
01073
01074 *offset = offset_t;
01075 free(noiseMask);
01076
01077
01078
01079 return 0;
01080
01081 }
01082
01083
01084
01085
01086
01087 int getNoiseMask(DRMS_Record_t *inRec, float *noise, int *ll, int *ur, char *maskQuery)
01088 {
01089
01090 int status = 0;
01091
01092 int harpnum = drms_getkey_int(inRec, "HARPNUM", &status);
01093 int harpflag = (harpnum == DRMS_MISSING_INT) ? 0 : 1;
01094
01095 int nx = ur[0] - ll[0] + 1;
01096 int ny = ur[1] - ll[1] + 1;
01097
01098
01099
01100 TIME tobs = drms_getkey_time(inRec, "T_OBS", &status);
01101 float vr = drms_getkey_float(inRec, "OBS_VR", &status);
01102
01103 double *noise_fd = (double *) (malloc(FOURK2 * sizeof(double)));
01104
01105 int xDim = FOURK, yDim = FOURK;
01106 float r, x0, y0;
01107 if (getEphemeris(inRec, &r, &x0, &y0)) {
01108 SHOW("Getting ephemeris error\n");
01109 return 1;
01110 }
01111 x0 -= 1; y0 -= 1;
01112
01113
01114
01115
01116 int maskid = obstime2maskid(tobs);
01117 status = noisemaskimag4twindow(xDim, yDim, x0, y0, r, vr, maskid, noise_fd, maskQuery);
01118
01119 if (status) {
01120 free(noise_fd);
01121 return status;
01122 }
01123
01124
01125
01126 int idx, idx_fd;
01127 int full_disk = ((nx == FOURK) && (ny == FOURK));
01128 float r_thresh = 0.995 * r;
01129 for (int j = 0; j < ny; j++) {
01130 for (int i = 0; i < nx; i++) {
01131 idx = j * nx + i;
01132 idx_fd = (j + ll[1]) * FOURK + (i + ll[0]);
01133 noise[idx] = noise_fd[idx_fd];
01134 if (full_disk && (hypot(i * 1.0 + ll[0] - x0, j * 1.0 + ll[1] - y0) > r_thresh))
01135 noise[idx] = 1.e4;
01136 }
01137 }
01138
01139 free(noise_fd);
01140
01141 return 0;
01142
01143 }
01144
01145
01146
01147
01148
01149
01150 int getLinearMask(DRMS_Record_t *inRec, float *noiseMask, int *ll, int *ur,
01151 float bthresh0, float bthresh1)
01152 {
01153
01154 int nx = ur[0] - ll[0] + 1;
01155 int ny = ur[1] - ll[1] + 1;
01156 int nxny = nx * ny;
01157
01158
01159
01160 float radius, xcen, ycen;
01161 if (getEphemeris(inRec, &radius, &xcen, &ycen)) {
01162 SHOW("Getting ephemeris error\n");
01163 return 1;
01164 }
01165 xcen -= 1; ycen -= 1;
01166 float rad2 = radius * radius;
01167
01168
01169
01170 int idx;
01171 float xx, yy, mu2;
01172 for (int j = 0; j < ny; j++) {
01173 for (int i = 0; i < nx; i++) {
01174 idx = j * nx + i;
01175 xx = i + ll[0] - xcen;
01176 yy = j + ll[1] - ycen;
01177 mu2 = (xx * xx + yy * yy) / rad2;
01178 if (mu2 < 1) {
01179 noiseMask[idx] = bthresh1 + (bthresh0 - bthresh1) * sqrt(1. - mu2);
01180 } else {
01181 noiseMask[idx] = DRMS_MISSING_FLOAT;
01182 }
01183 }
01184 }
01185
01186 return 0;
01187 }
01188
01189
01190
01191
01192
01193 void getRadialAcute(DRMS_Record_t *inRec, char *ambig_r, float *Bx, float *By, float *Bz,
01194 int *ll, int *ur, int *ll0, int *ur0)
01195 {
01196
01197 int status = 0;
01198
01199 int nx = ur[0] - ll[0] + 1;
01200 int nx0 = ur0[0] - ll0[0] + 1;
01201 int ny0 = ur0[1] - ll0[1] + 1;
01202
01203 int i0 = ll0[0] - ll[0];
01204 int j0 = ll0[1] - ll[1];
01205
01206 float r, x0, y0;
01207 if (getEphemeris(inRec, &r, &x0, &y0)) {
01208 SHOW("Getting ephemeris error\n");
01209 return;
01210 }
01211 x0 -= 1; y0 -= 1;
01212
01213 float crota2 = drms_getkey_float(inRec, "CROTA2", &status);
01214 double lonc = drms_getkey_float(inRec, "CRLN_OBS", &status) * DTOR;
01215 double latc = drms_getkey_float(inRec, "CRLT_OBS", &status) * DTOR;
01216 double rsun_ref = drms_getkey_double(inRec, "RSUN_REF", &status);
01217 if (status) rsun_ref = 6.96e8;
01218 double dsun_obs = drms_getkey_double(inRec, "DSUN_OBS", &status);
01219
01220 double pa = -1. * crota2 * DTOR;
01221 double asd = asin(rsun_ref / dsun_obs);
01222
01223 double xx, yy;
01224 double lat, lon;
01225 double rho, sinlat, coslat, sig, mu, chi;
01226 double a31, a32, a33;
01227
01228 double bz0, bz1;
01229 int idx, idx1;
01230
01231 for (int i = 0; i < nx0; i++) {
01232 for (int j = 0; j < ny0; j++) {
01233 idx = i + j * nx0;
01234 idx1 = i + i0 + (j + j0) * nx;
01235
01236 xx = (ll0[0] + i - x0) / r;
01237 yy = (ll0[1] + j - y0) / r;
01238 if (img2sphere(xx, yy, asd, latc, lonc, pa,
01239 &rho, &lat, &lon, &sinlat, &coslat, &sig, &mu, &chi))
01240 continue;
01241
01242 a31 = cos(lat) * (sin(latc) * sin(pa) * cos(lon - lonc) + cos(pa) * sin(lon - lonc))
01243 - sin(lat) * cos(latc) * sin(pa);
01244 a32 = - cos(lat) * (sin(latc) * cos(pa) * cos(lon - lonc) - sin(pa) * sin(lon - lonc))
01245 + sin(lat) * cos(latc) * cos(pa);
01246 a33 = cos(lat) * cos(latc) * cos(lon - lonc) + sin(lat) * sin(latc);
01247
01248 bz0 = a31 * Bx[idx1] + a32 * By[idx1] + a33 * Bz[idx1];
01249 bz1 = - a31 * Bx[idx1] - a32 * By[idx1] + a33 * Bz[idx1];
01250
01251 ambig_r[idx] = (fabs(bz0) < fabs(bz1)) ? 1 : 0;
01252 }
01253 }
01254
01255
01256
01257 return;
01258
01259 }
01260
01261
01262
01263
01264
01265
01266
01267 void erodeAndGrow(DRMS_Record_t *inRec, int *bitmap, int nerode, int ngrow,
01268 int *ll, int *ur, int *ll0, int *ur0)
01269 {
01270
01271 int status = 0;
01272
01273 int harpnum = drms_getkey_int(inRec, "HARPNUM", &status);
01274 int harpflag = (harpnum == DRMS_MISSING_INT) ? 0 : 1;
01275
01276 int nx = ur[0] - ll[0] + 1;
01277 int ny = ur[1] - ll[1] + 1;
01278 int nxny = nx * ny;
01279 int nx0 = ur0[0] - ll0[0] + 1;
01280 int ny0 = ur0[1] - ll0[1] + 1;
01281 int nxny0 = nx0 * ny0;
01282
01283 int i, j, l, m;
01284
01285 printf("nerode=%d, ngrow=%d\n", nerode, ngrow);
01286
01287
01288
01289 int nxe = nx + 2 * nerode;
01290 int nye = ny + 2 * nerode;
01291 int nxnye = nxe * nye;
01292
01293 int *erodemap = (int *)calloc(nxnye, sizeof(int));
01294 for (i = 0; i < nxnye; i++) erodemap[i] = 1;
01295
01296 for (i = 0; i < nxny; i++) {
01297 if (bitmap[i] == 0) {
01298 j = i + nerode * (nx + 1 + 2 * (i / nx + nerode));
01299 for (l = -nerode; l <= nerode; l++) {
01300 for (m = -nerode; m <= nerode; m++) {
01301 erodemap[j + l + m * (nx + 2 * nerode)] = 0;
01302 }
01303 }
01304 }
01305 }
01306
01307
01308
01309
01310 int idx, idx1;
01311
01312 if (harpflag) {
01313
01314 int i0 = ll0[0] - ll[0], i1 = ur0[0] - ll[0];
01315 int j0 = ll0[1] - ll[1], j1 = ur0[1] - ll[1];
01316 printf("i0=%d,j0=%d,i1=%d,j1=%d\n", i0, j0, i1, j1);
01317 for (i = 0; i < nx; i++) {
01318 for (j = 0; j < ny; j++) {
01319 idx = j * nx + i;
01320 idx1 = (j + nerode) * nxe + (i + nerode);
01321 if (erodemap[idx1] == 0) { bitmap[idx] = 1; }
01322 if (i < i0 || i > i1 || j < j0 || j > j1) { bitmap[idx] = 0; }
01323 }
01324 }
01325 printf("here\n");
01326 free(erodemap);
01327 return;
01328
01329 } else {
01330
01331 for (i = 0; i < nx; i++) {
01332 for (j = 0; j < ny; j++) {
01333 idx = j * nx + i;
01334 idx1 = (j + nerode) * nxe + (i + nerode);
01335 if (erodemap[idx1] == 0) { bitmap[idx] = 0; }
01336 }
01337 }
01338
01339 }
01340
01341
01342
01343 int nxg = nxe + 2 * ngrow;
01344 int nyg = nye + 2 * ngrow;
01345 int nxnyg = nxg * nyg;
01346 int *growmap = (int *)calloc(nxnyg, sizeof(int));
01347
01348 for (i = 0; i < nxnye; i++) {
01349 if (erodemap[i]) {
01350 j = i + ngrow * (nxe + 1 + 2 * (i / nxe + ngrow));
01351 for (l = -ngrow; l <= ngrow; l++) {
01352 for (m = -ngrow; m <= ngrow; m++) {
01353 growmap[j + l + m * (nxe + 2 * ngrow)] = 1;
01354 }
01355 }
01356 }
01357 }
01358
01359
01360
01361 int tmp_bit;
01362 for (i = 0; i < nxny; i++) {
01363 tmp_bit = growmap[i + (nerode + ngrow) * (nx + 1 + 2 * (i / nx + nerode + ngrow))];
01364 if (tmp_bit && !bitmap[i]) { bitmap[i] = 1; }
01365 }
01366
01367 free(erodemap); free(growmap);
01368
01369
01370
01371 return;
01372
01373 }
01374
01375
01376
01377
01378
01379 int writeData(DRMS_Record_t *outRec, DRMS_Record_t *inRec,
01380 float *Bx, float *probBa, char *ambig_r,
01381 int *ll, int *ur, int *ll0, int *ur0, int harpflag)
01382 {
01383
01384 int status = 0;
01385
01386
01387
01388 int i0 = ll0[0] - ll[0];
01389 int j0 = ll0[1] - ll[1];
01390
01391 int nx = ur[0] - ll[0] + 1;
01392 int ny = ur[1] - ll[1] + 1;
01393 int nxny = nx * ny;
01394 int nx0 = ur0[0] - ll0[0] + 1;
01395 int ny0 = ur0[1] - ll0[1] + 1;
01396 int nxny0 = nx0 * ny0;
01397
01398 int outDims[2] = {nx0, ny0};
01399
01400
01401
01402 char *confidence = (char *) calloc(nxny0, sizeof(char));
01403 char *ambig_flag = (char *) calloc(nxny0, sizeof(char));
01404
01405 int idx, idx1;
01406 for (int i = 0; i < nx0; i++) {
01407 for (int j = 0; j < ny0; j++) {
01408 idx = i + j * nx0;
01409 idx1 = (i + i0) + (j + j0) * nx;
01410
01411 confidence[idx] = (char) (probBa[idx1] * 100);
01412 ambig_flag[idx] = (Bx[idx1] > 0.) ? 1 : 0;
01413 }
01414 }
01415
01416
01417
01418
01419 if (harpflag) {
01420
01421 for (int i = 0; i < nxny0; i++) {
01422 ambig_flag[i] = (ambig_flag[i] > 0) ? 7 : 0;
01423 }
01424
01425 } else {
01426
01427
01428
01429 char pot, r;
01430 srand((unsigned)time(NULL));
01431 for (int i = 0; i < nx0; i++) {
01432 for (int j = 0; j < ny0; j++) {
01433 idx = i + j * nx0;
01434 pot = (ambig_flag[idx] % 2);
01435 if (confidence[idx] > 51) {
01436 ambig_flag[idx] += pot * 2;
01437 } else {
01438 r = rand() % 2;
01439 ambig_flag[idx] += r * 2;
01440 }
01441 }
01442 }
01443
01444
01445
01446 for (int i = 0; i < nx0; i++) {
01447 for (int j = 0; j < ny0; j++) {
01448 idx = i + j * nx0;
01449 pot = (ambig_flag[idx] % 2);
01450 if (confidence[idx] > 51) {
01451 ambig_flag[idx] += pot * 4;
01452 } else {
01453 ambig_flag[idx] += ambig_r[idx] * 4;
01454 }
01455 }
01456 }
01457
01458 }
01459
01460
01461
01462
01463
01464
01465 if (harpflag) {
01466 DRMS_Segment_t *inSeg_bitmap = drms_segment_lookup(inRec, "bitmap");
01467 DRMS_Array_t *inArray_bitmap = drms_segment_read(inSeg_bitmap, DRMS_TYPE_CHAR, &status);
01468 if (status) {
01469 printf("Bitmap reading error \n");
01470 return 1;
01471 }
01472 char *bitmap = (char *) inArray_bitmap->data;
01473 for (int i = 0; i < nx0; i++) {
01474 for (int j = 0; j < ny0; j++) {
01475 idx = i + j * nx0;
01476 if (!bitmap[idx]) {
01477 confidence[idx] = 0;
01478 ambig_flag[idx] = 0;
01479 }
01480 }
01481 }
01482 drms_free_array(inArray_bitmap);
01483 } else {
01484 float x0 = drms_getkey_float(inRec, "CRPIX1", &status) - 1.;
01485 float y0 = drms_getkey_float(inRec, "CRPIX2", &status) - 1.;
01486 float rsun_obs = drms_getkey_float(inRec, "RSUN_OBS", &status);
01487 float dx = drms_getkey_float(inRec, "CDELT1", &status);
01488 float r = rsun_obs / dx;
01489 printf("x0=%f, y0=%f, r=%f, nx0=%d, ny0=%d\n", x0, y0, r, nx0, ny0);
01490 for (int i = 0; i < nx0; i++) {
01491 for (int j = 0; j < ny0; j++) {
01492 idx = i + j * nx0;
01493 if (hypot((i - x0), (j - y0)) > r) {
01494 confidence[idx] = 0;
01495 ambig_flag[idx] = 0;
01496 }
01497 }
01498 }
01499 }
01500
01501
01502
01503 DRMS_Segment_t *inSeg_qual = drms_segment_lookup(inRec, "qual_map");
01504 DRMS_Array_t *inArray_qual = drms_segment_readslice(inSeg_qual, DRMS_TYPE_INT, ll0, ur0, &status);
01505 if (status) {
01506 printf("Qual map reading error \n");
01507 return 1;
01508 }
01509 int *inData_qual = (int *) inArray_qual->data;
01510
01511 DRMS_Segment_t *inSeg_conf = drms_segment_lookup(inRec, "confid_map");
01512 DRMS_Array_t *inArray_conf = drms_segment_readslice(inSeg_conf, DRMS_TYPE_CHAR, ll0, ur0, &status);
01513 if (status) {
01514 printf("Confid map reading error \n");
01515 return 1;
01516 }
01517 char *inData_conf = (char *) inArray_conf->data;
01518
01519 int *qual_map = (int *) calloc(nxny0, sizeof(int));
01520 char *confid_map = (char *) calloc(nxny0, sizeof(char));
01521
01522 for (int i = 0; i < nx0; i++) {
01523 for (int j = 0; j < ny0; j++) {
01524 idx = i + j * nx0;
01525
01526 confid_map[idx] = inData_conf[idx];
01527 qual_map[idx] = inData_qual[idx];
01528 #ifdef QUALMAP
01529 qual_map[idx] = qual_map[idx] | getAmbCode(confidence[idx]);
01530 #endif
01531 }
01532 }
01533
01534
01535
01536 DRMS_Array_t *outArray_flag = drms_array_create(DRMS_TYPE_CHAR, 2, outDims, ambig_flag, &status);
01537 if (status) { printf("Error creating flag array\n"); return 1; }
01538 DRMS_Array_t *outArray_prob = drms_array_create(DRMS_TYPE_CHAR, 2, outDims, confidence, &status);
01539 if (status) { printf("Error creating prob array\n"); return 1; }
01540 DRMS_Array_t *outArray_qual = drms_array_create(DRMS_TYPE_INT, 2, outDims, qual_map, &status);
01541 if (status) { printf("Error creating qual array\n"); return 1; }
01542 DRMS_Array_t *outArray_conf = drms_array_create(DRMS_TYPE_CHAR, 2, outDims, confid_map, &status);
01543 if (status) { printf("Error creating conf array\n"); return 1; }
01544
01545
01546
01547 DRMS_Segment_t *outSeg_flag = drms_segment_lookup(outRec, "disambig");
01548 DRMS_Segment_t *outSeg_prob = drms_segment_lookup(outRec, "conf_disambig");
01549 DRMS_Segment_t *outSeg_qual = drms_segment_lookup(outRec, "info_map");
01550 DRMS_Segment_t *outSeg_conf = drms_segment_lookup(outRec, "confid_map");
01551
01552 for (int i = 0; i < 2; i++) {
01553 outSeg_flag->axis[i] = outArray_flag->axis[i];
01554 outSeg_prob->axis[i] = outArray_prob->axis[i];
01555 outSeg_qual->axis[i] = outArray_qual->axis[i];
01556 outSeg_conf->axis[i] = outArray_conf->axis[i];
01557 }
01558
01559 outArray_flag->parent_segment = outSeg_flag;
01560 outArray_prob->parent_segment = outSeg_prob;
01561 outArray_qual->parent_segment = outSeg_qual;
01562 outArray_conf->parent_segment = outSeg_conf;
01563
01564 status = drms_segment_write(outSeg_flag, outArray_flag, 0);
01565 if (status) { printf("Problem writing flag file\n"); return 1; }
01566 status = drms_segment_write(outSeg_prob, outArray_prob, 0);
01567 if (status) { printf("Problem writing prob file\n"); return 1; }
01568 status = drms_segment_write(outSeg_qual, outArray_qual, 0);
01569 if (status) { printf("Problem writing qual file\n"); return 1; }
01570 status = drms_segment_write(outSeg_conf, outArray_conf, 0);
01571 if (status) { printf("Problem writing conf file\n"); return 1; }
01572
01573
01574
01575 drms_free_array(inArray_qual);
01576 drms_free_array(inArray_conf);
01577
01578 drms_free_array(outArray_flag);
01579 drms_free_array(outArray_prob);
01580 drms_free_array(outArray_qual);
01581 drms_free_array(outArray_conf);
01582
01583
01584
01585 return 0;
01586
01587 }
01588
01589
01590
01591
01592
01593
01594 int writeAuxData(DRMS_Record_t *outRec, DRMS_Record_t *inRec,
01595 float *Bx, float *By, float *Bz, float *probBa, float *dBt, int *bitmap,
01596 int *ll, int *ur)
01597 {
01598
01599 int status = 0;
01600
01601 int nx = ur[0] - ll[0] + 1;
01602 int ny = ur[1] - ll[1] + 1;
01603
01604 int outDims[2] = {nx, ny};
01605
01606 DRMS_Array_t *outArray_Bx = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, Bx, &status);
01607 DRMS_Array_t *outArray_By = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, By, &status);
01608 DRMS_Array_t *outArray_Bz = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, Bz, &status);
01609 DRMS_Array_t *outArray_probBa = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, probBa, &status);
01610 DRMS_Array_t *outArray_dBt = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, dBt, &status);
01611 DRMS_Array_t *outArray_bitmap = drms_array_create(DRMS_TYPE_INT, 2, outDims, bitmap, &status);
01612
01613 DRMS_Segment_t *outSeg_Bx = drms_segment_lookup(outRec, "Bx");
01614 DRMS_Segment_t *outSeg_By = drms_segment_lookup(outRec, "By");
01615 DRMS_Segment_t *outSeg_Bz = drms_segment_lookup(outRec, "Bz");
01616 DRMS_Segment_t *outSeg_probBa = drms_segment_lookup(outRec, "probBa");
01617 DRMS_Segment_t *outSeg_dBt = drms_segment_lookup(outRec, "dBt");
01618 DRMS_Segment_t *outSeg_bitmap = drms_segment_lookup(outRec, "bitmap");
01619 for (int i = 0; i < 2; i++) {
01620 outSeg_Bx->axis[i] = outArray_Bx->axis[i];
01621 outSeg_By->axis[i] = outArray_By->axis[i];
01622 outSeg_Bz->axis[i] = outArray_Bz->axis[i];
01623 outSeg_probBa->axis[i] = outArray_probBa->axis[i];
01624 outSeg_dBt->axis[i] = outArray_dBt->axis[i];
01625 outSeg_bitmap->axis[i] = outArray_bitmap->axis[i];
01626 }
01627 outArray_Bx->parent_segment = outSeg_Bx;
01628 outArray_By->parent_segment = outSeg_By;
01629 outArray_Bz->parent_segment = outSeg_Bz;
01630 outArray_probBa->parent_segment = outSeg_probBa;
01631 outArray_dBt->parent_segment = outSeg_dBt;
01632 outArray_bitmap->parent_segment = outSeg_bitmap;
01633
01634 status = drms_segment_write(outSeg_Bx, outArray_Bx, 0);
01635 status = drms_segment_write(outSeg_By, outArray_By, 0);
01636 status = drms_segment_write(outSeg_Bz, outArray_Bz, 0);
01637 status = drms_segment_write(outSeg_probBa, outArray_probBa, 0);
01638 status = drms_segment_write(outSeg_dBt, outArray_dBt, 0);
01639 status = drms_segment_write(outSeg_bitmap, outArray_bitmap, 0);
01640
01641 drms_free_array(outArray_Bx);
01642 drms_free_array(outArray_By);
01643 drms_free_array(outArray_Bz);
01644 drms_free_array(outArray_probBa);
01645 drms_free_array(outArray_dBt);
01646 drms_free_array(outArray_bitmap);
01647
01648 return 0;
01649
01650 }
01651
01652
01653
01654
01655
01656 void set_Bunit(DRMS_Record_t *outRec)
01657 {
01658
01659 int nBunit = ARRLENGTH(BSegName);
01660 int nSeg = (outRec->segments).num_total;
01661
01662
01663 for (int iSeg = 0; iSeg < nSeg; iSeg++) {
01664 DRMS_Segment_t *outSeg = drms_segment_lookupnum(outRec, iSeg);
01665 char *segName = outSeg->info->name;
01666
01667 int iBunit;
01668 for (iBunit = 0; iBunit < nBunit; iBunit++) {
01669 if (strcmp(segName, BSegName[iBunit]) == 0) break;
01670 }
01671
01672
01673 char bunit_xxx[20];
01674 sprintf(bunit_xxx, "BUNIT_%03d", iSeg);
01675 drms_setkey_string(outRec, bunit_xxx, Bunit[iBunit]);
01676 }
01677
01678 return;
01679
01680 }
01681
01682
01683
01684
01685
01686 void set_stats(DRMS_Record_t *outRec)
01687 {
01688
01689 int status = 0;
01690 int nSeg = (outRec->segments).num_total;
01691
01692
01693 for (int iSeg = 0; iSeg < nSeg; iSeg++) {
01694 DRMS_Segment_t *outSeg = drms_segment_lookupnum(outRec, iSeg);
01695 DRMS_Array_t *outArray = drms_segment_read(outSeg, DRMS_TYPE_FLOAT, &status);
01696 if (!status) {
01697 outSeg->record = outRec;
01698 outSeg->info->segnum = iSeg;
01699
01700 status = set_statistics(outSeg, outArray, 1);
01701
01702 drms_free_array(outArray);
01703 }
01704 }
01705
01706 return;
01707
01708 }
01709