X-shooter Pipeline Reference Manual 3.8.15
xsh_utils_imagelist.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-06-12 12:22:16 $
23 * $Revision: 1.1 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#include <xsh_utils_wrappers.h>
28#include <xsh_utils_image.h>
29#include <xsh_error.h>
30#include <xsh_utils.h>
31#include <xsh_pfits_qc.h>
32#include <xsh_pfits.h>
33#include <xsh_dfs.h>
34#include <xsh_data_pre.h>
35#include <xsh_data_instrument.h>
36#include <math.h>
37#include <string.h>
38#include <float.h>
39#include <xsh_globals.h>
40
46cpl_image*
48{
49
50 int sx=0;
51 int sy=0;
52 int nimg=0;
53 cpl_image* img=NULL;
54 cpl_array* values=NULL;
55
56 float **pdata = NULL;
57 cpl_binary ** pbinary = NULL;
58
59 int i=0;
60 cpl_size k=0;
61 int count=0;
62 int sx_sy=0;
63
64 float* pima=NULL;
65
66 cpl_image* result=NULL;
67
68
69 XSH_ASSURE_NOT_NULL_MSG(iml, "Null input imagelist");
70
71 nimg=cpl_imagelist_get_size(iml);
72 if(nimg>0) {
73 img=cpl_imagelist_get(iml,0);
74 }
75 sx=cpl_image_get_size_x(img);
76 sy=cpl_image_get_size_y(img);
77 sx_sy=sx*sy;
78
79
80 /* create the array of pointers to image data */
81 pdata = cpl_malloc (nimg * sizeof (float *));
82 assure (pdata != NULL, cpl_error_get_code (),
83 "Cant allocate memory for data pointers");
84
85 /* create the array of pointers to image binary */
86 pbinary = cpl_malloc (nimg * sizeof (cpl_binary *));
87 assure (pbinary != NULL, cpl_error_get_code (),
88 "Cant allocate memory for binary pointers");
89
90 /* Populate images pointer array */
91 for (k = 0; k < nimg; k++) {
92 check( pdata[k] = cpl_image_get_data_float(cpl_imagelist_get (iml, k)));
93 check( pbinary[k] = cpl_mask_get_data(cpl_image_get_bpm(
94 cpl_imagelist_get(iml, k))));
95 }
96
97 result=cpl_image_new(sx,sy,CPL_TYPE_FLOAT);
98 pima=cpl_image_get_data_float(result);
99
100
101 values=cpl_array_new(nimg,CPL_TYPE_FLOAT);
102
103 /* Loop over all pixels */
104 for (i = 0; i < sx_sy; i++){
105
106 /* fill array only with good pixel values */
107 for (count=0, k = 0; k < nimg; k++) {
108 if ( ( (pbinary[k])[i] == CPL_BINARY_0) ) {
109 cpl_array_set_float(values,k,(pdata[k])[i]);
110 count++;
111 } else {
112 cpl_array_set_invalid(values,k);
113 }
114
115 }
116 /* finally compute median and store result in output */
117 if(count>0) {
118 pima[i]=cpl_array_get_median(values);
119 } else {
120 pima[i]=(pdata[0])[i];
121 }
122
123 }
124
125
126
127 cleanup:
128
129 cpl_array_delete(values);
130 cpl_free(pdata);
131 cpl_free(pbinary);
132
133 return result;
134
135}
136
137
138
144cpl_image*
146{
147
148 int sx=0;
149 int sy=0;
150 int nimg=0;
151 cpl_image* img=NULL;
152 cpl_array* values=NULL;
153
154 float **pdata = NULL;
155 cpl_binary ** pbinary = NULL;
156
157 int i=0;
158 cpl_size k=0;
159 int count=0;
160 int sx_sy=0;
161
162 float* pima=NULL;
163 double mean=0;
164 cpl_image* result=NULL;
165
166
167 XSH_ASSURE_NOT_NULL_MSG(iml, "Null input imagelist");
168
169 nimg=cpl_imagelist_get_size(iml);
170 if(nimg>0) {
171 img=cpl_imagelist_get(iml,0);
172 }
173 sx=cpl_image_get_size_x(img);
174 sy=cpl_image_get_size_y(img);
175 sx_sy=sx*sy;
176
177
178 /* create the array of pointers to image data */
179 pdata = cpl_malloc (nimg * sizeof (float *));
180 assure (pdata != NULL, cpl_error_get_code (),
181 "Cant allocate memory for data pointers");
182
183 /* create the array of pointers to image binary */
184 pbinary = cpl_malloc (nimg * sizeof (cpl_binary *));
185 assure (pbinary != NULL, cpl_error_get_code (),
186 "Cant allocate memory for binary pointers");
187
188 /* Populate images pointer array */
189 for (k = 0; k < nimg; k++) {
190 check( pdata[k] = cpl_image_get_data_float(cpl_imagelist_get (iml, k)));
191 check( pbinary[k] = cpl_mask_get_data(cpl_image_get_bpm(
192 cpl_imagelist_get(iml, k))));
193 }
194
195 result=cpl_image_new(sx,sy,CPL_TYPE_FLOAT);
196 pima=cpl_image_get_data_float(result);
197
198
199 values=cpl_array_new(nimg,CPL_TYPE_FLOAT);
200
201 /* Loop over all pixels */
202 for (i = 0; i < sx_sy; i++){
203
204 /* fill array only with good pixel values */
205 for (count=0, k = 0; k < nimg; k++) {
206 if ( ( (pbinary[k])[i] == CPL_BINARY_0) ) {
207 cpl_array_set_float(values,k,(pdata[k])[i]);
208 count++;
209 } else {
210 cpl_array_set_invalid(values,k);
211 }
212
213 }
214 /* finally compute mean and store result in output */
215 mean=cpl_array_get_mean(values);
216
217 pima[i]=mean;
218 }
219
220 cpl_array_delete(values);
221
222
223 cleanup:
224
225 cpl_array_delete(values);
226 cpl_free(pdata);
227 cpl_free(pbinary);
228
229
230 return result;
231
232}
233
234
235cpl_error_code
236xsh_imagelist_cut_dichroic_uvb(cpl_imagelist* org_data_iml,
237 cpl_imagelist* org_errs_iml,
238 cpl_imagelist* org_qual_iml,
239 cpl_propertylist* phead)
240{
241
242 cpl_vector* valid;
243 double* data=NULL;
244
245 int k=0;
246 int naxis3=0;
247 int zcut=0;
248
249 double crval3=0;
250 double cdelt3=0;
251 double wave_cut=XSH_UVB_DICHROIC_WAVE_CUT; /* 556 nm dichroic cut */
252 double wave_min=0;
253 double wave_max=0;
254
255 /* compute axis at which we need to cut */
256 check(naxis3=cpl_imagelist_get_size(org_data_iml));
257 crval3=xsh_pfits_get_crval3(phead);
258 cdelt3=xsh_pfits_get_cdelt3(phead);
259
260 wave_min = crval3;
261 wave_max = wave_min + cdelt3*naxis3;
262 cpl_ensure_code(wave_max > wave_cut, CPL_ERROR_ILLEGAL_INPUT);
263 zcut = (int) ( (wave_cut-wave_min) / cdelt3 + 0.5 );
264 cpl_ensure_code(zcut <= naxis3, CPL_ERROR_ILLEGAL_INPUT);
265
266 if (zcut == naxis3) {
267 return CPL_ERROR_NONE;
268 }
269
270 /* prepare vector to specify which image to erase */
271 valid=cpl_vector_new(naxis3);
272 cpl_vector_add_scalar(valid,1.);
273 data=cpl_vector_get_data(valid);
274 for(k=zcut+1;k<naxis3;k++) {
275 data[k]=-1;
276 }
277
278 /* cut cubes */
279 cpl_imagelist_erase(org_data_iml,valid);
280 cpl_imagelist_erase(org_errs_iml,valid);
281 cpl_imagelist_erase(org_qual_iml,valid);
282
283 cleanup:
284 xsh_free_vector(&valid);
285
286 return cpl_error_get_code();
287}
288
289
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
Definition: xsh_error.h:103
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
double xsh_pfits_get_cdelt3(const cpl_propertylist *plist)
find out the cdelt3
Definition: xsh_pfits.c:2235
double xsh_pfits_get_crval3(const cpl_propertylist *plist)
find out the crval3
Definition: xsh_pfits.c:1946
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
#define XSH_UVB_DICHROIC_WAVE_CUT
Definition: xsh_globals.h:38
cpl_image * xsh_imagelist_collapse_median_create(cpl_imagelist *iml)
Compute median on imagelist.
cpl_image * xsh_imagelist_collapse_mean_create(cpl_imagelist *iml)
Compute mean on imagelist.
cpl_error_code xsh_imagelist_cut_dichroic_uvb(cpl_imagelist *org_data_iml, cpl_imagelist *org_errs_iml, cpl_imagelist *org_qual_iml, cpl_propertylist *phead)