MUSE Pipeline Reference Manual  0.18.5
muse_makeima.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /* vim:set sw=8 sts=8 et cin: */
3 /*
4  * This file is part of the MUSE Instrument Pipeline
5  * Copyright (C) 2007-2012 European Southern Observatory
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 /*----------------------------------------------------------------------------*
23  * Includes *
24  *----------------------------------------------------------------------------*/
25 
26 #include <string.h>
27 #include <cpl.h>
28 #include <muclipm_make_image.h>
29 
30 /*----------------------------------------------------------------------------*/
38 /*----------------------------------------------------------------------------*/
39 int main(int argc, char **argv)
40 {
41  /* local variables */
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];
49  char property[72];
50  int nbchannel, nifu;
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;
55  float cpl_version;
56 
57  cpl_init(0);
58 
59  cpl_error_reset();
60  cpl_msg_set_component_on();
61 /* cpl_msg_set_level(CPL_MSG_DEBUG); */
62 
63  if (argc < 3 || argc > 5) {
64  cpl_msg_error(idstring, "Usage is: makeima science mask [bias] [flat]");
65 
66  cpl_end();
67  return -1;
68  }
69  cpl_version = cpl_version_get_major() + 0.1*cpl_version_get_minor();
70  printf("Running CPL VERSION %2.1f\n",cpl_version);
71 
72  /* create the output image */
73 
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);
78  }
79  else
80  {
81  sprintf(filename,"reconst_%s",argv[1]);
82  }
83 
84  /* eval nb of extensions in files */
85 
86  n_IFU = cpl_fits_count_extensions(argv[1]);
87 
88  if (n_IFU > 24) /* INM may put more extensions like COSMIC table ... */
89  n_IFU = 24;
90 
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) {
94  n_IFU = mask_nbext;
95  }
96 
97  if (argc > 3) {
98  bias_nbext = cpl_fits_count_extensions(argv[3]);
99 
100  if (bias_nbext < n_IFU)
101  n_IFU = bias_nbext;
102  }
103 
104  /* find valid extensions into Science file */
105 
106  for (nbext = 1; nbext <= n_IFU; nbext++) {
107  raw_img = cpl_image_load(argv[1], CPL_TYPE_FLOAT, 0, nbext);
108  if (!raw_img)
109  ext[nbext-1] = 0 ;
110  else {
111  ext[nbext-1] = 1 ;
112  cpl_image_delete(raw_img);
113  }
114  }
115 
116  computed_img = cpl_image_new(MUCLIPM_NB_SUBSLICERS*MUCLIPM_NB_SPEC_PER_SLICE,
117  MUCLIPM_NB_SLICES*n_IFU, CPL_TYPE_FLOAT);
118 
119  /* printf("TUNED for INM DATA !!!! Reverse order for channels\n"); */
120 
121  for (nbchannel = 0; nbchannel < n_IFU; nbchannel++) {
122 
123  current_chan = nbchannel+1;
124 
125  if (!ext[nbchannel]) continue;
126 
127  tmp_img = cpl_image_new(MUCLIPM_NB_SUBSLICERS*MUCLIPM_NB_SPEC_PER_SLICE,
128  MUCLIPM_NB_SLICES, CPL_TYPE_FLOAT);
129 
130  raw_img = cpl_image_load(argv[1], CPL_TYPE_FLOAT, 0, current_chan);
131  if (!raw_img) {
132  cpl_msg_info(idstring,"%d extension(s) processed",current_chan);
133  break;
134  }
135  raw_img_nx = cpl_image_get_size_x(raw_img);
136  raw_img_ny = cpl_image_get_size_y(raw_img);
137 
138  if (raw_img_nx == 0 || raw_img_ny == 0) continue;
139 
140  /* get channel number */
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);
145  /* to be disable for old INM data like Starfield */
146 
147  if (n_IFU == 24)
148  nifu = 24 - nifu;
149 
150  /* nifu--; */
151  cpl_propertylist_delete(hdr);
152 
153  current_chan = cpl_fits_find_extension(argv[2],extname);
154  if (current_chan < 0) continue;
155 
156  mask = cpl_image_load(argv[2], CPL_TYPE_INT, 0, current_chan);
157  if (!mask) {
158  cpl_msg_error(idstring, "Could not load frame %s for channel %d: %s",
159  argv[2], current_chan,cpl_error_get_message());
160  continue;
161  }
162  mask_nx = cpl_image_get_size_x(mask);
163  mask_ny = cpl_image_get_size_y(mask);
164 
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");
167  cpl_end();
168  return -1 ;
169  }
170 
171  mask_hdr = cpl_propertylist_load(argv[2], current_chan);
172  if (!mask_hdr) {
173  cpl_propertylist_delete(mask_hdr);
174  cpl_image_delete(mask);
175  continue;
176  }
177 
178  /* read slice limitder */
179 
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);
185  /* printf("Slice %ld: limits [%ld,%ld]\n", i+1, xstart[i],xend[i]); */
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);
189  return -1;
190  }
191  }
192 
193  for (i=1; i< MUCLIPM_NB_SLICES*MUCLIPM_NB_SUBSLICERS; i++) {
194 
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]);
198  return -1;
199  }
200  }
201 
202  if (argc > 3) {
203  bias = cpl_image_load(argv[3], CPL_TYPE_FLOAT, 0, current_chan);
204  if (!bias) {
205  cpl_msg_error(idstring, "Could not load frame %s for channel %d: %s",
206  argv[3], current_chan, cpl_error_get_message());
207  continue;
208  }
209  bias_nx = cpl_image_get_size_x(bias);
210  bias_ny = cpl_image_get_size_y(bias);
211 
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");
214  cpl_end();
215  return -1 ;
216  }
217  }
218  else {
219  bias = NULL;
220  }
221 
222  /* compute reconstructed image */
223  if ( muclipm_make_image(raw_img, bias, NULL, mask, xstart, xend, &tmp_img) != CPL_ERROR_NONE)
224  {
225  cpl_msg_info(idstring,"Error while computing reconstructed image");
226  cpl_end();
227  return -1;
228  };
229 
230  if (n_IFU < 24)
231  {
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());
235  cpl_end();
236  }
237  }
238  else
239  {
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());
243  cpl_end();
244  }
245  }
246 
247  /* free memory associated to input image */
248  cpl_image_delete(raw_img);
249 
250  /* test first if bias was used */
251  if (bias != NULL)
252  cpl_image_delete(bias);
253 
254  cpl_image_delete(mask);
255  cpl_propertylist_delete(mask_hdr);
256 
257  cpl_image_delete(tmp_img);
258  }
259 
260  hdr = cpl_propertylist_load(pt_file, 0);
261 
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);
265 
266  cpl_end();
267  return 0;
268 }
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.