00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <jsoc_main.h>
00014 #include <cmdparams.h>
00015 #include <drms.h>
00016 #include <drms_names.h>
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include <ctype.h>
00020 #include <strings.h>
00021 #include <sys/types.h>
00022 #include <sys/time.h>
00023 #include <sys/stat.h>
00024 #include <unistd.h>
00025 #include <printk.h>
00026 #include <astro.h>
00027 #include <fresize.h>
00028 #include <gapfill.h>
00029 #include "list.h"
00030 #include </home/jsoc/include/fftw3.h>
00031
00032 #include "imgdecode.h"
00033 #include "lev0lev1.h"
00034 #include "quallev1.h"
00035 #include "limb_fit.h"
00036 #include "cosmic_ray.h"
00037 #include "get_pointing_info.c"
00038
00039
00040 #define LEV0SERIESNAMEIRIS "iris_ground.lev0_dc1"
00041 #define LEV1SERIESNAMEIRIS "iris_ground.lev1_dc1"
00042
00043 #define DSCRSNAME "iris_ground.window_table"
00044 #define DSFFNAME "iris.flatfield_prelim"
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #define LEV1LOG_BASEDIR "/usr/local/logs/lev1"
00055 #define H1LOGFILE "/usr/local/logs/lev1/build_lev1_iris.%s.log"
00056 #define NUMTIMERS 8 //number of seperate timers avail
00057 #define NOTSPECIFIED "***NOTSPECIFIED***"
00058 #define LOGTEST 0
00059 #define CAL_HCFTID 17 //image is cal mode
00060
00061
00062 #define SAA_HLZ_SERIES "iris.saa_hlz"
00063
00064 int compare_rptr(const void *a, const void *b);
00065 static TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss);
00066
00067
00068 ModuleArgs_t module_args[] = {
00069 {ARG_STRING, "mode", NOTSPECIFIED, "either recnum or fsn"},
00070 {ARG_STRING, "dsin", NOTSPECIFIED, "dataset of lev0 filtergrams"},
00071 {ARG_STRING, "dsout", NOTSPECIFIED, "dataset of lev1 output"},
00072 {ARG_STRING, "logfile", NOTSPECIFIED, "optional log file name. Will create one if not given"},
00073 {ARG_INTS, "brec", "0", "first lev0 rec# to process. 0=error must be given by build_lev1_mgr"},
00074 {ARG_INTS, "erec", "0", "last lev0 rec# to process. 0=error must be given by build_lev1_mgr"},
00075 {ARG_INTS, "bfsn", "0", "first lev0 fsn# to process. 0=error must be given by build_lev1_mgr"},
00076 {ARG_INTS, "efsn", "0", "last lev0 fsn# to process. 0=error must be given by build_lev1_mgr"},
00077 {ARG_INTS, "quicklook", "1", "1=quick look, 0 = definitive mode"},
00078 {ARG_FLAG, "v", "0", "verbose flag"},
00079 {ARG_FLAG, "h", "0", "help flag"},
00080 {ARG_FLAG, "r", "0", "restart flag"},
00081 {ARG_END}
00082 };
00083
00084 CmdParams_t cmdparams;
00085
00086 char *module_name = "build_lev1_iris_NEW";
00087
00088 FILE *h1logfp;
00089 static LEV0LEV1 lev0lev1;
00090 static LEV0LEV1 *l0l1 = &lev0lev1;
00091
00092 static DRMS_Record_t *rs;
00093 static DRMS_Record_t *crsrec;
00094 static DRMS_Record_t *rs0, *rs1, *rsff, *rsbad_pix, *rec_bad_aia, *rt, *rresp;
00095 static DRMS_Record_t *rptr;
00096 static DRMS_Segment_t *segment, *segment0;
00097 static DRMS_Segment_t *segmentff;
00098 static DRMS_Segment_t *darkseg;
00099 static DRMS_Segment_t *badseg;
00100
00101
00102 static DRMS_Array_t *segArray;
00103 static DRMS_RecordSet_t *crsset;
00104 static DRMS_RecordSet_t *rset0, *rset1, *rsetff, *rsbad_aia, *rs_t=NULL,
00105 *rs_resp=NULL;
00106 static DRMS_Array_t *Array0;
00107 static DRMS_Array_t *Arrayff;
00108 static DRMS_Array_t *ArrayDark;
00109 static DRMS_Array_t *ArrayBad;
00110 static DRMS_Array_t *ArraySpike;
00111 static TIME sdo_epoch;
00112 static PTINFO *ptinfo = NULL;
00113 static PTINFO ptdata;
00114 static char bld_vers[16];
00115 static char datestr[32];
00116 static char open_dsname[256];
00117 static char dsffname[128];
00118 static char path[DRMS_MAXPATHLEN], bad_pix_path[DRMS_MAXPATHLEN];
00119 static char bad_aia_path[DRMS_MAXPATHLEN];
00120 static char rs1_path[DRMS_MAXPATHLEN];
00121 static struct timeval first[NUMTIMERS], second[NUMTIMERS];
00122
00123
00124 static char *orbseries = "iris.orbit_vectors";
00125
00126 static int nspikes, respike, fid, aiftsid, *oldvalues, *spikelocs, *newvalues;
00127 static int hcftid, aiagp6;
00128 static short aifcps;
00129 double aiascale = 1.0;
00130
00131 IORBIT_Info_t *IOinfo = NULL;
00132
00133 IORBIT_Info_t IOdata;
00134 LIBASTRO_Error_t IOstatus;
00135 unsigned int fsnarray[NUMRECLEV1];
00136 unsigned int fsnx = 0;
00137
00138
00139 float data1[MAXPIXELS];
00140 float ftmp;
00141 int data1A[MAXPIXELS];
00142 short data1S[MAXPIXELS];
00143 int array_cosmic[16777216];
00144 double tgttimes[NUMRECLEV1];
00145
00146 long long brec, erec, bfsn, efsn;
00147 long long bnumx, enumx;
00148 int verbose;
00149 int modeflg = 0;
00150 int imagecnt = 0;
00151 int restartflg = 0;
00152 int abortflg = 0;
00153 int sigalrmflg = 0;
00154 int ignoresigalrmflg = 0;
00155 int quicklook;
00156
00157 int flatmiss[NUMRECLEV1];
00158 int orbmiss[NUMRECLEV1];
00159 int asdmiss[NUMRECLEV1];
00160 int mpdmiss[NUMRECLEV1];
00161 int limbmiss[NUMRECLEV1];
00162 int noimage[NUMRECLEV1];
00163 int missflg[NUMRECLEV1];
00164
00165 char logname[128];
00166 char argdsin[128], argdsout[128], arglogfile[128];
00167 char argbx[80], argex[80], argquick[80], argmode[80];
00168 char timetag[32];
00169 char tlmseriesname[128];
00170 char lev0seriesname[128];
00171 char *username;
00172 char *logfile;
00173 char *mode;
00174 char *dsin;
00175 char *dsout;
00176 char *dsff;
00177
00179 typedef struct {
00180 float rsun_lf;
00181 float x0_lf;
00182 float y0_lf;
00183 } LIMB_SOMETHING;
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 void do_quallev1(DRMS_Record_t *rs0, DRMS_Record_t *rs1, int inx, unsigned int fsn)
00199 {
00200 int quallev1 = 0;
00201 int rstatus;
00202 char *pchar;
00203
00204 quallev1 = missflg[inx];
00205 if(flatmiss[inx]) quallev1 = quallev1 | Q_NOFLAT;
00206 if(orbmiss[inx]) quallev1 = quallev1 | Q_NOORB;
00207 if(limbmiss[inx]) quallev1 = quallev1 | Q_NOLIMB;
00208 if(asdmiss[inx]) {
00209 quallev1 = quallev1 | Q_NOASD;
00210 drms_setkey_string(rs1, "ASD_REC", DRMS_MISSING_STRING);
00211 }
00212 if(mpdmiss[inx]) quallev1 = quallev1 | Q_NOMPD;
00213 if(noimage[inx]) quallev1 = quallev1 | Q_MISSALL;
00214 if(ptinfo) {
00215 ptdata = ptinfo[inx];
00216 if(strcmp(ptdata.acs_mode, "SCIENCE")) {
00217 quallev1 = quallev1 | Q_NOACS_SCI;
00218 }
00219 if(strcmp(ptdata.acs_eclp, "NO")) {
00220 quallev1 = quallev1 | Q_ACS_ECLP;
00221 }
00222 if(strcmp(ptdata.acs_sunp, "YES")) {
00223 quallev1 = quallev1 | Q_ACS_SUNP;
00224 }
00225 if(strcmp(ptdata.acs_safe, "NO")) {
00226 quallev1 = quallev1 | Q_ACS_SAFE;
00227 }
00228 }
00229 pchar = drms_getkey_string(rs0, "IMG_TYPE", &rstatus);
00230 if(rstatus) {
00231 printk("ERROR: in drms_getkey_string(IMG_TYPE) fsn=%u\n", fsn);
00232 }
00233 else {
00234 if(strcmp(pchar, "LIGHT")) {
00235 quallev1 = quallev1 | Q_IMG_TYPE;
00236 }
00237 }
00238 if(quicklook) {
00239 quallev1 = quallev1 | Q_NRT;
00240 }
00241 drms_setkey_int(rs1, "QUALITY", quallev1);
00242 drms_setkey_string(rs1, "BLD_VERS", bld_vers);
00243 }
00244
00245 int nice_intro ()
00246 {
00247 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00248 if (usage)
00249 {
00250 printf ("Usage:\nbuild_lev1_iris [-vhr] "
00251 "mode=<recnum|fsn> dsin=<lev0> dsout=<lev1>\n"
00252 "brec=<rec#>|bfsn=<fsn#> erec=<rec#>|efsn=<fsn#>\n"
00253 "quicklook=<0|1> [logfile=<file>]\n"
00254 " -h: help - show this message then exit\n"
00255 " -v: verbose\n"
00256 " -r: restart. only used when we restart our selves periodically\n"
00257 "mode= recnum: brec and erec have the record # range to process \n"
00258 " fsn: bfsn and efsn have the fsn # range to process\n"
00259 " For safety, the mode and arg name used must be consistent\n"
00260 "dsin= data set name of lev0 input\n"
00261 " default hmi=hmi.lev0e aia=aia.lev0e\n"
00262 "dsout= data set name of lev1 output\n"
00263 " default hmi=su_production.hmi_lev1e aia=su_production.aia_lev1e\n"
00264 "brec= first lev0 rec# to process. 0=error must be given by build_lev1_mgr\n"
00265 "erec= last lev0 rec# to process. 0=error must be given by build_lev1_mgr\n"
00266 "bfsn= first fsn# to process. 0=error must be given by build_lev1_mgr\n"
00267 "efsn= last fsn# to process. 0=error must be given by build_lev1_mgr\n"
00268 "quicklook= 1 = quicklook mode, 0 = definitive mode\n"
00269 "logfile= optional log file name. If not given uses:\n"
00270 " /usr/local/logs/lev1/build_lev1_aia.<time_stamp>.log\n");
00271 return(1);
00272 }
00273 verbose = cmdparams_get_int (&cmdparams, "v", NULL);
00274 restartflg = cmdparams_get_int (&cmdparams, "r", NULL);
00275 return (0);
00276 }
00277
00278 TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss)
00279 {
00280 static int firstcall = 1;
00281 if (firstcall)
00282 {
00283 firstcall = 0;
00284 }
00285
00286 return(sdo_epoch + (TIME)sdo_s + (TIME)(sdo_ss & 0xFFFF)/65536.0);
00287 }
00288
00289
00290 char *do_datestr() {
00291 time_t tval;
00292 struct tm *t_ptr;
00293
00294 tval = time(NULL);
00295 t_ptr = localtime(&tval);
00296 sprintf(datestr, "%d.%02d.%02d_%02d:%02d:%02d",
00297 (t_ptr->tm_year+1900), (t_ptr->tm_mon+1),
00298 t_ptr->tm_mday, t_ptr->tm_hour, t_ptr->tm_min, t_ptr->tm_sec);
00299 return(datestr);
00300 }
00301
00302
00303 char *gettimetag()
00304 {
00305 struct timeval tvalr;
00306 struct tm *t_ptr;
00307
00308 gettimeofday(&tvalr, NULL);
00309 t_ptr = localtime((const time_t *)&tvalr);
00310 sprintf(timetag, "%04d.%02d.%02d.%02d%02d%02d",
00311 (t_ptr->tm_year+1900), (t_ptr->tm_mon+1), t_ptr->tm_mday, t_ptr->tm_hour, t_ptr->tm_min, t_ptr->tm_sec);
00312 return(timetag);
00313 }
00314
00315
00316 void BeginTimer(int n)
00317 {
00318 gettimeofday (&first[n], NULL);
00319 }
00320
00321 float EndTimer(int n)
00322 {
00323 gettimeofday (&second[n], NULL);
00324 if (first[n].tv_usec > second[n].tv_usec) {
00325 second[n].tv_usec += 1000000;
00326 second[n].tv_sec--;
00327 }
00328 return (float) (second[n].tv_sec-first[n].tv_sec) +
00329 (float) (second[n].tv_usec-first[n].tv_usec)/1000000.0;
00330 }
00331
00332
00333 int h1log(const char *fmt, ...)
00334 {
00335 va_list args;
00336 char string[32768];
00337
00338 va_start(args, fmt);
00339 vsprintf(string, fmt, args);
00340 if(h1logfp) {
00341 fprintf(h1logfp, string);
00342 fflush(h1logfp);
00343 }
00344 else {
00345 printf(string);
00346 fflush(stdout);
00347 }
00348 va_end(args);
00349 return(0);
00350 }
00351
00352 int send_mail(char *fmt, ...)
00353 {
00354 va_list args;
00355 char string[1024], cmd[1024];
00356
00357 va_start(args, fmt);
00358 vsprintf(string, fmt, args);
00359 sprintf(cmd, "echo \"%s\" | Mail -s \"build_lev1_iris mail\" lev0_user", string);
00360 system(cmd);
00361 va_end(args);
00362 return(0);
00363 }
00364
00365
00366 void abortit(int stat)
00367 {
00368 printk("***Abort in progress ...\n");
00369 printk("**Exit build_lev1_iris w/ status = %d\n", stat);
00370 if (h1logfp) fclose(h1logfp);
00371 exit(stat);
00372 }
00373
00375 int rdout_mode_correct()
00376 {
00377 return(0);
00378 }
00379
00381 int orbit_calc()
00382 {
00383 return(0);
00384 }
00385
00386 static int GetSaaHlz(DRMS_Env_t *env, DRMS_RecordSet_t *rs, double *tgttimes, int ntimes, int (*myprintkerr)(const char *fmt, ...))
00387 {
00388 IORBIT_SaaHlzInfo_t *saahlzinfo = NULL;
00389 int itime;
00390 LinkedList_t *list = NULL;
00391 LinkedList_t **pList = NULL;
00392 ListNode_t *node = NULL;
00393 char *eventType = NULL;
00394 int nhlz;
00395 int shlz;
00396 int saa;
00397 HContainer_t *colToList = NULL;
00398 HContainer_t **pColToList = NULL;
00399 char timeStr[IORBIT_SAAHLZINFO_TIME_KEY_LEN];
00400 LIBASTRO_Error_t rv;
00401 int ret = 0;
00402
00403 rv = iorbit_getSaaHlzInfo(env, SAA_HLZ_SERIES, tgttimes, ntimes, &saahlzinfo);
00404
00405 if (rv == kLIBASTRO_Success)
00406 {
00407 for (itime = 0; itime < ntimes; itime++)
00408 {
00409 nhlz = 0;
00410 shlz = 0;
00411 saa = 0;
00412
00413 snprintf(timeStr, sizeof(timeStr), "%lf", tgttimes[itime]);
00414
00415 if ((pColToList = hcon_lookup(saahlzinfo, timeStr)) != NULL)
00416 {
00417 colToList = *pColToList;
00418
00419 if ((pList = hcon_lookup(colToList, IORBIT_SAAHLZINFO_KW_EVENT_TYPE)) != NULL)
00420 {
00421 list = *pList;
00422 list_llreset(list);
00423
00424 while((node = list_llnext(list)) != NULL)
00425 {
00426 eventType = (char *)node->data;
00427
00428 if (strcasecmp(eventType, "NHLZ") == 0)
00429 {
00430 nhlz = 1;
00431 }
00432 else if (strcasecmp(eventType, "SHLZ") == 0)
00433 {
00434 shlz = 1;
00435 }
00436 else if (strcasecmp(eventType, "SAA") == 0)
00437 {
00438 saa = 1;
00439 }
00440 }
00441
00442
00443 if (nhlz && shlz)
00444 {
00445
00446 printkerr("ERROR - For tobs %lf, both event types NHLZ and SHLZ exist.\n", tgttimes[itime]);
00447 drms_setkey_int(rs->records[itime], "HLZ", 0);
00448 }
00449 else if (nhlz)
00450 {
00451 printf(" Setting HLZ to 1.\n");
00452 drms_setkey_int(rs->records[itime], "HLZ", 1);
00453 }
00454 else if (shlz)
00455 {
00456 printf(" Setting HLZ to 2.\n");
00457 drms_setkey_int(rs->records[itime], "HLZ", 2);
00458 }
00459 else
00460 {
00461 drms_setkey_int(rs->records[itime], "HLZ", 0);
00462 }
00463
00464 if (saa)
00465 {
00466 drms_setkey_int(rs->records[itime], "SAA", 1);
00467 }
00468 else
00469 {
00470 drms_setkey_int(rs->records[itime], "SAA", 0);
00471 }
00472 }
00473 else
00474 {
00475 myprintkerr("ERROR - SAA-HLZ info for keyword %s unexecpectedly missing.\n", IORBIT_SAAHLZINFO_KW_EVENT_TYPE);
00476 ret = 1;
00477 }
00478 }
00479 else
00480 {
00481 myprintkerr("ERROR - SAA-HLZ info for tobs %lf unexecpectedly missing.\n", tgttimes[itime]);
00482 ret = 1;
00483 }
00484 }
00485
00486 iorbit_cleanSaaHlzInfo(&saahlzinfo);
00487 }
00488 else
00489 {
00490 myprintkerr("ERROR - Couldn't query db properly.\n");
00491 ret = 1;
00492 }
00493
00494 return ret;
00495 }
00496
00497
00498
00499 #include "get_image_location.c"
00500 #include "limb_fit_function.c"
00501 #include "cosmic_ray.c"
00502 #include "heightformation.c"
00503
00504
00505 int do_ingest(long long bbrec, long long eerec)
00506 {
00507
00508 Image_Location *p_imageloc;
00509 Image_Location imageloc[NUMRECLEV1];
00510 TIME t_obs0;
00511 TIME tobs[NUMRECLEV1];
00512 float percentd;
00513 float cdelt1, rsun, crpix1, crpix2, crota2;
00514 double rsun_lf, x0_lf, y0_lf;
00515 int rstatus, dstatus, lstatus, ncnt, fcnt, i, j, k, qualint, nobs;
00516 int hshiexp, hcamid, nbad, n_cosmic;
00517 int *spikedata, status, axes[2], nbytes;
00518 uint32_t missvals, totvals;
00519 long long recnum0, recnum1, recnumff;
00520 char recrange[128], lev0name[128], flatrec[128], temprec[128], pointrec[128];
00521
00522 double scroll;
00523
00524 if(modeflg) sprintf(recrange, ":#%lld-#%lld", bbrec, eerec);
00525 else sprintf(recrange, "%lld-%lld", bbrec, eerec);
00526 sprintf(open_dsname, "%s[%s]", dsin, recrange);
00527 printk("open_dsname = %s\n", open_dsname);
00528 printk("#levnum recnum fsn\n");
00529
00530 t_obs0 = 0;
00531 rset0 = drms_open_records(drms_env, open_dsname, &rstatus);
00532 if(!rset0 || (rset0->n == 0) || rstatus) {
00533 printk("Can't do drms_open_records(%s)\n", open_dsname);
00534 printf("Can't do drms_open_records(%s)\n", open_dsname);
00535
00536 return(0);
00537 }
00538 drms_stage_records(rset0, 1, 0);
00539 ncnt = rset0->n;
00540 rptr = (DRMS_Record_t *)malloc(ncnt * sizeof(DRMS_Record_t));
00541 if(rptr == NULL) {
00542 printk("Can't malloc() for DRMS_Record_t sort\n");
00543 return(1);
00544 }
00545
00546 for(i=0; i < ncnt; i++) {
00547 memcpy(&rptr[i], rset0->records[i], sizeof(DRMS_Record_t));
00548 }
00549
00550 qsort(rptr, ncnt, sizeof(DRMS_Record_t), &compare_rptr);
00551
00552
00553
00554 for(i=0; i < ncnt; i++) {
00555 flatmiss[i] = 0;
00556 orbmiss[i] = 0;
00557 limbmiss[i] = 0;
00558 asdmiss[i] = 0;
00559 mpdmiss[i] = 0;
00560 noimage[i] = 0;
00561 missflg[i] = 0;
00562 rs0 = &rptr[i];
00563 recnum0 = rs0->recnum;
00564
00565 fsnx = drms_getkey_int(rs0, "FSN", &rstatus);
00566 fsnarray[i] = fsnx;
00567 printk("*0 %u %u\n", recnum0, fsnx);
00568
00569 tobs[i] = drms_getkey_time(rs0, "t_obs", &rstatus);
00570 if(rstatus) {
00571 printk("Error on drms_getkey_time() fsn=%u. Use DRMS_MISSING_TIME\n",
00572 fsnx);
00573 tobs[i] = DRMS_MISSING_TIME;
00574 }
00575 }
00576
00577 HContainer_t *keymap = NULL;
00578
00579 keymap = hcon_create(DRMS_MAXKEYNAMELEN, DRMS_MAXKEYNAMELEN, NULL, NULL, NULL, NULL, 0);
00580
00581 if (!keymap)
00582 {
00583 IOstatus = kLIBASTRO_OutOfMemory;
00584 }
00585 else {
00586 hcon_insert(keymap, "kXGCI", "geixobs");
00587 hcon_insert(keymap, "kYGCI", "geiyobs");
00588 hcon_insert(keymap, "kZGCI", "geizobs");
00589 hcon_insert(keymap, "kXHCI", "heixobs");
00590 hcon_insert(keymap, "kYHCI", "heiyobs");
00591 hcon_insert(keymap, "kZHCI", "heizobs");
00592 hcon_insert(keymap, "kRSUNOBS", "rsunobs");
00593 hcon_insert(keymap, "kOBSVR", "obsvr");
00594 hcon_insert(keymap, "kDSUNOBS", "dsunobs");
00595 hcon_insert(keymap, "kOBSDATE", "obsdate");
00596
00597 IOstatus = iorbit_getinfo_ext(drms_env,
00598 orbseries,
00599 NULL,
00600 IORBIT_Alg_Quadratic,
00601 tobs,
00602 ncnt,
00603 kIORBIT_CacheAction_DontCache,
00604 &IOinfo,
00605 keymap);
00606 hcon_destroy(&keymap);
00607 }
00608 if(IOstatus != kLIBASTRO_Success) {
00609 if(IOstatus == kLIBASTRO_InsufficientData) {
00610 printk("***ERROR in iorbit_getinfo_ext: kLIBASTRO_InsufficientData\n");
00611 }
00612 else {
00613 printk("***ERROR in iorbit_getinfo_ext() status=%d\n", IOstatus);
00614 }
00615 for(j=0; j < ncnt; j++) {
00616 orbmiss[j] = 1;
00617 }
00618 return(1);
00619 }
00620
00621
00622
00623 rset1 = drms_create_records(drms_env, ncnt, dsout, DRMS_PERMANENT,&dstatus);
00624 if(dstatus) {
00625 printk("**ERROR: Can't create records for %s\n", dsout);
00626 for(j=0; j < ncnt; j++) {
00627 noimage[j] = 1;
00628 }
00629 return(1);
00630 }
00631
00632
00633
00634
00635 if (GetSaaHlz(drms_env, rset1, tobs, ncnt, printkerr))
00636 {
00637 printk("***Error - Unable to fetch SAA-HLZ information.\n");
00638 return 1;
00639 }
00640
00641
00642
00643
00644
00645 for(i=0; i < ncnt; i++) {
00646
00647 rs0 = &rptr[i];
00648 recnum0 = rs0->recnum;
00649 fsnx = fsnarray[i];
00650 sprintf(lev0name, "%s[%u]", dsin, fsnx);
00651 if(drms_getkey_int(rs0, "QUALITY", 0) < 0) {
00652 printk("Bad QUALITY for %s, no lev1 made\n", lev0name);
00653 noimage[i] = 1;
00654
00655 }
00656 segment0 = drms_segment_lookupnum(rs0, 0);
00657 int seg0sz = segment0->axis[0]*segment0->axis[1];
00658 Array0 = drms_segment_read(segment0, DRMS_TYPE_SHORT, &rstatus);
00659 if(!Array0) {
00660 printk("Can't do drms_segment_read() %s status=%d\n",
00661 lev0name, rstatus);
00662 noimage[i] = 1;
00663 return(1);
00664 continue;
00665 }
00666 l0l1->adata0 = (short *)Array0->data;
00667 l0l1->dat1.adata1A = &data1A;
00668 l0l1->rs0 = rs0;
00669 l0l1->recnum0 = recnum0;
00670 l0l1->fsn = fsnx;
00671 l0l1->nx = segment0->axis[0];
00672 l0l1->ny = segment0->axis[1];
00673 l0l1->datavals = drms_getkey_int(rs0, "DATAVALS", &rstatus);
00674 l0l1->missvals = drms_getkey_int(rs0, "MISSVALS", &rstatus);
00675
00676 sprintf(open_dsname, "%s[%u]", dsout, fsnx);
00677 rs = rset1->records[i];
00678
00679
00680
00681
00682 {
00683 if(!quicklook) {
00684 DRMS_RecordSet_t *rset;
00685 DRMS_Record_t *rt;
00686 int st;
00687 TIME t_obs, time_qbi;
00688 float aeulrbrx, aeulrbry, aeulrbrz, acg_roll, ophase;
00689 t_obs = drms_getkey_time(rs0, "t_obs", &st);
00690 if (st) {
00691 t_obs = DRMS_MISSING_TIME;
00692 }
00693 else {
00694 sprintf(open_dsname, "iris.pointing_data[? date_obs > %f and date_obs <= %f ?]", t_obs-5, t_obs+5);
00695 printf("%s\n", open_dsname);
00696 rset = drms_open_records(drms_env, open_dsname, &st);
00697 if (!rset || !rset->n || st) {
00698 printk("Error in drms_open_records(%s); setting scroll to zero\n", open_dsname);
00699 scroll = 0.0;
00700 } else {
00701
00702 rt = rset->records[0];
00703 sprintf(pointrec, "iris.pointing_data[:#%lld]", rt->recnum);
00704 if(dstatus = drms_setkey_string(rs, "POINTREC", pointrec)) {
00705 printk("**ERROR on drms_setkey_string() for %s\n", pointrec);
00706 }
00707 time_qbi = drms_getkey_time(rt, "TIME_QBI", &st);
00708 aeulrbrx = drms_getkey_float(rt, "A_EULERBR_X", &st);
00709 aeulrbry = drms_getkey_float(rt, "A_EULERBR_Y", &st);
00710 aeulrbrz = drms_getkey_float(rt, "A_EULERBR_Z", &st);
00711 acg_roll = drms_getkey_float(rt, "A_CG_ROLL_ANGLE", &st);
00712 ophase = drms_getkey_float(rt, "OPHASE", &st);
00713
00714 drms_setkey_time(rs, "TIME_QBI", time_qbi);
00715 drms_setkey_float(rs, "AEULRBRX", aeulrbrx);
00716 drms_setkey_float(rs, "AEULRBRY", aeulrbry);
00717 drms_setkey_float(rs, "AEULRBRZ", aeulrbrz);
00718 drms_setkey_float(rs, "SAT_ROT", aeulrbrz);
00719 drms_setkey_float(rs, "ACG_ROLL", acg_roll);
00720 drms_setkey_float(rs, "OPHASE", ophase);
00721
00722 scroll = aeulrbrz;
00723 }
00724
00725 if (rset)
00726 drms_close_records(rset, DRMS_FREE_RECORD);
00727 }
00728 }
00729 }
00730
00731 drms_record_directory(rs, rs1_path, 0);
00732 if(!*rs1_path) {
00733 printk("***ERROR: No path to segment for %s\n", open_dsname);
00734 noimage[i] = 1;
00735 continue;
00736 }
00737 printf("\npath to lev1 = %s\n", rs1_path);
00738 if(rstatus = iris_isp2wcs(rs0, rs, scroll)) {
00739 printk("**ERROR: iris_isp2wcs() status = %d\n", rstatus);
00740 printk("Press on after error at fsn=%u...\n", fsnx);
00741 printf("**ERROR: at fsn %u\n", fsnx);
00742 continue;
00743
00744
00745 }
00746 dstatus = drms_setkey_int(rs, "FSN", fsnx);
00747
00748 if(!(segment = drms_segment_lookup(rs, "image_lev1"))) {
00749 printk("No drms_segment_lookup(rs, image_lev1) for %s\n", open_dsname);
00750 noimage[i] = 1;
00751 continue;
00752 }
00753 segArray = drms_array_create(DRMS_TYPE_INT,
00754 segment->info->naxis,
00755 segment0->axis,
00756 &data1A,
00757 &dstatus);
00758
00759
00760
00761
00762
00763 rstatus = drms_copykeys(rs, rs0, 0, kDRMS_KeyClass_Explicit);
00764 if(rstatus != DRMS_SUCCESS) {
00765 printk("Error %d in drms_copykeys() for fsn %u\n", fsnx);
00766 return(1);
00767 continue;
00768 }
00769 qualint = drms_getkey_int(rs0, "QUALITY", &rstatus);
00770 drms_setkey_int(rs, "QUALLEV0", qualint);
00771 fid = drms_getkey_int(rs0, "FID", &rstatus);
00772
00773 short isqisysn = drms_getkey_short(rs0, "ISQISYSN", &rstatus);
00774 int iimgots1 = drms_getkey_int(rs0, "IIMGOTS1", &rstatus);
00775 int iimgots2 = drms_getkey_int(rs0, "IIMGOTS2", &rstatus);
00776 int iimgots3 = drms_getkey_int(rs0, "IIMGOTS3", &rstatus);
00777 switch (isqisysn) {
00778 case 0:
00779 drms_setkey_int(rs, "IIMGOTS", iimgots1);
00780 break;
00781 case 1:
00782 drms_setkey_int(rs, "IIMGOTS", iimgots2);
00783 break;
00784 case 2:
00785 drms_setkey_int(rs, "IIMGOTS", iimgots3);
00786 break;
00787 }
00788
00789 drms_setkey_time(rs, "T_OBS", tobs[i]);
00790 printk("t_obs for lev0 = %10.5f fsn=%u\n", tobs[i], fsnarray[i]);
00791 drms_setkey_double(rs, "DATE", CURRENT_SYSTEM_TIME);
00792 if(ptinfo) {
00793 ptdata = ptinfo[i];
00794 drms_setkey_float(rs, "SAT_Y0", ptdata.sat_y0);
00795 drms_setkey_float(rs, "SAT_Z0", ptdata.sat_z0);
00796 drms_setkey_float(rs, "SAT_ROT", ptdata.sat_rot);
00797 drms_setkey_string(rs, "ACS_MODE", ptdata.acs_mode);
00798 drms_setkey_string(rs, "ACS_ECLP", ptdata.acs_eclp);
00799 drms_setkey_string(rs, "ACS_SUNP", ptdata.acs_sunp);
00800 drms_setkey_string(rs, "ACS_SAFE", ptdata.acs_safe);
00801 drms_setkey_string(rs, "ASD_REC", ptdata.asd_rec);
00802 drms_setkey_string(rs, "ACS_CGT", ptdata.acs_cgt);
00803 }
00804 if(IOinfo) {
00805 IOdata = IOinfo[i];
00806 drms_setkey_double(rs, "RSUN_OBS", IOdata.rsun_obs);
00807 drms_setkey_double(rs, "DSUN_OBS", IOdata.dsun_obs);
00808 drms_setkey_double(rs, "OBS_VR", IOdata.obs_vr);
00809 drms_setkey_double(rs, "GEIX_OBS", IOdata.gciX);
00810 drms_setkey_double(rs, "GEIY_OBS", IOdata.gciY);
00811 drms_setkey_double(rs, "GEIZ_OBS", IOdata.gciZ);
00812 drms_setkey_double(rs, "HEIX_OBS", IOdata.hciX);
00813 drms_setkey_double(rs, "HEIY_OBS", IOdata.hciY);
00814 drms_setkey_double(rs, "HEIZ_OBS", IOdata.hciZ);
00815 drms_setkey_string(rs, "ORB_REC", IOdata.orb_rec);
00816 }
00817
00818
00819
00820
00821
00822
00823
00824 drms_setkey_float(rs, "X0_MP", imageloc[i].x);
00825 drms_setkey_float(rs, "Y0_MP", imageloc[i].y);
00826 drms_setkey_float(rs, "INST_ROT", imageloc[i].instrot);
00827
00828 drms_setkey_float(rs, "IMSCL_MP", imageloc[i].imscale);
00829 drms_setkey_string(rs, "MPO_REC", imageloc[i].mpo_rec);
00830
00831 int camera = drms_getkey_int(rs0, "CAMERA", &rstatus);
00832 if(rstatus) {
00833 printk("Can't do drms_getkey_int() for fsn %u\n", fsnx);
00834 noimage[i] = 1;
00835 goto TEMPSKIP;
00836
00837 }
00838 if ( 0 == drms_setkey_time(rs, "T_REC", tobs[i])) {
00839 int status, allstat = 0;
00840 double tr_step;
00841 long long tr_index;
00842 TIME t_rec, tr_epoch;
00843 tr_index = drms_getkey_longlong(rs, "T_REC_index", &status);
00844 allstat += status;
00845 tr_step = drms_getkey_double(rs, "T_REC_step", &status);
00846 allstat += status;
00847 tr_epoch = drms_getkey_time(rs, "T_REC_epoch", &status);
00848 allstat += status;
00849 if (0 == allstat) {
00850 t_rec = tr_epoch + tr_index*tr_step;
00851 drms_setkey_time(rs, "T_REC", t_rec);
00852 }
00853 }
00854
00855 int iicrsid = drms_getkey_int(rs0, "IICRSID", &rstatus);
00856 if(rstatus) {
00857 printk("Can't do drms_getkey_int() for IICRSID for fsn %u\n", fsnx);
00858 printk("No CRS_TABLE keywords for lev1 fsn %u\n", fsnx);
00859 }
00860 else {
00861 sprintf(open_dsname, "%s[%d]", DSCRSNAME, iicrsid);
00862 printk("Open: %s\n", open_dsname);
00863 crsset = drms_open_records(drms_env, open_dsname, &rstatus);
00864 if(!crsset || (crsset->n == 0) || rstatus) {
00865 printk("Can't do drms_open_records(%s)\n", open_dsname);
00866 }
00867 else {
00868 crsrec = crsset->records[0];
00869 char *crsstr = drms_getkey_string(crsrec, "CRS_DESC", &status);
00870 drms_setkey_string(rs, "CRS_DESC", crsstr);
00871 crsstr = drms_getkey_string(crsrec, "CRS_TYPE", &status);
00872 drms_setkey_string(rs, "CRS_TYPE", crsstr);
00873 int crsint = drms_getkey_int(crsrec, "CRS_NREG", &status);
00874 drms_setkey_int(rs, "CRS_NREG", crsint);
00875 crsint = drms_getkey_int(crsrec, "TSR1", &status);
00876 drms_setkey_int(rs, "TSR1", crsint);
00877 crsint = drms_getkey_int(crsrec, "TER1", &status);
00878 drms_setkey_int(rs, "TER1", crsint);
00879 crsint = drms_getkey_int(crsrec, "TSC1", &status);
00880 drms_setkey_int(rs, "TSC1", crsint);
00881 crsint = drms_getkey_int(crsrec, "TEC1", &status);
00882 drms_setkey_int(rs, "TEC1", crsint);
00883 crsint = drms_getkey_int(crsrec, "TSR2", &status);
00884 drms_setkey_int(rs, "TSR2", crsint);
00885 crsint = drms_getkey_int(crsrec, "TER2", &status);
00886 drms_setkey_int(rs, "TER2", crsint);
00887 crsint = drms_getkey_int(crsrec, "TSC2", &status);
00888 drms_setkey_int(rs, "TSC2", crsint);
00889 crsint = drms_getkey_int(crsrec, "TEC2", &status);
00890 drms_setkey_int(rs, "TEC2", crsint);
00891 crsint = drms_getkey_int(crsrec, "TSR3", &status);
00892 drms_setkey_int(rs, "TSR3", crsint);
00893 crsint = drms_getkey_int(crsrec, "TER3", &status);
00894 drms_setkey_int(rs, "TER3", crsint);
00895 crsint = drms_getkey_int(crsrec, "TSC3", &status);
00896 drms_setkey_int(rs, "TSC3", crsint);
00897 crsint = drms_getkey_int(crsrec, "TEC3", &status);
00898 drms_setkey_int(rs, "TEC3", crsint);
00899 crsint = drms_getkey_int(crsrec, "TSR4", &status);
00900 drms_setkey_int(rs, "TSR4", crsint);
00901 crsint = drms_getkey_int(crsrec, "TER4", &status);
00902 drms_setkey_int(rs, "TER4", crsint);
00903 crsint = drms_getkey_int(crsrec, "TSC4", &status);
00904 drms_setkey_int(rs, "TSC4", crsint);
00905 crsint = drms_getkey_int(crsrec, "TEC4", &status);
00906 drms_setkey_int(rs, "TEC4", crsint);
00907 crsint = drms_getkey_int(crsrec, "TSR5", &status);
00908 drms_setkey_int(rs, "TSR5", crsint);
00909 crsint = drms_getkey_int(crsrec, "TER5", &status);
00910 drms_setkey_int(rs, "TER5", crsint);
00911 crsint = drms_getkey_int(crsrec, "TSC5", &status);
00912 drms_setkey_int(rs, "TSC5", crsint);
00913 crsint = drms_getkey_int(crsrec, "TEC5", &status);
00914 drms_setkey_int(rs, "TEC5", crsint);
00915 crsint = drms_getkey_int(crsrec, "TSR6", &status);
00916 drms_setkey_int(rs, "TSR6", crsint);
00917 crsint = drms_getkey_int(crsrec, "TER6", &status);
00918 drms_setkey_int(rs, "TER6", crsint);
00919 crsint = drms_getkey_int(crsrec, "TSC6", &status);
00920 drms_setkey_int(rs, "TSC6", crsint);
00921 crsint = drms_getkey_int(crsrec, "TEC6", &status);
00922 drms_setkey_int(rs, "TEC6", crsint);
00923 crsint = drms_getkey_int(crsrec, "TSR7", &status);
00924 drms_setkey_int(rs, "TSR7", crsint);
00925 crsint = drms_getkey_int(crsrec, "TER7", &status);
00926 drms_setkey_int(rs, "TER7", crsint);
00927 crsint = drms_getkey_int(crsrec, "TSC7", &status);
00928 drms_setkey_int(rs, "TSC7", crsint);
00929 crsint = drms_getkey_int(crsrec, "TEC7", &status);
00930 drms_setkey_int(rs, "TEC7", crsint);
00931 crsint = drms_getkey_int(crsrec, "TSR8", &status);
00932 drms_setkey_int(rs, "TSR8", crsint);
00933 crsint = drms_getkey_int(crsrec, "TER8", &status);
00934 drms_setkey_int(rs, "TER8", crsint);
00935 crsint = drms_getkey_int(crsrec, "TSC8", &status);
00936 drms_setkey_int(rs, "TSC8", crsint);
00937 crsint = drms_getkey_int(crsrec, "TEC8", &status);
00938 drms_setkey_int(rs, "TEC8", crsint);
00939
00940 crsint = drms_getkey_int(crsrec, "WIN_FLIP", &status);
00941 l0l1->winflip = crsint;
00942 drms_setkey_int(rs, "WIN_FLIP", crsint);
00943 printk("Close: %s\n", open_dsname);
00944 drms_close_records(crsset, DRMS_FREE_RECORD);
00945 }
00946 }
00947
00948
00949 if(!quicklook) {
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961 float itf1ccd1, itf2ccd2, itnuccd3, itsjccd4;
00962 float bt06cbpx, bt07cbnx, bt15ieb, it08gtwm;
00963 float it14sppx, it16spnx;
00964
00965 char *dstemp = "iris.temperatures_60s";
00966 if(fabs(tobs[i] - t_obs0) > 300.0) {
00967 char *selstr = "select max(date_obs) from ";
00968 char *whrstr = "where date_obs <= ";
00969 int nr;
00970 if(rs_t) {
00971 drms_close_records(rs_t, DRMS_FREE_RECORD);
00972 rs_t = NULL;
00973 }
00974 sprintf(open_dsname, "%s[? date_obs=(%s %s %s %f) ?]",
00975 dstemp, selstr, dstemp, whrstr, tobs[i]);
00976 printf(" %s\n", open_dsname);
00977 rt = NULL;
00978 rs_t = drms_open_records(drms_env, open_dsname, &rstatus);
00979 if(rstatus) printk("Can not open temperature series.\n");
00980 else {
00981 nr = rs_t->n;
00982 if(nr != 1) printk("%d records != 1.\n", nr);
00983 rt = rs_t->records[0];
00984 }
00985 t_obs0 = tobs[i];
00986 }
00987 if (rt) {
00988 int st;
00989 sprintf(temprec, "%s[:#%lld]", dstemp, rt->recnum);
00990 if(dstatus = drms_setkey_string(rs, "TEMP_REC", temprec)) {
00991 printk("**ERROR on drms_setkey_string() for %s\n", temprec);
00992 }
00993 itf1ccd1 = drms_getkey_float(rt, "ITF1CCD1", &st);
00994 itf2ccd2 = drms_getkey_float(rt, "ITF2CCD2", &st);
00995 itnuccd3 = drms_getkey_float(rt, "ITNUCCD3", &st);
00996 itsjccd4 = drms_getkey_float(rt, "ITSJCCD4", &st);
00997 bt06cbpx = drms_getkey_float(rt, "BT06CBPX", &st);
00998 bt07cbnx = drms_getkey_float(rt, "BT07CBNX", &st);
00999 bt15ieb = drms_getkey_float(rt, "BT15IEB", &st);
01000 it08gtwm = drms_getkey_float(rt, "IT08GTWM", &st);
01001 it14sppx = drms_getkey_float(rt, "IT14SPPX", &st);
01002 it16spnx = drms_getkey_float(rt, "IT16SPNX", &st);
01003
01004 drms_setkey_float(rs, "ITF1CCD1", itf1ccd1);
01005 drms_setkey_float(rs, "ITF2CCD2", itf2ccd2);
01006 drms_setkey_float(rs, "ITNUCCD3", itnuccd3);
01007 drms_setkey_float(rs, "ITSJCCD4", itsjccd4);
01008 drms_setkey_float(rs, "BT06CBPX", bt06cbpx);
01009 drms_setkey_float(rs, "BT07CBNX", bt07cbnx);
01010 drms_setkey_float(rs, "BT15IEB", bt15ieb);
01011 drms_setkey_float(rs, "IT08GTWM", it08gtwm);
01012 drms_setkey_float(rs, "IT14SPPX", it14sppx);
01013 drms_setkey_float(rs, "IT16SPNX", it16spnx);
01014 }
01015 }
01016
01017
01018
01019 if(quicklook) {
01020 DRMS_RecordSet_t *rset;
01021 DRMS_Record_t *rt;
01022 int st;
01023 TIME t_obs, roll_start;
01024 float roll_deg;
01025 t_obs = drms_getkey_time(rs0, "t_obs", &st);
01026 if (st) {
01027 t_obs = DRMS_MISSING_TIME;
01028 }
01029 else {
01030 sprintf(open_dsname, "iris.timeline_roll[? roll_start > %f and roll_start <= %f ?]", t_obs-86401, t_obs+1);
01031 printf("%s\n", open_dsname);
01032 rset = drms_open_records(drms_env, open_dsname, &st);
01033
01034 if (!rset || !rset->n || st) {
01035 printk("Error in drms_open_records(%s); setting scroll to zero\n", open_dsname);
01036 scroll = 0.0;
01037
01038 } else {
01039
01040
01041 rt = rset->records[(rset->n)-1];
01042 sprintf(pointrec, "iris.timeline_roll[:#%lld]", rt->recnum);
01043 if(dstatus = drms_setkey_string(rs, "POINTREC", pointrec)) {
01044 printk("**ERROR on drms_setkey_string() for %s\n", pointrec);
01045 }
01046 roll_start = drms_getkey_time(rt, "ROLL_START", &st);
01047 roll_deg = drms_getkey_float(rt, "DEGREES", &st);
01048
01049 drms_setkey_float(rs, "SAT_ROT", roll_deg);
01050
01051 scroll = roll_deg;
01052 }
01053
01054 if (rset)
01055 drms_close_records(rset, DRMS_FREE_RECORD);
01056 }
01057 }
01058
01059
01060 char *imgpath = drms_getkey_string(rs0, "IMG_PATH", &rstatus);
01061 if(rstatus) {
01062 printk("Can't do drms_getkey_string() for IMG_PATH\n");
01063 return(1);
01064 }
01065 if(quicklook) {
01066 sprintf(open_dsname, "%s[? t_start=(select max(t_start) from %s where t_start <= %10.5f and t_stop > %10.5f and IMG_PATH='%s') and IMG_PATH='%s' ?]",
01067 dsffname, dsffname, tobs[i], tobs[i], imgpath, imgpath);
01068 }
01069 else {
01070 sprintf(open_dsname, "%s[? t_start <= %10.5f and t_stop > %10.5f and IMG_PATH='%s' ?]",
01071 dsffname, tobs[i], tobs[i], imgpath);
01072 }
01073
01074
01075 rsetff = drms_open_records(drms_env, open_dsname, &rstatus);
01076 if(!rsetff || (rsetff->n == 0) || rstatus) {
01077 printk("Can't do drms_open_records(%s)\n", open_dsname);
01078 flatmiss[i] = 1; noimage[i] = 1;
01079 goto TEMPSKIP;
01080 return(1);
01081 }
01082 fcnt = rsetff->n;
01083 if(fcnt > 1) {
01084 printk("More than one FF found for %s?\n", open_dsname);
01085 printk("Use last one of %d found\n", fcnt);
01086
01087 }
01088
01089 rsff = rsetff->records[fcnt-1];
01090 recnumff = rsff->recnum;
01091 sprintf(flatrec, "%s[:#%lld]", dsffname, recnumff);
01092 if(dstatus = drms_setkey_string(rs, "FLAT_REC", flatrec )) {
01093 printk("**ERROR on drms_setkey_string() for %s\n", flatrec);
01094 }
01095 drms_record_directory(rsff, path, 1);
01096 if(!*path) {
01097 printk("***ERROR: No path to segment for %s\n", open_dsname);
01098
01099 return(1);
01100 }
01101
01102 segmentff = drms_segment_lookup(rsff, "flatfield");
01103 Arrayff = drms_segment_read(segmentff, DRMS_TYPE_FLOAT, &rstatus);
01104 if(!Arrayff) {
01105 printk("Can't do drms_segment_read() for Flat Field status=%d\n",
01106 rstatus);
01107 return(1);
01108 }
01109 l0l1->adataff = (float *)Arrayff->data;
01110
01111
01112
01113 darkseg = drms_segment_lookup(rsff, "dark");
01114 ArrayDark = drms_segment_read(darkseg, DRMS_TYPE_FLOAT, &rstatus);
01115 if(!ArrayDark) {
01116 printk("Can't do drms_segment_read() for dark. status=%d\n", rstatus);
01117 return(1);
01118 }
01119 l0l1->adatadark = (float *)ArrayDark->data;
01120
01121 badseg = drms_segment_lookup(rsff, "bad_pixel");
01122 ArrayBad = drms_segment_read(badseg, DRMS_TYPE_INT, &rstatus);
01123 nbad = drms_array_size(ArrayBad)/sizeof(int);
01124 if(!ArrayBad) {
01125 printk("Can't do drms_segment_read() for bad_pixel. status=%d\n",
01126 rstatus);
01127 return(1);
01128 }
01129 l0l1->adatabad = (int *)ArrayBad->data;
01130
01131
01132
01133
01134 l0l1->rs1 = rs;
01135 l0l1->rsff = rsff;
01136 l0l1->recnum1 = rs->recnum;
01137 l0l1->darkflag = 0;
01138 l0l1->sumx = drms_getkey_short(rs0, "SUMSPTRL", &rstatus);
01139 l0l1->sumy = drms_getkey_short(rs0, "SUMSPAT", &rstatus);
01140 hshiexp = drms_getkey_int(rs, "HSHIEXP", &rstatus);
01141 hcamid = drms_getkey_int(rs, "HCAMID", &rstatus);
01142 float sumdc=0.0;
01143 int idc, numdc=0;
01144 int aimgshce = drms_getkey_int(rs, "AIMGSHCE", &rstatus);
01145
01146 short iifrmtyp = drms_getkey_short(rs0, "IIFRMTYP", &rstatus);
01147 if((iifrmtyp == 2) || (iifrmtyp == 5)) l0l1->darkflag = 1;
01148 if(rstatus = do_flat_iris(l0l1)) {
01149 printk("***ERROR in do_flat_iris() status=%d\n", rstatus);
01150 printf("***ERROR in do_flat_iris() status=%d\n", rstatus);
01151 flatmiss[i] = 1; noimage[i] = 1;
01152
01153 goto FLATERR;
01154 }
01155
01156
01157
01158
01159
01160
01161 drms_setkey_float(rs, "OSCNMEAN", l0l1->oscnmean);
01162 drms_setkey_float(rs, "OSCNRMS", l0l1->oscnrms);
01163 drms_setkey_int(rs, "DATAMIN", l0l1->datamin);
01164 drms_setkey_int(rs, "DATAMAX", l0l1->datamax);
01165 drms_setkey_int(rs, "DATAMEDN", l0l1->datamedn);
01166 drms_setkey_float(rs, "DATAMEAN", l0l1->datamean);
01167 drms_setkey_float(rs, "DATARMS", l0l1->data_rms);
01168 drms_setkey_float(rs, "DATASKEW", l0l1->dataskew);
01169 drms_setkey_float(rs, "DATAKURT", l0l1->datakurt);
01170 drms_setkey_int(rs, "DATAVALS", l0l1->datavals);
01171 drms_setkey_int(rs, "MISSVALS", l0l1->missvals);
01172 missvals = (uint32_t)l0l1->missvals;
01173 totvals = (uint32_t)l0l1->datavals + missvals;
01174 drms_setkey_int(rs, "TOTVALS", (int)totvals);
01175 percentd = (float)((100.0 * (float)l0l1->datavals)/(float)totvals);
01176 drms_setkey_float(rs, "PERCENTD", percentd);
01177 if(missvals > 0) missflg[i] = missflg[i] | Q_1_MISS0;
01178 if(missvals > (uint32_t)(totvals * 0.01))
01179 missflg[i] = missflg[i] | Q_1_MISS1;
01180 if(missvals > (uint32_t)(totvals * 0.05))
01181 missflg[i] = missflg[i] | Q_1_MISS2;
01182 if(missvals > (uint32_t)(totvals * 0.25))
01183 missflg[i] = missflg[i] | Q_1_MISS3;
01184 if(l0l1->datavals == 0)
01185 missflg[i] = missflg[i] | Q_MISSALL;
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204 FLATERR:
01205 drms_close_records(rsetff, DRMS_FREE_RECORD);
01206 free(ArrayDark->data);
01207 free(Arrayff->data);
01208 free(ArrayBad->data);
01209
01210 TEMPSKIP:
01211 x0_lf = DRMS_MISSING_DOUBLE;
01212 y0_lf = DRMS_MISSING_DOUBLE;
01213 rsun_lf = DRMS_MISSING_DOUBLE;
01214
01215
01216
01217 int skiplimb = 0;
01218 hcftid = drms_getkey_int(rs, "HCFTID", &rstatus);
01219 if(hcftid == CAL_HCFTID) {
01220 printk("Cal mode image fsn=%u\n", fsnx);
01221 skiplimb = 1;
01222 }
01223 else {
01224 if(hshiexp == 0) {
01225 if(hcamid == 0 || hcamid == 1) {
01226 printk("Dark image fsn=%u\n", fsnx);
01227 skiplimb = 1;
01228 }
01229 }
01230 }
01231 lstatus = 1;
01232
01233
01234 WCSEND:
01235
01236
01237
01238
01239
01240 do_quallev1(rs0, rs, i, fsnx);
01241
01242
01243
01244 IRISSKIP:
01245 dstatus = drms_segment_writewithkeys(segment, segArray, 0);
01246
01247 if (dstatus) {
01248 printk("ERROR: drms_segment_write error=%d for fsn=%u\n",
01249 dstatus,fsnx);
01250 noimage[i] = 1;
01251 }
01252 recnum1 = rs->recnum;
01253 printk("*1 %u %u\n", recnum1, fsnx);
01254 free(Array0->data);
01255
01256 if (rs_resp) drms_close_records(rs_resp, DRMS_FREE_RECORD);
01257 rs_resp = NULL;
01258 }
01259
01260 drms_close_records(rset0, DRMS_FREE_RECORD);
01261 drms_close_records(rset1, DRMS_INSERT_RECORD);
01262 return(0);
01263 }
01264
01265 int compare_rptr(const void *a, const void *b)
01266 {
01267 TIME t1, t2;
01268 int rstatus;
01269 DRMS_Record_t *x=(DRMS_Record_t *)a, *y=(DRMS_Record_t *)b;
01270
01271 t1 = drms_getkey_time(x, "t_obs", &rstatus);
01272 if(rstatus) t1 = DRMS_MISSING_TIME;
01273 t2 = drms_getkey_time(y, "t_obs", &rstatus);
01274 if(rstatus) t2 = DRMS_MISSING_TIME;
01275 if(t1 < t2) return(-1);
01276 if(t1 > t2) return(1);
01277 return(0);
01278 }
01279
01280
01281 void setup()
01282 {
01283 FILE *fin;
01284 char string[128], cwdbuf[128], idstr[256], lfile[128];
01285 int tpid;
01286
01287 sdo_epoch = sscan_time("1958.01.01_00:00:00_TAI");
01288 do_datestr();
01289 printk_set(h1log, h1log);
01290 printk("%s\n", datestr);
01291 gethostname(idstr, 256);
01292 printf("Host: %s\n", idstr);
01293 printk("Host: %s\n", idstr);
01294 getcwd(cwdbuf, 126);
01295 sprintf(idstr, "Cwd: %s\nCall: ", cwdbuf);
01296 sprintf(string, "build_lev1_iris started as pid=%d ppid=%d user=%s\n",
01297 getpid(), getppid(), username);
01298 strcat(idstr, string);
01299 printk("%s", idstr);
01300 printf("%s", idstr);
01301 if(restartflg) printk("-r ");
01302 sprintf(argmode, "mode=%s", mode);
01303 sprintf(argdsin, "dsin=%s", dsin);
01304 sprintf(argdsout, "dsout=%s", dsout);
01305 if(modeflg) {
01306 sprintf(argbx, "brec=%lld", brec);
01307 sprintf(argex, "erec=%lld", erec);
01308 }
01309 else {
01310 sprintf(argbx, "bfsn=%lld", bfsn);
01311 sprintf(argex, "efsn=%lld", efsn);
01312 }
01313 sprintf(argquick, "quicklook=%d", quicklook);
01314 sprintf(arglogfile, "logfile=%s", logname);
01315 printk("%s %s %s %s %s %s %s\n",
01316 argmode, argdsin, argdsout, argbx, argex, argquick, arglogfile);
01317 printf("%s %s %s %s %s %s %s\n",
01318 argmode, argdsin, argdsout, argbx, argex, argquick, arglogfile);
01319 if(!restartflg) {
01320
01321
01322 }
01323 sprintf(bld_vers, "%s", jsoc_version);
01324 sprintf(idstr, "ps -ef | grep %s", LEV1VIEWERNAME);
01325 fin = popen(idstr, "r");
01326 while(fgets(string, sizeof string, fin)) {
01327 if(!(strstr(string, "perl"))) continue;
01328 sscanf(string, "%s %d", idstr, &tpid);
01329 sprintf(lfile, "%s/build_lev1_iris_restart_%d.touch", LEV1LOG_BASEDIR, tpid);
01330 sprintf(idstr, "/bin/touch %s", lfile);
01331 printk("%s\n", idstr);
01332 system(idstr);
01333 }
01334 umask(002);
01335
01336
01337
01338
01339
01340
01341 }
01342
01343
01344 int DoIt(void)
01345 {
01346 long long numofrecs, frec, lrec;
01347 int numrec, numofchunks, i;
01348 char line[80];
01349
01350 if (nice_intro())
01351 return (0);
01352 if(!(username = (char *)getenv("USER"))) username = "nouser";
01353 mode = cmdparams_get_str(&cmdparams, "mode", NULL);
01354 if(strcmp(mode, "recnum") && strcmp(mode, "fsn")) {
01355 printf("Error: mode= must be given as 'recnum' or 'fsn'\n");
01356 return(0);
01357 }
01358 if(!strcmp(mode, "recnum")) modeflg = 1;
01359 dsin = cmdparams_get_str(&cmdparams, "dsin", NULL);
01360 dsout = cmdparams_get_str(&cmdparams, "dsout", NULL);
01361 brec = cmdparams_get_int(&cmdparams, "brec", NULL);
01362 erec = cmdparams_get_int(&cmdparams, "erec", NULL);
01363 bfsn = cmdparams_get_int(&cmdparams, "bfsn", NULL);
01364 efsn = cmdparams_get_int(&cmdparams, "efsn", NULL);
01365 quicklook = cmdparams_get_int(&cmdparams, "quicklook", NULL);
01366
01367 if(modeflg) {
01368 if(brec == 0 || erec == 0) {
01369 fprintf(stderr, "brec and erec must be given for recnum mode. 0 not allowed\n");
01370 return(0);
01371 }
01372 if(brec > erec) {
01373 fprintf(stderr, "brec must be <= erec\n");
01374 return(0);
01375 }
01376 bnumx = brec;
01377 enumx = erec;
01378 }
01379 else {
01380 if(bfsn == 0 || efsn == 0) {
01381 fprintf(stderr, "bfsn and efsn must be given for fsn mode. 0 not allowed\n");
01382 return(0);
01383 }
01384 if(bfsn > efsn) {
01385 fprintf(stderr, "bfsn must be <= efsn\n");
01386 return(0);
01387 }
01388 bnumx = bfsn;
01389 enumx = efsn;
01390 }
01391 logfile = cmdparams_get_str(&cmdparams, "logfile", NULL);
01392 if (strcmp(dsin, NOTSPECIFIED) == 0) {
01393 dsin = LEV0SERIESNAMEIRIS;
01394 }
01395 if (strcmp(dsout, NOTSPECIFIED) == 0) {
01396 dsout = LEV1SERIESNAMEIRIS;
01397 }
01398 sprintf(dsffname, "%s", DSFFNAME);
01399
01400 if (strcmp(logfile, NOTSPECIFIED) == 0) {
01401 sprintf(logname, H1LOGFILE, gettimetag());
01402 }
01403 else {
01404 sprintf(logname, "%s", logfile);
01405 }
01406 if(restartflg || LOGTEST) {
01407 if((h1logfp=fopen(logname, "a")) == NULL)
01408 fprintf(stderr, "**Can't open for append the log file %s\n", logname);
01409 }
01410 else {
01411 if((h1logfp=fopen(logname, "w")) == NULL)
01412 fprintf(stderr, "**Can't open the log file %s\n", logname);
01413 }
01414 setup();
01415 numofrecs = (enumx - bnumx) + 1;
01416 numrec = NUMRECLEV1;
01417 numofchunks = numofrecs/numrec;
01418 if((numofrecs % numrec) != 0) numofchunks++;
01419 lrec = bnumx-1;
01420 for(i = 0; i < numofchunks; i++) {
01421 frec = lrec+1; lrec = (frec + numrec)-1;
01422 if(lrec > enumx) lrec=enumx;
01423 if(do_ingest(frec, lrec)) {
01424 printf("build_lev1_iris abort\nSee log: %s\n", logname);
01425 send_mail("build_lev1_iris abort\nSee log: %s\n", logname);
01426 return(0);
01427 }
01428 }
01429 printf("build_lev1_iris done last fsn=%u\n", fsnx);
01430 return(0);
01431 }