gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master ce19153 5/7: Type of output can be specified i


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master ce19153 5/7: Type of output can be specified in MakeProfiles
Date: Tue, 16 Aug 2016 14:30:38 +0000 (UTC)

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

    Type of output can be specified in MakeProfiles
    
    MakeProfiles builds the profiles internally as a float array. Until now it
    would also save the final output image in the same type unless the
    background image had a different type. With the added capabilities to
    specify the flat profile's pixel values, it is now necessary for the output
    to have a certain data type (based on the flat values you wanted), for
    example when making labels to be used in MakeCatalog, all the pixels have
    integer values, and MakeCatalog also expects an integer type (int, short,
    or long) for the labeled image.
    
    With this commit, there is a new `--type' (`-T') option which allows the
    user to set the output file type. Its values are given as a string of
    standard types for easy typing on the commandline or in the configuration
    files.
    
    Given the separation of user-interface functions (like this) from the main
    processing functions, implementing this new option is conceptually easy:
    mainly to do with the `args.h' and `ui.c' files. The value given by the
    user is immediately checked against the acceptable values and saved in the
    `uiparams' structure as a CFITSIO BITPIX keyword value. If necessary it is
    later copied to the `p->bitpix' variable which determines the type of the
    output. With this more generic solution, the procedure to write the
    function in the `write' function of `mkprof.c' also got much more simpler.
    
    MakeProfile's `inputascanvas' test was also modified to save its value as a
    long type to test this option.
    
    Two other minor issues are corrected with this commit:
    
      - The replace option was only read from the command-line, not the
        configuration files.
    
      - When printing the value of the `--mforflatpix' option, it would
        mistakenly print the value to `--tcol'.
---
 doc/gnuastro.texi             |   23 ++++++++++---
 src/mkprof/args.h             |   14 +++++++-
 src/mkprof/astmkprof.conf     |    1 +
 src/mkprof/main.h             |    7 ++--
 src/mkprof/mkprof.c           |   30 ++++++-----------
 src/mkprof/ui.c               |   74 ++++++++++++++++++++++++++++++++---------
 src/mkprof/ui.h               |    3 ++
 tests/mkprof/inputascanvas.sh |    2 +-
 8 files changed, 111 insertions(+), 43 deletions(-)

diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 4613ffd..fd66d6c 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -12436,6 +12436,20 @@ option is ignored.
 second FITS axis (vertical when viewed in SAO ds9), see the
 explanation for @option{--naxis1}.
 
address@hidden -T
address@hidden --type
+(@option{=STR}) Type of the output image pixels (specified by @code{BITPIX}
+in the FITS standard). It can have any one of the following values:
address@hidden, @option{short}, @option{long}, @option{longlong},
address@hidden, @option{double}. Internally, the images are made in the
+float type, but upon writing of the single output (not individual
+profiles), the image will be converted to the type specified by this
+option.
+
+When a background image is given without the @option{--inputascanvas}
+option, it is assumed that you want the output in the same format as the
+background, therefore the value to this option is ignored.
+
 @item -C
 @itemx --inputascanvas
 When an image is given as an argument, use the input image as the canvas to
@@ -12443,10 +12457,11 @@ make all the mock profiles. In practice, this option 
only changes the
 non-blank pixels (see @ref{Blank pixels}) of the input image (in memory
 after reading, not the actual input file) to zero before making the
 profiles. The profiles are then built on those cleared pixels. With this
-option (like when building on a background image) none of the
address@hidden', address@hidden', address@hidden',
address@hidden', address@hidden', address@hidden', and
address@hidden' are necessary.
+option (like when building on a background image) the values to
address@hidden, @option{--naxis2}, @option{--crpix1}, @option{--crpix2},
address@hidden, @option{--crval2}, and @option{--resolution} are
+ignored. However, if @option{--type} is given, its value will take
+precedence over the background image type.
 
 This option can be very useful when the mock image is to be made over a
 real sky region and compared to a real image (that might have blank
diff --git a/src/mkprof/args.h b/src/mkprof/args.h
index 209d100..1d97426 100644
--- a/src/mkprof/args.h
+++ b/src/mkprof/args.h
@@ -61,7 +61,7 @@ const char doc[] =
    Available letters (-V which is used by GNU is also removed):
 
    a d f g j k l u v
-   A B E G H I J L M O Q T U W Z
+   A B E G H I J L M O Q U W Z
 
    Maximum integer used so far: 517
 */
@@ -146,6 +146,14 @@ static struct argp_option options[] =
       "Do not create a merged image of all profiles.",
       2
     },
