gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 1a22d5b: Programs: extra dimensions (length 1)


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 1a22d5b: Programs: extra dimensions (length 1) are removed before processing
Date: Sun, 9 Jun 2019 16:19:41 -0400 (EDT)

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

    Programs: extra dimensions (length 1) are removed before processing
    
    Until now, when the input dataset has length-1 dimensions, the programs
    would crash with a message that the number of dimensions isn't
    correct. With this commit, immediately after reading the dataset, all such
    redundant dimensions are removed. Two library functions were added to allow
    easily doing this.
    
    This task was done on a dataset provided by Zahra Bagheri.
---
 NEWS                        |  8 +++++---
 bin/arithmetic/arithmetic.c |  2 ++
 bin/arithmetic/operands.c   | 10 ++++++++--
 bin/arithmetic/ui.c         | 13 +++++++++++++
 bin/arithmetic/ui.h         |  3 +++
 bin/convertt/ui.c           |  2 ++
 bin/convolve/ui.c           | 14 +++++++++++---
 bin/mkcatalog/ui.c          | 13 +++++++++++++
 bin/mknoise/ui.c            |  2 ++
 bin/mkprof/ui.c             | 17 ++++++++++-------
 bin/noisechisel/ui.c        | 10 ++++++++--
 bin/segment/ui.c            | 23 +++++++++++++++++++++--
 bin/statistics/ui.c         | 11 +++++++++--
 bin/warp/ui.c               |  3 +++
 doc/gnuastro.texi           | 21 +++++++++++++++++++++
 lib/dimension.c             | 45 +++++++++++++++++++++++++++++++++++++++++++++
 lib/fits.c                  | 21 +++++++++++++++++++++
 lib/gnuastro/dimension.h    |  8 ++++++++
 lib/gnuastro/fits.h         |  3 +++
 19 files changed, 208 insertions(+), 21 deletions(-)

diff --git a/NEWS b/NEWS
index a2aa627..937f7c1 100644
--- a/NEWS
+++ b/NEWS
@@ -56,12 +56,14 @@ See the end of the file for license conditions.
      for using them in combination with their names.
    - list.h: Functions to return the last element in linked lists. For
      example `gal_list_sizet_last' or `gal_list_data_last'.
-   - gal_list_data_to_array_ptr: Make an array of pointers from the list.
+   - gal_arithmetic_operator_string: Return operator string from code.
+   - gal_arithmetic_set_operator: Return operator code from string.
    - gal_blank_initialize_array: Initialize an array with blank values.
+   - gal_dimension_remove_extra: Remove extra (length 1) dimensions.
+   - gal_list_data_to_array_ptr: Make an array of pointers from the list.
+   - gal_fits_img_info_dim: Only return the size information of a dataset.
    - GAL_BLANK_INT: Blank value for `int' (can be 16-bit or 32-bit).
    - GAL_BLANK_UINT: Blank value for unsigned `int' (can be 16-bit or 32-bit).
-   - gal_arithmetic_operator_string: Return operator string from code.
-   - gal_arithmetic_set_operator: Return operator code from string.
 
 
 ** Removed features
diff --git a/bin/arithmetic/arithmetic.c b/bin/arithmetic/arithmetic.c
index 7c85a94..f0586b4 100644
--- a/bin/arithmetic/arithmetic.c
+++ b/bin/arithmetic/arithmetic.c
@@ -1211,6 +1211,8 @@ reversepolish(struct arithmeticparams *p)
           /* Read the data, note that the WCS has already been set. */
           p->operands->data=gal_array_read_one_ch(filename, hdu, NULL,
                                                   p->cp.minmapsize);
+          data=p->operands->data;
+          data->ndim=gal_dimension_remove_extra(data->ndim, data->dsize, NULL);
           if(!p->cp.quiet) printf(" - %s (hdu %s) is read.\n", filename, hdu);
         }
       else
