gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 784b220: Fits: --primaryimghdu allows copying/


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 784b220: Fits: --primaryimghdu allows copying/cutting to zero-th extension
Date: Sun, 9 Jun 2019 11:47:58 -0400 (EDT)

branch: master
commit 784b22078c09103c34815413787dce13e82a2296
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Fits: --primaryimghdu allows copying/cutting to zero-th extension
    
    Until now, it wasn't easy to copy a requested extension to the zero-th
    extension of a FITS file. With this commit, the Fits program has a new
    `----primaryimghdu' option which will copy the requested image HDU into the
    zero-th extension of an output file that doesn't yet exist.
    
    This task was suggested by Zahra Bagheri.
---
 NEWS                         |  4 ++++
 THANKS                       |  1 +
 bin/fits/args.h              | 13 +++++++++++
 bin/fits/fits.c              | 54 ++++++++++++++++++++++++++++++++++++++++----
 bin/fits/main.h              |  1 +
 bin/fits/ui.h                |  1 +
 doc/announce-acknowledge.txt |  1 +
 doc/gnuastro.texi            | 19 ++++++++++++----
 8 files changed, 85 insertions(+), 9 deletions(-)

diff --git a/NEWS b/NEWS
index 5fc250c..a2aa627 100644
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,10 @@ See the end of the file for license conditions.
    --usedredshift: Print the used redshift as a "Specific calculation" (in
      line with other single-valued calculations).
 
+  Fits:
+   --primaryimghdu: Copy/cut the given image HDU to the zero-th/first
+     HDU of the output file that doesn't yet exist.
+
   Statistics:
    --sigclip-number, --sigclip-median, --sigclip-mean, --sigclip-std: Do
      sigma-clipping and only print the desired value as a single-value
diff --git a/THANKS b/THANKS
index bfe6ab9..45074bc 100644
--- a/THANKS
+++ b/THANKS
@@ -23,6 +23,7 @@ support in Gnuastro. The list is ordered alphabetically (by 
family name).
     Hamed Altafi                         address@hidden
     Roland Bacon                         address@hidden
     Roberto Baena Gallé                  address@hidden
+    Zahra Bagheri                        address@hidden
     Karl Berry                           address@hidden
     Leindert Boogaard                    address@hidden
     Nicolas Bouché                       address@hidden
diff --git a/bin/fits/args.h b/bin/fits/args.h
index 5f3d899..b4c6584 100644
--- a/bin/fits/args.h
+++ b/bin/fits/args.h
@@ -89,6 +89,19 @@ struct argp_option program_options[] =
       GAL_OPTIONS_NOT_MANDATORY,
       GAL_OPTIONS_NOT_SET
     },
+    {
+      "primaryimghdu",
+      UI_KEY_PRIMARYIMGHDU,
+      0,
+      0,
+      "Copy/cut image HDUs to primary/zero-th HDU.",
+      UI_GROUP_EXTENSION,
+      &p->primaryimghdu,
+      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 432bbbd..11b3d3b 100644
--- a/bin/fits/fits.c
+++ b/bin/fits/fits.c
@@ -25,6 +25,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <errno.h>
 #include <error.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <gnuastro/list.h>
 #include <gnuastro/fits.h>
@@ -54,7 +55,7 @@ fits_has_error(struct fitsparams *p, int actioncode, char 
*string, int status)
     case FITS_ACTION_UPDATE:        action="updated";      break;
     case FITS_ACTION_WRITE:         action="written";      break;
     case FITS_ACTION_COPY:          action="copied";       break;
-    case FITS_ACTION_REMOVE:        action="renoved";      break;
+    case FITS_ACTION_REMOVE:        action="removed";      break;
 
     default:
       error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at `%s' so we "
@@ -299,16 +300,53 @@ fits_hdu_remove(struct fitsparams *p, int *r)
 
 
 
