gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master b19eedd6: ConvertType and Crop: the 0-th exten


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master b19eedd6: ConvertType and Crop: the 0-th extension includes all metadata
Date: Mon, 1 Jan 2024 13:52:23 -0500 (EST)

branch: master
commit b19eedd693c72d524b87ebcad74bf4cf231a9ee3
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    ConvertType and Crop: the 0-th extension includes all metadata
    
    Until now, ConvertType didn't write any metadata in its output FITS files
    and Crop would write its metadata in the HDU that contained data. This was
    contrary to the new idea that Gnuastro's data should only have WCS metadata
    (which is necessary to locate it on the sky). Other metadata should be
    written in the 0-th.
    
    With this commit, both issues have been addressed.
---
 NEWS                           |  5 +++
 bin/convertt/convertt.c        |  1 +
 bin/convertt/ui.c              |  4 ++
 bin/crop/onecrop.c             | 98 ++++++++++++++++++++++++------------------
 bin/script/psf-scale-factor.sh |  2 +-
 bin/script/psf-stamp.sh        |  2 +-
 doc/gnuastro.texi              |  2 +-
 7 files changed, 68 insertions(+), 46 deletions(-)

diff --git a/NEWS b/NEWS
index c44a3eb5..19246af7 100644
--- a/NEWS
+++ b/NEWS
@@ -179,6 +179,11 @@ See the end of the file for license conditions.
     Sepideh Eskandarlou and implemented by Faezeh Bidjarchian after a poll
     on Gnuastro's Matrix chat channel (#gnuastro:openastronomy.org).
 
+*** Crop
+  - The 'ICF*' keywords are now written in the 0-th HDU of Crop's outputs
+    (the extension with no data, only metadata). Therefore, the WCS is now
+    the only metadata in the HDU containing data.
+
 *** Library
   - gal_fits_key_write: to generalize, the "title" argument has been
     removed (because it is a keyword). It can also create the file if it
diff --git a/bin/convertt/convertt.c b/bin/convertt/convertt.c
index f1782b8d..71f71ac1 100644
--- a/bin/convertt/convertt.c
+++ b/bin/convertt/convertt.c
@@ -331,6 +331,7 @@ convertt(struct converttparams *p)
     case OUT_FORMAT_FITS:
       if(p->numch==3 && p->rgbtohsv)
         color_rgb_to_hsv(p);
+      gal_fits_key_write(p->cp.ckeys, p->cp.output, "0", "NONE", 1, 1);
       for(channel=p->chll; channel!=NULL; channel=channel->next)
         gal_fits_img_write(channel, p->cp.output, NULL, 0);
       break;
diff --git a/bin/convertt/ui.c b/bin/convertt/ui.c
index 97ad8c47..4d7a267a 100644
--- a/bin/convertt/ui.c
+++ b/bin/convertt/ui.c
@@ -1791,6 +1791,10 @@ ui_read_check_inputs_setup(int argc, char *argv[], 
struct converttparams *p)
   gal_options_print_state(&p->cp);
 
 
+  /* Prepare all the options as FITS keywords to write in output later. */
+  gal_options_as_fits_keywords(&p->cp);
+
+
   /* Check that the options and arguments fit well with each other. Note
      that arguments don't go in a configuration file. So this test should
      be done after (possibly) printing the option values. */
diff --git a/bin/crop/onecrop.c b/bin/crop/onecrop.c
index a9a71167..cc3de79c 100644
--- a/bin/crop/onecrop.c
+++ b/bin/crop/onecrop.c
@@ -477,7 +477,8 @@ onecrop_flpixel(struct onecropparams *crp)
                     status, wcs_errmsg[status]);
 
           /* Find the first and last pixels of this crop. */
-          gal_box_border_from_center(pixcrd, ndim, p->iwidth, fpixel, lpixel);
+          gal_box_border_from_center(pixcrd, ndim, p->iwidth, fpixel,
+                                     lpixel);
         }
       break;
 
@@ -503,6 +504,54 @@ onecrop_flpixel(struct onecropparams *crp)
 
 
 
