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