calibrate_ns.c

00001 #include <stdio.h>
00002 #include "eclipse.h"
00003 #include "spiffi.h"
00004 /*
00005  this Python script needs a frame of a pinhole source with a continuous 
00006  spectrum, that is shifted exactly perpendicular to the slitlets. It fits 
00007  the spectra in spatial direction by a Gaussian fit function and therefore 
00008  determines the sub-pixel position of the source. Then the average distances 
00009  of the source spectra from the center of the slitlets are determined and 
00010  saved in an ASCII list sorted according to the reconstructed image slitlet 
00011  sequence.
00012 */
00013 
00014 int calibrate_ns (char * ini_file)
00015 {
00016   
00017     ns_config * cfg ;
00018     OneImage ** list_object ;
00019     OneImage ** list_off;
00020     fits_header * header ;
00021     fits_header ** head ;
00022 
00023     OneImage * im_on ;
00024     OneImage * im_on_sub ;
00025     OneImage * im_on_ind ;
00026     OneImage * im_on_gauss ;
00027     OneImage * im_mask ;
00028     OneImage ** im ;
00029 
00030 
00031 
00032 
00033     OneImage * im_off ;
00034 
00035     OneCube * cube_object ;
00036     OneCube * cube_off ;
00037     
00038     const char* _id = "calibrate_ns";
00039     char* name;
00040     int typ;
00041     int i = 0;
00042 
00043     int nob =0;
00044     int nof =0;
00045 
00046     float* distances;
00047 
00048 /*
00049 #----------------------------------------------------------
00050 #                  INITIALIZE 
00051 #----------------------------------------------------------
00052 */
00053 
00054 /*parse the file names and parameters to the ns_config data structure cfg */
00055     cfg = parse_ns_ini_file(ini_file);
00056     if (cfg == NULL) {
00057       printf ("Error: could not parse .ini file!\n");
00058       exit(-1);
00059     }
00060 
00061 
00062     if (cfg->maskInd == 1){
00063       if(is_fits_file (cfg->mask) != 1) {
00064     cpl_msg_error(_id,"Input file %s is not FITS",cfg->mask);
00065         return -1;
00066       }
00067     }
00068 
00069     /*
00070 #---------------------------------------------------------
00071 #  stack the frames in data cubes and take the clean mean of all frames
00072 #-------------------------------------------------------
00073     */
00074 
00075     /* allocate memory for lists of object and off-frames */
00076     list_object = new_image_list (cfg->nobj);
00077     if (list_object == NULL) {
00078       printf("Error: could not allocate memory\n");
00079       exit(-1);
00080     }
00081     if (cfg->noff > 0) {
00082       list_off = new_image_list(cfg->noff);
00083       if (list_off == NULL) {
00084         printf("Error: could not allocate memory\n");
00085         exit(-1);
00086       }
00087     }
00088     /* build different image lists for the different cases */
00089     nob = 0;
00090     nof = 0;
00091     for (i=0;i< cfg->nframes;i++){
00092       name = cfg->framelist[i];
00093       if(is_fits_file (name) != 1) {
00094     cpl_msg_error(_id,"Input file %s is not FITS",name);
00095         return -1;
00096       }
00097       im[i]=load_image( name );
00098       /* read the fits header to change the gain and noise parameter */
00099       head[i]=fits_read_header(name);
00100     }
00101     for (i=0;i< cfg->nframes;i++) {
00102       typ = intarray_get_value( cfg->frametype, i );
00103       if (im[i] == NULL) {
00104         printf("Error: could not load image\n");
00105         exit(-1);
00106       }
00107       if (typ == 1) {
00108         insert_image( list_object, im[i], nob );
00109         header = head[i];
00110         nob = nob + 1;
00111       }
00112       else {
00113         insert_image( list_off, im[i], nof);
00114         nof = nof + 1;
00115       }
00116     }
00117     if ((cfg->noff != nof) || (cfg->nobj != nob)){        
00118       printf("Error: something wrong with the number of the different types of frames");
00119       exit(-1);
00120     }
00121 
00122     cube_object = list_make_cube(list_object, nob);
00123     if (cube_object == NULL) {
00124       printf ("Error: could not create data cube!");
00125       exit(-1);
00126     }
00127     destroy_list(list_object);
00128 
00129     if (nof > 0) {
00130       cube_off = list_make_cube(list_off, nof);
00131     }
00132     if (cube_off == NULL) {
00133       printf ("Error: could not create data cube!");
00134       exit(-1);
00135     }
00136     destroy_list(list_off);
00137 
00138 
00139     /*take the average of the different cubes */
00140     im_on = average_with_rejection( cube_object, 
00141                                     cfg->loReject, cfg->hiReject );
00142     if (im_on == NULL) {
00143       printf ( "Error: average_with_rejection failed\n" );
00144       exit(-1);
00145     }
00146     if (cfg->noff != 0) {
00147       im_off = average_with_rejection( cube_off, 
00148                                        cfg->loReject, cfg->hiReject );
00149       if (im_off == NULL) {
00150         printf ( "Error: average_with_rejection failed\n" );
00151         exit(-1);
00152       }
00153       destroy_cube(cube_off);
00154     }
00155     destroy_cube(cube_object);
00156 
00157     /* finally, subtract off from on frames and store the result in 
00158        the object cube */
00159 
00160     if (cfg->noff != 0) {
00161       im_on_sub = sub_image(im_on, im_off);
00162       if (im_on_sub == NULL) {
00163         printf ( "Error: sub_image failed\n" );
00164         exit(-1);
00165       }
00166       /* free memory */
00167       destroy_image(im_on);
00168       destroy_image(im_off);
00169     }
00170     else {
00171       im_on_sub = im_on;
00172     }
00173 
00174     /*
00175 #---------------------------------------------------------
00176 # convolution with Gaussian if recommended
00177 #---------------------------------------------------------
00178     */
00179 
00180     if (cfg->gaussInd == 1) {
00181       im_on_gauss = convolveNSImageByGauss(im_on_sub, cfg->hw);
00182     }
00183     if (im_on_gauss == NULL) {
00184       printf ( "Error: could not carry out convolveNSImageByGauss\n" );
00185       exit(-1);
00186     }
00187     destroy_image(im_on_sub);
00188 
00189     /*
00190 #---------------------------------------------------------
00191 # static bad pixel indication
00192 #---------------------------------------------------------
00193     */
00194 
00195     if (cfg->gaussInd == 1) {
00196       im_on_sub = im_on_gauss;
00197     }
00198     if (cfg->maskInd == 1) {
00199       im_mask = load_image(cfg->mask);
00200       if (im_mask == NULL) {
00201         printf ( "Error: could not load static bad pixel mask\n" );
00202         exit(-1);
00203       }
00204       im_on_ind = multImageByMask(im_on_sub, im_mask);
00205       if (im_on_ind == NULL) {
00206         printf ( "Error: could not carry out multImageByMask\n" );
00207         exit(-1);
00208       }
00209       destroy_image(im_mask);
00210       destroy_image(im_on_sub);
00211     }
00212     else {
00213       im_on_ind = im_on_sub;
00214     }
00215 
00216     save_image_to_fits_hdr_dump(im_on_ind, cfg->fitsname, header, -32);
00217 
00218 
00219     distances = calibrate_ns_test(im_on_ind, cfg->nslits, cfg->halfWidth, 
00220                               cfg->fwhm , cfg->minDiff, 
00221                               cfg->estimated_dist, cfg->devtol, 950, 1050 );
00222     if (distances == NULL) {
00223       printf ("calibrate_ns_test failed\n");
00224       exit(-1);
00225     }
00226 
00227     parameterToAscii(distances, cfg->nslits, cfg->outName);
00228 
00229     /* free memory */
00230 
00231     for (i=0; i< cfg->nframes;i++) {
00232       fits_header_destroy(head[i]);
00233     }
00234     destroy_array(distances);
00235     destroy_image(im_on_ind);
00236     destroy_intarray(cfg->frametype);
00237     ns_cfg_destroy (cfg);
00238     destroy_stringarray (cfg->framelist, cfg->nframes);
00239     return 0;
00240  
00241 }
00242 

Generated on Wed Oct 26 13:08:51 2005 for SINFONI Pipeline Reference Manual by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001