00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "limbfit_ann.h"
00015
00016 int do_one_limbfit(DRMS_Record_t *record_in,DRMS_Record_t *record_out,
00017 LIMBFIT_INPUT *input, LIMBFIT_OUTPUT *results, int *status)
00018 {
00019 static char *log_msg_code="do_one_limbfit_ann";
00020 static char *series_name="limbfit annulus data";
00021 results->series_name=series_name;
00022
00023
00024 int rstatus;
00025 char log_msg[200];
00026
00027 int seg_cnt;
00028 DRMS_Segment_t *segment_in;
00029 DRMS_Segment_t *segment_out;
00030 static DRMS_Array_t *img;
00031
00032
00033
00034 seg_cnt = drms_record_numsegments (record_in);
00035 if (seg_cnt < 1)
00036 {
00037 sprintf(log_msg,"no segments in selected record_in");
00038 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA, 0, log_msg, log_msg_code,results->opf);
00039 write_mini_output(PROCSTAT_NO_LA_DB_READ_PB,record_in,record_out,0,results);
00040 return(ERR_DRMS_READ_MISSING_DATA);
00041 }
00042 segment_in = drms_segment_lookupnum(record_in, 0);
00043 img = drms_segment_read(segment_in, DRMS_TYPE_INT, &rstatus);
00044 if(!img) {
00045 sprintf(log_msg,"Can't do drms_segment_read() status=%d", rstatus);
00046 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA, 0, log_msg, log_msg_code, results->opf);
00047 write_mini_output(PROCSTAT_NO_LA_DB_READ_PB,record_in,record_out,0,results);
00048 return(ERR_DRMS_READ_MISSING_DATA);
00049 }
00050 else
00051 {
00052 input->ix = drms_getkey_float(record_in, "X0_LF", &rstatus);
00053 if(rstatus) {
00054 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA,rstatus, "drms_getkey_float(X0_LF)", log_msg_code, results->opf);
00055 write_mini_output(PROCSTAT_NO_LA_DB_READ_PB,record_in,record_out,0,results);
00056 return(ERR_DRMS_READ_MISSING_DATA);
00057 }
00058 if(isnan(input->ix)) {
00059 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_XYR_LA,rstatus, "X0_LF missing", log_msg_code, results->opf);
00060 write_mini_output(PROCSTAT_NO_LA_XYR_LF_MISSING,record_in,record_out,0,results);
00061 return(ERR_DRMS_READ_MISSING_XYR_LA);
00062 }
00063 input->iy = drms_getkey_float(record_in, "Y0_LF", &rstatus);
00064 if(rstatus) {
00065 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_DATA,rstatus, "drms_getkey_float(Y0_LF)", log_msg_code, results->opf);
00066 write_mini_output(PROCSTAT_NO_LA_DB_READ_PB,record_in,record_out,0,results);
00067 return(ERR_DRMS_READ_MISSING_DATA);
00068 }
00069 if(isnan(input->iy)) {
00070 lf_logmsg("ERROR", "DRMS", ERR_DRMS_READ_MISSING_XYR_LA,rstatus, "Y0_LF missing", log_msg_code, results->opf);
00071 write_mini_output(PROCSTAT_NO_LA_XYR_LF_MISSING,record_in,record_out,0,results);
00072 return(ERR_DRMS_READ_MISSING_XYR_LA);
00073 }
00074
00075
00076
00077
00078
00079 int naxis_row=img->axis[0];
00080 int naxis_col=img->axis[1];
00081 long i,j;
00082 int *data=img->data;
00083
00084
00085
00086
00087
00088
00089 float cmean,ctot=0.0;
00090 long nbp=0;
00091 float limx_m=(float)input->ix-250;
00092 float limx_p=(float)input->ix+250;
00093 float limy_m=(float)input->iy-250;
00094 float limy_p=(float)input->iy+250;
00095 for (i=limx_m;i<limx_p;i++)
00096 {
00097 for (j=limy_m;j<limy_p;j++)
00098 {
00099 ctot=ctot+data[i*naxis_col+j];
00100 nbp++;
00101 }
00102 }
00103 cmean=ctot/nbp;
00104 results->cmean=cmean;
00105 if (results->debug)
00106 {
00107 sprintf(log_msg," cmean = %6.4f (ctot= %6.4f , nbp=%ld)", cmean,ctot,nbp);
00108 lf_logmsg("DEBUG", "APP", 0, 0, log_msg, log_msg_code, results->opf);
00109 }
00110
00111
00112
00113
00114
00115
00116 int *p_data=&data[0];
00117 int *p_mask=input->pf_mask;
00118 while(p_mask<=input->pl_mask)
00119 {
00120 if (*(p_mask) == 0)
00121 {
00122 *(p_data)=EQNANVAL;
00123 }
00124 *(p_data++);
00125 *(p_mask++);
00126 }
00127
00128
00129
00130
00131 if (results->debug) printf("write in the db\n");
00132
00133
00134
00135
00136
00137
00138 drms_copykeys(record_out, record_in, 1, kDRMS_KeyClass_Explicit);
00139
00140 drms_keyword_setdate(record_out);
00141 write_lf_keywords(VOID,record_out, results,0);
00142
00143
00144 *status=0;
00145 rstatus=0;
00146
00147 int naxes[2];
00148 naxes[0]=naxis_row;
00149 naxes[1]=naxis_col;
00150
00151 DRMS_Array_t *array1=drms_array_create(DRMS_TYPE_INT,2,naxes,data,&rstatus);
00152 if (rstatus)
00153 {
00154 lf_logmsg("ERROR", "DRMS", ERR_DRMS_ARRAY_CREATE,rstatus, "ERR_DRMS_ARRAY_CREATE(annulus)", log_msg_code, results->opf);
00155 return ERR_DRMS_ARRAY_CREATE;
00156 }
00157 segment_out = drms_segment_lookupnum(record_out, 0);
00158 if (segment_out)
00159 {
00160 if ( drms_segment_write(segment_out,array1,0) )
00161 {
00162 lf_logmsg("ERROR", "DRMS", ERR_DRMS_SEGMENT_WRITE,rstatus, "ERR_DRMS_SEGMENT_WRITE(annulus)", log_msg_code, results->opf);
00163 return ERR_DRMS_SEGMENT_WRITE;
00164 }
00165 }
00166 else
00167 {
00168 lf_logmsg("ERROR", "DRMS", ERR_DRMS_SEGMENT_LOOKUPNUM,rstatus, "ERR_DRMS_SEGMENT_LOOKUPNUM(annulus)", log_msg_code, results->opf);
00169 return ERR_DRMS_SEGMENT_LOOKUPNUM;
00170 }
00171 drms_free_array (array1);
00172
00173
00174 rstatus=0;
00175
00176
00177
00178
00179
00180 rstatus=drms_setkey_string(record_out, "PROCSTAT", PROCSTAT_OK);
00181
00182 if (results->debug) printf("end write in the db %d\n",rstatus);
00183
00184 if (results->debug) printf("done...%u\n",input->fsn);
00185 lf_logmsg("INFO", "APP", 0, 0, "End writing in the DB", log_msg_code, results->opf);
00186 }
00187
00188 return 0;
00189 }
00190
00191 int write_mini_output(char * errcode, DRMS_Record_t *record_in,DRMS_Record_t *record_out,
00192 int tbf, LIMBFIT_OUTPUT *results)
00193 {
00194
00195 char *log_msg_code="write_mini_output";
00196 lf_logmsg("INFO", "APP", 0, 0, "Writing only header", log_msg_code, results->opf);
00197
00198 drms_copykeys(record_out, record_in, 1, kDRMS_KeyClass_Explicit);
00199
00200 drms_keyword_setdate(record_out);
00201
00202 write_lf_keywords(errcode,record_out, results,1);
00203 return(0);
00204 }
00205
00206 int write_lf_keywords(char * errcode, DRMS_Record_t *record_out, LIMBFIT_OUTPUT *results,
00207 int pass)
00208 {
00209
00210 drms_setkey_string(record_out, "SERIESCN", results->series_name);
00211 drms_setkey_string(record_out, "INSERIES", results->dsin);
00212 drms_setkey_string(record_out, "CODENAME", results->code_name);
00213 drms_setkey_string(record_out, "CODEVERS", results->code_version);
00214 drms_setkey_string(record_out, "CODEDATE", results->code_date);
00215 drms_setkey_float (record_out, "CENT_X", results->cenx);
00216 drms_setkey_float (record_out, "CENT_Y", results->ceny);
00217 drms_setkey_float (record_out, "R_MIN", results->r_min);
00218 drms_setkey_float (record_out, "R_MAX", results->r_max);
00219 drms_setkey_double(record_out, "CMEAN", results->cmean);
00220 drms_setkey_string(record_out, "COMMENTS", results->comment);
00221 drms_setkey_string(record_out, "BLD_VERS", results->bld_vers);
00222
00223
00224
00225 if (pass==1)
00226 drms_setkey_string(record_out, "PROCSTAT", errcode);
00227
00228 return(0);
00229 }