00001
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <time.h>
00005 #include <math.h>
00006 #include <sys/time.h>
00007 #include <sys/resource.h>
00008 #include <limits.h>
00009 #include <sys/types.h>
00010 #include <sys/stat.h>
00011 #include "jsoc_main.h"
00012 #include "astro.h"
00013 #include "drms_dsdsapi.h"
00014 #include "fstats.h"
00015
00016
00017
00018 #include "/home/wso/src/libastro.d/solephem.c"
00019 #define C1 0.0757644
00020 #define C2 92353.9357
00021 #define RADSINDEG (PI/180)
00022
00023
00024 #define kRecSetIn "in"
00025 #define kSeriesOut "out"
00026 #define kSegIn "segin"
00027 #define kSegOut "segout"
00028 #define kNOTSPECIFIED "not specified"
00029
00030 #define EQPOINTS "EQPOINTS"
00031 #define HALFWIN "HALFWIN"
00032 #define LOSFLAG "l"
00033 #define FORCEFLAG "f"
00034 #define DLOGFLAG "d"
00035 #define NOISESIGMA "NOISESIGMA"
00036 #define MAXNOISEADJ "MAXNOISEADJ"
00037
00038
00039 #define MINOUTLIERPTS "MINOUTLIERPTS"
00040
00041
00042
00043 #define DPC_OBSR "DPC_OBSR"
00044 #define FD_Magnetogram_Sum "FD_Magnetogram_Sum"
00045 #define FD_Magnetogram "FD_Magnetogram"
00046 #define HWNWIDTH "HWNWIDTH"
00047
00048
00049 #define kCARSTRCH "CARSTRCH"
00050 #define kDIFROT_A "DIFROT_A"
00051 #define kDIFROT_B "DIFROT_B"
00052 #define kDIFROT_C "DIFROT_C"
00053 #define CR "CR"
00054 #define NBIN "NBIN"
00055 #define QUAL_CHECK (0xfffefb00)
00056
00057 const float kNOISE_EQ = 10.0;
00058 char *module_name = "hmib3compsynoptic";
00059
00060 #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
00061
00062
00063
00064
00065
00066
00067
00068 typedef struct MagCol_struct
00069 {
00070 float dist;
00071 float *datacolBr;
00072 float *datacolBt;
00073 float *datacolBp;
00074 float equivPts;
00075 int ds;
00076
00077 int col;
00078 char valid;
00079 } MagCol_t;
00080
00081 int CalcSynCols(int start,
00082 int end,
00083 int incr,
00084 MagCol_t **smc,
00085 float *synopBr,
00086 float *synopBt,
00087 float *synopBp,
00088 int *wt,
00089 char *ww,
00090 float *epts,
00091
00092 int *len,
00093 float center,
00094 float nEquivPtsReq,
00095 int los,
00096
00097 float sensAdj,
00098 float noiseS,
00099 float nsig,
00100 float maxNoiseAdj,
00101 int minOutPts,
00102 int dlog);
00103
00104 int magStats(float **valBr, int npts, double sum, int outThreshold, double *avg, float *med);
00105 void SortMagCols(MagCol_t *mc, int nelem);
00106 void FreeMagColsData(int start, int end, int incr, int *n, MagCol_t **mc);
00107 void m_sort(MagCol_t *mc, MagCol_t *mc_cpy, int sz);
00108 void frebinbox(float *image_in, float *image_out, int nx, int ny, int nbinx, int nbiny);
00109 TIME CarringtonTime(int crot, double L);
00110
00111 ModuleArgs_t module_args[] =
00112 {
00113 {ARG_STRING, kRecSetIn, "", "Input data records."},
00114 {ARG_STRING, kSeriesOut, "", "Output data series."},
00115 {ARG_STRING, kSegIn, kNOTSPECIFIED, ""},
00116 {ARG_STRING, kSegOut, kNOTSPECIFIED, ""},
00117 {ARG_STRING, "smallsyn", "NOTSPECIFIED", ""},
00118 {ARG_INT, CR, "", "", ""},
00119 {ARG_FLOAT, "nsig", "3.0", "", ""},
00120 {ARG_INT, "MAPMMAX", "1800", "", ""},
00121 {ARG_INT, "SINBDIVS", "720", "", ""},
00122 {ARG_FLOAT, "MAPLGMAX", "90.0", "", ""},
00123 {ARG_FLOAT, "MAPLGMIN", "-90.0", "", ""},
00124 {ARG_FLOAT, EQPOINTS, "20.0", "", ""},
00125 {ARG_FLOAT, HALFWIN, "15.0", "", ""},
00126 {ARG_FLOAT, "center", "0.0", "", ""},
00127 {ARG_INT, "checkqual", "0", "", ""},
00128 {ARG_STRING, "qualmask", "0x0", ""},
00129 {ARG_FLAG, LOSFLAG, "0", "-1", "1"},
00130 {ARG_FLAG, FORCEFLAG, "0", "-1", "1"},
00131 {ARG_FLAG, DLOGFLAG, "0", "-1", "1"},
00132 {ARG_FLOAT, NOISESIGMA, "3.0", "", ""},
00133 {ARG_FLOAT, MAXNOISEADJ, "3.0", "", ""},
00134 {ARG_INT, MINOUTLIERPTS, "4", "", ""},
00135 {ARG_INT, NBIN, "5", "", ""},
00136 {ARG_END}
00137 };
00138
00139 int DoIt(void)
00140 {
00141
00142 int newstat = 0;
00143 int status = DRMS_SUCCESS;
00144 float *synopBr, *synopBt, *synopBp;
00145 int *wt;
00146 char *ww;
00147 float *epts = NULL;
00148
00149 float *psBr, *psBt, *psBp, *pmBr, *pmBt, *pmBp;
00150 char *odir;
00151 struct stat stbuf;
00152 float nsig;
00153 int cr, mapmmax, sinbdivs;
00154 int checkqual;
00155 unsigned int quality, qualvld, qualmask;
00156 float lgmax, lgmin;
00157 float center;
00158 float halfWindow;
00159 int len[2];
00160 int ngood, idx;
00161 int started, ended;
00162 int firstidx, lastidx;
00163 int mapmidcol, synmidcol;
00164 int cols2right, cols2left;
00165 int mrc, mlc, src, slc;
00166 int mapcols, col, row, syncol;
00167 double maprightct, mapleftct;
00168 double synleftct, synrightct;
00169 double synstart;
00170 double synend;
00171 double synstep;
00172 double subSynopStart, subSynopEnd, synRange;
00173 int SyncolStart, SyncolEnd, nsynop;
00174 char tstr[64];
00175 int mag_num = 0;
00176 TIME tstart, tstop, trot, tearth;
00177 TIME tdiff, magtime;
00178 double r,b,l,vr,vn,vw;
00179 int y,m,d;
00180 TIME tmod, delta_T = 0.0;
00181 double bearth;
00182 double carrtime;
00183 char dsname[50400], key[50400], logfn[50400], synfn[50400], wtfn[50400], losFn[50400];
00184 char eptsfn[50400];
00185 int ds, nds, noutRec;
00186 int fsn, lsn;
00187 int i,j;
00188 double statMin, statMax, statMedn, statMean, statSig, statSkew, statKurt;
00189 double smallstatMin, smallstatMax, smallstatMedn, smallstatMean, smallstatSig, smallstatSkew, smallstatKurt;
00190 int statNgood;
00191 int smallstatNgood;
00192 long long calVer;
00193 double eph[30];
00194 char historyofthemodule[2048];
00195
00196 char *cvsinfo = strdup("$Id: hmib3compsynoptic.c,v 1.2 2016/11/16 22:14:13 yliu Exp $");
00197
00198
00199 sprintf(historyofthemodule,"New Module -- March 2014");
00200
00201 struct {
00202 int recno;
00203 double mapct;
00204 double mapCM;
00205 float mapdev;
00206 int ds;
00207 float tmin;
00208 float tmax;
00209 TIME tobs;
00210 } imrec[50400];
00211
00212 int nStackMags;
00213 float nEquivPtsReq;
00214 MagCol_t **sortedMagCol = NULL;
00215
00216 long long modVers = soi_vers_num;
00217 long long imgVers = 0;
00218
00219
00220 int carrStretch = 1;
00221 float diffrotA = DRMS_MISSING_FLOAT;
00222 float diffrotB = DRMS_MISSING_FLOAT;
00223 float diffrotC = DRMS_MISSING_FLOAT;
00224
00225 const char *synopFileName = NULL;
00226 const char *wtFileName = NULL;
00227 const char *eptsFileName = NULL;
00228
00229 int los;
00230 int force;
00231 int dlog;
00232 float noiseS = cmdparams_get_float(&cmdparams, NOISESIGMA, &status);
00233 float maxNoiseAdj = cmdparams_get_float(&cmdparams, MAXNOISEADJ, &status);
00234
00235
00236 int minOutPts = cmdparams_get_int(&cmdparams, MINOUTLIERPTS, &status);
00237
00238
00239
00240 int sensAdjDone = -1;
00241 float sensAdj;
00242
00243 int nxtSyncol = -1;
00244 int calcsynret = 0;
00245 int nbin = 5;
00246 delta_T = sscan_time("1977.01.01_00:00:00_TAI") - sscan_time("1601.01.01_00:00:00_UT");
00247
00248
00249
00250
00251
00252 cr = cmdparams_get_int(&cmdparams, CR, &status);
00253 nsig = cmdparams_get_float(&cmdparams, "nsig", &status);
00254 mapmmax = cmdparams_get_int(&cmdparams, "MAPMMAX", &status);
00255 sinbdivs = cmdparams_get_int(&cmdparams, "SINBDIVS", &status);
00256 checkqual = cmdparams_get_int(&cmdparams, "checkqual", &status);
00257 nbin = cmdparams_get_int(&cmdparams, "NBIN", &status);
00258 qualmask = strtoul(cmdparams_get_str(&cmdparams, "qualmask", &status), (char **) 0, 0);
00259 center = cmdparams_get_float(&cmdparams, "center", &status);
00260 halfWindow = cmdparams_get_float(&cmdparams, HALFWIN, &status);
00261 lgmax = cmdparams_get_float(&cmdparams, "MAPLGMAX", &status);
00262 lgmin = cmdparams_get_float(&cmdparams, "MAPLGMIN", &status);
00263 firstidx = 0;
00264
00265 if (lgmax <= lgmin)
00266 {
00267 printf("MAPLGMAX must be greater than MAPLGMIN\n");
00268 DIE("MAKES_NO_SENSE");
00269 }
00270
00271 nEquivPtsReq = cmdparams_get_float(&cmdparams, EQPOINTS, &status);
00272
00273
00274 len[0] = rint(360.0 / (lgmax - lgmin) * mapmmax);
00275 len[1] = 2 * sinbdivs;
00276
00277 printf("%d, %d\n", len[0], len[1]);
00278
00279 synstep = 360.0 / len[0];
00280 synstart = (cr - 1) * 360.0 + 0.0 * synstep;
00281 synend = cr * 360.0 - 1.0 * synstep;
00282
00283 nStackMags = rint(2 * halfWindow * 0.5);
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 tstart = CarringtonTime(cr, 360.0);
00300 tstop = CarringtonTime(cr, 0.0);
00301 trot = CarringtonTime(cr, 180.0);
00302
00303
00304
00305
00306
00307
00308
00309 los = (cmdparams_isflagset(&cmdparams, LOSFLAG) != 0);
00310 force = (cmdparams_isflagset(&cmdparams, FORCEFLAG) != 0);
00311 dlog = (cmdparams_isflagset(&cmdparams, DLOGFLAG) != 0);
00312
00313 printf("\n######### FIRST PASS #################\n");
00314
00315
00316
00317
00318 if (modVers < 0) modVers = -modVers;
00319 char *inRecQuery, *outRecQuery, *smallRecQuery;
00320 inRecQuery = (char *)cmdparams_get_str(&cmdparams, kRecSetIn, &status);
00321 outRecQuery = (char *)cmdparams_get_str(&cmdparams, kSeriesOut, &status);
00322 smallRecQuery = (char *)cmdparams_get_str(&cmdparams, "smallsyn", &status);
00323
00324 DRMS_RecordSet_t *inRD, *outRD, *smallRD;
00325 DRMS_Record_t *outRec, *smallRec;
00326
00327 inRD = drms_open_records(drms_env, inRecQuery, &status);
00328 if (status || inRD->n == 0)
00329 DIE("No input data found");
00330 nds = inRD->n;
00331 idx = 0;
00332 for (ds=0; ds < nds; ds++)
00333 {
00334 double clong;
00335 double cmLong;
00336 double mapct = 0.0;
00337 double mapctnew;
00338 double mapCM = 0.0;
00339 double mapCMnew;
00340 double remapLgmax = 0.0;
00341 double remapLgmin = 0.0;
00342 int orot;
00343 char t_obs[64];
00344 char *imgVersStr = NULL;
00345
00346 int csKey = 0;
00347 float csCoeffKey;
00348 int sensAdjKey;
00349 TIME trec;
00350 char trecstr[100];
00351 DRMS_Record_t *inRec;
00352 inRec = inRD->records[ds];
00353
00354 calVer = drms_getkey_longlong(inRec, "CALVER64", &status);
00355 trec = drms_getkey_time(inRec, "T_REC", &status);
00356 sprint_time(trecstr, trec, "TAI", 0);
00357
00358
00359
00360 quality = drms_getkey_int(inRec, "QUALITY", &status);
00361
00362 if (quality & QUAL_CHECK)
00363 {
00364 printf("SKIP: Bad QUALITY = 0x%08x; T_REC=%s, rejected iRec = %d\n", quality, trecstr, ds);
00365 continue;
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458 csKey = drms_getkey_int(inRec, kCARSTRCH, &status);
00459 csKey = (drms_ismissing_int(csKey)) ? 0 : csKey;
00460
00461 if (idx == 0)
00462 {
00463 carrStretch = csKey;
00464 }
00465 else
00466 {
00467 if (csKey != carrStretch)
00468 {
00469 printf(" Attempt to mix carr-streched and non-carr-stretched images.\n");
00470 printf(" Rejecting ds=%d.\n", ds);
00471 continue;
00472 }
00473 }
00474
00475 if (carrStretch > 0)
00476 {
00477 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_A, &status);
00478 if (idx == 0)
00479 {
00480 diffrotA = (float)csCoeffKey;
00481 }
00482 else
00483 {
00484 if (fabsf(csCoeffKey - diffrotA) > 0.001)
00485 {
00486 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00487 printf(" Rejecting ds=%d.\n", ds);
00488 continue;
00489 }
00490 }
00491
00492 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_B, &status);
00493 if (idx == 0)
00494 {
00495 diffrotB = (float)csCoeffKey;
00496 }
00497 else
00498 {
00499 if (fabsf(csCoeffKey - diffrotB) > 0.001)
00500 {
00501 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00502 printf(" Rejecting ds=%d.\n", ds);
00503 continue;
00504 }
00505 }
00506
00507 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_C, &status);
00508 if (idx == 0)
00509 {
00510 diffrotC = (float)csCoeffKey;
00511 }
00512 else
00513 {
00514 if (fabsf(csCoeffKey - diffrotC) > 0.001)
00515 {
00516 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00517 printf(" Rejecting ds=%d.\n", ds);
00518 continue;
00519 }
00520 }
00521 }
00522
00523 clong = drms_getkey_double(inRec, "CRVAL1", &status);
00524 cmLong = drms_getkey_double(inRec, "CRLN_OBS", &status);
00525 orot = drms_getkey_int(inRec, "CAR_ROT", &status);
00526 if (drms_ismissing_float(clong) || drms_ismissing_int(orot))
00527 {
00528 printf(" Bad MAP_L0 or OBS_CR in header; skipped");
00529 continue;
00530 }
00531
00532 mapctnew = (double)(orot) * 360.0 - clong - center;
00533 mapCMnew = (double)(orot) * 360.0 - cmLong - center;
00534 if (mapCMnew < mapCM)
00535 {
00536 printf(" Source image (ds=%d) has a smaller CT than previous img; skipped.\n", ds);
00537 continue;
00538 }
00539 else
00540 {
00541 mapct = mapctnew;
00542 mapCM = mapCMnew;
00543 }
00544
00545 remapLgmax = drms_getkey_double(inRec, "MAPLGMAX", &status);
00546 remapLgmin = drms_getkey_double(inRec, "MAPLGMIN", &status);
00547 imrec[idx].mapdev = (remapLgmax - remapLgmin) / 2.0;
00548 imrec[idx].recno = drms_getkey_int(inRec, "I_DREC", &status);
00549 imrec[idx].mapct = mapct;
00550 imrec[idx].mapCM = mapCM;
00551 imrec[idx].ds = ds;
00552 strcpy(t_obs, drms_getkey_string(inRec, "T_REC", &status));
00553 imrec[idx].tobs = sscan_time(t_obs);
00554 imrec[idx].tmin = MAX(mapCM - halfWindow, mapct + center - imrec[idx].mapdev);
00555 imrec[idx].tmax = MIN(mapCM + halfWindow, mapct + center + imrec[idx].mapdev);
00556 ++idx;
00557
00558 }
00559 ngood = idx;
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 printf("\n######### SECOND PASS #################\n");
00575
00576 if (!(wt = (int *) calloc(len[0],4)))
00577 DIE("MALLOC_FAILED");
00578 if (!(ww = (char *) malloc(len[0]*len[1]*2)))
00579 DIE("MALLOC_FAILED");
00580 if (!(epts = (float *) malloc(len[0] * len[1] * sizeof(float))))
00581 DIE("MALLOC_FAILED");
00582 if (!(synopBr = (float *) malloc(len[0]*len[1]*4)))
00583 DIE("MALLOC_FAILED");
00584 if (!(synopBt = (float *) malloc(len[0]*len[1]*4)))
00585 DIE("MALLOC_FAILED");
00586 if (!(synopBp = (float *) malloc(len[0]*len[1]*4)))
00587 DIE("MALLOC_FAILED");
00588
00589
00590 sortedMagCol = (MagCol_t **)malloc(sizeof(MagCol_t *) * len[0]);
00591 if (!sortedMagCol)
00592 DIE("MALLOC_FAILED");
00593 memset(sortedMagCol, 0, sizeof(MagCol_t *) * len[0]);
00594
00595
00596 started = 0;
00597 ended = 0;
00598 nxtSyncol = len[0] - 1;
00599 synRange = 60.0 ;
00600
00601
00602 nsynop = rint(360.0/synRange);
00603
00604
00605
00606
00607
00608 for (ds = 0; ds < nsynop; ds++)
00609 {
00610 subSynopStart = synstart + ds * synRange;
00611 subSynopEnd = synstart + (ds + 1) * synRange - synstep;
00612 SyncolStart = rint((synend - subSynopStart) / synstep);
00613 SyncolEnd = rint((synend - subSynopEnd) / synstep);
00614 printf("ds=%d\n", ds);
00615
00616 for (idx = 0; idx < ngood; idx++)
00617 {
00618 int recno;
00619 int col;
00620 float magtype = 0.0;
00621 char *mType = NULL;
00622 float equivPts = 0.0;
00623 char t_mag[64];
00624 TIME tmag;
00625 DRMS_Record_t *inRec;
00626 DRMS_Segment_t *inSeg = NULL;
00627 DRMS_Array_t *inArrayBr, *inArrayBt, *inArrayBp;
00628 inRec = inRD->records[imrec[idx].ds];
00629
00630 if (imrec[idx].tmax <= subSynopStart || imrec[idx].tmin >= subSynopEnd) continue;
00631
00632
00633
00634
00635 maprightct = MAX(subSynopStart, imrec[idx].tmin);
00636 mapleftct = MIN(subSynopEnd, imrec[idx].tmax);
00637 mapcols = drms_getkey_int(inRec, "MAPMMAX", &status) + 1;
00638 mapmidcol = rint((mapcols - 1.0) / 2 + center / synstep);
00639 cols2right = rint((imrec[idx].mapct - maprightct) / synstep);
00640 cols2left = rint((imrec[idx].mapct - mapleftct) / synstep);
00641 synmidcol = rint((synend - imrec[idx].mapct) / synstep);
00642
00643
00644
00645
00646
00647 mrc = mapmidcol + cols2right;
00648 mlc = mapmidcol + cols2left;
00649
00650
00651 src = synmidcol + cols2right;
00652 slc = synmidcol + cols2left;
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665 equivPts = 1.0;
00666
00667
00668
00669 inSeg = drms_segment_lookup(inRec, "Br");
00670 inArrayBr = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00671
00672 if (status)
00673 {
00674 printf(" No data file found, status=%d\n", status);
00675 drms_free_array(inArrayBr);
00676 continue;
00677 }
00678
00679 inSeg = drms_segment_lookup(inRec, "Bt");
00680 inArrayBt = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00681
00682 if (status)
00683 {
00684 printf(" No data file found, status=%d\n", status);
00685 drms_free_array(inArrayBr);
00686 drms_free_array(inArrayBt);
00687 continue;
00688 }
00689
00690 inSeg = drms_segment_lookup(inRec, "Bp");
00691 inArrayBp = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00692
00693 if (status)
00694 {
00695 printf(" No data file found, status=%d\n", status);
00696 drms_free_array(inArrayBr);
00697 drms_free_array(inArrayBt);
00698 drms_free_array(inArrayBp);
00699 continue;
00700 }
00701
00702 for (col = mlc; col <= mrc; ++col)
00703 {
00704 MagCol_t mMagCol;
00705
00706 mMagCol.dist = ((col - mapmidcol) * synstep + imrec[idx].mapct) - imrec[idx].mapCM;
00707 mMagCol.datacolBr = (float *)malloc(sizeof(float) * len[1]);
00708 mMagCol.datacolBt = (float *)malloc(sizeof(float) * len[1]);
00709 mMagCol.datacolBp = (float *)malloc(sizeof(float) * len[1]);
00710 mMagCol.equivPts = equivPts;
00711 mMagCol.ds = imrec[idx].ds;
00712 mMagCol.col = col;
00713 mMagCol.valid = 1;
00714
00715 pmBr = (float *)inArrayBr->data + col;
00716 pmBt = (float *)inArrayBt->data + col;
00717 pmBp = (float *)inArrayBp->data + col;
00718
00719 psBr = mMagCol.datacolBr;
00720 psBt = mMagCol.datacolBt;
00721 psBp = mMagCol.datacolBp;
00722
00723 for (row = 0; row < len[1]; ++row)
00724 {
00725 *psBr = *pmBr;
00726 pmBr += mapcols;
00727 psBr++;
00728 *psBt = *pmBt;
00729 pmBt += mapcols;
00730 psBt++;
00731 *psBp = *pmBp;
00732 pmBp += mapcols;
00733 psBp++;
00734 }
00735
00736 syncol = col + slc - mlc;
00737
00738 if (!sortedMagCol[syncol])
00739 {
00740 sortedMagCol[syncol] = (MagCol_t *)malloc(sizeof(MagCol_t) * nStackMags);
00741 if (!sortedMagCol[syncol]) DIE("MALLOC_FAILED");
00742 memset(sortedMagCol[syncol], 0, sizeof(MagCol_t) * nStackMags);
00743 }
00744
00745 if (wt[syncol] > 0 && (wt[syncol] % nStackMags == 0))
00746 {
00747 int frames = 1 + (wt[syncol] / nStackMags);
00748 MagCol_t *mc = (MagCol_t *)malloc(sizeof(MagCol_t) * frames * nStackMags);
00749 memcpy(mc,
00750 sortedMagCol[syncol],
00751 sizeof(MagCol_t) * (frames - 1) * nStackMags);
00752 free(sortedMagCol[syncol]);
00753 sortedMagCol[syncol] = mc;
00754 }
00755
00756 sortedMagCol[syncol][wt[syncol]] = mMagCol;
00757 wt[syncol]++;
00758
00759 }
00760
00761 drms_free_array(inArrayBr);
00762 drms_free_array(inArrayBt);
00763 drms_free_array(inArrayBp);
00764 }
00765
00766 calcsynret = CalcSynCols(SyncolStart,
00767 SyncolEnd,
00768 -1,
00769 sortedMagCol,
00770 synopBr,
00771 synopBt,
00772 synopBp,
00773 wt,
00774 ww,
00775 epts,
00776
00777 len,
00778 center,
00779 nEquivPtsReq,
00780 los,
00781
00782 sensAdj,
00783 noiseS,
00784 nsig,
00785 maxNoiseAdj,
00786 minOutPts,
00787 dlog);
00788
00789 printf("ds=%d\n", ds);
00790 FreeMagColsData(SyncolStart, SyncolEnd, -1, wt, sortedMagCol);
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804 if (calcsynret) DIE(" Cannot be finished up!");
00805 }
00806
00807 if (!ended) lastidx = ngood-1;
00808 magtime = imrec[(int)(ngood/2)].tobs;
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824 float *smallSynopBr = NULL, *smallSynopBt = NULL, *smallSynopBp = NULL, *smallEpts = NULL;
00825 int smallDims[2], xout, yout;
00826
00827 xout = len[0]/nbin; yout = len[1]/(nbin-1);
00828 if (!(smallSynopBr = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00829 if (!(smallSynopBt = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00830 if (!(smallSynopBp = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00831 if (!(smallEpts = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00832 smallDims[0] = xout; smallDims[1] = yout;
00833
00834
00835
00836
00837
00838 frebinbox(synopBr, smallSynopBr, len[0], len[1], nbin, nbin-1);
00839 frebinbox(synopBt, smallSynopBt, len[0], len[1], nbin, nbin-1);
00840 frebinbox(synopBp, smallSynopBp, len[0], len[1], nbin, nbin-1);
00841 frebinbox(epts, smallEpts, len[0], len[1], nbin, nbin-1);
00842
00843
00844
00845 outRD = drms_create_records(drms_env, 1, outRecQuery, DRMS_PERMANENT, &status);
00846 if (status)
00847 DIE("Output record not created");
00848 smallRD = drms_create_records(drms_env, 1, smallRecQuery, DRMS_PERMANENT, &status);
00849 if (status)
00850 DIE("Output record not created");
00851
00852 DRMS_Segment_t *outSeg, *smallSeg;
00853 DRMS_Array_t *outArray, *smallArray;
00854 int outDims[2] = {len[0],len[1]};
00855 outRec = outRD->records[0];
00856 smallRec = smallRD->records[0];
00857
00858
00859
00860 if (fstats(len[0]*len[1], synopBr, &statMin, &statMax, &statMedn, &statMean, &statSig,
00861 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00862
00863 i = len[0]*len[1];
00864 drms_setkey_int(outRec, "TOTVALS", i);
00865 drms_setkey_int(outRec, "BRVALS", statNgood);
00866 i = len[0]*len[1]-statNgood;
00867 drms_setkey_int(outRec, "BRMISSVA", i);
00868 drms_setkey_double(outRec, "BRMIN", statMin);
00869 drms_setkey_double(outRec, "BRMAX", statMax);
00870 drms_setkey_double(outRec, "BRMEDN", statMedn);
00871 drms_setkey_double(outRec, "BRMEAN", statMean);
00872 drms_setkey_double(outRec, "BRRMS", statSig);
00873 drms_setkey_double(outRec, "BRSKEW", statSkew);
00874 drms_setkey_double(outRec, "BRKURT", statKurt);
00875
00876 if (fstats(len[0]*len[1], synopBt, &statMin, &statMax, &statMedn, &statMean, &statSig,
00877 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00878
00879 drms_setkey_int(outRec, "BTVALS", statNgood);
00880 i = len[0]*len[1]-statNgood;
00881 drms_setkey_int(outRec, "BTMISSVA", i);
00882 drms_setkey_double(outRec, "BTMIN", statMin);
00883 drms_setkey_double(outRec, "BTMAX", statMax);
00884 drms_setkey_double(outRec, "BTMEDN", statMedn);
00885 drms_setkey_double(outRec, "BTMEAN", statMean);
00886 drms_setkey_double(outRec, "BTRMS", statSig);
00887 drms_setkey_double(outRec, "BTSKEW", statSkew);
00888 drms_setkey_double(outRec, "BTKURT", statKurt);
00889
00890 if (fstats(len[0]*len[1], synopBp, &statMin, &statMax, &statMedn, &statMean, &statSig,
00891 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00892
00893 drms_setkey_int(outRec, "BPVALS", statNgood);
00894 i = len[0]*len[1]-statNgood;
00895 drms_setkey_int(outRec, "BPMISSVA", i);
00896 drms_setkey_double(outRec, "BPMIN", statMin);
00897 drms_setkey_double(outRec, "BPMAX", statMax);
00898 drms_setkey_double(outRec, "BPMEDN", statMedn);
00899 drms_setkey_double(outRec, "BPMEAN", statMean);
00900 drms_setkey_double(outRec, "BPRMS", statSig);
00901 drms_setkey_double(outRec, "BPSKEW", statSkew);
00902 drms_setkey_double(outRec, "BPKURT", statKurt);
00903
00904 if (fstats(xout*yout, smallSynopBr, &smallstatMin, &smallstatMax, &smallstatMedn, &smallstatMean, &smallstatSig,
00905 &smallstatSkew, &smallstatKurt, &smallstatNgood)) printf("\n Statistics computation failed\n");
00906
00907 i = xout*yout;
00908 drms_setkey_int(smallRec, "TOTVALS", i);
00909 drms_setkey_int(smallRec, "BRVALS", smallstatNgood);
00910 i = xout*yout-smallstatNgood;
00911 drms_setkey_int(smallRec, "BRMISSVA", i);
00912 drms_setkey_double(smallRec, "BRMIN", smallstatMin);
00913 drms_setkey_double(smallRec, "BRMAX", smallstatMax);
00914 drms_setkey_double(smallRec, "BRMEDN", smallstatMedn);
00915 drms_setkey_double(smallRec, "BRMEAN", smallstatMean);
00916 drms_setkey_double(smallRec, "BRRMS", smallstatSig);
00917 drms_setkey_double(smallRec, "BRSKEW", smallstatSkew);
00918 drms_setkey_double(smallRec, "BRKURT", smallstatKurt);
00919
00920 if (fstats(xout*yout, smallSynopBt, &smallstatMin, &smallstatMax, &smallstatMedn, &smallstatMean, &smallstatSig,
00921 &smallstatSkew, &smallstatKurt, &smallstatNgood)) printf("\n Statistics computation failed\n");
00922
00923 drms_setkey_int(smallRec, "BTVALS", smallstatNgood);
00924 i = xout*yout-smallstatNgood;
00925 drms_setkey_int(smallRec, "BTMISSVA", i);
00926 drms_setkey_double(smallRec, "BTMIN", smallstatMin);
00927 drms_setkey_double(smallRec, "BTMAX", smallstatMax);
00928 drms_setkey_double(smallRec, "BTMEDN", smallstatMedn);
00929 drms_setkey_double(smallRec, "BTMEAN", smallstatMean);
00930 drms_setkey_double(smallRec, "BTRMS", smallstatSig);
00931 drms_setkey_double(smallRec, "BTSKEW", smallstatSkew);
00932 drms_setkey_double(smallRec, "BTKURT", smallstatKurt);
00933
00934 if (fstats(xout*yout, smallSynopBp, &smallstatMin, &smallstatMax, &smallstatMedn, &smallstatMean, &smallstatSig,
00935 &smallstatSkew, &smallstatKurt, &smallstatNgood)) printf("\n Statistics computation failed\n");
00936
00937 drms_setkey_int(smallRec, "BPVALS", smallstatNgood);
00938 i = xout*yout-smallstatNgood;
00939 drms_setkey_int(smallRec, "BPMISSVA", i);
00940 drms_setkey_double(smallRec, "BPMIN", smallstatMin);
00941 drms_setkey_double(smallRec, "BPMAX", smallstatMax);
00942 drms_setkey_double(smallRec, "BPMEDN", smallstatMedn);
00943 drms_setkey_double(smallRec, "BPMEAN", smallstatMean);
00944 drms_setkey_double(smallRec, "BPRMS", smallstatSig);
00945 drms_setkey_double(smallRec, "BPSKEW", smallstatSkew);
00946 drms_setkey_double(smallRec, "BPKURT", smallstatKurt);
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965 outSeg = drms_segment_lookup(outRec, "Br");
00966 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, synopBr, &status);
00967 status = drms_segment_write(outSeg, outArray, 0);
00968 if (status)
00969 DIE("problem writing file");
00970 drms_free_array(outArray);
00971
00972
00973 outSeg = drms_segment_lookup(outRec, "Bt");
00974 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, synopBt, &status);
00975 status = drms_segment_write(outSeg, outArray, 0);
00976 if (status)
00977 DIE("problem writing file");
00978 drms_free_array(outArray);
00979
00980
00981 outSeg = drms_segment_lookup(outRec, "Bp");
00982 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, synopBp, &status);
00983 status = drms_segment_write(outSeg, outArray, 0);
00984 if (status)
00985 DIE("problem writing file");
00986 drms_free_array(outArray);
00987
00988
00989 outSeg = drms_segment_lookupnum(outRec, 3);
00990 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, epts, &status);
00991 status = drms_segment_write(outSeg, outArray, 0);
00992 if (status)
00993 DIE("problem writing file");
00994 drms_free_array(outArray);
00995
00996
00997 smallSeg = drms_segment_lookupnum(smallRec, 0);
00998 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallSynopBr, &status);
00999 status = drms_segment_write(smallSeg, smallArray, 0);
01000 if (status)
01001 DIE("problem writing file");
01002 drms_free_array(smallArray);
01003
01004
01005 smallSeg = drms_segment_lookupnum(smallRec, 1);
01006 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallSynopBt, &status);
01007 status = drms_segment_write(smallSeg, smallArray, 0);
01008 if (status)
01009 DIE("problem writing file");
01010 drms_free_array(smallArray);
01011
01012
01013 smallSeg = drms_segment_lookupnum(smallRec, 2);
01014 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallSynopBp, &status);
01015 status = drms_segment_write(smallSeg, smallArray, 0);
01016 if (status)
01017 DIE("problem writing file");
01018 drms_free_array(smallArray);
01019
01020
01021 smallSeg = drms_segment_lookupnum(smallRec, 3);
01022 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallEpts, &status);
01023 status = drms_segment_write(smallSeg, smallArray, 0);
01024 if (status)
01025 DIE("problem writing file");
01026 drms_free_array(smallArray);
01027
01028
01029
01030
01031
01032 sprint_ut(tstr, CURRENT_SYSTEM_TIME);
01033 sscanf(tstr, "%d.%d.%d", &y, &m, &d);
01034 sprintf(tstr, "%04d-%02d-%02d", y, m, d);
01035 drms_setkey_string(outRec, "DATE", tstr);
01036
01037
01038
01039
01040 drms_setkey_string(outRec, "HISTORY", historyofthemodule);
01041 drms_setkey_string(outRec, "BLD_VERS", jsoc_version);
01042 status = drms_setkey_string(outRec, "CODEVER", cvsinfo);
01043 drms_setkey_string(outRec, "CTYPE1", "CRLN-CEA");
01044 drms_setkey_string(outRec, "CTYPE2", "CRLT-CEA");
01045
01046 i=len[0]-(len[0]/2+1.0)+1.0;
01047
01048
01049
01050
01051
01052 drms_setkey_double(outRec, "CRPIX1", i);
01053
01054 l=(len[1]+1.0)/2;
01055 drms_setkey_double(outRec, "CRPIX2", l);
01056
01057
01058 carrtime = (cr-1)*360.0 + 180.0;
01059 drms_setkey_double(outRec, "CRVAL1", carrtime);
01060 i=0.0;
01061 drms_setkey_double(outRec, "CRVAL2", i);
01062 l=-360.0/len[0];
01063 drms_setkey_double(outRec, "CDELT1", l);
01064 l=2.0/len[1];
01065 drms_setkey_double(outRec, "CDELT2", l);
01066 drms_setkey_string(outRec, "CUNIT1", "degree");
01067 drms_setkey_string(outRec, "CUNIT2", "Sine Latitude");
01068 drms_setkey_string(outRec, "WCSNAME", "Carrington Heliographic");
01069
01070
01071 sprint_at(tstr, trot - delta_T);
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093 drms_setkey_string(outRec, "T_OBS", tstr);
01094 drms_setkey_string(outRec, "T_ROT", tstr);
01095 sprint_at(tstr, tstart - delta_T);
01096 drms_setkey_string(outRec, "T_START", tstr);
01097 sprint_at(tstr, tstop - delta_T);
01098 drms_setkey_string(outRec, "T_STOP", tstr);
01099
01100
01101 drms_setkey_int(outRec, "CAR_ROT", cr);
01102 drms_setkey_double(outRec, "CARRTIME", carrtime);
01103 solephem(trot, eph);
01104
01105 drms_setkey_double(outRec, "B0_ROT", eph[9]/RADSINDEG);
01106 solephem(tstart, eph);
01107
01108 drms_setkey_double(outRec, "B0_FRST", eph[9]/RADSINDEG);
01109 solephem(tstop, eph);
01110
01111 drms_setkey_double(outRec, "B0_LAST", eph[9]/RADSINDEG);
01112
01113 l=(cr-1)*360.0;
01114 drms_setkey_double(outRec, "LON_FRST", l);
01115 l=cr*360.0-360.0/len[0];
01116 drms_setkey_double(outRec, "LON_LAST", l);
01117 l=-360.0/len[0];
01118 drms_setkey_double(outRec, "LON_STEP", l);
01119 l=center;
01120 drms_setkey_double(outRec, "W_OFFSET", l);
01121 drms_setkey_string(outRec, "W_WEIGHT", "Even");
01122 i=lastidx-firstidx+1;
01123 drms_setkey_int(outRec, "IMG_NUM", i);
01124 sprint_at(tstr, imrec[firstidx].tobs);
01125 drms_setkey_string(outRec, "IMG_FRST", tstr);
01126 sprint_at(tstr, imrec[lastidx].tobs);
01127 drms_setkey_string(outRec, "IMG_LAST", tstr);
01128 sprint_at(tstr, magtime);
01129 drms_setkey_string(outRec, "IMG_ROT", tstr);
01130 drms_setkey_float(outRec, HWNWIDTH, halfWindow);
01131 drms_setkey_float(outRec, EQPOINTS, nEquivPtsReq);
01132 drms_setkey_float(outRec, "NSIGMA", nsig);
01133 drms_setkey_longlong(outRec, "CALVER64", calVer);
01134
01135
01136
01137
01138 if (carrStretch > 0)
01139 {
01140 drms_setkey_int(outRec, kCARSTRCH, carrStretch);
01141 drms_setkey_float(outRec, kDIFROT_A, diffrotA);
01142 drms_setkey_float(outRec, kDIFROT_B, diffrotB);
01143 drms_setkey_float(outRec, kDIFROT_C, diffrotC);
01144 }
01145
01146
01147
01148
01149
01150
01151 sprint_ut(tstr, CURRENT_SYSTEM_TIME);
01152 sscanf(tstr, "%d.%d.%d", &y, &m, &d);
01153 sprintf(tstr, "%04d-%02d-%02d", y, m, d);
01154 drms_setkey_string(smallRec, "DATE", tstr);
01155
01156
01157
01158
01159 drms_setkey_string(smallRec, "HISTORY", historyofthemodule);
01160 drms_setkey_string(smallRec, "BLD_VERS", jsoc_version);
01161 status = drms_setkey_string(smallRec, "CODEVER", cvsinfo);
01162 drms_setkey_string(smallRec, "CTYPE1", "CRLN-CEA");
01163 drms_setkey_string(smallRec, "CTYPE2", "CRLT-CEA");
01164
01165 double tmp;
01166 tmp=xout - ((180.0-((nbin+1)/2.0-1.0)*(360.0/len[0]))/(360.0/xout)+1.0) + 1.0;
01167
01168
01169
01170 drms_setkey_double(smallRec, "CRPIX1", tmp);
01171 tmp=(yout+1.0)/2;
01172 drms_setkey_double(smallRec, "CRPIX2", tmp);
01173
01174 carrtime = (cr-1)*360.0 + 180.0;
01175 drms_setkey_double(smallRec, "CRVAL1", carrtime);
01176 tmp=0.0;
01177 drms_setkey_double(smallRec, "CRVAL2", tmp);
01178 l=-360.0/xout;
01179 drms_setkey_double(smallRec, "CDELT1", l);
01180 l=2.0/yout;
01181 drms_setkey_double(smallRec, "CDELT2", l);
01182 drms_setkey_string(smallRec, "CUNIT1", "degree");
01183 drms_setkey_string(smallRec, "CUNIT2", "Sine Latitude");
01184 drms_setkey_string(smallRec, "WCSNAME", "Carrington Heliographic");
01185
01186
01187 sprint_at(tstr, trot - delta_T);
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209 drms_setkey_string(smallRec, "T_OBS", tstr);
01210 drms_setkey_string(smallRec, "T_ROT", tstr);
01211 sprint_at(tstr, tstart - delta_T);
01212 drms_setkey_string(smallRec, "T_START", tstr);
01213 sprint_at(tstr, tstop - delta_T);
01214 drms_setkey_string(smallRec, "T_STOP", tstr);
01215
01216
01217 drms_setkey_int(smallRec, "CAR_ROT", cr);
01218 drms_setkey_double(smallRec, "CARRTIME", carrtime);
01219 solephem(trot, eph);
01220
01221 drms_setkey_double(smallRec, "B0_ROT", eph[9]/RADSINDEG);
01222
01223 solephem(tstart, eph);
01224 drms_setkey_double(smallRec, "B0_FRST", eph[9]/RADSINDEG);
01225
01226 solephem(tstop, eph);
01227 drms_setkey_double(smallRec, "B0_LAST", eph[9]/RADSINDEG);
01228
01229 l=(cr-1)*360.0+((nbin+1)/2.0-1.0)*(360.0/len[0]);
01230 drms_setkey_double(smallRec, "LON_FRST", l);
01231 l=cr*360.0-360.0/len[0]-((nbin+1)/2.0-1.0)*(360.0/len[0]);
01232 drms_setkey_double(smallRec, "LON_LAST", l);
01233 l=-360.0/xout;
01234 drms_setkey_double(smallRec, "LON_STEP", l);
01235 l=center;
01236 drms_setkey_double(smallRec, "W_OFFSET", l);
01237 drms_setkey_string(smallRec, "W_WEIGHT", "Even");
01238 i=lastidx-firstidx+1;
01239 drms_setkey_int(smallRec, "IMG_NUM", i);
01240 sprint_at(tstr, imrec[firstidx].tobs);
01241 drms_setkey_string(smallRec, "IMG_FRST", tstr);
01242 sprint_at(tstr, imrec[lastidx].tobs);
01243 drms_setkey_string(smallRec, "IMG_LAST", tstr);
01244 sprint_at(tstr, magtime);
01245 drms_setkey_string(smallRec, "IMG_ROT", tstr);
01246 drms_setkey_float(smallRec, HWNWIDTH, halfWindow);
01247 drms_setkey_float(smallRec, EQPOINTS, nEquivPtsReq);
01248 drms_setkey_float(smallRec, "NSIGMA", nsig);
01249 drms_setkey_longlong(smallRec, "CALVER64", calVer);
01250
01251
01252 if (carrStretch > 0)
01253 {
01254 drms_setkey_int(smallRec, kCARSTRCH, carrStretch);
01255 drms_setkey_float(smallRec, kDIFROT_A, diffrotA);
01256 drms_setkey_float(smallRec, kDIFROT_B, diffrotB);
01257 drms_setkey_float(smallRec, kDIFROT_C, diffrotC);
01258 }
01259
01260 if (sortedMagCol)
01261 {
01262 for (i = 0; i < len[0]; i++)
01263 {
01264 if (sortedMagCol[i])
01265 {
01266 free(sortedMagCol[i]);
01267 }
01268 }
01269 free(sortedMagCol);
01270 }
01271
01272 drms_close_records(inRD, DRMS_FREE_RECORD);
01273 drms_close_records(outRD, DRMS_INSERT_RECORD);
01274 drms_close_records(smallRD, DRMS_INSERT_RECORD);
01275 return 0;
01276 }
01277
01278 int CalcSynCols(int start,
01279 int end,
01280 int incr,
01281 MagCol_t **smc,
01282 float *synopBr,
01283 float *synopBt,
01284 float *synopBp,
01285 int *wt,
01286 char *ww,
01287 float *epts,
01288
01289 int *len,
01290 float center,
01291 float nEquivPtsReq,
01292 int los,
01293
01294 float sensAdj,
01295 float noiseS,
01296 float nsig,
01297 float maxNoiseAdj,
01298 int minOutPts,
01299 int dlog)
01300 {
01301 float midSynRow;
01302 float cosrho;
01303
01304
01305
01306
01307
01308
01309 char *rejDescFormat;
01310 int col;
01311 int row;
01312 int i;
01313 int j;
01314 static int nrej = 0;
01315 int marked;
01316 float nEquivPts;
01317
01318 midSynRow = (float)(len[1] - 1) / 2.0;
01319
01320 rejDescFormat = " magnetogram: ds=%d, sn=%d, magcol=%d, magrow=%d\n";
01321 if (minOutPts < 2) minOutPts = 2;
01322
01323
01324 for (col = start; incr > 0 ? col <= end : col >= end; col += incr)
01325 {
01326 float *valBr = (float *)malloc(sizeof(float) * wt[col]);
01327 float *valBt = (float *)malloc(sizeof(float) * wt[col]);
01328 float *valBp = (float *)malloc(sizeof(float) * wt[col]);
01329 float *ept = (float *)malloc(sizeof(float) * wt[col]);
01330 MagCol_t *pcols = NULL;
01331 marked = 0;
01332
01333 if (dlog)
01334 {
01335 pcols = (MagCol_t *)malloc(sizeof(MagCol_t) * wt[col]);
01336 memset(pcols, 0, sizeof(MagCol_t) * wt[col]);
01337 }
01338
01339 if (!valBr || !ept || !valBt || !valBp) printf("MALLOC FAILED");
01340
01341
01342 SortMagCols(smc[col], wt[col]);
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360 for (row = 0; row < len[1]; ++row)
01361 {
01362 float sinLat = (row - midSynRow) / midSynRow;
01363 float cosLat = sqrt(1 - sinLat * sinLat);
01364 float sum;
01365 int npts;
01366 float sumfinalBr = 0.0, sumfinalBt = 0.0, sumfinalBp = 0.0;
01367 int nptsfinal = 0;
01368 int Maxnepts = nEquivPtsReq + 10;
01369
01370
01371
01372 sum = 0.0;
01373 npts = 0;
01374 j = 0;
01375 nEquivPts = 0.0;
01376
01377
01378
01379
01380
01381 for (i = 0; i < wt[col] && nEquivPts < Maxnepts; i++)
01382 {
01383 float magValBr = *(smc[col][i].datacolBr + row);
01384 float magValBt = *(smc[col][i].datacolBt + row);
01385 float magValBp = *(smc[col][i].datacolBp + row);
01386 if (!drms_ismissing_float(magValBr))
01387 {
01388 nEquivPts += smc[col][i].equivPts;
01389 sum += magValBr;
01390 valBr[j] = magValBr;
01391 valBt[j] = magValBt;
01392 valBp[j] = magValBp;
01393 ept[j] = smc[col][i].equivPts;
01394
01395 if (dlog && pcols) pcols[j] = smc[col][i];
01396
01397 j++;
01398 npts++;
01399 }
01400 }
01401
01402
01403 if (npts > 1 && nsig > 0.0) {
01404 double avg = 0.0;
01405 double ssqr = 0.0;
01406 double sig;
01407 float med;
01408 int nStatPts = 0;
01409 float *statVals;
01410 float noiseLevel;
01411 float *outlier = NULL;
01412 int statsDone = 0;
01413 int sIdx = 0;
01414 float dev = 0.0;
01415 int nPtsB4Rej = npts;
01416
01417
01418
01419 noiseLevel = kNOISE_EQ * noiseS * sensAdj;
01420
01421
01422
01423 statVals = (float *)malloc(sizeof(float) * npts);
01424 if (!statVals) printf("MALLOC_FAILED");
01425
01426 memcpy(statVals, valBr, sizeof(float) * npts);
01427 nStatPts = magStats(&statVals, npts, sum, minOutPts, &avg, &med);
01428
01429
01430
01431
01432 for (j = 0; j < nPtsB4Rej; ++j)
01433 {
01434
01435
01436 if (!statsDone)
01437 {
01438 for (sIdx = 0; sIdx < nStatPts; sIdx++)
01439 {
01440 ssqr += statVals[sIdx] * statVals[sIdx];
01441 }
01442
01443 sig = sqrt((ssqr - nStatPts * avg * avg) / (nStatPts - 1));
01444 statsDone = 1;
01445 }
01446
01447 dev = fabs(valBr[j] - med);
01448 if (nptsfinal >= nEquivPtsReq) break;
01449 if (dev < nsig * sig)
01450 {
01451 sumfinalBr += valBr[j];
01452 sumfinalBt += valBt[j];
01453 sumfinalBp += valBp[j];
01454 nptsfinal += 1;
01455
01456 --npts;
01457 ++nrej;
01458 if (dlog)
01459 {
01460 char rejDescBuf[128];
01461
01462 snprintf(rejDescBuf,
01463 sizeof(rejDescBuf),
01464 rejDescFormat,
01465 pcols[j].ds,
01466 pcols[j].col,
01467 row);
01468
01469 }
01470
01471 }
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494 }
01495
01496 free(statVals);
01497 }
01498
01499
01500
01501
01502
01503 if (nptsfinal)
01504 {
01505
01506 float synValBr = sumfinalBr/nptsfinal;
01507 float synValBt = sumfinalBt/nptsfinal;
01508 float synValBp = sumfinalBp/nptsfinal;
01509 float minVal = (SHRT_MIN + 1);
01510 float maxVal = (SHRT_MAX - 1);
01511
01512
01513
01514
01515 if (synValBr < minVal)
01516 {
01517 synopBr[row * len[0] + col] = minVal;
01518 }
01519 else if (synValBr > maxVal)
01520 {
01521 synopBr[row * len[0] + col] = maxVal;
01522 }
01523 else
01524 {
01525 synopBr[row * len[0] + col] = synValBr;
01526 }
01527
01528 if (synValBt < minVal)
01529 {
01530 synopBt[row * len[0] + col] = minVal;
01531 }
01532 else if (synValBt > maxVal)
01533 {
01534 synopBt[row * len[0] + col] = maxVal;
01535 }
01536 else
01537 {
01538 synopBt[row * len[0] + col] = synValBt;
01539 }
01540
01541 if (synValBp < minVal)
01542 {
01543 synopBp[row * len[0] + col] = minVal;
01544 }
01545 else if (synValBp > maxVal)
01546 {
01547 synopBp[row * len[0] + col] = maxVal;
01548 }
01549 else
01550 {
01551 synopBp[row * len[0] + col] = synValBp;
01552 }
01553
01554 }
01555 else
01556 {
01557 synopBr[row * len[0] + col] = DRMS_MISSING_FLOAT;
01558 synopBt[row * len[0] + col] = DRMS_MISSING_FLOAT;
01559 synopBp[row * len[0] + col] = DRMS_MISSING_FLOAT;
01560 }
01561
01562 ww[row * len[0] + col] = npts;
01563
01564 epts[row * len[0] + col] = nptsfinal;
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579 }
01580
01581 free(valBr);
01582 valBr = NULL;
01583 free(valBt);
01584 valBt = NULL;
01585 free(valBp);
01586 valBp = NULL;
01587 free(ept);
01588 ept = NULL;
01589
01590 if (pcols)
01591 {
01592 free(pcols);
01593 pcols = NULL;
01594 }
01595 }
01596 return 0;
01597 }
01598
01599 int cmp(const void *a, const void *b)
01600 {
01601 float aa = *(float *)a;
01602 float bb = *(float *)b;
01603 return (aa<bb) ? -1 : (aa==bb) ? 0 : 1;
01604 }
01605
01606
01607 int magStats(float **valBr, int npts, double sum, int outThreshold, double *avg, float *med)
01608 {
01609 int actPts = npts;
01610 float *out = NULL;
01611 float *in = *valBr;
01612 int idx = 0;
01613 int iOut = 0;
01614 float medianInt = 0.0;
01615 float first;
01616 float last;
01617
01618 qsort((void *)in, npts, sizeof(float), cmp);
01619
01620 if (npts > outThreshold && npts > 1)
01621 {
01622 actPts = npts - 1;
01623 out = malloc(sizeof(float) * actPts);
01624
01625 medianInt = in[npts/2];
01626
01627 first = in[0];
01628 last = in[npts - 1];
01629
01630 if (fabs(first - medianInt) <= fabs(last - medianInt))
01631 {
01632 idx = 0;
01633 sum -= last;
01634 }
01635 else
01636 {
01637 idx = 1;
01638 sum -= first;
01639 }
01640
01641 for (iOut = 0; idx < npts && iOut < actPts; idx++, iOut++)
01642 {
01643 out[iOut] = in[idx];
01644 }
01645
01646 free(in);
01647 *valBr = out;
01648 in = *valBr;
01649 }
01650
01651 *avg = sum / actPts;
01652 *med = in[actPts / 2];
01653
01654 return actPts;
01655 }
01656
01657 int CmpMagCol(const void *a, const void *b)
01658 {
01659 MagCol_t *aa = (MagCol_t *)a;
01660 MagCol_t *bb = (MagCol_t *)b;
01661
01662 if (aa->valid && bb->valid)
01663 {
01664 return (fabsf(aa->dist) < fabsf(bb->dist)) ? -1 :
01665 ((fabsf(aa->dist) == fabsf(bb->dist)) ? 0 : 1);
01666 }
01667 else if (aa->valid)
01668 {
01669 return -1;
01670 }
01671 else if (bb->valid)
01672 {
01673 return 1;
01674 }
01675 else
01676 {
01677 return 0;
01678 }
01679 }
01680
01681 void SortMagCols(MagCol_t *mc, int nelem)
01682 {
01683
01684 MagCol_t *mc_cpy = NULL;
01685 int i;
01686 mc_cpy = (MagCol_t *)malloc(sizeof(MagCol_t) * nelem);
01687 for (i = 0; i < nelem; i++)
01688 mc_cpy[i] = mc[i];
01689 m_sort(mc, mc_cpy, nelem);
01690 free(mc_cpy);
01691 }
01692
01693 void m_sort(MagCol_t *mc, MagCol_t *mc_cpy, int sz)
01694 {
01695 int n, i, j, mid;
01696 mid = sz/2;
01697 if (mid > 1) m_sort(mc_cpy, mc, mid);
01698 if (sz-mid > 1) m_sort(mc_cpy + mid, mc + mid, sz - mid);
01699
01700
01701 MagCol_t *mc_cpy2 = mc_cpy + mid;
01702 for (n=0, i=0, j=0; i<mid && j<sz-mid; n++)
01703 mc[n] = (fabs(mc_cpy[i].dist) < fabs(mc_cpy2[j].dist)) ? mc_cpy[i++] : mc_cpy2[j++];
01704 while (i<mid)
01705 mc[n++] = mc_cpy[i++];
01706 while (j<sz-mid)
01707 mc[n++] = mc_cpy2[j++];
01708 }
01709
01710 void frebinbox(float *image_in, float *image_out, int nx, int ny, int nbinx, int nbiny)
01711 {
01712 int nxout, nyout;
01713 int ii, jj, i, j;
01714 nxout = nx / nbinx; nyout = ny / nbiny;
01715 for (j = 0; j < nyout; j++) {
01716 int yOff, jy;
01717 jy = j * nbiny;
01718 for (i = 0; i < nxout; i++) {
01719 int ix;
01720 ix = i * nbinx;
01721 float aveval = 0.0;
01722 int number = 0;
01723 for (jj = 0; jj < nbiny; jj++) {
01724 yOff = (jy + jj) * nx;
01725 for (ii = 0; ii < nbinx; ii++) {
01726 int iData = yOff + ix + ii;
01727 if (!(isnan(image_in[iData]))) {
01728 aveval += image_in[iData];
01729 number += 1;
01730 }
01731 }
01732 }
01733 image_out[j*nxout+i] = aveval/number;
01734 }
01735 }
01736 }
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750
01751
01752
01753
01754
01755
01756
01757
01758
01759
01760
01761
01762
01763
01764
01765
01766
01767
01768
01769
01770
01771
01772
01773
01774
01775
01776
01777
01778
01779
01780
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792
01793
01794
01795
01796
01797
01798
01799
01800
01801
01802
01803
01804
01805
01806
01807
01808
01809
01810
01811
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829 void FreeMagColsData(int start, int end, int incr, int *n, MagCol_t **mc)
01830 {
01831 if (mc)
01832 {
01833 float *dataBr = NULL, *dataBt = NULL, *dataBp = NULL;
01834 int cIdx;
01835 int idx;
01836
01837 for (cIdx = start; incr > 0 ? cIdx <= end : cIdx >= end; cIdx += incr)
01838 {
01839 for (idx = 0; idx < n[cIdx]; idx++)
01840 {
01841 dataBr = mc[cIdx][idx].datacolBr;
01842 dataBt = mc[cIdx][idx].datacolBt;
01843 dataBp = mc[cIdx][idx].datacolBp;
01844 if (dataBp)
01845 {
01846 free(dataBp);
01847 mc[cIdx][idx].datacolBp = NULL;
01848 }
01849 if (dataBt)
01850 {
01851 free(dataBt);
01852 mc[cIdx][idx].datacolBt = NULL;
01853 }
01854 if (dataBr)
01855 {
01856 free(dataBr);
01857 mc[cIdx][idx].datacolBr = NULL;
01858 mc[cIdx][idx].valid = 0;
01859 }
01860 }
01861 }
01862 }
01863 }
01864
01865 TIME CarringtonTime(int crot, double L)
01866 {
01867 double eph[30];
01868 double CT, clong=0.0;
01869 double err, CTp50m, CTm50m;
01870 TIME t;
01871 char *stime;
01872 char tstr[64];
01873 CT = 0.0;
01874 static TIME T1853 = 0.0, delta_T = 0.0;
01875 T1853 = sscan_time("1853.11.09_22:27:24_UTC");
01876
01877 delta_T = sscan_time("1977.01.01_00:00:00_TAI") - sscan_time("1601.01.01_00:00:00_UT");
01878
01879 CT = 360.0 * crot - L;
01880 t = (C2 + CT * C1) * SID;
01881
01882
01883 solephem(t, eph);
01884 err = CT - eph[8];
01885 solephem(t+6*3600.0, eph);
01886 CTp50m = eph[8];
01887 solephem(t-6*3600.0, eph);
01888 CTm50m = eph[8];
01889
01890 t += 12*3600 * (err/(CTp50m - CTm50m));
01891 solephem(t, eph);
01892 err = CT - eph[8];
01893 solephem(t+300.0, eph);
01894 CTp50m = eph[8];
01895 solephem(t-300.0, eph);
01896 CTm50m = eph[8];
01897
01898 t += 600 * (err/(CTp50m - CTm50m));
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912 return(t);
01913 }
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924
01925
01926
01927
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941
01942
01943
01944
01945
01946
01947
01948
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014
02015
02016
02017
02018
02019
02020
02021
02022
02023
02024
02025
02026
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040
02041
02042