diff --git a/bin/arithmetic/operands.c b/bin/arithmetic/operands.c
index 577fd93..9b7cceb 100644
--- a/bin/arithmetic/operands.c
+++ b/bin/arithmetic/operands.c
@@ -267,6 +267,7 @@ operands_copy_named(struct arithmeticparams *p, char *name)
 void
 operands_add(struct arithmeticparams *p, char *filename, gal_data_t *data)
 {
+  size_t ndim, *dsize;
   struct operand *newnode;
 
   /* Some operators might not actually return any dataset (data=NULL), in
@@ -305,13 +306,17 @@ operands_add(struct arithmeticparams *p, char *filename, 
gal_data_t *data)
               else
                 newnode->hdu=gal_list_str_pop(&p->hdus);
 
-              /* If no WCS is set yet, use the WCS of this image. */
+              /* If no WCS is set yet, use the WCS of this image and remove
+                 possibly extra dimensions if necessary. */
               if(p->refdata.wcs==NULL)
                 {
+                  dsize=gal_fits_img_info_dim(filename, newnode->hdu, &ndim);
                   p->refdata.wcs=gal_wcs_read(filename, newnode->hdu, 0, 0,
                                               &p->refdata.nwcs);
+                  ndim=gal_dimension_remove_extra(ndim, dsize, p->refdata.wcs);
                   if(p->refdata.wcs && !p->cp.quiet)
                     printf(" - WCS: %s (hdu %s).\n", filename, newnode->hdu);
+                  free(dsize);
                 }
             }
           else newnode->hdu=NULL;
@@ -349,8 +354,9 @@ operands_pop(struct arithmeticparams *p, char *operator)
       hdu=operands->hdu;
       filename=operands->filename;
 
-      /* Read the dataset. */
+      /* Read the dataset and remove possibly extra dimensions. */
       data=gal_array_read_one_ch(filename, hdu, NULL, p->cp.minmapsize);
+      data->ndim=gal_dimension_remove_extra(data->ndim, data->dsize, NULL);
 
       /* Arithmetic changes the contents of a dataset, so the existing name
          (in the FITS `EXTNAME' keyword) should not be passed on beyond
diff --git a/bin/arithmetic/ui.c b/bin/arithmetic/ui.c
index 0ef9a24..f1e39e5 100644
--- a/bin/arithmetic/ui.c
+++ b/bin/arithmetic/ui.c
@@ -357,10 +357,16 @@ ui_check_options_and_arguments(struct arithmeticparams *p)
 static void
 ui_preparations(struct arithmeticparams *p)
 {
+  size_t ndim, *dsize;
+
   /* In case a file is specified to read the WCS from (and ignore input
      datasets), read the WCS prior to starting parsing of the arguments. */
   if(p->wcsfile)
     {
+      /* Read the number of dimensions and the size of each. */
+      dsize=gal_fits_img_info_dim(p->wcsfile, p->wcshdu, &ndim);
+
+      /* Read the WCS. */
       p->refdata.wcs=gal_wcs_read(p->wcsfile, p->wcshdu, 0, 0,
                                   &p->refdata.nwcs);
       if(p->refdata.wcs)
@@ -371,6 +377,13 @@ ui_preparations(struct arithmeticparams *p)
       else
         fprintf(stderr, "WARNING: %s (hdu %s) didn't contain a "
                 "(readable by WCSLIB) WCS.", p->wcsfile, p->wcshdu);
+
+      /* Correct the WCS dimensions if necessary. Note that we don't need
+         the `ndim' or `dsize' any more. */
+      ndim=gal_dimension_remove_extra(ndim, dsize, p->refdata.wcs);
+
+      /* Clean up. */
+      free(dsize);
     }
 }
 
diff --git a/bin/arithmetic/ui.h b/bin/arithmetic/ui.h
index ebd20f8..37bf97d 100644
--- a/bin/arithmetic/ui.h
+++ b/bin/arithmetic/ui.h
@@ -51,6 +51,9 @@ enum option_keys_enum
 void
 ui_read_check_inputs_setup(int argc, char *argv[], struct arithmeticparams *p);
 
+size_t *
+ui_read_ndim_dsize(char *filename, char *hdu, size_t *ndim);
+
 void
 freeandreport(struct arithmeticparams *p, struct timeval *t1);
 
diff --git a/bin/convertt/ui.c b/bin/convertt/ui.c
index a3c3330..145ae5d 100644
--- a/bin/convertt/ui.c
+++ b/bin/convertt/ui.c
@@ -523,6 +523,8 @@ ui_make_channels_ll(struct converttparams *p)
           /* Read in the array and its WCS information. */
           data=gal_fits_img_read(name->v, hdu, p->cp.minmapsize);
           data->wcs=gal_wcs_read(name->v, hdu, 0, 0, &data->nwcs);
+          data->ndim=gal_dimension_remove_extra(data->ndim, data->dsize,
+                                                data->wcs);
           gal_list_data_add(&p->chll, data);
 
           /* A FITS file only has one channel. */
