gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master ba0c2ce: Library (wcs.h): limiting header keys


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master ba0c2ce: Library (wcs.h): limiting header keys for wcs works with comment keys
Date: Tue, 18 May 2021 20:01:00 -0400 (EDT)

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

    Library (wcs.h): limiting header keys for wcs works with comment keys
    
    Until now, when the input HDU to the 'gal_wcs_read_fitsptr' function had
    empty keyword lines, or COMMENT or HISTORY keywords, the values given to
    'hstartwcs' and 'hendwcs' would not be correct (the values to similarly
    named options in the Crop and Warp programs! This was happening because we
    were always setting CFITSIO's 'fits_hdr2str' function to ignore comments
    when reading the keywords into a string!
    
    With this commit, the second argument to CFITSIO's 'fits_hdr2str' function
    is now optional: so when the user wants to actually give header keyword
    numbers to ignore, the keyword numbers correspond to the same order they
    see on the command-line with the 'astfits' program. When the user doesn't
    want to limit the keywords, it will ignore comment and empty lines like
    before (and not waste CPU power on extra lines).
    
    This fixes bug #60634.
---
 NEWS              |  1 +
 bin/crop/ui.c     |  3 +--
 doc/gnuastro.texi | 17 ++++++++++++-----
 lib/wcs.c         | 29 +++++++++++++++++------------
 4 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/NEWS b/NEWS
index 4bb605e..ffb9fea 100644
--- a/NEWS
+++ b/NEWS
@@ -253,6 +253,7 @@ See the end of the file for license conditions.
   bug #60484: Match crashes when called with --coord and --ccol2 (together)
   bug #60603: Table crashes with an empty input and --range
   bug #60619: Crop crashes with differing image and WCS dimension
+  bug #60634: Crop's '--hstartwcs' and '--hendwcs' wrong with comment keys
 
 
 
diff --git a/bin/crop/ui.c b/bin/crop/ui.c
index 4b28cb8..0b99b27 100644
--- a/bin/crop/ui.c
+++ b/bin/crop/ui.c
@@ -915,10 +915,9 @@ ui_preparations_to_img_mode(struct cropparams *p)
 static void
 ui_preparations_to_img_mode_values(struct cropparams *p)
 {
-  struct wcsprm *wcs=p->imgs[0].wcs;
-
   int i, status, *stat=NULL;
   gal_data_t *tmp, *coords=NULL;
+  struct wcsprm *wcs=p->imgs[0].wcs;
   size_t ndim=p->imgs[0].ndim, plysize=p->nvertices;
   double *phi=NULL, *theta=NULL, *pixcrd=NULL, *imgcrd=NULL;
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index ab03808..3f5304f 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -11667,12 +11667,19 @@ Input image parameters:
 @table @option
 
 @item --hstartwcs=INT
-@cindex CANDELS survey
 Specify the first keyword card (line number) to start finding the input image 
world coordinate system information.
-Distortions were only recently included in WCSLIB (from version 5).
-Therefore until now, different telescope would apply their own specific set of 
WCS keywords and put them into the image header along with those that WCSLIB 
does recognize.
-So now that WCSLIB recognizes most of the standard distortion parameters, they 
will get confused with the old ones and give completely wrong results.
-For example in the CANDELS-GOODS South 
images@footnote{@url{https://archive.stsci.edu/pub/hlsp/candels/goods-s/gs-tot/v1.0/}}.
+This is useful when certain header keywords of the input may cause bad 
conflicts with your crop (see an example described below).
+To get line numbers of the header keywords, you can pipe the fully printed 
header into @command{cat -n} like below:
+
+@example
+$ astfits image.fits -h1 | cat -n
+@end example
+
+@cindex CANDELS survey
+For example, distortions have only been present in WCSLIB from version 5.15 
(released in mid 2016).
+Therefore some pipelines still apply their own specific set of WCS keywords 
for distortions and put them into the image header along with those that WCSLIB 
does recognize.
+So now that WCSLIB recognizes most of the standard distortion parameters, they 
will get confused with the old ones and give wrong results.
+For example in the CANDELS-GOODS South images that were created before WCSLIB 
5.15@footnote{@url{https://archive.stsci.edu/pub/hlsp/candels/goods-s/gs-tot/v1.0/}}.
 
 The two @option{--hstartwcs} and @option{--hendwcs} are thus provided so when 
using older datasets, you can specify what region in the FITS headers you want 
to use to read the WCS keywords.
 Note that this is only relevant for reading the WCS information, basic data 
information like the image size are read separately.
diff --git a/lib/wcs.c b/lib/wcs.c
index a90d12e..72b7324 100644
--- a/lib/wcs.c
+++ b/lib/wcs.c
@@ -145,9 +145,9 @@ gal_wcs_read_fitsptr(fitsfile *fptr, int linearmatrix, 
size_t hstartwcs,
                      size_t hendwcs, int *nwcs)
 {
   /* Declaratins: */
-  int sumcheck;
   size_t i, fulllen;
   int nkeys=0, status=0;
+  int sumcheck, nocomments;
   struct wcsprm *wcs=NULL;
   char *fullheader, *to, *from;
   int fixstatus[NWCSFIX]={0};/* For the various wcsfix checks.          */
@@ -158,8 +158,16 @@ gal_wcs_read_fitsptr(fitsfile *fptr, int linearmatrix, 
size_t hstartwcs,
   void *fixnaxis = NULL;     /* For now disable cylfix() with this      */
                              /* (because it depends on image size).     */
 
+  /* In case the user has asked to limit the HDU keyword cards to use for
+     WCS reading, also count comment/history/empty lines (so the lines here
+     correspond to the output of 'astfits image.fits -h1'). But if no
+     limitation is requested, avoid those lines to make the processing
+     easier for WCSLIB. */
+  nocomments = hendwcs>hstartwcs ? 0 : 1;
+
   /* CFITSIO function: */
-  if( fits_hdr2str(fptr, 1, NULL, 0, &fullheader, &nkeys, &status) )
+  if( fits_hdr2str(fptr, nocomments, NULL, 0, &fullheader,
+                   &nkeys, &status) )
     gal_fits_io_error(status, NULL);
 
   /* Only consider the header keywords in the current range: */
@@ -167,11 +175,10 @@ gal_wcs_read_fitsptr(fitsfile *fptr, int linearmatrix, 
size_t hstartwcs,
     {
       /* Mark the last character in the desired region. */
       fullheader[hendwcs*(FLEN_CARD-1)]='\0';
-      /*******************************************************/
-      /******************************************************
+
+      /* For a check:
       printf("%s\n", fullheader);
-      ******************************************************/
-      /*******************************************************/
+      */
 
       /* Shift all the characters to the start of the string. */
       if(hstartwcs)                /* hstartwcs!=0 */
@@ -183,20 +190,19 @@ gal_wcs_read_fitsptr(fitsfile *fptr, int linearmatrix, 
size_t hstartwcs,
 
       nkeys=hendwcs-hstartwcs;
 
-      /*******************************************************/
-      /******************************************************
+      /* For a check:
       printf("\n\n\n###############\n\n\n\n\n\n");
       printf("%s\n", &fullheader[1*(FLEN_CARD-1)]);
       exit(0);
-      ******************************************************/
-      /*******************************************************/
+      */
     }
 
   /* WCSlib function to parse the FITS headers. */
   status=wcspih(fullheader, nkeys, relax, ctrl, &nreject, nwcs, &wcs);
   if(status)
     {
-      fprintf(stderr, "\n##################\n"
+      fprintf(stderr,"\n"
+              "##################\n"
               "WCSLIB Warning: wcspih ERROR %d: %s.\n"
               "##################\n",
               status, wcs_errmsg[status]);
@@ -1387,7 +1393,6 @@ gal_wcs_remove_dimension(struct wcsprm *wcs, size_t 
fitsdim)
 #endif
   /**************************************************/
 
-
   /* First loop over the arrays. */
   for(i=0;i<naxis;++i)
     {



reply via email to

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