124 const int pre_overscan_corr,
128 cpl_frame * result = NULL;
133 char resultname[256];
151 if(pre_overscan_corr==0) {
157 check(avg=cpl_image_get_mean(xbias->
data));
167 sprintf(resultname,
"%s.fits",tag);
171 check( cpl_frame_set_tag( result,tag));
174 if ( cpl_error_get_code() != CPL_ERROR_NONE) {
197 cpl_frameset * result = NULL;
199 cpl_frame* res = NULL;
201 char resultname[256];
202 int i = 0, size_on = 0, size_off = 0;
209 check(size_on = cpl_frameset_get_size(on));
210 check(size_off = cpl_frameset_get_size(off));
217 for ( i=0; i< size_on; i++) {
218 cpl_frame* on_f = NULL;
219 cpl_frame* off_f = NULL;
221 check( on_f = cpl_frameset_get_frame(on,i));
222 check( off_f = cpl_frameset_get_frame(off,i));
223 sprintf(resultname,
"ON-OFF_%d.fits",i);
226 check(cpl_frameset_insert(result, res));
231 if (cpl_error_get_code() != CPL_ERROR_NONE) {
251 cpl_frame * result = NULL;
259 double dit_tol=0.001;
260 const char* tag=NULL;
276 "EXPTIME must be greater than 0 : %f",
exptime);
286 tag=cpl_frame_get_tag(frame);
288 check(cpl_frame_set_tag (result,tag));
291 if (cpl_error_get_code () != CPL_ERROR_NONE) {
301 const int xmax,
const int x_hbox,
302 const int decode_bp,
int* qual,
303 int* pmask,
int*
size) {
309 if((xmax-xmin+1) >= 2*x_hbox+1) {
310 xcen = 0.5*(xmin+xmax);
313 xs = (xs > 0) ? xs: 0;
314 xe = (xe <
nx) ? xe:
nx-1;
316 for (
x = xs;
x <= xe;
x++) {
317 if ( (qual[offset +
x] & decode_bp) == 0) {
318 pmask[offset +
x] = 1;
359 float x1 = 0, x2 = 0;
362 cpl_image* mask = NULL;
391 if ( strcmp(prefix,
"MFLAT_D2") == 0 ) {
403 qual=cpl_image_get_data_int(pre->
qual);
406 mask = cpl_image_new(
nx,
ny, CPL_TYPE_INT);
407 pmask = cpl_image_get_data_int(mask);
409 for (
y = 1;
y <
ny;
y++) {
424 for (ord = orderlist->
size - 1; ord > 0; ord--) {
441 if (y <= starty || y >=
endy) {
446 if (ord < (orderlist->
size - 1)) {
447 if (y <= starty || y >=
endy) {
468 if (ord == orderlist->
size - 1) {
469 if ( strcmp(prefix,
"MFLAT_D2") == 0 ) {
477 if (y <= starty || y >=
endy) {
512 if (y <= 650 || y >=
endy) {
531 if (y <= 1600 || y >=
endy) {
579 x1 = x2 - sx_backg + x_hbox;
605 if ( strcmp(prefix,
"MFLAT_D2") == 0 ) {
607 if (ord == orderlist->
size - 2) {
608 if (y <= starty || y >=
endy) {
627 xmax = xmin+x_margin2;
652 sprintf(name,
"bkg_mask_%s.fits",prefix);
655 pdata = cpl_image_get_data_float(pre->
data);
656 perrs = cpl_image_get_data_float(pre->
errs);
657 pqual = cpl_image_get_data_int(pre->
qual);
659 for (
y = 1;
y <
ny;
y++) {
660 for (
x = 1;
x <
nx;
x++) {
662 if ((pmask[pix] == 1) && ((pqual[pix] & instr->
decode_bp) == 0)) {
703 int grid_size_x = 0, grid_size_y = 0;
704 int incr = 0, i = 0,
y = 0, lost = 0;
707 float x1 = 0, x2 = 0, dx = 0;
708 double medflux = 0.0;
711 grid_size_x = orderlist->
size + 5;
712 grid_size_y = background_par->
sampley + 2;
715 "parameter background-nb-y=%d "
716 "must be set to a positive value", background_par->
sampley);
721 grid_size_y > 0 && grid_size_y < pre->
ny,
722 CPL_ERROR_ILLEGAL_INPUT,
723 "grid_size_y=%d must be in (0,%d) range "
724 "parameter background-nb-y=%d "
725 "may have been set to a too large or too small value", grid_size_y, pre->
ny, background_par->
sampley);
732 CPL_ERROR_ILLEGAL_INPUT,
733 "parameter -background-radius-x (%d) must be > 0 and < %d", background_par->
radius_x, pre->
nx);
736 CPL_ERROR_ILLEGAL_INPUT,
737 "parameter -background-radius-y (%d) must be > 0 and < %d", background_par->
radius_y, pre->
ny);
739 int box_sizex = 2 * background_par->
radius_x + 1;
740 int box_sizey = 2 * background_par->
radius_y + 1;
744incr = (int) ceil((
float) pre->
ny / (float) background_par->
sampley);
748 for (
y = 1;
y <= (pre->
ny - 1 + incr);
y += incr) {
751 if ((
y + incr) > pre->
ny) {
756 if ((
y == 1) || (
y == pre->
ny)) {
757 sizey = background_par->
radius_y + 1;
774 if ((y <= orderlist->list[orderlist->
size - 2].starty)
789 for (i = orderlist->
size - 1; i > 0; i--) {
796 if (y <= starty || y >=
endy) {
821 x = floor(0.5 * (x1 + x2 + 1));
822 dx = 0.5 * (x2 - x1);
825 if ((
x > (background_par->
radius_x + 1)) && (box_sizex < dx)) {
845 if (i == (orderlist->
size - 1)) {
850 xsh_msg(
"Monitor dx=%d x=%g y=%d",
x, dx,
y);
852 if (
x > (background_par->
radius_x + 1)) {
873 x = x1 + (x1 - x2) * 1.2;
901 if ((y <= orderlist->list[0].
starty) ||
y >= orderlist->
list[0].
endy) {
922 cleanup:
return grid;
948 cpl_frame *edges_order_tab_frame,
952 cpl_frame **grid_frame,
953 cpl_frame **backg_frame,
956 const int save_sub_bkg)
959 cpl_frame *result = NULL;
964 cpl_image *background_img = NULL;
971 cpl_propertylist *plist = NULL;
972 cpl_table *grid_tbl = NULL;
998 xsh_msg(
"generate background image: poly");
1006 xsh_msg(
"Prepare final products");
1010 plist=cpl_propertylist_new();
1021 check( cpl_image_save( background_img, fname, CPL_BPP_IEEE_FLOAT,
1022 plist, CPL_IO_DEFAULT));
1030 CPL_FRAME_TYPE_IMAGE, CPL_FRAME_GROUP_CALIB,
1031 CPL_FRAME_LEVEL_FINAL));
1034 check( cpl_image_subtract( pre->
data, background_img));
1043 if(save_sub_bkg==0) {
1050 check(cpl_frame_set_tag (result, pcatg));
1051 check(cpl_frame_set_type (result, CPL_FRAME_TYPE_IMAGE));
1052 check(cpl_frame_set_group (result, CPL_FRAME_GROUP_CALIB));
1053 check(cpl_frame_set_level (result, CPL_FRAME_LEVEL_FINAL));
1065 plist = cpl_propertylist_new();
1067 check( cpl_table_save( grid_tbl, plist, NULL, fname, CPL_IO_DEFAULT));
1077 CPL_FRAME_GROUP_CALIB,CPL_FRAME_LEVEL_FINAL);
1082 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1115 cpl_image* image_bkg=NULL;
1116 float *background_data = NULL;
1120 background_data = cpl_image_get_data_float(image_bkg);
1122 for (
y = 1;
y <= sy;
y++)
1125 for (
x = 1;
x <= sx;
x++)
1140 double mse = 0, rmse = 0;
1141 cpl_size total_clipped = 0;
1142 cpl_image* bkg_img = NULL;
1147 xsh_msg(
"degx=%d degy=%d kappa=%g", degx, degy,
kappa);
1162 cpl_size deg_xy = (degx + 1) * (degy + 1);
1164 cpl_table_get_nrow(*grid_tab) > (degx + 1)*(degy + 1),
1165 CPL_ERROR_ILLEGAL_OUTPUT,
1166 "Too few sample points available (%" CPL_SIZE_FORMAT
" point(s)) to make the fit "
1167 "(more than %" CPL_SIZE_FORMAT
" points needed). "
1168 "Increase number of sample points or increase kappa", cpl_table_get_nrow(*grid_tab), deg_xy);
1173 background =
xsh_polynomial_regression_2d( *grid_tab,
"X",
"Y",
"INT",
"ERR", degx, degy,
"INTfit", NULL, NULL, &mse, NULL, NULL,
kappa, -1),
1174 "Error fitting polynomial");
1177 cpl_table_duplicate_column(*grid_tab,
"Residual", *grid_tab,
"INT");
1178 cpl_table_subtract_columns(*grid_tab,
"Residual",
"INTfit");
1181 rmse = cpl_table_get_column_stdev(*grid_tab,
"Residual");
1188 "Error selecting rows");
1198 total_clipped += n_clipped;
1204 "RMS = %f. %" CPL_SIZE_FORMAT
" of %" CPL_SIZE_FORMAT
" points rejected in kappa-sigma clipping", rmse, n_clipped, cpl_table_get_nrow(*grid_tab));
1206 cpl_table_erase_selected(*grid_tab);
1208 if (n_clipped > 0) {
1209 cpl_table_erase_column(*grid_tab,
"INTfit");
1210 cpl_table_erase_column(*grid_tab,
"Residual");
1213 }
while (n_clipped > 0);
1234 double percentage = 100.0 * ((double) total_clipped)
1235 / (total_clipped + cpl_table_get_nrow(*grid_tab));
1239 "%" CPL_SIZE_FORMAT
" of %" CPL_SIZE_FORMAT
" points (%.2f %%) were rejected in "
1240 "kappa-sigma clipping. RMS = %.2f ADU", total_clipped, cpl_table_get_nrow(*grid_tab) + total_clipped, percentage, sqrt(mse));
1247 "Error generating background polynomial");
void xsh_grid_add(xsh_grid *grid, int x, int y, double data, double errs, int qual)
add a point to a grid
xsh_grid * xsh_grid_create(int size)
Create a grid.
void xsh_grid_sort(xsh_grid *grid)
sort grid points
int xsh_grid_get_index(xsh_grid *grid)
get the number of elements in the grid
void xsh_grid_free(xsh_grid **grid)
Free a grid.
cpl_table * xsh_grid2table(xsh_grid *grid)
Dump main info about a grid.
void xsh_order_list_set_bin_y(xsh_order_list *list, int bin)
Set the bin of image in y.
void xsh_order_list_set_bin_x(xsh_order_list *list, int bin)
Set the bin of image in x.
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
double xsh_order_list_eval(xsh_order_list *list, cpl_polynomial *poly, double y)
Evaluate an order list poly.
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
void xsh_pre_multiply_scalar(const xsh_pre *pre, double x)
multiply a frame in PRE format by a scalar
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
void xsh_pre_subtract_scalar(const xsh_pre *pre, double x)
subtract a scalar from a frame in PRE format
double xsh_pre_data_window_median_flux_pa(xsh_pre *pre, int x, int y, int size_x, int size_y, double *tab, int *status)
compute median sample value on the data window with preallocated memory tab. Only good pixels codes a...
void xsh_pre_subtract(xsh_pre *self, const xsh_pre *right)
Subtract one PRE image from another The data units are subtracted data = data1 - data2 The error imag...
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
#define XSH_ASSURE_NOT_ILLEGAL(cond)
#define assure(CONDITION, ERROR_CODE,...)
#define check_msg(COMMAND,...)
#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.
int xsh_instrument_get_binx(xsh_instrument *instrument)
int xsh_instrument_get_biny(xsh_instrument *instrument)
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
#define xsh_msg_dbg_medium(...)
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_low(...)
#define xsh_msg_dbg_high(...)
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
double xsh_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
cpl_error_code xsh_pfits_set_wcs(cpl_propertylist *header, const double crpix1, const double crval1, const double cdelt1, const double crpix2, const double crval2, const double cdelt2)
int xsh_bkg_yskip_lo_uvb[XSH_ORDERS_UVB]
int xsh_bkg_yskip_up_vis[XSH_ORDERS_VIS]
cpl_frame * xsh_subtract_background(cpl_frame *frame, cpl_frame *edges_order_tab_frame, xsh_background_param *background_par, xsh_instrument *instr, const char *prefix, cpl_frame **grid_frame, cpl_frame **backg_frame, const int save_bkg, const int save_grid, const int save_sub_bkg)
Subtract the inter-order background from PRE frame.
static cpl_image * xsh_background_poly(xsh_pre *image, cpl_table **grid_tbl, xsh_background_param *back_par)
cpl_frameset * xsh_subtract_nir_on_off(cpl_frameset *on, cpl_frameset *off, xsh_instrument *instr)
(NIR only) subtract the OFF set of files from the On set of files
cpl_frame * xsh_subtract_bias(cpl_frame *frame, cpl_frame *bias, xsh_instrument *instr, const char *type, const int pre_overscan_corr, const int save_tmp)
Subtract the master bias frame from PRE frame.
static void xsh_fill_bkg_mask_range(const int y, const int nx, const int xmin, const int xmax, const int x_hbox, const int decode_bp, int *qual, int *pmask, int *size)
int xsh_bkg_yskip_up_uvb[XSH_ORDERS_UVB]
static xsh_grid * xsh_crea_grid_from_mask(xsh_pre *pre, xsh_order_list *orderlist, const int x_hbox, xsh_instrument *instr, const char *prefix)
Generates sampling grid using sampling boxes distributed along inter-order background regions.
cpl_frame * xsh_subtract_dark(cpl_frame *frame, cpl_frame *dark, const char *filename, xsh_instrument *instr)
subtract the master dark frame from PRE frame
int xsh_bkg_yskip_lo_vis[XSH_ORDERS_VIS]
static cpl_image * xsh_image_generate_background(const int sx, const int sy, const polynomial *background_pol)
Subtract the previously defined background.
int xsh_bkg_yskip_lo_nir[XSH_ORDERS_NIR]
int xsh_bkg_yskip_up_nir[XSH_ORDERS_NIR]
void xsh_polynomial_delete(polynomial **p)
Delete a polynomial.
double xsh_polynomial_evaluate_2d(const polynomial *p, double x1, double x2)
Evaluate a 2d polynomial.
polynomial * xsh_polynomial_regression_2d(cpl_table *t, const char *X1, const char *X2, const char *Y, const char *sigmaY, int degree1, int degree2, const char *polynomial_fit, const char *residual_square, const char *variance_fit, double *mse, double *red_chisq, polynomial **variance, double kappa, double min_reject)
Fit a 2d polynomial to three table columns.
int xsh_select_table_rows(cpl_table *t, const char *column, cpl_table_select_operator operator, double value)
Select table rows.
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
cpl_propertylist * data_header
#define XSH_NAME_PREFIX_LAMP_MODE_ARM(name, prefix, id, ext, instr)
#define XSH_PRE_DATA_TYPE
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
#define XSH_FREE(POINTER)
#define XSH_MALLOC(POINTER, TYPE, SIZE)
#define XSH_NEW_FRAMESET(POINTER)