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
00026 #ifdef HAVE_CONFIG_H
00027 #include <config.h>
00028 #endif
00029
00030
00039 #include <xsh_drl.h>
00040 #include <xsh_pfits.h>
00041 #include <xsh_utils.h>
00042 #include <xsh_data_order.h>
00043 #include <xsh_data_arclist.h>
00044 #include <xsh_error.h>
00045 #include <xsh_utils.h>
00046 #include <xsh_msg.h>
00047 #include <xsh_data_pre.h>
00048 #include <xsh_data_order.h>
00049 #include <xsh_data_resid_tab.h>
00050 #include <cpl.h>
00051
00052
00053 static int
00054 data_order_compare (const void *one, const void *two)
00055 {
00056 xsh_sort_data* a = NULL;
00057 xsh_sort_data* b = NULL;
00058 double* da = NULL, *db = NULL;
00059
00060 a = (xsh_sort_data *) one;
00061 b = (xsh_sort_data *) two;
00062
00063 da = (double*)(a->data);
00064 db = (double*)(b->data);
00065
00066 if ((*da) <= (*db))
00067 return -1;
00068 else
00069 return 1;
00070 }
00071
00072
00073
00074 cpl_frame* xsh_create_order_table( cpl_frame *in_frame,
00075 cpl_frame* spectralformat_frame,
00076 cpl_frame *resid_frame,
00077 cpl_frame *arclines,
00078 xsh_detect_arclines_param *da,
00079 xsh_clipping_param *dac,
00080 xsh_instrument *instrument)
00081 {
00082 xsh_pre *pre = NULL;
00083 cpl_frame *result = NULL ;
00084 xsh_resid_tab *resid_tab = NULL ;
00085 xsh_order_list *list = NULL ;
00086 xsh_arclist *arclist = NULL;
00087 cpl_propertylist *header = NULL ;
00088 double *vlambda = NULL, *thpre_x = NULL, *thpre_y = NULL,
00089 *xgauss = NULL, *ygauss = NULL, *vorder = NULL ;
00090 double *pos_x = NULL, *pos_y = NULL ;
00091 int size, i, nlinecat, nlineclean;
00092 int *sort = NULL;
00093 const char *model_date = NULL;
00094 const char *tag = NULL;
00095 char fname[256];
00096 xsh_spectralformat_list *spectralformat_list = NULL;
00097 cpl_table* spc_fmt_tab=NULL;
00098
00099 xsh_msg("Creating the Order Table");
00100 XSH_ASSURE_NOT_NULL( in_frame);
00101 XSH_ASSURE_NOT_NULL( resid_frame);
00102 XSH_ASSURE_NOT_NULL( arclines);
00103 XSH_ASSURE_NOT_NULL( da);
00104 XSH_ASSURE_NOT_NULL( dac);
00105 XSH_ASSURE_NOT_NULL( instrument);
00106 XSH_ASSURE_NOT_NULL( spectralformat_frame);
00107
00108 check( pre = xsh_pre_load( in_frame, instrument));
00109 check( resid_tab = xsh_resid_tab_load( resid_frame));
00110 check( arclist = xsh_arclist_load( arclines));
00111 check( spectralformat_list = xsh_spectralformat_list_load(
00112 spectralformat_frame, instrument));
00113 check( nlinecat = xsh_arclist_get_size( arclist));
00114 check( nlineclean = xsh_arclist_get_size( arclist));
00115 check( model_date = xsh_pfits_get_date( resid_tab->header));
00116
00117
00118 check( size = xsh_resid_tab_get_size( resid_tab));
00119 xsh_msg_dbg_high ( " Resid Table Size: %d", size);
00120
00121 check( vlambda = xsh_resid_tab_get_lambda_data( resid_tab ) ) ;
00122 check( vorder = xsh_resid_tab_get_order_data( resid_tab ) ) ;
00123 check( thpre_x = xsh_resid_tab_get_thpre_x_data( resid_tab ) ) ;
00124 check( thpre_y = xsh_resid_tab_get_thpre_y_data( resid_tab ) ) ;
00125 check( xgauss = xsh_resid_tab_get_xgauss_data( resid_tab ) ) ;
00126 check( ygauss = xsh_resid_tab_get_ygauss_data( resid_tab ) ) ;
00127
00128
00129 XSH_CALLOC( pos_x, double, size);
00130 XSH_CALLOC( pos_y, double, size);
00131 for( i = 0 ; i<size ; i++ ) {
00132 *(pos_x+i) = *(xgauss+i) ;
00133 *(pos_y+i) = *(ygauss+i) ;
00134 }
00135
00136 check( sort = xsh_sort( vorder, size, sizeof(double),
00137 data_order_compare));
00138 check(xsh_reindex( vorder, sort, size));
00139 check(xsh_reindex( pos_x, sort, size));
00140 check(xsh_reindex( pos_y, sort, size));
00141 check(xsh_reindex( vlambda, sort, size));
00142
00143
00144 check(list = xsh_order_list_create( instrument));
00145 xsh_msg_dbg_medium("Number of orders expected : %d",list->size);
00146
00147 xsh_order_list_fit(list, size, vorder, pos_x, pos_y, da->ordertab_deg_y);
00148
00149
00150 for( i=0; i < list->size; i++){
00151 list->list[i].starty = 1;
00152 list->list[i].endy = pre->ny;
00153 }
00154 spc_fmt_tab=cpl_table_load(cpl_frame_get_filename(spectralformat_frame),1,0);
00155 if(cpl_table_has_column(spc_fmt_tab,XSH_SPECTRALFORMAT_TABLE_COLNAME_XMIN)) {
00156 for( i=0; i < list->size; i++){
00157 list->list[i].starty = spectralformat_list->list[i].ymin;
00158 list->list[i].endy = spectralformat_list->list[i].ymax;
00159 }
00160 }
00161 xsh_free_table(&spc_fmt_tab);
00162 xsh_spectralformat_list_free(&spectralformat_list);
00163 xsh_msg( " Save the produced order table" ) ;
00164
00165 if ( cpl_error_get_code() == CPL_ERROR_NONE){
00166
00167 check(header = xsh_order_list_get_header(list));
00168 check(xsh_pfits_set_qc_nlinecat(header,nlinecat));
00169 check(xsh_pfits_set_qc_nlinefound(header,size));
00170
00171 check(xsh_pfits_set_qc(header, (void*)model_date,
00172 XSH_QC_MODEL_FMTCHK_DATE, instrument));
00173
00174 tag=XSH_GET_TAG_FROM_ARM( XSH_ORDER_TAB_GUESS,instrument);
00175 sprintf(fname,"%s%s",tag,".fits");
00176 check(result = xsh_order_list_save(list,instrument,fname,tag,pre->ny));
00177 xsh_add_temporary_file( fname ) ;
00178
00179
00180
00181 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_MEDIUM) {
00182 FILE* debug_out = NULL;
00183 int dy,dor;
00184
00185 debug_out = fopen("predict_cen_points.log","w");
00186 for(dy=0;dy< size; dy++){
00187 fprintf(debug_out,"%f %f\n",pos_x[dy],pos_y[dy]);
00188 }
00189
00190 fclose(debug_out);
00191 debug_out = fopen("predict_cen.log","w");
00192
00193 for(dor = 0; dor < list->size; dor++){
00194 for(dy=0;dy<pre->ny;dy++){
00195 float dx = 0;
00196
00197 check( dx = cpl_polynomial_eval_1d(list->list[dor].cenpoly,dy,NULL));
00198 fprintf(debug_out,"%f %d\n",dx,dy);
00199 }
00200 }
00201 fclose(debug_out);
00202 }
00203
00204 }
00205 else{
00206 if (da->mode_iterative){
00207 cpl_error_reset();
00208 xsh_msg("********** Not enough orders to produce ORDER_TABLE");
00209 }
00210 }
00211
00212 cleanup:
00213 xsh_pre_free( &pre);
00214 xsh_resid_tab_free( &resid_tab);
00215 xsh_arclist_free( &arclist);
00216 xsh_order_list_free( &list);
00217 xsh_spectralformat_list_free(&spectralformat_list);
00218 XSH_FREE( pos_x);
00219 XSH_FREE( pos_y);
00220 XSH_FREE( sort);
00221 xsh_spectralformat_list_free(&spectralformat_list);
00222 return result ;
00223 }
00224