KMOS Pipeline Reference Manual  1.0.7
kmo_fits_check.c
00001 /* $Id: kmo_fits_check.c,v 1.6 2013/01/09 13:51:39 aagudo Exp $
00002  *
00003  * This file is part of the KMOS Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: aagudo $
00023  * $Date: 2013/01/09 13:51:39 $
00024  * $Revision: 1.6 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <string.h>
00033 
00034 #include <cpl.h>
00035 
00036 #include "kmo_utils.h"
00037 #include "kmo_dfs.h"
00038 #include "kmo_error.h"
00039 #include "kmo_priv_fits_check.h"
00040 #include "kmo_constants.h"
00041 #include "kmo_debug.h"
00042 
00043 static int kmo_fits_check_create(cpl_plugin *);
00044 static int kmo_fits_check_exec(cpl_plugin *);
00045 static int kmo_fits_check_destroy(cpl_plugin *);
00046 static int kmo_fits_check(cpl_parameterlist *, cpl_frameset *);
00047 
00048 
00049 static char kmo_fits_check_description[] =
00050 "Recipe to print information on FITS files, contained data/noise values or\n"
00051 "header keywords of all extensions of a fits file, preferably a KMOS fits-file\n"
00052 "(RAW, F1I, F2I, F3I, F2D etc.). This recipe is intended for debugging purposes\n"
00053 "only.\n"
00054 "By default a short summary is printed.\n"
00055 "The following data types of keywords are recognized: bool, char, double, float,\n"
00056 "int, long, string\n"
00057 "As input one fits-file is accepted, no output frame is generated.\n"
00058 "\n"
00059 "BASIC PARAMETERS:\n"
00060 "-----------------\n"
00061 "--h\n"
00062 "With this parameter just the header keywords are printed:\n"
00063 "  –1  prints the primary header and the headers of all the extensions\n"
00064 "   0  prints just the primary header\n"
00065 "   1  prints the header of the first extension etc.\n"
00066 "\n"
00067 "--d\n"
00068 "With this parameter just the data (or depending on the extension: noise) is\n"
00069 "printed:\n"
00070 "  –1 prints the primary header and the headers of all the extensions\n"
00071 "   0 prints data of the primary header which is empty for KMOS FITS frames\n"
00072 "   1 prints the data/noise of the first extension etc.\n"
00073 "This parameter should only be used with very small datasets, otherwise the\n"
00074 "screen will be flooded with numbers.\n"
00075 "\n"
00076 "-------------------------------------------------------------------------------\n"
00077 "  Input files:\n"
00078 "\n"
00079 "   DO                    KMOS                                                  \n"
00080 "   category              Type   Explanation                    Required #Frames\n"
00081 "   --------              -----  -----------                    -------- -------\n"
00082 "   <none or any>         <any>  Any FITS file                      Y       1   \n"
00083 "\n"
00084 "  Output files:\n"
00085 "\n"
00086 "   DO                    KMOS\n"
00087 "   category              Type   Explanation\n"
00088 "   --------              -----  -----------\n"
00089 "   <none>                <none> No output frames generated, only text output   \n"
00090 "-------------------------------------------------------------------------------\n"
00091 "\n";
00092 
00107 int cpl_plugin_get_info(cpl_pluginlist *list)
00108 {
00109     cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
00110     cpl_plugin *plugin = &recipe->interface;
00111 
00112     cpl_plugin_init(plugin,
00113                         CPL_PLUGIN_API,
00114                         KMOS_BINARY_VERSION,
00115                         CPL_PLUGIN_TYPE_RECIPE,
00116                         "kmo_fits_check",
00117                         "Check contents of a KMOS fits-file",
00118                         kmo_fits_check_description,
00119                         "Alex Agudo Berbel",
00120                         "agudo@mpe.mpg.de",
00121                         kmos_get_license(),
00122                         kmo_fits_check_create,
00123                         kmo_fits_check_exec,
00124                         kmo_fits_check_destroy);
00125 
00126     cpl_pluginlist_append(list, plugin);
00127 
00128     return 0;
00129 }
00130 
00138 static int kmo_fits_check_create(cpl_plugin *plugin)
00139 {
00140     cpl_recipe *recipe;
00141     cpl_parameter *p;
00142 
00143     /* Check that the plugin is part of a valid recipe */
00144     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00145         recipe = (cpl_recipe *)plugin;
00146     else
00147         return -1;
00148 
00149     /* Create the parameters list in the cpl_recipe object */
00150     recipe->parameters = cpl_parameterlist_new();
00151 
00152     /* Fill the parameters list */
00153     /* --data */
00154     p = cpl_parameter_new_value("kmos.fits_check.data",
00155                                 CPL_TYPE_INT,
00156                                 "The extension whose data to print. "
00157                                 "(0 is the empty primary extension, "
00158                                 "-1 prints just all data extensions, "
00159                                 "by default all headers and data of all "
00160                                 "extensions are printed.",
00161                                 "kmos.fits_check",
00162                                 -2);
00163     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "data");
00164     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00165     cpl_parameterlist_append(recipe->parameters, p);
00166 
00167     /* --header */
00168     p = cpl_parameter_new_value("kmos.fits_check.header",
00169                                 CPL_TYPE_INT,
00170                                 "The extension whose header to print"
00171                                 "(0 is the empty primary extension, "
00172                                 "-1 prints just all headers, "
00173                                 "by default all headers and data of all "
00174                                 "extensions are printed.",
00175                                 "kmos.fits_check",
00176                                 -2);
00177     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "header");
00178     cpl_parameter_disable(p, CPL_PARAMETER_MODE_ENV);
00179     cpl_parameterlist_append(recipe->parameters, p);
00180 
00181     return 0;
00182 }
00183 
00189 static int kmo_fits_check_exec(cpl_plugin *plugin)
00190 {
00191     cpl_recipe  *recipe;
00192 
00193     /* Get the recipe out of the plugin */
00194     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00195         recipe = (cpl_recipe *)plugin;
00196     else return -1 ;
00197 
00198     return kmo_fits_check(recipe->parameters, recipe->frames);
00199 }
00200 
00206 static int kmo_fits_check_destroy(cpl_plugin *plugin)
00207 {
00208     cpl_recipe *recipe;
00209 
00210     /* Get the recipe out of the plugin */
00211     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00212         recipe = (cpl_recipe *)plugin;
00213     else return -1 ;
00214 
00215     cpl_parameterlist_delete(recipe->parameters);
00216     return 0 ;
00217 }
00218 
00219 
00220 
00229 static int kmo_fits_check(cpl_parameterlist *parlist, cpl_frameset *frameset)
00230 {
00231     int                 i                   = 0,
00232                         ret_val             = 0,
00233                         info                = FALSE,
00234                         valid               = TRUE,
00235                         h                   = 0,
00236                         d                   = 0;
00237     cpl_imagelist       *imglist            = NULL;
00238     cpl_image           *img                = NULL;
00239     kmclipm_vector      *vec                = NULL;
00240     cpl_frame           *frame              = NULL;
00241     cpl_propertylist    *pl                 = NULL;
00242     cpl_table           *tbl                = NULL;
00243     main_fits_desc      desc;
00244     const char          *xtension = NULL;
00245 
00246     KMO_TRY
00247     {
00248         kmo_init_fits_desc(&desc);
00249 
00250         /* --- check input --- */
00251         KMO_TRY_ASSURE((parlist != NULL) &&
00252                        (frameset != NULL),
00253                        CPL_ERROR_NULL_INPUT,
00254                        "Not all input data is provided!");
00255 
00256         if (cpl_frameset_get_size(frameset) == 0) {
00257             KMO_TRY_ASSURE(cpl_frameset_get_size(frameset) == 1,
00258                            CPL_ERROR_ILLEGAL_INPUT,
00259                            "Recipe needs a FITS file to inspect!");
00260         } else {
00261             KMO_TRY_ASSURE(cpl_frameset_get_size(frameset) == 1,
00262                            CPL_ERROR_ILLEGAL_INPUT,
00263                            "Recipe can check just one frame!");
00264         }
00265 
00266         KMO_TRY_EXIT_IF_NULL(
00267             frame = kmo_dfs_get_frame(frameset, "0"));
00268 
00269         cpl_msg_info("", "--- Parameter setup for kmo_fits_check ----");
00270 
00271         h = kmo_dfs_get_parameter_int(parlist, "kmos.fits_check.header");
00272         KMO_TRY_CHECK_ERROR_STATE();
00273         KMO_TRY_EXIT_IF_ERROR(
00274             kmo_dfs_print_parameter_help(parlist, "kmos.fits_check.header"));
00275 
00276         d = kmo_dfs_get_parameter_int(parlist, "kmos.fits_check.data");
00277         KMO_TRY_CHECK_ERROR_STATE();
00278         KMO_TRY_EXIT_IF_ERROR(
00279             kmo_dfs_print_parameter_help(parlist, "kmos.fits_check.data"));
00280 
00281         cpl_msg_info("", "-------------------------------------------");
00282 
00283         cpl_msg_severity msg_level = cpl_msg_get_level();
00284         cpl_msg_set_level(CPL_MSG_OFF);
00285         desc = kmo_identify_fits_header(cpl_frame_get_filename(frame));
00286         cpl_msg_set_level(msg_level);
00287 
00288         if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
00289             // This doesn't seem to be a valid KMOS FITS frame
00290             valid = FALSE;
00291             cpl_error_reset();
00292         } else {
00293             valid = TRUE;
00294 
00295             if (desc.frame_type == illegal_frame) {
00296                 valid = FALSE;
00297                 cpl_error_reset();
00298             }
00299         }
00300 
00301         if ((h == -2) && (d == -2)) {
00302             // default case, print info
00303             info = TRUE;
00304         }
00305 
00306         if (info) {
00307             /* print info */
00308             KMO_TRY_EXIT_IF_ERROR(
00309                 kmo_fits_check_print_info(valid, &desc, frame));
00310         } else if (((h == -1) && (d > cpl_frame_get_nextensions(frame)+1)) ||
00311                    ((d == -1) && (h > cpl_frame_get_nextensions(frame)+1))) {
00312             printf("++++++++++++++++++++++++++++++++++++++++"
00313                    "++++++++++++++++++++++++++++++++++++++++\n");
00314 
00315             printf("ERROR: Supplied fits file has only %d extensions!\n",
00316                    cpl_frame_get_nextensions(frame)+1);
00317 
00318             printf("++++++++++++++++++++++++++++++++++++++++"
00319                    "++++++++++++++++++++++++++++++++++++++++\n");
00320         } else {
00321             /* print requested header or data section (-1 prints all of them) */
00322             int nr_ext = 0;
00323             if (valid) {
00324                 nr_ext = desc.nr_ext;
00325             } else {
00326                 nr_ext = cpl_frame_get_nextensions(frame);
00327             }
00328 
00329             if ((h < -2) || (d < -2)) {
00330                 printf(">>> The specified extension number must be "
00331                        "equal or greater than 0 (-1 to display all headers)!\n");
00332             } else if ((h > nr_ext) || (d > nr_ext)) {
00333                 if (nr_ext == 0) {
00334                     printf(">>> The file has only a primary extension!\n");
00335                 } else if (nr_ext == 1) {
00336                     printf(">>> The file has only 1 extension!\n");
00337                 } else {
00338                     printf(">>> The file has only %d extensions!\n", nr_ext);
00339                 }
00340             } else {
00341                 for (i = 0; i <= nr_ext; i++) {
00342                     KMO_TRY_EXIT_IF_NULL(
00343                         pl = kmclipm_propertylist_load(cpl_frame_get_filename(frame),
00344                                                   i));
00345 
00346                     // print label
00347                     if (((h == -1) || (d == -1)) ||
00348                         ((h == i) && (d == -2)) ||
00349                         ((h == -1) && (d == -2)) ||
00350                         ((h == -2) && (d == i)) ||
00351                         ((h == -2) && (d == -1)) ||
00352                         ((h == d) && (h == i))) {
00353                         if (i == 0) {
00354                             printf("++++++++++++++++++++++++++++++++++++++++"
00355                                    "++++++++++++++++++++++++++++++++++++++++\n");
00356                             if (cpl_propertylist_get_int(pl, NAXIS) == 0) {
00357                                 printf("Extension 0, primary\n");
00358                                 printf("--------------------\n");
00359                             } else {
00360                                 printf("Extension 0\n");
00361                                 printf("-----------\n");
00362                             }
00363                         } else {
00364                             printf("++++++++++++++++++++++++++++++++++++++++"
00365                                    "++++++++++++++++++++++++++++++++++++++++\n");
00366                             printf("Extension %d\n", i);
00367                             printf("------------\n");
00368                         }
00369                     }
00370 
00371                     // print header
00372                     if (((h == -1) && (d == -1)) ||
00373                         ((d == -2) && (h == i)) ||
00374                         ((d == -2) && (h == -1)) ||
00375                         ((h == d) && (h == i)))
00376                     {
00377                         KMO_TRY_EXIT_IF_ERROR(
00378                             kmo_fits_check_print_header(pl));
00379                     }
00380 
00381                     // print separator between header and data
00382                     if (d == h) {
00383                         printf("---------------------------\n");
00384                     }
00385 
00386                     // print data
00387                     if (((h == -1) && (d == -1)) ||
00388                         ((h == -2) && (d == i)) ||
00389                         ((h == -2) && (d == -1)) ||
00390                         ((h == d) && (h == i))) {
00391 
00392                         if (cpl_propertylist_get_int(pl, NAXIS) == 0) {
00393                             printf("empty\n");
00394                         } else {
00395                             if (cpl_propertylist_has(pl, XTENSION)) {
00396                                 xtension = cpl_propertylist_get_string(pl,
00397                                                                     XTENSION);
00398                             } else {
00399                                 xtension = "";
00400                             }
00401 
00402                             if ((desc.fits_type == f3i_fits) ||
00403                                 ((cpl_propertylist_get_int(pl, NAXIS) == 3) &&
00404                                         (desc.fits_type == illegal_fits))) {
00405                                 KMO_TRY_EXIT_IF_NULL(
00406                                     imglist = kmclipm_imagelist_load(
00407                                                   cpl_frame_get_filename(frame),
00408                                                   CPL_TYPE_FLOAT, i));
00409 
00410                                 KMO_TRY_EXIT_IF_ERROR(
00411                                     kmo_fits_check_print_imagelist(imglist));
00412 
00413                                 cpl_imagelist_delete(imglist); imglist = NULL;
00414                             } else if ((desc.fits_type == f2d_fits) ||
00415                                        (desc.fits_type == b2d_fits) ||
00416                                        (desc.fits_type == f2i_fits) ||
00417                                        (desc.fits_type == raw_fits) ||
00418                                        ((cpl_propertylist_get_int(pl, NAXIS) == 2) &&
00419                                         (desc.fits_type == illegal_fits) &&
00420                                         (strcmp(xtension, "BINTABLE") != 0)))
00421                             {
00422                                 KMO_TRY_EXIT_IF_NULL(
00423                                     img = kmclipm_image_load(
00424                                                   cpl_frame_get_filename(frame),
00425                                                   CPL_TYPE_FLOAT, 0, i));
00426 
00427                                 KMO_TRY_EXIT_IF_ERROR(
00428                                     kmo_fits_check_print_image(img));
00429 
00430                                 cpl_image_delete(img); img = NULL;
00431                             } else if ((desc.fits_type == f1i_fits) ||
00432                                        (desc.fits_type == f1d_fits) ||
00433                                        (desc.fits_type == f1s_fits) ||
00434                                        ((cpl_propertylist_get_int(pl, NAXIS) == 1) &&
00435                                         (desc.fits_type == illegal_fits)))
00436                             {
00437                                 KMO_TRY_EXIT_IF_NULL(
00438                                     vec = kmclipm_vector_load(
00439                                                   cpl_frame_get_filename(frame),
00440                                                   i));
00441 
00442                                 KMO_TRY_EXIT_IF_ERROR(
00443                                     kmo_fits_check_print_vector(vec));
00444 
00445                                 kmclipm_vector_delete(vec); vec = NULL;
00446                             } else if ((desc.fits_type == f1l_fits) ||
00447                                        (desc.fits_type == f2l_fits) ||
00448                                        (strcmp(xtension, "BINTABLE") == 0))
00449                             {
00450                                 KMO_TRY_EXIT_IF_NULL(
00451                                     tbl = kmclipm_table_load(
00452                                                   cpl_frame_get_filename(frame),
00453                                                   i, 0));
00454 
00455                                 KMO_TRY_EXIT_IF_ERROR(
00456                                     kmo_fits_check_print_table(tbl));
00457 
00458                                 cpl_table_delete(tbl); tbl = NULL;
00459                             }
00460                         }
00461                     }
00462                     cpl_propertylist_delete(pl); pl = NULL;
00463                 }
00464                 printf("++++++++++++++++++++++++++++++++++++++++"
00465                        "++++++++++++++++++++++++++++++++++++++++\n");
00466 
00467                 if (desc.fits_type == raw_fits) {
00468                     printf(">>> Attention: It is assumed that the file\n");
00469                     printf(">>>            is a RAW-fits with 4 extensions\n");
00470                 }
00471             }
00472         }
00473     }
00474     KMO_CATCH
00475     {
00476         KMO_CATCH_MSG();
00477 
00478         ret_val = -1;
00479     }
00480 
00481     kmo_free_fits_desc(&desc);
00482 
00483     return ret_val;
00484 }
00485