+/* This is similar to the library's `gal_fits_open_to_write', except that
+   it won't create an empty first extension.*/
+fitsfile *
+fits_open_to_write_no_blank(char *filename)
+{
+  int status=0;
+  fitsfile *fptr;
+
+  /* When the file exists just open it. Otherwise, create the file. But we
+     want to leave the first extension as a blank extension and put the
+     image in the next extension to be consistent between tables and
+     images. */
+  if(access(filename,F_OK) == -1 )
+    {
+      /* Create the file. */
+      if( fits_create_file(&fptr, filename, &status) )
+        gal_fits_io_error(status, NULL);
+    }
+
+  /* Open the file, ready for later steps. */
+  if( fits_open_file(&fptr, filename, READWRITE, &status) )
+    gal_fits_io_error(status, NULL);
+
+  /* Return the pointer. */
+  return fptr;
+}
+
+
+
+
+
 static void
 fits_hdu_copy(struct fitsparams *p, int cut1_copy0, int *r)
 {
   char *hdu;
-  fitsfile *in, *out;
   int status=0, hdutype;
+  fitsfile *in, *out=NULL;
   gal_list_str_t *list = cut1_copy0 ? p->cut : p->copy;
 
-  /* Open the output file. */
-  out=gal_fits_open_to_write(p->cp.output);
+  /* Open the output file.
+
+############################################# USE this after checking the
+nature of the first extension to be copied. If its a table, use the
+library's function.  #############################################
+
+*/
+
 
   /* Copy all the given extensions. */
   while(list)
@@ -320,6 +358,14 @@ fits_hdu_copy(struct fitsparams *p, int cut1_copy0, int *r)
       in=gal_fits_hdu_open(p->filename, hdu,
                            cut1_copy0 ? READWRITE : READONLY);
 
+      /* If the output isn't opened yet, open it.  */
+      if(out==NULL)
+        out = ( ( gal_fits_hdu_format(p->filename, hdu)==IMAGE_HDU
+                  && p->primaryimghdu )
+                ? fits_open_to_write_no_blank(p->cp.output)
+                : gal_fits_open_to_write(p->cp.output) );
+
+
       /* Copy to the extension. */
       if( fits_copy_hdu(in, out, 0, &status) )
         *r=fits_has_error(p, FITS_ACTION_COPY, hdu, status);
diff --git a/bin/fits/main.h b/bin/fits/main.h
index 460a067..6b50be2 100644
--- a/bin/fits/main.h
+++ b/bin/fits/main.h
@@ -61,6 +61,7 @@ struct fitsparams
   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   primaryimghdu;     /* Copy/cut HDU into primary HDU.        */
   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.                */
diff --git a/bin/fits/ui.h b/bin/fits/ui.h
index 229cc61..4249fb4 100644
--- a/bin/fits/ui.h
+++ b/bin/fits/ui.h
@@ -72,6 +72,7 @@ enum option_keys_enum
   UI_KEY_TITLE        = 1000,
   UI_KEY_OUTHDU,
   UI_KEY_COPYKEYS,
+  UI_KEY_PRIMARYIMGHDU,
 };
 
 
diff --git a/doc/announce-acknowledge.txt b/doc/announce-acknowledge.txt
index 1c554bd..431c8a0 100644
--- a/doc/announce-acknowledge.txt
+++ b/doc/announce-acknowledge.txt
@@ -1,6 +1,7 @@
 Alphabetically ordered list to acknowledge in the next release.
 
 Hamed Altafi
+Zahra Bagheri
 Leindert Boogaard
 Bruno Haible
 Raul Infante-Sainz
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 31db2e4..bfb1742 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -10258,15 +10258,24 @@ output file, see explanations above.
 
 @item -R STR
 @itemx --remove=STR
-Remove the specified HDU from the input file. From CFITSIO: ``In the case
-of deleting the primary array (the first HDU in the file) then [it] will be
-replaced by a null primary array containing the minimum set of required
-keywords and no data.''. So in practice, any existing data (array) and
-meta-data in the first extension will be removed, but the number of
+Remove the specified HDU from the input file.
+
+The first (zero-th) HDU cannot be removed with this option. Consider using
+@option{--copy} or @option{--cut} in combination with
+@option{primaryimghdu} to not have an empty zero-th HDU. From CFITSIO: ``In
+the case of deleting the primary array (the first HDU in the file) then
+[it] will be replaced by a null primary array containing the minimum set of
+required keywords and no data.''. So in practice, any existing data (array)
+and meta-data in the first extension will be removed, but the number of
 extensions in the file won't change. This is because of the unique position
 the first FITS extension has in the FITS standard (for example it cannot be
 used to store tables).
 
+@item --primaryimghdu
+Copy or cut an image HDU to the zero-th HDU/extension a file that doesn't
+yet exist. This option is thus irrelevant if the output file already exists
+or the copied/cut extension is a FITS table.
+
 @end table
 
 



reply via email to

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