00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "limbfit_tas.h"
00018
00019
00020 char *module_name = "lfwrp";
00021
00022 ModuleArgs_t module_args[] = {
00023 {ARG_STRING, "dsin", "hmi.lev1", "input data set (default=hmi.lev1)"},
00024 {ARG_STRING, "dsout", "su_scholl.limbfit", "output data set"},
00025 {ARG_STRING, "logdir", "./", "logs directory"},
00026 {ARG_STRING, "tmpdir", "./", "tmp directory"},
00027 {ARG_STRING, "bdate", " ", "begin date"},
00028
00029 {ARG_INTS, "bfsn", "0", "first lev1 fsn# to process"},
00030 {ARG_INTS, "efsn", "0", "last lev1 fsn# to process"},
00031 {ARG_INT, "cam", "0", "camera selection: 0: both = default, otherwise: 1 or 2"},
00032 {ARG_INT, "fid", "0", "default=all, otherwise one FID number or mask"},
00033
00034 {ARG_INT, "spe", "0", "used to activate only 1 iteration of the FORTRAN code and AHI low for roll analaysis (default=0 normal processing - =1 activate)"},
00035 {ARG_INT, "cc", "0", "used to activate center calculation instead of using X0/YO_LF (default=0 no center calculation, =1 calculation)"},
00036 {ARG_INT, "iter", "3", "to change the number of iterations of the limb.f code (default=3)"},
00037 {ARG_INT, "fldf", "0", "0: no full ldf processing (default), 1: with full ldf proc."},
00038 {ARG_INT, "src", "0", "0: hmi.lev1 (default), 1: hmi.lev1_nrt (quality test is different) 2: limbfit_annulus (initial test is different)"},
00039
00040 {ARG_STRING, "comment", " ", "to add a comment in a dataset"},
00041 {ARG_STRING, "preset", "all", "current possible values: roll_cont, roll_all, pipeline, pipeline2, all, none (default=all <=> cam and fid selection)"},
00042 {ARG_INT, "debug", "0", "debug level (default: 0 no debug info)"},
00043 {ARG_END}
00044
00045 };
00046
00047 void get_sdate(char *sdate)
00048 {
00049
00050 time_t t = time(NULL);
00051 struct tm *timeptr;
00052
00053 timeptr = localtime(&t);
00054 sprintf(sdate, "%d.%02d.%02d_%02d:%02d:%02d",
00055 (timeptr->tm_year+1900), (timeptr->tm_mon+1),
00056 timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
00057 }
00058
00059
00060 void lf_logmsg(char * type1, char * type2, int return_code, int status, char *message, char *code_name, FILE *opf)
00061 {
00062 static char sdate[32];
00063 get_sdate(sdate);
00064 if ( !(strcmp(type1,"INFO")) || (status==0 &&return_code==0))
00065 fprintf(opf,"%s/%20s: %s %5s %5s msg: '%s' \n",LOGMSG1, code_name, sdate,type1,type2, message);
00066 else
00067 fprintf(opf,"%s/%20s: %s %5s %5s msg: '%s' return code: %d exit: %d\n",LOGMSG1, code_name, sdate,type1,type2, message, return_code,status);
00068 }
00069
00070 void close_on_error(DRMS_Record_t *record_in,DRMS_Record_t *record_out,DRMS_Array_t *data_array)
00071 {
00072 drms_free_array (data_array);
00073 drms_close_record (record_out, DRMS_FREE_RECORD);
00074 drms_close_record (record_in, DRMS_FREE_RECORD);
00075
00076 }
00077
00078
00079
00080
00081
00082 int process_n_records_fsn(char * open_dsname, LIMBFIT_INPUT *lfv, LIMBFIT_OUTPUT *lfr, LIMBFIT_IO_PUT *lfw, int *status)
00083 {
00084 static char *log_msg_code="process_n_records";
00085 char log_msg[200];
00086 sprintf(log_msg,"doing process for %s -> %s",open_dsname,lfr->dsout);
00087 lf_logmsg("INFO", "APP", 0,0, log_msg, log_msg_code, lfr->opf);
00088
00089
00090
00091
00092
00093
00094
00095 DRMS_RecordSet_t *drs_in,*drs_out;
00096 DRMS_Record_t *record_in,*record_out;
00097 int rstatus, ncnt,r;
00098
00099 drs_in = drms_open_records(drms_env, open_dsname, &rstatus);
00100 if (!drs_in) {
00101 sprintf(log_msg,"unable to open record set %s\n",open_dsname);
00102 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ, rstatus, log_msg, log_msg_code, lfr->opf);
00103 fprintf (stderr, log_msg);
00104 *status=ERR_DRMS_READ;
00105 return(ERR_DRMS_READ);
00106 }
00107
00108 ncnt = drs_in->n;
00109 if (ncnt < 1) {
00110 sprintf(log_msg,"no records in selected set %s\n",open_dsname);
00111 lf_logmsg("WARNING", "DRMS", WAR_DRMS_NORECORD, rstatus, log_msg, log_msg_code, lfr->opf);
00112 fprintf (stderr, log_msg);
00113 *status=WAR_DRMS_NORECORD;
00114 drms_close_records(drs_in, DRMS_FREE_RECORD);
00115 return(WAR_DRMS_NORECORD);
00116 }
00117
00118 drs_out = drms_create_records(drms_env, ncnt, lfr->dsout, DRMS_PERMANENT,&rstatus);
00119 if (!drs_out) {
00120 sprintf(log_msg,"unable to create record set %s\n",lfr->dsout);
00121 lf_logmsg("ERROR", "DRMS", ERR_DRMS_WRITE, rstatus, log_msg, log_msg_code, lfr->opf);
00122 fprintf (stderr, log_msg);
00123 *status=ERR_DRMS_WRITE;
00124 drms_close_records(drs_in, DRMS_FREE_RECORD);
00125 return(ERR_DRMS_WRITE);
00126 }
00127
00128 unsigned int fsn = 0;
00129
00130 for(r=0; r < ncnt; r++)
00131 {
00132 record_in = drs_in->records[r];
00133 record_out = drs_out->records[r];
00134
00135 fsn = drms_getkey_int(record_in, "FSN", &rstatus);
00136 if(rstatus) {
00137 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_KW, rstatus, "drms_getkey_string(FSN)", log_msg_code, lfr->opf);
00138 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,lfr);
00139 *status=ERR_DRMS_READ_MISSING_KW;
00140 drms_close_records(drs_out, DRMS_INSERT_RECORD);
00141 drms_close_records(drs_in, DRMS_FREE_RECORD);
00142 return(0);
00143 }
00144 sprintf(log_msg,"FSN:%u processing",fsn);
00145 lf_logmsg("INFO", "APP", 0,0, log_msg, log_msg_code, lfr->opf);
00146 printf("selection: #%u\n",fsn);
00147 lfv->fsn=fsn;
00148 if (do_one_limbfit(record_in,record_out,lfv,lfr,lfw,&rstatus))
00149 {
00150 if (rstatus < 0 && rstatus > -300)
00151 {
00152 drms_close_records(drs_out, DRMS_INSERT_RECORD);
00153 drms_close_records(drs_in, DRMS_FREE_RECORD);
00154 lf_logmsg("ERROR", "APP", rstatus, 0, "to be aborted", log_msg_code, lfr->opf);
00155 return(rstatus);
00156 }
00157 }
00158 fflush(lfr->opf);
00159 }
00160 drms_close_records(drs_out, DRMS_INSERT_RECORD);
00161 drms_close_records(drs_in, DRMS_FREE_RECORD);
00162 lf_logmsg("INFO", "APP", 0, 0, "Records saved", log_msg_code, lfr->opf);
00163 if (lfr->debug) printf("records saved\n");
00164
00165 return(0);
00166 }
00167
00168 int process_all_records_smpl(char * open_dsname, LIMBFIT_INPUT *lfv, LIMBFIT_OUTPUT *lfr, LIMBFIT_IO_PUT *lfw, int *status)
00169 {
00170 static char *log_msg_code="process_n_records";
00171 char log_msg[200];
00172 sprintf(log_msg,"doing process for %s -> %s",open_dsname,lfr->dsout);
00173 lf_logmsg("INFO", "APP", 0,0, log_msg, log_msg_code, lfr->opf);
00174
00175
00176 DRMS_RecordSet_t *drs_in,*drs_out;
00177 DRMS_Record_t *record_in,*record_out;
00178 int rstatus;
00179 long long ncnt;
00180 unsigned int fsn = 0;
00181
00182 drs_in = drms_open_records(drms_env, open_dsname, &rstatus);
00183 if (!drs_in) {
00184 sprintf(log_msg,"unable to open record set %s\n",open_dsname);
00185 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ, rstatus, log_msg, log_msg_code, lfr->opf);
00186 fprintf (stderr, log_msg);
00187 *status=ERR_DRMS_READ;
00188 return(ERR_DRMS_READ);
00189 }
00190 ncnt = drs_in->n;
00191 if (ncnt < 1) {
00192 sprintf(log_msg,"no records in selected set %s\n",open_dsname);
00193 lf_logmsg("WARNING", "DRMS", WAR_DRMS_NORECORD, rstatus, log_msg, log_msg_code, lfr->opf);
00194 fprintf (stderr, log_msg);
00195 *status=WAR_DRMS_NORECORD;
00196 drms_close_records(drs_in, DRMS_FREE_RECORD);
00197 return(WAR_DRMS_NORECORD);
00198 }
00199
00200 sprintf(log_msg,"---> %lld records",ncnt);
00201 lf_logmsg("INFO", "APP", 0,0, log_msg, log_msg_code, lfr->opf);
00202
00203
00204
00205
00206 long long numofrecs, nrec, n, j;
00207 int numrec, numofchunks, i;
00208 numofrecs = ncnt;
00209 numrec = NUMRECPERTRANS;
00210 numofchunks = numofrecs/numrec;
00211 if((numofrecs % numrec) != 0) numofchunks++;
00212 n=0;
00213 for(i = 0; i < numofchunks; i++)
00214 {
00215 if ((ncnt-n)>=numrec) nrec=numrec; else nrec=ncnt-n;
00216
00217 drs_out = drms_create_records(drms_env, nrec, lfr->dsout, DRMS_PERMANENT,&rstatus);
00218 if (!drs_out) {
00219 sprintf(log_msg,"unable to create record set %s\n",lfr->dsout);
00220 lf_logmsg("ERROR", "DRMS", ERR_DRMS_WRITE, rstatus, log_msg, log_msg_code, lfr->opf);
00221 fprintf (stderr, log_msg);
00222 *status=ERR_DRMS_WRITE;
00223 drms_close_records(drs_in, DRMS_FREE_RECORD);
00224 return(ERR_DRMS_WRITE);
00225 }
00226 for(j=0; j<numrec && n<ncnt; j++)
00227 {
00228 record_in = drs_in->records[n];
00229 record_out = drs_out->records[j];
00230
00231 fsn = drms_getkey_int(record_in, "FSN", &rstatus);
00232 if(rstatus) {
00233 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_KW, rstatus, "drms_getkey_string(FSN)", log_msg_code, lfr->opf);
00234 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,lfr);
00235 *status=ERR_DRMS_READ_MISSING_KW;
00236 drms_close_records(drs_out, DRMS_INSERT_RECORD);
00237 drms_close_records(drs_in, DRMS_FREE_RECORD);
00238 return(0);
00239 }
00240 sprintf(log_msg,"FSN:%u processing",fsn);
00241 lf_logmsg("INFO", "APP", 0,0, log_msg, log_msg_code, lfr->opf);
00242 printf("selection: #%u\n",fsn);
00243
00244 if (do_one_limbfit(record_in,record_out,lfv,lfr,lfw,&rstatus))
00245 {
00246 if (rstatus < 0 && rstatus > -300)
00247 {
00248 drms_close_records(drs_out, DRMS_INSERT_RECORD);
00249 drms_close_records(drs_in, DRMS_FREE_RECORD);
00250 lf_logmsg("ERROR", "APP", rstatus, 0, "to be aborted", log_msg_code, lfr->opf);
00251 return(rstatus);
00252 }
00253 }
00254 fflush(lfr->opf);
00255 n++;
00256 }
00257 drms_close_records(drs_out, DRMS_INSERT_RECORD);
00258 lf_logmsg("INFO", "APP", 0, 0, "Records saved", log_msg_code, lfr->opf);
00259 if (lfr->debug) printf("records saved\n");
00260 drms_server_end_transaction(drms_env,0,0);
00261 drms_server_begin_transaction(drms_env);
00262 }
00263 drms_close_records(drs_in, DRMS_FREE_RECORD);
00264 sprintf(log_msg,"close %s", open_dsname);
00265 lf_logmsg("INFO", "APP", 0, 0, log_msg, log_msg_code, lfr->opf);
00266
00267 return(0);
00268 }
00269
00270
00271
00272
00273
00274
00275
00276 int DoIt(void)
00277 {
00278
00279
00280 CmdParams_t *params = &cmdparams;
00281
00282 int debug = params_get_int (params, "debug");
00283 int spe = params_get_int (params, "spe");
00284 int cc = params_get_int (params, "cc");
00285 int fldf = params_get_int (params, "fldf");
00286 int iter = params_get_int (params, "iter");
00287 int cam = params_get_int (params, "cam");
00288 int fid = params_get_int (params, "fid");
00289
00290 int src = params_get_int (params, "src");
00291
00292 char* dsin = params_get_str (params, "dsin");
00293 char* dsout = params_get_str (params, "dsout");
00294 char* log_dir = params_get_str (params, "logdir");
00295 char* tmp_dir = params_get_str (params, "tmpdir");
00296 char* preset = params_get_str (params, "preset");
00297 char* comment = params_get_str (params, "comment");
00298 long long bfsn = params_get_int (params, "bfsn");
00299 long long efsn = params_get_int (params, "efsn");
00300 char* bdate = params_get_str (params, "bdate");
00301
00302
00303 static char *log_msg_code="DoIt";
00304 char log_msg[200];
00305 int result;
00306
00307 static char open_dsname[400];
00308 char recrange[128];
00309 static char qual[15];
00310
00311 if ((bfsn == 0 || efsn == 0) && strcmp(bdate," ")==0)
00312 {
00313 fprintf(stderr, "bfsn and efsn must be given for fsn mode or begin date must be specified. \n");
00314 return(ERR_EXIT);
00315 }
00316 if(strcmp(bdate," ")==0 && bfsn > efsn)
00317 {
00318 fprintf(stderr, "bfsn must be <= efsn\n");
00319 return(ERR_EXIT);
00320 }
00321 if(cam < 0 || cam > 2)
00322 {
00323 fprintf(stderr, "cam must be equal to 0, 1, or 2\n");
00324 return(ERR_EXIT);
00325 }
00326 if(src == 0)
00327 sprintf(qual,"0");
00328 else
00329 if(src == 1)
00330 sprintf(qual,"1073741824");
00331 else
00332 if(src == 2)
00333 sprintf(qual," ");
00334 else
00335 {
00336 fprintf(stderr, "src to 0, 1, or 2\n");
00337 return(ERR_EXIT);
00338 }
00339
00340 static char bld_vers[16];
00341 sprintf(bld_vers, "%s", jsoc_version);
00342
00343
00344
00345
00346 static char flogname[128];
00347 static char sdate[32];
00348 get_sdate(sdate);
00349
00350 FILE *opf;
00351 sprintf(flogname, "%s/limbfit_%s_%lld_%lld.log",log_dir,sdate,bfsn,efsn);
00352 if((opf=fopen(flogname, "w")) == NULL)
00353 {
00354 fprintf(stderr, "**Can't open the log file %s\n", flogname);
00355 return(ERR_EXIT);
00356 }
00357 lf_logmsg("INFO", "APP", 0, 0, "Begin... ", log_msg_code, opf);
00358
00359
00360 static char tcam[20];
00361 static char tfid[100];
00362 static char tdat[50];
00363 static char tfil[20];
00364 static char tbase[128];
00365 if (src < 2)
00366 sprintf(tfil, "[? quality = %s ", qual);
00367 else
00368 sprintf(tfil, "[");
00369
00370 int presetval=0;
00371 int setval=0;
00372
00373 if(strcmp(preset,"ppl1d")==0)
00374 {
00375 sprintf(tcam, " and camera=2 ");
00376 sprintf(tfid, " and CAST(FID AS TEXT) LIKE '%8'");
00377 sprintf(tdat, "[%s]",bdate);
00378 presetval=10;
00379 }
00380 else if(strcmp(preset,"ppl2d")==0)
00381 {
00382 sprintf(tcam, " and camera=1 ");
00383 sprintf(tfid, " and FID<10010 ");
00384 sprintf(tdat, "[%s]",bdate);
00385 presetval=10;
00386 }
00387 else if(strcmp(preset,"ppl3d")==0)
00388 {
00389 sprintf(tcam, " and camera=1 ");
00390 sprintf(tfid, " and FID>10010 and hftsacid = 1021 and hflreftm % 540 = 135 ");
00391 sprintf(tdat, "[%s]",bdate);
00392
00393 presetval=10;
00394 }
00395 else if(strcmp(preset,"roll_all")==0)
00396 {
00397 sprintf(tcam, " ");
00398 sprintf(tfid, " ");
00399 presetval=20;
00400 }
00401 else if(strcmp(preset,"roll_cont")==0)
00402 {
00403 sprintf(tcam, " ");
00404 sprintf(tfid, " AND FID<10010 ");
00405 presetval=20;
00406 }
00407 else if(strcmp(preset,"none")==0)
00408 {
00409 if (cam == 1 || cam == 2) sprintf(tcam, " and camera = %d ", cam); else sprintf(tcam, " ");
00410 if (fid != 0) sprintf(tfid, " and fid = %d ", fid); else sprintf(tfid, " ");
00411 presetval=20;
00412 }
00413 else
00414 {
00415 sprintf(tcam, " ");
00416 sprintf(tfid, " ");
00417 presetval=20;
00418 setval=1;
00419 }
00420
00421 if ((setval == 1) && (src==2))
00422 sprintf(tbase, "%s]", tfil);
00423 else
00424 sprintf(tbase, "%s%s%s ?]", tfil,tcam,tfid);
00425
00426
00427
00428
00429
00430
00431 float * anls = (float *) malloc(sizeof(float)*(MAX_SIZE_ANN_VARS*3));
00432 if(!anls)
00433 {
00434 lf_logmsg("ERROR", "APP", ERR_MALLOC_FAILED, 0,"malloc failed (anls)", log_msg_code, opf);
00435 return(ERR_EXIT);
00436 }
00437
00438
00439
00440
00441 static LIMBFIT_INPUT limbfit_vars ;
00442 static LIMBFIT_INPUT *lfv = &limbfit_vars;
00443 static LIMBFIT_OUTPUT limbfit_res ;
00444 static LIMBFIT_OUTPUT *lfr = &limbfit_res;
00445 static LIMBFIT_IO_PUT limbfit_ios ;
00446 static LIMBFIT_IO_PUT *lfw = &limbfit_ios;
00447
00448 lfr->code_name=CODE_NAME;
00449 lfr->code_version=CODE_VERSION;
00450 lfr->code_date=CODE_DATE;
00451 lfr->comment=comment;
00452 lfr->dsin=dsin;
00453 lfr->bld_vers=bld_vers;
00454 lfr->opf=opf;
00455 lfr->tmp_dir=tmp_dir;
00456 lfr->dsout=dsout;
00457 lfr->debug=debug;
00458 lfr->cc=cc;
00459
00460 lfv->src=src;
00461 lfv->cc=cc;
00462 lfv->spe=spe;
00463 lfv->iter=iter;
00464 lfv->fldf=fldf;
00465
00466
00467 lfw->anls=anls;
00468 lfw->is_firstobs=0;
00469 lfw->anls_nbpix=0;
00470 lfw->pf_anls=&anls[0];
00471
00472
00473
00474
00475
00476
00477
00478
00479 if(presetval==20)
00480 {
00481
00482
00483
00484 long long numofrecs, frec, lrec;
00485 int numrec, numofchunks, i;
00486 numofrecs = (efsn - bfsn) + 1;
00487 numrec = NUMRECPERTRANS;
00488 numofchunks = numofrecs/numrec;
00489 if((numofrecs % numrec) != 0) numofchunks++;
00490 lrec = bfsn-1;
00491
00492 for(i = 0; i < numofchunks; i++)
00493 {
00494 frec = lrec+1;
00495 lrec = (frec + numrec)-1;
00496 if(lrec > efsn) lrec=efsn;
00497 sprintf(recrange, "%lld-%lld", frec, lrec);
00498 if (src==2)
00499 sprintf(open_dsname, "%s[][][%s]%s", dsin, recrange,tbase);
00500 else
00501 sprintf(open_dsname, "%s[][%s]%s", dsin, recrange,tbase);
00502
00503 sprintf(log_msg,"open %s -> %s (logdir: %s, tmpdir: %s, cam: %d, spe: %d, cc: %d, iter: %d, comment: %s , debug: %d)",
00504 open_dsname,dsout,log_dir,tmp_dir,cam,spe,cc,iter,comment,debug);
00505
00506 lf_logmsg("INFO", "APP", 0, 0, log_msg, log_msg_code, opf);
00507 if(process_n_records_fsn(open_dsname, lfv, lfr, lfw, &result))
00508 {
00509 if (result < 0 && result > -400)
00510 {
00511 lf_logmsg("ERROR", "ABORT", result, 0, "", log_msg_code, opf);
00512 fprintf(opf,"lfwrp abort\nSee log: %s\n", flogname);
00513 fprintf(opf,"lfwrp: Restart it as %s[][%lld-%lld]%s\n", dsin,frec,efsn,tbase);
00514
00515 fclose(opf);
00516 return(ERR_EXIT);
00517 }
00518 }
00519 sprintf(log_msg,"close %s", open_dsname);
00520 lf_logmsg("INFO", "APP", 0, 0, log_msg, log_msg_code, opf);
00521 drms_server_end_transaction(drms_env,0,0);
00522 drms_server_begin_transaction(drms_env);
00523 }
00524 }
00525 else if(presetval==10)
00526 {
00527
00528 sprintf(open_dsname, "%s%s[]%s", dsin, tdat, tbase);
00529 sprintf(log_msg,"open %s -> %s (logdir: %s, tmpdir: %s, comment: %s , debug: %d)",
00530 open_dsname,dsout,log_dir,tmp_dir,comment,debug);
00531 lf_logmsg("INFO", "APP", 0, 0, log_msg, log_msg_code, opf);
00532 if(process_all_records_smpl(open_dsname, lfv, lfr, lfw, &result))
00533 {
00534 if (result < 0 && result > -400)
00535 {
00536 lf_logmsg("ERROR", "ABORT", result, 0, "", log_msg_code, opf);
00537 fprintf(opf,"lfwrp abort\nSee log: %s\n", flogname);
00538
00539 fclose(opf);
00540 return(ERR_EXIT);
00541 }
00542 }
00543 }
00544
00545 free(anls);
00546 lf_logmsg("INFO", "APP", 0, 0, "Batch End... ", log_msg_code, opf);
00547 fclose(opf);
00548 printf("Batch end\n");
00549
00550 return(0);
00551 }
00552