~ubuntu-branches/debian/sid/cpl-plugin-sinfo/sid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * This file is part of the ESO SINFONI Pipeline
 * Copyright (C) 2004,2005 European Southern Observatory
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
 */
/*----------------------------------------------------------------------------

     File name    :       spiffi_north_south_test.c
   Author       :    A. Modigliani
   Created on   :    Sep 17, 2003
   Description  : 

 * nsh.h
 * Result of a north-south test exposure are 32 continuum spectra of a 
 * pinhole that means one spectrum in each slitlet at the same spatial 
 * position.
 * Each spectrum is fitted in sp[atial direction by a Gaussian to get the 
 * sub-pixel positions for each row.
 *
 * Then the distances are determined in each row and averaged
 *
 * Result: are distances of each slitlet from each other => 31 values stored 
 *  in an ASCII file this Python script needs a frame of a pinhole source with 
 *  a continuous spectrum, that is shifted exactly perpendicular to the 
 *  slitlets. It fits the spectra in spatial direction by a Gaussian fit 
 *  function and therefore determines the sub-pixel position of the source.
 *
 *  Then the distances of the slitlets from each other are determined and 
 *   saved in an ASCII list. 
 *


 ---------------------------------------------------------------------------*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
/*----------------------------------------------------------------------------
                                Includes
 ---------------------------------------------------------------------------*/
#include "sinfo_new_nst.h"
#include "sinfo_pro_save.h"
#include "sinfo_pro_types.h"
#include "sinfo_functions.h"
#include "sinfo_ns_ini_by_cpl.h"
#include "sinfo_cube_construct.h"
#include "sinfo_utilities.h"
#include "sinfo_utils_wrappers.h"
#include "sinfo_error.h"
#include "sinfo_globals.h"

/*----------------------------------------------------------------------------
                                Defines
 ---------------------------------------------------------------------------*/

/**@{*/
/**
 * @addtogroup sinfo_rec_distortion North South test
 *
 * TBD
 */

/*----------------------------------------------------------------------------
                             Function Definitions
 ---------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
   Function     :       north_south_test()
   In           :       ini_file: file name of according .ini file
   Out          :       integer (0 if it worked, -1 if it doesn't) 
   Job          : Result of a north-south test exposure are 32 continuum 
                  spectra of a pinhole    that means one spectrum in each 
                  slitlet at the same spatial position.

                  Each spectrum is fitted in sp[atial direction by a Gaussian
                  to get the sub-pixel positions for each row.

                  Then the distances are determined in each row and averaged

                  Result: are distances of each slitlet from each other =>
                          31 values stored in an ASCII file


 ---------------------------------------------------------------------------*/
