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
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033 #include "omega_recipe.h"
00034
00035 #include "omega_coadd.h"
00036 #include "omega_catalog.h"
00037 #include "omega_background.h"
00038 #include "omega_cosmic.h"
00039 #include "omega_photometry.h"
00040 #include "omega_science.h"
00041 #include "omega_satellites.h"
00042 #include "omega_wcscor.h"
00091
00092
00093
00094
00095 static int omega_science_create(cpl_plugin *) ;
00096 static int omega_science_exec(cpl_plugin *) ;
00097 static int omega_science_destroy(cpl_plugin *) ;
00098 static int omega_science(cpl_frameset *,cpl_parameterlist *);
00099
00100
00101
00102
00103
00104 static int omega_science_retrieve_input_param(const cpl_parameterlist *parlist);
00105 static int omega_science_load_calib();
00106 static void omega_science_init(void);
00107 static void omega_science_tidy(int level);
00108 static cpl_error_code omega_correct_crpix_union(cpl_propertylist *xlist,
00109 cpl_bivector *offsets);
00110
00111
00117 static struct {
00118 int extnum;
00119 int oc;
00120 int sub_back;
00121 int mode;
00122
00123 }omega_science_config;
00124
00130 static struct {
00131 int nmatches;
00132 int nstars;
00133 double mratio;
00134 float ZP;
00135 float ZPerr;
00136
00137
00138 double Seeing;
00139
00140 }omega_sci_qc;
00141
00142
00143
00144
00145 static struct {
00146
00147
00148 const cpl_frame *bpmfr;
00149 const cpl_frame *fringesfr;
00150 const cpl_frame *illumfr;
00151 const cpl_frame *mbiasfr;
00152 const cpl_frame *mflatfr;
00153 const cpl_frame *nskyfr;
00154 const cpl_frame *usnoa2;
00155 const cpl_frame *photomfr;
00156
00157 cpl_size *labels;
00158 cpl_frameset *scilist;
00159 cpl_propertylist *ph;
00160 cpl_propertylist *eh;
00161 omega_fits *scifits;
00162 cpl_image *mbias;
00163 cpl_image *mflat;
00164 cpl_image *bpm;
00165 cpl_image *illum;
00166 cpl_image *fringes;
00167 cpl_image *nsky;
00168 cpl_table *photom;
00169
00170
00171 char *proname;
00172 char *confname;
00173 char *catname;
00174 omega_fits *sfits;
00175 cpl_image **combined;
00176 cpl_table *catalogue;
00177 cpl_propertylist *qclist;
00178 cpl_propertylist *plist;
00179 }ps;
00180
00181
00182
00183 #define RECIPE "omega_science"
00184
00185
00186
00194
00195 int cpl_plugin_get_info(cpl_pluginlist * list)
00196 {
00197 cpl_recipe * recipe = cpl_calloc(1, sizeof(*recipe)) ;
00198 cpl_plugin * plugin = &recipe->interface ;
00199
00200 cpl_plugin_init(plugin,
00201 CPL_PLUGIN_API,
00202 OMEGA_BINARY_VERSION,
00203 CPL_PLUGIN_TYPE_RECIPE,
00204 "omega_science",
00205 "OMEGA - This recipe is used to reduce science observations.",
00206 "The recipe assumes that the data is for a single CCD. \n"
00207 "The following operations are performed: \n\n"
00208 " 1. Trim and overscan correct; \n"
00209 " 2. Subtract the bias and divide by the flat field; \n"
00210 " 3. Correct for fringing, illumination and the background, if requested; \n"
00211 " 4. Extract the zero point from the data and calculate the extinction \n"
00212 " value and save both in the header and in one table. \n\n"
00213 " ------------------------------------------------------------------------ \n\n"
00214 " Mandatory inputs : \n\n"
00215 " : a raw standard star frame \n"
00216 " : a master bias frame (Calfile 541) \n"
00217 " : a master flat field (Calfile 546) \n\n"
00218 " : a master USNOA2 table \n"
00219 " : a standard stars catalog table \n\n"
00220 " Optional inputs : \n\n"
00221 " : fringe map (Calfile 545) \n"
00222 " : bad pixels map (Calfile 522+535) \n"
00223 " : illumination correction frame (Calfile 548) \n\n"
00224 " The recipe will save the extinction and zero point values in one table.",
00225 "Sandra Castro / Armin Gabasch",
00226 "scastro@eso.org",
00227 omega_get_license(),
00228 omega_science_create,
00229 omega_science_exec,
00230 omega_science_destroy) ;
00231
00232 cpl_pluginlist_append(list, plugin) ;
00233
00234 return 0;
00235 }
00236
00237
00246
00247 static int omega_science_create(cpl_plugin * plugin)
00248 {
00249 cpl_recipe * recipe;
00250 cpl_parameter * p ;
00251 char *path = NULL;
00252
00253
00254 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00255 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00256 cpl_func, __LINE__, cpl_error_get_where());
00257 return (int)cpl_error_get_code();
00258 }
00259
00260 if (plugin == NULL) {
00261 cpl_msg_error(cpl_func, "Null plugin");
00262 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00263 }
00264
00265
00266 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00267 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00268 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00269 }
00270
00271
00272 recipe = (cpl_recipe *)plugin;
00273
00274
00275 recipe->parameters = cpl_parameterlist_new() ;
00276 if (recipe->parameters == NULL) {
00277 cpl_msg_error(cpl_func, "Parameter list allocation failed");
00278 cpl_ensure_code(0, (int)CPL_ERROR_ILLEGAL_OUTPUT);
00279 }
00280
00281
00282
00283
00284 p = cpl_parameter_new_value("omega.omega_science.ExtensionNumber",
00285 CPL_TYPE_INT,
00286 "FITS extension number to load (1 to 32). (-1 == all)",
00287 "omega_science",
00288 -1) ;
00289
00290 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"ext") ;
00291 cpl_parameterlist_append(recipe->parameters, p) ;
00292
00293 p = cpl_parameter_new_range("omega.omega_science.OverscanMethod",
00294 CPL_TYPE_INT,
00295 "Overscan Correction Method",
00296 "overscan",
00297 0, 0, 6);
00298 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"oc-meth") ;
00299 cpl_parameterlist_append(recipe->parameters, p) ;
00300
00301
00302
00303 p = cpl_parameter_new_value("omega.omega_science.LowThreSatuPixel",
00304 CPL_TYPE_DOUBLE,
00305 "Low threshold for calculating the saturated pixels map.",
00306 "omega_science",
00307 -500.0) ;
00308
00309 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "lt-satu") ;
00310 cpl_parameterlist_append(recipe->parameters, p) ;
00311
00312 p = cpl_parameter_new_value("omega.omega_science.HighThreSatuPixel",
00313 CPL_TYPE_DOUBLE,
00314 "High threshold for calculating the saturated pixels map.",
00315 "omega_science",
00316 60000.0) ;
00317
00318 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "ht-satu") ;
00319 cpl_parameterlist_append(recipe->parameters, p) ;
00320
00321
00322 p = cpl_parameter_new_value("omega.omega_science.DetectionThreSatellite",
00323 CPL_TYPE_DOUBLE,
00324 "Minimum SNR for pixels to contribute to Hough map.",
00325 "omega.Hough",
00326 4.0) ;
00327
00328 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "det-sate") ;
00329 cpl_parameterlist_append(recipe->parameters, p) ;
00330
00331 p = cpl_parameter_new_value("omega.omega_science.HoughThreshold",
00332 CPL_TYPE_DOUBLE,
00333 "Threshold for satellite tracks in Hough image.",
00334 "omega.Hough",
00335 400.0) ;
00336
00337 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "hough-thre") ;
00338 cpl_parameterlist_append(recipe->parameters, p) ;
00339
00340
00341
00342 p = cpl_parameter_new_value("omega.omega_science.SigmaFringeScaling",
00343 CPL_TYPE_DOUBLE,
00344 "Sigma threshold in image data for scaling estimate (fringes only)",
00345 "omega_science",
00346 5.0) ;
00347
00348 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sigma-fringes") ;
00349 cpl_parameterlist_append(recipe->parameters, p) ;
00350
00351 p = cpl_parameter_new_value("omega.omega_science.LowThreFringe",
00352 CPL_TYPE_DOUBLE,
00353 "Lower bound of fringes to include in scaling (fringes only)",
00354 "omega_science",
00355 1.5) ;
00356
00357 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "lfringes") ;
00358 cpl_parameterlist_append(recipe->parameters, p) ;
00359
00360 p = cpl_parameter_new_value("omega.omega_science.HighThreFringe",
00361 CPL_TYPE_DOUBLE,
00362 "Higher bound of fringes to include in scaling (fringes only)",
00363 "omega_science",
00364 5.0) ;
00365
00366 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "hfringes") ;
00367 cpl_parameterlist_append(recipe->parameters, p) ;
00368
00369
00370
00371 p = cpl_parameter_new_value("omega.omega_science.SubtractBackground",
00372 CPL_TYPE_BOOL,
00373 "Boolean value to subtract background or not. 1(TRUE), 0(FALSE)",
00374 "omega_science",
00375 1) ;
00376
00377 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sub-back") ;
00378 cpl_parameterlist_append(recipe->parameters, p) ;
00379
00380 p = cpl_parameter_new_value("omega.omega_science.BackThreshold",
00381 CPL_TYPE_DOUBLE,
00382 "Detection threshold for background in image (Sextractor DETECT_THRESH)",
00383 "omega_science",
00384 1000.) ;
00385
00386 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "backthre") ;
00387 cpl_parameterlist_append(recipe->parameters, p) ;
00388
00389
00390
00391 path = cpl_sprintf("%s", OMEGA_BIN_PATH);
00392 p = cpl_parameter_new_value("omega.omega_science.BinPath",
00393 CPL_TYPE_STRING,
00394 "Path to any external executable program.",
00395 "omega.BinPath",
00396 path);
00397
00398 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "bin-path");
00399 cpl_parameterlist_append(recipe->parameters, p);
00400 cpl_free(path);
00401
00402
00403 path = cpl_sprintf("%s/omega.sex", OMEGA_CONFIG_PATH);
00404 p = cpl_parameter_new_value("omega.omega_science.SexConfig",
00405 CPL_TYPE_STRING,
00406 "Path to Sextractor config file.",
00407 "omega.sextractor",
00408 path);
00409
00410 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-config");
00411 cpl_parameterlist_append(recipe->parameters, p);
00412 cpl_free(path);
00413
00414
00415 path = cpl_sprintf("%s/omega.conv", OMEGA_CONFIG_PATH);
00416 p = cpl_parameter_new_value("omega.omega_science.SexConv",
00417 CPL_TYPE_STRING,
00418 "Path to Sextractor convolution mask file.",
00419 "omega.sextractor",
00420 path);
00421
00422 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-conv");
00423 cpl_parameterlist_append(recipe->parameters, p);
00424 cpl_free(path);
00425
00426
00427 path = cpl_sprintf("%s/omega.param", OMEGA_CONFIG_PATH);
00428 p = cpl_parameter_new_value("omega.omega_science.SexParam",
00429 CPL_TYPE_STRING,
00430 "Path to Sextractor parameters file.",
00431 "omega.sextractor",
00432 path);
00433
00434 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-param");
00435 cpl_parameterlist_append(recipe->parameters, p);
00436 cpl_free(path);
00437
00438 path = cpl_sprintf("%s/omega.nnw", OMEGA_CONFIG_PATH);
00439 p = cpl_parameter_new_value("omega.omega_science.SexNnw",
00440 CPL_TYPE_STRING,
00441 "Path to Sextractor neural network config file.",
00442 "omega.sextractor",
00443 path);
00444
00445 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-nnw");
00446 cpl_parameterlist_append(recipe->parameters, p);
00447 cpl_free(path);
00448
00449 p = cpl_parameter_new_value("omega.omega_science.SexDetThreshold",
00450 CPL_TYPE_DOUBLE,
00451 "Detection threshold for Sextractor sources.",
00452 "omega.sextractor",
00453 1.5);
00454
00455 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-dthre");
00456 cpl_parameterlist_append(recipe->parameters, p);
00457
00458
00459
00460 path = cpl_sprintf("%s/omega.cosmic.sex", OMEGA_CONFIG_PATH);
00461 p = cpl_parameter_new_value("omega.omega_science.SexCosmic",
00462 CPL_TYPE_STRING,
00463 "Path to Sextractor cosmic detection mode config file.",
00464 "omega.Sextractor",
00465 path);
00466
00467 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-cosmic");
00468 cpl_parameterlist_append(recipe->parameters, p);
00469 cpl_free(path);
00470
00471 path = cpl_sprintf("%s/omega.cosmic.param", OMEGA_CONFIG_PATH);
00472 p = cpl_parameter_new_value("omega.omega_science.SexCosmicParam",
00473 CPL_TYPE_STRING,
00474 "Path to Sextractor cosmic parameters file.",
00475 "omega.Sextractor",
00476 path);
00477
00478 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"sex-cosmic-param");
00479 cpl_parameterlist_append(recipe->parameters, p);
00480 cpl_free(path);
00481
00482 path = cpl_sprintf("%s/cosmic.ret", OMEGA_CONFIG_PATH);
00483 p = cpl_parameter_new_value("omega.omega_science.SexCosmicFilt",
00484 CPL_TYPE_STRING,
00485 "Path to Sextractor filter mask for cosmic detection mode.",
00486 "omega.Sextractor",
00487 path);
00488
00489 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-cosfilt");
00490 cpl_parameterlist_append(recipe->parameters, p);
00491 cpl_free(path);
00492
00493 p = cpl_parameter_new_value("omega.omega_science.SexCosmicDet",
00494 CPL_TYPE_DOUBLE,
00495 "Detection threshold for detecting cosmic rays in Sextractor",
00496 "omega.Sextractor",
00497 5.0) ;
00498
00499 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "sex-cosmic-det") ;
00500 cpl_parameterlist_append(recipe->parameters, p) ;
00501
00502
00503
00504 p = cpl_parameter_new_value("omega.omega_science.AstromDetThre",
00505 CPL_TYPE_DOUBLE,
00506 "Sextractor threshold to detect stars for astrometric correction.",
00507 "omega.Astrom",
00508 10.0) ;
00509
00510 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "astrom-thre") ;
00511 cpl_parameterlist_append(recipe->parameters, p) ;
00512
00513 p = cpl_parameter_new_enum("omega.omega_science.PlateSolution",
00514 CPL_TYPE_INT,
00515 "Number of constant plate fits to do for the astrometry solution.",
00516 "omega.Astrom",
00517 4,2,4,6);
00518
00519 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "platesol");
00520 cpl_parameterlist_append(recipe->parameters, p);
00521
00522 p = cpl_parameter_new_value("omega.omega_science.NumIterations",
00523 CPL_TYPE_INT,
00524 "Number of iterations for the plate solution fitting.",
00525 "omega.Astrom",
00526 3);
00527
00528 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "niter");
00529 cpl_parameterlist_append(recipe->parameters, p);
00530
00531
00532 p = cpl_parameter_new_range("omega.omega_science.SextractorFlagLimit",
00533 CPL_TYPE_INT,
00534 "Sextractor FLAG upper limit to preselect good sources for the "
00535 "astrometric correction computation.", "omega.Astrom",
00536 256, 0, 256);
00537 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"SextractorFlagLimit") ;
00538 cpl_parameterlist_append(recipe->parameters, p) ;
00539
00540 p = cpl_parameter_new_range("omega.omega_science.SextractorClassStar",
00541 CPL_TYPE_DOUBLE,
00542 "Sextractor CLASS_STAR lower limit to preselect starlike objects "
00543 "for the astrometric correction computation.", "omega.Astrom",
00544 0.0, 0.0, 1.0);
00545 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,"SextractorClassStar") ;
00546 cpl_parameterlist_append(recipe->parameters, p) ;
00547
00548
00549
00550
00551 p = cpl_parameter_new_value("omega.omega_science.PhotomDetThre",
00552 CPL_TYPE_DOUBLE,
00553 "Sextractor threshold to detect stars for photometric correction.",
00554 "omega.Photom",
00555 10.0) ;
00556
00557 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "photom-thre") ;
00558 cpl_parameterlist_append(recipe->parameters, p) ;
00559
00560 p = cpl_parameter_new_value("omega.omega_science.MatchPhotomRadius",
00561 CPL_TYPE_DOUBLE,
00562 "Radius within which to match photometric stds and stars",
00563 "omega.Photom",
00564 10.0) ;
00565
00566 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "radius") ;
00567 cpl_parameterlist_append(recipe->parameters, p) ;
00568
00569 p = cpl_parameter_new_value("omega.omega_science.PhotomAperture",
00570 CPL_TYPE_DOUBLE,
00571 "Aperture in pixels for photometric measurements in Sextractor",
00572 "omega.Photom",
00573 30.0) ;
00574
00575 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "aperture") ;
00576 cpl_parameterlist_append(recipe->parameters, p) ;
00577
00578
00579 p = cpl_parameter_new_value("omega.omega_science.Nstars",
00580 CPL_TYPE_INT,
00581 "Number of stars in image to use in "
00582 "pattern matching",
00583 "omega.Pattern",
00584 25);
00585
00586 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "nstars");
00587 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00588 cpl_parameterlist_append(recipe->parameters, p);
00589
00590
00591 p = cpl_parameter_new_value("omega.omega_science.Npattern",
00592 CPL_TYPE_INT,
00593 "Number of catalogue sources to use in "
00594 "pattern matching",
00595 "omega.Pattern",
00596 15);
00597
00598 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "npattern");
00599 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00600 cpl_parameterlist_append(recipe->parameters, p);
00601
00602
00603 p = cpl_parameter_new_value("omega.omega_science.Tolerance",
00604 CPL_TYPE_DOUBLE,
00605 "Max relative difference of angles and scales from their "
00606 "median value for match acceptance (tolerance)",
00607 "omega.Pattern",
00608 0.1);
00609
00610 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "tol");
00611 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00612 cpl_parameterlist_append(recipe->parameters, p);
00613
00614 p = cpl_parameter_new_value("omega.omega_science.SearchPatternRadius",
00615 CPL_TYPE_DOUBLE,
00616 "Search radius for full PPM (pixels)",
00617 "omega.Pattern",
00618 10.0);
00619 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "pradius");
00620 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00621 cpl_parameterlist_append(recipe->parameters, p);
00622
00623 p = cpl_parameter_new_value("omega.omega_science.MaxPatternRadius",
00624 CPL_TYPE_DOUBLE,
00625 "Maximum search radius for full PPM (pixels)",
00626 "omega.Pattern",
00627 40.0);
00628 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "maxpradius");
00629 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00630 cpl_parameterlist_append(recipe->parameters, p);
00631
00632
00633 p = cpl_parameter_new_value("omega.omega_science.xcorr",
00634 CPL_TYPE_STRING,
00635 "String of four values meaning: x,y search area and "
00636 "x,y measurement area, needed for cross-correlation "
00637 "when stacking images",
00638 "omega.Stack",
00639 "20,20,20,20");
00640
00641 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "xcorr");
00642 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00643 cpl_parameterlist_append(recipe->parameters, p);
00644
00645 p = cpl_parameter_new_value("omega.omega_science.CombMode",
00646 CPL_TYPE_STRING,
00647 "Combination mode to use when stacking images. "
00648 "union,intersect or first",
00649 "omega.Stack",
00650 "union");
00651
00652 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "comb-mode");
00653 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00654 cpl_parameterlist_append(recipe->parameters, p);
00655
00656 p = cpl_parameter_new_value("omega.omega_science.StackRej",
00657 CPL_TYPE_STRING,
00658 "Number of low and high values to reject when stacking",
00659 "omega.Stack",
00660 "0,0");
00661
00662 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "stack-rej");
00663 cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00664 cpl_parameterlist_append(recipe->parameters, p);
00665
00666
00667 p = cpl_parameter_new_value("omega.omega_science.CatThre",
00668 CPL_TYPE_DOUBLE,
00669 "Sextractor threshold to detect sources in stack image",
00670 "omega.Sextractor",
00671 3.0) ;
00672
00673 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "cat-thre") ;
00674 cpl_parameterlist_append(recipe->parameters, p) ;
00675
00676
00677
00678 p = cpl_parameter_new_value("omega.omega_science.mag_zpt_no_std_table",
00679 CPL_TYPE_DOUBLE,
00680 "Zeropoint used if the standard star zeropoint table is empty ",
00681 "omega.Sextractor",
00682 0.0) ;
00683 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "mag_zpt_no_std_table") ;
00684 cpl_parameterlist_append(recipe->parameters, p) ;
00685
00686
00687
00688 p = cpl_parameter_new_value("omega.omega_science.mag_zpt_err_no_std_table",
00689 CPL_TYPE_DOUBLE,
00690 "Zeropoint error used if the standard star zeropoint table is empty ",
00691 "omega.Sextractor",
00692 1.0) ;
00693 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "mag_zpt_err_no_std_table") ;
00694 cpl_parameterlist_append(recipe->parameters, p) ;
00695
00696
00697
00698 p = cpl_parameter_new_value("omega.omega_science.ext_no_std_table",
00699 CPL_TYPE_DOUBLE,
00700 "extinction used if the standard star zeropoint table is empty",
00701 "omega.Sextractor",
00702 1.0) ;
00703 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI, "ext_no_std_table") ;
00704 cpl_parameterlist_append(recipe->parameters, p) ;
00705
00706
00707
00708 return 0;
00709 }
00710
00711
00717
00718 static int omega_science_exec(cpl_plugin * plugin)
00719 {
00720 cpl_recipe * recipe;
00721 int recipe_status;
00722
00723
00724 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00725 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
00726 cpl_func, __LINE__, cpl_error_get_where());
00727 return (int)cpl_error_get_code();
00728 }
00729
00730 if (plugin == NULL) {
00731 cpl_msg_error(cpl_func, "Null plugin");
00732 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00733 }
00734
00735
00736 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00737 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00738 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00739 }
00740
00741
00742 recipe = (cpl_recipe *)plugin;
00743
00744
00745 if (recipe->parameters == NULL) {
00746 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
00747 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00748 }
00749 if (recipe->frames == NULL) {
00750 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
00751 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00752 }
00753
00754
00755 recipe_status = omega_science(recipe->frames, recipe->parameters);
00756
00757
00758 if (cpl_dfs_update_product_header(recipe->frames)) {
00759 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
00760 }
00761
00762 return recipe_status;
00763
00764 }
00765
00766
00772
00773 static int omega_science_destroy(cpl_plugin * plugin)
00774 {
00775 cpl_recipe * recipe;
00776
00777 if (plugin == NULL) {
00778 cpl_msg_error(cpl_func, "Null plugin");
00779 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
00780 }
00781
00782
00783 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
00784 cpl_msg_error(cpl_func, "Plugin is not a recipe");
00785 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
00786 }
00787
00788
00789 recipe = (cpl_recipe *)plugin;
00790
00791 cpl_parameterlist_delete(recipe->parameters) ;
00792
00793 return 0 ;
00794 }
00795
00796
00806
00807 static int omega_science(cpl_frameset *set, cpl_parameterlist *pars)
00808 {
00809 int j,jst,jfn,i,n_ext;
00810 cpl_size nlab;
00811 int nraw = 0;
00812 int refine = 0;
00813 static int isfirst = 0;
00814 cpl_size *plabels = NULL;
00815 int has_fringes = 0;
00816 double pixscalex = 0.0;
00817 double pixscaley = 0.0;
00818 double Seeing = 0.0;
00819 double ellipticity = 0.0;
00820 double zeropoint_final=0.0;
00821 const char *smoothed = "omega_stacksmooth.fits";
00822 const char *smoothed_weight = "omega_stacksmooth_weight.fits";
00823 const char *tag = NULL;
00824 const char *tag_mask = NULL;
00825 const char * maskname= NULL;
00826
00827 cpl_image *smooth;
00828 cpl_image *image_simple_mask=NULL;
00829 cpl_frame *sciframe;
00830 cpl_frame *firstframe=NULL;
00831 cpl_frame *product_frame=NULL ;
00832 cpl_frame *product_frame_mask=NULL ;
00833 cpl_frame *conf_frame;
00834 cpl_frame *catframe;
00835 cpl_frameset *proset=NULL;
00836 cpl_frameset *proset_mask=NULL;
00837 cpl_bivector *offsets=NULL;
00838 cpl_propertylist *plist, *xlist, *alist;
00839 cpl_propertylist *plist_dummy=NULL;
00840 cpl_propertylist *flist;
00841 cpl_propertylist *qclist=NULL;
00842 cpl_parameter *par = NULL;
00843 const char *sval = NULL;
00844
00845
00846
00847
00848 if (pars == NULL) {
00849 cpl_msg_error (cpl_func, "Parameters list not found");
00850 return -1;
00851 }
00852
00853 if (cpl_frameset_is_empty(set) == 1) {
00854 cpl_msg_error (cpl_func, "Frameset not found");
00855 return -1;
00856 }
00857
00858
00859 if(omega_science_retrieve_input_param(pars) != 0){
00860 cpl_msg_error(cpl_func,"Cannot retrieve input parameters. %s", cpl_error_get_message());
00861 return -1;
00862 }
00863
00864
00865 if (oc_dfs_set_groups(set)) {
00866 cpl_msg_error(cpl_func, "Cannot identify RAW and CALIB frames") ;
00867 return -1 ;
00868 }
00869
00870
00871 omega_science_init();
00872 omega_sci_qc.Seeing = 0.0;
00873 omega_sci_qc.mratio = 0.0;
00874 omega_sci_qc.nmatches = 0;
00875 omega_sci_qc.nstars = 0;
00876
00877
00878 if ((ps.labels = cpl_frameset_labelise(set,omega_compare_tags,
00879 &nlab)) == NULL) {
00880 cpl_msg_error(cpl_func,"Cannot labelise the input frameset");
00881 omega_science_tidy(0);
00882 return -1;
00883 }
00884
00885 omega_science_config.mode = 0;
00886
00887 if ((ps.scilist = omega_frameset_subgroup(set,ps.labels,nlab,
00888 STARE_RAW)) != NULL) {
00889 omega_science_config.mode = 1;
00890 refine = 0;
00891 tag = STARE_RAW;
00892 }
00893 else if ((ps.scilist = omega_frameset_subgroup(set,ps.labels,nlab,
00894 JITTER_RAW)) != NULL) {
00895 omega_science_config.mode = 2;
00896 refine = 0;
00897 tag = JITTER_RAW;
00898 }
00899 else if ((ps.scilist = omega_frameset_subgroup(set,ps.labels,nlab,
00900 DITHER_RAW)) != NULL) {
00901 omega_science_config.mode = 3;
00902 refine = 0;
00903 tag = DITHER_RAW;
00904 }
00905 else if ((ps.scilist = omega_frameset_subgroup(set,ps.labels,nlab,
00906 OFFSET_RAW)) != NULL) {
00907 omega_science_config.mode = 4;
00908 refine = 0;
00909 tag = OFFSET_RAW;
00910 }
00911 else {
00912 cpl_msg_error(cpl_func,"Cannot find science frames in input frameset");
00913 omega_science_tidy(0);
00914 return -1;
00915 }
00916
00917 nraw = cpl_frameset_get_size(ps.scilist);
00918 cpl_msg_info(cpl_func,"There are %d %s frames in frame set",nraw,tag);
00919
00920
00921
00922 ps.mbiasfr = cpl_frameset_find_const(set, OMEGA_CALIB_BIAS);
00923 if (ps.mbiasfr == NULL) {
00924 cpl_msg_error(cpl_func,"Cannot find %s frame in frame set", OMEGA_CALIB_BIAS);
00925 omega_science_tidy(0);
00926 }
00927 cpl_msg_info(cpl_func,"Using %s %s",OMEGA_CALIB_BIAS, cpl_frame_get_filename(ps.mbiasfr));
00928
00929
00930 ps.mflatfr = cpl_frameset_find_const(set, OMEGA_CALIB_FLAT);
00931 if(ps.mflatfr == NULL) {
00932 cpl_msg_error(cpl_func,"Cannot find %s frame in frame set", OMEGA_CALIB_FLAT);
00933 omega_science_tidy(0);
00934 return -1;
00935 }
00936 cpl_msg_info(cpl_func,"Using %s %s", OMEGA_CALIB_FLAT, cpl_frame_get_filename(ps.mflatfr));
00937
00938
00939 ps.photomfr = cpl_frameset_find (set,OMEGA_CALIB_ZP);
00940 if(ps.photomfr == NULL) {
00941 cpl_msg_warning(cpl_func,"Cannot find %s in frame set", OMEGA_CALIB_ZP);
00942
00943
00944 }
00945 else{
00946 cpl_msg_info(cpl_func,"Using %s %s",OMEGA_CALIB_ZP,cpl_frame_get_filename(ps.photomfr));
00947 }
00948
00949 ps.usnoa2 = cpl_frameset_find (set, OMEGA_USNOA2);
00950 if (ps.usnoa2 == NULL) {
00951 cpl_msg_error(cpl_func,"Cannot find %s frame in frame set",OMEGA_USNOA2);
00952 omega_science_tidy(0);
00953 return -1;
00954 }
00955 cpl_msg_info(cpl_func,"Using %s %s", OMEGA_USNOA2, cpl_frame_get_filename(ps.usnoa2));
00956
00957
00958 ps.bpmfr = cpl_frameset_find (set,OMEGA_CALIB_BPM);
00959 if(ps.bpmfr == NULL) {
00960 cpl_msg_error(cpl_func,"Cannot find %s in frame set", OMEGA_CALIB_BPM);
00961 omega_science_tidy(0);
00962 return -1;
00963 }
00964 cpl_msg_info(cpl_func,"Using %s %s",OMEGA_CALIB_BPM,cpl_frame_get_filename(ps.bpmfr));
00965
00966
00967
00968 ps.illumfr = cpl_frameset_find(set, OMEGA_CALIB_ILLUM);
00969 if(ps.illumfr != NULL)
00970 cpl_msg_info(cpl_func,"Using %s %s",OMEGA_CALIB_ILLUM,cpl_frame_get_filename(ps.illumfr));
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983 sciframe = cpl_frameset_find(set,tag);
00984
00985 omega_extensions(sciframe,omega_science_config.extnum,&jst,&jfn);
00986 if(omega_science_config.extnum == 0){
00987 cpl_msg_error(cpl_func,"Unsupported extension request, %d",omega_science_config.extnum);
00988 omega_science_tidy(0);
00989 return -1;
00990 }
00991
00992
00993
00994 for(i=0; i < nraw; i++){
00995 cpl_msg_info(cpl_func,"Working on %s",cpl_frame_get_filename(sciframe));
00996
00997
00998
00999
01000 for (j = jst; j <= jfn; j++) {
01001 isfirst = (j == jst);
01002
01003 cpl_msg_indent_more();
01004 cpl_msg_info(cpl_func,".....Working on extension %d.....",j);
01005 cpl_msg_indent_less();
01006
01007
01008 if(omega_science_load_calib(j) != 0){
01009 cpl_msg_error(cpl_func,"Cannot load calibration frame(s)");
01010 omega_science_tidy(0);
01011 return -1;
01012 }
01013
01014
01015 ps.scifits = omega_fits_load(sciframe, CPL_TYPE_FLOAT, j);
01016
01017
01018 ps.sfits = omega_jitter_process(ps.scifits,ps.mbias,ps.mflat,ps.bpm,ps.photom,
01019 ps.usnoa2,ps.illum,ps.fringes,ps.nsky, &zeropoint_final ,pars, j);
01020
01021 if(ps.sfits == NULL){
01022 cpl_msg_error(cpl_func,"Unable to reduce image");
01023 omega_science_tidy(0);
01024 return -1;
01025 }
01026
01027
01028 omega_science_tidy(1);
01029
01030
01031
01032 if(isfirst){
01033 tag = NULL;
01034 if(omega_science_config.mode == 1) {
01035 ps.proname = cpl_sprintf("%s_%s_%d.fits", INSTRUME,SIMPLE_STARE,i);
01036 product_frame = omega_product_frame(ps.proname, SIMPLE_STARE, CPL_FRAME_TYPE_IMAGE);
01037 tag = SIMPLE_STARE;
01038 }
01039 if(omega_science_config.mode == 2){
01040 ps.proname = cpl_sprintf("%s_%s_%d.fits", INSTRUME,SIMPLE_JITTER,i);
01041 product_frame = omega_product_frame(ps.proname, SIMPLE_JITTER, CPL_FRAME_TYPE_IMAGE);
01042 tag = SIMPLE_JITTER;
01043 }
01044 if(omega_science_config.mode == 3){
01045 ps.proname = cpl_sprintf("%s_%s_%d.fits", INSTRUME,SIMPLE_DITHER,i);
01046 product_frame = omega_product_frame(ps.proname, SIMPLE_DITHER, CPL_FRAME_TYPE_IMAGE);
01047 tag = SIMPLE_DITHER;
01048 }
01049 if(omega_science_config.mode == 4){
01050 ps.proname = cpl_sprintf("%s_%s_%d.fits", INSTRUME,SIMPLE_OFFSET,i);
01051 product_frame = omega_product_frame(ps.proname, SIMPLE_OFFSET, CPL_FRAME_TYPE_IMAGE);
01052 tag = SIMPLE_OFFSET;
01053 }
01054 maskname=cpl_sprintf("%s_%s_%d.fits", INSTRUME,SIMPLE_MASK,i);
01055 product_frame_mask = omega_product_frame(maskname, SIMPLE_MASK, CPL_FRAME_TYPE_IMAGE);
01056 tag_mask=SIMPLE_MASK;
01057 }
01058
01059 cpl_msg_info(cpl_func,"Saving SIMPLE image");
01060 if(omega_save_fits(ps.sfits,set,pars,NULL,CPL_BPP_IEEE_FLOAT,ps.proname,RECIPE,
01061 product_frame,sciframe,isfirst) == -1){
01062 cpl_msg_error(cpl_func,"Cannot save SIMPLE image. %s",cpl_error_get_message());
01063 cpl_free(maskname);
01064 omega_science_tidy(0);
01065 return -1;
01066 }
01067
01068
01069 image_simple_mask=cpl_image_new_from_mask(cpl_image_get_bpm(ps.sfits->image));
01070 cpl_image_delete(ps.sfits->image); ps.sfits->image=NULL;
01071 ps.sfits->image=image_simple_mask;
01072
01073 cpl_msg_info(cpl_func,"Saving SIMPLE MASK file");
01074 if(omega_save_fits(ps.sfits,set,pars,NULL,CPL_BPP_8_UNSIGNED,maskname,RECIPE,
01075 product_frame_mask,sciframe,isfirst) == -1){
01076 cpl_msg_error(cpl_func,"Cannot save SIMPLE image. %s",cpl_error_get_message());
01077 cpl_free(maskname);
01078 omega_science_tidy(0);
01079 return -1;
01080 }
01081
01082
01083 freefits(ps.sfits);
01084 }
01085
01086 sciframe = cpl_frameset_find(set, NULL);
01087
01088 freespace(ps.proname);
01089 cpl_free(maskname);
01090 product_frame = NULL;
01091 product_frame_mask= NULL;
01092 }
01093
01094
01095 if ((plabels = cpl_frameset_labelise(set,omega_compare_tags,
01096 &nlab)) == NULL) {
01097 cpl_msg_error(cpl_func,"Cannot labelise the output frameset");
01098 omega_science_tidy(0);
01099 return -1;
01100 }
01101
01102
01103 if ((proset = omega_frameset_subgroup(set,plabels,nlab,
01104 tag)) == NULL) {
01105 cpl_msg_error(cpl_func,"Cannot create simple frameset");
01106 freespace(plabels);
01107 omega_science_tidy(0);
01108 return -1;
01109 }
01110
01111
01112 if ((proset_mask = omega_frameset_subgroup(set,plabels,nlab,
01113 tag_mask)) == NULL) {
01114 cpl_msg_error(cpl_func,"Cannot create simple mask frameset");
01115 freespace(plabels);
01116 omega_science_tidy(0);
01117 return -1;
01118 }
01119
01120
01121 freespace(plabels);
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144 firstframe = cpl_frameset_get_first(proset);
01145 n_ext = cpl_fits_count_extensions(cpl_frame_get_filename(firstframe));
01146
01147
01148 plist = cpl_propertylist_load_regexp(cpl_frame_get_filename(firstframe),0,"ESO INS FILT",0);
01149 flist = omega_pfits_get_filter_info(plist);
01150 has_fringes = cpl_propertylist_get_int(flist, "HAS_FRINGES");
01151 freeplist(plist);
01152 freeplist(flist);
01153
01154
01155 cpl_msg_info(cpl_func,"Creating stack image and catalogue");
01156 for(j = 1; j <= n_ext; j++){
01157 isfirst = (j == 1);
01158
01159 offsets=omega_get_offsets_from_wcs(proset, firstframe, j);
01160
01161
01162 cpl_msg_indent_more();
01163 cpl_msg_info(cpl_func,"....... Image in position %d .......",j);
01164 cpl_msg_indent_less();
01165
01166 plist = cpl_propertylist_load(cpl_frame_get_filename(firstframe),j);
01167 omega_get_pixelscale(plist, &pixscalex, &pixscaley);
01168 freeplist(plist);
01169
01170
01171
01172
01173
01174 if(cpl_frameset_get_size(proset)==1){
01175 ps.combined = cpl_malloc(2*sizeof(cpl_image *));
01176 ps.combined[0]=cpl_image_load(cpl_frame_get_filename(cpl_frameset_get_first(proset)),CPL_TYPE_FLOAT,0,j);
01177 ps.combined[1]=cpl_image_load(cpl_frame_get_filename(cpl_frameset_get_first(proset_mask)),CPL_TYPE_INT,0,j);
01178 cpl_image_threshold(ps.combined[1],0.5,0.6,1,0);
01179 }
01180 else{
01181
01182 ps.combined = omega_jitter_stack(proset, proset_mask, offsets, refine, pars, j);
01183 }
01184
01185 if(ps.combined == NULL){
01186 cpl_msg_error(cpl_func,"Unable to create stack image. %s", cpl_error_get_message());
01187 freebivector(offsets);
01188 freeframeset(proset);
01189 freeframeset(proset_mask);
01190 omega_science_tidy(0);
01191 return -1;
01192 }
01193
01194
01195 if(cpl_frameset_get_size(proset) >= 3){
01196 cpl_imagelist *ilist = cpl_imagelist_load_frameset(proset, CPL_TYPE_FLOAT,1,j);
01197 if((ps.nsky = omega_nsky_create(ilist, NULL,
01198 cpl_frameset_get_size(proset))) != NULL){
01199 cpl_image_divide(ps.combined[0],ps.nsky);
01200 }
01201
01202
01203 if(has_fringes == 1){
01204 cpl_msg_info(cpl_func,"Applying fringing correction");
01205 ps.bpm = cpl_image_load(cpl_frame_get_filename(ps.bpmfr),CPL_TYPE_FLOAT,0,j);
01206 ps.fringes = cpl_image_subtract_scalar_create(ps.nsky, 1.0);
01207 if(ps.fringes != NULL)
01208 omega_fringecor(ps.combined[0],ps.fringes,ps.bpm,pars);
01209
01210 freeimage(ps.fringes);
01211 freeimage(ps.bpm);
01212 }
01213 freeilist(ilist);
01214 freeimage(ps.nsky);
01215 }
01216
01217
01218 smooth = omega_smooth_image(ps.combined[0], 1);
01219
01220 if(cpl_detector_interpolate_rejected(smooth) != CPL_ERROR_NONE)
01221 cpl_msg_debug(cpl_func,"Cannot clean rejected pixels. %s",
01222 cpl_error_get_message());
01223
01224 xlist = cpl_propertylist_load(cpl_frame_get_filename(firstframe),j);
01225
01226
01227 plist_dummy = cpl_propertylist_load_regexp(
01228 cpl_frame_get_filename(firstframe),0,"ESO INS FILT",0);
01229 cpl_propertylist_copy_property_regexp(xlist,plist_dummy,"ESO INS FILT",0);
01230 freeplist(plist_dummy);
01231
01232
01233
01234
01235
01236 par = cpl_parameterlist_find(pars, "omega.omega_science.CombMode");
01237 sval = cpl_parameter_get_string(par);
01238 if (!strcmp(sval, "union")){
01239 omega_correct_crpix_union(xlist, offsets);
01240 }
01241
01242 alist = cpl_propertylist_new();
01243 cpl_propertylist_append_string(alist, "EXTNAME",
01244 cpl_propertylist_get_string(xlist, "EXTNAME"));
01245 cpl_propertylist_set_comment(alist,"EXTNAME", "Extension name");
01246
01247
01248 cpl_propertylist_save(xlist,smoothed,CPL_IO_CREATE);
01249 cpl_image_save(smooth,smoothed,BITPIX,xlist,CPL_IO_EXTEND);
01250
01251 cpl_propertylist_save(xlist,smoothed_weight,CPL_IO_CREATE);
01252 cpl_image_save(ps.combined[1],smoothed_weight,BITPIX,xlist,CPL_IO_EXTEND);
01253
01254
01255 cpl_msg_info(cpl_func,"Refining WCS for stack image");
01256 ps.plist = omega_match_points(smoothed, smoothed_weight, ps.usnoa2, pars, j,
01257 &omega_sci_qc.nmatches, &omega_sci_qc.mratio);
01258 if(ps.plist == NULL){
01259 cpl_msg_warning(cpl_func,"Unable to refine WCS for stack image");
01260 ps.plist = cpl_propertylist_duplicate(xlist);
01261 }
01262
01263 freeimage(smooth);
01264 freeplist(xlist);
01265
01266 ps.qclist = cpl_propertylist_new();
01267 cpl_propertylist_update_int(ps.qclist, "ESO QC SCI MATCHES", omega_sci_qc.nmatches);
01268 cpl_propertylist_set_comment(ps.qclist, "ESO QC SCI MATCHES",
01269 "number matches between USNOA2 cat and stars");
01270 cpl_propertylist_update_double(ps.qclist, "ESO QC SCI RATIO MATCHES", omega_sci_qc.mratio);
01271 cpl_propertylist_set_comment(ps.qclist, "ESO QC SCI RATIO MATCHES",
01272 "ratio USNOA2 matches/detected stars");
01273
01274
01275 if(isfirst){
01276 ps.proname = cpl_sprintf("%s_%s.fits", INSTRUME,STACK_PROCATG);
01277 ps.confname = cpl_sprintf("%s_%s.fits", INSTRUME,CONF_PROCATG);
01278 product_frame = omega_product_frame(ps.proname, STACK_PROCATG, CPL_FRAME_TYPE_IMAGE);
01279 conf_frame = omega_product_frame(ps.confname,CONF_PROCATG, CPL_FRAME_TYPE_IMAGE);
01280 }
01281
01282 cpl_msg_info(cpl_func,"Saving stack and confidence map");
01283
01284 if(!cpl_propertylist_has(ps.plist,"EXTNAME")){
01285 cpl_propertylist_copy_property(ps.plist,alist,"EXTNAME");
01286 }
01287 if(omega_save_image(ps.combined[0],set,pars,ps.plist,ps.qclist,CPL_BPP_IEEE_FLOAT,
01288 ps.proname,RECIPE,product_frame,NULL,isfirst) == -1){
01289 cpl_msg_error(cpl_func,"Cannot save product. %s", cpl_error_get_message());
01290 freebivector(offsets);
01291 freeframeset(proset);
01292 freeframeset(proset_mask);
01293 freeplist(alist);
01294 omega_science_tidy(0);
01295 return -1;
01296 }
01297
01298 if(omega_save_image(ps.combined[1],set,pars,alist,NULL,CPL_BPP_16_SIGNED,
01299 ps.confname,RECIPE,conf_frame,NULL,isfirst) == -1){
01300 cpl_msg_error(cpl_func,"Cannot save product. %s", cpl_error_get_message());
01301 freebivector(offsets);
01302 freeframeset(proset);
01303 freeframeset(proset_mask);
01304 freeplist(alist);
01305 omega_science_tidy(0);
01306 return -1;
01307 }
01308
01309 freeplist(ps.qclist);
01310 freeplist(ps.plist);
01311 if(ps.combined!=NULL) {
01312 freeimage(ps.combined[0]);
01313 freeimage(ps.combined[1]);
01314 freespace(ps.combined);
01315 }
01316
01317
01318 ps.catalogue = omega_science_catalogue(ps.proname, ps.confname, zeropoint_final, pars);
01319 if(ps.catalogue == NULL){
01320 cpl_msg_error(cpl_func,"Unable to create catalogue of stack. %s", cpl_error_get_message());
01321 freeplist(alist);
01322 freebivector(offsets);
01323 freeframeset(proset);
01324 freeframeset(proset_mask);
01325 omega_science_tidy(0);
01326 return -1;
01327 }
01328
01329 if(omega_basic_param(ps.catalogue, pixscalex, &Seeing, &ellipticity) == -1){
01330 cpl_msg_warning(cpl_func,"Cannot find stars with CLASS_STAR near 1 to calculate seeing");
01331 }
01332
01333
01334 qclist = cpl_propertylist_new();
01335 cpl_propertylist_update_double(qclist, "ESO QC SCI SEEING", Seeing);
01336 cpl_propertylist_set_comment(qclist, "ESO QC SCI SEEING", "calculated seeing of science field");
01337 cpl_propertylist_update_double(qclist, "ESO QC SCI ELLIPTICITY", ellipticity);
01338 cpl_propertylist_set_comment(qclist, "ESO QC SCI ELLIPTICITY", "calculated ellipticity of science field");
01339
01340
01341
01342 if(isfirst){
01343 ps.catname = cpl_sprintf("%s_%s.fits", INSTRUME,CAT_PROCATG);
01344 catframe = omega_product_frame(ps.catname, CAT_PROCATG, CPL_FRAME_TYPE_TABLE);
01345 }
01346 cpl_msg_info(cpl_func,"Saving catalogue");
01347 if(omega_save_table(ps.catalogue,set,pars,alist,qclist,ps.catname,RECIPE,
01348 catframe,NULL,isfirst) == -1){
01349 freebivector(offsets);
01350 freeframeset(proset);
01351 freeframeset(proset_mask);
01352 freeplist(alist);
01353 freeplist(qclist);
01354 omega_science_tidy(0);
01355 return -1;
01356 }
01357
01358 freetable(ps.catalogue);
01359 freebivector(offsets);
01360 freeplist(alist);
01361 freeplist(qclist);
01362 }
01363
01364 cpl_msg_info(cpl_func,"Cleaning up");
01365
01366 freeframeset(proset);
01367 freeframeset(proset_mask);
01368 freeplist(alist);
01369 freeplist(qclist);
01370 omega_science_tidy(0);
01371
01372 return 0;
01373
01374 }
01375
01376
01377
01378 static int omega_science_retrieve_input_param(const cpl_parameterlist * parlist)
01379 {
01380
01381 const cpl_parameter *par = NULL;
01382 cpl_errorstate prestate = cpl_errorstate_get();
01383
01384
01385 par = cpl_parameterlist_find_const(parlist, "omega.omega_science.ExtensionNumber") ;
01386 omega_science_config.extnum = cpl_parameter_get_int(par) ;
01387 par = cpl_parameterlist_find_const(parlist, "omega.omega_science.OverscanMethod") ;
01388 omega_science_config.oc = cpl_parameter_get_int(par) ;
01389
01390 if(!cpl_errorstate_is_equal(prestate)){
01391 cpl_errorstate_dump(prestate, CPL_FALSE, NULL);
01392 return -1;
01393 }
01394
01395 return 0;
01396 }
01397
01398 static int omega_science_load_calib(int ext)
01399 {
01400
01401
01402
01403 ps.mbias = cpl_image_load(cpl_frame_get_filename(ps.mbiasfr), CPL_TYPE_FLOAT, 0, ext);
01404 if(ps.mbias == NULL){
01405 cpl_msg_error(cpl_func,"Cannot load Master Bias. %s", cpl_error_get_message());
01406 return -1;
01407 }
01408
01409 ps.mflat = cpl_image_load(cpl_frame_get_filename(ps.mflatfr),CPL_TYPE_FLOAT,0,ext);
01410 if (ps.mflat == NULL){
01411 cpl_msg_error(cpl_func,"Cannot load Master Flat. %s",cpl_error_get_message());
01412 return -1;
01413 }
01414
01415
01416 ps.bpm = cpl_image_load(cpl_frame_get_filename(ps.bpmfr), CPL_TYPE_INT, 0, ext);
01417 if(ps.bpm == NULL){
01418 cpl_msg_error(cpl_func, "Cannot load BPM. %s", cpl_error_get_message());
01419 return -1;
01420 }
01421
01422
01423 ps.photom = cpl_table_load(cpl_frame_get_filename(ps.photomfr),ext,0);
01424 if(ps.photom == NULL){
01425 cpl_msg_warning(cpl_func,"Cannot load the zeropoint table. %s",cpl_error_get_message());
01426 cpl_msg_warning(cpl_func,"Using values defined in the recipe parameter");
01427 }
01428
01429
01430
01431
01432
01433
01434
01435 if(ps.illumfr != NULL)
01436 ps.illum = cpl_image_load(cpl_frame_get_filename(ps.illumfr),CPL_TYPE_FLOAT,0,ext);
01437
01438
01439
01440
01441
01442 return 0;
01443 }
01444
01445 static cpl_error_code omega_correct_crpix_union(cpl_propertylist *xlist,
01446 cpl_bivector *offsets){
01447
01448 cpl_vector * offset_x=NULL;
01449 cpl_vector * offset_y=NULL;
01450 double crpix1=0.;
01451 double crpix2=0.;
01452
01453 offset_x=cpl_bivector_get_x(offsets);
01454 offset_y=cpl_bivector_get_y(offsets);
01455
01456
01457 if (cpl_vector_get_max(offset_x)-cpl_vector_get_min(offset_x) > 3000 ||
01458 cpl_vector_get_max(offset_y)-cpl_vector_get_min(offset_y) > 6000){
01459
01460
01461 return cpl_error_get_code();
01462 }
01463
01464
01465 if(cpl_propertylist_has(xlist, "CRPIX1") &&
01466 cpl_propertylist_has(xlist, "CRPIX2")){
01467 crpix1=cpl_propertylist_get_double(xlist, "CRPIX1");
01468 crpix2=cpl_propertylist_get_double(xlist, "CRPIX2");
01469
01470 cpl_propertylist_update_double(xlist,"CRPIX1",
01471 crpix1-cpl_vector_get_min(offset_x));
01472
01473 cpl_propertylist_update_double(xlist,"CRPIX2",
01474 crpix2-cpl_vector_get_min(offset_y));
01475
01476 }
01477
01478 return cpl_error_get_code();
01479
01480
01481 }
01482
01483
01484 static void omega_science_init(void) {
01485 ps.bpmfr = NULL;
01486 ps.fringesfr = NULL;
01487 ps.usnoa2 = NULL;
01488 ps.nskyfr = NULL;
01489 ps.illumfr = NULL;
01490 ps.mbiasfr = NULL;
01491 ps.mflatfr = NULL;
01492 ps.photomfr = NULL;
01493 ps.ph = NULL;
01494 ps.eh = NULL;
01495 ps.scifits = NULL;
01496 ps.sfits = NULL;
01497 ps.combined = NULL;
01498 ps.catalogue = NULL;
01499 ps.scilist = NULL;
01500 ps.labels = NULL;
01501 ps.mbias = NULL;
01502 ps.mflat = NULL;
01503 ps.bpm = NULL;
01504 ps.photom = NULL;
01505 ps.proname = NULL;
01506 ps.confname = NULL;
01507 ps.catname = NULL;
01508 ps.nsky = NULL;
01509 ps.illum = NULL;
01510 ps.fringes = NULL;
01511 ps.qclist = NULL;
01512 ps.plist = NULL;
01513 }
01514
01515
01516
01517
01518 static void omega_science_tidy(int level) {
01519 freeimage(ps.mbias);
01520 freeimage(ps.mflat);
01521 freeimage(ps.bpm);
01522 freeimage(ps.illum);
01523 freetable(ps.photom);
01524 freefits(ps.scifits);
01525 freeframeset(ps.scilist);
01526 if(level == 1)
01527 return;
01528
01529 freeimage(ps.fringes);
01530 freeimage(ps.nsky);
01531 freefits(ps.sfits);
01532 if(ps.combined!=NULL) {
01533 freeimage(ps.combined[0]);
01534 freeimage(ps.combined[1]);
01535 freespace(ps.combined);
01536 }
01537 freetable(ps.catalogue);
01538 freeimage(ps.mflat);
01539 freespace(ps.labels);
01540 freespace(ps.proname);
01541 freespace(ps.confname);
01542 freespace(ps.catname);
01543 freeplist(ps.qclist);
01544 freeplist(ps.plist);
01545 }
01546