gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 61aec7b 019/113: Convolve name and dimensional


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 61aec7b 019/113: Convolve name and dimensionality checks
Date: Fri, 16 Apr 2021 10:33:34 -0400 (EDT)

branch: master
commit 61aec7bbb82b686b8ebb2cd4c6ee08a99dad1bd1
Author: Mohammad Akhlaghi <akhlaghi@gnu.org>
Commit: Mohammad Akhlaghi <akhlaghi@gnu.org>

    Convolve name and dimensionality checks
    
    The Convolve program wasn't checking the sanity of the input file names and
    the dimensionality of the input. These checks are now implemented in
    `bin/convolve/ui.c'.
    
    Also to help in reading, the spaces in `gal_fits_name_is_fits' were
    adjusted and `gal_fits_suffix_is_fits' was made more simple/efficient by
    avoiding simultaneous checks with and without a dot.
---
 bin/convolve/ui.c | 33 ++++++++++++++++++++++++++++++---
 lib/fits.c        | 31 ++++++++++++++++---------------
 2 files changed, 46 insertions(+), 18 deletions(-)

diff --git a/bin/convolve/ui.c b/bin/convolve/ui.c
index 59f472b..342a045 100644
--- a/bin/convolve/ui.c
+++ b/bin/convolve/ui.c
@@ -220,6 +220,12 @@ ui_read_check_only_options(struct convolveparams *p)
 {
   struct gal_options_common_params *cp=&p->cp;
 
+  /* Make sure the kernel name is a FITS file and a HDU is given. */
+  if( gal_fits_name_is_fits(p->kernelname)==0 )
+    error(EXIT_FAILURE, 0, "`%s' is not a recognized FITS file name",
+          p->kernelname);
+
+
   /* Read the domain from a string into an integer. */
   if( !strcmp("spatial", p->domainstr) )
     p->domain=CONVOLVE_DOMAIN_SPATIAL;
@@ -229,6 +235,7 @@ ui_read_check_only_options(struct convolveparams *p)
     error(EXIT_FAILURE, 0, "domain value `%s' not recognized. Please use "
           "either `spatial' or `frequency'", p->domainstr);
 
+
   /* If we are in the spatial domain, make sure that the necessary
      parameters are set. */
   if( p->domain==CONVOLVE_DOMAIN_SPATIAL )
@@ -259,6 +266,11 @@ ui_check_options_and_arguments(struct convolveparams *p)
      a HDU is also given. */
   if(p->filename==NULL)
     error(EXIT_FAILURE, 0, "no input file is specified");
+
+  /* Make sure the input name is a FITS file name. */
+  if( gal_fits_name_is_fits(p->filename)==0 )
+    error(EXIT_FAILURE, 0, "`%s' is not a recognized FITS file name",
+          p->filename);
 }
 
 
@@ -344,9 +356,16 @@ ui_preparations(struct convolveparams *p)
   p->input->wcs=gal_wcs_read(p->filename, cp->hdu, 0, 0, &p->input->nwcs);
 
 
-  /* See if there are any blank values. */
+  /* Domain specific checks. */
   if(p->domain==CONVOLVE_DOMAIN_FREQUENCY)
     {
+      /* Check the dimensionality. */
+      if(p->input->ndim!=2)
+        error(EXIT_FAILURE, 0, "%s (hdu %s) has %zu dimensions. Frequency "
+              "domain convolution currently only operates on 2D images",
+              p->filename, cp->hdu, p->input->ndim);
+
+      /* Blank values. */
       if( gal_blank_present(p->input, 1) )
         fprintf(stderr, "\n----------------------------------------\n"
                 "######## %s WARNING ########\n"
@@ -361,8 +380,16 @@ ui_preparations(struct convolveparams *p)
                 PROGRAM_NAME);
     }
   else
-    gal_tile_full_sanity_check(p->filename, cp->hdu, p->input, &cp->tl);
-
+    {
+      /* Check the dimensionality. */
+      if(p->input->ndim!=2 && p->input->ndim!=3)
+        error(EXIT_FAILURE, 0, "%s (hdu %s) has %zu dimensions. Spatial "
+              "domain convolution currently only operates on 2D images or "
+              "3D cubes", p->filename, cp->hdu, p->input->ndim);
+
+      /* Tessellation checks. */
+      gal_tile_full_sanity_check(p->filename, cp->hdu, p->input, &cp->tl);
+    }
 
 
   /* Read the file specified by --kernel. If makekernel is specified, then
diff --git a/lib/fits.c b/lib/fits.c
index 6cf7621..f970c0b 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -102,12 +102,12 @@ gal_fits_name_is_fits(char *name)
 {
   size_t len;
   len=strlen(name);
-  if ( ( len>=3 && strcmp(&name[len-3], "fit") == 0 )
-       || ( len>=4 && strcmp(&name[len-4], "fits") == 0 )
-       || ( len>=7 && strcmp(&name[len-7], "fits.gz") == 0 )
-       || ( len>=6 && strcmp(&name[len-6], "fits.Z") == 0 )
-       || ( len>=3 && strcmp(&name[len-3], "imh") == 0 )
-       || ( len>=7 && strcmp(&name[len-7], "fits.fz") == 0 ) )
+  if ( (    len>=3 && strcmp(&name[len-3], "fit"     ) == 0 )
+       || ( len>=4 && strcmp(&name[len-4], "fits"    ) == 0 )
+       || ( len>=7 && strcmp(&name[len-7], "fits.gz" ) == 0 )
+       || ( len>=6 && strcmp(&name[len-6], "fits.Z"  ) == 0 )
+       || ( len>=3 && strcmp(&name[len-3], "imh"     ) == 0 )
+       || ( len>=7 && strcmp(&name[len-7], "fits.fz" ) == 0 ) )
     return 1;
   else
     return 0;
@@ -124,15 +124,16 @@ gal_fits_name_is_fits(char *name)
 int
 gal_fits_suffix_is_fits(char *suffix)
 {
-  if (strcmp(suffix, "fit") == 0        || strcmp(suffix, ".fit") == 0
-      || strcmp(suffix, "fits") == 0    || strcmp(suffix, ".fits") == 0
-      || strcmp(suffix, "fits.gz") == 0 || strcmp(suffix, ".fits.gz") == 0
-      || strcmp(suffix, "fits.Z") == 0  || strcmp(suffix, ".fits.Z") == 0
-      || strcmp(suffix, "imh") == 0     || strcmp(suffix, ".imh") == 0
-      || strcmp(suffix, "fits.fz") == 0 || strcmp(suffix, ".fits.fz") == 0)
-   return 1;
- else
-   return 0;
+  char *nodot = suffix[0]=='.' ? (suffix+1) : suffix;
+  if ( strcmp(   nodot, "fit"     ) == 0
+       || strcmp(nodot, "fits"    ) == 0
+       || strcmp(nodot, "fits.gz" ) == 0
+       || strcmp(nodot, "fits.Z"  ) == 0
+       || strcmp(nodot, "imh"     ) == 0
+       || strcmp(nodot, "fits.fz" ) == 0 )
+    return 1;
+  else
+    return 0;
 }
 
 



reply via email to

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