00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <jsoc_main.h>
00013 #include <cmdparams.h>
00014 #include <drms.h>
00015 #include <drms_names.h>
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <ctype.h>
00019 #include <strings.h>
00020 #include <sys/types.h>
00021 #include <sys/time.h>
00022 #include <sys/stat.h>
00023 #include <unistd.h>
00024 #include <printk.h>
00025 #include <astro.h>
00026 #include <fresize.h>
00027 #include <gapfill.h>
00028 #include "fftw3.h"
00029 #include "imgdecode.h"
00030 #include "lev0lev1.h"
00031 #include "quallev1.h"
00032
00033
00034 #define LEV1SERIESNAMEHMI "hmi.lev1"
00035 #define LEV1SERIESNAMEAIA "su_production.aia_lev1e" //temp test case
00036 #define DSFFNAMEHMI "hmi.flatfield"
00037
00038
00039 #define DSFFNAMEAIA "aia.flatfield"
00040
00041 #define LEV1LOG_BASEDIR "/usr/local/logs/lev1"
00042 #define H1LOGFILE "/usr/local/logs/lev1/build_lev1_empty.%s.log"
00043 #define NUMTIMERS 8 //number of seperate timers avail
00044 #define NOTSPECIFIED "***NOTSPECIFIED***"
00045 #define LOGTEST 0
00046 #define CAL_HCFTID 17 //image is cal mode
00047
00048 int compare_rptr(const void *a, const void *b);
00049 static TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss);
00050
00051
00052 ModuleArgs_t module_args[] = {
00053 {ARG_STRING, "instru", NOTSPECIFIED, "instrument. either hmi or aia"},
00054 {ARG_STRING, "dsout", NOTSPECIFIED, "dataset of lev1 output"},
00055 {ARG_STRING, "argfile", NOTSPECIFIED, "file name with FSN args from lev1_def_gui_called"},
00056 {ARG_STRING, "logfile", NOTSPECIFIED, "optional log file name. Will create one if not given"},
00057 {ARG_INTS, "bfsn", "0", "first lev1 fsn# to process. 0=error"},
00058 {ARG_INTS, "efsn", "0", "last lev1 fsn# to process. 0=error"},
00059 {ARG_FLAG, "v", "0", "verbose flag"},
00060 {ARG_FLAG, "h", "0", "help flag"},
00061 {ARG_END}
00062 };
00063
00064 CmdParams_t cmdparams;
00065
00066 char *module_name = "build_lev1_empty";
00067
00068 FILE *h1logfp;
00069 static LEV0LEV1 lev0lev1;
00070 static LEV0LEV1 *l0l1 = &lev0lev1;
00071 static DRMS_Record_t *rs;
00072 static DRMS_Record_t *rs0, *rs1, *rsff, *rsbad_pix, *rec_bad_aia;
00073 static DRMS_Record_t *rptr;
00074 static DRMS_Segment_t *segment;
00075 static DRMS_Segment_t *segmentff;
00076 static DRMS_Segment_t *darkseg;
00077 static DRMS_Segment_t *badseg;
00078 static DRMS_Segment_t *badoutpixseg;
00079 static DRMS_Segment_t *spikeseg;
00080 static DRMS_Array_t *segArray;
00081 static DRMS_RecordSet_t *rset0, *rset1, *rsetff, *rsbad_aia;
00082 static DRMS_Array_t *Array0;
00083 static DRMS_Array_t *Arrayff;
00084 static DRMS_Array_t *ArrayDark;
00085 static DRMS_Array_t *ArrayBad;
00086 static DRMS_Array_t *ArraySpike;
00087 static TIME sdo_epoch;
00088
00089
00090 static char bld_vers[16];
00091 static char datestr[32], datestrZ[32];
00092 static char open_dsname[256];
00093 static char dsffname[128];
00094 static char path[DRMS_MAXPATHLEN], bad_pix_path[DRMS_MAXPATHLEN];
00095 static char bad_aia_path[DRMS_MAXPATHLEN];
00096 static char rs1_path[DRMS_MAXPATHLEN];
00097 static struct timeval first[NUMTIMERS], second[NUMTIMERS];
00098 static char *orbseries = "sdo.fds_orbit_vectors";
00099
00100
00101 static int nspikes, respike, fid, aiftsid, *oldvalues, *spikelocs, *newvalues;
00102 static int hcftid, aiagp6;
00103 static short aifcps;
00104 double aiascale = 1.0;
00105
00106 IORBIT_Info_t *IOinfo = NULL;
00107 IORBIT_Info_t IOdata;
00108 LIBASTRO_Error_t IOstatus;
00109 unsigned int fsnarray[NUMRECLEV1];
00110 unsigned int fsnx = 0;
00111
00112
00113 float data1[MAXPIXELS];
00114 float ftmp;
00115 int data1A[MAXPIXELS];
00116 int array_cosmic[16777216];
00117 double tgttimes[NUMRECLEV1];
00118
00119 unsigned int bfsn, efsn;
00120 unsigned int bnumx, enumx;
00121 int verbose;
00122 int hmiaiaflg = 0;
00123 int imagecnt = 0;
00124 int restartflg = 0;
00125 int abortflg = 0;
00126 int sigalrmflg = 0;
00127 int ignoresigalrmflg = 0;
00128
00129 int flatmiss[NUMRECLEV1];
00130 int orbmiss[NUMRECLEV1];
00131 int asdmiss[NUMRECLEV1];
00132 int mpdmiss[NUMRECLEV1];
00133 int limbmiss[NUMRECLEV1];
00134 int noimage[NUMRECLEV1];
00135 int missflg[NUMRECLEV1];
00136
00137 char logname[128];
00138 char argdsout[128], arglogfile[128], arginstru[80], argargfile[128];
00139 char argbx[80], argex[80];
00140 char timetag[32];
00141 char tlmseriesname[128];
00142 char lev0seriesname[128];
00143 char *username;
00144 char *logfile;
00145 char *argfile;
00146 char *instru;
00147 char *dsout;
00148 char *dsff;
00149
00150
00151 int get_nspikes() { return nspikes; }
00152 int get_respike(void) { return respike; }
00153 int *get_spikelocs() { return spikelocs; }
00154 int *get_oldvalues() { return oldvalues; }
00155 int *get_newvalues() { return newvalues; }
00156 void set_nspikes(int new_nspikes) { nspikes = new_nspikes; }
00157 void set_spikelocs(int *new_spikelocs) { spikelocs = new_spikelocs; }
00158 void set_oldvalues(int *new_oldvalues) { oldvalues = new_oldvalues; }
00159 void set_newvalues(int *new_newvalues) { newvalues = new_newvalues; }
00160
00161 int nice_intro ()
00162 {
00163 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00164 if (usage)
00165 {
00166 printf ("Usage:\nbuild_lev1_empty [-vh] "
00167 "instru=<hmi|aia> dsout=<lev1>\n"
00168 "bfsn=<fsn#> efsn=<fsn#>\n"
00169 "argfile=<file>\n"
00170 "[logfile=<file>]\n"
00171 " -h: help - show this message then exit\n"
00172 " -v: verbose\n"
00173 "instru= instrument. must be 'hmi' or 'aia'\n"
00174 "dsout= data set name of lev1 output\n"
00175 " default hmi=su_production.hmi_lev1e aia=su_production.aia_lev1e\n"
00176 "bfsn= first fsn# to process. 0=error must be given by build_lev1_mgr\n"
00177 "efsn= last fsn# to process. 0=error must be given by build_lev1_mgr\n"
00178 "argfile= file with FSN args from lev1_def_gui_called\n"
00179 "logfile= optional log file name. If not given uses:\n"
00180 " /usr/local/logs/lev1/build_lev1_empty.<time_stamp>.log\n");
00181 return(1);
00182 }
00183 verbose = cmdparams_get_int (&cmdparams, "v", NULL);
00184
00185 return (0);
00186 }
00187
00188 TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss)
00189 {
00190 static int firstcall = 1;
00191 if (firstcall)
00192 {
00193 firstcall = 0;
00194 }
00195
00196 return(sdo_epoch + (TIME)sdo_s + (TIME)(sdo_ss & 0xFFFF)/65536.0);
00197 }
00198
00199
00200 char *do_datestr() {
00201 time_t tval;
00202 struct tm *t_ptr;
00203
00204 tval = time(NULL);
00205 t_ptr = localtime(&tval);
00206 sprintf(datestr, "%d.%02d.%02d_%02d:%02d:%02d",
00207 (t_ptr->tm_year+1900), (t_ptr->tm_mon+1),
00208 t_ptr->tm_mday, t_ptr->tm_hour, t_ptr->tm_min, t_ptr->tm_sec);
00209 return(datestr);
00210 }
00211
00212
00213 char *do_datestrZ() {
00214 time_t tval;
00215 struct tm *t_ptr;
00216
00217 tval = time(NULL);
00218 t_ptr = gmtime(&tval);
00219 sprintf(datestrZ, "%d-%02d-%02dT%02d:%02d:%02dZ",
00220 (t_ptr->tm_year+1900), (t_ptr->tm_mon+1),
00221 t_ptr->tm_mday, t_ptr->tm_hour, t_ptr->tm_min, t_ptr->tm_sec);
00222 return(datestrZ);
00223 }
00224
00225
00226 char *gettimetag()
00227 {
00228 struct timeval tvalr;
00229 struct tm *t_ptr;
00230
00231 gettimeofday(&tvalr, NULL);
00232 t_ptr = localtime((const time_t *)&tvalr);
00233 sprintf(timetag, "%04d.%02d.%02d.%02d%02d%02d",
00234 (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);
00235 return(timetag);
00236 }
00237
00238
00239 void BeginTimer(int n)
00240 {
00241 gettimeofday (&first[n], NULL);
00242 }
00243
00244 float EndTimer(int n)
00245 {
00246 gettimeofday (&second[n], NULL);
00247 if (first[n].tv_usec > second[n].tv_usec) {
00248 second[n].tv_usec += 1000000;
00249 second[n].tv_sec--;
00250 }
00251 return (float) (second[n].tv_sec-first[n].tv_sec) +
00252 (float) (second[n].tv_usec-first[n].tv_usec)/1000000.0;
00253 }
00254
00255
00256 int h1log(const char *fmt, ...)
00257 {
00258 va_list args;
00259 char string[32768];
00260
00261 va_start(args, fmt);
00262 vsprintf(string, fmt, args);
00263 if(h1logfp) {
00264 fprintf(h1logfp, string);
00265 fflush(h1logfp);
00266 }
00267 else {
00268 printf(string);
00269 fflush(stdout);
00270 }
00271 va_end(args);
00272 return(0);
00273 }
00274
00275 int send_mail(char *fmt, ...)
00276 {
00277 va_list args;
00278 char string[1024], cmd[1024];
00279
00280 va_start(args, fmt);
00281 vsprintf(string, fmt, args);
00282 sprintf(cmd, "echo \"%s\" | Mail -s \"build_lev1_empty mail\" lev0_user", string);
00283 system(cmd);
00284 va_end(args);
00285 return(0);
00286 }
00287
00288
00289 void abortit(int stat)
00290 {
00291 printk("***Abort in progress ...\n");
00292 printk("**Exit build_lev1_empty w/ status = %d\n", stat);
00293 if (h1logfp) fclose(h1logfp);
00294 exit(stat);
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 int do_ingest(unsigned int bbrec, unsigned int eerec)
00307 {
00308 int rstatus, dstatus, lstatus, ncnt, fcnt, i, j, k, qualint, nobs;
00309
00310 ncnt = (eerec - bbrec) + 1;
00311 rset1 = drms_create_records(drms_env, ncnt, dsout, DRMS_PERMANENT,&dstatus);
00312 if(dstatus) {
00313 printk("**ERROR: Can't create records for %s\n", dsout);
00314 for(j=0; j < ncnt; j++) {
00315 noimage[j] = 1;
00316 }
00317 return(1);
00318 }
00319 for(i=0; i < ncnt; i++) {
00320 rs = rset1->records[i];
00321 dstatus = drms_setkey_int(rs, "FSN", bbrec++);
00322 dstatus = drms_setkey_string(rs, "LEV0SERIES", "N/A");
00323 dstatus = drms_setkey_int(rs, "QUALITY", Q_MISSALL);
00324 dstatus = drms_setkey_string(rs, "BLD_VERS", bld_vers);
00325 dstatus = drms_setkey_time(rs, "DATE", time(0) + UNIX_EPOCH);
00326 }
00327 drms_close_records(rset1, DRMS_INSERT_RECORD);
00328 return(0);
00329 }
00330
00331
00332 void setup()
00333 {
00334 FILE *fin;
00335 char string[128], cwdbuf[128], idstr[256], lfile[128];
00336 int tpid;
00337
00338 sdo_epoch = sscan_time("1958.01.01_00:00:00_TAI");
00339 do_datestr();
00340 printk_set(h1log, h1log);
00341 printk("%s\n", datestr);
00342 gethostname(idstr, 256);
00343 printf("Host: %s\n", idstr);
00344 printk("Host: %s\n", idstr);
00345 getcwd(cwdbuf, 126);
00346 sprintf(idstr, "Cwd: %s\nCall: ", cwdbuf);
00347 sprintf(string, "build_lev1_empty started as pid=%d ppid=%d user=%s\n",
00348 getpid(), getppid(), username);
00349 strcat(idstr, string);
00350 printk("%s", idstr);
00351 printf("%s", idstr);
00352
00353 sprintf(arginstru, "instru=%s", instru);
00354 sprintf(argdsout, "dsout=%s", dsout);
00355 sprintf(argbx, "bfsn=%u", bfsn);
00356 sprintf(argex, "efsn=%u", efsn);
00357 sprintf(arglogfile, "logfile=%s", logname);
00358 sprintf(argargfile, "argfile=%s", argfile);
00359 printk("%s %s %s %s %s %s\n",
00360 arginstru, argdsout, argbx, argex, argargfile, arglogfile);
00361 printf("%s %s %s %s %s %s\n",
00362 arginstru, argdsout, argbx, argex, argargfile, arglogfile);
00363 if(!restartflg) {
00364
00365
00366 }
00367 sprintf(bld_vers, "%s", jsoc_version);
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 umask(002);
00381 }
00382
00383
00384 int DoIt(void)
00385 {
00386 FILE *fplog;
00387 long long numofrecs, frec, lrec;
00388 int numrec, numofchunks, i;
00389 unsigned int lowfsn, highfsn;
00390 char line[128], scr[80];
00391
00392 if (nice_intro())
00393 return (0);
00394 if(!(username = (char *)getenv("USER"))) username = "nouser";
00395 instru = cmdparams_get_str(&cmdparams, "instru", NULL);
00396 if(strcmp(instru, "hmi") && strcmp(instru, "aia")) {
00397 printf("Error: instru=%s must be given as 'hmi' or 'aia'\n", instru);
00398 printk("Error: instru=%s must be given as 'hmi' or 'aia'\n", instru);
00399 return(0);
00400 }
00401 if(!strcmp(instru, "aia")) hmiaiaflg = 1;
00402 dsout = cmdparams_get_str(&cmdparams, "dsout", NULL);
00403 bfsn = cmdparams_get_int(&cmdparams, "bfsn", NULL);
00404 efsn = cmdparams_get_int(&cmdparams, "efsn", NULL);
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417 logfile = cmdparams_get_str(&cmdparams, "logfile", NULL);
00418 argfile = cmdparams_get_str(&cmdparams, "argfile", NULL);
00419 if (strcmp(dsout, NOTSPECIFIED) == 0) {
00420 fprintf(stderr, "You must specify dsout\n");
00421 return(0);
00422 }
00423 if (strcmp(argfile, NOTSPECIFIED) == 0) {
00424 fprintf(stderr, "You must specify argfile\n");
00425 return(0);
00426 }
00427 if (strcmp(logfile, NOTSPECIFIED) == 0) {
00428 sprintf(logname, H1LOGFILE, gettimetag());
00429 }
00430 else {
00431 sprintf(logname, "%s", logfile);
00432 }
00433 if((h1logfp=fopen(logname, "w")) == NULL)
00434 fprintf(stderr, "**Can't open the log file %s\n", logname);
00435 setup();
00436
00437 if((fplog=fopen(argfile, "r")) == NULL) {
00438 fprintf(stderr, "Can't open %s\n", argfile);
00439 return(0);
00440 }
00441
00442
00443
00444 while(fgets(line, 128, fplog)) {
00445 if (strstr(line, "Missing FSN:")) {
00446 sscanf(line, "%s %s %s %s %u %s %u", scr, scr, scr, scr, &lowfsn, scr, &highfsn);
00447 printf("lowfsn=%u highfsn=%u\n", lowfsn, highfsn);
00448 if(do_ingest(lowfsn+1, highfsn-1)) {
00449 printf("build_lev1_empty abort\nSee log: %s\n", logname);
00450 send_mail("build_lev1_empty abort\nSee log: %s\n", logname);
00451 return(0);
00452 }
00453 }
00454 }
00455 fclose(fplog);
00456 printf("build_lev1_empty done\n");
00457 return(0);
00458 }