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