00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <jsoc_main.h>
00033 #include <cmdparams.h>
00034 #include <drms.h>
00035 #include <drms_names.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <ctype.h>
00039 #include <strings.h>
00040 #include <errno.h>
00041 #include <sys/types.h>
00042 #include <sys/time.h>
00043 #include <sys/stat.h>
00044 #include <dirent.h>
00045 #include <unistd.h>
00046 #include <printk.h>
00047 #include <math.h>
00048 #include "packets.h"
00049
00050 #include "imgdecode_hmiaia.h"
00051 #include "decode_hk_vcdu.h"
00052 #include "decode_hk.h"
00053 #include "load_hk_config_files.h"
00054 #include "add_small_image.c"
00055 #include "mypng.h"
00056 #include "tdsignals.h"
00057 #include "quallev0.h"
00058
00059 #define RESTART_CNT 2 //#of tlm files to process before restart
00060
00061
00062
00063
00065
00066
00067
00068
00070
00071
00072
00073
00074 #define LEV0SERIESNAMEHMIGND "hmi_ground.lev0_dds"
00075 #define TLMSERIESNAMEHMIGND "hmi_ground.tlm_dds"
00076
00077
00078
00079
00081
00082
00083
00085
00086
00087
00088
00089 #define LEV0SERIESNAMEAIAGND "aia_ground.lev0_dds"
00090 #define TLMSERIESNAMEAIAGND "aia_ground.tlm_dds"
00091
00092
00093
00094
00095
00096
00097
00098
00099 #define TLMSERIESNAMEHMI "hmi.tlm"
00100 #define LEV0SERIESNAMEHMI "hmi.lev0a"
00101 #define LEV0SERIESNAMEAIA "aia.lev0"
00102 #define TLMSERIESNAMEAIA "aia.tlm"
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 #define H0LOGFILE "/usr/local/logs/lev0/ingest_lev0.%s.%s.%s.log"
00121 #define PKTSZ 1788 //size of VCDU pkt
00122 #define MAXFILES 65535 //max # of file can handle in tlmdir
00123 #define NUMTIMERS 8 //number of seperate timers avail
00124 #define IMAGE_NUM_COMMIT 12 //number of complete images until commit
00125
00126 #define TESTAPPID 0x199 //appid of test pattern packet
00127 #define TESTVALUE 0xc0b //first value in test pattern packet
00128 #define MAXERRMSGCNT 10 //max # of err msg before skip the tlm file
00129 #define NOTSPECIFIED "***NOTSPECIFIED***"
00130 #define ENVFILE "/home/prodtest/cvs/JSOC/proj/lev0/apps/SOURCE_ENV_FOR_HK_DECODE"
00131 #define ENVFILE_GND "/home/prodtest/cvs/JSOC/proj/lev0/apps/SOURCE_ENV_FOR_HK_DECODE_GROUND"
00132
00133 extern int decode_next_hk_vcdu(unsigned short *tbuf, CCSDS_Packet_t **hk, unsigned int *Fsn);
00134 extern int write_hk_to_drms();
00135 extern void HMI_compute_exposure_times(DRMS_Record_t *rec, HK_Keyword_t *isp, int flg);
00136 extern int set_HMI_mech_values(DRMS_Record_t *rec);
00137 static TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss);
00138
00139
00140 ModuleArgs_t module_args[] = {
00141 {ARG_STRING, "vc", NOTSPECIFIED, "Primary virt channel to listen to"},
00142 {ARG_STRING, "indir", NOTSPECIFIED, "directory containing the files to ingest"},
00143 {ARG_STRING, "outdir", NOTSPECIFIED, "directory to move the files to after the ingest"},
00144 {ARG_STRING, "pipedir", NOTSPECIFIED, "directory to get parc files from JSOC backend"},
00145 {ARG_STRING, "logfile", NOTSPECIFIED, "optional log file name. Will create one if not given"},
00146 {ARG_FLAG, "v", "0", "verbose flag"},
00147 {ARG_FLAG, "h", "0", "help flag"},
00148 {ARG_FLAG, "r", "0", "restart flag"},
00149 {ARG_FLAG, "l", "0", "wdlog flag"},
00150 {ARG_FLAG, "c", "0", "continue processing the current fsn after a imgdecode() error"},
00151 {ARG_END}
00152 };
00153
00154 CmdParams_t cmdparams;
00155
00156 char *module_name = "ingest_lev0";
00157
00158 typedef enum
00159 {
00160 kThreadSigErr_Success
00161 } ThreadSigErr_t;
00162 static int errskip=0;
00163 static int gLoop = 1;
00164 static pthread_mutex_t mutex;
00165 static struct timeval tv0;
00166 static td_alarm_t talarm = 0;
00167
00168 FILE *h0logfp;
00169 static IMG Image, ImageOld;
00170 static IMG *Img, *ImgO, *ImgC;
00171 static CCSDS_Packet_t *Hk;
00172 static DRMS_Record_t *rs;
00173 static DRMS_Segment_t *segment;
00174 static DRMS_Array_t *segArray;
00175 static DRMS_RecordSet_t *rset;
00176 static DRMS_Record_t *rs_old, *rsc;
00177 static DRMS_Segment_t *segmentc;
00178 static DRMS_Array_t *cArray, *oldArray;
00179 static TIME sdo_epoch;
00180 static char datestr[32];
00181 static char bld_vers[16];
00182 static struct timeval first[NUMTIMERS], second[NUMTIMERS];
00183
00184 int INVALtime;
00185 unsigned int fsn = 0;
00186 unsigned int fsnx = 0;
00187 unsigned int fsn_prev = 0;
00188 unsigned int fsn_pre_rexmit = 0;
00189 unsigned int fid = 0;
00190
00191 short *rdat;
00192 long long vcdu_seq_num;
00193 long long vcdu_seq_num_next;
00194 long long total_missing_im_pdu;
00195 unsigned int vcdu_24_cnt, vcdu_24_cnt_next;
00196 int verbose;
00197 int grounddata;
00198 int appid;
00199 int testid1, testid2;
00200 int hmiaiaflg;
00201 int whk_status;
00202 int total_tlm_vcdu;
00203 int total_missing_vcdu;
00204 int errmsgcnt, fileimgcnt;
00205 int cntsleeps = 0;
00206 int paused = 0;
00207 int imagecnt = 0;
00208 int restartflg = 0;
00209 int logflg = 0;
00210 int abortflg = 0;
00211 int conterr = 0;
00212 int sigalrmflg = 0;
00213 int ignoresigalrmflg = 0;
00214 int firstfound = 0;
00215 int rexmitmode = 0;
00216 int ALRMSEC = 60;
00217 int sleep_interval = 2;
00218 char logname[128];
00219 char argvc[32], argindir[96], arglogfile[96], argoutdir[96], argpipedir[96];
00220 char timetag[32];
00221 char pchan[8];
00222 char rchan[8];
00223 char stopfile[80];
00224 char tlmseriesname[128];
00225 char lev0seriesname[128];
00226 char tlmnamekey[128];
00227 char tlmnamekeyfirst[128];
00228 char oldtlmdsnam[128];
00229 char dbname[128];
00230 char *jdbname;
00231 char *username;
00232 char *tlmdir;
00233 char *outdir;
00234 char *pipedir;
00235 char *logfile;
00236 char *vc;
00237 struct stat stbuf;
00238 struct p_r_chans {
00239 char *pchan;
00240 char *rchan;
00241 int instru;
00242 };
00243 typedef struct p_r_chans P_R_CHANS;
00244
00245 P_R_CHANS p_r_chan_pairs[] = {
00246 {"VC01", "VC09", 1},
00247 {"VC04", "VC12", 1},
00248 {"VC02", "VC10", 0},
00249 {"VC05", "VC13", 0},
00250 {"n/a", "n/a"}
00251 };
00252
00253 struct namesort {
00254 char *name;
00255 };
00256 typedef struct namesort NAMESORT;
00257
00258
00259
00260
00261 void shandler(int sig, pthread_mutex_t *mtx)
00262 {
00263 float elapsed;
00264 struct timeval tv1;
00265 gettimeofday(&tv1, NULL);
00266
00267 elapsed = (float)((tv1.tv_sec * 1000000.0 + tv1.tv_usec -
00268 (tv0.tv_sec * 1000000.0 + tv0.tv_usec)) / 1000000.0);
00269
00270 printk("Thread '%lld' received alarm signal '%d'.\n",
00271 (long long )pthread_self(), sig);
00272 printk("Elapsed time is %f seconds.\n", elapsed);
00273
00274
00275 pthread_mutex_lock(mtx);
00276 sigalrmflg = 1;
00277 pthread_mutex_unlock(mtx);
00278 }
00279
00280 int nice_intro ()
00281 {
00282 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00283 if (usage)
00284 {
00285 printf ("Usage:\ningest_lev0_hmiaia [-vh] "
00286 "vc=<virt chan> indir=</dir> [outdir=</dir>] [pipedir=</dir>] [logfile=<file>]\n"
00287 " -h: help - show this message then exit\n"
00288 " -v: verbose\n"
00289
00290 " -l: keep logs under /dds (usually do for front end only)\n"
00291 " -c: continue processing the current fsn after a imgdecode() error\n"
00292 " -r: restart. only used when we restart our selves periodically\n"
00293 "vc= primary virt channel to listen to e.g. VC02\n"
00294 "indir= directory containing the files to ingest\n"
00295 "outdir= optional dir to copy the files to after the ingest\n"
00296 " Do not specify this when run in the backend (cl1n001) so rm can be done\n"
00297 "pipedir= optional dir to get .parc files from JSOC backend\n"
00298 "logfile= optional log file name. Will create one if not given\n");
00299 return(1);
00300 }
00301 verbose = cmdparams_get_int (&cmdparams, "v", NULL);
00302
00303 conterr = cmdparams_get_int (&cmdparams, "c", NULL);
00304 restartflg = cmdparams_get_int (&cmdparams, "r", NULL);
00305 logflg = cmdparams_get_int (&cmdparams, "l", 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)/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 h0log(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(h0logfp) {
00372 fprintf(h0logfp, string);
00373 fflush(h0logfp);
00374 } else {
00375 printf(string);
00376 fflush(stdout);
00377 }
00378 va_end(args);
00379 return(0);
00380 }
00381
00382 int send_mail(char *fmt, ...)
00383 {
00384 va_list args;
00385 char string[1024], cmd[1024];
00386
00387 va_start(args, fmt);
00388 vsprintf(string, fmt, args);
00389 sprintf(cmd, "echo \"%s\" | Mail -s \"ingest_lev0 mail\" jsoc_ops", string);
00390 system(cmd);
00391 va_end(args);
00392 return(0);
00393 }
00394
00395
00396 void do_quallev0(DRMS_Record_t *rs, IMG *img, int fsn)
00397 {
00398 char *hwltnset, *cdark;
00399 char *hseqerr, *aistate;
00400 char wave_str[16];
00401 int status, hsqfgsn, asqfsn;
00402 float percentd;
00403 uint32_t missvals, datav;
00404 uint32_t hcf1encd, hcf2encd, hps1encd, hps2encd, hps3encd;
00405 uint32_t hwt1encd, hwt2encd, hwt3encd, hwt4encd;
00406 uint32_t hcf1pos, hcf2pos, hpl1pos, hpl2pos, hpl3pos, hwl1pos, hwl2pos;
00407 uint32_t hwl3pos, hwl4pos;
00408 uint32_t aiawvlen, aifwen, aiasen;
00409 short aifiltyp;
00410 uint32_t quallev0 = 0;
00411 uint32_t qmiss = 0;
00412
00413 if(img->overflow) quallev0 = quallev0 | Q_OVFL;
00414 if(img->headerr) quallev0 = quallev0 | Q_HDRERR;
00415 if(img->nerrors) quallev0 = quallev0 | Q_CMPERR;
00416 if(img->last_pix_err) quallev0 = quallev0 | Q_LPXERR;
00417 if(img->reopened) quallev0 = quallev0 | Q_REOPENED;
00418 missvals = img->totalvals - img->datavals;
00419 if(missvals > 0) quallev0 = quallev0 | Q_MISS0;
00420 datav = img->totalvals;
00421 if(missvals > (uint32_t)(datav * 0.01)) quallev0 = quallev0 | Q_MISS1;
00422 if(missvals > (uint32_t)(datav * 0.05)) quallev0 = quallev0 | Q_MISS2;
00423 if(missvals > (uint32_t)(datav * 0.25)) quallev0 = quallev0 | Q_MISS3;
00424 if(missvals == datav) quallev0 = quallev0 | Q_MISSI;
00425 if(img->datavals == 0) quallev0 = quallev0 | Q_MISSALL;
00426
00427
00428
00429
00430
00431
00432
00433
00434 if(fsn == 469769216) quallev0 = quallev0 | Q_CORRUPT;
00435 if(INVALtime) quallev0 = quallev0 | Q_INVALTIME;
00436 if(cdark = drms_getkey_string(rs, "IMG_TYPE", &status)) {
00437 if(!strcmp(cdark, "DARK")) quallev0 = quallev0 | Q_DARK;
00438 free(cdark);
00439 }
00440 if(!hmiaiaflg) {
00441 hsqfgsn = drms_getkey_int(rs, "HSQFGSN", &status);
00442 if(status || (fsn != hsqfgsn)) quallev0 = quallev0 | Q_NOISP;
00443
00444
00445
00446
00447
00448 if(hwltnset = drms_getkey_string(rs, "HWLTNSET", &status)) {
00449 if(!strcmp(hwltnset, "OPEN")) quallev0 = quallev0 | Q_ISSOPEN;
00450 free(hwltnset);
00451 }
00452 hcf1encd = drms_getkey_int(rs, "HCF1ENCD", &status);
00453 hcf1pos = drms_getkey_int(rs, "HCF1POS", &status);
00454
00455 if(!((hcf1encd == hcf1pos) || (hcf1encd == hcf1pos-1)))
00456 quallev0 = quallev0 | Q_HCF1ENCD;
00457 hcf2encd = drms_getkey_int(rs, "HCF2ENCD", &status);
00458 hcf2pos = drms_getkey_int(rs, "HCF2POS", &status);
00459
00460 if(!((hcf2encd == hcf2pos) || (hcf2encd == hcf2pos-1)))
00461 quallev0 = quallev0 | Q_HCF2ENCD;
00462 hps1encd = drms_getkey_int(rs, "HPS1ENCD", &status);
00463 hpl1pos = drms_getkey_int(rs, "HPL1POS", &status);
00464 if(!((hpl1pos == hps1encd) || (hpl1pos == (hps1encd+1) % 240)))
00465 quallev0 = quallev0 | Q_HPS1ENCD;
00466 hps2encd = drms_getkey_int(rs, "HPS2ENCD", &status);
00467 hpl2pos = drms_getkey_int(rs, "HPL2POS", &status);
00468 if(!((hpl2pos == hps2encd) || (hpl2pos == (hps2encd+1) % 240)))
00469 quallev0 = quallev0 | Q_HPS2ENCD;
00470 hps3encd = drms_getkey_int(rs, "HPS3ENCD", &status);
00471 hpl3pos = drms_getkey_int(rs, "HPL3POS", &status);
00472 if(!((hpl3pos == hps3encd) || (hpl3pos == (hps3encd+1) % 240)))
00473 quallev0 = quallev0 | Q_HPS3ENCD;
00474 hwt1encd = drms_getkey_int(rs, "HWT1ENCD", &status);
00475 hwl1pos = drms_getkey_int(rs, "HWL1POS", &status);
00476 if(!((hwl1pos == hwt1encd) || (hwl1pos == (hwt1encd+1) % 240)))
00477 quallev0 = quallev0 | Q_HWT1ENCD;
00478 hwt2encd = drms_getkey_int(rs, "HWT2ENCD", &status);
00479 hwl2pos = drms_getkey_int(rs, "HWL2POS", &status);
00480 if(!((hwl2pos == hwt2encd) || (hwl2pos == (hwt2encd+1) % 240)))
00481 quallev0 = quallev0 | Q_HWT2ENCD;
00482 hwt3encd = drms_getkey_int(rs, "HWT3ENCD", &status);
00483 hwl3pos = drms_getkey_int(rs, "HWL3POS", &status);
00484 if(!((hwl3pos == hwt3encd) || (hwl3pos == (hwt3encd+1) % 240)))
00485 quallev0 = quallev0 | Q_HWT3ENCD;
00486 hwt4encd = drms_getkey_int(rs, "HWT4ENCD", &status);
00487 hwl4pos = drms_getkey_int(rs, "HWL4POS", &status);
00488 if(!((hwl4pos == hwt4encd) || (hwl4pos == (hwt4encd+1) % 240)))
00489 quallev0 = quallev0 | Q_HWT4ENCD;
00490 }
00491 else {
00492 asqfsn = drms_getkey_int(rs, "ASQFSN", &status);
00493 if(fsn != asqfsn) quallev0 = quallev0 | Q_NOISP;
00494 if(aistate = drms_getkey_string(rs, "AISTATE", &status)) {
00495 if(!strcmp(aistate, "OPEN")) quallev0 = quallev0 | AQ_ISSOPEN;
00496 free(aistate);
00497 }
00498 strcpy(wave_str, "UNKNOWN");
00499 aiawvlen = drms_getkey_int(rs, "AIAWVLEN", &status);
00500 aifiltyp = drms_getkey_short(rs, "AIFILTYP", &status);
00501 aifwen = drms_getkey_int(rs, "AIFWEN", &status);
00502 aiasen = drms_getkey_int(rs, "AIASEN", &status);
00503 switch(aiawvlen) {
00504 case 9:
00505 if(aifiltyp == 0) {
00506 if((aifwen != 269) && (aifwen != 270)) {
00507 quallev0 = quallev0 | A94Mech_Err;
00508 }
00509 }
00510 else if(aifiltyp == 1) {
00511 if((aifwen != 11) && (aifwen != 12)) {
00512 quallev0 = quallev0 | A94Mech_Err;
00513 }
00514 }
00515 else if(aifiltyp == 2) {
00516 if((aifwen != 74) && (aifwen != 75)) {
00517 quallev0 = quallev0 | A94Mech_Err;
00518 }
00519 }
00520
00521
00522 if((aifwen == 269) || (aifwen == 270)) {
00523 strcpy(wave_str, "94_THIN");
00524 }
00525 else if((aifwen == 11) || (aifwen == 12)) {
00526 strcpy(wave_str, "94_THICK");
00527 }
00528 else if((aifwen == 74) || (aifwen == 75)) {
00529 strcpy(wave_str, "94_OPEN");
00530 }
00531 break;
00532 case 1:
00533 if(aifiltyp == 0) {
00534 if((aifwen != 269) && (aifwen != 270)) {
00535 quallev0 = quallev0 | A131Mech_Err;
00536 }
00537 }
00538 else if(aifiltyp == 1) {
00539 if((aifwen != 11) && (aifwen != 12)) {
00540 quallev0 = quallev0 | A131Mech_Err;
00541 }
00542 }
00543 else if(aifiltyp == 2) {
00544 if((aifwen != 74) && (aifwen != 75)) {
00545 quallev0 = quallev0 | A131Mech_Err;
00546 }
00547 }
00548 if((aifwen == 269) || (aifwen == 270)) {
00549 strcpy(wave_str, "131_THIN");
00550 }
00551 else if((aifwen == 11) || (aifwen == 12)) {
00552 strcpy(wave_str, "131_THICK");
00553 }
00554 else if((aifwen == 74) || (aifwen == 75)) {
00555 strcpy(wave_str, "131_OPEN");
00556 }
00557 break;
00558 case 7:
00559 if(aifiltyp == 0) {
00560 if((aifwen != 203) && (aifwen != 204)) {
00561 quallev0 = quallev0 | A171Mech_Err;
00562 }
00563 }
00564 else if(aifiltyp == 1) {
00565 if((aifwen != 11) && (aifwen != 12)) {
00566 quallev0 = quallev0 | A171Mech_Err;
00567 }
00568 }
00569 else if(aifiltyp == 2) {
00570
00571 if((aifwen != 74) && (aifwen != 75)) {
00572 quallev0 = quallev0 | A171Mech_Err;
00573 }
00574 }
00575 if((aifwen == 203) || (aifwen == 204)) {
00576 strcpy(wave_str, "171_THIN");
00577 }
00578 else if((aifwen == 11) || (aifwen == 12)) {
00579 strcpy(wave_str, "171_THICK");
00580 }
00581 break;
00582
00583 case 3:
00584 if(aifiltyp == 0) {
00585 if(aiasen != 6) {
00586 quallev0 = quallev0 | A193Mech_Err;
00587 }
00588 if((aifwen != 269) && (aifwen != 270)) {
00589 quallev0 = quallev0 | A193Mech_Err;
00590 }
00591 }
00592 else if(aifiltyp == 1) {
00593 if(aiasen != 6) {
00594 quallev0 = quallev0 | A193Mech_Err;
00595 }
00596 if((aifwen != 11) && (aifwen != 12)) {
00597 quallev0 = quallev0 | A193Mech_Err;
00598 }
00599 }
00600 else if(aifiltyp == 2) {
00601 if(aiasen != 6) {
00602 quallev0 = quallev0 | A193Mech_Err;
00603 }
00604 if((aifwen != 74) && (aifwen != 75)) {
00605 quallev0 = quallev0 | A193Mech_Err;
00606 }
00607 }
00608 if(aiasen == 6) {
00609 if((aifwen == 269) || (aifwen == 270)) {
00610 strcpy(wave_str, "193_THIN");
00611 } else if((aifwen == 11) || (aifwen == 12)) {
00612 strcpy(wave_str, "193_THICK");
00613 } else if((aifwen == 74) || (aifwen == 75)) {
00614 strcpy(wave_str, "193_OPEN");
00615 }
00616 }
00617 else {
00618 if((aifwen == 269) || (aifwen == 270)) {
00619 strcpy(wave_str, "MIX_THIN");
00620 } else if((aifwen == 11) || (aifwen == 12)) {
00621 strcpy(wave_str, "MIX_THICK");
00622 } else if((aifwen == 74) || (aifwen == 75)) {
00623 strcpy(wave_str, "MIX_OPEN");
00624 }
00625 }
00626 break;
00627 case 2:
00628 if(aifiltyp == 0) {
00629 if(aiasen != 24) {
00630 quallev0 = quallev0 | A211Mech_Err;
00631 }
00632 if((aifwen != 203) && (aifwen != 204)) {
00633 quallev0 = quallev0 | A211Mech_Err;
00634 }
00635 }
00636 else if(aifiltyp == 1) {
00637 if(aiasen != 24) {
00638 quallev0 = quallev0 | A211Mech_Err;
00639 }
00640 if((aifwen != 137) && (aifwen != 138)) {
00641 quallev0 = quallev0 | A211Mech_Err;
00642 }
00643 }
00644 else if(aifiltyp == 2) {
00645 if(aiasen != 24) {
00646 quallev0 = quallev0 | A211Mech_Err;
00647 }
00648 if((aifwen != 74) && (aifwen != 75)) {
00649 quallev0 = quallev0 | A211Mech_Err;
00650 }
00651 }
00652 if(aiasen == 24) {
00653 if((aifwen == 203) || (aifwen == 204)) {
00654 strcpy(wave_str, "211_THIN");
00655 } else if((aifwen == 137) || (aifwen == 138)) {
00656 strcpy(wave_str, "211_THICK");
00657 } else if((aifwen == 74) || (aifwen == 75)) {
00658 strcpy(wave_str, "211_OPEN");
00659 }
00660 }
00661 else {
00662 if((aifwen == 203) || (aifwen == 204)) {
00663 strcpy(wave_str, "MIX_THIN");
00664 } else if((aifwen == 137) || (aifwen == 138)) {
00665 strcpy(wave_str, "MIX_THICK");
00666 } else if((aifwen == 74) || (aifwen == 75)) {
00667 strcpy(wave_str, "MIX_OPEN");
00668 }
00669 }
00670 break;
00671 case 8:
00672 if(aifiltyp == 0) {
00673 if((aifwen != 203) && (aifwen != 204)) {
00674 quallev0 = quallev0 | A304Mech_Err;
00675 }
00676 }
00677 else if(aifiltyp == 1) {
00678 if((aifwen != 137) && (aifwen != 138)) {
00679 quallev0 = quallev0 | A304Mech_Err;
00680 }
00681 }
00682 else if(aifiltyp == 2) {
00683 if((aifwen != 74) && (aifwen != 75)) {
00684 quallev0 = quallev0 | A304Mech_Err;
00685 }
00686 }
00687 if((aifwen == 203) || (aifwen == 204)) {
00688 strcpy(wave_str, "304_THIN");
00689 } else if((aifwen == 137) || (aifwen == 138)) {
00690 strcpy(wave_str, "304_THICK");
00691 } else if((aifwen == 74) || (aifwen == 75)) {
00692 strcpy(wave_str, "304_OPEN");
00693 }
00694 break;
00695 case 0:
00696 if(aifiltyp == 0) {
00697 if((aifwen != 203) && (aifwen != 204)) {
00698 quallev0 = quallev0 | A335Mech_Err;
00699 }
00700 }
00701 else if(aifiltyp == 1) {
00702 if((aifwen != 137) && (aifwen != 138)) {
00703 quallev0 = quallev0 | A335Mech_Err;
00704 }
00705 }
00706 else if(aifiltyp == 2) {
00707 if((aifwen != 74) && (aifwen != 75)) {
00708 quallev0 = quallev0 | A335Mech_Err;
00709 }
00710 }
00711 if((aifwen == 203) || (aifwen == 204)) {
00712 strcpy(wave_str, "335_THIN");
00713 } else if((aifwen == 137) || (aifwen == 138)) {
00714 strcpy(wave_str, "335_THICK");
00715 } else if((aifwen == 74) || (aifwen == 75)) {
00716 strcpy(wave_str, "335_OPEN");
00717 }
00718 break;
00719 case 4:
00720 if((aifwen != 269) && (aifwen != 270)) {
00721 quallev0 = quallev0 | A160Mech_Err;
00722 }
00723 else strcpy(wave_str, "1600");
00724 break;
00725 case 5:
00726 if((aifwen != 137) && (aifwen != 138)) {
00727 quallev0 = quallev0 | A170Mech_Err;
00728 }
00729 else strcpy(wave_str, "1700");
00730 break;
00731 case 6:
00732 if((aifwen != 74) && (aifwen != 75)) {
00733 quallev0 = quallev0 | A450Mech_Err;
00734 }
00735 else strcpy(wave_str, "4500");
00736 break;
00737 }
00738 if(!strcmp(wave_str, "UNKNOWN")) quallev0 = quallev0 | AQ_INVAL_WL;
00739 }
00740
00741 drms_setkey_int(rs, "QUALITY", quallev0);
00742 drms_setkey_string(rs, "BLD_VERS", bld_vers);
00743 drms_setkey_string(rs, "WAVE_STR", wave_str);
00744 percentd = (float)((100.0 * (float)img->datavals)/(float)img->totalvals);
00745 drms_setkey_float(rs, "PERCENTD", percentd);
00746 }
00747
00748
00749 void close_image(DRMS_Record_t *rs, DRMS_Segment_t *seg, DRMS_Array_t *array,
00750 IMG *img, int fsn)
00751 {
00752 STAT stat;
00753 int status, n, k;
00754 uint32_t missvals;
00755 long long cmdx;
00756 char tlmdsname[128];
00757
00758 printk("*Closing image for fsn = %u\n", fsn);
00759 if(imgstat(img, &stat)) {
00760 printk("**Error on imgstat() for fsn = %u\n", fsn);
00761 }
00762 else {
00763 drms_setkey_short(rs, "DATAMIN", stat.min);
00764 drms_setkey_short(rs, "DATAMAX", stat.max);
00765 drms_setkey_short(rs, "DATAMEDN", stat.median);
00766 drms_setkey_float(rs, "DATAMEAN", stat.mean);
00767 drms_setkey_float(rs, "DATARMS", stat.rms);
00768 drms_setkey_float(rs, "DATASKEW", stat.skew);
00769 drms_setkey_float(rs, "DATAKURT", stat.kurt);
00770 }
00771
00772 if(hmiaiaflg) {
00773 drms_setkey_int(rs, "CAMERA", (img->telnum)+1);
00774 cmdx = drms_getkey_longlong(rs, "AIMGSHCE", &status);
00775 if (0 == cmdx) drms_setkey_int(rs, "AIMGTYP", 1);
00776 }
00777 drms_setkey_int(rs, "IMGAPID", img->apid);
00778 drms_setkey_int(rs, "CROPID", img->cropid);
00779 if(drms_setkey_int(rs, "LUTID", img->luid)) {
00780 printk("ERROR on setkey_int for LUTID\n");
00781 }
00782 drms_setkey_int(rs, "TAPCODE", img->tap);
00783 n = (img->N) & 0x1F;
00784 n = n << 3;
00785 k = (img->K) & 0x07;
00786 if(img->N == 16) {
00787 drms_setkey_int(rs, "COMPID", 0);
00788 drms_setkey_int(rs, "BITSELID", 0);
00789 }
00790 else {
00791 drms_setkey_int(rs, "COMPID", n+k);
00792 drms_setkey_int(rs, "BITSELID", img->R);
00793 }
00794 drms_setkey_int(rs, "FID", img->fid);
00795 drms_setkey_int(rs, "TOTVALS", img->totalvals);
00796 drms_setkey_int(rs, "DATAVALS", img->datavals);
00797 drms_setkey_int(rs, "NPACKETS", img->npackets);
00798 drms_setkey_int(rs, "NERRORS", img->nerrors);
00799 drms_setkey_short(rs, "EOIERROR", img->last_pix_err);
00800 drms_setkey_short(rs, "HEADRERR", img->headerr);
00801 drms_setkey_short(rs, "OVERFLOW", img->overflow);
00802 missvals = img->totalvals - img->datavals;
00803 drms_setkey_int(rs, "MISSVALS", missvals);
00804 snprintf(tlmdsname, 128, "%s[%s]", tlmseriesname, tlmnamekeyfirst);
00805 drms_setkey_string(rs, "TLMDSNAM", tlmdsname);
00806 unsigned int pts = img->first_packet_time >> 16;
00807 int ptss = img->first_packet_time & 0xffff;
00808 TIME fpt = SDO_to_DRMS_time(pts, ptss);
00809 drms_setkey_double(rs, "IMGFPT", fpt);
00810 drms_setkey_double(rs, "DATE", CURRENT_SYSTEM_TIME);
00811 do_quallev0(rs, img, fsn);
00812
00813 status = drms_segment_write(seg, array, 0);
00814 if (status) {
00815 printk("ERROR: drms_segment_write error=%d for fsn=%u\n", status, fsn);
00816 }
00817 add_small_array(rs, array, 8, 16);
00818 array->data = NULL;
00819 drms_free_array(array);
00820 if((status = drms_close_record(rs, DRMS_INSERT_RECORD))) {
00821 printk("**ERROR: drms_close_record failed for %s fsn=%u\n",
00822 lev0seriesname, fsn);
00823 }
00824 img->initialized = 0;
00825
00826 }
00827
00828
00829 void abortit(int stat)
00830 {
00831 printk("***Abort in progress ...\n");
00832 printk("**Exit ingest_lev0_hmiaia w/ status = %d\n", stat);
00833 if (h0logfp) fclose(h0logfp);
00834 exit(stat);
00835 }
00836
00837
00838 int compare_names(const void *a, const void *b)
00839 {
00840 NAMESORT *x=(NAMESORT *)a, *y=(NAMESORT *)b;
00841 return(strcmp(x->name+4, y->name+4));
00842 }
00843
00844 unsigned short MDI_getshort (unsigned char *c)
00845 {
00846 unsigned short s = 0;
00847
00848 s = (unsigned short) *c++ << 8;
00849 s |= (unsigned short) *c;
00850 return s;
00851 }
00852
00853
00854 int fsn_normal_new_image()
00855 {
00856 int dstatus, i;
00857
00858
00859 Img->initialized = 0;
00860 Img->reopened = 0;
00861 errmsgcnt = 0;
00862 fsn_prev = fsnx;
00863 sprintf(tlmnamekeyfirst, "%s", tlmnamekey);
00864 rs = drms_create_record(drms_env, lev0seriesname, DRMS_PERMANENT, &dstatus);
00865 if(dstatus) {
00866 if(dstatus == DRMS_ERROR_SUMOPEN) {
00867 printk("**ERROR: DRMS can't open w/SUMS. Aborting...\n");
00868 abortit(4);
00869 }
00870 printk("**ERROR: Can't create record for %s fsn=%u\n", lev0seriesname, fsnx);
00871 return(1);
00872 }
00873 dstatus = drms_setkey_int(rs, "FSN", fsnx);
00874 if(!(segment = drms_segment_lookup(rs, "image"))) {
00875 printk("No drms_segment_lookup(rs, image)\n");
00876 return(1);
00877 }
00878
00879 #if 0
00880 Img->datavals = 0;
00881 Img->npackets = 0;
00882 Img->nerrors = 0;
00883 Img->last_pix_err = 0;
00884 Img->first_packet_time = UINT64_MAX;
00885 for (i = 0; i < MAXPIXELS; ++i)
00886 Img->dat[i] = BLANK;
00887 for (i = 0; i < MAXHIST; ++i)
00888 Img->hist[i] = 0;
00889 #endif
00890 imgdecode_init_hack(Img);
00891
00892 rdat = Img->dat;
00893 segArray = drms_array_create(DRMS_TYPE_SHORT,
00894 segment->info->naxis,
00895 segment->axis,
00896 rdat,
00897 &dstatus);
00898 ImgC = Img;
00899 return(0);
00900 }
00901
00902
00903
00904 int fsn_change_normal()
00905 {
00906 int dstatus, rstatus, compid, n, k;
00907 char reopen_dsname[256];
00908 char *cptr;
00909
00910 printk("*FSN has changed from %u to %u\n", fsn_prev, fsnx);
00911 if(fsn_prev == 0) {
00912 errmsgcnt = 0;
00913 errskip = 0;
00914 fsn_prev = fsnx;
00915 if(hmiaiaflg)
00916 sprintf(reopen_dsname, "%s[%u]", LEV0SERIESNAMEAIA, fsnx);
00917 else
00918 sprintf(reopen_dsname, "%s[%u]", LEV0SERIESNAMEHMI, fsnx);
00919 printk("Open normal prev ds: %s\n", reopen_dsname);
00920 rset = drms_open_records(drms_env, reopen_dsname, &rstatus);
00921 if(rstatus) {
00922 printk("Can't do drms_open_records(%s)\n", reopen_dsname);
00923 return(1);
00924 }
00925 if(!rset || (rset->n == 0) || rstatus) {
00926 printk("No prev ds\n");
00927 fsn_normal_new_image();
00928 }
00929 else {
00930 Img->initialized = 1;
00931 Img->reopened = 1;
00932 Img->fsn = fsnx;
00933 Img->apid = appid;
00934 rs_old = rset->records[0];
00935 rs = drms_clone_record(rs_old, DRMS_PERMANENT, DRMS_COPY_SEGMENTS, &rstatus);
00936 if(rstatus) {
00937 printk("Can't do drms_clone_record()\n");
00938 return(1);
00939 }
00940 drms_close_records(rset, DRMS_FREE_RECORD);
00941 rstatus = drms_setkey_int(rs, "FSN", fsnx);
00942 Img->telnum = drms_getkey_int(rs, "CAMERA", &rstatus);
00943 Img->telnum--;
00944 Img->cropid = drms_getkey_int(rs, "CROPID", &rstatus);
00945 Img->fid = drms_getkey_int(rs, "FID", &rstatus);
00946 Img->luid = drms_getkey_int(rs, "LUTID", &rstatus);
00947 if(rstatus) {
00948 Img->luid = 0;
00949 printk("ERROR on getkey_int for LUTID\n");
00950 }
00951 Img->tap = drms_getkey_int(rs, "TAPCODE", &rstatus);
00952 compid = drms_getkey_int(rs, "COMPID", &rstatus);
00953 if(compid == 0) {
00954 n = 16; k = 0;
00955 }
00956 else {
00957 k = compid & 0x07;
00958 n = compid >> 3;
00959 n = n & 0x1F;
00960 }
00961 Img->N = n;
00962 Img->K = k;
00963 Img->R = drms_getkey_int(rs, "BITSELID", &rstatus);
00964 Img->overflow = drms_getkey_int(rs, "OVERFLOW", &rstatus);
00965 Img->headerr = drms_getkey_int(rs, "HEADRERR", &rstatus);
00966 Img->totalvals = drms_getkey_int(rs, "TOTVALS", &rstatus);
00967 Img->datavals = drms_getkey_int(rs, "DATAVALS", &rstatus);
00968 Img->npackets = drms_getkey_int(rs, "NPACKETS", &rstatus);
00969 Img->nerrors = drms_getkey_int(rs, "NERRORS", &rstatus);
00970 Img->last_pix_err = drms_getkey_int(rs, "EOIERROR", &rstatus);
00971
00972 TIME fpt = drms_getkey_double(rs, "IMGFPT", &rstatus);
00973 Img->first_packet_time = round(65536.0*(fpt - sdo_epoch));
00974 if(Img->first_packet_time == 0)
00975 Img->first_packet_time = UINT64_MAX;
00976 snprintf(oldtlmdsnam, 128, "%s", drms_getkey_string(rs, "TLMDSNAM",&rstatus));
00977 cptr = strstr(oldtlmdsnam, "VC");
00978 if(cptr)
00979 snprintf(tlmnamekeyfirst, 23, "%s", cptr);
00980 else
00981 sprintf(tlmnamekeyfirst, "%s", "UNK");
00982 segment = drms_segment_lookupnum(rs, 0);
00983 cArray = drms_segment_read(segment, DRMS_TYPE_SHORT, &rstatus);
00984 if(rstatus) {
00985 printk("Can't do drms_segment_read()\n");
00986 return(1);
00987 }
00988 short *adata = (short *)cArray->data;
00989 memcpy(Img->dat, adata, 2*MAXPIXELS);
00990 rdat = Img->dat;
00991 segArray = drms_array_create(DRMS_TYPE_SHORT,
00992 segment->info->naxis,
00993 segment->axis,
00994 rdat,
00995 &dstatus);
00996 ImgC = Img;
00997 drms_free_array(cArray);
00998 }
00999 }
01000 else {
01001 if(errskip) {
01002 errskip = 0;
01003 printk("*Skip Closing image for fsn = %u\n", fsn_prev);
01004 }
01005 else {
01006 if(rs) {
01007 close_image(rs, segment, segArray, Img, fsn_prev);
01008
01009 if(imagecnt++ >= IMAGE_NUM_COMMIT) {
01010 printk("drms_server_end_transaction()\n");
01011 drms_server_end_transaction(drms_env, 0 , 0);
01012 printk("drms_server_begin_transaction()\n");
01013 drms_server_begin_transaction(drms_env);
01014 imagecnt = 0;
01015 }
01016 fileimgcnt++;
01017 }
01018 else {
01019 printk("**ERROR: Null record ptr for an opened image fsn=%u\n",fsn_prev);
01020 }
01021 }
01022 if(fsn_normal_new_image()) {
01023 printk("**ERROR: Can't create new image\n");
01024 return(1);
01025 }
01026 }
01027 return(0);
01028 }
01029
01030
01031
01032 int fsn_change_rexmit()
01033 {
01034 char rexmit_dsname[256];
01035 int rstatus, dstatus, compid, n, k;
01036 char *cptr;
01037
01038 if(fsn_prev != 0) {
01039 if(rsc) {
01040 close_image(rsc, segmentc, oldArray, ImgO, fsn_prev);
01041 imagecnt++;
01042 fileimgcnt++;
01043 }
01044 else {
01045 printk("**ERROR: Null record ptr for an rexmit image fsn=%u\n",fsn_prev);
01046 }
01047 }
01048 errmsgcnt = 0;
01049 fsn_prev = fsnx;
01050 if(hmiaiaflg)
01051 sprintf(rexmit_dsname, "%s[%u]", LEV0SERIESNAMEAIA, fsnx);
01052 else
01053 sprintf(rexmit_dsname, "%s[%u]", LEV0SERIESNAMEHMI, fsnx);
01054 printk("Open prev ds: %s\n", rexmit_dsname);
01055 rset = drms_open_records(drms_env, rexmit_dsname, &rstatus);
01056 if(rstatus) {
01057 printk("Can't do drms_open_records(%s)\n", rexmit_dsname);
01058 return(1);
01059 }
01060 if(!rset || (rset->n == 0) || rstatus) {
01061 startnew:
01062 printk("No prev ds\n");
01063 ImgO->initialized = 0;
01064 ImgO->reopened = 0;
01065 imgdecode_init_hack(ImgO);
01066 sprintf(tlmnamekeyfirst, "%s", tlmnamekey);
01067 rsc = drms_create_record(drms_env, lev0seriesname, DRMS_PERMANENT, &rstatus);
01068 if(rstatus) {
01069 if(rstatus == DRMS_ERROR_SUMOPEN) {
01070 printk("**ERROR: DRMS can't open w/SUMS. Aborting...\n");
01071 abortit(4);
01072 }
01073 printk("Can't create record for %s fsn=%u\n", lev0seriesname, fsnx);
01074 return(1);
01075 }
01076 rstatus = drms_setkey_int(rsc, "FSN", fsnx);
01077 if(!(segmentc = drms_segment_lookup(rsc, "image"))) {
01078 printk("No drms_segment_lookup(rsc, image)\n");
01079 return(1);
01080 }
01081 rdat = ImgO->dat;
01082 oldArray = drms_array_create(DRMS_TYPE_SHORT,
01083 segmentc->info->naxis,
01084 segmentc->axis,
01085 rdat,
01086 &dstatus);
01087 }
01088 else {
01089 ImgO->initialized = 1;
01090 ImgO->reopened = 1;
01091 ImgO->fsn = fsnx;
01092 ImgO->apid = appid;
01093 rs_old = rset->records[0];
01094 rsc = drms_clone_record(rs_old, DRMS_PERMANENT, DRMS_COPY_SEGMENTS, &rstatus);
01095 if(rstatus) {
01096 printk("Can't do drms_clone_record()\n");
01097 printk("Assume this was a temp ds and the segments are now gone...\n");
01098 goto startnew;
01099
01100 }
01101 drms_close_records(rset, DRMS_FREE_RECORD);
01102 rstatus = drms_setkey_int(rsc, "FSN", fsnx);
01103 ImgO->telnum = drms_getkey_int(rsc, "CAMERA", &rstatus);
01104 ImgO->telnum--;
01105 ImgO->cropid = drms_getkey_int(rsc, "CROPID", &rstatus);
01106 ImgO->fid = drms_getkey_int(rsc, "FID", &rstatus);
01107 ImgO->luid = drms_getkey_int(rsc, "LUTID", &rstatus);
01108 ImgO->tap = drms_getkey_int(rsc, "TAPCODE", &rstatus);
01109 compid = drms_getkey_int(rsc, "COMPID", &rstatus);
01110 if(compid == 0) {
01111 n = 16; k = 0;
01112 }
01113 else {
01114 k = compid & 0x07;
01115 n = compid >> 3;
01116 n = n & 0x1F;
01117 }
01118 ImgO->N = n;
01119 ImgO->K = k;
01120 ImgO->R = drms_getkey_int(rsc, "BITSELID", &rstatus);
01121 ImgO->overflow = drms_getkey_int(rsc, "OVERFLOW", &rstatus);
01122 ImgO->headerr = drms_getkey_int(rsc, "HEADRERR", &rstatus);
01123 ImgO->totalvals = drms_getkey_int(rsc, "TOTVALS", &rstatus);
01124 ImgO->datavals = drms_getkey_int(rsc, "DATAVALS", &rstatus);
01125 ImgO->npackets = drms_getkey_int(rsc, "NPACKETS", &rstatus);
01126 ImgO->nerrors = drms_getkey_int(rsc, "NERRORS", &rstatus);
01127 ImgO->last_pix_err = drms_getkey_int(rsc, "EOIERROR", &rstatus);
01128
01129 TIME fpt = drms_getkey_double(rsc, "IMGFPT", &rstatus);
01130 ImgO->first_packet_time = round(65536.0*(fpt - sdo_epoch));
01131 if(ImgO->first_packet_time == 0)
01132 ImgO->first_packet_time = UINT64_MAX;
01133 snprintf(oldtlmdsnam, 128, "%s", drms_getkey_string(rsc, "TLMDSNAM",&rstatus));
01134 cptr = strstr(oldtlmdsnam, "VC");
01135 if(cptr)
01136 snprintf(tlmnamekeyfirst, 23, "%s", cptr);
01137 else
01138 sprintf(tlmnamekeyfirst, "%s", "UNK");
01139 segmentc = drms_segment_lookupnum(rsc, 0);
01140 cArray = drms_segment_read(segmentc, DRMS_TYPE_SHORT, &rstatus);
01141 if(rstatus) {
01142 printk("Can't do drms_segment_read()\n");
01143 return(1);
01144 }
01145 short *adata = (short *)cArray->data;
01146 memcpy(ImgO->dat, adata, 2*MAXPIXELS);
01147 rdat = ImgO->dat;
01148 oldArray = drms_array_create(DRMS_TYPE_SHORT,
01149 segmentc->info->naxis,
01150 segmentc->axis,
01151 rdat,
01152 &dstatus);
01153 drms_free_array(cArray);
01154 }
01155 ImgC = ImgO;
01156 return(0);
01157 }
01158
01159
01160 int get_tlm(char *file, int rexmit, int higherver)
01161 {
01162 FILE *fpin;
01163 unsigned char cbuf[PKTSZ];
01164 long long gap_42_cnt;
01165 int status, rstatus, fpkt_cnt, i, j, sync_bad_cnt;
01166 int datval, eflg, firstflg;
01167 unsigned int cnt1, cnt2, cnt3, gap_24_cnt;
01168 int zero_pn;
01169 unsigned short pksync1, pksync2;
01170 float ftmp;
01171 int decode_status=0;
01172 unsigned int Fsn;
01173
01174 if(!(fpin = fopen(file, "r"))) {
01175 printk("*Can't open tlm file %s\n", file);
01176 return(1);
01177 }
01178 BeginTimer(1);
01179 printk("*Processing tlm file %s\n", file);
01180 fpkt_cnt = sync_bad_cnt = eflg = errmsgcnt = fileimgcnt = 0;
01181 zero_pn = gap_24_cnt = gap_42_cnt = 0;
01182 firstflg = 1;
01183 if(rexmit || higherver) {
01184 if(Img->initialized) {
01185 if(rs) {
01186 close_image(rs, segment, segArray, Img, fsn_prev);
01187 printk("drms_server_end_transaction()\n");
01188 drms_server_end_transaction(drms_env, 0 , 0);
01189 printk("drms_server_begin_transaction()\n");
01190 drms_server_begin_transaction(drms_env);
01191 imagecnt = 0;
01192 }
01193 else
01194 printk("**ERROR: Null record ptr for an opened image fsn=%u\n",fsn_prev);
01195 }
01196 fsn_pre_rexmit = fsn_prev;
01197 fsn_prev = 0;
01198 }
01199
01200 while((status = fread(cbuf,sizeof(char),PKTSZ,fpin) ) == PKTSZ) {
01201 pksync1 = MDI_getshort(cbuf);
01202 pksync2 = MDI_getshort(cbuf+2);
01203 if((pksync1 == 0) && (pksync2 == 0)) {
01204 if(!zero_pn) {
01205 printk("*0 PN code at pkt# %d\n", fpkt_cnt);
01206 printk("*Subsequent ones will be ignored until non-0 again\n");
01207 zero_pn = 1;
01208 }
01209 fpkt_cnt++;
01210 continue;
01211 }
01212 if((pksync1 != 0x1acf) || (pksync2 != 0xfc1d)) {
01213 printk("*Lost sync at VCDU pkt# %d. pksync1=%x pksync2=%x\n",
01214 fpkt_cnt, pksync1, pksync2);
01215 fpkt_cnt++;
01216 eflg++;
01217 if(sync_bad_cnt++ > 4) {
01218 printk("**Too many out of sync packets.\n");
01219
01220 return(0);
01221 }
01222 printk(" Will attempt to press on...\n");
01223 zero_pn = 0;
01224 continue;
01225 }
01226 if(firstflg) {
01227 printk("*VCDU pkt# %d sync = %x %x\n", fpkt_cnt, pksync1, pksync2);
01228 }
01229 fpkt_cnt++;
01230
01231 cnt1 = MDI_getshort(cbuf+6);
01232 cnt2 = MDI_getshort(cbuf+8);
01233 cnt2 = (cnt2 >> 8)& 0xFF;
01234 cnt2 = ((cnt1 << 8)& 0xFF00) + cnt2;
01235 cnt1 = (cnt1 >> 8)& 0xFF;
01236 vcdu_24_cnt = (cnt1*65536) + cnt2;
01237 if(vcdu_24_cnt_next != vcdu_24_cnt) {
01238 printk("*VCDU 24bit seq num out of sequence. exp: %u rec: %u\n",
01239 vcdu_24_cnt_next, vcdu_24_cnt);
01240 if(vcdu_24_cnt_next > vcdu_24_cnt) {
01241 printk("*NOTE: VCDU 24 bit counter retarded\n");
01242 printk("*NOTE: gap report will be inaccurate (tbd)\n");
01243 }
01244 if(!firstflg) {
01245 gap_24_cnt += vcdu_24_cnt - vcdu_24_cnt_next;
01246 }
01247 }
01248 vcdu_24_cnt_next = vcdu_24_cnt + 1;
01249
01250 cnt1 = MDI_getshort(cbuf+10);
01251 cnt2 = MDI_getshort(cbuf+12);
01252 cnt3 = MDI_getshort(cbuf+14);
01253 cnt1 = cnt1 & 0x03ff;
01254 vcdu_seq_num = (cnt1*4294967296) + (cnt2*65536) + cnt3;
01255
01256 if(vcdu_seq_num_next != vcdu_seq_num) {
01257 printk("*IM_PDU seq num out of sequence. exp: %lld rec: %lld\n",
01258 vcdu_seq_num_next, vcdu_seq_num);
01259 if(vcdu_seq_num_next > vcdu_seq_num) {
01260 printk("*NOTE: IM_PDU 42 bit counter retarded\n");
01261 printk("*NOTE: gap report will be inaccurate\n");
01262 }
01263 if(!firstflg) {
01264 gap_42_cnt += vcdu_seq_num - vcdu_seq_num_next;
01265 }
01266 eflg++;
01267 }
01268 firstflg = 0;
01269 vcdu_seq_num_next = vcdu_seq_num + 1;
01270
01271 appid = MDI_getshort(cbuf+18);
01272 appid = appid & 0x07ff;
01273 if(hmiaiaflg) {
01274
01275 testid1 = 507; testid2 = 517;
01276 }
01277 else {
01278
01279 testid1 = 407; testid2 = 417;
01280 }
01281
01282 if((appid == testid1) || (appid == testid2)) {
01283 if(errmsgcnt++ < MAXERRMSGCNT) {
01284 printk("*Test ApID of %0x found for IM_PDU Cntr = %lld\n",
01285 appid, vcdu_seq_num);
01286 for(i=0, j=TESTVALUE; i < 877; i=i+2, j++) {
01287 datval = MDI_getshort(cbuf+32+i);
01288 if(datval != j) {
01289 printk("*Test data value=%0x, expected=%0x for IM_PDU Cntr=%lld\n",
01290 datval, j, vcdu_seq_num);
01291 printk("*File = %s\n", file);
01292 eflg++;
01293 break;
01294 }
01295 }
01296 }
01297 continue;
01298 }
01299
01300
01301
01302 if(appid == APID_HMI_SCIENCE_1 || appid == APID_HMI_SCIENCE_2 ||
01303 appid == APID_AIA_SCIENCE_1 || appid == APID_AIA_SCIENCE_2)
01304 {
01305 cnt1 = MDI_getshort(cbuf+32);
01306 cnt2 = MDI_getshort(cbuf+34);
01307 fsnx = (unsigned int)(cnt1<<16)+(unsigned int)(cnt2);
01308 fsnx = fsnx & 0x3fffffff;
01309 if(fsnx == 0) continue;
01310 if(rexmit || higherver) {
01311 if(fsnx != fsn_prev) {
01312 if(fsn_change_rexmit()) {
01313 printk("***FATAL ERROR in fsn_change_rexmit()\n");
01314 return(1);
01315 }
01316 }
01317 else {
01318 ImgC->apid = appid;
01319 }
01320 }
01321 else {
01322 if(fsnx != fsn_prev) {
01323 if(fsn_change_normal()) {
01324 printk("***FATAL ERROR in fsn_change_normal()\n");
01325 return(1);
01326 }
01327 }
01328 }
01329
01330 if(errskip) { continue; }
01331 rstatus = imgdecode((unsigned short *)(cbuf+10), ImgC);
01332 switch(rstatus) {
01333 case 0:
01334
01335 break;
01336 case IMGDECODE_DECOMPRESS_ERROR:
01337 if(errmsgcnt++ < MAXERRMSGCNT)
01338 printk("*imgdecode() ret: IMGDECODE_DECOMPRESS_ERROR\n");
01339 break;
01340 case IMGDECODE_TOO_MANY_PIXELS:
01341 if(errmsgcnt++ < MAXERRMSGCNT)
01342 printk("*imgdecode() ret: IMGDECODE_TOO_MANY_PIXELS\n");
01343 break;
01344 case IMGDECODE_BAD_N:
01345 if(errmsgcnt++ < MAXERRMSGCNT)
01346 printk("*imgdecode() ret: IMGDECODE_BAD_N\n");
01347 break;
01348 case IMGDECODE_BAD_APID:
01349 if(errmsgcnt++ < MAXERRMSGCNT)
01350 printk("*imgdecode() ret: IMGDECODE_BAD_APID\n");
01351 break;
01352 case IMGDECODE_NO_LOOKUP_TABLE:
01353 if(errmsgcnt++ < MAXERRMSGCNT)
01354 printk("*imgdecode() ret: IMGDECODE_NO_LOOKUP_TABLE\n");
01355 break;
01356 case IMGDECODE_LOOKUP_ID_MISMATCH:
01357 if(errmsgcnt++ < MAXERRMSGCNT)
01358 printk("*imgdecode() ret: IMGDECODE_LOOKUP_ID_MISMATCH\n");
01359 break;
01360 case IMGDECODE_BAD_LOOKUP_TABLE:
01361 if(errmsgcnt++ < MAXERRMSGCNT)
01362 printk("*imgdecode() ret: IMGDECODE_BAD_LOOKUP_TABLE\n");
01363 break;
01364 case IMGDECODE_NO_CROP_TABLE:
01365 if(errmsgcnt++ < MAXERRMSGCNT)
01366 printk("*imgdecode() ret: IMGDECODE_NO_CROP_TABLE\n");
01367 break;
01368 case IMGDECODE_CROP_ID_MISMATCH:
01369 if(errmsgcnt++ < MAXERRMSGCNT)
01370 printk("*imgdecode() ret: IMGDECODE_CROP_ID_MISMATCH\n");
01371 break;
01372 case IMGDECODE_BAD_CROP_GEOMETRY:
01373 if(errmsgcnt++ < MAXERRMSGCNT)
01374 printk("*imgdecode() ret: IMGDECODE_BAD_CROP_GEOMETRY\n");
01375 break;
01376 case IMGDECODE_BAD_CROP_TABLE:
01377 if(errmsgcnt++ < MAXERRMSGCNT)
01378 printk("*imgdecode() ret: IMGDECODE_BAD_CROP_TABLE\n");
01379 break;
01380 case IMGDECODE_BAD_CROP_SKIP_TAKE:
01381 if(errmsgcnt++ < MAXERRMSGCNT)
01382 printk("*imgdecode() ret: IMGDECODE_BAD_CROP_SKIP_TAKE\n");
01383 break;
01384 case IMGDECODE_BAD_OFFSET:
01385 if(errmsgcnt++ < MAXERRMSGCNT)
01386 printk("*imgdecode() ret: IMGDECODE_BAD_OFFSET\n");
01387 break;
01388 case IMGDECODE_OUT_OF_MEMORY:
01389 if(errmsgcnt++ < MAXERRMSGCNT)
01390 printk("*imgdecode() ret: IMGDECODE_OUT_OF_MEMORY\n");
01391 break;
01392 default:
01393 if(errmsgcnt++ < MAXERRMSGCNT)
01394 printk("*imgdecode() ret: unknown err status = %d:\n", rstatus);
01395 break;
01396 }
01397 if(rstatus) {
01398 if(!conterr) {
01399 errskip = 1;
01400 printk("*FSN imgdecode() error. Skip fsn=%u\n", fsnx);
01401 }
01402 else printk("*FSN imgdecode() error. Continue processing fsn=%u\n", fsnx);
01403 }
01404 }
01405 else {
01406
01407 decode_status = decode_next_hk_vcdu((unsigned short *)(cbuf+10), &Hk, &Fsn);
01408 switch (decode_status) {
01409 case SUCCESS_HK_NEED_TO_WTD_CTD:
01410 printk("*ISP found for fsn = %u\n", Fsn);
01411 fsnx = Fsn;
01412 if(rexmit || higherver) {
01413 if(fsnx != fsn_prev) {
01414 if(fsn_change_rexmit()) {
01415 printk("***FATAL ERROR in fsn_change_rexmit()\n");
01416 return(1);
01417 }
01418 }
01419
01420 HMI_compute_exposure_times(rsc, Hk->keywords, hmiaiaflg);
01421 whk_status = write_hk_to_drms(rsc, &Hk);
01422 if(!hmiaiaflg) {
01423 if(whk_status = set_HMI_mech_values(rsc)) {
01424 printk("***ERROR: mechanism position keywords are wrong!\n");
01425 }
01426 }
01427 }
01428 else {
01429 if(fsnx != fsn_prev) {
01430 if(fsn_change_normal()) {
01431 printk("***FATAL ERROR in fsn_change_normal()\n");
01432 return(1);
01433 }
01434 }
01435
01436 HMI_compute_exposure_times(rs, Hk->keywords, hmiaiaflg);
01437 whk_status = write_hk_to_drms(rs, &Hk);
01438 if(!hmiaiaflg) {
01439 if(whk_status = set_HMI_mech_values(rs)) {
01440 printk("***ERROR: mechanism position keywords are wrong!\n");
01441 }
01442 }
01443 }
01444 hk_ccsds_free(&Hk);
01445 rstatus=0;
01446 break;
01447 case SUCCESS_HK_NEED_TO_CTD:
01448
01449
01450 break;
01451 case SUCCESS_SKIP_IMAGE:
01452 printk("decode_next_hk_vcdu() ret:Warning:SUCCESS_SKIP_IMAGE\n");
01453 break;
01454 case SUCCESS_SKIP_PROCESSING_APID:
01455 printk("decode_next_hk_vcdu() ret:Warning:SUCCESS_SKIP_PROCESSING_APID:\n");
01456 break;
01457 case ERROR_HK_FAILED_GETTING_FSN:
01458 printk("decode_next_hk_vcdu() ret:Warning:ERROR_HK_FAILED_GETTING_FSN:\n");
01459 break;
01460 case ERROR_NODATA:
01461 printk("decode_next_hk_vcdu() ret:Warning:ERROR_NODATA\n");
01462 break;
01463 case ERROR_HK_ENVIRONMENT_VARS_NOT_SET:
01464 printk("decode_next_hk_vcdu() ret:ERROR:ERROR_HK_ENVIRONMENT_VARS_NOT_SET\n");
01465 break;
01466 case ERROR_HK_FAILED_OPEN_DAYFILE:
01467 printk("decode_next_hk_vcdu() ret:ERROR:ERROR_HK_FAILED_OPEN_DAYFILE\n");
01468 break;
01469 default:
01470 printk("decode_next_hk_vcdu() ret:Warning:Unexpected return code for decode_status:<%d>:\n", decode_status);
01471 break;
01472 }
01473
01474 }
01475 }
01476 fclose(fpin);
01477 if(!eflg) {
01478 printk("*No errors in tlm file\n");
01479 }
01480 if(rexmit || higherver) {
01481 if(rsc) {
01482 close_image(rsc, segmentc, oldArray, ImgO, fsnx);
01483 printk("drms_server_end_transaction()\n");
01484 drms_server_end_transaction(drms_env, 0 , 0);
01485 printk("drms_server_begin_transaction()\n\n");
01486 drms_server_begin_transaction(drms_env);
01487 imagecnt = 0;
01488 fileimgcnt++;
01489
01490 fsn_prev = 0;
01491 rsc = 0;
01492 }
01493 }
01494 ftmp = EndTimer(1);
01495 printk("**Processed %s\n**with %d images and %d VCDUs in %f sec\n\n",
01496 file, fileimgcnt, fpkt_cnt, ftmp);
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507
01508
01509
01510
01511 if(fpkt_cnt != total_tlm_vcdu) {
01512 printk("**WARNING: Found #vcdu=%d; expected=%d\n", fpkt_cnt, total_tlm_vcdu);
01513 }
01514
01515
01516
01517
01518 if(gap_42_cnt != total_missing_im_pdu) {
01519 printk("**WARNING: IM_PDU 42bit cntr gaps=%lld; expected=%lld\n",
01520 gap_42_cnt, total_missing_im_pdu);
01521 }
01522 ignoresigalrmflg = 1;
01523 return(0);
01524 }
01525
01526
01527
01528
01529 void do_ingest()
01530 {
01531 FILE *fp;
01532 DRMS_Record_t *rs_tlm;
01533 DIR *dfd;
01534 NAMESORT *nameptr;
01535 struct dirent *dp;
01536 int i, j, status, mvstat;
01537 int rexmit, higherversion;
01538 char path[DRMS_MAXPATHLEN];
01539 char name[128], line[128], tlmfile[128], tlmname[96];
01540 char cmd[256], xxname[128], vername[16];
01541 char *token, *filename;
01542
01543 if((dfd=opendir(tlmdir)) == NULL) {
01544 printk("**Can't opendir(%s) to find files\n", tlmdir);
01545 abortit(3);
01546 }
01547 i = 0;
01548 if((nameptr = (NAMESORT *)malloc(MAXFILES * sizeof(NAMESORT))) == NULL) {
01549 printk("***Can't alloc memory for file name sort\n");
01550 abortit(3);
01551 }
01552
01553 while((dp=readdir(dfd)) != NULL) {
01554
01555
01556 if(strstr(dp->d_name, pchan) || strstr(dp->d_name, rchan) || strstr(dp->d_name, ".dsf")) {
01557 nameptr[i++].name = strdup(dp->d_name);
01558 if(i >= MAXFILES) {
01559 printk("***Fatal error. Too many (%d) files in %s\n", MAXFILES, tlmdir);
01560 printf("***Fatal error. Too many (%d) files in %s\n", MAXFILES, tlmdir);
01561 abortit(3);
01562 }
01563 if(!strstr(dp->d_name, ".dsf")) cntsleeps = 0;
01564 }
01565 }
01566 closedir(dfd);
01567 qsort(nameptr, i, sizeof(NAMESORT), &compare_names);
01568
01569 for(j=0; j < i; j++) {
01570
01571
01572
01573
01574
01575 if(!strcmp(pchan, "VC01") || !strcmp(pchan, "VC02")) {
01576 if(strstr(nameptr[j].name, ".dsf")) {
01577
01578 sprintf(cmd, "/bin/mv -f %s/%s %s/dsf/",tlmdir,nameptr[j].name,tlmdir);
01579 printk("*mv dsf file to %s/dsf/\n", tlmdir);
01580 printk("%s\n", cmd);
01581 if(system(cmd)) {
01582 printk("***Error on: %s\n", cmd);
01583 }
01584 }
01585 }
01586 if(!strstr(nameptr[j].name, ".qac")) {
01587 free(nameptr[j].name);
01588 continue;
01589 }
01590 rexmit = higherversion = 0;
01591 if(strstr(nameptr[j].name, ".qacx")) {
01592 rexmit = 1;
01593 }
01594 sprintf(name, "%s/%s", tlmdir, nameptr[j].name);
01595 printk("%s\n*Found qac file:\n* %s\n", do_datestr(), name);
01596 if(!(fp=fopen(name, "r"))) {
01597 printk("***Can't open %s\n", name);
01598 free(nameptr[j].name);
01599 continue;
01600 }
01601
01602 while(fgets(line, 256, fp)) {
01603 if(line[0] == '#' || line[0] == '\n') continue;
01604 if(strstr(line, "TLM_FILE_NAME=")) {
01605 token = (char *)strtok(line, "=");
01606 token = (char *)strtok(NULL, "\n");
01607 printk("tlm file is %s\n", token);
01608 sprintf(tlmfile, "%s/%s", tlmdir, token);
01609 sprintf(tlmname, "%s", token);
01610 }
01611 else if(strstr(line, "TLM_FILE_SIZE=")) {
01612 token = (char *)strtok(line, "=");
01613 token = (char *)strtok(NULL, "=");
01614 printk("*tlm file size is %s", token);
01615 }
01616 else if((strstr(line, "TOTAL_TLM_IM_PDU=")) || (strstr(line, "TOTAL_VCDU="))) {
01617 token = (char *)strtok(line, "=");
01618 token = (char *)strtok(NULL, "\n");
01619 total_tlm_vcdu = atoi(token);
01620 }
01621 else if(strstr(line, "TOTAL_MISSING_VCDU=")) {
01622 token = (char *)strtok(line, "=");
01623 token = (char *)strtok(NULL, "\n");
01624 total_missing_vcdu = atoi(token);
01625 }
01626 else if(strstr(line, "TOTAL_MISSING_IM_PDU=")) {
01627 token = (char *)strtok(line, "=");
01628 token = (char *)strtok(NULL, "\n");
01629 total_missing_im_pdu = atol(token);
01630 break;
01631 }
01632 }
01633 fclose(fp);
01634
01635 strcpy((char *)tlmnamekey, (char *)tlmname);
01636 filename = (char *)rindex(tlmname, '.');
01637 if(!filename) {
01638 printk("**ERROR: Bad formed tlm name in qac file\n");
01639 printk(" tlm name = %s\n", tlmname);
01640 continue;
01641 }
01642 *filename = 0;
01643 *((char *)tlmnamekey+22) = 0;
01644 rs_tlm = drms_create_record(drms_env, tlmseriesname,
01645 DRMS_PERMANENT, &status);
01646 if(status) {
01647 if(status == DRMS_ERROR_SUMOPEN) {
01648 printk("**ERROR: DRMS can't open w/SUMS. Aborting...\n");
01649 abortit(4);
01650 }
01651 printk("***Can't create record for %s. Status=%d\n", tlmseriesname, status);
01652 continue;
01653 }
01654 if((status = drms_setkey_string(rs_tlm, "filename", tlmnamekey))) {
01655 printk("**ERROR: drms_setkey_string failed for 'filename'\n");
01656 }
01657 drms_record_directory(rs_tlm, path, 0);
01658 if(!*path) {
01659 printk("***ERROR: No path to segment for %s\n", tlmseriesname);
01660 continue;
01661 }
01662 if(outdir) {
01663 sprintf(cmd, "cp -p %s %s", tlmfile, outdir);
01664 printk("*cp tlm file to %s\n", outdir);
01665 printk("%s\n", cmd);
01666 if(system(cmd)) {
01667 printk("***Error on: %s\n", cmd);
01668 }
01669 sprintf(cmd, "cp -p %s %s", name, outdir);
01670 printk("*cp qac file to %s\n", outdir);
01671 printk("%s\n", cmd);
01672 if(system(cmd)) {
01673 printk("***Error on: %s\n", cmd);
01674 }
01675 }
01676 sprintf(cmd, "/bin/mv %s %s", tlmfile, path);
01677 printk("*mv tlm to %s\n", path);
01678 printk("%s\n", cmd);
01679 if(status = system(cmd)) {
01680 printk("**ERROR: %d errno=%d on: %s\n", status, errno, cmd);
01681 if(WIFEXITED(status)) {
01682 if(mvstat = WEXITSTATUS(status)) {
01683 printk("**ERROR: mv exit status = %d\n", mvstat);
01684 }
01685 }
01686 printk("**Continue after ERROR on mv of tlm file\n");
01687 continue;
01688 }
01689 sprintf(cmd, "/bin/mv %s %s", name, path);
01690 printk("*mv qac to %s\n", path);
01691 printk("%s\n", cmd);
01692 if(status = system(cmd)) {
01693 printk("**ERROR: %d errno=%d on: %s\n", status, errno, cmd);
01694 if(WIFEXITED(status)) {
01695 if(mvstat = WEXITSTATUS(status)) {
01696 printk("**ERROR: mv exit status = %d\n", mvstat);
01697 }
01698 }
01699 printk("**Continue after ERROR on mv of qac file\n");
01700 continue;
01701 }
01702 if((status = drms_close_record(rs_tlm, DRMS_INSERT_RECORD))) {
01703 printk("**ERROR: drms_close_record failed for %s\n", tlmseriesname);
01704 }
01705
01706 sprintf(xxname, "%s/%s.tlm", path, tlmname);
01707 filename = (char *)rindex(tlmname, '_');
01708 sprintf(vername, "%s", filename);
01709 if(strcmp(vername, "_00")) {
01710 higherversion = 1;
01711 printk("Higher version tlm found: %s.tlm\n", tlmname);
01712 }
01713 firstfound = 1;
01714 if(get_tlm(xxname, rexmit, higherversion)) {
01715 printk("***Error in lev0 extraction for %s\n", xxname);
01716
01717
01718 }
01719 if((stat(stopfile, &stbuf) == 0) || abortflg) { break; }
01720 }
01721 free(nameptr);
01722 }
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741
01742 void do_pipe2soc() {
01743 DIR *dfd;
01744 struct dirent *dp;
01745 FILE *fp;
01746 int ptape_fn, complete;
01747 char line[128], fname[128], cmd[128];
01748 char *su_name, *ptape_id, *ptape_date;
01749
01750 if((dfd=opendir(pipedir)) == NULL) {
01751 printk("**Can't opendir(%s) to find files\n", pipedir);
01752 return;
01753 }
01754 while((dp=readdir(dfd)) != NULL) {
01755 if(strstr(dp->d_name, ".parc")) {
01756 sprintf(fname, "%s/%s", pipedir, dp->d_name);
01757 if(!(fp=fopen(fname, "r"))) {
01758 printk("***Can't open %s\n", fname);
01759 continue;
01760 }
01761 printk("Found parc file: %s\n", fname);
01762 complete = 1;
01763 while(fgets(line, 128, fp)) {
01764 if(line[0] == '#' || line[0] == '\n') continue;
01765 printk("%s", line);
01766 su_name = (char *)strtok(line, " ");
01767 ptape_id = (char *)strtok(NULL, " ");
01768
01769 errno = 0;
01770 ptape_fn = (int)strtol((char *)strtok(NULL, " "), (char **) NULL, 10);
01771 if(errno) continue;
01772 ptape_date = (char *)strtok(NULL, "\n");
01773 if(SUMLIB_SafeTapeUpdate(su_name,ptape_id,ptape_fn,ptape_date)) {
01774 printk("**ERROR in SUMLIB_SafeTapeUpdate(%s...)\n", su_name);
01775 complete = 0;
01776 }
01777 }
01778 fclose(fp);
01779 if(complete) {
01780 sprintf(cmd, "/bin/rm -f %s", fname);
01781 }
01782 else {
01783 sprintf(cmd, "/bin/mv -f %s %s/err/", fname, pipedir);
01784 }
01785 printk("%s\n", cmd);
01786 system(cmd);
01787 }
01788 }
01789 closedir(dfd);
01790 }
01791
01792
01793 void setup()
01794 {
01795 FILE *fp;
01796 int i;
01797 char string[128], cwdbuf[128], idstr[256];
01798 char envfile[100], s1[256],s2[256],s3[256], line[256];
01799 ThreadSigErr_t error = kThreadSigErr_Success;
01800
01801 sdo_epoch = sscan_time("1958.01.01_00:00:00_TAI");
01802 do_datestr();
01803 printk_set(h0log, h0log);
01804 printk("%s\n", datestr);
01805 getcwd(cwdbuf, 126);
01806 sprintf(idstr, "Cwd: %s\nCall: ", cwdbuf);
01807 if(grounddata)
01808 sprintf(string, "ingest_lev0_hmiaia -g started as pid=%d user=%s\n", getpid(), username);
01809 else
01810 sprintf(string, "ingest_lev0_hmiaia started as pid=%d user=%s\n", getpid(), username);
01811 strcat(idstr, string);
01812 printk("*%s", idstr);
01813 if(restartflg) printk("-r ");
01814 sprintf(argvc, "vc=%s", vc);
01815 sprintf(argindir, "indir=%s", tlmdir);
01816 sprintf(arglogfile, "logfile=%s", logname);
01817 if(outdir) {
01818 sprintf(argoutdir, "outdir=%s", outdir);
01819 printk("%s %s %s %s\n", argvc, argindir, argoutdir, arglogfile);
01820 } else {
01821 printk("%s %s %s\n", argvc, argindir, arglogfile);
01822 }
01823 strcpy(pchan, vc);
01824 if(rexmitmode)
01825 sprintf(stopfile, "/usr/local/logs/lev0/%s_stopX", pchan);
01826 else
01827 sprintf(stopfile, "/usr/local/logs/lev0/%s_stop", pchan);
01828
01829
01830
01831
01832
01833
01834
01835 for(i=0; ; i++) {
01836 if(!strcmp(p_r_chan_pairs[i].pchan, pchan)) {
01837 strcpy(rchan, p_r_chan_pairs[i].rchan);
01838 hmiaiaflg = p_r_chan_pairs[i].instru;
01839 break;
01840 }
01841 if(!strcmp(p_r_chan_pairs[i].pchan, "n/a")) {
01842 printk("!!ERROR: Invalid VCid (%s) specified\n", pchan);
01843 fprintf(stderr, "!!ERROR: Invalid VCid (%s) specified. Abort\n", pchan);
01844 abortit(1);
01845 }
01846 }
01847 if(hmiaiaflg) {
01848 if(grounddata) {
01849 sprintf(tlmseriesname, "%s", TLMSERIESNAMEAIAGND);
01850 sprintf(lev0seriesname, "%s", LEV0SERIESNAMEAIAGND);
01851 }
01852 else {
01853 sprintf(tlmseriesname, "%s", TLMSERIESNAMEAIA);
01854 sprintf(lev0seriesname, "%s", LEV0SERIESNAMEAIA);
01855 }
01856 }
01857 else {
01858 if(grounddata) {
01859 sprintf(tlmseriesname, "%s", TLMSERIESNAMEHMIGND);
01860 sprintf(lev0seriesname, "%s", LEV0SERIESNAMEHMIGND);
01861 }
01862 else {
01863 sprintf(tlmseriesname, "%s", TLMSERIESNAMEHMI);
01864 sprintf(lev0seriesname, "%s", LEV0SERIESNAMEHMI);
01865 }
01866 }
01867 if(!restartflg) {
01868 printk("tlmseriesname=%s\nlev0seriesname=%s\n",
01869 tlmseriesname, lev0seriesname);
01870 }
01871 sprintf(bld_vers, "%s", jsoc_version);
01872 umask(002);
01873 Image.initialized = 0;
01874 ImageOld.initialized = 0;
01875 Img = &Image;
01876 ImgO = &ImageOld;
01877 imgdecode_init_hack(Img);
01878 imgdecode_init_hack(ImgO);
01879
01880
01881
01882 if(grounddata) strcpy(envfile, ENVFILE_GND );
01883 else strcpy(envfile, ENVFILE );
01884
01885 if(!(fp=fopen(envfile, "r"))) {
01886 printk("***Can't open %s. Check setting is correct\n", envfile);
01887 exit(0);
01888 }
01889
01890 while( fgets(line, MAXLINE_IN_FILE, fp) != NULL )
01891 {
01892 if (!strncmp(line, "#", 1)) {
01893 continue;
01894 }
01895 else {
01896 sscanf(line, "%s %s %s ", s1,s2,s3);
01897
01898 setenv(s2, s3, 1);
01899 }
01900 }
01901 fclose(fp);
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913
01914 }
01915
01916
01917 int DoIt(void)
01918 {
01919 pid_t pid;
01920 int wflg = 1;
01921 char *args[6];
01922 char callcmd[256];
01923
01924 if (nice_intro())
01925 return (0);
01926 if(!(username = (char *)getenv("USER"))) username = "nouser";
01927 vc = cmdparams_get_str(&cmdparams, "vc", NULL);
01928 tlmdir = cmdparams_get_str(&cmdparams, "indir", NULL);
01929 outdir = cmdparams_get_str(&cmdparams, "outdir", NULL);
01930 pipedir = (char *)cmdparams_get_str(&cmdparams, "pipedir", NULL);
01931 logfile = cmdparams_get_str(&cmdparams, "logfile", NULL);
01932 if(strstr(tlmdir, "rexmit")) { rexmitmode = 1; }
01933 if (strcmp(vc, NOTSPECIFIED) == 0) {
01934 fprintf(stderr, "'vc' virt channel must be specified. Abort\n");
01935 return(1);
01936 }
01937 if (strcmp(tlmdir, NOTSPECIFIED) == 0) {
01938 fprintf(stderr, "'indir' must be specified. Abort\n");
01939 return(1);
01940 }
01941 if (strcmp(outdir, NOTSPECIFIED) == 0) {
01942 outdir = NULL;
01943 }
01944 if (strcmp(pipedir, NOTSPECIFIED) == 0) {
01945 pipedir = NULL;
01946 }
01947 else {
01948 if(!(jdbname = (char *)getenv("JSOC_DBNAME"))) {
01949 printk("!!ERROR: No JSOC_DBNAME in environment\n");
01950 fprintf(stderr, "!!ERROR: No JSOC_DBNAME in environment\n");
01951 abortit(1);
01952 }
01953 else {
01954 sprintf(dbname, "%s_sums", jdbname);
01955 }
01956 if(DS_ConnectDB_Q(dbname)) {
01957 printk("**Can't connect to DB %s\n", dbname);
01958 printk("**ERROR: Will not be able to process .parc files\n");
01959 pipedir = NULL;
01960 }
01961 }
01962 if (strcmp(logfile, NOTSPECIFIED) == 0) {
01963 sprintf(logname, H0LOGFILE, username, vc, gettimetag());
01964 }
01965 else {
01966 sprintf(logname, "%s", logfile);
01967 }
01968 if(restartflg) {
01969
01970 if((h0logfp=fopen(logname, "a")) == NULL)
01971 fprintf(stderr, "**Can't open for append the log file %s\n", logname);
01972 }
01973 else {
01974 if((h0logfp=fopen(logname, "w")) == NULL)
01975 fprintf(stderr, "**Can't open the log file %s\n", logname);
01976 }
01977 setup();
01978 while(wflg) {
01979 do_ingest();
01980
01981 if((stat(stopfile, &stbuf) == 0) || abortflg) {
01982 printk("Abort or Found file: %s. Terminate.\n", stopfile);
01983 if(abortflg) send_mail("Abort for ingest_lev0 for %s\n", pchan);
01984
01985 if(Image.initialized) {
01986 if(rs) {
01987 close_image(rs, segment, segArray, &Image, fsn_prev);
01988 }
01989 }
01990 else {
01991 printk("*No image to close on exit\n");
01992 }
01993
01994
01995
01996
01997
01998
01999
02000 if(rexmitmode)
02001 sprintf(callcmd, "touch /usr/local/logs/lev0/%s_exitX", pchan);
02002 else
02003 sprintf(callcmd, "touch /usr/local/logs/lev0/%s_exit", pchan);
02004 system(callcmd);
02005 wflg = 0;
02006 continue;
02007 }
02008 sleep(sleep_interval);
02009 if(cntsleeps == 0) {
02010 if(paused) {
02011 paused = 0;
02012 if(!rexmitmode) {
02013 send_mail("tlm files seen again for ingest_lev0 for %s\n", pchan);
02014 }
02015 }
02016 }
02017 cntsleeps++;
02018 if(cntsleeps > 300) {
02019 if(!paused) {
02020 if(!rexmitmode) {
02021 send_mail("No files seen for ingest_lev0 for %s for 600sec\n", pchan);
02022 }
02023 paused = 1;
02024 }
02025 }
02026 }
02027 return(0);
02028 }
02029