+static void
+one_crop_make_array_0th(struct onecropparams *crp, char *outname,
+                        long *fpixel_i, long *lpixel_i, long *naxes)
+{
+  struct cropparams *p=crp->p;
+
+  int status=0;
+  fitsfile *fptr;
+  char basekeyname[FLEN_KEYWORD-5];
+  size_t i, j=0, ndim=p->imgs->ndim;
+  struct inputimgs *img=&p->imgs[crp->in_ind];
+  char region[FLEN_VALUE], regionkey[FLEN_KEYWORD];
+
+  /* Create the file (after this function returns, the rest assumes that
+     the file is ready). */
+  if(p->primaryimghdu)
+    {
+      if(fits_create_file(&fptr, outname, &status))
+        gal_fits_io_error(status, "creating file");
+      fits_close_file(fptr, &status);
+    }
+  else
+    {
+      /* Write the selected region of this image as a string to include
+         as a FITS keyword. Then we want to delete the last coma ','.*/
+      for(i=0;i<ndim;++i)
+        j += sprintf(&region[j], "%ld:%ld,", fpixel_i[i], lpixel_i[i]);
+      region[j-1]='\0';
+
+      /* Add the specific cropping parameters. */
+      gal_fits_key_list_title_add_end(&p->cp.ckeys, "Crop metadata", 0);
+      sprintf(basekeyname, "ICF%zu", crp->numimg);
+      gal_fits_key_write_filename(basekeyname, img->name, &p->cp.ckeys,
+                                  0, p->cp.quiet);
+      sprintf(regionkey, "%sPIX", basekeyname);
+      gal_fits_key_list_add_end(&p->cp.ckeys, GAL_TYPE_STRING, regionkey,
+                                0, region, 0, "Range of pixels used "
+                                "for this output.", 0, NULL, 0);
+
+      /* Write the 0-th HDU. */
+      gal_fits_key_write(p->cp.ckeys, outname, "0", "NONE", 0, 1);
+    }
+}
+
+
+
+
+
 
 /* Find the size of the final FITS image (irrespective of how many
    crops will be needed for it) and make the image to keep the
@@ -541,6 +590,7 @@ onecrop_make_array(struct onecropparams *crp, long 
*fpixel_i,
     for(i=0;i<ndim;++i)
       naxes[i] = crp->lpixel[i]-crp->fpixel[i]+1;
 
+
   /* Create the FITS file with a blank first extension, then close it, so
      with the next 'fits_open_file', we build the image in the second
      extension. But only if the user didn't want the append the crop to an
@@ -548,16 +598,7 @@ onecrop_make_array(struct onecropparams *crp, long 
*fpixel_i,
      for Gnuastro's outputs, we can consistently use '-h1' (something like
      how you count columns, or generally everything from 1). */
   if(p->append==0 || gal_checkset_check_file_return(outname)==0)
-    {
-      if(fits_create_file(&ofp, outname, &status))
-        gal_fits_io_error(status, "creating file");
-      if(p->primaryimghdu==0)
-        {
-          fits_create_img(ofp, SHORT_IMG, 0, naxes, &status);
-          gal_fits_key_write(p->cp.ckeys, outname, "0", "NONE", 0, 0);
-        }
-      fits_close_file(ofp, &status);
-    }
+    one_crop_make_array_0th(crp, outname, fpixel_i, lpixel_i, naxes);
 
 
   /* Create the output crop image. */
@@ -629,12 +670,6 @@ onecrop_make_array(struct onecropparams *crp, long 
*fpixel_i,
           gal_fits_io_error(status, NULL);
         }
     }
-
-
-  /* Add the Crop information title, the actual keywords will be written
-     later. The keywords will contain the pixels of the is cropped region
-     from input image(s): */
-  gal_fits_key_write_title_in_ptr("Crop information", ofp);
 }
 
 
@@ -662,11 +697,8 @@ onecrop(struct onecropparams *crp)
   char *stdoutstring;
   int status=0, anynul=0;
   int returnvalue=1, hasoneelem=1;
