94 double flux_b,
double weight_b,
double *flux_res,
100 double tmp_val = 1.0/(weight_a+weight_b);
101 *flux_res = (weight_a*flux_a+weight_b*flux_b) * tmp_val;
102 *err_res = sqrt(tmp_val);
109xsh_spectrum_clean(
xsh_spectrum *spectrum,
const int decode_bp){
117 cpl_mask * filtered =
xsh_bpm_filter(mask,5,5,CPL_FILTER_CLOSING);
119 cpl_image_save(spectrum->
flux,
"before.fits", CPL_TYPE_FLOAT, NULL, CPL_IO_CREATE);
120 cpl_image_reject_from_mask(spectrum->
flux,filtered);
122 cpl_image_reject_from_mask(spectrum->
errs,filtered);
123 cpl_detector_interpolate_rejected(spectrum->
flux);
124 cpl_detector_interpolate_rejected(spectrum->
errs);
125 cpl_image_save(spectrum->
flux,
"after.fits", CPL_TYPE_FLOAT, NULL, CPL_IO_CREATE);
150 cpl_frame *res_frame = NULL;
154 int iorder = 0, islit = 0;
155 int spectrum_size_lambda = 0;
156 int spectrum_size_slit = 0;
157 double lambda_min = 0.0, lambda_max = 0.0,
lambda_step = 0.0;
158 double *spectrum_flux = NULL;
159 double *spectrum_errs = NULL;
160 int *spectrum_qual = NULL;
161 int spectrum_flux_index = 0;
162 int *spectrum_by_lambda = NULL;
163 const char* rec_pcatg = NULL;
164 char* spectrum_name = NULL;
165 const char* name = NULL;
166 cpl_propertylist* header = NULL;
177 name = cpl_frame_get_filename(rec_frame);
178 header = cpl_propertylist_load(name, 0);
184 }
else if (naxis == 1) {
204 double slit_min = 0.0, slit_max = 0.0,
slit_step = 0.0;
211 "Construct 2D spectrum %f %f %f", slit_min, slit_max,
slit_step);
213 "lambda min %f max %f step %f", lambda_min, lambda_max,
lambda_step);
219 "lambda min %f max %f step %f", lambda_min, lambda_max,
lambda_step);
234 "Lambda %f to %f", spectrum_size_lambda, spectrum_size_slit,
235 lambda_min, lambda_min+
lambda_step*(spectrum_size_lambda-1));
240 int problem_slit_size = 0;
242 for (islit = 0; islit < spectrum_size_slit; islit++) {
243 for (iorder = rec_list->
size - 1; iorder >= 0; iorder--) {
256 if (spectrum_size_slit != nslit) {
261 problem_slit_size = 1;
265 "slit %d lambda %f %f", islit, lambda[0], lambda[nlambda-1]);
274 "iorder %d begin at index %f --> %d", iorder,
n, spectrum_flux_index);
277 int ilambda_off=spectrum_flux_index+ islit*spectrum_size_lambda;
278 int islit_nlambda = islit * nlambda;
280 for (ilambda = 0; ilambda < nlambda; ilambda++) {
283 int i_rec_order = ilambda + islit_nlambda;
284 int i_spectrum = ilambda_off + ilambda;
291 if (errs[i_rec_order] == 0) {
292 spectrum_qual[i_spectrum] = qual[i_rec_order];
312 if (spectrum_by_lambda[i_spectrum] == 0) {
313 spectrum_by_lambda[i_spectrum] = 1;
314 spectrum_flux[i_spectrum] = flux[i_rec_order];
315 spectrum_errs[i_spectrum] = errs[i_rec_order];
316 spectrum_qual[i_spectrum] = qual[i_rec_order];
318 double flux_a, err_a, weight_a;
319 double flux_b, err_b, weight_b;
320 double flux_res = 0.0, err_res = 1.0;
321 int qual_a, qual_b, qual_res;
323 flux_a = spectrum_flux[i_spectrum];
324 err_a = spectrum_errs[i_spectrum];
325 qual_a = spectrum_qual[i_spectrum];
327 flux_b = flux[i_rec_order];
328 err_b = errs[i_rec_order];
329 qual_b = qual[i_rec_order];
337 if (((qual_a & decode_bp) == 0) && ((qual_b & decode_bp) > 0)) {
343 }
else if (((qual_a & decode_bp) > 0) && ((qual_b & decode_bp) == 0)) {
352 weight_a = 1.0 / (err_a * err_a);
353 weight_b = 1.0 / (err_b * err_b);
356 qual_res = qual_a | qual_b;
361 spectrum_flux[i_spectrum] = flux_res;
362 spectrum_errs[i_spectrum] = err_res;
363 spectrum_qual[i_spectrum] = qual_res;
364 spectrum_by_lambda[i_spectrum] = 1;
373 if (problem_slit_size == 1) {
378 cpl_propertylist_erase_regexp(rec_list->
header,
"^CRVAL1", 0);
379 cpl_propertylist_erase_regexp(rec_list->
header,
"^CRVAL2", 0);
380 cpl_propertylist_erase_regexp(rec_list->
header,
"^CRPIX1", 0);
381 cpl_propertylist_erase_regexp(rec_list->
header,
"^CRPIX2", 0);
382 cpl_propertylist_erase_regexp(rec_list->
header,
"^CDELT1", 0);
383 cpl_propertylist_erase_regexp(rec_list->
header,
"^CDELT2", 0);
393 check( cpl_frame_set_tag( res_frame, tag));
419 int merge_par,
const char* rec_prefix)
421 cpl_frame *res_frame = NULL;
449 int merge_par,
int slitlet,
450 const char* rec_prefix)
452 cpl_frame *res_frame = NULL;
453 cpl_propertylist *header = NULL;
454 const char *rec_pcatg = NULL;
455 const char *tag_suf = NULL;
456 const char *filename = NULL;
464 check( filename = cpl_frame_get_filename( rec_frame));
465 check( header = cpl_propertylist_load( filename, 0));
467 check(tag_suf = cpl_frame_get_tag(rec_frame));
479 else if ( strstr( rec_pcatg,
XSH_ORDER2D ) != NULL ) {
507 else if ( strstr( rec_pcatg,
XSH_ORDER1D ) != NULL){
513 xsh_msg_error(
"Invalid PRO.CATG %s for file %s", rec_pcatg, filename);
515 sprintf(tag,
"%s_%s",rec_prefix,tag_suf);
527 cpl_propertylist *rec_header = NULL;
528 cpl_propertylist *shift_header = NULL;
529 const char *rec_name = NULL;
530 const char *shift_name = NULL;
531 double waveref, slitref;
536 check( rec_name = cpl_frame_get_filename( rec_frame));
537 check( shift_name = cpl_frame_get_filename( shift_frame));
539 check( rec_header = cpl_propertylist_load( rec_name, 0));
540 check( shift_header = cpl_propertylist_load( shift_name, 0));
545 if (cpl_error_get_code() == CPL_ERROR_NONE){
548 check( cpl_propertylist_save( rec_header, rec_name, CPL_IO_EXTEND));
578 const char* rec_prefix)
580 cpl_frameset *result_set = NULL;
581 cpl_frameset *drl_frameset = NULL;
589 check( result_set = cpl_frameset_new());
594 cpl_frame * rec_frame = NULL ;
595 cpl_frame * ord_frame = NULL ;
597 check( rec_frame = cpl_frameset_get_frame( drl_frameset, i));
601 merge_par,slitlet,rec_prefix));
605 cpl_frame_get_filename( ord_frame)) ;
606 check( cpl_frameset_insert( result_set, ord_frame));
611 if (cpl_error_get_code() != CPL_ERROR_NONE){
static xsh_instrument * instrument
cpl_mask * xsh_qual_to_cpl_mask(cpl_image *qual, const int decode_bp)
cpl_mask * xsh_bpm_filter(const cpl_mask *input_mask, cpl_size kernel_nx, cpl_size kernel_ny, cpl_filter_mode filter)
Allows the growing and shrinking of bad pixel masks. It can be used to e.g. set pixels to bad if the ...
int xsh_rec_list_get_nslit(xsh_rec_list *list, int idx)
double * xsh_rec_list_get_lambda(xsh_rec_list *list, int idx)
xsh_rec_list * xsh_rec_list_load(cpl_frame *frame, xsh_instrument *instrument)
load an rec list from a frame
float * xsh_rec_list_get_data1(xsh_rec_list *list, int idx)
float * xsh_rec_list_get_errs1(xsh_rec_list *list, int idx)
xsh_rec_list * xsh_rec_list_load_eso(cpl_frame *frame, xsh_instrument *instrument)
xsh_rec_list * xsh_rec_list_load_eso_1d(cpl_frame *frame, xsh_instrument *instrument)
int * xsh_rec_list_get_qual1(xsh_rec_list *list, int idx)
int xsh_rec_list_get_nlambda(xsh_rec_list *list, int idx)
void xsh_rec_list_free(xsh_rec_list **list)
free memory associated to a rec_list
int xsh_spectrum_get_size_lambda(xsh_spectrum *s)
Get lambda axis size of spectrum.
double * xsh_spectrum_get_errs(xsh_spectrum *s)
Get errs of spectrum.
int * xsh_spectrum_get_qual(xsh_spectrum *s)
Get qual of spectrum.
cpl_frame * xsh_spectrum_save(xsh_spectrum *s, const char *filename, const char *tag)
save a spectrum
double * xsh_spectrum_get_flux(xsh_spectrum *s)
Get flux of spectrum.
xsh_spectrum * xsh_spectrum_2D_create(double lambda_min, double lambda_max, double lambda_step, double slit_min, double slit_max, double slit_step)
Create a 2D spectrum structure.
xsh_spectrum * xsh_spectrum_1D_create(double lambda_min, double lambda_max, double lambda_step)
Create a 1D spectrum structure.
void xsh_spectrum_free(xsh_spectrum **s)
free memory associated to an 1D spectrum
int xsh_spectrum_get_size_slit(xsh_spectrum *s)
Get slit axis ize of spectrum.
#define xsh_error_reset()
#define XSH_ASSURE_NOT_NULL(pointer)
cpl_frameset * xsh_merge_ord_ifu(cpl_frameset *rec_frameset, xsh_instrument *instrument, int merge_par, const char *rec_prefix)
Merge orders of the rectified frame using merge parameters.
cpl_frame * xsh_merge_ord(cpl_frame *rec_frame, xsh_instrument *instrument, int merge_par, const char *rec_prefix)
Merge orders of the rectified frame using merge parameters.
static void xsh_merge_point(double flux_a, double weight_a, double flux_b, double weight_b, double *flux_res, double *err_res)
Compute flux and error associated to each merged spectrum point.
static void xsh_frame_set_shift_ref(cpl_frame *rec_frame, cpl_frame *shift_frame)
cpl_frame * xsh_merge_ord_slitlet(cpl_frame *rec_frame, xsh_instrument *instrument, int merge_par, int slitlet, const char *rec_prefix)
Merge orders of the rectified frame using merge parameters.
static cpl_frame * xsh_merge_ord_with_tag(cpl_frame *rec_frame, xsh_instrument *instrument, int merge_par, const char *tag)
Merge the orders.
#define xsh_msg_warning(...)
Print an warning message.
#define xsh_msg_dbg_medium(...)
#define xsh_msg_error(...)
Print an error message.
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_high(...)
double xsh_pfits_get_rectify_space_max(cpl_propertylist *plist)
find out the rectify SPACE max
const char * xsh_pfits_get_bunit(const cpl_propertylist *plist)
find out the BUNIT
double xsh_pfits_get_rectify_lambda_min(cpl_propertylist *plist)
find out the rectify lambda min
double xsh_pfits_get_rectify_space_min(cpl_propertylist *plist)
find out the rectify space min
double xsh_pfits_get_shiftifu_lambdaref(cpl_propertylist *plist)
double xsh_pfits_get_rectify_bin_lambda(cpl_propertylist *plist)
find out the rectify lambda binning
void xsh_pfits_set_shiftifu_lambdaref(cpl_propertylist *plist, double value)
double xsh_pfits_get_rectify_bin_space(cpl_propertylist *plist)
find out the rectify space (slit) binning
const char * xsh_pfits_get_pcatg(const cpl_propertylist *plist)
find out the pcatg
void xsh_pfits_set_shiftifu_slitref(cpl_propertylist *plist, double value)
double xsh_pfits_get_shiftifu_slitref(cpl_propertylist *plist)
void xsh_pfits_set_bunit(cpl_propertylist *plist, const char *value)
Write the BUNIT value.
int xsh_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value
double xsh_pfits_get_rectify_lambda_max(cpl_propertylist *plist)
find out the rectify lambda max
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
void xsh_free_mask(cpl_mask **m)
Deallocate an image mask and set the pointer to NULL.
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
long xsh_round_double(double x)
Computes round(x)
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
cpl_propertylist * header
cpl_propertylist * errs_header
cpl_propertylist * flux_header
cpl_frameset * xsh_frameset_drl_frames(cpl_frameset *set)
extract DRL specific frames from frameset
#define XSH_GET_TAG_FROM_SLITLET(TAG, slitlet, instr)
#define XSH_COMBINED_OFFSET_2D_IFU
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
#define XSH_COMBINED_OFFSET_2D_SLIT
static const char * SlitletName[]
#define XSH_FREE(POINTER)
#define XSH_CALLOC(POINTER, TYPE, SIZE)