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;
00089
00090 double *xgauss = NULL, *ygauss = NULL, *vorder = NULL ;
00091 double *pos_x = NULL, *pos_y = NULL ;
00092 int size, i, nlinecat;
00093
00094 int *sort = NULL;
00095 const char *model_date = NULL;
00096 const char *tag = NULL;
00097 char fname[256];
00098 xsh_spectralformat_list *spectralformat_list = NULL;
00099 cpl_table* spc_fmt_tab=NULL;
00100 FILE* debug_out = NULL;
00101
00102 xsh_msg("Creating the Order Table");
00103 XSH_ASSURE_NOT_NULL( in_frame);
00104 XSH_ASSURE_NOT_NULL( resid_frame);
00105 XSH_ASSURE_NOT_NULL( arclines);
00106 XSH_ASSURE_NOT_NULL( da);
00107 XSH_ASSURE_NOT_NULL( dac);
00108 XSH_ASSURE_NOT_NULL( instrument);
00109 XSH_ASSURE_NOT_NULL( spectralformat_frame);
00110
00111 check( pre = xsh_pre_load( in_frame, instrument));
00112 check( resid_tab = xsh_resid_tab_load( resid_frame));
00113 check( arclist = xsh_arclist_load( arclines));
00114 check( spectralformat_list = xsh_spectralformat_list_load(
00115 spectralformat_frame, instrument));
00116 check( nlinecat = xsh_arclist_get_size( arclist));
00117
00118 check( model_date = xsh_pfits_get_date( resid_tab->header));
00119
00120
00121 check( size = xsh_resid_tab_get_size( resid_tab));
00122 xsh_msg_dbg_high ( " Resid Table Size: %d", size);
00123
00124 check( vlambda = xsh_resid_tab_get_lambda_data( resid_tab ) ) ;
00125 check( vorder = xsh_resid_tab_get_order_data( resid_tab ) ) ;
00126
00127
00128 check( xgauss = xsh_resid_tab_get_xgauss_data( resid_tab ) ) ;
00129 check( ygauss = xsh_resid_tab_get_ygauss_data( resid_tab ) ) ;
00130
00131
00132 XSH_CALLOC( pos_x, double, size);
00133 XSH_CALLOC( pos_y, double, size);
00134 for( i = 0 ; i<size ; i++ ) {
00135 *(pos_x+i) = *(xgauss+i) ;
00136 *(pos_y+i) = *(ygauss+i) ;
00137 }
00138
00139 check( sort = xsh_sort( vorder, size, sizeof(double),
00140 data_order_compare));
00141 check(xsh_reindex( vorder, sort, size));
00142 check(xsh_reindex( pos_x, sort, size));
00143 check(xsh_reindex( pos_y, sort, size));
00144 check(xsh_reindex( vlambda, sort, size));
00145
00146
00147 check(list = xsh_order_list_create( instrument));
00148 xsh_msg_dbg_medium("Number of orders expected : %d",list->size);
00149
00150 xsh_order_list_fit(list, size, vorder, pos_x, pos_y, da->ordertab_deg_y);
00151 xsh_msg("size=%d",size);
00152
00153
00154 for( i=0; i < list->size; i++){
00155 list->list[i].starty = 1;
00156 list->list[i].endy = pre->ny;
00157 }
00158 spc_fmt_tab=cpl_table_load(cpl_frame_get_filename(spectralformat_frame),1,0);
00159 if(cpl_table_has_column(spc_fmt_tab,XSH_SPECTRALFORMAT_TABLE_COLNAME_XMIN)) {
00160 for( i=0; i < list->size; i++){
00161 list->list[i].starty = spectralformat_list->list[i].ymin;
00162 list->list[i].endy = spectralformat_list->list[i].ymax;
00163 }
00164 }
00165 xsh_free_table(&spc_fmt_tab);
00166 xsh_spectralformat_list_free(&spectralformat_list);
00167 xsh_msg( " Save the produced order table" ) ;
00168
00169 if ( cpl_error_get_code() == CPL_ERROR_NONE){
00170
00171 check(header = xsh_order_list_get_header(list));
00172 check(xsh_pfits_set_qc_nlinecat(header,nlinecat));
00173 xsh_msg("create order tab. nlinecat=%d",nlinecat);
00174 check(xsh_pfits_set_qc_nlinefound(header,size));
00175
00176 check(xsh_pfits_set_qc(header, (void*)model_date,
00177 XSH_QC_MODEL_FMTCHK_DATE, instrument));
00178
00179 tag=XSH_GET_TAG_FROM_ARM( XSH_ORDER_TAB_GUESS,instrument);
00180 sprintf(fname,"%s%s",tag,".fits");
00181 check(result = xsh_order_list_save(list,instrument,fname,tag,pre->ny));
00182
00183
00184
00185
00186 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_MEDIUM) {
00187 int dy,dor;
00188
00189 debug_out = fopen("predict_cen_points.log","w");
00190 for(dy=0;dy< size; dy++){
00191 fprintf(debug_out,"%f %f\n",pos_x[dy],pos_y[dy]);
00192 }
00193
00194 fclose(debug_out);
00195 debug_out = fopen("predict_cen.log","w");
00196
00197 for(dor = 0; dor < list->size; dor++){
00198 for(dy=0;dy<pre->ny;dy++){
00199 float dx = 0;
00200
00201 check( dx = cpl_polynomial_eval_1d(list->list[dor].cenpoly,dy,NULL));
00202 fprintf(debug_out,"%f %d\n",dx,dy);
00203 }
00204 }
00205 fclose(debug_out);
00206 }
00207
00208 }
00209 else{
00210 if (da->mode_iterative){
00211 cpl_error_reset();
00212 xsh_msg("********** Not enough orders to produce ORDER_TABLE");
00213 }
00214 }
00215
00216 cleanup:
00217 if(debug_out!=NULL) {
00218 fclose(debug_out);
00219 }
00220 xsh_pre_free( &pre);
00221 xsh_resid_tab_free( &resid_tab);
00222 xsh_arclist_free( &arclist);
00223 xsh_order_list_free( &list);
00224 xsh_spectralformat_list_free(&spectralformat_list);
00225 XSH_FREE( pos_x);
00226 XSH_FREE( pos_y);
00227 XSH_FREE( sort);
00228 xsh_spectralformat_list_free(&spectralformat_list);
00229 return result ;
00230 }
00231