00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00032
00035
00036
00037
00038
00039 #include <math.h>
00040 #include <xsh_drl.h>
00041
00042 #include <xsh_utils_table.h>
00043 #include <xsh_badpixelmap.h>
00044 #include <xsh_data_pre.h>
00045 #include <xsh_dfs.h>
00046 #include <xsh_pfits.h>
00047 #include <xsh_error.h>
00048 #include <xsh_msg.h>
00049 #include <xsh_fit.h>
00050 #include <xsh_data_instrument.h>
00051 #include <xsh_data_localization.h>
00052 #include <xsh_data_rec.h>
00053 #include <xsh_ifu_defs.h>
00054 #include <xsh_data_image_3d.h>
00055 #include <xsh_data_pre_3d.h>
00056
00057 #include <cpl.h>
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 static void fill_img( cpl_image * img, int ilambda, cpl_image * down,
00068 cpl_image * cen, cpl_image * up, int up_shift, int cen_shift)
00069 {
00070 int nx, ny, j ;
00071
00072
00073 check(nx = cpl_image_get_size_x( down ));
00074 check(ny = cpl_image_get_size_y( down));
00075 xsh_msg_dbg_high( "fill image at %d (%d x %d )", ilambda, nx, ny ) ;
00076
00077 for( j = 1 ; j <= ny ; j++ ) {
00078 int rej ;
00079 double flux ;
00080
00081 check(flux = cpl_image_get( down, ilambda+1, j, &rej )) ;
00082 check(cpl_image_set( img, 1, j+cen_shift, flux )) ;
00083 }
00084
00085 check(nx = cpl_image_get_size_x( cen));
00086 check(ny = cpl_image_get_size_y( cen));
00087
00088 for( j = 1 ; j <= ny ; j++ ) {
00089 int rej ;
00090 double flux ;
00091
00092 check(flux = cpl_image_get( cen, ilambda+1, j, &rej )) ;
00093 check(cpl_image_set( img, 2, j+cen_shift, flux ) );
00094 }
00095
00096 check(nx = cpl_image_get_size_x( up ));
00097 check(ny = cpl_image_get_size_y( up));
00098 for( j = 1 ; j <= ny ; j++ ) {
00099 int rej ;
00100 double flux ;
00101
00102 check(flux = cpl_image_get( up, ilambda+1, j, &rej )) ;
00103 check(cpl_image_set( img, 3, j+up_shift, flux ) );
00104 }
00105
00106 cleanup:
00107 return ;
00108 }
00109
00110 static void make_data_cube( xsh_pre_3d * pre_3d, xsh_pre * pre_down,
00111 xsh_pre * pre_cen, xsh_pre * pre_up )
00112 {
00113 int nx, ny, nz, i ;
00114 cpl_image * cur_img = NULL ;
00115 int up_shift=0;
00116 int cen_shift =0;
00117 int slit_up, slit_cen;
00118
00119 nx = xsh_pre_3d_get_nx( pre_3d ) ;
00120 ny = xsh_pre_3d_get_ny( pre_3d ) ;
00121 nz = xsh_pre_3d_get_nz( pre_3d ) ;
00122 xsh_msg( "make_data_cube: %d x %d x %d", nx, ny, nz ) ;
00123
00124 slit_up = xsh_pre_get_ny( pre_up);
00125 slit_cen = xsh_pre_get_ny( pre_cen);
00126 if ( slit_up > slit_cen){
00127 cen_shift = slit_up-slit_cen;
00128 }
00129 else{
00130 up_shift=slit_cen-slit_up;
00131 }
00132
00133 xsh_msg( "make_data_cube: %d x %d x %d slit_shift %dx%dx%d", nx, ny, nz,
00134 cen_shift,cen_shift,up_shift);
00135
00136 for( i = 0 ; i<nz ; i++ ) {
00137
00138
00139 check( cur_img = cpl_image_new( nx, ny, CPL_TYPE_FLOAT ) ) ;
00140 check(fill_img( cur_img, i, pre_down->data, pre_cen->data, pre_up->data,
00141 up_shift,cen_shift)) ;
00142 check( xsh_image_3d_insert( pre_3d->data, cur_img, i ) ) ;
00143 xsh_free_image( &cur_img ) ;
00144
00145 check( cur_img = cpl_image_new( nx, ny, CPL_TYPE_FLOAT ) ) ;
00146 check(fill_img( cur_img, i, pre_down->errs, pre_cen->errs, pre_up->errs,
00147 up_shift,cen_shift)) ;
00148 check( xsh_image_3d_insert( pre_3d->errs, cur_img, i ) ) ;
00149 xsh_free_image( &cur_img ) ;
00150
00151 check( cur_img = cpl_image_new( nx, ny, CPL_TYPE_INT ) ) ;
00152 check(fill_img( cur_img, i, pre_down->qual, pre_cen->qual, pre_up->qual,
00153 up_shift,cen_shift)) ;
00154 check( xsh_image_3d_insert( pre_3d->qual, cur_img, i ) ) ;
00155 xsh_free_image( &cur_img ) ;
00156 }
00157
00158 cleanup:
00159 xsh_free_image( &cur_img ) ;
00160 return ;
00161 }
00162
00163
00164 cpl_frame* xsh_format( cpl_frameset *spectrum_frame_set,
00165 const char *result_name, xsh_instrument * instrument,
00166 const char* rec_prefix)
00167 {
00168 cpl_frame * result = NULL ;
00169 xsh_image_3d * flux_3d = NULL, * errs_3d = NULL, * qual_3d = NULL ;
00170 xsh_pre * pre_down = NULL, * pre_cen = NULL, * pre_up = NULL ;
00171 cpl_frame * frame_down = NULL, * frame_cen = NULL, * frame_up = NULL ;
00172 int nslitlet = 3, nslit, nlambda ;
00173 int nslit_up, nslit_lo, nslit_cen;
00174 char tag[80];
00175 char pcatg[80];
00176 xsh_pre_3d * pre_3d = NULL ;
00177 double lambda_min, lambda_max, lambda_step ;
00178 double crval2 = 0.0, cdelt2=0.0;
00179
00180 XSH_ASSURE_NOT_NULL( spectrum_frame_set ) ;
00181 XSH_ASSURE_NOT_NULL( result_name ) ;
00182 XSH_ASSURE_NOT_NULL( instrument ) ;
00183
00184 check( frame_down = cpl_frameset_get_frame( spectrum_frame_set, 0));
00185 check( pre_down = xsh_pre_load( frame_down, instrument));
00186 check( frame_cen = cpl_frameset_get_frame( spectrum_frame_set, 1));
00187 check( pre_cen = xsh_pre_load( frame_cen, instrument));
00188 check( frame_up = cpl_frameset_get_frame( spectrum_frame_set, 2));
00189 check( pre_up = xsh_pre_load( frame_up, instrument));
00190
00191
00192
00193 check(lambda_min=xsh_pfits_get_rectify_lambda_min( pre_down->data_header));
00194 check(lambda_max=xsh_pfits_get_rectify_lambda_max( pre_down->data_header));
00195 check(lambda_step=xsh_pfits_get_rectify_bin_lambda( pre_down->data_header));
00196 xsh_msg( "Rect Lambda min, max, bin: %lf, %lf, %lf", lambda_min, lambda_max,
00197 lambda_step) ;
00198
00199 nslit_lo = xsh_pre_get_ny( pre_down);
00200 nslit_up = xsh_pre_get_ny( pre_up);
00201 nslit_cen = xsh_pre_get_ny( pre_cen);
00202
00203 nslit = nslit_cen;
00204 check( crval2 = xsh_pfits_get_crval2(pre_cen->data_header));
00205 check( cdelt2 = xsh_pfits_get_cdelt2(pre_cen->data_header));
00206
00207 if (nslit_up > nslit_cen){
00208 nslit += nslit_up-nslit_cen;
00209 crval2 -= (nslit_up-nslit_cen)*cdelt2;
00210 }
00211 if (nslit_lo > nslit_cen){
00212 nslit += nslit_lo-nslit_cen;
00213 }
00214
00215 nlambda = xsh_pre_get_nx( pre_down);
00216
00217 xsh_msg("nslit : lo %d cen %d up %d",nslit_lo,nslit_cen,nslit_up);
00218
00219 check( pre_3d = xsh_pre_3d_new( nslitlet, nslit, nlambda));
00220 xsh_msg( "Pre_3d created: %dx%dx%d", nslitlet, nslit, nlambda);
00221
00222 check( flux_3d = xsh_pre_3d_get_data( pre_3d));
00223 check( errs_3d = xsh_pre_3d_get_errs( pre_3d));
00224 check( qual_3d = xsh_pre_3d_get_qual( pre_3d));
00225
00226
00227 check( xsh_pre_flip( pre_down, 0));
00228 check( xsh_pre_flip( pre_up, 0));
00229
00230 make_data_cube( pre_3d, pre_down, pre_cen, pre_up);
00231
00232
00233 check( cpl_propertylist_append( pre_3d->data_header,
00234 pre_cen->data_header));
00235 check( cpl_propertylist_append( pre_3d->errs_header,
00236 pre_cen->errs_header));
00237 check( cpl_propertylist_append( pre_3d->qual_header,
00238 pre_cen->qual_header));
00239
00240 sprintf(pcatg,"%s_%s",rec_prefix,
00241 XSH_GET_TAG_FROM_ARM( XSH_MERGE3D_IFU, instrument));
00242
00243 xsh_msg( " Setting PRO.CATG '%s'", pcatg ) ;
00244 check( xsh_pfits_set_pcatg( pre_3d->data_header, pcatg));
00245 check( xsh_pfits_set_pcatg( pre_3d->errs_header, pcatg));
00246 check( xsh_pfits_set_pcatg( pre_3d->qual_header, pcatg));
00247
00248
00249
00250
00251 check( xsh_pfits_set_crpix1( pre_3d->data_header, 1.0));
00252 check( xsh_pfits_set_crpix1( pre_3d->errs_header, 1.0));
00253 check( xsh_pfits_set_crpix1( pre_3d->qual_header, 1.0));
00254
00255 check( xsh_pfits_set_crval1( pre_3d->data_header, -0.6));
00256 check( xsh_pfits_set_crval1( pre_3d->errs_header, -0.6));
00257 check( xsh_pfits_set_crval1( pre_3d->qual_header, -0.6));
00258
00259 check( xsh_pfits_set_cdelt1( pre_3d->data_header, 0.6));
00260 check( xsh_pfits_set_cdelt1( pre_3d->errs_header, 0.6));
00261 check( xsh_pfits_set_cdelt1( pre_3d->qual_header, 0.6));
00262
00263
00264 check( xsh_pfits_set_crval2( pre_3d->data_header, crval2));
00265 check( xsh_pfits_set_crval2( pre_3d->errs_header, crval2));
00266 check( xsh_pfits_set_crval2( pre_3d->qual_header, crval2));
00267
00268 check( xsh_pfits_set_crpix3( pre_3d->data_header, 0.5));
00269 check( xsh_pfits_set_crpix3( pre_3d->errs_header, 0.5));
00270 check( xsh_pfits_set_crpix3( pre_3d->qual_header, 0.5));
00271
00272 check( xsh_pfits_set_crval3( pre_3d->data_header, lambda_min));
00273 check( xsh_pfits_set_crval3( pre_3d->errs_header, lambda_min));
00274 check( xsh_pfits_set_crval3( pre_3d->qual_header, lambda_min));
00275
00276
00277 check( xsh_pfits_set_cdelt3( pre_3d->data_header, lambda_step));
00278 check( xsh_pfits_set_cdelt3( pre_3d->errs_header, lambda_step));
00279 check( xsh_pfits_set_cdelt3( pre_3d->qual_header, lambda_step));
00280
00281 check( xsh_pfits_set_ctype3( pre_3d->data_header, "LINEAR"));
00282 check( xsh_pfits_set_ctype3( pre_3d->errs_header, "LINEAR"));
00283 check( xsh_pfits_set_ctype3( pre_3d->qual_header, "LINEAR"));
00284
00285
00286 check(xsh_set_cd_matrix3d(pre_3d->data_header));
00287 check(xsh_set_cd_matrix3d(pre_3d->errs_header));
00288 check(xsh_set_cd_matrix3d(pre_3d->qual_header));
00289
00290 xsh_msg( "Saving %s", result_name) ;
00291
00292 check( result = xsh_pre_3d_save( pre_3d, result_name, 0));
00293
00294 sprintf(tag,"%s_%s",rec_prefix,
00295 XSH_GET_TAG_FROM_ARM( XSH_MERGE3D_IFU, instrument));
00296
00297 xsh_msg( " Setting TAG '%s'", tag);
00298 check( cpl_frame_set_tag( result, tag));
00299 check( cpl_frame_set_group( result, CPL_FRAME_GROUP_PRODUCT));
00300 check( cpl_frame_set_level( result, CPL_FRAME_LEVEL_FINAL));
00301 check(cpl_frame_set_type( result, CPL_FRAME_TYPE_IMAGE));
00302
00303 cleanup:
00304 xsh_pre_free( &pre_down);
00305 xsh_pre_free( &pre_cen);
00306 xsh_pre_free( &pre_up);
00307 xsh_pre_3d_free( &pre_3d);
00308 return result;
00309 }