92 memset(
pdist, 0, nb*
sizeof(
int) ) ;
94 for( i = 0 ; (i+dist_order) < nb ; i++,
pdist++ ) {
95 *
pdist = xdisp[i] - xdisp[i+dist_order] ;
102 cpl_frame * spectral_frame,
108 int * abs_orders = NULL ;
109 int * dist_pixel = NULL ;
110 int * xdisp_cen = NULL ;
113 int * distances = NULL, * delta = NULL, * the_absorder = NULL ;
122 xsh_msg(
">>>> Entering compute_abs_order" ) ;
128 spec_size = spectral_list->
size ;
129 xsh_msg(
" Nb of Orders in Spectral Format: %d", spec_size ) ;
135 check(xsh_spectralformat_list_get_xdisp_cen( spectral_list,
136 abs_orders, xdisp_cen));
139 xsh_msg(
" DIST_ORDER = %d", dist_order ) ;
140 first_order = abs_orders[0] ;
141 last_order = abs_orders[spec_size-1]+1 ;
145 xsh_msg(
"First Order: %d, Last Order: %d", first_order, last_order ) ;
148 xsh_msg(
"Calcul Distances" ) ;
149 for( ndist = 0, i = 0 ; i<nb ; i++, ndist++ ) {
153 if ( j >= nb ) break ;
154 distances[i] = abs((start_x+i)->start_x - (start_x+j)->start_x ) ;
155 xsh_msg(
"distances[%d] = %d", i, distances[i] ) ;
164 float min_avg = 9999999. ;
166 for( i = 0 ; i<ndist ; i++ ) {
168 float sdelta = 0., avg = 0. ;
170 if ( distances[i] <= 0 ) break ;
171 xsh_msg(
" Start order %d", i ) ;
172 for( k = i, j = 0 ; j<spec_size; j++, k++ ) {
173 if ( dist_pixel[j] <= 0 ) break ;
174 sdelta += abs(distances[k] - dist_pixel[j]) ;
175 xsh_msg(
" found: %d, spec: %d, delta: %d (%d,%d)", k, j,
176 abs(distances[k] - dist_pixel[j]), distances[k],
179 if ( j == 0 ) continue ;
180 avg = sdelta/(float)j ;
181 xsh_msg(
" ===> avg: %.3lf", avg ) ;
183 if ( avg < min_avg ) {
188 xsh_msg(
" Best first absolute order found: %d (%d)", min_idx,
192 good_order = abs_orders[0] ;
193 xsh_msg(
"First good order: index %d, absorder %d, Position %d",
194 min_idx, good_order, (start_x+min_idx)->start_x ) ;
197 for( ngood=0, i=min_idx ; i<nb && good_order<last_order ; i++, ngood++ ) {
198 (start_x+i)->absorder = good_order++ ;
199 xsh_msg(
"Center[%d]: Absorder %d, Position %d", i, (start_x+i)->absorder,
200 (start_x+i)->start_x) ;
204 if ( min_idx != 0 ) {
205 xsh_msg(
"Remove spurious orders" ) ;
206 memmove( start_x, start_x+min_idx,
sizeof(
START_ORDER)*ngood ) ;
212 xsh_msg(
" ===== Orders Found: %d (should be %d)", ngood, expected ) ;
214 "\n***** NOT ENOUGH ORDERS FOUND. STOP *****\n" ) ;
235 for( j = 0, i = (nb-1) ; i>=0 ; i--, j++ )
236 *(nstart+j) = *(pstart+i) ;
249 if (
first->order < secnd->
order )
return -1 ;
250 else if (
first->order > secnd->
order )
return 1 ;
273 if ( thresh < 0 ) thresh = 0. ;
274 for( ppos = pos_x+ix0, ix = ix0 ; ix <
max ; ix++, ppos++ ) {
277 if ( *ppos < thresh ) continue ;
281 for( flux0 = 0., ppos++, ix++ ; ix <
max ; ix++, ppos++ ) {
282 if ( *ppos > flux0 ) {
292 for( ; ix <
max ; ix++, ppos++ ) {
293 if ( *ppos < thresh ) {
320 double max = -9999999. ;
322 for( ixmax = 0, ix = ix0 ; ix<ix1 ; ix++ ) {
326 flux = cpl_image_get( pre->
data, ix, iy, &rej ) ;
328 if ( rej != 0 ) continue ;
341 static FILE * fgnu = NULL, * freg = NULL ;
342 static const char * gnuname ;
349 if ( fgnu == NULL ) {
351 fgnu = fopen( gnuname,
"w" ) ;
352 fprintf( fgnu,
"set term x11\nplot " ) ;
355 else first_call = 0 ;
356 ppcent = pcent + fpos ;
358 sprintf( fname,
"order_%s_%02d.dat", arm,
order ) ;
359 fout = fopen( fname,
"w" ) ;
361 if ( !first_call ) fprintf( fgnu,
"," ) ;
362 fprintf( fgnu,
"'%s' u 1:2 w points pt 5 t''", fname ) ;
364 for( i = fpos ; i<npos; i++, ppcent++ )
365 fprintf( fout,
"%d %d %.3lf\n",
370 fprintf( fgnu,
"\n" ) ;
376 freg = fopen(
"order_create.reg",
"w" ) ;
377 fprintf( freg,
"# Region file format: DS9 version 4.0\n" ) ;
378 fprintf( freg,
"global color=red font=\"helvetica 4 normal\"select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 source\n" ) ;
379 fprintf( freg,
"image\n# RED center_x center_y (pixels)\n" ) ;
380 for( ppcent = pcent+fpos, i = fpos ; i <npos ; i++, ppcent++ ) {
381 fprintf( freg,
"point(%d.,%d.) #point=cross color=red font=\"helvetica 4 normal\"\n", ppcent->
pos_x, ppcent->
pos_y ) ;
383 if (
order == 0 ) fclose( freg ) ;
392 sprintf( fname,
"histo_%s.dat", arm ) ;
393 fout = fopen( fname,
"w" ) ;
395 for( i = 0 ; i<
size ; i++ )
396 fprintf( fout,
"%d %lf\n", i, *(posx+i) ) ;
404 double * vorder = NULL, * po ;
405 double * pos_x = NULL, * px ;
406 double * pos_y = NULL, * py ;
416 for( i = 0 ; i<npos ; i++, po++, px++, py++, pcent++ ) {
455 cpl_frame * spectral_frame,
460 cpl_frame * result = NULL ;
461 int nx,
ny, iy0, iy=0, ix, ix0, ix1, step_y ;
465 double * pos_x = NULL ;
467 int nfound, iord, npos, fpos ;
470 const char* tag=NULL;
471 const char* fname=NULL;
473 xsh_msg(
" ======= xsh_order_table_from_fmtchk" ) ;
474 xsh_msg(
" Window: %d, Step: %d, Search S/N; %.1lf, Follow S/N: %.1lf",
492 xsh_msg(
" Expected Orders: %d", norders ) ;
528 med = cpl_image_get_median_window( pre->
data, 1, iy0,
nx, iy0 ) ;
529 stdev = cpl_image_get_stdev_window( pre->
data, 1, iy0,
nx, iy0 ) ;
530 xsh_msg(
" ===> Y=%d, median = %lf, stdev = %lf", iy, med, stdev ) ;
532 for( ix = 1; ix <=
nx ; ix++ ) {
536 flux = cpl_image_get( pre->
data, ix, iy0, &rej ) ;
537 *(pos_x+ix-1) = flux ;
544 for( ix = 0, imax = 0, nfound = 0 ; nfound < (norders*2) ; ) {
547 if ( ix == -1 ) break ;
548 xsh_msg(
"Found Order %d at %d,%d, value: %lf", nfound, found, iy0,
550 (order_startx+nfound)->start_x = found+1 ;
555 xsh_msg(
"Found only 0 orders!");
556 xsh_msg(
"Try to decrease drecoverformat-search-min-sn parameter value");
569 spectral_frame, detect_param,
574 for( fpos = 0, npos = 0, iord = 0 ; iord < norders ; iord++ ) {
577 int low_y = 0, up_y =
ny-1 ;
580 xsh_msg(
" Seaching for Order %d (window: %d)", iord,
582 xsh_msg(
" Starting position: %d,%d",
583 (order_startx+iord)->start_x, iy0 );
585 ix = (order_startx+iord)->start_x ;
586 the_ord = (order_startx+iord)->absorder ;
587 centers[npos].
order = the_ord ;
588 centers[npos].
pos_x = ix ;
589 centers[npos].
pos_y = iy0 ;
590 centers[npos].
flux = *(pos_x+ix) ;
592 for( iy = (iy0+step_y) ; iy <=
ny ; iy+= step_y, npos++ ) {
596 if ( ix0 < 1 ) ix0 = 1 ;
597 if ( ix1 >=
nx ) ix1 =
nx ;
600 max = cpl_image_get( pre->
data, ix, iy, &dum ) ;
601 if ( dum != 0 ) continue ;
602 if (
max <= 0 || max < med*detect_param->fmtchk_follow_sn ) break ;
604 centers[npos].
order = the_ord ;
605 centers[npos].
pos_x = ix ;
606 centers[npos].
pos_y = iy ;
610 ix = (order_startx+iord)->start_x ;
611 for( iy = (iy0-step_y) ; iy > 0 ; iy -= step_y, npos++ ) {
614 if ( ix0 < 1 ) break ;
615 if ( ix1 >
nx ) break ;
618 max = cpl_image_get( pre->
data, ix, iy, &dum ) ;
619 if (
max <= 0 || max < med*detect_param->fmtchk_follow_sn ) {
620 xsh_msg(
" Stop Following at %d,%d (value = %.2lf)", ix, iy,
max ) ;
624 centers[npos].
order = the_ord ;
625 centers[npos].
pos_x = ix ;
626 centers[npos].
pos_y = iy ;
648 fname=
"ORDER_TAB_FROM_FMTCHK.fits";
static xsh_instrument * instrument
xsh_order_list * xsh_order_list_create(xsh_instrument *instr)
create an empty order list
cpl_frame * xsh_order_list_save(xsh_order_list *order_list, xsh_instrument *instrument, const char *filename, const char *tag, const int ny)
Save an order list to a frame.
void xsh_order_list_fit(xsh_order_list *list, int size, double *order, double *posx, double *posy, int deg_poly)
fit the polynomial solution of given points
int xsh_pre_get_ny(const xsh_pre *pre)
Get ny of pre structure.
int xsh_pre_get_nx(const xsh_pre *pre)
Get nx of pre structure.
static int compute_abs_order(START_ORDER *start_x, int nb, cpl_frame *spectral_frame, xsh_detect_continuum_param *detect_param, xsh_instrument *instrument)
static int find_maximum(int *found, double *pos_x, int ix0, int max, double thresh)
static void save_centers(CENTER_ORDER *pcent, int fpos, int npos, const char *arm)
static void save_pos_x(double *posx, int size, const char *arm)
cpl_frame * xsh_order_table_from_fmtchk(xsh_pre *pre, cpl_frame *spectral_frame, xsh_detect_continuum_param *detect_param, xsh_instrument *instrument)
static void invert_startx(START_ORDER *pstart, int nb)
static int search_max(xsh_pre *pre, int iy, int ix0, int ix1)
static void fit_order_list(xsh_order_list *list, int npos, CENTER_ORDER *pcent, int degree)
static int comp_center(const void *one, const void *two)
static void compute_dist_pixel(int *xdisp, int *pdist, int dist_order, int first, int last)
#define XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
#define XSH_ASSURE_NOT_NULL(pointer)
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
XSH_INSTRCONFIG * xsh_instrument_get_config(xsh_instrument *i)
Get the instrument default set of keywords.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_high(...)
int xsh_debug_level_get(void)
get debug level
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
#define pdist(x1, y1, x2, y2)
#define XSH_ORDER_TAB_GUESS
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
#define XSH_FREE(POINTER)
#define XSH_CALLOC(POINTER, TYPE, SIZE)