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
00034
00035
00036 #include <string.h>
00037 #include <math.h>
00038 #include <float.h>
00039 #include <assert.h>
00040 #include <cpl.h>
00041
00042 #include <irplib_stdstar.h>
00043
00044 #include "irplib_tools.h"
00045 #include "irplib_utils.h"
00046
00047 #include "naco_parameter.h"
00048 #include "naco_dfs.h"
00049 #include "naco_pfits.h"
00050
00051
00052
00053
00054
00055 #define naco_plot_manpage \
00056 "The recipe can produce a number of predefined plots. " \
00057 "Zero means that none of the plots are produced, while " \
00058 "increasing values (e.g. 1 or 2) increases the number " \
00059 "of plots produced. If the plotting fails a warning is " \
00060 "produced, and the recipe continues. " \
00061 "The default behaviour of the plotting is to use " \
00062 "gnuplot (with option -persist). The recipe currently " \
00063 "produces 1D-plots using gnuplot commands. The recipe " \
00064 "user can control the actual plotting-command used by " \
00065 "the recipe to create the plot by setting the " \
00066 "environment variable CPL_PLOTTER. Currently, if " \
00067 "CPL_PLOTTER " \
00068 "is set it must contain the string 'gnuplot'. Setting " \
00069 "it to 'cat > my_gnuplot_$$.txt' causes a number of " \
00070 "ASCII-files to be created, which each produce a plot " \
00071 "when given as standard input to gnuplot (e.g. later " \
00072 "or on a different computer). A finer control of the " \
00073 "plotting options can be obtained by writing an " \
00074 "executable script, e.g. my_gnuplot.pl, that " \
00075 "executes gnuplot after setting the desired gnuplot " \
00076 "options (e.g. set terminal pslatex color) " \
00077 "and then setting CPL_PLOTTER to my_gnuplot.pl. " \
00078 "The predefined plots include plotting of images. " \
00079 "Images can be plotted not only with gnuplot, but also " \
00080 "using the pnm format. This is controlled with the " \
00081 "environment variable CPL_IMAGER. If CPL_IMAGER " \
00082 "is set to a string that does not contain the word " \
00083 "gnuplot, the recipe will generate the plot in pnm " \
00084 "format. E.g. setting CPL_IMAGER to " \
00085 "'display - &' will produce a gray-scale image " \
00086 "using the image viewer display."
00087
00088
00089 #define NACO_XCORR_SX 10
00090 #define NACO_XCORR_SY 10
00091
00092
00093 #define NACO_PARAMETER_SET(MASK, VARNAME, TYPE, MAN, DEFAULT, SHORT) \
00094 if (bitmask & MASK) { \
00095 char * paramname = cpl_sprintf(PACKAGE ".%s." VARNAME, recipe); \
00096 \
00097 p = cpl_parameter_new_value(paramname, TYPE, MAN, context, DEFAULT); \
00098 cpl_free(paramname); \
00099 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, SHORT); \
00100 cpl_parameterlist_append(self, p); \
00101 \
00102 if (cpl_error_get_code()) \
00103 (void)cpl_error_set_where(cpl_func); \
00104 \
00105 bitmask ^= MASK; \
00106 \
00107 \
00108 if (chkmask & MASK) \
00109 (void)cpl_error_set(cpl_func, CPL_ERROR_UNSPECIFIED); \
00110 chkmask |= MASK; \
00111 \
00112 }
00113
00114
00115 #define NACO_PARAMETER_GET_BOOL(MASK, VARNAME) \
00116 if (bitmask & MASK) { \
00117 value = irplib_parameterlist_get_bool(self, PACKAGE, recipe, VARNAME); \
00118 \
00119 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), CPL_FALSE); \
00120 \
00121 nbits++; \
00122 bitmask ^= MASK; \
00123 \
00124 }
00125
00126
00127 #define NACO_PARAMETER_GET_INT(MASK, VARNAME) \
00128 if (bitmask & MASK) { \
00129 value = irplib_parameterlist_get_int(self, PACKAGE, recipe, VARNAME); \
00130 \
00131 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), 0); \
00132 \
00133 nbits++; \
00134 bitmask ^= MASK; \
00135 \
00136 }
00137
00138
00139 #define NACO_PARAMETER_GET_DOUBLE(MASK, VARNAME) \
00140 if (bitmask & MASK) { \
00141 value = irplib_parameterlist_get_double(self, PACKAGE, recipe, VARNAME); \
00142 \
00143 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), 0.0); \
00144 \
00145 nbits++; \
00146 bitmask ^= MASK; \
00147 \
00148 }
00149
00150
00151 #define NACO_PARAMETER_GET_STRING(MASK, VARNAME) \
00152 if (bitmask & MASK) { \
00153 value = irplib_parameterlist_get_string(self, PACKAGE, recipe, VARNAME); \
00154 \
00155 cpl_ensure(value != NULL, cpl_error_get_code(), NULL); \
00156 \
00157 nbits++; \
00158 bitmask ^= MASK; \
00159 \
00160 }
00161
00162
00163
00169
00170
00174
00183
00184 cpl_error_code naco_parameter_set(cpl_parameterlist * self,
00185 const char * recipe,
00186 naco_parameter bitmask)
00187 {
00188
00189 cpl_parameter * p;
00190 char * context;
00191 naco_parameter chkmask = 0;
00192
00193
00194 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
00195 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
00196
00197
00198 context = cpl_sprintf(PACKAGE ".%s", recipe);
00199
00200
00201 NACO_PARAMETER_SET(NACO_PARAM_XTMAX, "extract_max", CPL_TYPE_INT,
00202 "Stop the spectrum extraction at this column", 1024,
00203 "xtmax");
00204
00205
00206 NACO_PARAMETER_SET(NACO_PARAM_XTMIN, "extract_min", CPL_TYPE_INT,
00207 "Start the spectrum extraction at this column", 1,
00208 "xtmin");
00209
00210
00211 NACO_PARAMETER_SET(NACO_PARAM_SAVE, "save", CPL_TYPE_INT,
00212 "A positive value causes additional, intermediate "
00213 "products to be saved", 0, "save");
00214
00215
00216 NACO_PARAMETER_SET(NACO_PARAM_PLOT, "plot", CPL_TYPE_INT,
00217 naco_plot_manpage, 0, "plot");
00218
00219
00220 NACO_PARAMETER_SET(NACO_PARAM_STAR_R, "star_r", CPL_TYPE_DOUBLE,
00221 "The star radius [arcsecond]",
00222 STREHL_STAR_RADIUS, "star_r");
00223
00224
00225 NACO_PARAMETER_SET(NACO_PARAM_BG_RINT, "bg_r1", CPL_TYPE_DOUBLE,
00226 "The internal radius of the background [arcsecond]",
00227 STREHL_BACKGROUND_R1, "bg_r1");
00228
00229
00230 NACO_PARAMETER_SET(NACO_PARAM_BG_REXT, "bg_r2", CPL_TYPE_DOUBLE,
00231 "The external radius of the background [arcsecond]",
00232 STREHL_BACKGROUND_R2, "bg_r2");
00233
00234
00235 NACO_PARAMETER_SET(NACO_PARAM_REJBORD, "rej_bord", CPL_TYPE_STRING,
00236 "Rejected left right bottom and top border [pixel]",
00237 "200 200 200 200", "r");
00238
00239
00240 NACO_PARAMETER_SET(NACO_PARAM_HOT_LIM, "hot_threshold", CPL_TYPE_DOUBLE,
00241 "Hot pixel map threshold", 10.0, "hot_t");
00242
00243
00244 NACO_PARAMETER_SET(NACO_PARAM_COLD_LIM, "cold_threshold", CPL_TYPE_DOUBLE,
00245 "Cold pixel map threshold", 6.0, "cold_t");
00246
00247
00248 NACO_PARAMETER_SET(NACO_PARAM_DEV_LIM, "dev_threshold", CPL_TYPE_DOUBLE,
00249 "Deviant pixel map threshold", 5.0, "dev_t");
00250
00251
00252 NACO_PARAMETER_SET(NACO_PARAM_NSAMPLES, "nsamples", CPL_TYPE_INT,
00253 "Number of samples for RON computation", 100,
00254 "nsamples");
00255
00256
00257 NACO_PARAMETER_SET(NACO_PARAM_HALFSIZE, "hsize", CPL_TYPE_INT,
00258 "Half size of the window for RON computation", 2,
00259 "hsize");
00260
00261
00262 NACO_PARAMETER_SET(NACO_PARAM_FORCE, "force", CPL_TYPE_BOOL,
00263 "Force the computation", FALSE, "force");
00264
00265
00266 NACO_PARAMETER_SET(NACO_PARAM_SLIT_W, "slit_width", CPL_TYPE_INT,
00267 "Slit width", 20, "slit_w");
00268
00269
00270 NACO_PARAMETER_SET(NACO_PARAM_BPMTHRES, "thresholds", CPL_TYPE_STRING,
00271 "Low and high thresholds for the Bad Pixel Map",
00272 "0.5 2.0", "t");
00273
00274
00275 NACO_PARAMETER_SET(NACO_PARAM_PROPFIT, "proport", CPL_TYPE_BOOL,
00276 "Use the proportional fit", FALSE, "prop");
00277
00278
00279 NACO_PARAMETER_SET(NACO_PARAM_BPM, "bpm", CPL_TYPE_BOOL,
00280 "Create the bad pixel map", FALSE, "bpm");
00281
00282
00283 NACO_PARAMETER_SET(NACO_PARAM_ERRORMAP, "errmap", CPL_TYPE_BOOL,
00284 "Create the error map", FALSE, "errmap");
00285
00286
00287 NACO_PARAMETER_SET(NACO_PARAM_INTCEPT, "intercept", CPL_TYPE_BOOL,
00288 "Create the intercept image", FALSE, "intercept");
00289
00290
00291 NACO_PARAMETER_SET(NACO_PARAM_RA, "ra", CPL_TYPE_DOUBLE,
00292 "Right Ascension [Degrees]", 999.0, "ra");
00293
00294
00295 NACO_PARAMETER_SET(NACO_PARAM_DEC, "dec", CPL_TYPE_DOUBLE,
00296 "DEClination [Degrees]", 999.0, "dec");
00297
00298
00299 NACO_PARAMETER_SET(NACO_PARAM_PIXSCALE, "pscale", CPL_TYPE_DOUBLE,
00300 "Pixel scale", -1.0, "pscale");
00301
00302
00303 NACO_PARAMETER_SET(NACO_PARAM_MAGNITD, "mag", CPL_TYPE_DOUBLE,
00304 "Magnitude", IRPLIB_STDSTAR_NOMAG, "mag");
00305
00306
00307 NACO_PARAMETER_SET(NACO_PARAM_SX, "sx", CPL_TYPE_INT,
00308 "Size of the search window in X-direction [pixel]",
00309 NACO_XCORR_SX, "sx");
00310
00311
00312 NACO_PARAMETER_SET(NACO_PARAM_SY, "sy", CPL_TYPE_INT,
00313 "Size of the search window in Y-direction [pixel]",
00314 NACO_XCORR_SY, "sy");
00315
00316
00317 NACO_PARAMETER_SET(NACO_PARAM_CHK_IMG, "check_im", CPL_TYPE_BOOL,
00318 "Create the check image", FALSE, "check_im");
00319
00320
00321
00322 NACO_PARAMETER_SET(NACO_PARAM_OFFSETS, "offsets", CPL_TYPE_STRING,
00323 "An optional ASCII specification of the offsets "
00324 "in case those in FITS-headers are missing or wrong. "
00325 "The file must consist of one line per object FITS-"
00326 "file and each line must consist of two "
00327 "numbers which represent the shift in pixels of that "
00328 "image relative to the first image. The first line "
00329 "should thus comprise two zeros. Correct FITS-header "
00330 "offsets mean that the i'th X offset can be gotten "
00331 "from Xoffset_0 - Xoffset_i, where Xoffset_i is the "
00332 "value of " NACO_PFITS_DOUBLE_CUMOFFSETX " and "
00333 "likewise for Y.", NULL, "off");
00334
00335
00336 NACO_PARAMETER_SET(NACO_PARAM_OBJECTS, "objects", CPL_TYPE_STRING,
00337 "objects file", NULL, "objs");
00338
00339
00340 NACO_PARAMETER_SET(NACO_PARAM_ODDEVEN, "oddeven", CPL_TYPE_BOOL,
00341 "Apply the odd-even correction. Warning: This flag "
00342 "currently has no effect", CPL_FALSE, "oddeven");
00343
00344
00345 NACO_PARAMETER_SET(NACO_PARAM_XCORR, "xcorr", CPL_TYPE_STRING,
00346 "Cross correlation search and measure sizes",
00347 "40 40 65 65", "xcorr");
00348
00349
00350 NACO_PARAMETER_SET(NACO_PARAM_UNION, "union", CPL_TYPE_BOOL,
00351 "Combine images using their union, as opposed to their "
00352 "intersection (deprecated and ignored, "
00353 "see --combine_method)", TRUE, "union");
00354
00355
00356 NACO_PARAMETER_SET(NACO_PARAM_REJ_HILO, "rej", CPL_TYPE_STRING,
00357 "Low and high number of rejected values", "2 2", "rej");
00358
00359
00360
00361
00362 NACO_PARAMETER_SET(NACO_PARAM_COMBINE, "comb_meth", CPL_TYPE_STRING,
00363 "Combine images using one of: 1) Onto the first image "
00364 "(first); 2) Their union (union); 3) Their intersection"
00365 " (inter). NB: Only the 'first'-method produces an "
00366 "image product with WCS coordinates. A successful "
00367 "'first'-method always produces a combined image with "
00368 "dimensions equal to those of the input images. "
00369 "For the 'union'-method the result image is at least "
00370 "as large as the input images while for the 'inter'-"
00371 "method the result image is at most as large as the "
00372 "input images", "union", "combine_method");
00373
00374
00375 NACO_PARAMETER_SET(NACO_PARAM_SKYPLANE, "sky_planes", CPL_TYPE_INT,
00376 "Estimate the sky using the median of the last n planes "
00377 "in the previous cube and the first n planes in the "
00378 "following cube. If the cube has less than n planes "
00379 "then use all planes. Zero means all. (Ignored for non-"
00380 "cube data)", 25, "sky_planes");
00381
00382
00383 NACO_PARAMETER_SET(NACO_PARAM_CUBEMODE, "cube_mode", CPL_TYPE_STRING,
00384 "Collapse cube data using one of: 1) No recentering "
00385 "(add); 2) Shift-and-Add (saa). (Ignored for non-"
00386 "cube data)", "saa", "cube_mode");
00387
00388
00389 NACO_PARAMETER_SET(NACO_PARAM_LUCK_STR, "lucky_strehl", CPL_TYPE_DOUBLE,
00390 "In cube mode use only the frames with a strehl ratio "
00391 "in the given top fraction. (Ignored for non-"
00392 "cube data)", 1.0, "lucky");
00393
00394
00395 NACO_PARAMETER_SET(NACO_PARAM_SAVECUBE, "save_cube", CPL_TYPE_BOOL,
00396 "Append the cube of corrected object images that are "
00397 "shifted added together to the product", FALSE,
00398 "save_cube");
00399
00400 cpl_free(context);
00401
00402 cpl_ensure_code(bitmask == 0, CPL_ERROR_UNSUPPORTED_MODE);
00403
00404
00405 return cpl_error_set_where(cpl_func);
00406 }
00407
00408
00418
00419 cpl_boolean naco_parameterlist_get_bool(const cpl_parameterlist * self,
00420 const char * recipe,
00421 naco_parameter bitmask)
00422 {
00423
00424 int nbits = 0;
00425 cpl_boolean value = CPL_FALSE;
00426
00427
00428 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), CPL_FALSE);
00429 cpl_ensure(self, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00430 cpl_ensure(recipe, CPL_ERROR_NULL_INPUT, CPL_FALSE);
00431
00432
00433 NACO_PARAMETER_GET_BOOL(NACO_PARAM_FORCE, "force");
00434
00435
00436 NACO_PARAMETER_GET_BOOL(NACO_PARAM_PROPFIT, "proport");
00437
00438
00439 NACO_PARAMETER_GET_BOOL(NACO_PARAM_BPM, "bpm");
00440
00441
00442 NACO_PARAMETER_GET_BOOL(NACO_PARAM_ERRORMAP, "errmap");
00443
00444
00445 NACO_PARAMETER_GET_BOOL(NACO_PARAM_INTCEPT, "intercept");
00446
00447
00448 NACO_PARAMETER_GET_BOOL(NACO_PARAM_CHK_IMG, "check_im");
00449
00450
00451 NACO_PARAMETER_GET_BOOL(NACO_PARAM_ODDEVEN, "oddeven");
00452
00453
00454 NACO_PARAMETER_GET_BOOL(NACO_PARAM_SAVECUBE, "save_cube");
00455
00456 cpl_ensure(bitmask == 0, CPL_ERROR_UNSUPPORTED_MODE, CPL_FALSE);
00457 cpl_ensure(nbits == 1, CPL_ERROR_ILLEGAL_INPUT, CPL_FALSE);
00458
00459 return value;
00460
00461 }
00462
00463
00473
00474 int naco_parameterlist_get_int(const cpl_parameterlist * self,
00475 const char * recipe,
00476 naco_parameter bitmask)
00477 {
00478
00479 int nbits = 0;
00480 int value = 0;
00481
00482
00483 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), 0);
00484 cpl_ensure(self, CPL_ERROR_NULL_INPUT, 0);
00485 cpl_ensure(recipe, CPL_ERROR_NULL_INPUT, 0);
00486
00487
00488
00489
00490
00491 NACO_PARAMETER_GET_INT(NACO_PARAM_XTMAX, "extract_max");
00492
00493
00494 NACO_PARAMETER_GET_INT(NACO_PARAM_XTMIN, "extract_min");
00495
00496
00497 NACO_PARAMETER_GET_INT(NACO_PARAM_SAVE, "save");
00498
00499
00500 NACO_PARAMETER_GET_INT(NACO_PARAM_PLOT, "plot");
00501
00502
00503 NACO_PARAMETER_GET_INT(NACO_PARAM_NSAMPLES, "nsamples");
00504
00505
00506 NACO_PARAMETER_GET_INT(NACO_PARAM_HALFSIZE, "hsize");
00507
00508
00509 NACO_PARAMETER_GET_INT(NACO_PARAM_SX, "sx");
00510
00511
00512 NACO_PARAMETER_GET_INT(NACO_PARAM_SY, "sy");
00513
00514
00515 NACO_PARAMETER_GET_INT(NACO_PARAM_SLIT_W, "slit_width");
00516
00517
00518 NACO_PARAMETER_GET_INT(NACO_PARAM_SKYPLANE, "sky_planes");
00519
00520 cpl_ensure(bitmask == 0, CPL_ERROR_UNSUPPORTED_MODE, 0);
00521 cpl_ensure(nbits == 1, CPL_ERROR_ILLEGAL_INPUT, 0);
00522
00523 return value;
00524
00525 }
00526
00527
00537
00538 double naco_parameterlist_get_double(const cpl_parameterlist * self,
00539 const char * recipe,
00540 naco_parameter bitmask)
00541 {
00542
00543 int nbits = 0;
00544 double value = DBL_MAX;
00545
00546
00547 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), 0.0);
00548 cpl_ensure(self, CPL_ERROR_NULL_INPUT, 0.0);
00549 cpl_ensure(recipe, CPL_ERROR_NULL_INPUT, 0.0);
00550
00551
00552
00553 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_STAR_R, "star_r");
00554
00555
00556 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_BG_RINT, "bg_r1");
00557
00558
00559 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_BG_REXT, "bg_r2");
00560
00561
00562
00563 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_HOT_LIM, "hot_threshold");
00564
00565
00566 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_COLD_LIM, "cold_threshold");
00567
00568
00569 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_DEV_LIM, "dev_threshold");
00570
00571
00572 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_RA, "ra");
00573
00574
00575 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_DEC, "dec");
00576
00577
00578 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_PIXSCALE, "pscale");
00579
00580
00581 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_MAGNITD, "mag");
00582
00583
00584 NACO_PARAMETER_GET_DOUBLE(NACO_PARAM_LUCK_STR, "lucky_strehl");
00585
00586
00587 cpl_ensure(bitmask == 0, CPL_ERROR_UNSUPPORTED_MODE, 0.0);
00588 cpl_ensure(nbits == 1, CPL_ERROR_ILLEGAL_INPUT, 0.0);
00589
00590 return value;
00591
00592 }
00593
00594
00603
00604 const char * naco_parameterlist_get_string(const cpl_parameterlist * self,
00605 const char * recipe,
00606 naco_parameter bitmask)
00607 {
00608
00609 int nbits = 0;
00610 const char * value = NULL;
00611 const cpl_boolean is_combine
00612 = bitmask & NACO_PARAM_COMBINE ? CPL_TRUE : CPL_FALSE;
00613
00614 cpl_ensure(self, CPL_ERROR_NULL_INPUT, NULL);
00615 cpl_ensure(recipe, CPL_ERROR_NULL_INPUT, NULL);
00616
00617
00618
00619 NACO_PARAMETER_GET_STRING(NACO_PARAM_REJBORD, "rej_bord");
00620
00621
00622 NACO_PARAMETER_GET_STRING(NACO_PARAM_BPMTHRES, "thresholds");
00623
00624
00625 NACO_PARAMETER_GET_STRING(NACO_PARAM_OFFSETS, "offsets");
00626
00627
00628 NACO_PARAMETER_GET_STRING(NACO_PARAM_OBJECTS, "objects");
00629
00630
00631 NACO_PARAMETER_GET_STRING(NACO_PARAM_XCORR, "xcorr");
00632
00633
00634 NACO_PARAMETER_GET_STRING(NACO_PARAM_REJ_HILO, "rej");
00635
00636
00637 NACO_PARAMETER_GET_STRING(NACO_PARAM_COMBINE, "comb_meth");
00638
00639
00640 NACO_PARAMETER_GET_STRING(NACO_PARAM_CUBEMODE, "cube_mode");
00641
00642
00643 cpl_ensure(bitmask == 0, CPL_ERROR_UNSUPPORTED_MODE, NULL);
00644 cpl_ensure(nbits == 1, CPL_ERROR_ILLEGAL_INPUT, NULL);
00645
00646 assert(value != NULL);
00647
00648
00649 if (is_combine)
00650 cpl_ensure(strcmp(value, "first") == 0 || strcmp(value, "union") == 0 ||
00651 strcmp(value, "intersect") == 0, CPL_ERROR_UNSUPPORTED_MODE,
00652 NULL);
00653
00654 return value;
00655 }
00656