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