gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 27323ea: New --numhdus option in Fits prints n


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 27323ea: New --numhdus option in Fits prints number of extensions/HDUs
Date: Tue, 9 Oct 2018 10:11:10 -0400 (EDT)

branch: master
commit 27323eac98cac30a907be9b56efc29b2907f5d12
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    New --numhdus option in Fits prints number of extensions/HDUs
    
    This new option will print the number of extensions in a FITS file (and
    nothing else). The thought of it actually came up as I was writing a script
    myself and noticed the lack of a simple step like this in my script.
    
    I also noticed that we weren't closing the opened FITS file in
    `fits_print_extension_info'. So this is also corrected.
---
 NEWS              |  3 +++
 bin/fits/args.h   | 13 +++++++++++
 bin/fits/fits.c   | 69 +++++++++++++++++++++++++++++++++++++++++--------------
 bin/fits/main.h   | 33 +++++++++++++-------------
 bin/fits/ui.c     | 10 ++++++--
 bin/fits/ui.h     |  3 ++-
 doc/gnuastro.texi | 11 +++++++++
 7 files changed, 106 insertions(+), 36 deletions(-)

diff --git a/NEWS b/NEWS
index 2096b6b..390b3dd 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,9 @@ GNU Astronomy Utilities NEWS                          -*- 
outline -*-
   Arithmetic:
     --onedasimage: write output as an image if it has one dimension, not table.
 
+  Fits:
+    --numhdus: prints the number of HDUs in the given FITS file.
+
   NoiseChisel:
     - New outlier identification algorithm for quantile thresholds. This is
       very useful when there are extended and bright sources in the
diff --git a/bin/fits/args.h b/bin/fits/args.h
index 76baf3c..372c7c1 100644
--- a/bin/fits/args.h
+++ b/bin/fits/args.h
@@ -76,6 +76,19 @@ struct argp_option program_options[] =
       GAL_OPTIONS_NOT_MANDATORY,
       GAL_OPTIONS_NOT_SET
     },
+    {
+      "numhdus",
+      UI_KEY_NUMHDUS,
+      0,
+      0,
+      "Print number of HDUs in the given FITS file.",
+      UI_GROUP_EXTENSION,
+      &p->numhdus,
+      GAL_OPTIONS_NO_ARG_TYPE,
+      GAL_OPTIONS_RANGE_0_OR_1,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET
+    },
 
 
 
diff --git a/bin/fits/fits.c b/bin/fits/fits.c
index 55c278b..2c47eed 100644
--- a/bin/fits/fits.c
+++ b/bin/fits/fits.c
@@ -221,8 +221,11 @@ fits_print_extension_info(struct fitsparams *p)
         }
     }
 
+  /* Close the file. */
+  fits_close_file(fptr, &status);
 
-  /* Print the resutls. */
+
+  /* Print the results. */
   if(!p->cp.quiet)
     {
       printf("%s\nRun on %s-----\n", PROGRAM_STRING, ctime(&p->rawtime));
@@ -244,6 +247,30 @@ fits_print_extension_info(struct fitsparams *p)
 
 
 static void
+fits_hdu_number(struct fitsparams *p)
+{
+  fitsfile *fptr;
+  int numhdu, status=0;
+
+  /* Read the first extension (necessary for reading the rest). */
+  fptr=gal_fits_hdu_open(p->filename, "0", READONLY);
+
+  /* Get the number of HDUs. */
+  if( fits_get_num_hdus(fptr, &numhdu, &status) )
+    gal_fits_io_error(status, "finding number of HDUs");
+
+  /* Close the file. */
+  fits_close_file(fptr, &status);
+
+  /* Print the result. */
+  printf("%d\n", numhdu);
+}
+
+
+
+
+
+static void
 fits_hdu_remove(struct fitsparams *p, int *r)
 {
   char *hdu;
@@ -332,24 +359,32 @@ fits(struct fitsparams *p)
 
     /* HDU, functions defined here. */
     case FITS_MODE_HDU:
-      if(p->copy)
-        {
-          fits_hdu_copy(p, 0, &r);
-          printhduinfo=0;
-        }
-      if(p->cut)
-        {
-          fits_hdu_copy(p, 1, &r);
-          printhduinfo=0;
-        }
-      if(p->remove)
+      /* Options that must be called alone. */
+      if(p->numhdus)
+        fits_hdu_number(p);
+
+      /* Options that can be called together. */
+      else
         {
-          fits_hdu_remove(p, &r);
-          printhduinfo=0;
-        }
+          if(p->copy)
+            {
+              fits_hdu_copy(p, 0, &r);
+              printhduinfo=0;
+            }
+          if(p->cut)
+            {
+              fits_hdu_copy(p, 1, &r);
+              printhduinfo=0;
+            }
+          if(p->remove)
+            {
+              fits_hdu_remove(p, &r);
+              printhduinfo=0;
+            }
 
-      if(printhduinfo)
-        fits_print_extension_info(p);
+          if(printhduinfo)
+            fits_print_extension_info(p);
+        }
       break;
 
     /* Not recognized. */
diff --git a/bin/fits/main.h b/bin/fits/main.h
index 78681f0..43edc06 100644
--- a/bin/fits/main.h
+++ b/bin/fits/main.h
@@ -53,22 +53,23 @@ enum fits_mode
 struct fitsparams
 {
   /* From the environment. */
-  struct gal_options_common_params cp;  /* Common parameters.       */
-  int  hdu_in_commandline;     /* HDU wasn't given in config. file. */
-  char          *filename;     /* Name of input file.               */
-  gal_list_str_t  *remove;     /* Remove extensions from a file.    */
-  gal_list_str_t    *copy;     /* Copy extensions to output.        */
-  gal_list_str_t     *cut;     /* Copy ext. to output and remove.   */
-  uint8_t    printallkeys;     /* Print all the header keywords.    */
-  uint8_t            date;     /* Set DATE to current time.         */
-  gal_list_str_t    *asis;     /* Strings to write asis.            */
-  gal_list_str_t  *delete;     /* Keywords to remove.               */
-  gal_list_str_t  *rename;     /* Rename a keyword.                 */
-  gal_list_str_t  *update;     /* For keywords to update.           */
-  gal_list_str_t   *write;     /* Full arg. for keywords to add.    */
-  gal_list_str_t *history;     /* HISTORY value.                    */
-  gal_list_str_t *comment;     /* COMMENT value.                    */
-  uint8_t     quitonerror;     /* Quit if an error occurs.          */
+  struct gal_options_common_params cp;  /* Common parameters.           */
+  int  hdu_in_commandline;     /* HDU wasn't given in config. file.     */
+  char          *filename;     /* Name of input file.                   */
+  gal_list_str_t  *remove;     /* Remove extensions from a file.        */
+  gal_list_str_t    *copy;     /* Copy extensions to output.            */
+  gal_list_str_t     *cut;     /* Copy ext. to output and remove.       */
+  uint8_t         numhdus;     /* Print number of HDUs in FITS file.    */
+  uint8_t    printallkeys;     /* Print all the header keywords.        */
+  uint8_t            date;     /* Set DATE to current time.             */
+  gal_list_str_t    *asis;     /* Strings to write asis.                */
+  gal_list_str_t  *delete;     /* Keywords to remove.                   */
+  gal_list_str_t  *rename;     /* Rename a keyword.                     */
+  gal_list_str_t  *update;     /* For keywords to update.               */
+  gal_list_str_t   *write;     /* Full arg. for keywords to add.        */
+  gal_list_str_t *history;     /* HISTORY value.                        */
+  gal_list_str_t *comment;     /* COMMENT value.                        */
+  uint8_t     quitonerror;     /* Quit if an error occurs.              */
 
   /* Internal: */
   int                         mode;  /* Operating on HDUs or keywords.  */
diff --git a/bin/fits/ui.c b/bin/fits/ui.c
index 787b9e4..6482d17 100644
--- a/bin/fits/ui.c
+++ b/bin/fits/ui.c
@@ -237,14 +237,20 @@ ui_read_check_only_options(struct fitsparams *p)
     }
 
   /* Same for the extension-related options */
-  if( p->remove || p->copy || p->cut )
+  if( p->remove || p->copy || p->cut || p->numhdus)
     {
       /* A small sanity check. */
       if(p->mode!=FITS_MODE_INVALID)
         error(EXIT_FAILURE, 0, "extension and keyword manipulation options "
               "cannot be called together");
 
-      /* Set the mode and turn on the `needshdu' flag. */
+      /* Unlike the rest of the HDU-related options, `--numhdus' must be
+         called alone. */
+      if(p->numhdus==1 && (p->remove || p->copy || p->cut) )
+        error(EXIT_FAILURE, 0, "`--numhdus' option must be called alone (it "
+              "cannot be called with other extension or keyword options)");
+
+      /* Set the operating mode. */
       p->mode=FITS_MODE_HDU;
 
       /* Make sure the output name is set. */
