00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "limbfit_ann.h"
00019
00020 char *module_name = "lfwrp_ann";
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_ann", "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, "src", "0", "0: hmi.lev1 (default), 1: hmi.lev1_nrt (quality test is different)"},
00035 {ARG_STRING, "comment", " ", "to add a comment in a dataset"},
00036 {ARG_INT, "debug", "0", "debug level (default: 0 no debug info)"},
00037 {ARG_END}
00038
00039 };
00040
00041 void get_sdate(char *sdate)
00042 {
00043
00044 time_t t = time(NULL);
00045 struct tm *timeptr;
00046
00047 timeptr = localtime(&t);
00048 sprintf(sdate, "%d.%02d.%02d_%02d:%02d:%02d",
00049 (timeptr->tm_year+1900), (timeptr->tm_mon+1),
00050 timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
00051 }
00052
00053
00054 void lf_logmsg(char * type1, char * type2, int return_code, int status, char *message, char *code_name, FILE *opf)
00055 {
00056 static char sdate[32];
00057 get_sdate(sdate);
00058 if ( !(strcmp(type1,"INFO")) || (status==0 &&return_code==0))
00059 fprintf(opf,"%s/%20s: %s %5s %5s msg: '%s' \n",LOGMSG1, code_name, sdate,type1,type2, message);
00060 else
00061 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);
00062 }
00063
00064 void close_on_error(DRMS_Record_t *record_in,DRMS_Record_t *record_out,DRMS_Array_t *data_array)
00065 {
00066 drms_free_array (data_array);
00067 drms_close_record (record_out, DRMS_FREE_RECORD);
00068 drms_close_record (record_in, DRMS_FREE_RECORD);
00069
00070 }
00071
00072
00073
00074
00075
00076 int process_n_records_fsn(char * open_dsname, LIMBFIT_INPUT *lfv, LIMBFIT_OUTPUT *lfr, int *status)
00077 {
00078 static char *log_msg_code="process_n_records";
00079 char log_msg[200];
00080 sprintf(log_msg,"doing process for %s -> %s",open_dsname,lfr->dsout);
00081 lf_logmsg("INFO", "APP", 0,0, log_msg, log_msg_code, lfr->opf);
00082
00083
00084
00085
00086
00087
00088
00089 DRMS_RecordSet_t *drs_in,*drs_out;
00090 DRMS_Record_t *record_in,*record_out;
00091 int rstatus, ncnt,r;
00092
00093 drs_in = drms_open_records(drms_env, open_dsname, &rstatus);
00094 if (!drs_in) {
00095 sprintf(log_msg,"unable to open record set %s\n",open_dsname);
00096 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ, rstatus, log_msg, log_msg_code, lfr->opf);
00097 fprintf (stderr, log_msg);
00098 *status=ERR_DRMS_READ;
00099 return(ERR_DRMS_READ);
00100 }
00101
00102 ncnt = drs_in->n;
00103 if (ncnt < 1) {
00104 sprintf(log_msg,"no records in selected set %s\n",open_dsname);
00105 lf_logmsg("WARNING", "DRMS", WAR_DRMS_NORECORD, rstatus, log_msg, log_msg_code, lfr->opf);
00106 fprintf (stderr, log_msg);
00107 *status=WAR_DRMS_NORECORD;
00108 drms_close_records(drs_in, DRMS_FREE_RECORD);
00109 return(WAR_DRMS_NORECORD);
00110 }
00111
00112 drs_out = drms_create_records(drms_env, ncnt, lfr->dsout, DRMS_PERMANENT,&rstatus);
00113 if (!drs_out) {
00114 sprintf(log_msg,"unable to create record set %s\n",lfr->dsout);
00115 lf_logmsg("ERROR", "DRMS", ERR_DRMS_WRITE, rstatus, log_msg, log_msg_code, lfr->opf);
00116 fprintf (stderr, log_msg);
00117 *status=ERR_DRMS_WRITE;
00118 drms_close_records(drs_in, DRMS_FREE_RECORD);
00119 return(ERR_DRMS_WRITE);
00120 }
00121
00122 unsigned int fsn = 0;
00123
00124 for(r=0; r < ncnt; r++)
00125 {
00126 record_in = drs_in->records[r];
00127 record_out = drs_out->records[r];
00128
00129 fsn = drms_getkey_int(record_in, "FSN", &rstatus);
00130 if(rstatus) {
00131 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_KW, rstatus, "drms_getkey_string(FSN)", log_msg_code, lfr->opf);
00132 write_mini_output(PROCSTAT_NO_LA_DB_READ_PB,record_in,record_out,0,lfr);
00133 *status=ERR_DRMS_READ_MISSING_KW;
00134 drms_close_records(drs_out, DRMS_INSERT_RECORD);
00135 drms_close_records(drs_in, DRMS_FREE_RECORD);
00136 return(0);
00137 }
00138 sprintf(log_msg,"FSN:%u processing",fsn);
00139 lf_logmsg("INFO", "APP", 0,0, log_msg, log_msg_code, lfr->opf);
00140 printf("selection: #%u\n",fsn);
00141 lfv->fsn=fsn;
00142 if (do_one_limbfit(record_in,record_out,lfv,lfr,&rstatus))
00143 {
00144 if (rstatus < 0 && rstatus > -300)
00145 {
00146 drms_close_records(drs_out, DRMS_INSERT_RECORD);
00147 drms_close_records(drs_in, DRMS_FREE_RECORD);
00148 lf_logmsg("ERROR", "APP", rstatus, 0, "to be aborted", log_msg_code, lfr->opf);
00149 return(rstatus);
00150 }
00151 }
00152 fflush(lfr->opf);
00153 }
00154 drms_close_records(drs_out, DRMS_INSERT_RECORD);
00155 drms_close_records(drs_in, DRMS_FREE_RECORD);
00156 lf_logmsg("INFO", "APP", 0, 0, "Records saved", log_msg_code, lfr->opf);
00157 if (lfr->debug) printf("records saved\n");
00158
00159 return(0);
00160 }
00161
00162
00163
00164
00165
00166
00167 int DoIt(void)
00168 {
00169
00170
00171 CmdParams_t *params = &cmdparams;
00172
00173 int debug = params_get_int (params, "debug");
00174 int cam = params_get_int (params, "cam");
00175 int fid = params_get_int (params, "fid");
00176
00177 int src = params_get_int (params, "src");
00178 char* dsin = params_get_str (params, "dsin");
00179 char* dsout = params_get_str (params, "dsout");
00180 char* log_dir = params_get_str (params, "logdir");
00181 char* tmp_dir = params_get_str (params, "tmpdir");
00182 char* comment = params_get_str (params, "comment");
00183 long long bfsn = params_get_int (params, "bfsn");
00184 long long efsn = params_get_int (params, "efsn");
00185 char* bdate = params_get_str (params, "bdate");
00186
00187
00188 static char *log_msg_code="DoIt";
00189 char log_msg[200];
00190 int result;
00191
00192 static char open_dsname[400];
00193 char recrange[128];
00194 static char qual[15];
00195
00196 if ((bfsn == 0 || efsn == 0) && strcmp(bdate," ")==0)
00197 {
00198 fprintf(stderr, "bfsn and efsn must be given for fsn mode or begin date must be specified. \n");
00199 return(ERR_EXIT);
00200 }
00201 if(strcmp(bdate," ")==0 && bfsn > efsn)
00202 {
00203 fprintf(stderr, "bfsn must be <= efsn\n");
00204 return(ERR_EXIT);
00205 }
00206 if(cam < 0 || cam > 2)
00207 {
00208 fprintf(stderr, "cam must be equal to 0, 1, or 2\n");
00209 return(ERR_EXIT);
00210 }
00211 if(src == 0)
00212 sprintf(qual,"0");
00213 else
00214 sprintf(qual,"1073741824");
00215
00216 static char bld_vers[16];
00217 sprintf(bld_vers, "%s", jsoc_version);
00218
00219
00220
00221
00222 static char flogname[128];
00223 static char sdate[32];
00224 get_sdate(sdate);
00225
00226 FILE *opf;
00227 sprintf(flogname, "%s/limbfit_%s_%lld_%lld.log",log_dir,sdate,bfsn,efsn);
00228 if((opf=fopen(flogname, "w")) == NULL)
00229 {
00230 fprintf(stderr, "**Can't open the log file %s\n", flogname);
00231 return(ERR_EXIT);
00232 }
00233 lf_logmsg("INFO", "APP", 0, 0, "Begin... ", log_msg_code, opf);
00234
00235
00236 static char tcam[20];
00237 static char tfid[100];
00238 static char tfil[20];
00239 static char tbase[128];
00240 sprintf(tfil, "[? quality = %s ", qual);
00241 if (cam == 1 || cam == 2) sprintf(tcam, " and camera = %d ", cam); else sprintf(tcam, " ");
00242 if (fid != 0) sprintf(tfid, " and fid = %d ", fid); else sprintf(tfid, " ");
00243
00244 sprintf(tbase, "%s%s%s ?]", tfil,tcam,tfid);
00245
00246
00247
00248
00249 static LIMBFIT_INPUT limbfit_vars ;
00250 static LIMBFIT_INPUT *lfv = &limbfit_vars;
00251 static LIMBFIT_OUTPUT limbfit_res ;
00252 static LIMBFIT_OUTPUT *lfr = &limbfit_res;
00253
00254 int * mask = (int *) malloc(sizeof(int)*IMG_SIZE);
00255 if(!mask)
00256 {
00257 lf_logmsg("ERROR", "APP", ERR_MALLOC_FAILED, 0,"malloc failed (mask)", log_msg_code, opf);
00258 return(ERR_EXIT);
00259 }
00260 lfv->mask=mask;
00261 lfv->pf_mask=&mask[0];
00262 lfv->pl_mask=&mask[IMG_SIZE-1];
00263
00264 lfr->code_name=CODE_NAME;
00265 lfr->code_version=CODE_VERSION;
00266 lfr->code_date=CODE_DATE;
00267 lfr->comment=comment;
00268 lfr->dsin=dsin;
00269 lfr->bld_vers=bld_vers;
00270 lfr->opf=opf;
00271 lfr->tmp_dir=tmp_dir;
00272 lfr->dsout=dsout;
00273 lfr->debug=debug;
00274 lfr->cenx=CENTX;
00275 lfr->ceny=CENTY;
00276 lfr->r_max=R_MAX;
00277 lfr->r_min=R_MIN;
00278
00279
00280 double iimcmy2,jjmcmx;
00281 float d,r,cmx,cmy,w2p,w2m;
00282 long tmask_one=0;
00283 long tmask_zero=0;
00284 long ii, jj;
00285
00286
00287 cmx=CENTX;
00288 cmy=CENTY;
00289 w2p=R_MAX;
00290 w2m=R_MIN;
00291 r=(w2p+w2m)/2;
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 for(ii = 0; ii < NAXIS_ROW; ii++)
00318 {
00319 iimcmy2=(ii-cmy)*(ii-cmy);
00320 for(jj = 0; jj < NAXIS_COL; jj++)
00321 {
00322 jjmcmx=jj-cmx;
00323 d=(float)sqrt(iimcmy2+(jjmcmx)*(jjmcmx));
00324 if (d<w2m || d>w2p)
00325 {
00326 lfv->mask[ii*NAXIS_COL+jj]=0;
00327 tmask_zero++;
00328 }
00329 else
00330 {
00331 lfv->mask[ii*NAXIS_COL+jj]=1;
00332 tmask_one++;
00333 }
00334 }
00335 }
00336 sprintf(log_msg,"Create Mask: total ann points = %ld /total points zeroed = %ld / r = %f, w2m = %f, w2p = %f, ", tmask_one,tmask_zero,r,w2m,w2p);
00337 lf_logmsg("INFO", "APP", 0, 0, log_msg, log_msg_code, lfr->opf);
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 long long numofrecs, frec, lrec;
00349 int numrec, numofchunks, i;
00350 numofrecs = (efsn - bfsn) + 1;
00351 numrec = NUMRECPERTRANS;
00352 numofchunks = numofrecs/numrec;
00353 if((numofrecs % numrec) != 0) numofchunks++;
00354 lrec = bfsn-1;
00355
00356 for(i = 0; i < numofchunks; i++)
00357 {
00358 frec = lrec+1;
00359 lrec = (frec + numrec)-1;
00360 if(lrec > efsn) lrec=efsn;
00361 sprintf(recrange, "%lld-%lld", frec, lrec);
00362 sprintf(open_dsname, "%s[][%s]%s", dsin, recrange,tbase);
00363
00364 sprintf(log_msg,"open %s -> %s (logdir: %s, tmpdir: %s, cam: %d, comment: %s , debug: %d)",
00365 open_dsname,dsout,log_dir,tmp_dir,cam,comment,debug);
00366
00367 lf_logmsg("INFO", "APP", 0, 0, log_msg, log_msg_code, opf);
00368 if(process_n_records_fsn(open_dsname, lfv, lfr, &result))
00369 {
00370 if (result < 0 && result > -400)
00371 {
00372 lf_logmsg("ERROR", "ABORT", result, 0, "", log_msg_code, opf);
00373 fprintf(opf,"lfwrp abort\nSee log: %s\n", flogname);
00374 fprintf(opf,"lfwrp: Restart it as %s[][%lld-%lld]%s\n", dsin,frec,efsn,tbase);
00375
00376 fclose(opf);
00377 return(ERR_EXIT);
00378 }
00379 }
00380 sprintf(log_msg,"close %s", open_dsname);
00381 lf_logmsg("INFO", "APP", 0, 0, log_msg, log_msg_code, opf);
00382 drms_server_end_transaction(drms_env,0,0);
00383 drms_server_begin_transaction(drms_env);
00384 }
00385 free(mask);
00386 lf_logmsg("INFO", "APP", 0, 0, "Batch End... ", log_msg_code, opf);
00387 fclose(opf);
00388 printf("Batch end\n");
00389
00390 return(0);
00391 }
00392