+    {
+      "type",
+      'T',
+      "STR",
+      0,
+      "byte, short, long, longlong, float, double.",
+      2
+    },
 
 
 
@@ -492,6 +500,10 @@ parse_opt(int key, char *arg, struct argp_state *state)
       break;
     case 'R':
       p->replace=1;
+      p->up.replaceset=1;
+      break;
+    case 'T':
+      checksaveouttype(p, arg);
       break;
 
     /* Profiles: */
diff --git a/src/mkprof/astmkprof.conf b/src/mkprof/astmkprof.conf
index 957e2aa..7b5aee2 100644
--- a/src/mkprof/astmkprof.conf
+++ b/src/mkprof/astmkprof.conf
@@ -25,6 +25,7 @@
  naxis2             1000
  oversample            5
  circumwidth           2
+ type              float
 
 # Profiles:
  tunitinp              0
diff --git a/src/mkprof/main.h b/src/mkprof/main.h
index 7c2bb9a..b56a6ba 100644
--- a/src/mkprof/main.h
+++ b/src/mkprof/main.h
@@ -31,8 +31,8 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 
 /* Progarm name macros: */
-#define SPACK           "astmkprof" /* Subpackage executable name. */
-#define SPACK_NAME      "MakeProfiles"     /* Subpackage full name.       */
+#define SPACK           "astmkprof"      /* Subpackage executable name. */
+#define SPACK_NAME      "MakeProfiles"   /* Subpackage full name.       */
 #define SPACK_STRING    SPACK_NAME" ("PACKAGE_NAME") "PACKAGE_VERSION
 #define LOGFILENAME     SPACK".log"
 #define LOGNUMCOLS      5
