gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 3803ffe8: Fits: units of a HDU are also printe


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 3803ffe8: Fits: units of a HDU are also printed when no option given
Date: Thu, 30 Jun 2022 12:48:47 -0400 (EDT)

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

    Fits: units of a HDU are also printed when no option given
    
    Until now, when no option was given, the table printed by the Fits program
    (that contains metadata about the HDUs) didn't show the units metadata! But
    the FITS standard does specify a special keyword for units ('BUNIT'), and
    it is commonly available from some datasets.
    
    With this commit, a new units column has been added for this scenario. HDUs
    that contain arrays (like 2D images or 3D cubes; not tables), and have the
    'BUNIT' keyword, will have a non-blank value in this column.
---
 NEWS            |  4 ++++
 bin/fits/fits.c | 51 ++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/NEWS b/NEWS
index 075de726..7a0c9fa0 100644
--- a/NEWS
+++ b/NEWS
@@ -68,6 +68,10 @@ See the end of the file for license conditions.
      Raul Infante-Sainz.
 
   Fits:
+   - When no option is specified (thus printing the HDU metadata in the
+     given file), the units of the HDUs are also printed as a new
+     column. For tables, it will be blank ('n/a'); because tables have
+     separate units on each column (use 'asttable table.fits -i').
    --copykeys: can now take any number of keyword names also. For example
      '--copykeys=KEYNAME1,KEYNAME2,KEYNAME3'. Until now, it was only
      possible to give this option a range of keyword positions (for example
diff --git a/bin/fits/fits.c b/bin/fits/fits.c
index b6d23bfa..91aa4c16 100644
--- a/bin/fits/fits.c
+++ b/bin/fits/fits.c
@@ -88,10 +88,12 @@ fits_print_extension_info(struct fitsparams *p)
 {
   uint16_t *ui16;
   fitsfile *fptr;
+  gal_data_t *unitscol;
+  char bunit[FLEN_VALUE];
   size_t i, numext, *dsize, ndim;
-  int hascomments=0, hasblankname=0;
-  char **tstra, **estra, **sstra, **cmstra;
   int j, nc, numhdu, hdutype, status=0, type;
+  char **tstra, **estra, **sstra, **cmstra, **ustra;
+  int hascomments=0, hasblankname=0, hasblankunits=0;
   gal_data_t *tmp, *cols=NULL, *sizecol, *commentscol;
   char *msg, *tstr=NULL, sstr[1000], extname[FLEN_VALUE];
 
@@ -114,6 +116,8 @@ fits_print_extension_info(struct fitsparams *p)
      linked list). */
   gal_list_data_add_alloc(&cols, NULL, GAL_TYPE_STRING, 1, &numext, NULL, 1,
                           -1, 1, "HDU_COMMENT", "note", "Possible comment");
+  gal_list_data_add_alloc(&cols, NULL, GAL_TYPE_STRING, 1, &numext, NULL, 1,
+                          -1, 1, "HDU_UNITS", "units", "Units of the pixels");
   gal_list_data_add_alloc(&cols, NULL, GAL_TYPE_STRING, 1, &numext, NULL, 1,
                           -1, 1, "HDU_SIZE", "name", "Size of image or table "
                           "number of rows and columns.");
@@ -136,7 +140,10 @@ fits_print_extension_info(struct fitsparams *p)
   sizecol = cols->next->next->next;
   sstra = sizecol->array;
 
-  commentscol = cols->next->next->next->next;
+  unitscol = cols->next->next->next->next;
+  ustra = unitscol->array;
+
+  commentscol = cols->next->next->next->next->next;
   cmstra = commentscol->array;
 
   cols->next->disp_width=15;
@@ -189,6 +196,31 @@ fits_print_extension_info(struct fitsparams *p)
       status=0;
 
 
+      /* Read the extension units (only for images). */
+      if(hdutype==IMAGE_HDU)
+        fits_read_keyword(fptr, "BUNIT", bunit, NULL, &status);
+      switch(status)
+        {
+        case 0:
+          /* For tables, we don't bother reading 'BUNIT', because it is
+             meaningless even if it exists (each colum has its separate
+             units). So 'status' will be zero in this case.*/
+          if(hdutype==IMAGE_HDU)
+            gal_fits_key_clean_str_value(bunit);
+          else /* Its a table HDU. */
+            { sprintf(bunit, "%s", GAL_BLANK_STRING); hasblankunits=1; }
+          break;
+
+        case KEY_NO_EXIST:
+          sprintf(bunit, "%s", GAL_BLANK_STRING); hasblankunits=1;
+          break;
+
+        default:
+          gal_fits_io_error(status, "reading BUNIT keyword");
+        }
+      status=0;
+
+
       /* Check if its a healpix grid. */
       if( gal_fits_hdu_is_healpix(fptr) )
         {
@@ -229,6 +261,7 @@ fits_print_extension_info(struct fitsparams *p)
             case 1: gal_checkset_allocate_copy(extname, estra+i); break;
             case 2: gal_checkset_allocate_copy(tstr, tstra+i);    break;
             case 3: gal_checkset_allocate_copy(sstr, sstra+i);    break;
+            case 4: gal_checkset_allocate_copy(bunit, ustra+i);   break;
             }
           ++j;
         }
@@ -249,7 +282,7 @@ fits_print_extension_info(struct fitsparams *p)
   /* If there weren't any comments, don't print the comment column. */
   if(hascomments==0)
     {
-      sizecol->next=NULL;
+      unitscol->next=NULL;
       gal_data_free(commentscol);
     }
 
@@ -262,13 +295,17 @@ fits_print_extension_info(struct fitsparams *p)
       printf(" Column 2: Name ('EXTNAME' in FITS standard, usable with "
              "'--hdu').\n");
       if(hasblankname)
-        printf("           ('%s' means that no name is specified for this "
-               "HDU)\n", GAL_BLANK_STRING);
+        printf("           ('%s': no name in HDU metadata)\n",
+               GAL_BLANK_STRING);
       printf(" Column 3: Image data type or 'table' format (ASCII or "
              "binary).\n");
       printf(" Column 4: Size of data in HDU.\n");
+      printf(" Column 5: Units of data in HDU (only images, for tables use 
'asttable -i').\n");
+      if(hasblankunits)
+        printf("           ('%s': no unit in HDU metadata, or is "
+               "HDU is a table)\n", GAL_BLANK_STRING);
       if(hascomments)
-        printf(" Column 5: Comments about the HDU (e.g., if its HEALpix, or "
+        printf(" Column 6: Comments about the HDU (e.g., if its HEALpix, or "
                "etc).\n");
       printf("-----\n");
     }



reply via email to

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