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