28 #include <muclipm_make_image.h>
39 int main(
int argc,
char **argv)
42 cpl_image *raw_img, *bias = NULL, *mask;
43 cpl_propertylist *mask_hdr, *hdr;
44 const char *idstring =
"muse_makeima_main";
45 char filename[256], extname[72], *pt_file;
46 cpl_image *computed_img, *tmp_img;
47 long xstart[MUCLIPM_NB_SLICES*MUCLIPM_NB_SUBSLICERS];
48 long xend[MUCLIPM_NB_SLICES*MUCLIPM_NB_SUBSLICERS];
51 long raw_img_nx, raw_img_ny, i;
52 long mask_nx, mask_ny;
53 long bias_nx, bias_ny;
54 int n_IFU, nbext, ext[24], mask_nbext, bias_nbext, current_chan;
60 cpl_msg_set_component_on();
63 if (argc < 3 || argc > 5) {
64 cpl_msg_error(idstring,
"Usage is: makeima science mask [bias] [flat]");
69 cpl_version = cpl_version_get_major() + 0.1*cpl_version_get_minor();
70 printf(
"Running CPL VERSION %2.1f\n",cpl_version);
74 pt_file = strrchr(argv[1],
'/');
75 if (pt_file != NULL) {
76 sprintf(filename,
"reconst_%s",pt_file+1);
77 printf(
"Filename is %s\n",filename);
81 sprintf(filename,
"reconst_%s",argv[1]);
86 n_IFU = cpl_fits_count_extensions(argv[1]);
91 printf(
"%d extensions will be processed\n", n_IFU);
92 mask_nbext = cpl_fits_count_extensions(argv[2]);
93 if (mask_nbext < n_IFU) {
98 bias_nbext = cpl_fits_count_extensions(argv[3]);
100 if (bias_nbext < n_IFU)
106 for (nbext = 1; nbext <= n_IFU; nbext++) {
107 raw_img = cpl_image_load(argv[1], CPL_TYPE_FLOAT, 0, nbext);
112 cpl_image_delete(raw_img);
116 computed_img = cpl_image_new(MUCLIPM_NB_SUBSLICERS*MUCLIPM_NB_SPEC_PER_SLICE,
117 MUCLIPM_NB_SLICES*n_IFU, CPL_TYPE_FLOAT);
121 for (nbchannel = 0; nbchannel < n_IFU; nbchannel++) {
123 current_chan = nbchannel+1;
125 if (!ext[nbchannel])
continue;
127 tmp_img = cpl_image_new(MUCLIPM_NB_SUBSLICERS*MUCLIPM_NB_SPEC_PER_SLICE,
128 MUCLIPM_NB_SLICES, CPL_TYPE_FLOAT);
130 raw_img = cpl_image_load(argv[1], CPL_TYPE_FLOAT, 0, current_chan);
132 cpl_msg_info(idstring,
"%d extension(s) processed",current_chan);
135 raw_img_nx = cpl_image_get_size_x(raw_img);
136 raw_img_ny = cpl_image_get_size_y(raw_img);
138 if (raw_img_nx == 0 || raw_img_ny == 0)
continue;
141 hdr = cpl_propertylist_load(argv[1], current_chan);
142 strcpy(extname, cpl_propertylist_get_string(hdr,
"EXTNAME"));
143 sscanf(extname,
"CHAN%02d", &nifu);
144 printf(
"Processing CHAN%d (extension %d)\n",nifu, nbchannel+1);
151 cpl_propertylist_delete(hdr);
153 current_chan = cpl_fits_find_extension(argv[2],extname);
154 if (current_chan < 0)
continue;
156 mask = cpl_image_load(argv[2], CPL_TYPE_INT, 0, current_chan);
158 cpl_msg_error(idstring,
"Could not load frame %s for channel %d: %s",
159 argv[2], current_chan,cpl_error_get_message());
162 mask_nx = cpl_image_get_size_x(mask);
163 mask_ny = cpl_image_get_size_y(mask);
165 if (mask_nx != raw_img_nx || mask_ny != raw_img_ny) {
166 cpl_msg_error(idstring,
"Science image and mask don't have the same size");
171 mask_hdr = cpl_propertylist_load(argv[2], current_chan);
173 cpl_propertylist_delete(mask_hdr);
174 cpl_image_delete(mask);
180 for (i=0; i< MUCLIPM_NB_SLICES*MUCLIPM_NB_SUBSLICERS; i++) {
181 sprintf(property,
"ESO DET SLICE%ld XSTART",i+1);
182 xstart[i] = cpl_propertylist_get_int(mask_hdr, property);
183 sprintf(property,
"ESO DET SLICE%ld XEND",i+1);
184 xend[i] = cpl_propertylist_get_int(mask_hdr, property);
186 if (xstart[i] >= xend[i]) {
187 cpl_msg_error(idstring,
"Channel %d: inconsistant slice limits in X [%ld,%ld] for slice %ld",
188 current_chan,xstart[i],xend[i], i+1);
193 for (i=1; i< MUCLIPM_NB_SLICES*MUCLIPM_NB_SUBSLICERS; i++) {
195 if (xstart[i] <= xend[i-1]) {
196 cpl_msg_error(idstring,
"Channel %d: inconsistant interslice for slices %ld and %ld (%ld,%ld)",
197 current_chan,i-1, i, xend[i-1],xstart[i]);
203 bias = cpl_image_load(argv[3], CPL_TYPE_FLOAT, 0, current_chan);
205 cpl_msg_error(idstring,
"Could not load frame %s for channel %d: %s",
206 argv[3], current_chan, cpl_error_get_message());
209 bias_nx = cpl_image_get_size_x(bias);
210 bias_ny = cpl_image_get_size_y(bias);
212 if (bias_nx != raw_img_nx || bias_ny != raw_img_ny) {
213 cpl_msg_error(idstring,
"Science image and bias have not the same size");
223 if (
muclipm_make_image(raw_img, bias, NULL, mask, xstart, xend, &tmp_img) != CPL_ERROR_NONE)
225 cpl_msg_info(idstring,
"Error while computing reconstructed image");
232 if (cpl_image_copy(computed_img,tmp_img,1,nbchannel*MUCLIPM_NB_SLICES+1)) {
233 cpl_msg_error(idstring,
"Could not copy subframe for channel %d: %s",
234 nifu+1, cpl_error_get_message());
240 if (cpl_image_copy(computed_img,tmp_img,1,nifu*MUCLIPM_NB_SLICES+1)) {
241 cpl_msg_error(idstring,
"Could not copy subframe for channel %d: %s",
242 nifu+1, cpl_error_get_message());
248 cpl_image_delete(raw_img);
252 cpl_image_delete(bias);
254 cpl_image_delete(mask);
255 cpl_propertylist_delete(mask_hdr);
257 cpl_image_delete(tmp_img);
260 hdr = cpl_propertylist_load(pt_file, 0);
262 cpl_image_flip(computed_img, 0);
263 cpl_image_save(computed_img, filename, CPL_BPP_IEEE_FLOAT, hdr, CPL_IO_DEFAULT);
264 cpl_image_delete(computed_img);
cpl_error_code muclipm_make_image(const cpl_image *raw_img, const cpl_image *offset, const cpl_image *flat, const cpl_image *mask, const long *slice_xstart, const long *slice_xend, cpl_image **out_img)
Collapses spectra over the spectral direction.