@@ -85,6 +85,7 @@ struct builtqueue
 
 struct uiparams
 {
+  int               type;  /* User's desired output bitpix.            */
   char         *backname;  /* Name of background image file name.      */
   char          *backhdu;  /* HDU of background image.                 */
   char          *catname;  /* Name of catalog of parameters.           */
@@ -101,6 +102,8 @@ struct uiparams
   int          naxis2set;
   int      oversampleset;
   int     circumwidthset;
+  int         replaceset;
+  int            typeset;
   int   inputascanvasset;
 
   int        tunitinpset;
diff --git a/src/mkprof/mkprof.c b/src/mkprof/mkprof.c
index e56c8a5..285c924 100644
--- a/src/mkprof/mkprof.c
+++ b/src/mkprof/mkprof.c
@@ -544,32 +544,22 @@ write(struct mkprofparams *p)
       /* Get the current time for verbose output. */
       if(verb) gettimeofday(&t1, NULL);
 
-      /* If the background image had a special file type, MakeProfile's
-         output should also have the same type. So here we check the type
-         and if its not float, we change to that type. */
-      if(p->up.backname)
-        {
-          if(bitpix==FLOAT_IMG)
-            array=out;
-          else
-            gal_fits_change_type(out, FLOAT_IMG, p->naxes[1]*p->naxes[0],
-                                 p->anyblank, &array, bitpix);
-        }
+      /* Make a temporary array of the desired type for writing the
+         output. */
+      if(bitpix==FLOAT_IMG)
+        array=out;
       else
-        {
-          array=out;
-          bitpix=FLOAT_IMG;
-        }
+        gal_fits_change_type(out, FLOAT_IMG, p->naxes[1]*p->naxes[0],
+                             p->anyblank, &array, bitpix);
 
-      /* Write the array inside the output FITS file. */
-      gal_fits_array_to_file(p->mergedimgname, "MockImg on back",
+      /* Write the array to the output FITS file. */
+      gal_fits_array_to_file(p->mergedimgname, "Mock image",
                              bitpix, array, p->naxes[1],
                              p->naxes[0], p->anyblank, p->wcs,
                              NULL, SPACK_STRING);
 
-      /* Free `array' when it was not a simple assignment but an
-         allocation,*/
-      if(p->up.backname && bitpix!=FLOAT_IMG)
+      /* Free `array' if it was allocated separately.*/
+      if(bitpix!=FLOAT_IMG)
         free(array);
 
       /* In verbose mode, print the information. */
diff --git a/src/mkprof/ui.c b/src/mkprof/ui.c
index 7263673..2508923 100644
--- a/src/mkprof/ui.c
+++ b/src/mkprof/ui.c
@@ -66,6 +66,30 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 /**************************************************************/
 /**************       Options and parameters    ***************/
 /**************************************************************/
+/* Check if the value to the `--type' option is recognized,  */
+void
+checksaveouttype(struct mkprofparams *p, char *arg)
+{
+  /* First check if the value is one of the accepted types. */
+  if     (strcmp(arg, "byte")==0)     p->up.type=BYTE_IMG;
+  else if(strcmp(arg, "short")==0)    p->up.type=SHORT_IMG;
+  else if(strcmp(arg, "long")==0)     p->up.type=LONG_IMG;
+  else if(strcmp(arg, "longlong")==0) p->up.type=LONGLONG_IMG;
+  else if(strcmp(arg, "float")==0)    p->up.type=FLOAT_IMG;
+  else if(strcmp(arg, "double")==0)   p->up.type=DOUBLE_IMG;
+  else
+    error(EXIT_FAILURE, 0, "given value of the `--type' (`-T') option "
+          "(`%s') is not recognized. It must be `byte', `short', `long' "
+          "`longlong', `float', or `double'.", arg);
+
+  /* Flag this option as set: */
+  p->up.typeset=1;
+}
+
+
+
+
+
 void
 readconfig(char *filename, struct mkprofparams *p)
 {
@@ -103,6 +127,8 @@ readconfig(char *filename, struct mkprofparams *p)
       if(strcmp(name, "hdu")==0)
         gal_checkset_allocate_copy_set(value, &cp->hdu, &cp->hduset);
 
+
+
       /* Outputs: */
       else if(strcmp(name, "output")==0)
         gal_checkset_allocate_copy_set(value, &cp->output, &cp->outputset);
@@ -137,6 +163,18 @@ readconfig(char *filename, struct mkprofparams *p)
                                     SPACK, filename, lineno);
           up->oversampleset=1;
         }
+      else if(strcmp(name, "replace")==0)
+        {
+          if(up->replaceset) continue;
+          gal_checkset_int_zero_or_one(value, &p->replace, name,
+                                       key, SPACK, filename, lineno);
+          up->replaceset=1;
+        }
+      else if(strcmp(name, "type")==0)
+        {
+          if(p->up.typeset) continue;
+          checksaveouttype(p, value);
+        }
 
 
 
@@ -374,6 +412,10 @@ printvalues(FILE *fp, struct mkprofparams *p)
     fprintf(fp, CONF_SHOWFMT"%d\n", "inputascanvas", up->inputascanvas);
   if(up->oversampleset)
     fprintf(fp, CONF_SHOWFMT"%lu\n", "oversample", p->oversample);
+  if(up->replaceset)
+    fprintf(fp, CONF_SHOWFMT"%d\n", "replace", p->replace);
+  if(up->typeset)
+    fprintf(fp, CONF_SHOWFMT"%s\n", "type", up->type);
 
   fprintf(fp, "\n# Profiles:\n");
   if(up->tunitinpset)
@@ -411,7 +453,7 @@ printvalues(FILE *fp, struct mkprofparams *p)
   if(up->tcolset)
     fprintf(fp, CONF_SHOWFMT"%lu\n", "tcol", p->tcol);
   if(up->mforflatpixset)
-    fprintf(fp, CONF_SHOWFMT"%lu\n", "mforflatpix", p->tcol);
+    fprintf(fp, CONF_SHOWFMT"%d\n", "mforflatpix", p->mforflatpix);
 
   fprintf(fp, "\n# WCS:\n");
   if(up->crpix1set)
@@ -470,11 +512,12 @@ checkifset(struct mkprofparams *p)
     GAL_CONFIGFILES_REPORT_NOTSET("circumwidth");
 
 
-  /* The output image size and WCS are only necessary if the user doesn't
-     want to use the input image's properties or doesn't want to make the
-     profiles on a background image. */
-  if(up->inputascanvasset==0 || p->up.backname)
+  /* The output image size, type, and WCS are only necessary if the user
+     has not provided an input image. */
+  if(p->up.backname==NULL)
     {
+      if(up->typeset==0)
+        GAL_CONFIGFILES_REPORT_NOTSET("type");
       if(up->naxis1set==0)
         GAL_CONFIGFILES_REPORT_NOTSET("naxis1");
       if(up->naxis2set==0)
@@ -801,6 +844,7 @@ preparearrays(struct mkprofparams *p)
   void *array=NULL;
   size_t i, naxes[2];
   double *wcstoimg=NULL;
+  struct uiparams *up=&p->up;
 
   /* Allocate space for the log file: */
   p->log=malloc(p->cs0*LOGNUMCOLS*sizeof *p->log);
@@ -811,13 +855,13 @@ preparearrays(struct mkprofparams *p)
 
   /* If a background image is specified, then use that as the output
      image to build the profiles over. */
-  if(p->up.backname)
+  if(up->backname)
     {
       /* Read the input WCS. */
-      gal_fits_read_wcs(p->up.backname, p->cp.hdu, 0, 0, &p->nwcs, &p->wcs);
+      gal_fits_read_wcs(up->backname, p->cp.hdu, 0, 0, &p->nwcs, &p->wcs);
 
       /* Read in the background image and its coordinates: */
-      p->anyblank=gal_fits_hdu_to_array(p->up.backname, p->cp.hdu,
+      p->anyblank=gal_fits_hdu_to_array(up->backname, p->cp.hdu,
                                         &p->bitpix, &array,
                                         &naxes[1], &naxes[0]);
       p->naxes[1]=naxes[1];
@@ -837,19 +881,19 @@ preparearrays(struct mkprofparams *p)
 
       /* If the user just wanted the headers, then change all non-NaN
          pixels to 0.0f. */
-      if(p->up.inputascanvas)
+      if(up->inputascanvas)
         gal_arraymanip_freplace_nonnans(p->out, naxes[0]*naxes[1], 0.0f);
     }
-  else
-    p->bitpix=FLOAT_IMG;
 
 
-  /* If there is no WCS structure (either no background image given or the
-     background image didn't have any WCS structure), then prepare the WCS
-     based on the given options. */
+  /* Make the WCS structure if it has not been set so far. */
   if(p->wcs==NULL)
     preparewcs(p);
 
+  /* Set the output image type when a background image is not specified,
+     or when inputascanvas is called (with a background image). */
+  if(!up->backname || up->inputascanvas)
+    p->bitpix=up->type;
 
   /* Convert the RA and Dec to X and Y. We will make a temporary array with
      four rows and the same number of columns, then use that array and the
@@ -860,7 +904,7 @@ preparearrays(struct mkprofparams *p)
      the X and Ys and the RA and Decs to be in touching pieces of memory
      and we can't guarantee that (the user might have these columns in
      any order). */
-  if(p->up.racolset)
+  if(up->racolset)
     {
       /* Allocate the space for the temporary array. */
       errno=0;
diff --git a/src/mkprof/ui.h b/src/mkprof/ui.h
index ea047b8..7b5af23 100644
--- a/src/mkprof/ui.h
+++ b/src/mkprof/ui.h
@@ -24,6 +24,9 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #define UI_H
 
 void
+checksaveouttype(struct mkprofparams *p, char *arg);
+
+void
 setparams(int argc, char *argv[], struct mkprofparams *p);
 
 void
diff --git a/tests/mkprof/inputascanvas.sh b/tests/mkprof/inputascanvas.sh
index f49519f..6412902 100755
--- a/tests/mkprof/inputascanvas.sh
+++ b/tests/mkprof/inputascanvas.sh
@@ -50,4 +50,4 @@ if [ ! -f $execname ] || [ ! -f $img ]; then exit 77; fi
 # Actual test script
 # ==================
 $execname $cat $img --mforflatpix --oversample=1 --inputascanvas  \
-          --output="inputascanvas.fits"
+          --type=long --output="inputascanvas.fits"



reply via email to

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