diff --git a/bin/fits/ui.h b/bin/fits/ui.h
index e2668c8..be7fccf 100644
--- a/bin/fits/ui.h
+++ b/bin/fits/ui.h
@@ -43,7 +43,7 @@ enum program_args_groups
 
 /* Available letters for short options:
 
-   b e f g i j l m n s v x y z
+   b e f g i j l m s v x y z
    A B E G J L O W X Y
  */
 enum option_keys_enum
@@ -52,6 +52,7 @@ enum option_keys_enum
   UI_KEY_REMOVE       = 'R',
   UI_KEY_COPY         = 'C',
   UI_KEY_CUT          = 'k',
+  UI_KEY_NUMHDUS      = 'n',
   UI_KEY_PRINTALLKEYS = 'p',
   UI_KEY_ASIS         = 'a',
   UI_KEY_DELETE       = 'd',
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index e490bac..e3bc4c8 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -9862,6 +9862,17 @@ HDUs given to this option (see @ref{Automatic output}).
 
 @table @option
 
address@hidden -n
address@hidden --numhdus
+Print the number of extensions/HDUs in the given file. Note that this
+option must be called alone and will only print a single number. It is thus
+useful in scripts, for example when you need to do check the number of
+extensions in a FITS file.
+
+For a complete list of basic meta-data on the extensions in a FITS file,
+don't use any of the options in this section or in @ref{Keyword
+manipulation}. For more, see @ref{Invoking astfits}.
+
 @item -C STR
 @itemx --copy=STR
 Copy the specified extension into the output file, see explanations above.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]