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