00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "limbfit_tas.h"
00015
00016 int do_one_limbfit(DRMS_Record_t *record_in,DRMS_Record_t *record_out,
00017 LIMBFIT_INPUT *input, LIMBFIT_OUTPUT *results, LIMBFIT_IO_PUT *ios, int *status)
00018 {
00019 static char *log_msg_code="do_one_limbfit";
00020 static char *series_name="limbfit data";
00021 results->series_name=series_name;
00022
00023
00024 int lf_retcode;
00025 int rstatus;
00026 char log_msg[200];
00027
00028 int seg_cnt;
00029 DRMS_Segment_t *segment_in;
00030 DRMS_Segment_t *segment_out;
00031 static DRMS_Array_t *img;
00032
00033
00034
00035 seg_cnt = drms_record_numsegments (record_in);
00036 if (seg_cnt < 1)
00037 {
00038 sprintf(log_msg,"no segments in selected record_in");
00039 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA, 0, log_msg, log_msg_code,results->opf);
00040 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00041 return(ERR_DRMS_READ_MISSING_DATA);
00042 }
00043 segment_in = drms_segment_lookupnum(record_in, 0);
00044 img = drms_segment_read(segment_in, DRMS_TYPE_FLOAT, &rstatus);
00045 if(!img) {
00046 sprintf(log_msg,"Can't do drms_segment_read() status=%d", rstatus);
00047 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA, 0, log_msg, log_msg_code, results->opf);
00048 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00049 return(ERR_DRMS_READ_MISSING_DATA);
00050 }
00051 else
00052 {
00053
00054 input->ix = drms_getkey_float(record_in, "X0_LF", &rstatus);
00055 if(rstatus) {
00056 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA,rstatus, "drms_getkey_float(X0_LF)", log_msg_code, results->opf);
00057 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00058 return(ERR_DRMS_READ_MISSING_DATA);
00059 }
00060 if(isnan(input->ix)) {
00061 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_XYR_LF,rstatus, "X0_LF missing", log_msg_code, results->opf);
00062 write_mini_output(PROCSTAT_NO_LF_XYR_LF_MISSING,record_in,record_out,0,results);
00063 return(ERR_DRMS_READ_MISSING_XYR_LF);
00064 }
00065 input->iy = drms_getkey_float(record_in, "Y0_LF", &rstatus);
00066 if(rstatus) {
00067 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA,rstatus, "drms_getkey_float(Y0_LF)", log_msg_code, results->opf);
00068 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00069 return(ERR_DRMS_READ_MISSING_DATA);
00070 }
00071 if(isnan(input->iy)) {
00072 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_XYR_LF,rstatus, "Y0_LF missing", log_msg_code, results->opf);
00073 write_mini_output(PROCSTAT_NO_LF_XYR_LF_MISSING,record_in,record_out,0,results);
00074 return(ERR_DRMS_READ_MISSING_XYR_LF);
00075 }
00076 input->ir = drms_getkey_float(record_in, "RSUN_LF", &rstatus);
00077 if(rstatus) {
00078 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA,rstatus, "drms_getkey_float(RSUN_LF)", log_msg_code, results->opf);
00079 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00080 return(ERR_DRMS_READ_MISSING_DATA);
00081 }
00082 if(isnan(input->ir)) {
00083 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_XYR_LF,rstatus, "RSUN_LF missing", log_msg_code, results->opf);
00084 write_mini_output(PROCSTAT_NO_LF_XYR_LF_MISSING,record_in,record_out,0,results);
00085 return(ERR_DRMS_READ_MISSING_XYR_LF);
00086 }
00087 input->img_sz0=img->axis[0];
00088 input->img_sz1=img->axis[1];
00089 input->data=img->data;
00090
00091 if (results->debug)
00092 {
00093 sprintf(log_msg,"X0_LF %f", input->ix);
00094 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00095 sprintf(log_msg,"Y0_LF %f", input->iy);
00096 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00097 sprintf(log_msg,"RSUN_LF %f", input->ir);
00098 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00099 }
00100
00101 lf_retcode=limbfit(input,results,ios);
00102 drms_free_array(img);
00103
00104 if (results->debug)
00105 {
00106 sprintf(log_msg,"XC %f", results->cenx);
00107 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00108 sprintf(log_msg,"YC %f", results->ceny);
00109 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00110 sprintf(log_msg,"R %f", results->radius);
00111 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00112 sprintf(log_msg,"Returned code %d", lf_retcode);
00113 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00114 sprintf(log_msg,"err1 %d", results->error1);
00115 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00116 sprintf(log_msg,"err2 %d", results->error2);
00117 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00118 }
00119
00120
00121
00122
00123
00124 if (input->fldf==0) results->fldfr=4;
00125
00126 if (lf_retcode >= 0 || lf_retcode == ERR_LIMBFIT_FIT_FAILED)
00127 {
00128 if (results->debug) printf("write in the db\n");
00129
00130
00131
00132
00133
00134
00135 drms_copykeys(record_out, record_in, 1, kDRMS_KeyClass_Explicit);
00136
00137 drms_keyword_setdate(record_out);
00138 write_lf_keywords(VOID,record_out, results,0, input->src);
00139
00140
00141 *status=0;
00142 rstatus=0;
00143
00144 int naxes[2];
00145
00146 naxes[0]=results->fits_ldfs_naxis1;
00147 naxes[1]=results->fits_ldfs_naxis2;
00148 DRMS_Array_t *array1=drms_array_create(DRMS_TYPE_FLOAT,2,naxes,results->fits_ldfs_data,&rstatus);
00149 if (rstatus)
00150 {
00151 lf_logmsg("ERROR", "DRMS", ERR_DRMS_ARRAY_CREATE,rstatus, "ERR_DRMS_ARRAY_CREATE(ldfs)", log_msg_code, results->opf);
00152 return ERR_DRMS_ARRAY_CREATE;
00153 }
00154 segment_out = drms_segment_lookupnum(record_out, 0);
00155 if (segment_out)
00156 {
00157 if ( drms_segment_write(segment_out,array1,0) )
00158 {
00159 lf_logmsg("ERROR", "DRMS", ERR_DRMS_SEGMENT_WRITE,rstatus, "ERR_DRMS_SEGMENT_WRITE(ldfs)", log_msg_code, results->opf);
00160 return ERR_DRMS_SEGMENT_WRITE;
00161 }
00162 }
00163 else
00164 {
00165 lf_logmsg("ERROR", "DRMS", ERR_DRMS_SEGMENT_LOOKUPNUM,rstatus, "ERR_DRMS_SEGMENT_LOOKUPNUM(ldfs)", log_msg_code, results->opf);
00166 return ERR_DRMS_SEGMENT_LOOKUPNUM;
00167 }
00168 drms_free_array (array1);
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 rstatus=0;
00186
00187
00188
00189
00190
00191 naxes[0]=results->fits_ab_naxis1;
00192 naxes[1]=results->fits_ab_naxis2;
00193 DRMS_Array_t *array2=drms_array_create(DRMS_TYPE_FLOAT,2,naxes,results->fits_alpha_beta,&rstatus);
00194 if (rstatus)
00195 {
00196 lf_logmsg("ERROR", "DRMS", ERR_DRMS_ARRAY_CREATE,rstatus, "ERR_DRMS_ARRAY_CREATE(ab)", log_msg_code, results->opf);
00197 return ERR_DRMS_ARRAY_CREATE;
00198 }
00199 segment_out = drms_segment_lookupnum(record_out, 1);
00200 if (segment_out)
00201 {
00202 if ( drms_segment_write(segment_out,array2,0) )
00203 {
00204 lf_logmsg("ERROR", "DRMS", ERR_DRMS_SEGMENT_WRITE,rstatus, "ERR_DRMS_SEGMENT_WRITE(ab)", log_msg_code, results->opf);
00205 return ERR_DRMS_SEGMENT_WRITE;
00206 }
00207 }
00208 else
00209 {
00210 lf_logmsg("ERROR", "DRMS", ERR_DRMS_SEGMENT_LOOKUPNUM,rstatus, "ERR_DRMS_SEGMENT_LOOKUPNUM(ab)", log_msg_code, results->opf);
00211 return ERR_DRMS_SEGMENT_LOOKUPNUM;
00212 }
00213 drms_free_array (array2);
00214
00215
00216 if (input->fldf==1)
00217 {
00218
00219 naxes[0]=results->fits_fldfs_nrows;
00220 naxes[1]=results->fits_fldfs_tfields;
00221 DRMS_Array_t *array4=drms_array_create(DRMS_TYPE_FLOAT,2,naxes,results->fits_fulldfs,&rstatus);
00222 if (rstatus)
00223 {
00224 lf_logmsg("ERROR", "DRMS", ERR_DRMS_ARRAY_CREATE,rstatus, "ERR_DRMS_ARRAY_CREATE(fullldfs)", log_msg_code, results->opf);
00225 return ERR_DRMS_ARRAY_CREATE;
00226 }
00227 segment_out = drms_segment_lookupnum(record_out, 3);
00228 if (segment_out)
00229 {
00230 if ( drms_segment_write(segment_out,array4,0) )
00231 {
00232 lf_logmsg("ERROR", "DRMS", ERR_DRMS_SEGMENT_WRITE,rstatus, "ERR_DRMS_SEGMENT_WRITE(fullldfs)", log_msg_code, results->opf);
00233 return ERR_DRMS_SEGMENT_WRITE;
00234 }
00235 }
00236 else
00237 {
00238 lf_logmsg("ERROR", "DRMS", ERR_DRMS_SEGMENT_LOOKUPNUM,rstatus, "ERR_DRMS_SEGMENT_LOOKUPNUM(fullldfs)", log_msg_code, results->opf);
00239 return ERR_DRMS_SEGMENT_LOOKUPNUM;
00240 }
00241 drms_free_array (array4);
00242 }
00243
00244
00245
00246
00247 rstatus=drms_setkey_string(record_out, "PROCSTAT", PROCSTAT_OK);
00248
00249 if (results->debug) printf("end write in the db %d\n",rstatus);
00250
00251
00252
00253
00254
00255
00256
00257 if (results->debug) printf("done...%u\n",input->fsn);
00258 lf_logmsg("INFO", "APP", 0, 0, "End writing in the DB", log_msg_code, results->opf);
00259 }
00260 else
00261 {
00262 sprintf(log_msg,"NO LDFS file created (ifail >0) or retcode <0 for fsn# %u", input->fsn);
00263 lf_logmsg("WARNING", "APP", ERR_LIMBFIT_FAILED, lf_retcode, log_msg, log_msg_code, results->opf);
00264 write_mini_output(PROCSTAT_NO_LF_FAILED,record_in,record_out,0,results);
00265
00266
00267
00268 if (results->error1 == 0)
00269 {
00270 free(results->fits_ldfs_data);
00271 free(results->fits_alpha_beta);
00272 if (results->fldfr<3) free(results->fits_fulldfs);
00273 }
00274 return(0);
00275 }
00276 }
00277
00278 return 0;
00279 }
00280
00281 int write_mini_output(char * errcode, DRMS_Record_t *record_in,DRMS_Record_t *record_out,
00282 int tbf, LIMBFIT_OUTPUT *results)
00283 {
00284
00285 char *log_msg_code="write_mini_output";
00286 lf_logmsg("INFO", "APP", 0, 0, "Writing only header", log_msg_code, results->opf);
00287
00288 drms_copykeys(record_out, record_in, 1, kDRMS_KeyClass_Explicit);
00289
00290 drms_keyword_setdate(record_out);
00291
00292 write_lf_keywords(errcode,record_out, results,1,0);
00293 if (tbf >=1)
00294 {
00295 free(results->fits_alpha_beta);
00296 if (results->fldfr<3) free(results->fits_fulldfs);
00297 if (tbf ==2) free(results->fits_ldfs_data);
00298 }
00299 return(0);
00300 }
00301
00302 int write_lf_keywords(char * errcode, DRMS_Record_t *record_out, LIMBFIT_OUTPUT *results,
00303 int pass, int src)
00304 {
00305
00306 drms_setkey_string(record_out, "SERIESCN", results->series_name);
00307 drms_setkey_string(record_out, "INSERIES", results->dsin);
00308 drms_setkey_string(record_out, "CODENAME", results->code_name);
00309 drms_setkey_string(record_out, "CODEVERS", results->code_version);
00310 drms_setkey_string(record_out, "CODEDATE", results->code_date);
00311 drms_setkey_float (record_out, "X_LFS", results->cenx);
00312 drms_setkey_float (record_out, "Y_LFS", results->ceny);
00313 drms_setkey_double(record_out, "R_LFS", results->radius);
00314 drms_setkey_int (record_out, "QUAL_LFS", results->quality);
00315 drms_setkey_int (record_out, "ERRO_LFS", results->error1);
00316 drms_setkey_int (record_out, "ERRO_FIT", results->error2);
00317 drms_setkey_int (record_out, "NB_FBINS", results->nb_fbins);
00318 drms_setkey_int (record_out, "FLDFR", results->fldfr);
00319 drms_setkey_float (record_out, "ES0", results->fits_es[0]);
00320 drms_setkey_float (record_out, "ES1", results->fits_es[1]);
00321 drms_setkey_float (record_out, "ES2", results->fits_es[2]);
00322 drms_setkey_float (record_out, "ES3", results->fits_es[3]);
00323 drms_setkey_float (record_out, "ES4", results->fits_es[4]);
00324 drms_setkey_float (record_out, "ES5", results->fits_es[5]);
00325 drms_setkey_float (record_out, "AS0", results->fits_as[0]);
00326 drms_setkey_float (record_out, "AS1", results->fits_as[1]);
00327 drms_setkey_float (record_out, "AS2", results->fits_as[2]);
00328 drms_setkey_float (record_out, "AS3", results->fits_as[3]);
00329 drms_setkey_float (record_out, "AS4", results->fits_as[4]);
00330 drms_setkey_float (record_out, "AS5", results->fits_as[5]);
00331 if (src<2)
00332 drms_setkey_double(record_out, "CMEAN", results->cmean);
00333 drms_setkey_int (record_out, "ANN_WD", results->ann_wd);
00334 drms_setkey_int (record_out, "MXSZANNV", results->mxszannv);
00335 drms_setkey_int (record_out, "NB_LDF", results->nb_ldf);
00336 drms_setkey_int (record_out, "NB_RDB", results->nb_rdb);
00337 drms_setkey_int (record_out, "NB_ABB", results->nb_abb);
00338 drms_setkey_double(record_out, "UP_LIMIT", results->up_limit);
00339 drms_setkey_double(record_out, "LO_LIMIT", results->lo_limit);
00340 drms_setkey_double(record_out, "INC_X", results->inc_x);
00341 drms_setkey_double(record_out, "INC_Y", results->inc_y);
00342 drms_setkey_int (record_out, "NFITPNTS", results->nfitpnts);
00343 drms_setkey_int (record_out, "NB_ITER", results->nb_iter);
00344 drms_setkey_int (record_out, "CEN_CALC", results->cc);
00345 drms_setkey_double(record_out, "AHI", results->ahi);
00346 drms_setkey_string(record_out, "COMMENTS", results->comment);
00347 drms_setkey_string(record_out, "BLD_VERS", results->bld_vers);
00348
00349
00350
00351 if (pass==1)
00352 drms_setkey_string(record_out, "PROCSTAT", errcode);
00353
00354 return(0);
00355 }