diff --git a/bin/convolve/ui.c b/bin/convolve/ui.c
index 25c7ad1..b80530e 100644
--- a/bin/convolve/ui.c
+++ b/bin/convolve/ui.c
@@ -438,6 +438,9 @@ ui_read_input(struct convolveparams *p)
                                                p->cp.minmapsize);
         p->input->wcs=gal_wcs_read(p->filename, p->cp.hdu, 0, 0,
                                    &p->input->nwcs);
+        p->input->ndim=gal_dimension_remove_extra(p->input->ndim,
+                                                  p->input->dsize,
+                                                  p->input->wcs);
       }
 
   /* The input isn't an image (wasn't read yet), so we'll read it as a
@@ -459,9 +462,14 @@ ui_read_kernel(struct convolveparams *p)
   if( p->kernelname
       && p->input->ndim>1
       && gal_array_name_recognized(p->kernelname)  )
-    p->kernel = gal_array_read_one_ch_to_type(p->kernelname, p->khdu,
-                                              NULL, INPUT_USE_TYPE,
-                                              p->cp.minmapsize);
+    {
+      p->kernel = gal_array_read_one_ch_to_type(p->kernelname, p->khdu,
+                                                NULL, INPUT_USE_TYPE,
+                                                p->cp.minmapsize);
+      p->kernel->ndim=gal_dimension_remove_extra(p->kernel->ndim,
+                                                 p->kernel->dsize,
+                                                 p->kernel->wcs);
+    }
   else
     p->kernel=ui_read_column(p, 1);
 
diff --git a/bin/mkcatalog/ui.c b/bin/mkcatalog/ui.c
index 150b798..fb04aff 100644
--- a/bin/mkcatalog/ui.c
+++ b/bin/mkcatalog/ui.c
@@ -619,6 +619,8 @@ ui_read_labels(struct mkcatalogparams *p)
   /* Read it into memory. */
   p->objects = gal_array_read_one_ch(p->objectsfile, p->cp.hdu, NULL,
                                      p->cp.minmapsize);
+  p->objects->ndim=gal_dimension_remove_extra(p->objects->ndim,
+                                              p->objects->dsize, NULL);
 
 
   /* Make sure it has an integer type. */
@@ -681,6 +683,8 @@ ui_read_labels(struct mkcatalogparams *p)
       /* Read the clumps image. */
       p->clumps = gal_array_read_one_ch(p->usedclumpsfile, p->clumpshdu,
                                         NULL, p->cp.minmapsize);
+      p->clumps->ndim=gal_dimension_remove_extra(p->clumps->ndim,
+                                                 p->clumps->dsize, NULL);
 
       /* Check its size. */
       if( gal_dimension_is_different(p->objects, p->clumps) )
@@ -977,6 +981,8 @@ ui_preparations_read_inputs(struct mkcatalogparams *p)
       p->values=gal_array_read_one_ch_to_type(p->usedvaluesfile, p->valueshdu,
                                               NULL, GAL_TYPE_FLOAT32,
                                               p->cp.minmapsize);
+      p->values->ndim=gal_dimension_remove_extra(p->values->ndim,
+                                                 p->values->dsize, NULL);
 
       /* Make sure it has the correct size. */
       if( gal_dimension_is_different(p->objects, p->values) )
@@ -1024,6 +1030,8 @@ ui_preparations_read_inputs(struct mkcatalogparams *p)
           p->sky=gal_array_read_one_ch_to_type(p->usedskyfile, p->skyhdu,
                                                NULL, GAL_TYPE_FLOAT32,
                                                p->cp.minmapsize);
