/*---------------------------------------------------------------------------- File name : sp_extract.c Author : Y. Jung Created on : Feb. 2001 Description : ISAAC spectrum extraction ---------------------------------------------------------------------------*/ /* $Id: sp_extract.c,v 1.24 2002/03/04 14:13:19 yjung Exp $ $Author: yjung $ $Date: 2002/03/04 14:13:19 $ $Revision: 1.24 $ */ /*---------------------------------------------------------------------------- Includes ---------------------------------------------------------------------------*/ #include "eclipse.h" #include "isaacp_lib.h" /*--------------------------------------------------------------------------- Function prototypes ---------------------------------------------------------------------------*/ static int sp_extract_engine(char *, char *, int, int, int, int, int) ; static int sp_extract_write_table(char *, int, pixelvalue **, char *) ; /*---------------------------------------------------------------------------- Main code ---------------------------------------------------------------------------*/ int isaac_sp_extract_main(void * dict) { dictionary * d ; int spec_width ; int sky_dist ; int sky_width ; int spec_pos ; int no_graph ; char argname[10] ; char * name_i ; char * name_o ; int nfiles ; int errors ; int i ; d = (dictionary*)dict ; /* Get options */ spec_width = dictionary_getint(d, "arg.width", 10) ; sky_dist = dictionary_getint(d, "arg.sky_dist", 10) ; sky_width = dictionary_getint(d, "arg.sky_width", 30) ; spec_pos = dictionary_getint(d, "arg.spec_pos", 512) ; no_graph = dictionary_getint(d, "arg.nograph", 0) ; /* Get input/output file names */ nfiles = dictionary_getint(d, "arg.n", -1) ; if (nfiles<0) { e_error("missing input file name(s): aborting"); return -1 ; } /* Loop on input file names */ errors = 0 ; for (i=1 ; ilx, 2)) == NULL) { e_warning("cannot estimate the dispersion model") ; wave_flag = 0 ; } /* Verify the validity of spec_pos */ spec_length = inimage->lx ; slit_length = inimage->ly ; if (spec_pos <= 1 || spec_pos >= inimage->ly) { e_error("invalid spectrum position: [%d] - aborting",spec_pos) ; image_del(inimage) ; free(wavecal) ; return -1 ; } /* Allocate the ouput arrays */ array[0] = malloc(spec_length*sizeof(pixelvalue)) ; array[1] = malloc(spec_length*sizeof(pixelvalue)) ; if (sky_width == 0) { e_comment(1, "No sky background subtraction") ; } /* Extract the spectrum and get rid of the residual sky */ for (i=0 ; i slit_length) up_side = slit_length ; spec_width = up_side - low_side ; /* Positions of the residual sky */ sky_pos[1] = (int)(spec_pos - sky_dist) ; sky_pos[0] = sky_pos[1] - sky_width ; sky_pos[2] = (int)(spec_pos + sky_dist) ; sky_pos[3] = sky_pos[2] + sky_width ; if ((extr_line = image_getvig(inimage, i+1, low_side, i+1, up_side)) == NULL) { e_error("error in line extraction - aborting") ; free(array[0]) ; free(array[1]) ; image_del(inimage) ; free(wavecal) ; return -1 ; } if ((sky_pos[0] < 1) && (sky_width !=0)) { median_1 = image_getmedian_vig(inimage, i+1, sky_pos[2], i+1, sky_pos[3]) ; sky_estim = median_1 ; } else if ((sky_pos[3] > slit_length) && (sky_width !=0)) { median_1 = image_getmedian_vig(inimage, i+1, sky_pos[0], i+1, sky_pos[1]) ; sky_estim = median_1 ; } else if (sky_width !=0) { median_1 = image_getmedian_vig(inimage, i+1, sky_pos[0], i+1, sky_pos[1]) ; median_2 = image_getmedian_vig(inimage, i+1, sky_pos[2], i+1, sky_pos[3]) ; sky_estim=(median_1 + median_2)/2 ; } else { sky_estim = 0 ; } array[1][i]=(pixelvalue)image_getsumpix(extr_line) ; array[1][i] = (pixelvalue)array[1][i] - spec_width*sky_estim ; image_del(extr_line) ; } image_del(inimage) ; /* Fill array[0] */ if (wave_flag) { for (i=0 ; icol ; for (i=0 ; inc ; i++) { qfits_col_fill(col, 1, sizeof(double), TFITS_BIN_TYPE_D, " ", " ", " ", " ", 0, 0.0, 0, 1.0, i*sizeof(double)) ; col++ ; } col = table->col ; sprintf(col->tlabel, "Wavelength") ; col++ ; sprintf(col->tlabel, "Extracted") ; /* Convert pixelvalue array to double array */ array_double[0] = malloc(nb_lines*sizeof(double)) ; array_double[1] = malloc(nb_lines*sizeof(double)) ; for (i=0 ; iname[0] = strdup(imagename) ; /* Write the HISTORY keywords with the input file names */ if (isaac_add_files_history(fh, lnames) == -1) { e_warning("cannot write HISTORY keywords in out file") ; } framelist_del(lnames) ; /* Write the file on disk */ if (qfits_save_table_hdrdump((void**)array_double, table, fh) == -1) { e_error("cannot write file: %s", name) ; qfits_header_destroy(fh) ; qfits_table_close(table) ; free(array_double[0]) ; free(array_double[1]) ; return -1 ; } qfits_table_close(table) ; qfits_header_destroy(fh) ; free(array_double[0]) ; free(array_double[1]) ; e_comment(0, "File [%s] produced", name) ; return 0 ; }