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