00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033
00034 #include <cpl.h>
00035
00036 #include <math.h>
00037
00038 #include "vircam_mods.h"
00039 #include "vircam_utils.h"
00040 #include "vircam_stats.h"
00041
00044
00090
00091
00092 extern int vircam_genbpm(vir_fits **flatlist, int nflatlist, float lthr,
00093 float hthr, cpl_array **bpm_array, int *nbad,
00094 float *badfrac, int *status) {
00095 cpl_image *master_img,*im;
00096 unsigned char *rejmask,*rejplus;
00097 cpl_propertylist *drs;
00098 int npi,i,status2,*bpm,k,nbmax;
00099 float *idata,med,sig,low,high;
00100 char *fctid = "vircam_genbpm";
00101
00102
00103
00104 *nbad = 0;
00105 *badfrac = 0.0;
00106 *bpm_array = NULL;
00107 if (*status != VIR_OK)
00108 return(*status);
00109
00110
00111
00112 status2 = VIR_OK;
00113 (void)vircam_imcombine(flatlist,nflatlist,1,3,1,5.0,&master_img,&rejmask,
00114 &rejplus,&drs,&status2);
00115 freespace(rejmask);
00116 freespace(rejplus);
00117 freepropertylist(drs);
00118 if (status2 != VIR_OK) {
00119 cpl_msg_error(fctid,"Flat combination failed");
00120 *status = VIR_FATAL;
00121 return(*status);
00122 }
00123
00124
00125
00126 idata = cpl_image_get_data_float(master_img);
00127 npi = vircam_getnpts(master_img);
00128 vircam_medsig(idata,NULL,npi,&med,&sig);
00129 cpl_image_divide_scalar(master_img,(double)med);
00130 for (i = 0; i < npi; i++)
00131 if (idata[i] == 0.0)
00132 idata[i] = 1.0;
00133
00134
00135
00136 *bpm_array = cpl_array_new(npi,CPL_TYPE_INT);
00137 bpm = cpl_array_get_data_int(*bpm_array);
00138 for (i = 0; i < npi; i++)
00139 bpm[i] = 0;
00140
00141
00142
00143 for (i = 0; i < nflatlist; i++) {
00144 im = cpl_image_duplicate(vircam_fits_get_image(flatlist[i]));
00145 cpl_image_divide(im,master_img);
00146 idata = cpl_image_get_data_float(im);
00147 vircam_medmad(idata,NULL,npi,&med,&sig);
00148 sig *= 1.48;
00149
00150
00151
00152 cpl_image_divide_scalar(im,med);
00153
00154
00155
00156 low = 1.0 - lthr*sig/med;
00157 high = 1.0 + hthr*sig/med;
00158
00159
00160
00161 for (k = 0; k < npi; k++)
00162 if (idata[k] < low || idata[k] > high)
00163 bpm[k] += 1;
00164 cpl_image_delete(im);
00165 }
00166 cpl_image_delete(master_img);
00167
00168
00169
00170
00171 nbmax = max(2,nflatlist/4);
00172 for (i = 0; i < npi; i++) {
00173 if (bpm[i] >= nbmax) {
00174 bpm[i] = 1;
00175 (*nbad)++;
00176 } else
00177 bpm[i] = 0;
00178 }
00179 *badfrac = (float)(*nbad)/(float)npi;
00180
00181
00182
00183 return(VIR_OK);
00184 }
00185
00189
00190
00191
00192
00193
00194
00195
00196