int 
sinfo_new_nst(const char* plugin_id, 
              cpl_parameterlist* config, 
              cpl_frameset* sof,
              cpl_frameset* ref_set)
{
    ns_config * cfg=NULL ;
    cpl_imagelist * list_object=NULL ;
    cpl_imagelist * list_off=NULL;
    cpl_image * im_on=NULL;
    cpl_image * im_on_sub=NULL ;
    cpl_image * im_on_ind=NULL ;
    cpl_image * im_on_gauss=NULL ;
    cpl_image * im_mask=NULL ;
    cpl_image * im_off=NULL ;

    char* name=NULL;
    char tbl_name[MAX_NAME_SIZE];
    int typ=0;
    int i = 0;

    int nob =0;
    int nof =0;

    float* distances=NULL;
    cpl_table* tbl_dist=NULL;

    cpl_vector* qc_dist=NULL;

    //double  qc_dist_mean=0;
    double  qc_dist_stdev=0;
    cpl_frameset* raw=NULL;

    cpl_table* qclog_tbl=NULL;
    char key_value[MAX_NAME_SIZE];
    char key_name[MAX_NAME_SIZE];
    int  no=0;
    double lo_cut=0.;
    double hi_cut=0.;
    int pdensity=0;

    /*
       -----------------------------------------------------------------
       1) parse the file names and parameters to the ns_config data 
          structure cfg
       -----------------------------------------------------------------
     */

    /*
      parse the file names and parameters to the ns_config data structure cfg 
     */

    sinfo_msg("Parse cpl input");
    check_nomsg(raw=cpl_frameset_new());
    cknull(cfg = sinfo_parse_cpl_input_ns(config,sof,&raw),
           "could not parse cpl input!") ;

    if (cfg->maskInd == 1) {
        if(sinfo_is_fits_file(cfg->mask) != 1) {
            sinfo_msg_error("Input file %s is not FITS",cfg->mask);
            goto cleanup;
        }
    }
    /*
     --------------------------------------------------------------------
     stack the frames in data cubes and take the clean mean of all frames
     --------------------------------------------------------------------
     */
    /* allocate memory for lists of object and off-frames */
    sinfo_msg("stack the frames in data cubes");
    check(list_object = cpl_imagelist_new(),
          "could not allocate memory");

    if (cfg->noff > 0 ) {
        check(list_off = cpl_imagelist_new(),
                        "could not allocate memory");
    }

    /*
      #build different image lists for the different cases----
     */
    sinfo_msg("build different image lists for the different cases");
    nob = 0;
    nof = 0;

    for (i=0; i< cfg->nframes; i++){
        name = cfg->framelist[i];
        if(sinfo_is_fits_file(name) != 1) {
            sinfo_msg_error("Input file %s is not FITS",name);
            goto cleanup;
        } else {
            typ = sinfo_new_intarray_get_value( cfg->frametype, i );
            if (typ == 1) {
                cpl_imagelist_set(list_object,
                                cpl_image_load(name,CPL_TYPE_FLOAT,0,0),nob);
                nob = nob + 1;
            } else {
                cpl_imagelist_set(list_off,
                                cpl_image_load(name,CPL_TYPE_FLOAT,0,0),nof);
                nof = nof + 1;
            }
        }
    }

    if (cfg->noff != nof || cfg->nobj != nob ){
        sinfo_msg_error("something wrong with the number of the "
                        "different types of frames");
        goto cleanup;
    }

    /*
  #---take the average of the different cubes -------------
     */
    sinfo_msg("take the average of the different cubes");

    check_nomsg(no=cpl_imagelist_get_size(list_object));
    lo_cut=(floor)(cfg->loReject*no+0.5);
    hi_cut=(floor)(cfg->hiReject*no+0.5);
    check(im_on=cpl_imagelist_collapse_minmax_create(list_object,lo_cut,hi_cut),
          "sinfo_average_with_rejection failed" );

    if (cfg->noff != 0) {
        /*
      im_off = sinfo_average_with_rejection( cube_off, 
                                       cfg->loReject, cfg->hiReject );
         */
        check_nomsg(no=cpl_imagelist_get_size(list_off));
        lo_cut=(floor)(cfg->loReject*no+0.5);
        hi_cut=(floor)(cfg->hiReject*no+0.5);
        check(im_off=cpl_imagelist_collapse_minmax_create(list_off,lo_cut,hi_cut),
              "sinfo_average_with_rejection failed" );
        sinfo_free_imagelist(&list_off);
    }
    sinfo_free_imagelist(&list_object);

    /*
  #finally, subtract off from on frames and store the result in the object cube
     */

    if (cfg->noff != 0) {
        sinfo_msg("subtract off from on frames");
        check(im_on_sub = cpl_image_subtract_create(im_on, im_off),
              "sinfo_sub_image failed" );

        sinfo_free_image(&im_on);
        sinfo_free_image(&im_off);
    } else {
        check_nomsg(im_on_sub = cpl_image_duplicate(im_on));
        sinfo_free_image(&im_on);
    }

    /*
  #---------------------------------------------------------
  # convolution with Gaussian if recommended
  #---------------------------------------------------------
     */
    if (cfg->gaussInd == 1) {
        sinfo_msg("convolution with Gaussian");
        cknull(im_on_gauss = sinfo_new_convolve_ns_image_by_gauss(im_on_sub,
                        cfg->hw),
               "could not carry out sinfo_convolveNSImageByGauss" );

        sinfo_free_image(&im_on_sub);
        check_nomsg(im_on_sub = cpl_image_duplicate(im_on_gauss));
        sinfo_free_image(&im_on_gauss);
    }

    /*
  #---------------------------------------------------------
  # static bad pixel indication
  #---------------------------------------------------------
     */

    if (cfg->maskInd == 1) {
        sinfo_msg("static bad pixel indication");
        check(im_mask = cpl_image_load(cfg->mask,CPL_TYPE_FLOAT,0,0),
              "could not load static bad pixel mask" );
        cknull(im_on_ind = sinfo_new_mult_image_by_mask(im_on_sub, im_mask),
               "could not carry out sinfo_multImageByMask" );
        sinfo_free_image(&im_mask);
        sinfo_free_image(&im_on_sub);

    } else {
        check_nomsg(im_on_ind = cpl_image_duplicate(im_on_sub));
        sinfo_free_image(&im_on_sub);
    }

    if(pdensity > 1 && strcmp(plugin_id,"sinfo_rec_distortion")!=0) {
        ck0(sinfo_pro_save_ima(im_on_ind,ref_set,sof,cfg->fitsname,
                        PRO_MASTER_SLIT,NULL,plugin_id,config),
                        "cannot save ima %s", cfg->fitsname);
    }

    /*
   #---------------------------------------------------------
   # do the north - south - test
   #---------------------------------------------------------
     */
    sinfo_msg("Do the north - south - test");

    cknull(distances = sinfo_north_south_test(im_on_ind,
                    cfg->nslits,
                    cfg->halfWidth,
                    cfg->fwhm ,
                    cfg->minDiff,
                    cfg->estimated_dist,
                    cfg->devtol,
                    IMA_PIX_START,
                    IMA_PIX_END ),
           "North South Test distance determination failed");
    sinfo_free_image(&im_on_ind);
    /*
   sinfo_new_parameter_to_ascii(distances, cfg->nslits - 1, cfg->outName);
     */
    check_nomsg(tbl_dist = cpl_table_new(cfg->nslits - 1));
    check_nomsg(cpl_table_new_column(tbl_dist,"slitlet_distance",
                    CPL_TYPE_FLOAT));
    check_nomsg(cpl_table_copy_data_float(tbl_dist,"slitlet_distance",
                    distances));

    strcpy(tbl_name,cfg->outName);

    check_nomsg(qclog_tbl = cpl_table_new(cfg->nslits + 1));
    check_nomsg(cpl_table_new_column(qclog_tbl,"key_name", CPL_TYPE_STRING));
    check_nomsg(cpl_table_new_column(qclog_tbl,"key_type", CPL_TYPE_STRING));
    check_nomsg(cpl_table_new_column(qclog_tbl,"key_value", CPL_TYPE_STRING));
    check_nomsg(cpl_table_new_column(qclog_tbl,"key_help", CPL_TYPE_STRING));

    check_nomsg(qc_dist=cpl_vector_new(cfg->nslits - 1));

    for(i=0;i<cfg->nslits - 1;i++) {
        snprintf(key_name,MAX_NAME_SIZE-1,"%s%i","QC SL DIST",i);
        cpl_table_set_string(qclog_tbl,"key_name",i,key_name);
        cpl_table_set_string(qclog_tbl,"key_type",i,"CPL_TYPE_DOUBLE");
        snprintf(key_value,MAX_NAME_SIZE-1,"%g",distances[i]);
        cpl_table_set_string(qclog_tbl,"key_value",i,key_value);
        cpl_table_set_string(qclog_tbl,"key_help",i,"Slitlet distance");

        cpl_vector_set(qc_dist,i,distances[i]);
    }
    //check_nomsg(qc_dist_mean=cpl_vector_get_mean(qc_dist));
    check_nomsg(qc_dist_stdev=cpl_vector_get_stdev(qc_dist));

    cpl_table_set_string(qclog_tbl,"key_name",cfg->nslits-1,"QC SL DISTAVG");
    cpl_table_set_string(qclog_tbl,"key_type",cfg->nslits-1,"CPL_TYPE_DOUBLE");
    snprintf(key_value,MAX_NAME_SIZE-1,"%g",cpl_vector_get_mean(qc_dist));
    cpl_table_set_string(qclog_tbl,"key_value",cfg->nslits-1,key_value);
    cpl_table_set_string(qclog_tbl,"key_help",cfg->nslits-1,
                         "Average Slitlet distance");

    cpl_table_set_string(qclog_tbl,"key_name",cfg->nslits,"QC SL DISTRMS");
    cpl_table_set_string(qclog_tbl,"key_type",cfg->nslits,"CPL_TYPE_DOUBLE");
    snprintf(key_value,MAX_NAME_SIZE-1,"%g",qc_dist_stdev);
    cpl_table_set_string(qclog_tbl,"key_value",cfg->nslits,key_value);
    cpl_table_set_string(qclog_tbl,"key_help",cfg->nslits,
                         "RMS Slitlet distance");

    ck0(sinfo_pro_save_tbl(tbl_dist,ref_set,sof,tbl_name,
                    PRO_SLITLETS_DISTANCE,qclog_tbl,plugin_id,config),
        "cannot dump tbl %s", tbl_name);

    sinfo_free_my_vector(&qc_dist);
    sinfo_free_table(&tbl_dist);
    sinfo_free_table(&qclog_tbl);
    sinfo_free_float(&distances);
    sinfo_ns_free (&cfg);
    sinfo_free_frameset(&raw);

    return 0;

    cleanup:
    sinfo_free_my_vector(&qc_dist);
    sinfo_free_table(&tbl_dist);
    sinfo_free_table(&qclog_tbl);
    sinfo_free_float(&distances);
    sinfo_free_table(&tbl_dist);
    sinfo_free_table(&qclog_tbl);
    sinfo_free_imagelist(&list_object);
    sinfo_free_imagelist(&list_off);
    sinfo_free_image(&im_on);
    sinfo_free_image(&im_mask);
    sinfo_free_image(&im_on_gauss);
    sinfo_free_image(&im_on_sub);
    sinfo_free_image(&im_off);
    sinfo_free_image(&im_on_ind);
    sinfo_ns_free (&cfg);
    sinfo_free_frameset(&raw);
    return -1;


}

/**@}*/