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: hmib3compsynoptic4cr2136.c,v 1.1 2016/06/24 22:59:37 arta 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
00363
00364
00365
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
00459
00460 csKey = drms_getkey_int(inRec, kCARSTRCH, &status);
00461 csKey = (drms_ismissing_int(csKey)) ? 0 : csKey;
00462
00463 if (idx == 0)
00464 {
00465 carrStretch = csKey;
00466 }
00467 else
00468 {
00469 if (csKey != carrStretch)
00470 {
00471 printf(" Attempt to mix carr-streched and non-carr-stretched images.\n");
00472 printf(" Rejecting ds=%d.\n", ds);
00473 continue;
00474 }
00475 }
00476
00477 if (carrStretch > 0)
00478 {
00479 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_A, &status);
00480 if (idx == 0)
00481 {
00482 diffrotA = (float)csCoeffKey;
00483 }
00484 else
00485 {
00486 if (fabsf(csCoeffKey - diffrotA) > 0.001)
00487 {
00488 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00489 printf(" Rejecting ds=%d.\n", ds);
00490 continue;
00491 }
00492 }
00493
00494 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_B, &status);
00495 if (idx == 0)
00496 {
00497 diffrotB = (float)csCoeffKey;
00498 }
00499 else
00500 {
00501 if (fabsf(csCoeffKey - diffrotB) > 0.001)
00502 {
00503 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00504 printf(" Rejecting ds=%d.\n", ds);
00505 continue;
00506 }
00507 }
00508
00509 csCoeffKey = (float)drms_getkey_double(inRec, kDIFROT_C, &status);
00510 if (idx == 0)
00511 {
00512 diffrotC = (float)csCoeffKey;
00513 }
00514 else
00515 {
00516 if (fabsf(csCoeffKey - diffrotC) > 0.001)
00517 {
00518 printf(" Attempt to use inconsistent carr stretch parameters.\n");
00519 printf(" Rejecting ds=%d.\n", ds);
00520 continue;
00521 }
00522 }
00523 }
00524
00525 clong = drms_getkey_double(inRec, "CRVAL1", &status);
00526 cmLong = drms_getkey_double(inRec, "CRLN_OBS", &status);
00527 orot = drms_getkey_int(inRec, "CAR_ROT", &status);
00528 if (drms_ismissing_float(clong) || drms_ismissing_int(orot))
00529 {
00530 printf(" Bad MAP_L0 or OBS_CR in header; skipped");
00531 continue;
00532 }
00533
00534 mapctnew = (double)(orot) * 360.0 - clong - center;
00535 mapCMnew = (double)(orot) * 360.0 - cmLong - center;
00536 if (mapCMnew < mapCM)
00537 {
00538 printf(" Source image (ds=%d) has a smaller CT than previous img; skipped.\n", ds);
00539 continue;
00540 }
00541 else
00542 {
00543 mapct = mapctnew;
00544 mapCM = mapCMnew;
00545 }
00546
00547 remapLgmax = drms_getkey_double(inRec, "MAPLGMAX", &status);
00548 remapLgmin = drms_getkey_double(inRec, "MAPLGMIN", &status);
00549 imrec[idx].mapdev = (remapLgmax - remapLgmin) / 2.0;
00550 imrec[idx].recno = drms_getkey_int(inRec, "I_DREC", &status);
00551 imrec[idx].mapct = mapct;
00552 imrec[idx].mapCM = mapCM;
00553 imrec[idx].ds = ds;
00554 strcpy(t_obs, drms_getkey_string(inRec, "T_REC", &status));
00555 imrec[idx].tobs = sscan_time(t_obs);
00556 imrec[idx].tmin = MAX(mapCM - halfWindow, mapct + center - imrec[idx].mapdev);
00557 imrec[idx].tmax = MIN(mapCM + halfWindow, mapct + center + imrec[idx].mapdev);
00558 ++idx;
00559
00560 }
00561 ngood = idx;
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576 printf("\n######### SECOND PASS #################\n");
00577
00578 if (!(wt = (int *) calloc(len[0],4)))
00579 DIE("MALLOC_FAILED");
00580 if (!(ww = (char *) malloc(len[0]*len[1]*2)))
00581 DIE("MALLOC_FAILED");
00582 if (!(epts = (float *) malloc(len[0] * len[1] * sizeof(float))))
00583 DIE("MALLOC_FAILED");
00584 if (!(synopBr = (float *) malloc(len[0]*len[1]*4)))
00585 DIE("MALLOC_FAILED");
00586 if (!(synopBt = (float *) malloc(len[0]*len[1]*4)))
00587 DIE("MALLOC_FAILED");
00588 if (!(synopBp = (float *) malloc(len[0]*len[1]*4)))
00589 DIE("MALLOC_FAILED");
00590
00591
00592 sortedMagCol = (MagCol_t **)malloc(sizeof(MagCol_t *) * len[0]);
00593 if (!sortedMagCol)
00594 DIE("MALLOC_FAILED");
00595 memset(sortedMagCol, 0, sizeof(MagCol_t *) * len[0]);
00596
00597
00598 started = 0;
00599 ended = 0;
00600 nxtSyncol = len[0] - 1;
00601 synRange = 60.0 ;
00602
00603
00604 nsynop = rint(360.0/synRange);
00605
00606
00607
00608
00609
00610 for (ds = 0; ds < nsynop; ds++)
00611 {
00612 subSynopStart = synstart + ds * synRange;
00613 subSynopEnd = synstart + (ds + 1) * synRange - synstep;
00614 SyncolStart = rint((synend - subSynopStart) / synstep);
00615 SyncolEnd = rint((synend - subSynopEnd) / synstep);
00616 printf("ds=%d\n", ds);
00617
00618 for (idx = 0; idx < ngood; idx++)
00619 {
00620 int recno;
00621 int col;
00622 float magtype = 0.0;
00623 char *mType = NULL;
00624 float equivPts = 0.0;
00625 char t_mag[64];
00626 TIME tmag;
00627 DRMS_Record_t *inRec;
00628 DRMS_Segment_t *inSeg = NULL;
00629 DRMS_Array_t *inArrayBr, *inArrayBt, *inArrayBp;
00630 inRec = inRD->records[imrec[idx].ds];
00631
00632 if (imrec[idx].tmax <= subSynopStart || imrec[idx].tmin >= subSynopEnd) continue;
00633
00634
00635
00636
00637 maprightct = MAX(subSynopStart, imrec[idx].tmin);
00638 mapleftct = MIN(subSynopEnd, imrec[idx].tmax);
00639 mapcols = drms_getkey_int(inRec, "MAPMMAX", &status) + 1;
00640 mapmidcol = rint((mapcols - 1.0) / 2 + center / synstep);
00641 cols2right = rint((imrec[idx].mapct - maprightct) / synstep);
00642 cols2left = rint((imrec[idx].mapct - mapleftct) / synstep);
00643 synmidcol = rint((synend - imrec[idx].mapct) / synstep);
00644
00645
00646
00647
00648
00649 mrc = mapmidcol + cols2right;
00650 mlc = mapmidcol + cols2left;
00651
00652
00653 src = synmidcol + cols2right;
00654 slc = synmidcol + cols2left;
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667 equivPts = 1.0;
00668
00669
00670
00671 inSeg = drms_segment_lookup(inRec, "Br");
00672 inArrayBr = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00673
00674 if (status)
00675 {
00676 printf(" No data file found, status=%d\n", status);
00677 drms_free_array(inArrayBr);
00678 continue;
00679 }
00680
00681 inSeg = drms_segment_lookup(inRec, "Bt");
00682 inArrayBt = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00683
00684 if (status)
00685 {
00686 printf(" No data file found, status=%d\n", status);
00687 drms_free_array(inArrayBr);
00688 drms_free_array(inArrayBt);
00689 continue;
00690 }
00691
00692 inSeg = drms_segment_lookup(inRec, "Bp");
00693 inArrayBp = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
00694
00695 if (status)
00696 {
00697 printf(" No data file found, status=%d\n", status);
00698 drms_free_array(inArrayBr);
00699 drms_free_array(inArrayBt);
00700 drms_free_array(inArrayBp);
00701 continue;
00702 }
00703
00704 for (col = mlc; col <= mrc; ++col)
00705 {
00706 MagCol_t mMagCol;
00707
00708 mMagCol.dist = ((col - mapmidcol) * synstep + imrec[idx].mapct) - imrec[idx].mapCM;
00709 mMagCol.datacolBr = (float *)malloc(sizeof(float) * len[1]);
00710 mMagCol.datacolBt = (float *)malloc(sizeof(float) * len[1]);
00711 mMagCol.datacolBp = (float *)malloc(sizeof(float) * len[1]);
00712 mMagCol.equivPts = equivPts;
00713 mMagCol.ds = imrec[idx].ds;
00714 mMagCol.col = col;
00715 mMagCol.valid = 1;
00716
00717 pmBr = (float *)inArrayBr->data + col;
00718 pmBt = (float *)inArrayBt->data + col;
00719 pmBp = (float *)inArrayBp->data + col;
00720
00721 psBr = mMagCol.datacolBr;
00722 psBt = mMagCol.datacolBt;
00723 psBp = mMagCol.datacolBp;
00724
00725 for (row = 0; row < len[1]; ++row)
00726 {
00727 *psBr = *pmBr;
00728 pmBr += mapcols;
00729 psBr++;
00730 *psBt = *pmBt;
00731 pmBt += mapcols;
00732 psBt++;
00733 *psBp = *pmBp;
00734 pmBp += mapcols;
00735 psBp++;
00736 }
00737
00738 syncol = col + slc - mlc;
00739
00740 if (!sortedMagCol[syncol])
00741 {
00742 sortedMagCol[syncol] = (MagCol_t *)malloc(sizeof(MagCol_t) * nStackMags);
00743 if (!sortedMagCol[syncol]) DIE("MALLOC_FAILED");
00744 memset(sortedMagCol[syncol], 0, sizeof(MagCol_t) * nStackMags);
00745 }
00746
00747 if (wt[syncol] > 0 && (wt[syncol] % nStackMags == 0))
00748 {
00749 int frames = 1 + (wt[syncol] / nStackMags);
00750 MagCol_t *mc = (MagCol_t *)malloc(sizeof(MagCol_t) * frames * nStackMags);
00751 memcpy(mc,
00752 sortedMagCol[syncol],
00753 sizeof(MagCol_t) * (frames - 1) * nStackMags);
00754 free(sortedMagCol[syncol]);
00755 sortedMagCol[syncol] = mc;
00756 }
00757
00758 sortedMagCol[syncol][wt[syncol]] = mMagCol;
00759 wt[syncol]++;
00760
00761 }
00762
00763 drms_free_array(inArrayBr);
00764 drms_free_array(inArrayBt);
00765 drms_free_array(inArrayBp);
00766 }
00767
00768 calcsynret = CalcSynCols(SyncolStart,
00769 SyncolEnd,
00770 -1,
00771 sortedMagCol,
00772 synopBr,
00773 synopBt,
00774 synopBp,
00775 wt,
00776 ww,
00777 epts,
00778
00779 len,
00780 center,
00781 nEquivPtsReq,
00782 los,
00783
00784 sensAdj,
00785 noiseS,
00786 nsig,
00787 maxNoiseAdj,
00788 minOutPts,
00789 dlog);
00790
00791 printf("ds=%d\n", ds);
00792 FreeMagColsData(SyncolStart, SyncolEnd, -1, wt, sortedMagCol);
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806 if (calcsynret) DIE(" Cannot be finished up!");
00807 }
00808
00809 if (!ended) lastidx = ngood-1;
00810 magtime = imrec[(int)(ngood/2)].tobs;
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826 float *smallSynopBr = NULL, *smallSynopBt = NULL, *smallSynopBp = NULL, *smallEpts = NULL;
00827 int smallDims[2], xout, yout;
00828
00829 xout = len[0]/nbin; yout = len[1]/(nbin-1);
00830 if (!(smallSynopBr = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00831 if (!(smallSynopBt = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00832 if (!(smallSynopBp = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00833 if (!(smallEpts = (float *) malloc(xout*yout*4))) DIE("MALLOC_FAILED");
00834 smallDims[0] = xout; smallDims[1] = yout;
00835
00836
00837
00838
00839
00840 frebinbox(synopBr, smallSynopBr, len[0], len[1], nbin, nbin-1);
00841 frebinbox(synopBt, smallSynopBt, len[0], len[1], nbin, nbin-1);
00842 frebinbox(synopBp, smallSynopBp, len[0], len[1], nbin, nbin-1);
00843 frebinbox(epts, smallEpts, len[0], len[1], nbin, nbin-1);
00844
00845
00846
00847 outRD = drms_create_records(drms_env, 1, outRecQuery, DRMS_PERMANENT, &status);
00848 if (status)
00849 DIE("Output record not created");
00850 smallRD = drms_create_records(drms_env, 1, smallRecQuery, DRMS_PERMANENT, &status);
00851 if (status)
00852 DIE("Output record not created");
00853
00854 DRMS_Segment_t *outSeg, *smallSeg;
00855 DRMS_Array_t *outArray, *smallArray;
00856 int outDims[2] = {len[0],len[1]};
00857 outRec = outRD->records[0];
00858 smallRec = smallRD->records[0];
00859
00860
00861
00862 if (fstats(len[0]*len[1], synopBr, &statMin, &statMax, &statMedn, &statMean, &statSig,
00863 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00864
00865 i = len[0]*len[1];
00866 drms_setkey_int(outRec, "TOTVALS", i);
00867 drms_setkey_int(outRec, "BRVALS", statNgood);
00868 i = len[0]*len[1]-statNgood;
00869 drms_setkey_int(outRec, "BRMISSVA", i);
00870 drms_setkey_double(outRec, "BRMIN", statMin);
00871 drms_setkey_double(outRec, "BRMAX", statMax);
00872 drms_setkey_double(outRec, "BRMEDN", statMedn);
00873 drms_setkey_double(outRec, "BRMEAN", statMean);
00874 drms_setkey_double(outRec, "BRRMS", statSig);
00875 drms_setkey_double(outRec, "BRSKEW", statSkew);
00876 drms_setkey_double(outRec, "BRKURT", statKurt);
00877
00878 if (fstats(len[0]*len[1], synopBt, &statMin, &statMax, &statMedn, &statMean, &statSig,
00879 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00880
00881 drms_setkey_int(outRec, "BTVALS", statNgood);
00882 i = len[0]*len[1]-statNgood;
00883 drms_setkey_int(outRec, "BTMISSVA", i);
00884 drms_setkey_double(outRec, "BTMIN", statMin);
00885 drms_setkey_double(outRec, "BTMAX", statMax);
00886 drms_setkey_double(outRec, "BTMEDN", statMedn);
00887 drms_setkey_double(outRec, "BTMEAN", statMean);
00888 drms_setkey_double(outRec, "BTRMS", statSig);
00889 drms_setkey_double(outRec, "BTSKEW", statSkew);
00890 drms_setkey_double(outRec, "BTKURT", statKurt);
00891
00892 if (fstats(len[0]*len[1], synopBp, &statMin, &statMax, &statMedn, &statMean, &statSig,
00893 &statSkew, &statKurt, &statNgood)) printf("\n Statistics computation failed\n");
00894
00895 drms_setkey_int(outRec, "BPVALS", statNgood);
00896 i = len[0]*len[1]-statNgood;
00897 drms_setkey_int(outRec, "BPMISSVA", i);
00898 drms_setkey_double(outRec, "BPMIN", statMin);
00899 drms_setkey_double(outRec, "BPMAX", statMax);
00900 drms_setkey_double(outRec, "BPMEDN", statMedn);
00901 drms_setkey_double(outRec, "BPMEAN", statMean);
00902 drms_setkey_double(outRec, "BPRMS", statSig);
00903 drms_setkey_double(outRec, "BPSKEW", statSkew);
00904 drms_setkey_double(outRec, "BPKURT", statKurt);
00905
00906 if (fstats(xout*yout, smallSynopBr, &smallstatMin, &smallstatMax, &smallstatMedn, &smallstatMean, &smallstatSig,
00907 &smallstatSkew, &smallstatKurt, &smallstatNgood)) printf("\n Statistics computation failed\n");
00908
00909 i = xout*yout;
00910 drms_setkey_int(smallRec, "TOTVALS", i);
00911 drms_setkey_int(smallRec, "BRVALS", smallstatNgood);
00912 i = xout*yout-smallstatNgood;
00913 drms_setkey_int(smallRec, "BRMISSVA", i);
00914 drms_setkey_double(smallRec, "BRMIN", smallstatMin);
00915 drms_setkey_double(smallRec, "BRMAX", smallstatMax);
00916 drms_setkey_double(smallRec, "BRMEDN", smallstatMedn);
00917 drms_setkey_double(smallRec, "BRMEAN", smallstatMean);
00918 drms_setkey_double(smallRec, "BRRMS", smallstatSig);
00919 drms_setkey_double(smallRec, "BRSKEW", smallstatSkew);
00920 drms_setkey_double(smallRec, "BRKURT", smallstatKurt);
00921
00922 if (fstats(xout*yout, smallSynopBt, &smallstatMin, &smallstatMax, &smallstatMedn, &smallstatMean, &smallstatSig,
00923 &smallstatSkew, &smallstatKurt, &smallstatNgood)) printf("\n Statistics computation failed\n");
00924
00925 drms_setkey_int(smallRec, "BTVALS", smallstatNgood);
00926 i = xout*yout-smallstatNgood;
00927 drms_setkey_int(smallRec, "BTMISSVA", i);
00928 drms_setkey_double(smallRec, "BTMIN", smallstatMin);
00929 drms_setkey_double(smallRec, "BTMAX", smallstatMax);
00930 drms_setkey_double(smallRec, "BTMEDN", smallstatMedn);
00931 drms_setkey_double(smallRec, "BTMEAN", smallstatMean);
00932 drms_setkey_double(smallRec, "BTRMS", smallstatSig);
00933 drms_setkey_double(smallRec, "BTSKEW", smallstatSkew);
00934 drms_setkey_double(smallRec, "BTKURT", smallstatKurt);
00935
00936 if (fstats(xout*yout, smallSynopBp, &smallstatMin, &smallstatMax, &smallstatMedn, &smallstatMean, &smallstatSig,
00937 &smallstatSkew, &smallstatKurt, &smallstatNgood)) printf("\n Statistics computation failed\n");
00938
00939 drms_setkey_int(smallRec, "BPVALS", smallstatNgood);
00940 i = xout*yout-smallstatNgood;
00941 drms_setkey_int(smallRec, "BPMISSVA", i);
00942 drms_setkey_double(smallRec, "BPMIN", smallstatMin);
00943 drms_setkey_double(smallRec, "BPMAX", smallstatMax);
00944 drms_setkey_double(smallRec, "BPMEDN", smallstatMedn);
00945 drms_setkey_double(smallRec, "BPMEAN", smallstatMean);
00946 drms_setkey_double(smallRec, "BPRMS", smallstatSig);
00947 drms_setkey_double(smallRec, "BPSKEW", smallstatSkew);
00948 drms_setkey_double(smallRec, "BPKURT", smallstatKurt);
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967 outSeg = drms_segment_lookup(outRec, "Br");
00968 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, synopBr, &status);
00969 status = drms_segment_write(outSeg, outArray, 0);
00970 if (status)
00971 DIE("problem writing file");
00972 drms_free_array(outArray);
00973
00974
00975 outSeg = drms_segment_lookup(outRec, "Bt");
00976 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, synopBt, &status);
00977 status = drms_segment_write(outSeg, outArray, 0);
00978 if (status)
00979 DIE("problem writing file");
00980 drms_free_array(outArray);
00981
00982
00983 outSeg = drms_segment_lookup(outRec, "Bp");
00984 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, synopBp, &status);
00985 status = drms_segment_write(outSeg, outArray, 0);
00986 if (status)
00987 DIE("problem writing file");
00988 drms_free_array(outArray);
00989
00990
00991 outSeg = drms_segment_lookupnum(outRec, 3);
00992 outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, outDims, epts, &status);
00993 status = drms_segment_write(outSeg, outArray, 0);
00994 if (status)
00995 DIE("problem writing file");
00996 drms_free_array(outArray);
00997
00998
00999 smallSeg = drms_segment_lookupnum(smallRec, 0);
01000 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallSynopBr, &status);
01001 status = drms_segment_write(smallSeg, smallArray, 0);
01002 if (status)
01003 DIE("problem writing file");
01004 drms_free_array(smallArray);
01005
01006
01007 smallSeg = drms_segment_lookupnum(smallRec, 1);
01008 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallSynopBt, &status);
01009 status = drms_segment_write(smallSeg, smallArray, 0);
01010 if (status)
01011 DIE("problem writing file");
01012 drms_free_array(smallArray);
01013
01014
01015 smallSeg = drms_segment_lookupnum(smallRec, 2);
01016 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallSynopBp, &status);
01017 status = drms_segment_write(smallSeg, smallArray, 0);
01018 if (status)
01019 DIE("problem writing file");
01020 drms_free_array(smallArray);
01021
01022
01023 smallSeg = drms_segment_lookupnum(smallRec, 3);
01024 smallArray = drms_array_create(DRMS_TYPE_FLOAT, 2, smallDims, smallEpts, &status);
01025 status = drms_segment_write(smallSeg, smallArray, 0);
01026 if (status)
01027 DIE("problem writing file");
01028 drms_free_array(smallArray);
01029
01030
01031
01032
01033
01034 sprint_ut(tstr, CURRENT_SYSTEM_TIME);
01035 sscanf(tstr, "%d.%d.%d", &y, &m, &d);
01036 sprintf(tstr, "%04d-%02d-%02d", y, m, d);
01037 drms_setkey_string(outRec, "DATE", tstr);
01038
01039
01040
01041
01042 drms_setkey_string(outRec, "HISTORY", historyofthemodule);
01043 drms_setkey_string(outRec, "BLD_VERS", jsoc_version);
01044 status = drms_setkey_string(outRec, "CODEVER", cvsinfo);
01045 drms_setkey_string(outRec, "CTYPE1", "CRLN-CEA");
01046 drms_setkey_string(outRec, "CTYPE2", "CRLT-CEA");
01047
01048 i=len[0]-(len[0]/2+1.0)+1.0;
01049
01050
01051
01052
01053
01054 drms_setkey_double(outRec, "CRPIX1", i);
01055
01056 l=(len[1]+1.0)/2;
01057 drms_setkey_double(outRec, "CRPIX2", l);
01058
01059
01060 carrtime = (cr-1)*360.0 + 180.0;
01061 drms_setkey_double(outRec, "CRVAL1", carrtime);
01062 i=0.0;
01063 drms_setkey_double(outRec, "CRVAL2", i);
01064 l=-360.0/len[0];
01065 drms_setkey_double(outRec, "CDELT1", l);
01066 drms_setkey_double(outRec, "CDELT2", 1.0/sinbdivs);
01067 drms_setkey_string(outRec, "CUNIT1", "degree");
01068 drms_setkey_string(outRec, "CUNIT2", "Sine Latitude");
01069 drms_setkey_string(outRec, "WCSNAME", "Carrington Heliographic");
01070
01071
01072 sprint_at(tstr, trot - delta_T);
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094 drms_setkey_string(outRec, "T_OBS", tstr);
01095 drms_setkey_string(outRec, "T_ROT", tstr);
01096 sprint_at(tstr, tstart - delta_T);
01097 drms_setkey_string(outRec, "T_START", tstr);
01098 sprint_at(tstr, tstop - delta_T);
01099 drms_setkey_string(outRec, "T_STOP", tstr);
01100
01101
01102 drms_setkey_int(outRec, "CAR_ROT", cr);
01103 drms_setkey_double(outRec, "CARRTIME", carrtime);
01104 solephem(trot, eph);
01105
01106 drms_setkey_double(outRec, "B0_ROT", eph[9]/RADSINDEG);
01107 solephem(tstart, eph);
01108
01109 drms_setkey_double(outRec, "B0_FRST", eph[9]/RADSINDEG);
01110 solephem(tstop, eph);
01111
01112 drms_setkey_double(outRec, "B0_LAST", eph[9]/RADSINDEG);
01113
01114 l=(cr-1)*360.0;
01115 drms_setkey_double(outRec, "LON_FRST", l);
01116 l=cr*360.0-360.0/len[0];
01117 drms_setkey_double(outRec, "LON_LAST", l);
01118 l=-360.0/len[0];
01119 drms_setkey_double(outRec, "LON_STEP", l);
01120 l=center;
01121 drms_setkey_double(outRec, "W_OFFSET", l);
01122 drms_setkey_string(outRec, "W_WEIGHT", "Even");
01123 i=lastidx-firstidx+1;
01124 drms_setkey_int(outRec, "IMG_NUM", i);
01125 sprint_at(tstr, imrec[firstidx].tobs);
01126 drms_setkey_string(outRec, "IMG_FRST", tstr);
01127 sprint_at(tstr, imrec[lastidx].tobs);
01128 drms_setkey_string(outRec, "IMG_LAST", tstr);
01129 sprint_at(tstr, magtime);
01130 drms_setkey_string(outRec, "IMG_ROT", tstr);
01131 drms_setkey_float(outRec, HWNWIDTH, halfWindow);
01132 drms_setkey_float(outRec, EQPOINTS, nEquivPtsReq);
01133 drms_setkey_float(outRec, "NSIGMA", nsig);
01134 drms_setkey_longlong(outRec, "CALVER64", calVer);
01135
01136
01137
01138
01139 if (carrStretch > 0)
01140 {
01141 drms_setkey_int(outRec, kCARSTRCH, carrStretch);
01142 drms_setkey_float(outRec, kDIFROT_A, diffrotA);
01143 drms_setkey_float(outRec, kDIFROT_B, diffrotB);
01144 drms_setkey_float(outRec, kDIFROT_C, diffrotC);
01145 }
01146
01147
01148
01149
01150
01151
01152 sprint_ut(tstr, CURRENT_SYSTEM_TIME);
01153 sscanf(tstr, "%d.%d.%d", &y, &m, &d);
01154 sprintf(tstr, "%04d-%02d-%02d", y, m, d);
01155 drms_setkey_string(smallRec, "DATE", tstr);
01156
01157
01158
01159
01160 drms_setkey_string(smallRec, "HISTORY", historyofthemodule);
01161 drms_setkey_string(smallRec, "BLD_VERS", jsoc_version);
01162 status = drms_setkey_string(smallRec, "CODEVER", cvsinfo);
01163 drms_setkey_string(smallRec, "CTYPE1", "CRLN-CEA");
01164 drms_setkey_string(smallRec, "CTYPE2", "CRLT-CEA");
01165
01166 double tmp;
01167 tmp=xout - ((180.0-((nbin+1)/2.0-1.0)*(360.0/len[0]))/(360.0/xout)+1.0) + 1.0;
01168
01169
01170
01171 drms_setkey_double(smallRec, "CRPIX1", tmp);
01172 tmp=(yout+1.0)/2;
01173 drms_setkey_double(smallRec, "CRPIX2", tmp);
01174
01175 carrtime = (cr-1)*360.0 + 180.0;
01176 drms_setkey_double(smallRec, "CRVAL1", carrtime);
01177 tmp=0.0;
01178 drms_setkey_double(smallRec, "CRVAL2", tmp);
01179 l=-360.0/xout;
01180 drms_setkey_double(smallRec, "CDELT1", l);
01181 drms_setkey_double(smallRec, "CDELT2", nbin*1.0/sinbdivs);
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