+          p->sky->ndim=gal_dimension_remove_extra(p->sky->ndim,
+                                                  p->sky->dsize, NULL);
 
           /* Check its size and prepare tile structure. */
           ui_preparation_check_size_read_tiles(p, p->sky, p->usedskyfile,
@@ -1053,6 +1061,8 @@ ui_preparations_read_inputs(struct mkcatalogparams *p)
       p->std=gal_array_read_one_ch_to_type(p->usedstdfile, p->stdhdu,
                                            NULL, GAL_TYPE_FLOAT32,
                                            p->cp.minmapsize);
+      p->std->ndim=gal_dimension_remove_extra(p->std->ndim,
+                                              p->std->dsize, NULL);
 
       /* Check its size and prepare tile structure. */
       ui_preparation_check_size_read_tiles(p, p->std, p->usedstdfile,
@@ -1086,6 +1096,9 @@ ui_preparations_read_inputs(struct mkcatalogparams *p)
           /* Read the mask image. */
           p->upmask = gal_array_read_one_ch(p->upmaskfile, p->upmaskhdu,
                                             NULL, p->cp.minmapsize);
+          p->upmask->ndim=gal_dimension_remove_extra(p->upmask->ndim,
+                                                     p->upmask->dsize,
+                                                     NULL);
 
           /* Check its size. */
           if( gal_dimension_is_different(p->objects, p->upmask) )
diff --git a/bin/mknoise/ui.c b/bin/mknoise/ui.c
index 3347590..5c4baad 100644
--- a/bin/mknoise/ui.c
+++ b/bin/mknoise/ui.c
@@ -286,6 +286,8 @@ ui_preparations(struct mknoiseparams *p)
   p->input=gal_array_read_one_ch_to_type(p->inputname, p->cp.hdu, NULL,
                                      GAL_TYPE_FLOAT64, p->cp.minmapsize);
   p->input->wcs=gal_wcs_read(p->inputname, p->cp.hdu, 0, 0, &p->input->nwcs);
+  p->input->ndim=gal_dimension_remove_extra(p->input->ndim, p->input->dsize,
+                                            p->input->wcs);
 
 
   /* If we are dealing with an input table, make sure the format of the
diff --git a/bin/mkprof/ui.c b/bin/mkprof/ui.c
index f86d984..af97e89 100644
--- a/bin/mkprof/ui.c
+++ b/bin/mkprof/ui.c
@@ -1024,6 +1024,7 @@ ui_prepare_canvas(struct mkprofparams *p)
   float *f, *ff;
   double truncr;
   long width[2]={1,1};
+  size_t tndim, *tdsize;
   int status=0, setshift=0;
   size_t i, nshift=0, *dsize=NULL, ndim_counter;
 
@@ -1035,7 +1036,10 @@ ui_prepare_canvas(struct mkprofparams *p)
          no merged image is desired, we just need the WCS information of
          the background image and the number of its dimensions. So
          `ndim==0' and what `dsize' points to is irrelevant. */
+      tdsize=gal_fits_img_info_dim(p->backname, p->backhdu, &tndim);
       p->wcs=gal_wcs_read(p->backname, p->backhdu, 0, 0, &p->nwcs);
+      tndim=gal_dimension_remove_extra(tndim, tdsize, p->wcs);
+      free(tdsize);
       if(p->nomerged==0)
         {
           /* If p->dsize was given as an option, free it. */
@@ -1308,7 +1312,7 @@ static void
 ui_read_ndim(struct mkprofparams *p)
 {
   gal_data_t *keysll;
-  size_t i, ndim_counter;
+  size_t i, *dsize, ndim_counter;
 
   if(p->kernel)
     {
@@ -1336,12 +1340,9 @@ ui_read_ndim(struct mkprofparams *p)
           if(p->nomerged)
             {
               /* Get the number of the background image's dimensions. */
-              keysll=gal_data_array_calloc(1);
-              keysll->name="NAXIS";   keysll->type=GAL_TYPE_SIZE_T;
-              gal_fits_key_read(p->backname, p->backhdu, keysll, 0, 0);
-              p->ndim = *(size_t *)(keysll->array);
-              keysll->name=NULL;
-              gal_data_array_free(keysll, 1, 1);
+              dsize=gal_fits_img_info_dim(p->backname, p->backhdu, &p->ndim);
+              p->ndim=gal_dimension_remove_extra(p->ndim, dsize, NULL);
+              free(dsize);
             }
           else
             {
@@ -1349,6 +1350,8 @@ ui_read_ndim(struct mkprofparams *p)
               p->out=gal_array_read_one_ch_to_type(p->backname, p->backhdu,
                                                    NULL, GAL_TYPE_FLOAT32,
                                                    p->cp.minmapsize);
+              p->out->ndim=gal_dimension_remove_extra(p->out->ndim,
+                                                      p->out->dsize, NULL);
               p->ndim=p->out->ndim;
             }
 
diff --git a/bin/noisechisel/ui.c b/bin/noisechisel/ui.c
index fb22eec..09941b1 100644
--- a/bin/noisechisel/ui.c
+++ b/bin/noisechisel/ui.c
@@ -559,16 +559,22 @@ ui_preparations_read_input(struct noisechiselparams *p)
   float *f;
   size_t ndim;
 
-  /* Read the input as a single precision floating point dataset. */
+  /* Read the input as a single precision floating point dataset, also load
+     the WCS and finally remove any possibly existing extra dimensions
+     (with a length of 1). */
   p->input = gal_array_read_one_ch_to_type(p->inputname, p->cp.hdu,
                                            NULL, GAL_TYPE_FLOAT32,
                                            p->cp.minmapsize);
   p->input->wcs = gal_wcs_read(p->inputname, p->cp.hdu, 0, 0,
                                &p->input->nwcs);
+  p->input->ndim=gal_dimension_remove_extra(p->input->ndim,
+                                            p->input->dsize,
+                                            p->input->wcs);
+
+  /* When the input doesn't have a name, use `INPUT'. */
   if(p->input->name==NULL)
     gal_checkset_allocate_copy("INPUT", &p->input->name);
 
-
   /* NoiseChisel currently only works on 2D datasets (images). */
   if(p->input->ndim!=2)
     error(EXIT_FAILURE, 0, "%s (hdu: %s) has %zu dimensions but NoiseChisel "
diff --git a/bin/segment/ui.c b/bin/segment/ui.c
index 9e6a908..f046c64 100644
--- a/bin/segment/ui.c
+++ b/bin/segment/ui.c
@@ -413,6 +413,11 @@ ui_prepare_inputs(struct segmentparams *p)
                                            p->cp.minmapsize);
   p->input->wcs = gal_wcs_read(p->inputname, p->cp.hdu, 0, 0,
                                &p->input->nwcs);
+  p->input->ndim=gal_dimension_remove_extra(p->input->ndim,
+                                            p->input->dsize,
+                                            p->input->wcs);
+
+  /* Set the name. */
   if(p->input->name) free(p->input->name);
   gal_checkset_allocate_copy("INPUT", &p->input->name);
 
@@ -435,6 +440,9 @@ ui_prepare_inputs(struct segmentparams *p)
       p->conv = gal_array_read_one_ch_to_type(p->convolvedname, p->chdu,
                                               NULL, GAL_TYPE_FLOAT32,
                                               p->cp.minmapsize);
+      p->conv->ndim=gal_dimension_remove_extra(p->conv->ndim,
+                                               p->conv->dsize,
+                                               p->conv->wcs);
       p->conv->wcs=gal_wcs_copy(p->input->wcs);
 
       /* Make sure it is the same size as the input. */
@@ -453,6 +461,8 @@ ui_prepare_inputs(struct segmentparams *p)
       /* Read the dataset into memory. */
       p->olabel = gal_array_read_one_ch(p->useddetectionname, p->dhdu,
                                         NULL, p->cp.minmapsize);
+      p->olabel->ndim=gal_dimension_remove_extra(p->olabel->ndim,
+                                                 p->olabel->dsize, NULL);
       if( gal_dimension_is_different(p->input, p->olabel) )
         error(EXIT_FAILURE, 0, "`%s' (hdu: %s) and `%s' (hdu: %s) have a"
               "different dimension/size", p->useddetectionname, p->dhdu,
@@ -527,8 +537,13 @@ ui_prepare_kernel(struct segmentparams *p)
   if(p->kernelname)
     {
       if( strcmp(p->kernelname, UI_NO_CONV_KERNEL_NAME) )
-        p->kernel=gal_fits_img_read_kernel(p->kernelname, p->khdu,
-                                           p->cp.minmapsize);
+        {
+          p->kernel=gal_fits_img_read_kernel(p->kernelname, p->khdu,
+                                             p->cp.minmapsize);
+          p->kernel->ndim=gal_dimension_remove_extra(p->kernel->ndim,
+                                                     p->kernel->dsize,
+                                                     NULL);
+        }
       else
         p->kernel=NULL;
     }
@@ -729,6 +744,8 @@ ui_read_std_and_sky(struct segmentparams *p)
       p->std=gal_array_read_one_ch_to_type(p->usedstdname, p->stdhdu,
                                            NULL, GAL_TYPE_FLOAT32,
                                            p->cp.minmapsize);
+      p->std->ndim=gal_dimension_remove_extra(p->std->ndim,
+                                              p->std->dsize, NULL);
 
       /* Make sure it has the correct size. */
       ui_check_size(p->input, p->std, tl->tottiles, p->inputname, p->cp.hdu,
@@ -787,6 +804,8 @@ ui_read_std_and_sky(struct segmentparams *p)
           sky=gal_array_read_one_ch_to_type(p->skyname, p->skyhdu,
                                             NULL, GAL_TYPE_FLOAT32,
                                             p->cp.minmapsize);
+          sky->ndim=gal_dimension_remove_extra(sky->ndim, sky->dsize,
+                                               NULL);
 
           /* Check its size. */
           ui_check_size(p->input, sky, tl->tottiles, p->inputname, p->cp.hdu,
diff --git a/bin/statistics/ui.c b/bin/statistics/ui.c
index a85881d..2378b31 100644
--- a/bin/statistics/ui.c
+++ b/bin/statistics/ui.c
@@ -848,6 +848,9 @@ ui_preparations(struct statisticsparams *p)
                                      cp->minmapsize);
       p->input->wcs=gal_wcs_read(p->inputname, cp->hdu, 0, 0,
                                  &p->input->nwcs);
+      p->input->ndim=gal_dimension_remove_extra(p->input->ndim,
+                                                p->input->dsize,
+                                                p->input->wcs);
     }
   else
     {
@@ -857,8 +860,12 @@ ui_preparations(struct statisticsparams *p)
 
   /* Read the convolution kernel if necessary. */
   if(p->sky && p->kernelname)
-    p->kernel=gal_fits_img_read_kernel(p->kernelname, p->khdu,
-                                       cp->minmapsize);
+    {
+      p->kernel=gal_fits_img_read_kernel(p->kernelname, p->khdu,
+                                         cp->minmapsize);
+      p->kernel->ndim=gal_dimension_remove_extra(p->kernel->ndim,
+                                                 p->kernel->dsize, NULL);
+    }
 
   /* Tile and channel sanity checks and preparations. */
   if(p->ontile || p->sky)
diff --git a/bin/warp/ui.c b/bin/warp/ui.c
index 1706d05..386a874 100644
--- a/bin/warp/ui.c
+++ b/bin/warp/ui.c
@@ -348,6 +348,9 @@ ui_check_options_and_arguments(struct warpparams *p)
                                              p->cp.minmapsize);
       p->input->wcs=gal_wcs_read(p->inputname, p->cp.hdu, p->hstartwcs,
                                  p->hendwcs, &p->input->nwcs);
+      p->input->ndim=gal_dimension_remove_extra(p->input->ndim,
+                                                p->input->dsize,
+                                                p->input->wcs);
       if(p->input->wcs)
         {
           p->pixelscale=gal_wcs_pixel_scale(p->input->wcs);
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index bfb1742..f1a70df 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -25508,6 +25508,17 @@ have the maximum value along the given dimension and 
if it is zero, the
 minimum.
 @end deftypefun
 
+@deftypefun size_t gal_dimension_remove_extra (size_t @code{ndim}, size_t 
@code{*dsize}, struct wcsprm @code{*wcs})
+Remove extra dimensions (those that only have a length of 1) from the basic
+size information of a dataset. @code{ndim} is the number of dimensions and
+@code{dsize} is an array with @code{ndim} elements containing the size
+along each dimension in the C dimension order. When @code{wcs!=NULL}, the
+respective dimension will also be removed from the WCS.
+
+This function will return the new number of dimensions and the @code{dsize}
+elements will contain the length along each new dimension.
+@end deftypefun
+
 @deffn {Function-like macro} GAL_DIMENSION_NEIGHBOR_OP (@code{index}, 
@code{ndim}, @code{dsize}, @code{connectivity}, @code{dinc}, @code{operation})
 Parse the neighbors of the element located at @code{index} and do the
 requested operation on them. This is defined as a macro to allow easy
@@ -27201,6 +27212,12 @@ then if the image has a name and units, the respective 
string will be put
 in these pointers.
 @end deftypefun
 
+@deftypefun {size_t *} gal_fits_img_info_dim (char @code{*filename}, char 
@code{*hdu}, size_t @code{*ndim})
+Put the number of dimensions in the @code{hdu} extension of @file{filename}
+in the space that @code{ndim} points to and return the size of the dataset
+along each dimension as an allocated array with @code{*ndim} elements.
+@end deftypefun
+
 @deftypefun {gal_data_t *} gal_fits_img_read (char @code{*filename}, char 
@code{*hdu}, size_t @code{minmapsize})
 Read the contents of the @code{hdu} extension/HDU of @code{filename} into a
 Gnuastro generic data container (see @ref{Generic data container}) and
@@ -27840,6 +27857,10 @@ points to. Please see @code{gal_wcs_read_fitsptr} for 
more.
 Return a fully allocated (independent) copy of @code{wcs}.
 @end deftypefun
 
+@deftypefun void gal_wcs_remove_dimension (struct wcsprm @code{*wcs}, size_t 
@code{fitsdim})
+Remove the given FITS dimension from the given @code{wcs} structure.
+@end deftypefun
+
 @deftypefun void gal_wcs_on_tile (gal_data_t @code{*tile})
 Create a WCSLIB @code{wcsprm} structure for @code{tile} using WCS
 parameters of the tile's allocated block dataset, see @ref{Tessellation
diff --git a/lib/dimension.c b/lib/dimension.c
index 310b5cb..61397b8 100644
--- a/lib/dimension.c
+++ b/lib/dimension.c
@@ -795,3 +795,48 @@ gal_dimension_collapse_minmax(gal_data_t *in, size_t 
c_dim, int max1_min0)
   if(num) gal_data_free(num);
   return minmax;
 }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/************************************************************************/
+/********************             Other            **********************/
+/************************************************************************/
+size_t
+gal_dimension_remove_extra(size_t ndim, size_t *dsize, struct wcsprm *wcs)
+{
+  size_t i, j;
+
+  for(i=0;i<ndim;++i)
+    if(dsize[i]==1)
+      {
+        /* Correct the WCS. */
+        if(wcs) gal_wcs_remove_dimension(wcs, ndim-i);
+
+        /* Shift all subsequent dimensions to replace this one. */
+        for(j=i;j<ndim-1;++j) dsize[j]=dsize[j+1];
+
+        /* Decrement the `i' and the total number of dimension. */
+        --i;
+        --ndim;
+      }
+
+  /* Return the number of dimensions. */
+  return ndim;
+}
diff --git a/lib/fits.c b/lib/fits.c
index fbbea32..6ccdae7 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -1790,6 +1790,27 @@ gal_fits_img_info(fitsfile *fptr, int *type, size_t 
*ndim, size_t **dsize,
 
 
 
+/* Get the basic array info to remove extra dimensions if necessary. */
+size_t *
+gal_fits_img_info_dim(char *filename, char *hdu, size_t *ndim)
+{
+  fitsfile *fptr;
+  size_t *dsize=NULL;
+  int status=0, type;
+
+  /* Open the given header, read the basic image information and close it
+     again. */
+  fptr=gal_fits_hdu_open(filename, hdu, READONLY);
+  gal_fits_img_info(fptr, &type, ndim, &dsize, NULL, NULL);
+  if( fits_close_file(fptr, &status) ) gal_fits_io_error(status, NULL);
+
+  return dsize;
+}
+
+
+
+
+
 /* Read a FITS image HDU into a Gnuastro data structure. */
 gal_data_t *
 gal_fits_img_read(char *filename, char *hdu, size_t minmapsize)
diff --git a/lib/gnuastro/dimension.h b/lib/gnuastro/dimension.h
index 2bdc9c4..c9bf682 100644
--- a/lib/gnuastro/dimension.h
+++ b/lib/gnuastro/dimension.h
@@ -116,6 +116,14 @@ gal_dimension_collapse_minmax(gal_data_t *in, size_t 
c_dim, int max1_min0);
 
 
 /************************************************************************/
+/********************             Other            **********************/
+/************************************************************************/
+size_t
+gal_dimension_remove_extra(size_t ndim, size_t *dsize, struct wcsprm *wcs);
+
+
+
+/************************************************************************/
 /********************          Neighbors           **********************/
 /************************************************************************/
 /* Purpose
diff --git a/lib/gnuastro/fits.h b/lib/gnuastro/fits.h
index 9071294..f25a997 100644
--- a/lib/gnuastro/fits.h
+++ b/lib/gnuastro/fits.h
@@ -235,6 +235,9 @@ void
 gal_fits_img_info(fitsfile *fptr, int *type, size_t *ndim, size_t **dsize,
                   char **name, char **unit);
 
+size_t *
+gal_fits_img_info_dim(char *filename, char *hdu, size_t *ndim);
+
 gal_data_t *
 gal_fits_img_read(char *filename, char *hdu, size_t minmapsize);
 



reply via email to

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