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 #include <xsh_utils_wrappers.h>
00028 #include <xsh_utils_image.h>
00029 #include <xsh_error.h>
00030 #include <xsh_utils.h>
00031 #include <xsh_pfits_qc.h>
00032 #include <xsh_pfits.h>
00033 #include <xsh_dfs.h>
00034 #include <xsh_data_pre.h>
00035 #include <xsh_data_instrument.h>
00036 #include <math.h>
00037 #include <string.h>
00038 #include <float.h>
00039
00045 cpl_image*
00046 xsh_imagelist_collapse_median_create(cpl_imagelist* iml)
00047 {
00048
00049 int sx=0;
00050 int sy=0;
00051 int nimg=0;
00052 cpl_image* img=NULL;
00053 cpl_array* values=NULL;
00054
00055 float **pdata = NULL;
00056 cpl_binary ** pbinary = NULL;
00057
00058 int i=0;
00059 cpl_size k=0;
00060 int count=0;
00061 int sx_sy=0;
00062
00063 float* pima=NULL;
00064
00065 cpl_image* result=NULL;
00066
00067
00068 XSH_ASSURE_NOT_NULL_MSG(iml, "Null input imagelist");
00069
00070 nimg=cpl_imagelist_get_size(iml);
00071 if(nimg>0) {
00072 img=cpl_imagelist_get(iml,0);
00073 }
00074 sx=cpl_image_get_size_x(img);
00075 sy=cpl_image_get_size_y(img);
00076 sx_sy=sx*sy;
00077
00078
00079
00080 pdata = cpl_malloc (nimg * sizeof (float *));
00081 assure (pdata != NULL, cpl_error_get_code (),
00082 "Cant allocate memory for data pointers");
00083
00084
00085 pbinary = cpl_malloc (nimg * sizeof (cpl_binary *));
00086 assure (pbinary != NULL, cpl_error_get_code (),
00087 "Cant allocate memory for binary pointers");
00088
00089
00090 for (k = 0; k < nimg; k++) {
00091 check( pdata[k] = cpl_image_get_data_float(cpl_imagelist_get (iml, k)));
00092 check( pbinary[k] = cpl_mask_get_data(cpl_image_get_bpm(
00093 cpl_imagelist_get(iml, k))));
00094 }
00095
00096 result=cpl_image_new(sx,sy,CPL_TYPE_FLOAT);
00097 pima=cpl_image_get_data_float(result);
00098
00099
00100 values=cpl_array_new(nimg,CPL_TYPE_FLOAT);
00101
00102
00103 for (i = 0; i < sx_sy; i++){
00104
00105
00106 for (count=0, k = 0; k < nimg; k++) {
00107 if ( ( (pbinary[k])[i] == CPL_BINARY_0) ) {
00108 cpl_array_set_float(values,k,(pdata[k])[i]);
00109 count++;
00110 } else {
00111 cpl_array_set_invalid(values,k);
00112 }
00113
00114 }
00115
00116 if(count>0) {
00117 pima[i]=cpl_array_get_median(values);
00118 } else {
00119 pima[i]=(pdata[0])[i];
00120 }
00121
00122 }
00123
00124
00125
00126 cleanup:
00127
00128 cpl_array_delete(values);
00129 cpl_free(pdata);
00130 cpl_free(pbinary);
00131
00132 return result;
00133
00134 }
00135
00136
00137
00143 cpl_image*
00144 xsh_imagelist_collapse_mean_create(cpl_imagelist* iml)
00145 {
00146
00147 int sx=0;
00148 int sy=0;
00149 int nimg=0;
00150 cpl_image* img=NULL;
00151 cpl_array* values=NULL;
00152
00153 float **pdata = NULL;
00154 cpl_binary ** pbinary = NULL;
00155
00156 int i=0;
00157 cpl_size k=0;
00158 int count=0;
00159 int sx_sy=0;
00160
00161 float* pima=NULL;
00162 double mean=0;
00163 cpl_image* result=NULL;
00164
00165
00166 XSH_ASSURE_NOT_NULL_MSG(iml, "Null input imagelist");
00167
00168 nimg=cpl_imagelist_get_size(iml);
00169 if(nimg>0) {
00170 img=cpl_imagelist_get(iml,0);
00171 }
00172 sx=cpl_image_get_size_x(img);
00173 sy=cpl_image_get_size_y(img);
00174 sx_sy=sx*sy;
00175
00176
00177
00178 pdata = cpl_malloc (nimg * sizeof (float *));
00179 assure (pdata != NULL, cpl_error_get_code (),
00180 "Cant allocate memory for data pointers");
00181
00182
00183 pbinary = cpl_malloc (nimg * sizeof (cpl_binary *));
00184 assure (pbinary != NULL, cpl_error_get_code (),
00185 "Cant allocate memory for binary pointers");
00186
00187
00188 for (k = 0; k < nimg; k++) {
00189 check( pdata[k] = cpl_image_get_data_float(cpl_imagelist_get (iml, k)));
00190 check( pbinary[k] = cpl_mask_get_data(cpl_image_get_bpm(
00191 cpl_imagelist_get(iml, k))));
00192 }
00193
00194 result=cpl_image_new(sx,sy,CPL_TYPE_FLOAT);
00195 pima=cpl_image_get_data_float(result);
00196
00197
00198 values=cpl_array_new(nimg,CPL_TYPE_FLOAT);
00199
00200
00201 for (i = 0; i < sx_sy; i++){
00202
00203
00204 for (count=0, k = 0; k < nimg; k++) {
00205 if ( ( (pbinary[k])[i] == CPL_BINARY_0) ) {
00206 cpl_array_set_float(values,k,(pdata[k])[i]);
00207 count++;
00208 } else {
00209 cpl_array_set_invalid(values,k);
00210 }
00211
00212 }
00213
00214 mean=cpl_array_get_mean(values);
00215
00216 pima[i]=mean;
00217 }
00218
00219 cpl_array_delete(values);
00220
00221
00222 cleanup:
00223
00224 cpl_array_delete(values);
00225 cpl_free(pdata);
00226 cpl_free(pbinary);
00227
00228
00229 return result;
00230
00231 }
00232
00233