00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "limbfit.h"
00015
00016 void lf_logmsg4fitsio(char *log_msg,char *log_msg_code,char *kw,unsigned int fsn,int status, FILE *opf)
00017 {
00018 sprintf(log_msg,"can't fits_update_key[%s] for fsn# %u", kw,fsn);
00019 lf_logmsg("ERROR", "FITSIO", ERR_FITSIO, status, log_msg, log_msg_code, opf);
00020 fits_report_error(opf, status);
00021 }
00022
00023 int get_set_kw(int typ, char *kw, char *kw_txt, unsigned int fsn,
00024 DRMS_Record_t *record_in,DRMS_Record_t *record_out, fitsfile *outfptr, int tbf, LIMBFIT_OUTPUT *results, int *status)
00025 {
00026 float kw_f;
00027 double kw_d;
00028 int kw_i;
00029 char *kw_c;
00030 int rstatus;
00031 char log_msg[200];
00032 char *log_msg_code="get_set_kw";
00033
00034 switch( typ )
00035 {
00036 case 1:
00037 kw_c = drms_getkey_string(record_in, kw, &rstatus);
00038 break;
00039 case 2:
00040 kw_i = drms_getkey_int(record_in, kw, &rstatus);
00041 break;
00042 case 3:
00043 kw_f = drms_getkey_float(record_in, kw, &rstatus);
00044 break;
00045 case 4:
00046 kw_d = drms_getkey_double(record_in, kw, &rstatus);
00047 }
00048 if(rstatus) {
00049 sprintf(log_msg,"drms_getkey_xxxx(%s)",kw);
00050 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA,rstatus, log_msg, log_msg_code, results->opf);
00051 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,tbf,results);
00052 *status=ERR_DRMS_READ_MISSING_DATA;
00053 return(ERR_DRMS_READ_MISSING_DATA);
00054 }
00055 switch( typ )
00056 {
00057 case 1:
00058 fits_update_key(outfptr, TSTRING, kw, kw_c, kw_txt, &rstatus);
00059 break;
00060 case 2:
00061 fits_update_key(outfptr, TINT, kw, &kw_i, kw_txt, &rstatus);
00062 break;
00063 case 3:
00064 fits_update_key(outfptr, TFLOAT, kw, &kw_f, kw_txt, &rstatus);
00065 break;
00066 case 4:
00067 fits_update_key(outfptr, TDOUBLE, kw, &kw_d, kw_txt, &rstatus);
00068 break;
00069 }
00070
00071 if ( rstatus != 0)
00072 {
00073 lf_logmsg4fitsio(log_msg, log_msg_code,kw,fsn,rstatus,results->opf);
00074 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,tbf,results);
00075 *status=ERR_DRMS_READ_MISSING_DATA;
00076 return ERR_FITSIO;
00077 }
00078 return(0);
00079 }
00080
00081 int do_one_limbfit(unsigned int fsn, DRMS_Record_t *record_in,DRMS_Record_t *record_out,
00082 LIMBFIT_INPUT *input, LIMBFIT_OUTPUT *results, LIMBFIT_IO_PUT *ios, int *status)
00083 {
00084 static char *log_msg_code="do_one_limbfit";
00085 static char *series_name="limbfit data";
00086 static char *file_type="full FITS";
00087 static char *proc_stat=PROCSTAT_OK;
00088
00089 int lf_retcode;
00090 int rstatus;
00091 char log_msg[200];
00092
00093 int seg_cnt;
00094 DRMS_Segment_t *segment_in;
00095 DRMS_Segment_t *segment_out;
00096 static DRMS_Array_t *img;
00097
00098
00099
00100 seg_cnt = drms_record_numsegments (record_in);
00101 if (seg_cnt < 1)
00102 {
00103 sprintf(log_msg,"no segments in selected record_in");
00104 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA, 0, log_msg, log_msg_code,results->opf);
00105 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00106 return(ERR_DRMS_READ_MISSING_DATA);
00107 }
00108 segment_in = drms_segment_lookupnum(record_in, 0);
00109 img = drms_segment_read(segment_in, DRMS_TYPE_FLOAT, &rstatus);
00110 if(!img) {
00111 sprintf(log_msg,"Can't do drms_segment_read() status=%d", rstatus);
00112 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA, 0, log_msg, log_msg_code, results->opf);
00113 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00114 return(ERR_DRMS_READ_MISSING_DATA);
00115 }
00116 else
00117 {
00118
00119
00120 input->ix = drms_getkey_float(record_in, "X0_LF", &rstatus);
00121 if(rstatus) {
00122 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA,rstatus, "drms_getkey_float(X0_LF)", log_msg_code, results->opf);
00123 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00124 return(ERR_DRMS_READ_MISSING_DATA);
00125 }
00126 if(isnan(input->ix)) {
00127 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_XYR_LF,rstatus, "X0_LF missing", log_msg_code, results->opf);
00128 write_mini_output(PROCSTAT_NO_LF_XYR_LF_MISSING,record_in,record_out,0,results);
00129 return(ERR_DRMS_READ_MISSING_XYR_LF);
00130 }
00131 input->iy = drms_getkey_float(record_in, "Y0_LF", &rstatus);
00132 if(rstatus) {
00133 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA,rstatus, "drms_getkey_float(Y0_LF)", log_msg_code, results->opf);
00134 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00135 return(ERR_DRMS_READ_MISSING_DATA);
00136 }
00137 if(isnan(input->iy)) {
00138 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_XYR_LF,rstatus, "Y0_LF missing", log_msg_code, results->opf);
00139 write_mini_output(PROCSTAT_NO_LF_XYR_LF_MISSING,record_in,record_out,0,results);
00140 return(ERR_DRMS_READ_MISSING_XYR_LF);
00141 }
00142 input->ir = drms_getkey_float(record_in, "RSUN_LF", &rstatus);
00143 if(rstatus) {
00144 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA,rstatus, "drms_getkey_float(RSUN_LF)", log_msg_code, results->opf);
00145 write_mini_output(PROCSTAT_NO_LF_DB_READ_PB,record_in,record_out,0,results);
00146 return(ERR_DRMS_READ_MISSING_DATA);
00147 }
00148 if(isnan(input->ir)) {
00149 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_XYR_LF,rstatus, "RSUN_LF missing", log_msg_code, results->opf);
00150 write_mini_output(PROCSTAT_NO_LF_XYR_LF_MISSING,record_in,record_out,0,results);
00151 return(ERR_DRMS_READ_MISSING_XYR_LF);
00152 }
00153 input->img_sz0=img->axis[0];
00154 input->img_sz1=img->axis[1];
00155 input->data=img->data;
00156
00157 if (results->debug)
00158 {
00159 sprintf(log_msg,"X0_LF %f", input->ix);
00160 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00161 sprintf(log_msg,"Y0_LF %f", input->iy);
00162 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00163 sprintf(log_msg,"RSUN_LF %f", input->ir);
00164 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00165 }
00166
00167 lf_retcode=limbfit(input,results,ios);
00168 drms_free_array(img);
00169
00170 if (results->debug)
00171 {
00172 sprintf(log_msg,"XC %f", results->cenx);
00173 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00174 sprintf(log_msg,"YC %f", results->ceny);
00175 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00176 sprintf(log_msg,"R %f", results->radius);
00177 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00178 sprintf(log_msg,"Returned code %d", lf_retcode);
00179 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00180 sprintf(log_msg,"err1 %d", results->error1);
00181 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00182 sprintf(log_msg,"err2 %d", results->error2);
00183 lf_logmsg("DEBUG", "INFO", 0, 0, log_msg, log_msg_code, results->opf);
00184 }
00185
00186
00187
00188
00189
00190 if (input->fldf==0) results->fldfr=4;
00191
00192 if (lf_retcode >= 0 || lf_retcode == ERR_LIMBFIT_FIT_FAILED)
00193 {
00194 if (results->debug) printf("write in the db\n");
00195
00196
00197
00198
00199
00200
00201
00202 drms_copykeys(record_out, record_in, 1, kDRMS_KeyClass_All);
00203
00204
00205 drms_keyword_setdate(record_out);
00206
00207
00208
00209
00210 rstatus=drms_setkey_string(record_out, "SERIESCN", series_name);
00211 rstatus=drms_setkey_string(record_out, "INSERIES", results->dsin);
00212 rstatus=drms_setkey_string(record_out, "CODENAME", results->code_name);
00213 rstatus=drms_setkey_string(record_out, "CODEVERS", results->code_version);
00214 rstatus=drms_setkey_string(record_out, "CODEDATE", results->code_date);
00215 rstatus=drms_setkey_float (record_out, "X_LFS", results->cenx);
00216 rstatus=drms_setkey_float (record_out, "Y_LFS", results->ceny);
00217 rstatus=drms_setkey_double(record_out, "R_LFS", results->radius);
00218 rstatus=drms_setkey_int (record_out, "QUAL_LFS", results->quality);
00219 rstatus=drms_setkey_int (record_out, "ERRO_LFS", results->error1);
00220 rstatus=drms_setkey_int (record_out, "ERRO_FIT", results->error2);
00221 rstatus=drms_setkey_int (record_out, "NB_FBINS", results->nb_fbins);
00222 rstatus=drms_setkey_int (record_out, "FLDFR", results->fldfr);
00223 rstatus=drms_setkey_double(record_out, "MAX_LIMB", results->max_limb);
00224 rstatus=drms_setkey_double(record_out, "CMEAN", results->cmean);
00225 rstatus=drms_setkey_int (record_out, "ANN_WD", results->ann_wd);
00226 rstatus=drms_setkey_int (record_out, "MXSZANNV", results->mxszannv);
00227 rstatus=drms_setkey_int (record_out, "NB_LDF", results->nb_ldf);
00228 rstatus=drms_setkey_int (record_out, "NB_RDB", results->nb_rdb);
00229 rstatus=drms_setkey_int (record_out, "NB_ABB", results->nb_abb);
00230 rstatus=drms_setkey_double(record_out, "UP_LIMIT", results->up_limit);
00231 rstatus=drms_setkey_double(record_out, "LO_LIMIT", results->lo_limit);
00232 rstatus=drms_setkey_double(record_out, "INC_X", results->inc_x);
00233 rstatus=drms_setkey_double(record_out, "INC_Y", results->inc_y);
00234 rstatus=drms_setkey_int (record_out, "NFITPNTS", results->nfitpnts);
00235 rstatus=drms_setkey_int (record_out, "NB_ITER", results->nb_iter);
00236 rstatus=drms_setkey_int (record_out, "CEN_CALC", input->cc);
00237 rstatus=drms_setkey_double(record_out, "AHI", results->ahi);
00238 rstatus=drms_setkey_string(record_out, "COMMENTS", results->comment);
00239 rstatus=drms_setkey_string(record_out, "BLD_VERS", results->bld_vers);
00240
00241
00242
00243
00244
00245
00246 long nelements, firstrow, firstelem;
00247 static char filenameout[128];
00248 sprintf(filenameout,"%s/%u_full.fits",results->tmp_dir,fsn);
00249
00250 firstrow=1;
00251 firstelem=1;
00252 *status=0;
00253 rstatus=0;
00254 remove(filenameout);
00255 fitsfile *outfptr;
00256 if (fits_create_file(&outfptr, filenameout, &rstatus))
00257 {
00258 fits_report_error(results->opf, rstatus);
00259 lf_logmsg4fitsio(log_msg, log_msg_code, "fits_create_file()",fsn,rstatus,results->opf);
00260 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,2,results);
00261 return ERR_FITSIO;
00262 }
00263
00264 long l_naxes[2];
00265 l_naxes[0]=results->fits_ldfs_naxis1;
00266 l_naxes[1]=results->fits_ldfs_naxis2;
00267 nelements=l_naxes[0]*l_naxes[1];
00268 int nax=2;
00269 if ( fits_create_img(outfptr, FLOAT_IMG, nax, l_naxes, &rstatus) )
00270 {
00271 lf_logmsg4fitsio(log_msg, log_msg_code, "fits_create_img()",fsn,rstatus,results->opf);
00272 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,2,results);
00273 return ERR_FITSIO;
00274 }
00275 if ( fits_write_img(outfptr, TFLOAT, 1, nelements, results->fits_ldfs_data, &rstatus) )
00276 {
00277 lf_logmsg4fitsio(log_msg, log_msg_code, "fits_write_img()",fsn,rstatus,results->opf);
00278 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,2,results);
00279 return ERR_FITSIO;
00280 }
00281
00282
00283
00284 if(get_set_kw(1,"ORIGIN","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00285
00286 TIME tobs;
00287 static char s_tobs[50];
00288 tobs = drms_getkey_time(record_in, "DATE__OBS", &rstatus);
00289 sprint_time(s_tobs, tobs, "UTC", 0);
00290 if ( fits_update_key(outfptr, TSTRING, "DATE-OBS", &s_tobs, "",&rstatus) )
00291 {
00292 lf_logmsg4fitsio(log_msg, log_msg_code, "(DATE__OBS)",fsn,rstatus,results->opf);
00293 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00294 return ERR_FITSIO;
00295 }
00296 tobs = drms_getkey_time(record_in, "T_OBS", &rstatus);
00297 sprint_time(s_tobs, tobs, "UTC", 0);
00298 if ( fits_update_key(outfptr, TSTRING, "T_OBS", s_tobs, "",&rstatus) )
00299 {
00300 lf_logmsg4fitsio(log_msg, log_msg_code, "(T_OBS)",fsn,rstatus,results->opf);
00301 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00302 return ERR_FITSIO;
00303 }
00304
00305 if(get_set_kw(2,"CAMERA","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00306 if(get_set_kw(4,"EXPTIME","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00307 if(get_set_kw(3,"EXPSDEV","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00308 if(get_set_kw(2,"WAVELNTH","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00309 if(get_set_kw(1,"WAVEUNIT","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00310 if ( fits_update_key(outfptr, TINT, "FSN", &fsn, "",&rstatus) )
00311 {
00312 lf_logmsg4fitsio(log_msg, log_msg_code, "(FSN)",fsn,rstatus,results->opf);
00313 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00314 return ERR_FITSIO;
00315 }
00316 if(get_set_kw(2,"FID","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00317 if(get_set_kw(2,"DATAMIN","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00318 if(get_set_kw(2,"DATAMAX","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00319 if(get_set_kw(2,"DATAMEDN","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00320 if(get_set_kw(3,"DATAMEAN","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00321 if(get_set_kw(3,"DATARMS","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00322 if(get_set_kw(1,"FLAT_REC","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00323 if(get_set_kw(1,"CTYPE1","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00324 if(get_set_kw(1,"CUNIT1","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00325 if(get_set_kw(3,"CRVAL1","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00326 if(get_set_kw(3,"CDELT1","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00327 if(get_set_kw(3,"CRPIX1","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00328 if(get_set_kw(1,"CTYPE2","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00329 if(get_set_kw(1,"CUNIT2","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00330 if(get_set_kw(3,"CRVAL2","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00331 if(get_set_kw(3,"CDELT2","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00332 if(get_set_kw(3,"CRPIX2","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00333 if(get_set_kw(3,"CROTA2","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00334 if(get_set_kw(3,"R_SUN","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00335 if(get_set_kw(1,"MPO_REC","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00336 if(get_set_kw(3,"INST_ROT","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00337 if (isnan(input->ix)) { input->ix=0.0;}
00338 if ( fits_update_key(outfptr, TDOUBLE, "X0_LF", &input->ix, "",&rstatus) )
00339 {
00340 lf_logmsg4fitsio(log_msg, log_msg_code, "(X0_LF)",fsn,rstatus,results->opf);
00341 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00342 return ERR_FITSIO;
00343 }
00344 if (isnan(input->iy)) { input->iy=0.0;}
00345 if ( fits_update_key(outfptr, TDOUBLE, "Y0_LF", &input->iy, "",&rstatus) )
00346 {
00347 lf_logmsg4fitsio(log_msg, log_msg_code, "(Y0_LF)",fsn,rstatus,results->opf);
00348 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00349 return ERR_FITSIO;
00350 }
00351 if (isnan(input->ir)) { input->ir=0.0;}
00352 if ( fits_update_key(outfptr, TDOUBLE, "RSUN_LF", &input->ir, "",&rstatus) )
00353 {
00354 lf_logmsg4fitsio(log_msg, log_msg_code, "(RSUN_LF)",fsn,rstatus,results->opf);
00355 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00356 return ERR_FITSIO;
00357 }
00358 if(get_set_kw(1,"ASD_REC","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00359 if(get_set_kw(3,"SAT_ROT","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00360 if(get_set_kw(1,"ORB_REC","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00361 if(get_set_kw(4,"DSUN_OBS","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00362 if(get_set_kw(4,"RSUN_OBS","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00363 if(get_set_kw(4,"HAEX_OBS","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00364 if(get_set_kw(4,"HAEY_OBS","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00365 if(get_set_kw(4,"HAEZ_OBS","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00366 if(get_set_kw(2,"HPLTID","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00367 if(get_set_kw(2,"HFTSACID","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00368 if(get_set_kw(2,"HWLTID","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00369 if(get_set_kw(2,"HCFTID","",fsn,record_in,record_out,outfptr,1,results,status)) return(*status);
00370 if ( fits_update_key(outfptr, TSTRING, "SERIESCN", series_name, "Series Name",&rstatus) )
00371 {
00372 lf_logmsg4fitsio(log_msg, log_msg_code, "(SERIESCN)",fsn,rstatus,results->opf);
00373 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00374 return ERR_FITSIO;
00375 }
00376 if ( fits_update_key(outfptr, TSTRING, "FILETYPE", file_type, "File content",&rstatus) )
00377 {
00378 lf_logmsg4fitsio(log_msg, log_msg_code, "(FILETYPE)",fsn,rstatus,results->opf);
00379 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00380 return ERR_FITSIO;
00381 }
00382 if ( fits_update_key(outfptr, TSTRING, "CODENAME", results->code_name, "Code Name",&rstatus) )
00383 {
00384 lf_logmsg4fitsio(log_msg, log_msg_code, "(CODENAME)",fsn,rstatus,results->opf);
00385 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00386 return ERR_FITSIO;
00387 }
00388 if ( fits_update_key(outfptr, TSTRING, "CODEVERS", results->code_version, "Code Version",&rstatus) )
00389 {
00390 lf_logmsg4fitsio(log_msg, log_msg_code, "(CODEVERS)",fsn,rstatus,results->opf);
00391 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00392 return ERR_FITSIO;
00393 }
00394 if ( fits_update_key(outfptr, TSTRING, "CODEDATE", results->code_date, "Code Date",&rstatus) )
00395 {
00396 lf_logmsg4fitsio(log_msg, log_msg_code, "(CODEDATE)",fsn,rstatus,results->opf);
00397 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00398 return ERR_FITSIO;
00399 }
00400 if ( fits_update_key(outfptr, TSTRING, "PROCSTAT", proc_stat, "Processing rstatus code",&rstatus) )
00401 {
00402 lf_logmsg4fitsio(log_msg, log_msg_code, "(PROCSTAT)",fsn,rstatus,results->opf);
00403 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00404 return ERR_FITSIO;
00405 }
00406
00407 tobs=drms_keyword_getdate(record_out);
00408 sprint_time(s_tobs, tobs, "UTC", 0);
00409 if ( fits_update_key(outfptr, TSTRING, "DATE", s_tobs, "",&rstatus) )
00410 {
00411 lf_logmsg4fitsio(log_msg, log_msg_code, "(DATE)",fsn,rstatus,results->opf);
00412 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00413 return ERR_FITSIO;
00414 }
00415 if ( fits_update_key(outfptr, TFLOAT, "X_LFS", &results->cenx, "Center Position X",&rstatus) )
00416 {
00417 lf_logmsg4fitsio(log_msg, log_msg_code, "(X_LFS)",fsn,rstatus,results->opf);
00418 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00419 return ERR_FITSIO;
00420 }
00421 if ( fits_update_key(outfptr, TFLOAT, "Y_LFS", &results->ceny, "Center Position Y",&rstatus) )
00422 {
00423 lf_logmsg4fitsio(log_msg, log_msg_code, "(Y_LFS)",fsn,rstatus,results->opf);
00424 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00425 return ERR_FITSIO;
00426 }
00427 if ( fits_update_key(outfptr, TDOUBLE, "R_LFS", &results->radius, "Solar Radius",&rstatus) )
00428 {
00429 lf_logmsg4fitsio(log_msg, log_msg_code, "(R_LFS)",fsn,rstatus,results->opf);
00430 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00431 return ERR_FITSIO;
00432 }
00433 if ( fits_update_key(outfptr, TDOUBLE, "CMEAN", &results->cmean, "Mean value of the 500x500 pixels center",&rstatus) )
00434 {
00435 lf_logmsg4fitsio(log_msg, log_msg_code, "(CMEAN)",fsn,rstatus,results->opf);
00436 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00437 return ERR_FITSIO;
00438 }
00439 if ( fits_update_key(outfptr, TDOUBLE, "MAX_LIMB", &results->cmean, "Maximum value on the limb",&rstatus) )
00440 {
00441 lf_logmsg4fitsio(log_msg, log_msg_code, "(MAX_LIMB)",fsn,rstatus,results->opf);
00442 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00443 return ERR_FITSIO;
00444 }
00445 if ( fits_update_key(outfptr, TINT, "QUAL_LFS", &results->quality, "Processing quality indicator",&rstatus) )
00446 {
00447 lf_logmsg4fitsio(log_msg, log_msg_code, "(QUAL_LFS)",fsn,rstatus,results->opf);
00448 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00449 return ERR_FITSIO;
00450 }
00451 if ( fits_update_key(outfptr, TINT, "ERRO_LFS", &results->error1, "Limb.f Processing error code",&rstatus) )
00452 {
00453 lf_logmsg4fitsio(log_msg, log_msg_code, "(ERRO_LFS)",fsn,rstatus,results->opf);
00454 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00455 return ERR_FITSIO;
00456 }
00457 if ( fits_update_key(outfptr, TINT, "ERRO_FIT", &results->error2, "Fitting Processing error code",&rstatus) )
00458 {
00459 lf_logmsg4fitsio(log_msg, log_msg_code, "(ERRO_FIT)",fsn,rstatus,results->opf);
00460 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00461 return ERR_FITSIO;
00462 }
00463 if ( fits_update_key(outfptr, TINT, "FLDFR", &results->fldfr, "",&rstatus) )
00464 {
00465 lf_logmsg4fitsio(log_msg, log_msg_code, "(FLDFR)",fsn,rstatus,results->opf);
00466 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00467 return ERR_FITSIO;
00468 }
00469 if ( fits_update_key(outfptr, TINT, "NB_FBINS", &results->nb_fbins, "",&rstatus) )
00470 {
00471 lf_logmsg4fitsio(log_msg, log_msg_code, "(NB_FBINS)",fsn,rstatus,results->opf);
00472 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00473 return ERR_FITSIO;
00474 }
00475 if ( fits_update_key(outfptr, TINT, "ANN_WD", &results->ann_wd, "",&rstatus) )
00476 {
00477 lf_logmsg4fitsio(log_msg, log_msg_code, "(ANN_WD)",fsn,rstatus,results->opf);
00478 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00479 return ERR_FITSIO;
00480 }
00481 if ( fits_update_key(outfptr, TINT, "MXSZANNV", &results->mxszannv, "",&rstatus) )
00482 {
00483 lf_logmsg4fitsio(log_msg, log_msg_code, "(MXSZANNV)",fsn,rstatus,results->opf);
00484 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00485 return ERR_FITSIO;
00486 }
00487 if ( fits_update_key(outfptr, TINT, "NB_LDF", &results->nb_ldf, "",&rstatus) )
00488 {
00489 lf_logmsg4fitsio(log_msg, log_msg_code, "(NB_LDF)",fsn,rstatus,results->opf);
00490 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00491 return ERR_FITSIO;
00492 }
00493 if ( fits_update_key(outfptr, TINT, "NB_RDB", &results->nb_rdb, "",&rstatus) )
00494 {
00495 lf_logmsg4fitsio(log_msg, log_msg_code, "(NB_RDB)",fsn,rstatus,results->opf);
00496 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00497 return ERR_FITSIO;
00498 }
00499 if ( fits_update_key(outfptr, TINT, "NB_ABB", &results->nb_abb, "",&rstatus) )
00500 {
00501 lf_logmsg4fitsio(log_msg, log_msg_code, "(NB_ABB)",fsn,rstatus,results->opf);
00502 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00503 return ERR_FITSIO;
00504 }
00505 if ( fits_update_key(outfptr, TDOUBLE, "UP_LIMIT", &results->up_limit, "",&rstatus) )
00506 {
00507 lf_logmsg4fitsio(log_msg, log_msg_code, "(UP_LIMIT)",fsn,rstatus,results->opf);
00508 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00509 return ERR_FITSIO;
00510 }
00511 if ( fits_update_key(outfptr, TDOUBLE, "LO_LIMIT", &results->lo_limit, "",&rstatus) )
00512 {
00513 lf_logmsg4fitsio(log_msg, log_msg_code, "(LO_LIMIT)",fsn,rstatus,results->opf);
00514 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00515 return ERR_FITSIO;
00516 }
00517 if ( fits_update_key(outfptr, TDOUBLE, "INC_X", &results->inc_x, "",&rstatus) )
00518 {
00519 lf_logmsg4fitsio(log_msg, log_msg_code, "(INC_X)",fsn,rstatus,results->opf);
00520 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00521 return ERR_FITSIO;
00522 }
00523 if ( fits_update_key(outfptr, TDOUBLE, "INC_Y", &results->inc_y, "",&rstatus) )
00524 {
00525 lf_logmsg4fitsio(log_msg, log_msg_code, "(INC_Y)",fsn,rstatus,results->opf);
00526 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00527 return ERR_FITSIO;
00528 }
00529 if ( fits_update_key(outfptr, TINT, "NFITPNTS", &results->nfitpnts, "",&rstatus) )
00530 {
00531 lf_logmsg4fitsio(log_msg, log_msg_code, "(NFITPNTS)",fsn,rstatus,results->opf);
00532 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00533 return ERR_FITSIO;
00534 }
00535 if ( fits_update_key(outfptr, TINT, "NB_ITER", &results->nb_iter, "",&rstatus) )
00536 {
00537 lf_logmsg4fitsio(log_msg, log_msg_code, "(NB_ITER)",fsn,rstatus,results->opf);
00538 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00539 return ERR_FITSIO;
00540 }
00541
00542 rstatus=0;
00543 int i;
00544
00545
00546 if (results->nb_iter==1)
00547 {
00548 char *tunitAB[] = { "","" };
00549 char *ttypeAB[] = { "Alpha", "Beta"};
00550 char *tformAB[] = { "1E","1E" };
00551 char extnameAB[]="LDF info / Alpha&Beta";
00552 if ( fits_create_tbl( outfptr, BINARY_TBL, results->fits_ab_nrows, results->fits_ab_tfields,
00553 ttypeAB, tformAB, tunitAB, extnameAB, &rstatus) )
00554 {
00555 fits_report_error(results->opf, rstatus);
00556 return ERR_FITSIO;
00557 }
00558 }
00559 else
00560 {
00561 char *tunitAB[] = { "","",""};
00562 char *ttypeAB[] = { "Alpha", "Beta","Beta Prev"};
00563 char *tformAB[] = { "1E","1E","1E" };
00564 char extnameAB[]="LDF info / Alpha&Beta[&Beta Prev]";
00565 if ( fits_create_tbl( outfptr, BINARY_TBL, results->fits_ab_nrows, results->fits_ab_tfields,
00566 ttypeAB, tformAB, tunitAB, extnameAB, &rstatus) )
00567 {
00568 fits_report_error(results->opf, rstatus);
00569 return ERR_FITSIO;
00570 }
00571 }
00572 for (i=1;i<=results->fits_ab_tfields;i++)
00573 {
00574 if (fits_write_col(outfptr, TFLOAT, i, firstrow, firstelem, results->fits_ab_nrows,
00575 results->fits_alpha_beta+(i-1)*results->fits_ab_nrows, &rstatus))
00576 {
00577 lf_logmsg4fitsio(log_msg, log_msg_code, "fits_write_col(AB)",fsn,rstatus,results->opf);
00578 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00579 return ERR_FITSIO;
00580 }
00581 }
00582
00583
00584 char extnameA[] = "LDF info / As / Es / Radius / IP";
00585 char *tunitAE[] = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
00586 char *tformAE[] = { "1D", "1D", "1D", "1D", "1D", "1D", "1D", "1D", "1D", "1D", "1D", "1D", "1D", "1D" };
00587 char *ttypeA[] = { "A0", "A1", "A2", "A3", "A4", "A5", "E0", "E1", "E2", "E3", "E4", "E5", "Radius", "IP1"};
00588
00589 if ( fits_create_tbl(outfptr, BINARY_TBL, results->fits_params_nrows, results->fits_params_tfields,
00590 ttypeA, tformAE,tunitAE, extnameA, &rstatus) )
00591 {
00592 fits_report_error(results->opf, rstatus);
00593 return ERR_FITSIO;
00594 }
00595
00596 for (i=1;i<=results->fits_params_tfields;i++)
00597 {
00598 if (fits_write_col(outfptr, TDOUBLE, i, firstrow, firstelem, results->fits_params_nrows,
00599 results->fits_params+(i-1)*results->fits_params_nrows, &rstatus))
00600 {
00601 lf_logmsg4fitsio(log_msg, log_msg_code, "fits_write_col(PARAMS)",fsn,rstatus,results->opf);
00602 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00603 return ERR_FITSIO;
00604 }
00605 }
00606
00607 if (input->fldf==1)
00608 {
00609
00610 char *tunitF[] = { "","" };
00611 char *ttypeF[] = { "Intensity", "Radius"};
00612 char *tformF[] = { "1E","1E" };
00613 char extnameF[]="Full LDF ";
00614 if ( fits_create_tbl( outfptr, BINARY_TBL, results->fits_fldfs_nrows, results->fits_fldfs_tfields,
00615 ttypeF, tformF, tunitF, extnameF, &rstatus) )
00616 {
00617 fits_report_error(results->opf, rstatus);
00618 return ERR_FITSIO;
00619 }
00620 for (i=1;i<=results->fits_fldfs_tfields;i++)
00621 {
00622 if (fits_write_col(outfptr, TFLOAT, i, firstrow, firstelem, results->fits_fldfs_nrows,
00623 results->fits_fulldfs+(i-1)*results->fits_fldfs_nrows, &rstatus))
00624 {
00625 lf_logmsg4fitsio(log_msg, log_msg_code, "fits_write_col(FLDF)",fsn,rstatus,results->opf);
00626 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00627 return ERR_FITSIO;
00628 }
00629 }
00630 }
00631
00632
00633
00634 if ( fits_update_key(outfptr, TSTRING, "PROCSTAT", proc_stat, "Processing rstatus code",&rstatus) )
00635 {
00636 lf_logmsg4fitsio(log_msg, log_msg_code, "(PROCSTAT)",fsn,rstatus,results->opf);
00637 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00638 return ERR_FITSIO;
00639 }
00640
00641
00642 if ( fits_close_file(outfptr, &rstatus) )
00643 {
00644 lf_logmsg4fitsio(log_msg, log_msg_code, "fits_close_file()",fsn,rstatus,results->opf);
00645 write_mini_output(PROCSTAT_NO_LF_FITS_WRITE_PB,record_in,record_out,1,results);
00646 return ERR_FITSIO;
00647 }
00648
00649
00650 segment_out = drms_segment_lookupnum (record_out, 0);
00651
00652 rstatus=drms_segment_write_from_file(segment_out,filenameout);
00653 if(rstatus) {
00654 sprintf(log_msg,"can't drms_segment_write_from_file() for fsn# %u", fsn);
00655 lf_logmsg("ERROR", "DRMS", ERR_DRMS_WRITE, rstatus, log_msg, log_msg_code, results->opf);
00656 return(ERR_DRMS_WRITE);
00657 }
00658
00659 rstatus=drms_setkey_string(record_out, "PROCSTAT", PROCSTAT_OK);
00660 if (!rstatus) remove(filenameout);
00661 else
00662 {
00663 sprintf(log_msg,"can't drms_setkey_string(PROCSTAT) for fsn# %u", fsn);
00664 lf_logmsg("ERROR", "DRMS", ERR_DRMS_WRITE, rstatus, log_msg, log_msg_code, results->opf);
00665 return(ERR_DRMS_WRITE);
00666 }
00667 if (results->debug) printf("end write in the db %d\n",rstatus);
00668
00669 free(results->fits_ldfs_data);
00670 free(results->fits_alpha_beta);
00671 free(results->fits_params);
00672 if (results->fldfr<3) free(results->fits_fulldfs);
00673
00674 if (results->debug) printf("done...%u\n",fsn);
00675 lf_logmsg("INFO", "APP", 0, 0, "End writing in the DB", log_msg_code, results->opf);
00676 }
00677 else
00678 {
00679 sprintf(log_msg,"NO LDFS file created (ifail >0) or retcode <0 for fsn# %u", fsn);
00680 lf_logmsg("WARNING", "APP", ERR_LIMBFIT_FAILED, lf_retcode, log_msg, log_msg_code, results->opf);
00681 write_mini_output(PROCSTAT_NO_LF_FAILED,record_in,record_out,0,results);
00682
00683
00684
00685 if (results->error1 == 0)
00686 {
00687 free(results->fits_ldfs_data);
00688 free(results->fits_alpha_beta);
00689 free(results->fits_params);
00690 if (results->fldfr<3) free(results->fits_fulldfs);
00691 }
00692 return(0);
00693 }
00694 }
00695
00696 return 0;
00697 }
00698
00699 int write_mini_output(char * errcode, DRMS_Record_t *record_in,DRMS_Record_t *record_out,
00700 int tbf, LIMBFIT_OUTPUT *results)
00701 {
00702
00703 char *log_msg_code="write_mini_output";
00704 lf_logmsg("INFO", "APP", 0, 0, "Writing only header", log_msg_code, results->opf);
00705
00706 drms_copykeys(record_out, record_in, 1, kDRMS_KeyClass_All);
00707
00708 drms_keyword_setdate(record_out);
00709
00710
00711 drms_setkey_string(record_out, "SERIESCN", "Limbfit data");
00712 drms_setkey_string(record_out, "CODENAME", results->code_name);
00713 drms_setkey_string(record_out, "CODEVERS", results->code_version);
00714 drms_setkey_string(record_out, "CODEDATE", results->code_date);
00715 drms_setkey_string(record_out, "INSERIES", results->dsin);
00716 drms_setkey_string(record_out, "COMMENTS", results->comment);
00717 drms_setkey_string(record_out, "PROCSTAT", errcode);
00718 drms_setkey_int (record_out, "QUAL_LFS", results->quality);
00719 drms_setkey_int (record_out, "ERRO_LFS", results->error1);
00720 drms_setkey_int (record_out, "ERRO_FIT", results->error2);
00721 drms_setkey_int (record_out, "ANN_WD", results->ann_wd);
00722 drms_setkey_int (record_out, "MXSZANNV", results->mxszannv);
00723 drms_setkey_int (record_out, "NB_LDF", results->nb_ldf);
00724 drms_setkey_int (record_out, "NB_RDB", results->nb_rdb);
00725 drms_setkey_int (record_out, "NB_ABB", results->nb_abb);
00726 drms_setkey_double(record_out, "UP_LIMIT", results->up_limit);
00727 drms_setkey_double(record_out, "LO_LIMIT", results->lo_limit);
00728 drms_setkey_double(record_out, "INC_X", results->inc_x);
00729 drms_setkey_double(record_out, "INC_Y", results->inc_y);
00730 drms_setkey_int (record_out, "NB_ITER", results->nb_iter);
00731 drms_setkey_int (record_out, "CEN_CALC", results->cc);
00732 drms_setkey_double(record_out, "AHI", results->ahi);
00733 drms_setkey_int (record_out, "FLDFR", results->fldfr);
00734 drms_setkey_int (record_out, "NFITPNTS", results->nfitpnts);
00735 drms_setkey_string(record_out, "BLD_VERS", results->bld_vers);
00736 if (tbf >=1)
00737 {
00738 free(results->fits_alpha_beta);
00739 free(results->fits_params);
00740 if (results->fldfr<3) free(results->fits_fulldfs);
00741 if (tbf ==2) free(results->fits_ldfs_data);
00742 }
00743 return(0);
00744 }