-  fitsfile *ifp=crp->infits, *ofp;
-  char basekeyname[FLEN_KEYWORD-5];     /* '-5': avoid gcc 8.1+ warnings! */
-  gal_fits_list_key_t *headers=NULL;    /* See above comment for more.    */
-  size_t i, j, cropsize=1, ndim=img->ndim;
-  char region[FLEN_VALUE], regionkey[FLEN_KEYWORD];
+  fitsfile *ifp=crp->infits, *ofp;      /* '-5': avoid gcc 8.1+ warnings! */
+  size_t i, cropsize=1, ndim=img->ndim; /* See above comment for more.    */
   long fpixel_o[MAXDIM], lpixel_o[MAXDIM], inc[MAXDIM];
   long naxes[MAXDIM], fpixel_i[MAXDIM], lpixel_i[MAXDIM];
 
@@ -743,31 +775,11 @@ onecrop(struct onecropparams *crp)
          single element and the user asked for it). */
       if(crp->outfits)
         {
-          /* Write the array into the image. */
+          /* Write the array into the file. */
           status=0;
           if( fits_write_subset(ofp, gal_fits_type_to_datatype(p->type),
                                 fpixel_o, lpixel_o, array, &status) )
             gal_fits_io_error(status, NULL);
-
-
-          /* Write the selected region of this image as a string to include
-             as a FITS keyword. Then we want to delete the last coma ','.*/
-          j=0;
-          for(i=0;i<ndim;++i)
-            j += sprintf(&region[j], "%ld:%ld,", fpixel_i[i], lpixel_i[i]);
-          region[j-1]='\0';
-
-
-          /* A section has been added to the cropped image from this input
-             image, so save the information of this image. */
-          sprintf(basekeyname, "ICF%zu", crp->numimg);
-          gal_fits_key_write_filename(basekeyname, img->name, &headers, 0,
-                                      p->cp.quiet);
-          sprintf(regionkey, "%sPIX", basekeyname);
-          gal_fits_key_list_add_end(&headers, GAL_TYPE_STRING, regionkey,
-                                    0, region, 0, "Range of pixels used "
-                                    "for this output.", 0, NULL, 0);
-          gal_fits_key_write_in_ptr(headers, ofp, 1);
         }
 
 
diff --git a/bin/script/psf-scale-factor.sh b/bin/script/psf-scale-factor.sh
index 492ae948..07804b85 100644
--- a/bin/script/psf-scale-factor.sh
+++ b/bin/script/psf-scale-factor.sh
@@ -676,7 +676,7 @@ if [ $nocentering = 0 ]; then
 
     # Read the overlap range from the 'ICF1PIX' keyword (which is printed
     # in all outputs of Crop).
-    overlaprange=$(astfits $cropped -h1 --keyvalue=ICF1PIX --quiet \
+    overlaprange=$(astfits $cropped -h0 --keyvalue=ICF1PIX --quiet \
                            | sed -e's|:| |g' -e's|,| |')
 
     # Calculate the position of the bottom-left pixel of the cropped image
diff --git a/bin/script/psf-stamp.sh b/bin/script/psf-stamp.sh
index 5769c8ee..f9c96a14 100644
--- a/bin/script/psf-stamp.sh
+++ b/bin/script/psf-stamp.sh
@@ -778,7 +778,7 @@ if [ $nocentering = 0 ]; then
 
     # Read the overlap range from the 'ICF1PIX' keyword (which is printed
     # in all outputs of Crop).
-    overlaprange=$(astfits $cropped -h1 --keyvalue=ICF1PIX -q \
+    overlaprange=$(astfits $cropped -h0 --keyvalue=ICF1PIX -q \
                        | sed -e's|:| |g' -e's|,| |')
 
     # Calculate the position of the bottom-left pixel of the cropped image
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 3001ac36..ae73ef57 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -20547,7 +20547,7 @@ However, if you want the cropped data to be written 
into the primary (0-th) HDU,
 If the output file already exists by default Crop will re-write it (so that 
all existing HDUs in it will be deleted).
 If you want the cropped HDU to be appended to existing HDUs, use 
@option{--append} described below.
 
-The header of each output cropped image will contain the names of the input 
image(s) it was cut from.
+The 0-th HDU of each output cropped image will contain the names of the input 
image(s) it was cut from.
 If a name is longer than the 70 character space that the FITS standard allows 
for header keyword values, the name will be cut into several keywords from the 
nearest slash (@key{/}).
 The keywords have the following format: @command{ICFn_m} (for Crop File).
 Where @command{n} is the number of the image used in this crop and @command{m} 
is the part of the name (it can be broken into multiple keywords).



reply via email to

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