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