MIDI Pipeline Reference Manual  2.8.3
midiTableToFits.c
1 #include <cpl.h>
2 
3 #include "midi_utils.h"
4 #include "midi_pfits.h"
5 #include "midi_dfs.h"
6 #include "string.h"
7 #include "midiTableToFits.h"
8 #include <config.h>
9 #include "midi_cplupgrade.h"
10 
11 int table_to_fitsimage(cpl_frameset * frameset,
12  const cpl_parameterlist * parlist,
13  const char * columname,
14  const char * outputfilename,
15  cpl_table * table)
16 {
17 
18  cpl_propertylist * qclist;
19 
20  int dimenDATA;
21  cpl_array * array_data=NULL;
22  cpl_image * image_data_int=NULL;
23  cpl_image * image_data=NULL;
24  cpl_image * image_stacked=NULL;
25  cpl_image * image_stacked_raw=NULL;
26  cpl_image * image_stacked_ycollapsed=NULL;
27  cpl_imagelist * imagelist_data=NULL;
28  cpl_imagelist * imagelist_data_sky=NULL;
29  cpl_imagelist * imagelist_data_target=NULL;
30  cpl_imagelist * imagelist_data_result=NULL;
31  int i;
32  int csky=0, ctarget=0; /* c == counter */
33  int maximum_images=0;
34  int undefined=0;
35  char *cubename;
36  cpl_errorstate prestate = cpl_errorstate_get();
37  char ** target_type;
38  cpl_type ctype;
39 
40  /* Load extension Imaging Data */
41 
42  dimenDATA=cpl_table_get_column_dimensions(table, columname);
43  cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
44 
45  if (dimenDATA != 2) {
46  return (int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
47  "DATA has a wrong dimension");
48  }
49 
50  cpl_msg_info(cpl_func, "Number of images in the Table: %" CPL_SIZE_FORMAT "",cpl_table_get_nrow(table));
51  cpl_msg_info(cpl_func, "Extracting %" CPL_SIZE_FORMAT " images: please wait ...",cpl_table_get_nrow(table));
52 
53  imagelist_data =cpl_imagelist_new();
54  imagelist_data_sky =cpl_imagelist_new();
55  imagelist_data_target=cpl_imagelist_new();
56 
57 /* Read target type T(arget),S(ky),B(ackground) and store them
58  At the moment TARTYP2 is read as TARTYP1 and TARTYP4 belong to DATA4 and DATA1
59  and therefore are switched */
60 
61  if (cpl_table_has_column(table, "TARTYP2"))
62  {
63  target_type=cpl_table_get_data_string(table, "TARTYP2");
64 
65  }
66  else
67  {
68  cpl_msg_warning(cpl_func, "TARTYP2 not found, using TARTYP1");
69  target_type=cpl_table_get_data_string(table, "TARTYP1");
70  }
71 /* Make sure that the sky and target imagelist have the same length */
72 /* so start the comparison only after the first U(ndefined) frame */
73 
74  maximum_images=0;
75  undefined=0;
76 
77  for (i=0; i<cpl_table_get_nrow(table);i++){
78  if(strcmp(target_type[i],"U")== 0 ){
79  undefined=1;}
80  if(strcmp(target_type[i],"S")== 0 && undefined!=0){
81  csky++;
82  }
83  if(strcmp(target_type[i],"T")== 0 && undefined!=0){
84  ctarget++;
85  }
86  }
87 
88  if (csky>=ctarget) maximum_images=ctarget;
89  else maximum_images=csky;
90 
91 
92  csky=0, ctarget=0, undefined=0;
93 
94  /* Loop over all "images" stored in DATA and add them to the imagelist */
95 
96 /* Check the type of the image (float or int) */
97  ctype=cpl_table_get_column_type(table, columname);
98 
99  for (i=0; i<cpl_table_get_nrow(table);i++){
100  array_data=(cpl_array *)cpl_table_get_array(table,columname, i);
101 
102  if(ctype&CPL_TYPE_INT){
103  image_data_int=cpl_image_wrap_int(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_int(array_data) );
104  /* Cast the image to float */
105  image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT);
106  cpl_image_unwrap(image_data_int);
107  }
108 
109  if(ctype&CPL_TYPE_FLOAT){
110  image_data_int=cpl_image_wrap_float(cpl_table_get_column_dimension(table,columname,0 ), cpl_table_get_column_dimension(table,columname,1 ), cpl_array_get_data_float(array_data) );
111  image_data=cpl_image_cast(image_data_int, CPL_TYPE_FLOAT);
112  cpl_image_unwrap(image_data_int);
113  }
114 
115 /* Append the image to the imagelists */
116  cpl_imagelist_set(imagelist_data,image_data,i);
117 
118  if(strcmp(target_type[i],"U")== 0 ){
119  undefined=1;}
120 
121  if(strcmp(target_type[i],"S")== 0 && csky<maximum_images && undefined!=0){
122  cpl_imagelist_set(imagelist_data_sky,image_data,csky++);
123  }
124  if(strcmp(target_type[i],"T")== 0 && ctarget<maximum_images && undefined!=0){
125  cpl_imagelist_set(imagelist_data_target,image_data,ctarget++);
126  }
127 
128  }
129 
130  imagelist_data_result=cpl_imagelist_duplicate(imagelist_data_target);
131  cpl_imagelist_subtract(imagelist_data_result,imagelist_data_sky);
132 
133  /* QC parameters */
134  qclist = cpl_propertylist_new();
135 /* qclist = cpl_propertylist_load(cpl_frame_get_filename(rawframe), 0 ); */
136 
137  cpl_frame_set_group (cpl_frameset_get_first(frameset), CPL_FRAME_GROUP_RAW) ;
138 
139  cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_CUBE");
140  cubename = cpl_sprintf("%s_%s", columname, outputfilename);
141  if(cpl_dfs_save_imagelist(frameset,
142  NULL,
143  parlist,
144  frameset,
145  NULL,
146  imagelist_data,
147  CPL_BPP_IEEE_FLOAT,
148  "midi_raw_to_fitsimage",
149  qclist,
150  NULL,
151  PACKAGE "/" PACKAGE_VERSION,
152  cubename)) {
153  /* Propagate the error */
154  (void)cpl_error_set_where(cpl_func);
155  }
156 
157 
158 /* Collapse the raw cube by a sigma clipping filter */
159  if (cpl_imagelist_get_size(imagelist_data)>1)
160  {
161  cpl_msg_info(cpl_func, "Saving clipping-collapsed raw fitsfile ...");
162  image_stacked_raw=cpl_imagelist_collapse_sigclip_create(imagelist_data, 2.0, 2.0, 0.5, (1<<1), NULL);
163  cpl_free(cubename);
164  cubename = cpl_sprintf("%s_%s_%s", columname, "raw_timecollapsed", outputfilename);
165 
166 
167 
168  cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_RAWTIMECOLLAPSED");
169  if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_stacked_raw,
170  CPL_BPP_IEEE_FLOAT, "midi_raw_to_fitsimage",
171  qclist, NULL,
172  PACKAGE "/" PACKAGE_VERSION,
173  cubename)) {
174  /* Propagate the error */
175  (void)cpl_error_set_where(cpl_func);
176  }
177  }
178 
179  cpl_image_delete(image_stacked_raw);
180 
181 
182  if(csky>0){
183  cpl_msg_info(cpl_func, "Saving sky cube ...");
184  cpl_free(cubename);
185  cubename = cpl_sprintf("%s_%s_%s", columname, "sky", outputfilename);
186 
187  cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_CUBE_SKY");
188  if(cpl_dfs_save_imagelist(frameset,
189  NULL,
190  parlist,
191  frameset,
192  NULL,
193  imagelist_data_sky,
194  CPL_BPP_IEEE_FLOAT,
195  "midi_raw_to_fitsimage",
196  qclist,
197  NULL,
198  PACKAGE "/" PACKAGE_VERSION,
199  cubename)) {
200  /* Propagate the error */
201  (void)cpl_error_set_where(cpl_func);
202  }
203  }
204 
205  if(ctarget>0){
206  cpl_msg_info(cpl_func, "Saving target cube ...");
207  cpl_free(cubename);
208  cubename = cpl_sprintf("%s_%s_%s", columname, "target", outputfilename);
209 
210  cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_CUBE_TARGET");
211  if(cpl_dfs_save_imagelist(frameset,
212  NULL,
213  parlist,
214  frameset,
215  NULL,
216  imagelist_data_target,
217  CPL_BPP_IEEE_FLOAT,
218  "midi_raw_to_fitsimage",
219  qclist,
220  NULL,
221  PACKAGE "/" PACKAGE_VERSION,
222  cubename)) {
223  /* Propagate the error */
224  (void)cpl_error_set_where(cpl_func);
225  }
226  }
227 
228  if(ctarget>0 && csky>0){
229  cpl_msg_info(cpl_func, "Saving (target-sky) cube ...");
230  cpl_free(cubename);
231  cubename = cpl_sprintf("%s_%s_%s", columname, "result", outputfilename);
232 
233  cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_CUBE_TARGET_MINUS_SKY");
234  if(cpl_dfs_save_imagelist(frameset,
235  NULL,
236  parlist,
237  frameset,
238  NULL,
239  imagelist_data_result,
240  CPL_BPP_IEEE_FLOAT,
241  "midi_raw_to_fitsimage",
242  qclist,
243  NULL,
244  PACKAGE "/" PACKAGE_VERSION,
245  cubename)) {
246  /* Propagate the error */
247  (void)cpl_error_set_where(cpl_func);
248  }
249  }
250 
251 
252  if(ctarget>0 && csky>0){
253 /* Collapse the result by a sigma clipping filter */
254  cpl_msg_info(cpl_func, "Saving clipping-collapsed (target-sky) fitsfile ...");
255  image_stacked=cpl_imagelist_collapse_sigclip_create(imagelist_data_result, 2.0, 2.0, 0.5, (1<<1), NULL);
256  cpl_free(cubename);
257  cubename = cpl_sprintf("%s_%s_%s", columname, "result_timecollapsed", outputfilename);
258 
259  cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_TIMECOLLAPSED");
260  if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_stacked,
261  CPL_BPP_IEEE_FLOAT, "midi_raw_to_fitsimage",
262  qclist, NULL,
263  PACKAGE "/" PACKAGE_VERSION,
264  cubename)) {
265  /* Propagate the error */
266  (void)cpl_error_set_where(cpl_func);
267  }
268 
269  }
270 
271  if(ctarget>0 && csky>0){
272 /* Collapse the result in time by a sigma clipped filter and then in y direction*/
273  cpl_msg_info(cpl_func, "Saving clipping-collapsed (target-sky) plus collapsed in y direction ...");
274 /* image_stacked_ycollapsed=cpl_image_collapse_window_create(image_stacked,1 ,1 ,260 ,40 ,0); */
275  image_stacked_ycollapsed=cpl_image_collapse_create(image_stacked,0);
276  cpl_free(cubename);
277  cubename = cpl_sprintf("%s_%s_%s", columname, "result_timecollapsed_ycollapsed", outputfilename);
278 
279  cpl_propertylist_update_string(qclist, CPL_DFS_PRO_CATG, "MIDI_TIMECOLLAPSED_YCOLLAPSED");
280  if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image_stacked_ycollapsed,
281  CPL_BPP_IEEE_FLOAT, "midi_raw_to_fitsimage",
282  qclist, NULL,
283  PACKAGE "/" PACKAGE_VERSION,
284  cubename)) {
285  /* Propagate the error */
286  (void)cpl_error_set_where(cpl_func);
287  }
288 /* cpl_image_save(image_stacked_ycollapsed, cubename, CPL_BPP_IEEE_FLOAT, qclist, CPL_IO_DEFAULT); */
289  cpl_image_delete(image_stacked_ycollapsed);
290  cpl_image_delete(image_stacked);
291  }
292 
293 
294 
295 /* Free the Memory */
296 
297  cpl_propertylist_delete(qclist);
298  cpl_free(cubename);
299 
300  while(cpl_imagelist_get_size(imagelist_data_sky)>0)
301  {
302  cpl_imagelist_unset(imagelist_data_sky,0);
303  }
304 
305  while(cpl_imagelist_get_size(imagelist_data_target)>0)
306  {
307  cpl_imagelist_unset(imagelist_data_target,0);
308  }
309 
310 
311  cpl_imagelist_delete(imagelist_data);
312  cpl_imagelist_delete(imagelist_data_result);
313 
314  cpl_imagelist_delete(imagelist_data_sky);
315  cpl_imagelist_delete(imagelist_data_target);
316 
317 
318  return (int)cpl_error_get_code();
319 }