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
00030 #include "fftw3.h"
00031 #include "imgdecode.h"
00032 #include "lev0lev1.h"
00033 #include "quallev1.h"
00034 #include "limb_fit.h"
00035 #include "cosmic_ray.h"
00036 #include "get_pointing_info.c"
00037
00038
00039
00040
00041 #define LEV0SERIESNAMEHMI "su_production.lev0f_hmi"
00042 #define LEV0SERIESNAMEAIA "su_production.lev0f_aia"
00043
00044 #define LEV1SERIESNAMEHMI "hmi.lev1"
00045 #define LEV1SERIESNAMEAIA "su_production.aia_lev1e" //temp test case
00046
00047
00048 #define DSFFNAMEHMI "hmi.flatfield"
00049
00050
00051 #define DSFFNAMEAIA "aia.flatfield"
00052
00053 #define kDpath "dpath"
00054 #define kDpathDef "/home/jsoc/cvs/Development/JSOC"
00055
00056 #define LEV1LOG_BASEDIR "/usr/local/logs/lev1"
00057 #define H1LOGFILE "/usr/local/logs/lev1/build_lev1_hmi.%s.log"
00058 #define NUMTIMERS 8 //number of seperate timers avail
00059 #define NOTSPECIFIED "***NOTSPECIFIED***"
00060 #define LOGTEST 0
00061 #define CAL_HCFTID 17 //image is cal mode
00062
00063 int compare_rptr(const void *a, const void *b);
00064 static TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss);
00065
00066
00067 ModuleArgs_t module_args[] = {
00068 {ARG_STRING, "instru", NOTSPECIFIED, "instrument. either hmi or aia"},
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, "dsff", NOTSPECIFIED, "dataset of darks flats bads"},
00073 {ARG_STRING, "dsaiabad", NOTSPECIFIED, "dataset of AIA bad pixels"},
00074 {ARG_STRING, "logfile", NOTSPECIFIED, "optional log file name. Will create one if not given"},
00075 {ARG_INTS, "brec", "0", "first lev0 rec# to process. 0=error must be given by build_lev1_mgr"},
00076 {ARG_INTS, "erec", "0", "last lev0 rec# to process. 0=error must be given by build_lev1_mgr"},
00077 {ARG_INTS, "bfsn", "0", "first lev0 fsn# to process. 0=error must be given by build_lev1_mgr"},
00078 {ARG_INTS, "efsn", "0", "last lev0 fsn# to process. 0=error must be given by build_lev1_mgr"},
00079 {ARG_INTS, "quicklook", "1", "1=quick look, 0 = definitive mode"},
00080 {ARG_FLAG, "v", "0", "verbose flag"},
00081 {ARG_FLAG, "h", "0", "help flag"},
00082 {ARG_FLAG, "r", "0", "restart flag"},
00083 {ARG_STRING, kDpath, kDpathDef, "path to the source code tree (the JSOC root directory)"},
00084 {ARG_END}
00085 };
00086
00087 CmdParams_t cmdparams;
00088
00089 char *module_name = "build_lev1_hmi";
00090
00091 FILE *h1logfp;
00092
00093
00094
00095 static LEV0LEV1 lev0lev1;
00096 static LEV0LEV1 *l0l1 = &lev0lev1;
00097
00098 static DRMS_Record_t *rs;
00099 static DRMS_Record_t *rs0, *rs1, *rsff, *rsbad_pix, *rec_bad_aia;
00100 static DRMS_Record_t *rptr;
00101 static DRMS_Segment_t *segment;
00102 static DRMS_Segment_t *segmentff;
00103 static DRMS_Segment_t *darkseg;
00104 static DRMS_Segment_t *badseg;
00105 static DRMS_Segment_t *badoutpixseg;
00106 static DRMS_Segment_t *spikeseg;
00107 static DRMS_Array_t *segArray;
00108 static DRMS_RecordSet_t *rset0, *rset1, *rsetff, *rsbad_aia;
00109 static DRMS_Array_t *Array0;
00110 static DRMS_Array_t *Arrayff;
00111 static DRMS_Array_t *ArrayDark;
00112 static DRMS_Array_t *ArrayBad;
00113 static DRMS_Array_t *ArraySpike;
00114 static TIME sdo_epoch;
00115 static PTINFO *ptinfo = NULL;
00116 static PTINFO ptdata;
00117 static char bld_vers[16];
00118 static char datestr[32];
00119 static char open_dsname[256];
00120 static char dsffname[128];
00121 static char open_aiabad_dsname[256];
00122 static char path[DRMS_MAXPATHLEN], bad_pix_path[DRMS_MAXPATHLEN];
00123 static char bad_aia_path[DRMS_MAXPATHLEN];
00124 static char rs1_path[DRMS_MAXPATHLEN];
00125 static struct timeval first[NUMTIMERS], second[NUMTIMERS];
00126 static char *orbseries = "sdo.fds_orbit_vectors";
00127
00128
00129 static int nspikes, respike, fid, aiftsid, *oldvalues, *spikelocs, *newvalues;
00130 static int hcftid, aiagp6;
00131 static short aifcps;
00132 double aiascale = 1.0;
00133
00134 IORBIT_Info_t *IOinfo = NULL;
00135 IORBIT_Info_t IOdata;
00136 LIBASTRO_Error_t IOstatus;
00137 unsigned int fsnarray[NUMRECLEV1];
00138 unsigned int fsnx = 0;
00139
00140
00141 float data1[MAXPIXELS];
00142 float ftmp;
00143 int data1A[MAXPIXELS];
00144 int array_cosmic[16777216];
00145 double tgttimes[NUMRECLEV1];
00146
00147 long long brec, erec, bfsn, efsn;
00148 long long bnumx, enumx;
00149 int verbose;
00150 int hmiaiaflg = 0;
00151 int modeflg = 0;
00152 int imagecnt = 0;
00153 int restartflg = 0;
00154 int abortflg = 0;
00155 int sigalrmflg = 0;
00156 int ignoresigalrmflg = 0;
00157 int quicklook;
00158
00159 int flatmiss[NUMRECLEV1];
00160 int orbmiss[NUMRECLEV1];
00161 int asdmiss[NUMRECLEV1];
00162 int mpdmiss[NUMRECLEV1];
00163 int limbmiss[NUMRECLEV1];
00164 int noimage[NUMRECLEV1];
00165 int missflg[NUMRECLEV1];
00166
00167 char logname[128];
00168 char argdsin[128], argdsout[128], arglogfile[128], arginstru[80];
00169 char argbx[80], argex[80], argquick[80], argmode[80], argdsaiabad[80];
00170 char timetag[32];
00171 char tlmseriesname[128];
00172 char lev0seriesname[128];
00173 char *username;
00174 char *logfile;
00175 char *instru;
00176 char *mode;
00177 char *dsin;
00178 char *dsout;
00179 char *dsff;
00180 char *dsaiabad;
00181
00183 typedef struct {
00184 float rsun_lf;
00185 float x0_lf;
00186 float y0_lf;
00187 } LIMB_SOMETHING;
00188
00189
00190
00191 int get_nspikes() { return nspikes; }
00192 int get_respike(void) { return respike; }
00193 int *get_spikelocs() { return spikelocs; }
00194 int *get_oldvalues() { return oldvalues; }
00195 int *get_newvalues() { return newvalues; }
00196 void set_nspikes(int new_nspikes) { nspikes = new_nspikes; }
00197 void set_spikelocs(int *new_spikelocs) { spikelocs = new_spikelocs; }
00198 void set_oldvalues(int *new_oldvalues) { oldvalues = new_oldvalues; }
00199 void set_newvalues(int *new_newvalues) { newvalues = new_newvalues; }
00200
00201
00202 void do_quallev1(DRMS_Record_t *rs0, DRMS_Record_t *rs1, int inx, unsigned int fsn)
00203 {
00204 int quallev1 = 0;
00205 int rstatus;
00206 char *pchar;
00207
00208 quallev1 = missflg[inx];
00209 if(flatmiss[inx]) quallev1 = quallev1 | Q_NOFLAT;
00210 if(orbmiss[inx]) quallev1 = quallev1 | Q_NOORB;
00211 if(limbmiss[inx]) quallev1 = quallev1 | Q_NOLIMB;
00212 if(asdmiss[inx]) {
00213 quallev1 = quallev1 | Q_NOASD;
00214 drms_setkey_string(rs1, "ASD_REC", DRMS_MISSING_STRING);
00215 }
00216 if(mpdmiss[inx]) quallev1 = quallev1 | Q_NOMPD;
00217 if(noimage[inx]) quallev1 = quallev1 | Q_MISSALL;
00218 if(ptinfo) {
00219 ptdata = ptinfo[inx];
00220 if(strcmp(ptdata.acs_mode, "SCIENCE")) {
00221 quallev1 = quallev1 | Q_NOACS_SCI;
00222 }
00223 if(strcmp(ptdata.acs_eclp, "NO")) {
00224 quallev1 = quallev1 | Q_ACS_ECLP;
00225 }
00226 if(strcmp(ptdata.acs_sunp, "YES")) {
00227 quallev1 = quallev1 | Q_ACS_SUNP;
00228 }
00229 if(strcmp(ptdata.acs_safe, "NO")) {
00230 quallev1 = quallev1 | Q_ACS_SAFE;
00231 }
00232 }
00233 pchar = drms_getkey_string(rs0, "IMG_TYPE", &rstatus);
00234 if(rstatus) {
00235 printk("ERROR: in drms_getkey_string(IMG_TYPE) fsn=%u\n", fsn);
00236 }
00237 else {
00238 if(strcmp(pchar, "LIGHT")) {
00239 quallev1 = quallev1 | Q_IMG_TYPE;
00240 }
00241 }
00242 if(hmiaiaflg) {
00243 pchar = drms_getkey_string(rs0, "AISTATE", &rstatus);
00244 if(aiftsid >= 0xc000) quallev1 = quallev1 | Q_CAL_IMG;
00245 if((aifcps <= -20) ||(aifcps >=100)) quallev1 = quallev1 | Q_AIA_FOOR;
00246 if(aiagp6 != 0) quallev1 = quallev1 | Q_AIA_REGF;
00247 }
00248 else {
00249 pchar = drms_getkey_string(rs0, "HWLTNSET", &rstatus);
00250 if((fid >= 1) && (fid <=9999)) {
00251 quallev1 = quallev1 | Q_CAL_IMG;
00252 }
00253 if(hcftid == CAL_HCFTID) quallev1 = quallev1 | Q_CALM_IMG;
00254 }
00255 if(rstatus) {
00256 printk("ERROR: in drms_getkey_string(HWLTNSET or AISTATE) fsn=%u\n", fsn);
00257 }
00258 else {
00259 if(!strcmp(pchar, "OPEN")) {
00260 quallev1 = quallev1 | Q_LOOP_OPEN;
00261 }
00262 }
00263
00264 if(quicklook) {
00265 quallev1 = quallev1 | Q_NRT;
00266 }
00267 drms_setkey_int(rs1, "QUALITY", quallev1);
00268 drms_setkey_string(rs1, "BLD_VERS", bld_vers);
00269 }
00270
00271 int nice_intro ()
00272 {
00273 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00274 if (usage)
00275 {
00276 printf ("Usage:\nbuild_lev1_hmi [-vhr] "
00277 "mode=<recnum|fsn> instru=<hmi|aia> dsin=<lev0> dsout=<lev1>\n"
00278 "brec=<rec#>|bfsn=<fsn#> erec=<rec#>|efsn=<fsn#>\n"
00279 "quicklook=<0|1> [logfile=<file>]\n"
00280 " -h: help - show this message then exit\n"
00281 " -v: verbose\n"
00282 " -r: restart. only used when we restart our selves periodically\n"
00283 "mode= recnum: brec and erec have the record # range to process \n"
00284 " fsn: bfsn and efsn have the fsn # range to process\n"
00285 " For safety, the mode and arg name used must be consistent\n"
00286 "instru= instrument. must be 'hmi' or 'aia'\n"
00287 "dsin= data set name of lev0 input\n"
00288 " default hmi=hmi.lev0e aia=aia.lev0e\n"
00289 "dsout= data set name of lev1 output\n"
00290 " default hmi=su_production.hmi_lev1e aia=su_production.aia_lev1e\n"
00291 "dsff= data set name of AIA flat field series\n"
00292 " default aia_test.flatfield\n"
00293 "dsaiabad= AIA bad pixel series, default is to use BPL from dsff,\n"
00294 "but bugs need to be fixed\n"
00295 "brec= first lev0 rec# to process. 0=error must be given by build_lev1_mgr\n"
00296 "erec= last lev0 rec# to process. 0=error must be given by build_lev1_mgr\n"
00297 "bfsn= first fsn# to process. 0=error must be given by build_lev1_mgr\n"
00298 "efsn= last fsn# to process. 0=error must be given by build_lev1_mgr\n"
00299 "quicklook= 1 = quicklook mode, 0 = definitive mode\n"
00300 "logfile= optional log file name. If not given uses:\n"
00301 " /usr/local/logs/lev1/build_lev1_hmi.<time_stamp>.log\n");
00302 return(1);
00303 }
00304 verbose = cmdparams_get_int (&cmdparams, "v", NULL);
00305 restartflg = cmdparams_get_int (&cmdparams, "r", NULL);
00306 return (0);
00307 }
00308
00309 TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss)
00310 {
00311 static int firstcall = 1;
00312 if (firstcall)
00313 {
00314 firstcall = 0;
00315 }
00316
00317 return(sdo_epoch + (TIME)sdo_s + (TIME)(sdo_ss & 0xFFFF)/65536.0);
00318 }
00319
00320
00321 char *do_datestr() {
00322 time_t tval;
00323 struct tm *t_ptr;
00324
00325 tval = time(NULL);
00326 t_ptr = localtime(&tval);
00327 sprintf(datestr, "%d.%02d.%02d_%02d:%02d:%02d",
00328 (t_ptr->tm_year+1900), (t_ptr->tm_mon+1),
00329 t_ptr->tm_mday, t_ptr->tm_hour, t_ptr->tm_min, t_ptr->tm_sec);
00330 return(datestr);
00331 }
00332
00333
00334 char *gettimetag()
00335 {
00336 struct timeval tvalr;
00337 struct tm *t_ptr;
00338
00339 gettimeofday(&tvalr, NULL);
00340 t_ptr = localtime((const time_t *)&tvalr);
00341 sprintf(timetag, "%04d.%02d.%02d.%02d%02d%02d",
00342 (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);
00343 return(timetag);
00344 }
00345
00346
00347 void BeginTimer(int n)
00348 {
00349 gettimeofday (&first[n], NULL);
00350 }
00351
00352 float EndTimer(int n)
00353 {
00354 gettimeofday (&second[n], NULL);
00355 if (first[n].tv_usec > second[n].tv_usec) {
00356 second[n].tv_usec += 1000000;
00357 second[n].tv_sec--;
00358 }
00359 return (float) (second[n].tv_sec-first[n].tv_sec) +
00360 (float) (second[n].tv_usec-first[n].tv_usec)/1000000.0;
00361 }
00362
00363
00364 int h1log(const char *fmt, ...)
00365 {
00366 va_list args;
00367 char string[32768];
00368
00369 va_start(args, fmt);
00370 vsprintf(string, fmt, args);
00371 if(h1logfp) {
00372 fprintf(h1logfp, string);
00373 fflush(h1logfp);
00374 }
00375 else {
00376 printf(string);
00377 fflush(stdout);
00378 }
00379 va_end(args);
00380 return(0);
00381 }
00382
00383 int send_mail(char *fmt, ...)
00384 {
00385 va_list args;
00386 char string[1024], cmd[1024];
00387
00388 va_start(args, fmt);
00389 vsprintf(string, fmt, args);
00390 sprintf(cmd, "echo \"%s\" | Mail -s \"build_lev1_hmi mail\" lev0_user", string);
00391 system(cmd);
00392 va_end(args);
00393 return(0);
00394 }
00395
00396
00397 void abortit(int stat)
00398 {
00399 printk("***Abort in progress ...\n");
00400 printk("**Exit build_lev1_hmi w/ status = %d\n", stat);
00401 if (h1logfp) fclose(h1logfp);
00402 exit(stat);
00403 }
00404
00406 int rdout_mode_correct()
00407 {
00408 return(0);
00409 }
00410
00412 int orbit_calc()
00413 {
00414 return(0);
00415 }
00416
00417
00418 #include "do_flat.c"
00419 #include "do_patch.c"
00420 #include "get_image_location.c"
00421 #include "limb_fit_function.c"
00422 #include "cosmic_ray.c"
00423 #include "heightformation.c"
00424
00425
00426 int do_ingest(long long bbrec, long long eerec, const char *dpath)
00427 {
00428
00429 Image_Location *p_imageloc;
00430 Image_Location imageloc[NUMRECLEV1];
00431 TIME t_obs0;
00432 TIME tobs[NUMRECLEV1];
00433 float percentd;
00434 float cdelt1, rsun, crpix1, crpix2, crota2;
00435 double rsun_lf, x0_lf, y0_lf;
00436 int rstatus, dstatus, lstatus, ncnt, fcnt, i, j, k, qualint, nobs;
00437 int hshiexp, hcamid, nbad, n_cosmic;
00438 int *spikedata, status, axes[2], nbytes;
00439 uint32_t missvals, totvals;
00440 long long recnum0, recnum1, recnumff;
00441 char recrange[128], lev0name[128], flatrec[128];
00442
00443
00444 if(modeflg) sprintf(recrange, ":#%lld-#%lld", bbrec, eerec);
00445 else sprintf(recrange, "%lld-%lld", bbrec, eerec);
00446 sprintf(open_dsname, "%s[%s]", dsin, recrange);
00447 printk("open_dsname = %s\n", open_dsname);
00448 printk("#levnum recnum fsn\n");
00449
00450 rset0 = drms_open_records(drms_env, open_dsname, &rstatus);
00451 if(!rset0 || (rset0->n == 0) || rstatus) {
00452 printk("Can't do drms_open_records(%s)\n", open_dsname);
00453 return(1);
00454 }
00455 drms_stage_records(rset0, 1, 0);
00456 ncnt = rset0->n;
00457 rptr = (DRMS_Record_t *)malloc(ncnt * sizeof(DRMS_Record_t));
00458 if(rptr == NULL) {
00459 printk("Can't malloc() for DRMS_Record_t sort\n");
00460 return(1);
00461 }
00462
00463 for(i=0; i < ncnt; i++) {
00464 memcpy(&rptr[i], rset0->records[i], sizeof(DRMS_Record_t));
00465 }
00466
00467 qsort(rptr, ncnt, sizeof(DRMS_Record_t), &compare_rptr);
00468
00469
00470
00471 for(i=0; i < ncnt; i++) {
00472 flatmiss[i] = 0;
00473 orbmiss[i] = 0;
00474 limbmiss[i] = 0;
00475 asdmiss[i] = 0;
00476 mpdmiss[i] = 0;
00477 noimage[i] = 0;
00478 missflg[i] = 0;
00479 rs0 = &rptr[i];
00480 recnum0 = rs0->recnum;
00481
00482 fsnx = drms_getkey_int(rs0, "FSN", &rstatus);
00483 fsnarray[i] = fsnx;
00484 printk("*0 %u %u\n", recnum0, fsnx);
00485
00486 tobs[i] = drms_getkey_time(rs0, "t_obs", &rstatus);
00487 if(rstatus) {
00488 printk("Error on drms_getkey_time() fsn=%u. Use DRMS_MISSING_TIME\n",
00489 fsnx);
00490 tobs[i] = DRMS_MISSING_TIME;
00491 }
00492 }
00493 if(rstatus = get_pointing_info(drms_env, tobs, ncnt, &ptinfo)) {
00494 printk("**ERROR: get_pointing_info() status = %d fsn tobs ASD:\n",
00495 rstatus);
00496 for(j=0; j < ncnt; j++) {
00497 printk("%u %10.5f ", fsnarray[j], tobs[j]);
00498 if(ptinfo) {
00499 ptdata = ptinfo[j];
00500 printk("%s\n", ptdata.asd_rec);
00501 }
00502 asdmiss[j] = 1;
00503 }
00504
00505 }
00506 if ((IOstatus = iorbit_getinfo(drms_env,
00507 orbseries,
00508 NULL,
00509 IORBIT_Alg_Quadratic,
00510 tobs,
00511 ncnt,
00512 kIORBIT_CacheAction_DontCache,
00513 &IOinfo)) != kLIBASTRO_Success)
00514 {
00515 if(IOstatus == kLIBASTRO_InsufficientData) {
00516 printk("***ERROR in iorbit_getinfo: kLIBASTRO_InsufficientData\n");
00517 }
00518 else {
00519 printk("***ERROR in iorbit_getinfo() status=%d\n", IOstatus);
00520 }
00521 for(j=0; j < ncnt; j++) {
00522 orbmiss[j] = 1;
00523 }
00524 return(1);
00525 }
00526 rset1 = drms_create_records(drms_env, ncnt, dsout, DRMS_PERMANENT,&dstatus);
00527 if(dstatus) {
00528 printk("**ERROR: Can't create records for %s\n", dsout);
00529 for(j=0; j < ncnt; j++) {
00530 noimage[j] = 1;
00531 }
00532 return(1);
00533 }
00534
00535 for(i=0; i < ncnt; i++) {
00536 rs0 = &rptr[i];
00537 imageloc[i].tobs = tobs[i];
00538 imageloc[i].camera = drms_getkey_int(rs0, "CAMERA", &rstatus);
00539 if(rstatus) {
00540 printk("ERROR: in drms_getkey_int(CAMERA) fsn=%u\n", fsnarray[i]);
00541 }
00542 imageloc[i].wavelength = drms_getkey_int(rs0, "WAVELNTH", &rstatus);
00543 if(rstatus) {
00544 printk("ERROR: in drms_getkey_int(WAVELNTH) fsn=%u\n", fsnarray[i]);
00545 }
00546 snprintf(imageloc[i].telescope, 10, "%s",
00547 drms_getkey_string(rs0, "TELESCOP", &rstatus));
00548 if(rstatus) {
00549 printk("ERROR: in drms_getkey_string(TELESCOP) fsn=%u\n", fsnarray[i]);
00550 }
00551 }
00552 p_imageloc = imageloc;
00553 rstatus = get_image_location(drms_env, ncnt, &p_imageloc);
00554 if(rstatus) {
00555 printk("ERROR: get_image_location() returns status=%d\n", rstatus);
00556 for(j=0; j < ncnt; j++) {
00557 mpdmiss[i] = 0;
00558 }
00559 return(1);
00560 }
00561
00562 for(i=0; i < ncnt; i++) {
00563
00564 rs0 = &rptr[i];
00565 recnum0 = rs0->recnum;
00566 fsnx = fsnarray[i];
00567 sprintf(lev0name, "%s[%u]", dsin, fsnx);
00568 if(drms_getkey_int(rs0, "QUALITY", 0) < 0) {
00569 printk("Bad QUALITY for %s, no lev1 made\n", lev0name);
00570 noimage[i] = 1;
00571
00572 }
00573 segment = drms_segment_lookupnum(rs0, 0);
00574 Array0 = drms_segment_read(segment, DRMS_TYPE_SHORT, &rstatus);
00575 if(!Array0) {
00576 printk("Can't do drms_segment_read() %s status=%d\n",
00577 lev0name, rstatus);
00578 noimage[i] = 1;
00579 return(1);
00580 continue;
00581 }
00582 l0l1->adata0 = (short *)Array0->data;
00583 l0l1->rs0 = rs0;
00584 l0l1->recnum0 = recnum0;
00585 l0l1->fsn = fsnx;
00586 if(hmiaiaflg) {
00587 l0l1->dat1.adata1A = &data1A;
00588
00589 l0l1->himgcfid = 90;
00590 aiftsid = drms_getkey_int(rs0, "AIFTSID", &rstatus);
00591 aifcps = drms_getkey_short(rs0, "AIFCPS", &rstatus);
00592 aiagp6 = drms_getkey_int(rs0, "AIAGP6", &rstatus);
00593 }
00594 else {
00595
00596
00597 if (NEED_PATCH1(fsnx))
00598 do_patch1(l0l1->adata0);
00599
00600 l0l1->dat1.adata1 = &data1;
00601 l0l1->himgcfid = drms_getkey_int(rs0, "HIMGCFID", &rstatus);
00602 }
00603 if(rstatus) {
00604 printk("Can't do drms_getkey_int(HIMGCFID) for fsn %u\n", fsnx);
00606 l0l1->himgcfid = 104;
00607
00608
00609 }
00610
00611 sprintf(open_dsname, "%s[%u]", dsout, fsnx);
00612 rs = rset1->records[i];
00613 drms_record_directory(rs, rs1_path, 0);
00614 if(!*rs1_path) {
00615 printk("***ERROR: No path to segment for %s\n", open_dsname);
00616 noimage[i] = 1;
00617 continue;
00618 }
00619
00620 dstatus = drms_setkey_int(rs, "FSN", fsnx);
00621 dstatus = drms_setkey_string(rs, "LEV0SERIES", lev0name);
00622 if(!(segment = drms_segment_lookup(rs, "image_lev1"))) {
00623 printk("No drms_segment_lookup(rs, image_lev1) for %s\n", open_dsname);
00624 noimage[i] = 1;
00625 continue;
00626 }
00627 if(hmiaiaflg) {
00628 segArray = drms_array_create(DRMS_TYPE_INT,
00629 segment->info->naxis,
00630 segment->axis,
00631 &data1A,
00632 &dstatus);
00633 }
00634 else {
00635 segArray = drms_array_create(DRMS_TYPE_FLOAT,
00636 segment->info->naxis,
00637 segment->axis,
00638 &data1,
00639 &dstatus);
00640 }
00641
00642 rstatus = drms_copykeys(rs, rs0, 0, kDRMS_KeyClass_Explicit);
00643 if(rstatus != DRMS_SUCCESS) {
00644 printk("Error %d in drms_copykeys() for fsn %u\n", fsnx);
00645 return(1);
00646 continue;
00647 }
00648 qualint = drms_getkey_int(rs0, "QUALITY", &rstatus);
00649 drms_setkey_int(rs, "QUALLEV0", qualint);
00650 fid = drms_getkey_int(rs0, "FID", &rstatus);
00651
00652 drms_setkey_time(rs, "T_OBS", tobs[i]);
00653 printk("t_obs for lev0 = %10.5f\n", tobs[i]);
00654 drms_setkey_double(rs, "DATE", CURRENT_SYSTEM_TIME);
00655 if(ptinfo) {
00656 ptdata = ptinfo[i];
00657 drms_setkey_float(rs, "SAT_Y0", ptdata.sat_y0);
00658 drms_setkey_float(rs, "SAT_Z0", ptdata.sat_z0);
00659 drms_setkey_float(rs, "SAT_ROT", ptdata.sat_rot);
00660 drms_setkey_string(rs, "ACS_MODE", ptdata.acs_mode);
00661 drms_setkey_string(rs, "ACS_ECLP", ptdata.acs_eclp);
00662 drms_setkey_string(rs, "ACS_SUNP", ptdata.acs_sunp);
00663 drms_setkey_string(rs, "ACS_SAFE", ptdata.acs_safe);
00664 drms_setkey_string(rs, "ASD_REC", ptdata.asd_rec);
00665 drms_setkey_string(rs, "ACS_CGT", ptdata.acs_cgt);
00666 }
00667 if(IOinfo) {
00668 IOdata = IOinfo[i];
00669 drms_setkey_double(rs, "HAEX_OBS", IOdata.hciX);
00670 drms_setkey_double(rs, "HAEY_OBS", IOdata.hciY);
00671 drms_setkey_double(rs, "HAEZ_OBS", IOdata.hciZ);
00672 drms_setkey_double(rs, "GAEX_OBS", IOdata.gciX);
00673 drms_setkey_double(rs, "GAEY_OBS", IOdata.gciY);
00674 drms_setkey_double(rs, "GAEZ_OBS", IOdata.gciZ);
00675
00676 drms_setkey_double(rs, "DSUN_OBS", IOdata.dsun_obs);
00677 drms_setkey_double(rs, "OBS_VR", IOdata.obs_vr);
00678 drms_setkey_double(rs, "OBS_VW", IOdata.obs_vw);
00679 drms_setkey_double(rs, "OBS_VN", IOdata.obs_vn);
00680 drms_setkey_double(rs, "RSUN_OBS", IOdata.rsun_obs);
00681 drms_setkey_float(rs, "CRLN_OBS", (float)IOdata.crln_obs);
00682 drms_setkey_float(rs, "CRLT_OBS", (float)IOdata.crlt_obs);
00683 drms_setkey_float(rs, "HGLT_OBS", (float)IOdata.crlt_obs);
00684 drms_setkey_float(rs, "HGLN_OBS", 0.0);
00685 drms_setkey_int(rs, "CAR_ROT", (int)IOdata.car_rot);
00686 drms_setkey_string(rs, "ORB_REC", IOdata.orb_rec);
00687 }
00688 drms_setkey_float(rs, "X0_MP", imageloc[i].x);
00689 drms_setkey_float(rs, "Y0_MP", imageloc[i].y);
00690 drms_setkey_float(rs, "INST_ROT", imageloc[i].instrot);
00691
00692 drms_setkey_float(rs, "IMSCL_MP", imageloc[i].imscale);
00693 drms_setkey_string(rs, "MPO_REC", imageloc[i].mpo_rec);
00694
00695 int camera = drms_getkey_int(rs0, "CAMERA", &rstatus);
00696 if(rstatus) {
00697 printk("Can't do drms_getkey_int() for fsn %u\n", fsnx);
00698 noimage[i] = 1;
00699 goto TEMPSKIP;
00700
00701 }
00702
00703 if(drms_ismissing_time(tobs[i])) {
00704 printk("DRMS_MISSING_TIME for fsn=%u. Continue...\n", fsnx);
00705 noimage[i] = 1;
00706 goto TEMPSKIP;
00707 }
00708 if(!hmiaiaflg) {
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719 sprintf(open_dsname, "%s[? t_start <= %10.5f and t_stop > %10.5f and CAMERA=%d ?]",
00720 dsffname, tobs[i], tobs[i], camera);
00721
00722 }
00723 else {
00724 char *wavstr = drms_getkey_string(rs0, "WAVE_STR", &rstatus);
00725 if(rstatus) {
00726 printk("Can't do drms_getkey_string() for WAVE_STR\n");
00727 return(1);
00728 }
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739 sprintf(open_dsname, "%s[? t_start <= %10.5f and t_stop > %10.5f and WAVE_STR=%s ?]",
00740 dsffname, tobs[i], tobs[i], wavstr);
00741
00742 }
00743
00744 rsetff = drms_open_records(drms_env, open_dsname, &rstatus);
00745 if(!rsetff || (rsetff->n == 0) || rstatus) {
00746 printk("Can't do drms_open_records(%s)\n", open_dsname);
00747 flatmiss[i] = 1; noimage[i] = 1;
00748 goto TEMPSKIP;
00749 return(1);
00750 }
00751 fcnt = rsetff->n;
00752 if(fcnt > 1) {
00753 printk("More than one FF found for %s?\n", open_dsname);
00754 printk("Use last one of %d found\n", fcnt);
00755
00756 }
00757
00758 rsff = rsetff->records[fcnt-1];
00759 recnumff = rsff->recnum;
00760 sprintf(flatrec, "%s[:#%lld]", dsffname, recnumff);
00761 if(dstatus = drms_setkey_string(rs, "FLAT_REC", flatrec )) {
00762 printk("**ERROR on drms_setkey_string() for %s\n", flatrec);
00763 }
00764 drms_record_directory(rsff, path, 1);
00765 if(!*path) {
00766 printk("***ERROR: No path to segment for %s\n", open_dsname);
00767
00768 return(1);
00769 }
00770
00771 segmentff = drms_segment_lookup(rsff, "flatfield");
00772 Arrayff = drms_segment_read(segmentff, DRMS_TYPE_FLOAT, &rstatus);
00773 if(!Arrayff) {
00774 printk("Can't do drms_segment_read() for Flat Field status=%d\n",
00775 rstatus);
00776 return(1);
00777 }
00778 l0l1->adataff = (float *)Arrayff->data;
00779
00780 darkseg = drms_segment_lookup(rsff, "DARK");
00781 ArrayDark = drms_segment_read(darkseg, DRMS_TYPE_FLOAT, &rstatus);
00782 if(!ArrayDark) {
00783 printk("Can't do drms_segment_read() for DARK. status=%d\n", rstatus);
00784 return(1);
00785 }
00786 l0l1->adatadark = (float *)ArrayDark->data;
00787
00788 if (strcmp(dsaiabad, NOTSPECIFIED)) {
00789 char *wavstr = drms_getkey_string(rs0, "WAVE_STR", &rstatus);
00790 sprintf(open_aiabad_dsname, "%s[%s][]", dsaiabad, wavstr);
00791 rsbad_aia = drms_open_records(drms_env, open_aiabad_dsname, &rstatus);
00792 if(!rsbad_aia || rsbad_aia->n == 0 || rstatus)
00793 printk("drms_open_records for rsbad_aia failed\n");
00794 fcnt = rsbad_aia->n;
00795 if(fcnt > 1) {
00796 printk("More than one bpl found for %s?\n", open_aiabad_dsname);
00797 }
00798 rec_bad_aia = rsbad_aia->records[fcnt-1];
00799 badseg = drms_segment_lookup(rec_bad_aia, "bad_pixel_list");
00800 } else {
00801 badseg = drms_segment_lookup(rsff, "BAD_PIXEL");
00802 }
00803 ArrayBad = drms_segment_read(badseg, DRMS_TYPE_INT, &rstatus);
00804 nbad = drms_array_size(ArrayBad)/sizeof(int);
00805 if(!ArrayBad) {
00806 printk("Can't do drms_segment_read() for BAD_PIXEL. status=%d\n",
00807 rstatus);
00808 return(1);
00809 }
00810 l0l1->adatabad = (int *)ArrayBad->data;
00812
00813
00814
00815
00816 if(!hmiaiaflg) {
00817 if(!(badoutpixseg = drms_segment_lookup(rs, "bad_pixel_list"))) {
00818 printk("No drms_segment_lookup(rs, bad_pixel_list) for lev1\n");
00819 return(1);
00820 }
00821 }
00822 else {
00823 if(!(badoutpixseg = drms_segment_lookup(rs, "bad_pixel"))) {
00824 printk("No drms_segment_lookup(rs, bad_pixel) for lev1\n");
00825 return(1);
00826 }
00827 }
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837 l0l1->rs1 = rs;
00838 l0l1->rsff = rsff;
00839 l0l1->recnum1 = rs->recnum;
00840 l0l1->darkflag = 0;
00841 hshiexp = drms_getkey_int(rs, "HSHIEXP", &rstatus);
00842 hcamid = drms_getkey_int(rs, "HCAMID", &rstatus);
00843 if(hmiaiaflg) {
00844 int aimgshce = drms_getkey_int(rs, "AIMGSHCE", &rstatus);
00845 if(aimgshce == 0) l0l1->darkflag = 1;
00846 if(rstatus = do_flat_aia(l0l1)) {
00847 printk("***ERROR in do_flat_aia() status=%d\n", rstatus);
00848 printf("***ERROR in do_flat_aia() status=%d\n", rstatus);
00849 flatmiss[i] = 1; noimage[i] = 1;
00850
00851 goto FLATERR;
00852 }
00853 }
00854 else {
00855 if(hshiexp == 0) l0l1->darkflag = 1;
00856
00857 if(rstatus = do_flat(l0l1)) {
00858 printk("***ERROR in do_flat() status=%d\n", rstatus);
00859 printf("***ERROR in do_flat() status=%d\n", rstatus);
00860 flatmiss[i] = 1; noimage[i] = 1;
00861
00862 goto FLATERR;
00863 }
00864
00865
00866 }
00867
00868
00869
00870
00871
00872 drms_setkey_float(rs, "OSCNMEAN", l0l1->oscnmean);
00873 drms_setkey_float(rs, "OSCNRMS", l0l1->oscnrms);
00874 drms_setkey_int(rs, "DATAMIN", l0l1->datamin);
00875 drms_setkey_int(rs, "DATAMAX", l0l1->datamax);
00876 drms_setkey_int(rs, "DATAMEDN", l0l1->datamedn);
00877 drms_setkey_float(rs, "DATAMEAN", l0l1->datamean);
00878 drms_setkey_float(rs, "DATARMS", l0l1->data_rms);
00879 drms_setkey_float(rs, "DATASKEW", l0l1->dataskew);
00880 drms_setkey_float(rs, "DATAKURT", l0l1->datakurt);
00881 drms_setkey_int(rs, "DATAVALS", l0l1->datavals);
00882 drms_setkey_int(rs, "MISSVALS", l0l1->missvals);
00883 missvals = (uint32_t)l0l1->missvals;
00884 totvals = (uint32_t)l0l1->datavals + missvals;
00885 drms_setkey_int(rs, "TOTVALS", (int)totvals);
00886 percentd = (float)((100.0 * (float)l0l1->datavals)/(float)totvals);
00887 drms_setkey_float(rs, "PERCENTD", percentd);
00888 if(missvals > 0) missflg[i] = missflg[i] | Q_1_MISS0;
00889 if(missvals > (uint32_t)(totvals * 0.01))
00890 missflg[i] = missflg[i] | Q_1_MISS1;
00891 if(missvals > (uint32_t)(totvals * 0.05))
00892 missflg[i] = missflg[i] | Q_1_MISS2;
00893 if(missvals > (uint32_t)(totvals * 0.25))
00894 missflg[i] = missflg[i] | Q_1_MISS3;
00895 if(l0l1->datavals == 0)
00896 missflg[i] = missflg[i] | Q_MISSALL;
00897 if(hmiaiaflg && nspikes) {
00898 if (spikeseg = drms_segment_lookup(rs,"spikes") ) {
00899 nbytes = nspikes*sizeof(int);
00900 axes[0] = nspikes;
00901 axes[1] = 3;
00902 spikedata = (int *) malloc (3*nspikes*sizeof(int));
00903 ArraySpike = drms_array_create(DRMS_TYPE_INT, 2, axes,
00904 (void *) spikedata, &status);
00905 memcpy((void *)spikedata, (void *)spikelocs, nbytes);
00906 memcpy((void *)(spikedata+nspikes), (void *)oldvalues, nbytes);
00907 memcpy((void *)(spikedata+2*nspikes), (void *)newvalues, nbytes);
00908 status = drms_segment_write(spikeseg, ArraySpike, 0);
00909 drms_free_array(ArraySpike);
00910 }
00911 }
00912 if(!hmiaiaflg) {
00913
00914 dstatus = cosmic_rays(rs, l0l1->dat1.adata1, l0l1->adatabad, nbad,
00915 array_cosmic, &n_cosmic, 4096, 4096);
00916
00917
00918 if(dstatus) {
00919 printk("ERROR: cosmic_rays() error=%d\n", dstatus);
00920 noimage[i] = 1;
00921 }
00922 else {
00923 drms_setkey_int(rs, "NBADPERM", nbad);
00924 drms_setkey_int(rs, "NBADTOT", n_cosmic);
00925 }
00926 if(nbad != n_cosmic) {
00927 nbytes = n_cosmic*sizeof(int);
00928 axes[0] = n_cosmic;
00929 spikedata = (int *)malloc(nbytes);
00930 ArraySpike = drms_array_create(DRMS_TYPE_INT, 1, axes,
00931 (void *)spikedata, &status);
00932 memcpy((void *)spikedata, (void *)array_cosmic, nbytes);
00933 status = drms_segment_write(badoutpixseg, ArraySpike, 0);
00934 drms_free_array(ArraySpike);
00935 }
00936 else {
00937 dstatus = drms_segment_write(badoutpixseg, ArrayBad, 0);
00938 }
00939 if (dstatus) {
00940 printk("ERROR: drms_segment_write error=%d for lev1 bad_pixel_list\n",
00941 dstatus);
00942 noimage[i] = 1;
00943 }
00944 }
00945 FLATERR:
00946 drms_close_records(rsetff, DRMS_FREE_RECORD);
00947 free(ArrayDark->data);
00948 free(Arrayff->data);
00949 free(ArrayBad->data);
00950
00951 TEMPSKIP:
00952 if(!hmiaiaflg) {
00953 segArray->type = DRMS_TYPE_FLOAT;
00954 segArray->bscale = 1.0;
00955 segArray->bzero = 0.0;
00956 }
00957 dstatus = drms_segment_write(segment, segArray, 0);
00958 if (dstatus) {
00959 printk("ERROR: drms_segment_write error=%d for fsn=%u\n", dstatus,fsnx);
00960 noimage[i] = 1;
00961 }
00962 recnum1 = rs->recnum;
00963 printk("*1 %u %u\n", recnum1, fsnx);
00964 free(Array0->data);
00965
00966 x0_lf = DRMS_MISSING_DOUBLE;
00967 y0_lf = DRMS_MISSING_DOUBLE;
00968 rsun_lf = DRMS_MISSING_DOUBLE;
00969
00970
00971
00972 int skiplimb = 0;
00973 hcftid = drms_getkey_int(rs, "HCFTID", &rstatus);
00974 if(hcftid == CAL_HCFTID) {
00975 printk("Cal mode image fsn=%u\n", fsnx);
00976 skiplimb = 1;
00977 }
00978 else {
00979 if(hshiexp == 0) {
00980 if(hcamid == 0 || hcamid == 1) {
00981 printk("Dark image fsn=%u\n", fsnx);
00982 skiplimb = 1;
00983 }
00984 }
00985 }
00986 lstatus = 1;
00987
00988
00989 if(!skiplimb && !hmiaiaflg) {
00990
00991 lstatus = limb_fit(rs,l0l1->dat1.adata1,&rsun_lf,&x0_lf,&y0_lf,4096,4096,0, dpath);
00992 if(lstatus) {
00993 printk("ERROR: limb_fit() %d error for fsn=%u\n", lstatus, fsnx);
00994
00995 limbmiss[i] = 1;
00996 }
00997 else {
00998
00999
01000
01001 drms_setkey_float(rs, "RSUN_LF", (float)rsun_lf);
01002 drms_setkey_float(rs, "R_SUN", (float)rsun_lf);
01003 drms_setkey_float(rs, "X0_LF", (float)x0_lf);
01004 drms_setkey_float(rs, "Y0_LF", (float)y0_lf);
01005 drms_setkey_float(rs, "CRVAL1", 0.0);
01006 drms_setkey_float(rs, "CRVAL2", 0.0);
01007 drms_setkey_string(rs, "CTYPE1", "HPLN-TAN");
01008 drms_setkey_string(rs, "CTYPE2", "HPLT-TAN");
01009 drms_setkey_string(rs, "CUNIT1", "arcsec");
01010 drms_setkey_string(rs, "CUNIT2", "arcsec");
01011 cdelt1 = (float)IOdata.rsun_obs/rsun_lf;
01012 drms_setkey_float(rs, "CDELT1", cdelt1);
01013 drms_setkey_float(rs, "CDELT2", cdelt1);
01014 crpix1 = (float)x0_lf + 1;
01015 crpix2 = (float)y0_lf + 1;
01016 drms_setkey_float(rs, "CRPIX1", crpix1);
01017 drms_setkey_float(rs, "CRPIX2", crpix2);
01018 rsun = (float)rsun_lf;
01019 crota2 = imageloc[i].instrot + ptdata.sat_rot;
01020 drms_setkey_float(rs, "CROTA2", crota2);
01021 goto WCSEND;
01022 }
01023 }
01024
01025
01026 int cond2 = 0;
01027 if(!strcmp(ptdata.acs_mode, "SCIENCE") || !strcmp(ptdata.acs_mode, DRMS_MISSING_STRING)) {
01028
01029 if(!skiplimb && !hmiaiaflg) {
01030 cond2 = 1;
01031 }
01032 if(hmiaiaflg) {
01033 cond2 = 1;
01034 if(hshiexp == 0) {
01035 if(hcamid == 0 || hcamid == 1) {
01036 cond2 = 0;
01037 }
01038 }
01039 }
01040 if(cond2) {
01041
01042
01043
01044
01045
01046
01047
01048
01049 if(cond2) {
01050 printf("Calling WCS condition 2\n");
01051 rsun = (float)IOdata.rsun_obs/imageloc[i].imscale;
01052 drms_setkey_float(rs, "R_SUN", rsun);
01053 drms_setkey_string(rs, "CTYPE1", "HPLN-TAN");
01054 drms_setkey_string(rs, "CTYPE2", "HPLT-TAN");
01055 drms_setkey_float(rs, "CRVAL1", 0.0);
01056 drms_setkey_float(rs, "CRVAL2", 0.0);
01057 drms_setkey_string(rs, "CUNIT1", "arcsec");
01058 drms_setkey_string(rs, "CUNIT2", "arcsec");
01059 cdelt1 = (float)imageloc[i].imscale;
01060 drms_setkey_float(rs, "CDELT1", cdelt1);
01061 drms_setkey_float(rs, "CDELT2", cdelt1);
01062 crpix1 = imageloc[i].x + 1;
01063 crpix2 = imageloc[i].y + 1;
01064 drms_setkey_float(rs, "CRPIX1", crpix1);
01065 drms_setkey_float(rs, "CRPIX2", crpix2);
01066 crota2 = imageloc[i].instrot + ptdata.sat_rot;
01067 drms_setkey_float(rs, "CROTA2", crota2);
01068 goto WCSEND;
01069 }
01070 }
01071
01072 }
01073
01074 int cond3 = 0;
01075 if(strcmp(ptdata.acs_mode, "SCIENCE")) {
01076 if(!hmiaiaflg && !skiplimb && lstatus) {
01077 cond3 = 1;
01078 }
01079 else {
01080 if(hmiaiaflg) {
01081 cond3 = 1;
01082 if(hshiexp == 0) {
01083 if(hcamid == 0 || hcamid == 1) {
01084 cond3 = 0;
01085 }
01086 }
01087 }
01088 }
01089 }
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100 if(cond3) {
01101 printf("Calling WCS condition 3\n");
01102 rsun = (float)IOdata.rsun_obs/imageloc[i].imscale;
01103 drms_setkey_float(rs, "R_SUN", rsun);
01104 drms_setkey_string(rs, "CTYPE1", "HPLN-TAN");
01105 drms_setkey_string(rs, "CTYPE2", "HPLT-TAN");
01106 drms_setkey_string(rs, "CUNIT1", "arcsec");
01107 drms_setkey_string(rs, "CUNIT2", "arcsec");
01108 drms_setkey_float(rs, "CRVAL1", 0.0);
01109 drms_setkey_float(rs, "CRVAL2", 0.0);
01110 cdelt1 = (float)imageloc[i].imscale;
01111 drms_setkey_float(rs, "CDELT1", cdelt1);
01112 drms_setkey_float(rs, "CDELT2", cdelt1);
01113
01114 if(hmiaiaflg) {
01115 crpix1 = imageloc[i].x + (ptdata.sat_y0 - imageloc[i].yinrtb)/imageloc[i].imscale + 1;
01116 crpix2 = imageloc[i].y + (ptdata.sat_z0 - imageloc[i].zinrtb)/imageloc[i].imscale + 1;
01117 }
01118 else {
01119 crpix1 = imageloc[i].x - (ptdata.sat_y0 - imageloc[i].yinrtb)/imageloc[i].imscale + 1;
01120 crpix2 = imageloc[i].y - (ptdata.sat_z0 - imageloc[i].zinrtb)/imageloc[i].imscale + 1;
01121 }
01122 drms_setkey_float(rs, "CRPIX1", crpix1);
01123 drms_setkey_float(rs, "CRPIX2", crpix2);
01124 crota2 = imageloc[i].instrot + ptdata.sat_rot;
01125 drms_setkey_float(rs, "CROTA2", crota2);
01126 goto WCSEND;
01127 }
01128
01129 int cond4 = 0;
01130 if(!hmiaiaflg && skiplimb) {
01131 cond4 = 1;
01132 }
01133 else {
01134 if(hshiexp == 0) {
01135 if(hcamid == 0 || hcamid == 1) {
01136 cond4 = 1;
01137 }
01138 }
01139 }
01140 if(cond4) {
01141 printf("Calling WCS condition 4\n");
01142 rsun = DRMS_MISSING_FLOAT;
01143 drms_setkey_float(rs, "R_SUN", rsun);
01144 drms_setkey_string(rs, "CTYPE1", "RAW");
01145 drms_setkey_string(rs, "CTYPE2", "RAW");
01146 drms_setkey_string(rs, "CUNIT1", DRMS_MISSING_STRING);
01147 drms_setkey_string(rs, "CUNIT2", DRMS_MISSING_STRING);
01148 drms_setkey_float(rs, "CRVAL1", DRMS_MISSING_FLOAT);
01149 drms_setkey_float(rs, "CRVAL2", DRMS_MISSING_FLOAT);
01150 cdelt1 = DRMS_MISSING_FLOAT;
01151 drms_setkey_float(rs, "CDELT1", cdelt1);
01152 drms_setkey_float(rs, "CDELT2", cdelt1);
01153 crpix1 = DRMS_MISSING_FLOAT;
01154 crpix2 = DRMS_MISSING_FLOAT;
01155 drms_setkey_float(rs, "CRPIX1", crpix1);
01156 drms_setkey_float(rs, "CRPIX2", crpix2);
01157 crota2 = imageloc[i].instrot + ptdata.sat_rot;
01158 drms_setkey_float(rs, "CROTA2", crota2);
01159 goto WCSEND;
01160 }
01161
01162 WCSEND:
01163 if(!hmiaiaflg && !lstatus) {
01164
01165
01166
01167 if(!(dstatus = heightformation(fid, IOdata.obs_vr, &cdelt1, &rsun, &crpix1, &crpix2, crota2))) {
01168 drms_setkey_float(rs, "CDELT1", cdelt1);
01169 drms_setkey_float(rs, "CDELT2", cdelt1);
01170 drms_setkey_float(rs, "R_SUN", rsun);
01171 drms_setkey_float(rs, "CRPIX1", crpix1);
01172 drms_setkey_float(rs, "CRPIX2", crpix2);
01173
01174 }
01175 else {
01176
01177 printk("ERROR: heightformation() returned error for FID=%d\n", fid);
01178 }
01179 }
01180 drms_setkey_int(rs, "CALVER32", 0x12);
01181
01182
01183
01184
01185
01186 do_quallev1(rs0, rs, i, fsnx);
01187
01188
01189
01190 }
01191
01192 drms_close_records(rset0, DRMS_FREE_RECORD);
01193 drms_close_records(rset1, DRMS_INSERT_RECORD);
01194 return(0);
01195 }
01196
01197 int compare_rptr(const void *a, const void *b)
01198 {
01199 TIME t1, t2;
01200 int rstatus;
01201 DRMS_Record_t *x=(DRMS_Record_t *)a, *y=(DRMS_Record_t *)b;
01202
01203 t1 = drms_getkey_time(x, "t_obs", &rstatus);
01204 if(rstatus) t1 = DRMS_MISSING_TIME;
01205 t2 = drms_getkey_time(y, "t_obs", &rstatus);
01206 if(rstatus) t2 = DRMS_MISSING_TIME;
01207 if(t1 < t2) return(-1);
01208 if(t1 > t2) return(1);
01209 return(0);
01210 }
01211
01212
01213 void setup()
01214 {
01215 FILE *fin;
01216 char string[128], cwdbuf[128], idstr[256], lfile[128];
01217 int tpid;
01218
01219 sdo_epoch = sscan_time("1958.01.01_00:00:00_TAI");
01220 do_datestr();
01221 printk_set(h1log, h1log);
01222 printk("%s\n", datestr);
01223 gethostname(idstr, 256);
01224 printf("Host: %s\n", idstr);
01225 printk("Host: %s\n", idstr);
01226 getcwd(cwdbuf, 126);
01227 sprintf(idstr, "Cwd: %s\nCall: ", cwdbuf);
01228 sprintf(string, "build_lev1_hmi started as pid=%d ppid=%d user=%s\n",
01229 getpid(), getppid(), username);
01230 strcat(idstr, string);
01231 printk("%s", idstr);
01232 printf("%s", idstr);
01233 if(restartflg) printk("-r ");
01234 sprintf(argmode, "mode=%s", mode);
01235 sprintf(arginstru, "instru=%s", instru);
01236 sprintf(argdsin, "dsin=%s", dsin);
01237 sprintf(argdsout, "dsout=%s", dsout);
01238 if(modeflg) {
01239 sprintf(argbx, "brec=%lld", brec);
01240 sprintf(argex, "erec=%lld", erec);
01241 }
01242 else {
01243 sprintf(argbx, "bfsn=%lld", bfsn);
01244 sprintf(argex, "efsn=%lld", efsn);
01245 }
01246 sprintf(argquick, "quicklook=%d", quicklook);
01247 sprintf(arglogfile, "logfile=%s", logname);
01248 printk("%s %s %s %s %s %s %s %s %s\n",
01249 argmode, arginstru, argdsin, argdsout, argbx, argex, argquick, arglogfile, argdsaiabad);
01250 printf("%s %s %s %s %s %s %s %s %s\n",
01251 argmode, arginstru, argdsin, argdsout, argbx, argex, argquick, arglogfile, argdsaiabad);
01252 if(!restartflg) {
01253
01254
01255 }
01256 sprintf(bld_vers, "%s", jsoc_version);
01257 sprintf(idstr, "ps -ef | grep %s", LEV1VIEWERNAME);
01258 fin = popen(idstr, "r");
01259 while(fgets(string, sizeof string, fin)) {
01260 if(!(strstr(string, "perl"))) continue;
01261 sscanf(string, "%s %d", idstr, &tpid);
01262 sprintf(lfile, "%s/build_lev1_hmi_restart_%d.touch", LEV1LOG_BASEDIR, tpid);
01263 sprintf(idstr, "/bin/touch %s", lfile);
01264 printk("%s\n", idstr);
01265 system(idstr);
01266 }
01267 umask(002);
01268
01269
01270
01271
01272
01273
01274 }
01275
01276
01277 int DoIt(void)
01278 {
01279 long long numofrecs, frec, lrec;
01280 int numrec, numofchunks, i;
01281 char line[80];
01282 const char *dpath = NULL;
01283
01284 if (nice_intro())
01285 return (0);
01286 if(!(username = (char *)getenv("USER"))) username = "nouser";
01287 instru = cmdparams_get_str(&cmdparams, "instru", NULL);
01288 if(strcmp(instru, "hmi") && strcmp(instru, "aia")) {
01289 printf("Error: instru=%s must be given as 'hmi' or 'aia'\n", instru);
01290 printk("Error: instru=%s must be given as 'hmi' or 'aia'\n", instru);
01291 return(0);
01292 }
01293 mode = cmdparams_get_str(&cmdparams, "mode", NULL);
01294 if(strcmp(mode, "recnum") && strcmp(mode, "fsn")) {
01295 printf("Error: mode= must be given as 'recnum' or 'fsn'\n");
01296 return(0);
01297 }
01298 if(!strcmp(instru, "aia")) hmiaiaflg = 1;
01299 if(!strcmp(mode, "recnum")) modeflg = 1;
01300 dsin = cmdparams_get_str(&cmdparams, "dsin", NULL);
01301 dsout = cmdparams_get_str(&cmdparams, "dsout", NULL);
01302 dsff = cmdparams_get_str(&cmdparams, "dsff", NULL);
01303 dsaiabad = cmdparams_get_str(&cmdparams, "dsaiabad", NULL);
01304 if (strcmp(dsaiabad, NOTSPECIFIED)) sprintf(argdsaiabad, "dsaiabad=%s", dsaiabad);
01305 else sprintf(argdsaiabad, " ");
01306 brec = cmdparams_get_int(&cmdparams, "brec", NULL);
01307 erec = cmdparams_get_int(&cmdparams, "erec", NULL);
01308 bfsn = cmdparams_get_int(&cmdparams, "bfsn", NULL);
01309 efsn = cmdparams_get_int(&cmdparams, "efsn", NULL);
01310 quicklook = cmdparams_get_int(&cmdparams, "quicklook", NULL);
01311
01312 dpath = cmdparams_get_str(&cmdparams, kDpath, NULL);
01313
01314
01315 if(modeflg) {
01316 if(brec == 0 || erec == 0) {
01317 fprintf(stderr, "brec and erec must be given for recnum mode. 0 not allowed\n");
01318 return(0);
01319 }
01320 if(brec > erec) {
01321 fprintf(stderr, "brec must be <= erec\n");
01322 return(0);
01323 }
01324 bnumx = brec;
01325 enumx = erec;
01326 }
01327 else {
01328 if(bfsn == 0 || efsn == 0) {
01329 fprintf(stderr, "bfsn and efsn must be given for fsn mode. 0 not allowed\n");
01330 return(0);
01331 }
01332 if(bfsn > efsn) {
01333 fprintf(stderr, "bfsn must be <= efsn\n");
01334 return(0);
01335 }
01336 bnumx = bfsn;
01337 enumx = efsn;
01338 }
01339 logfile = cmdparams_get_str(&cmdparams, "logfile", NULL);
01340 if (strcmp(dsin, NOTSPECIFIED) == 0) {
01341 if(hmiaiaflg == 0) dsin = LEV0SERIESNAMEHMI;
01342 else dsin = LEV0SERIESNAMEAIA;
01343 }
01344 if (strcmp(dsout, NOTSPECIFIED) == 0) {
01345 if(hmiaiaflg == 0) dsout = LEV1SERIESNAMEHMI;
01346 else dsout = LEV1SERIESNAMEAIA;
01347 }
01348 if(hmiaiaflg) {
01349 if (strcmp(dsff, NOTSPECIFIED)) sprintf(dsffname, "%s", dsff);
01350 else sprintf(dsffname, "%s", DSFFNAMEAIA);
01351 if(strstr(dsin, "hmi") || strstr(dsout, "hmi")) {
01352 printf("Warning: You said instru=aia but have 'hmi' in ds name?\n");
01353 printf("Do you want to abort this [y/n]? ");
01354 if(gets(line) == NULL) { return(0); }
01355 if(strcmp(line, "n")) { return(0); }
01356 }
01357 }
01358 else {
01359 sprintf(dsffname, "%s", DSFFNAMEHMI);
01360 if(strstr(dsin, "aia") || strstr(dsout, "aia")) {
01361 printf("Warning: You said instru=hmi but have 'aia' in ds name?\n");
01362 printf("Do you want to abort this [y/n]? ");
01363 if(gets(line) == NULL) { return(0); }
01364 if(strcmp(line, "n")) { return(0); }
01365 }
01366 }
01367 if (strcmp(logfile, NOTSPECIFIED) == 0) {
01368 sprintf(logname, H1LOGFILE, gettimetag());
01369 }
01370 else {
01371 sprintf(logname, "%s", logfile);
01372 }
01373 if(restartflg || LOGTEST) {
01374 if((h1logfp=fopen(logname, "a")) == NULL)
01375 fprintf(stderr, "**Can't open for append the log file %s\n", logname);
01376 }
01377 else {
01378 if((h1logfp=fopen(logname, "w")) == NULL)
01379 fprintf(stderr, "**Can't open the log file %s\n", logname);
01380 }
01381 setup();
01382 numofrecs = (enumx - bnumx) + 1;
01383 numrec = NUMRECLEV1;
01384 numofchunks = numofrecs/numrec;
01385 if((numofrecs % numrec) != 0) numofchunks++;
01386 lrec = bnumx-1;
01387 for(i = 0; i < numofchunks; i++) {
01388 frec = lrec+1; lrec = (frec + numrec)-1;
01389 if(lrec > enumx) lrec=enumx;
01390 if(do_ingest(frec, lrec, dpath)) {
01391 printf("build_lev1_hmi abort\nSee log: %s\n", logname);
01392 send_mail("build_lev1_hmi abort\nSee log: %s\n", logname);
01393 return(0);
01394 }
01395 }
01396 printf("build_lev1_hmi done last fsn=%u\n", fsnx);
01397 return(0);
01398 }