gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 7279c9c0 5/6: PSF scripts: removing the --core


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 7279c9c0 5/6: PSF scripts: removing the --corewidth option to simplify the scripts
Date: Mon, 13 Jun 2022 11:18:56 -0400 (EDT)

branch: master
commit 7279c9c00c3c795eb8b997c8f83eeee09bfa74cd
Author: Raul Infante-Sainz <infantesainz@gmail.com>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    PSF scripts: removing the --corewidth option to simplify the scripts
    
    Until now, the PSF scripts were obtaining the clump and object labels
    internally by cropping a small region of the segment image. These labels
    are the one that should not be masked. The user could specify the size of
    that cropped image with the parameter '--cropwidth'.  However, this
    introduced several steps and complexities in the scripts. For example, if
    the region was larger than one pixel, the unique value was needed to be
    computed.
    
    With the recent completion of the task #16218, Crop has a parameter that
    provides the pixel value of a cropped image when it is of one pixel size.
    This is very convenient for obtaining the clump and object labels at a
    given position, which is exactly the task that the scripts need.
    
    With this commit, the scripts use this new feature to crop 1-pixel image
    and obtain its value directly using the new Crop's option
    ('--oneelemstdout'). As a consequence, the previous function that was
    designed for computing this values is not needed any more. In the same way,
    the '--corewidth' option has been removed. Now the cropped image to obtain
    the object and clumps labels from the segment image is of 1-pixel size.
---
 bin/script/psf-scale-factor.in | 125 +++++++++++++++--------------------
 bin/script/psf-stamp.in        | 144 +++++++++++++++--------------------------
 doc/gnuastro.texi              |  22 -------
 3 files changed, 103 insertions(+), 188 deletions(-)

diff --git a/bin/script/psf-scale-factor.in b/bin/script/psf-scale-factor.in
index 4f5d21fd..8b627208 100644
--- a/bin/script/psf-scale-factor.in
+++ b/bin/script/psf-scale-factor.in
@@ -46,7 +46,6 @@ tmpdir=""
 segment=""
 normradii=""
 stampwidth=""
-corewidth="3,3"
 sigmaclip="3,0.1"
 version=@VERSION@
 scriptname=@SCRIPT_NAME@
@@ -97,7 +96,6 @@ $scriptname options:
   -W, --stampwidth=INT    Width of the stamp in pixels.
   -n, --normradii=INT,INT Minimum and maximum radii (in pixels)
                           for computing the scaling factor value.
-  -w, --corewidth=INT     Area width of the central object in pixels for 
unmasking.
   -S, --segment=STR       Output of Segment (with OBJECTS and CLUMPS).
   -s, --sigmaclip=FLT,FLT Sigma-clip multiple and tolerance.
 
@@ -287,9 +285,6 @@ do
         -W|--stampwidth)     stampwidth="$2";                           
check_v "$1" "$stampwidth";  shift;shift;;
         -W=*|--stampwidth=*) stampwidth="${1#*=}";                      
check_v "$1" "$stampwidth";  shift;;
         -W*)                 stampwidth=$(echo "$1"  | sed -e's/-W//'); 
check_v "$1" "$stampwidth";  shift;;
-        -w|--corewidth)      corewidth="$2";                            
check_v "$1" "$corewidth";  shift;shift;;
-        -w=*|--corewidth=*)  corewidth="${1#*=}";                       
check_v "$1" "$corewidth";  shift;;
-        -w*)                 corewidth=$(echo "$1"  | sed -e's/-w//');  
check_v "$1" "$corewidth";  shift;;
         -S|--segment)        segment="$2";                              
check_v "$1" "$segment";  shift;shift;;
         -S=*|--segment=*)    segment="${1#*=}";                         
check_v "$1" "$segment";  shift;;
         -S*)                 segment=$(echo "$1"  | sed -e's/-S//');    
check_v "$1" "$segment";  shift;;
@@ -512,48 +507,6 @@ astcrop $inputs --hdu=$hdu --mode=img \
 
 
 
-# Function to find label of desired region
-# ----------------------------------------
-#
-# Given a certain extension ('CLUMPS' or 'OBJECTS'), find the respective
-# label.
-find_central_label() {
-
-  # Input arguments
-  hdu=$1
-
-  # Set the parameters.
-  case $hdu in
-      CLUMPS) labname=clump;;
-      OBJECTS) labname=object;;
-      *) cat <<EOF
-$scriptname: ERROR: a bug! Please contact us at bug-gnuastro@gnu.org to fix 
the problem. The first argument to 'find_central_label' is not 'CLUMPS' or 
'OBJECTS'
-EOF
-         exit 1
-      ;;
-  esac
-
-  # Crop the labeled image.
-  cropped_core=$tmpdir/cropped-core-$objectid.fits
-  astcrop $segment --hdu=$hdu --mode=img $quiet --width=$corewidth \
-          --center=$xcenter,$ycenter --output=$cropped_core
-  lab=$(astarithmetic $cropped_core unique --quiet)
-  numlab=$(echo "$lab" | awk '{print NF}')
-  if [ $numlab != 1 ]; then
-    cat <<EOF
-$scriptname: ERROR: there is more than one $labname label in the core region 
around the given coordinate (specified by '--corewidth=$corewidth', containing 
$labname labels: $lab). Therefore it is not possible to unambiguously identify 
a single $labname. Please decrease the box size given to '--corewidth'.
-EOF
-    exit 1
-  fi
-
-  # Write the output in a file.
-  echo $lab > $tmpdir/cropped-core-$labname-$objectid.txt
-}
-
-
-
-
-
 # Crop and unlabel the segmentation image
 # ---------------------------------------
 #
@@ -561,6 +514,8 @@ EOF
 # to mask all objects that are not the central one. If not, just consider
 # that the cropped and masked image is the cropped (not masked) image. The
 # process is as follow:
+#   - Compute the central clump and object labels. This is done with the
+#     option '--oneelemstdout' of Crop.
 #   - Crop the original mask image.
 #   - Crop the original mask image to a central region (core), in order to
 #     compute what is the central object id. This is necessary to unmask
@@ -571,34 +526,58 @@ EOF
 #     sky.
 #   - Mask all non zero pixels in the mask image as nan values.
 if [ x"$segment" != x ]; then
+    clab="not-initialized"
+    olab="not-initialized"
+    # Find the object and clump labels of the target. Labels are saved into
+    # plain text files just in case they are necessary for debugging tasks.
+    cropped_core_clump=$tmpdir/cropped-core-clump-$objectid.fits
+    cropped_core_object=$tmpdir/cropped-core-object-$objectid.fits
+    clumplab=$(astcrop $segment --hdu=CLUMPS --mode=img --width=1,1 \
+               --widthinpix --oneelemstdout --center=$xcenter,$ycenter \
+               --output=$cropped_core_clump --quiet)
+    objectlab=$(astcrop $segment --hdu=OBJECTS --mode=img --width=1,1 \
+                --widthinpix --oneelemstdout --center=$xcenter,$ycenter \
+                --output=$cropped_core_object --quiet)
+    echo $clumplab > $tmpdir/cropped-core-clump-$objectid.txt
+    echo $objectlab > $tmpdir/cropped-core-object-$objectid.txt
+
+    # Read the labels
+    clab=$(cat $tmpdir/cropped-core-clump-$objectid.txt)
+    olab=$(cat $tmpdir/cropped-core-object-$objectid.txt)
+
+    # If for any reason, a clump or object label couldn't be initialized at
+    # the given coordiante, simply ignore this step. But print a warning so
+    # the user is informed of the situation (and that this is a bug: 'clab'
+    # should be initialized!).
+    if [ "$clab" = "not-initialized"   -o   "$olab" = "not-initialized" ]; then
+        cat <<EOF
+$scriptname: WARNING: a bug! Please contact us at 'bug-gnuastro@gnu.org' to 
fix the problem. For some reason, no clump or object label could be initialized 
for the given coordinate within the given Segment image.
+EOF
+        cropped_masked=$cropped
 
-  # Find the object and clump labels of the target.
-  find_central_label CLUMPS
-  find_central_label OBJECTS
-  clab=$(cat $tmpdir/cropped-core-clump-$objectid.txt)
-  olab=$(cat $tmpdir/cropped-core-object-$objectid.txt)
-
-  # Crop the object and clump image to same size as desired stamp.
-  cropclp=$tmpdir/cropped-clumps-$objectid.fits
-  cropobj=$tmpdir/cropped-objects-$objectid.fits
-  astcrop $segment --hdu=OBJECTS --mode=img \
-          --center=$xcenter,$ycenter \
-          --width=$stampwidth --output=$cropobj $quiet
-  astcrop $segment --hdu=CLUMPS --mode=img \
-          --center=$xcenter,$ycenter \
-          --width=$stampwidth --output=$cropclp $quiet
-
-  # Mask all the undesired regions.
-  cropped_masked=$tmpdir/cropped-masked-$objectid.fits
-  astarithmetic $cropped --hdu=1 set-i --output=$cropped_masked \
-                $cropobj --hdu=1 set-o \
-                $cropclp --hdu=1 set-c \
-                                       \
-                c o $olab ne 0 where c $clab eq -1 where 0 gt set-cmask \
-                o o $olab eq 0 where set-omask \
-                i omask cmask or nan where $quiet
+    else
+        # Crop the object and clump image to same size as desired stamp.
+        cropclp=$tmpdir/cropped-clumps-$objectid.fits
+        cropobj=$tmpdir/cropped-objects-$objectid.fits
+        astcrop $segment --hdu=OBJECTS --mode=img \
+                --center=$xcenter,$ycenter \
+                --width=$xstampwidth,$ystampwidth --output=$cropobj $quiet
+        astcrop $segment --hdu=CLUMPS --mode=img \
+                --center=$xcenter,$ycenter \
+                --width=$xstampwidth,$ystampwidth --output=$cropclp $quiet
+
+        # Mask all the undesired regions.
+        cropped_masked=$tmpdir/cropped-masked-$objectid.fits
+        astarithmetic $cropped --hdu=1 set-i --output=$cropped_masked \
+                      $cropobj --hdu=1 set-o \
+                      $cropclp --hdu=1 set-c \
+                      \
+                      c o $olab ne 0 where c $clab eq -1 where 0 gt set-cmask \
+                      o o $olab eq 0 where set-omask \
+                      i omask cmask or nan where
+    fi
 else
-  cropped_masked=$cropped
+    cropped_masked=$cropped
 fi
 
 
diff --git a/bin/script/psf-stamp.in b/bin/script/psf-stamp.in
index 3d49e615..a2bc47d6 100644
--- a/bin/script/psf-stamp.in
+++ b/bin/script/psf-stamp.in
@@ -56,7 +56,6 @@ sigmaclip=""
 stampwidth=""
 normop="median"
 positionangle=0
-corewidth="3,3"
 version=@VERSION@
 scriptname=@SCRIPT_NAME@
 
@@ -102,7 +101,6 @@ $scriptname options:
   -n, --normradii=INT,INT Minimum and maximum radii (in pixels)
                           for computing the normalization value.
   -S, --segment=STR       Output of Segment (with OBJECTS and CLUMPS).
-  -w, --corewidth=INT     Area width of the central object in pixels for 
unmasking.
   -N, --normop=STR        Operator for computing the normalization value
                           (mean, sigclip-mean, etc.).
   -Q, --axisratio=FLT     Axis ratio for ellipse maskprofile (A/B).
@@ -290,9 +288,6 @@ do
         -W|--stampwidth)     stampwidth="$2";                           
check_v "$1" "$stampwidth";  shift;shift;;
         -W=*|--stampwidth=*) stampwidth="${1#*=}";                      
check_v "$1" "$stampwidth";  shift;;
         -W*)                 stampwidth=$(echo "$1"  | sed -e's/-W//'); 
check_v "$1" "$stampwidth";  shift;;
-        -w|--corewidth)      corewidth="$2";                            
check_v "$1" "$corewidth";  shift;shift;;
-        -w=*|--corewidth=*)  corewidth="${1#*=}";                       
check_v "$1" "$corewidth";  shift;;
-        -w*)                 corewidth=$(echo "$1"  | sed -e's/-w//');  
check_v "$1" "$corewidth";  shift;;
         -S|--segment)        segment="$2";                              
check_v "$1" "$segment";  shift;shift;;
         -S=*|--segment=*)    segment="${1#*=}";                         
check_v "$1" "$segment";  shift;;
         -S*)                 segment=$(echo "$1"  | sed -e's/-S//');    
check_v "$1" "$segment";  shift;;
@@ -558,57 +553,6 @@ fi
 
 
 
-# Function to find label of desired region
-# ----------------------------------------
-#
-# Given a certain extension ('CLUMPS' or 'OBJECTS'), find the respective
-# label.
-find_central_label() {
-
-  # Input arguments
-  hdu=$1
-  lab="not-initialized"
-
-  # Set the parameters.
-  case $hdu in
-      CLUMPS) labname=clump;;
-      OBJECTS) labname=object;;
-      *) cat <<EOF
-$scriptname: ERROR: a bug! Please contact us at 'bug-gnuastro@gnu.org' to fix 
the problem. The first argument to 'find_central_label' is not 'CLUMPS' or 
'OBJECTS'
-EOF
-         exit 1;;
-  esac
-
-  # Crop the labeled image.
-  cropped_core=$tmpdir/cropped-core-$objectid.fits
-  astcrop $segment --hdu=$hdu --mode=img $quiet --width=$corewidth \
-          --center=$xcenter,$ycenter --output=$cropped_core
-
-  # The coordinate may not be in the image. In this case, Crop will not
-  # create the requested output.
-  if [ -f $cropped_core ]; then
-      lab=$(astarithmetic $cropped_core unique --quiet)
-      numlab=$(echo "$lab" | awk '{print NF}')
-      if [ $numlab != 1 ]; then
-          cat <<EOF
-$scriptname: ERROR: there is more than one $labname label in the core region 
around the given coordinate (specified by '--corewidth=$corewidth', containing 
$labname labels: $lab). Therefore it is not possible to unambiguously identify 
a single $labname. Please decrease the box size given to '--corewidth'.
-EOF
-          exit 1
-      fi
-
-  # Coordinate was not in image. We'll just report an impossible label so
-  # all non-zero labels are masked.
-  else
-      lab=2147483646 # Largest possible 32-bit integer minus 1.
-  fi
-
-  # Write the output in a file.
-  echo $lab > $tmpdir/cropped-core-$labname-$objectid.txt
-}
-
-
-
-
 # Crop and unlabel the segmentation image
 # ---------------------------------------
 #
@@ -616,6 +560,8 @@ EOF
 # to mask all objects that are not the central one. If not, just consider
 # that the cropped and masked image is the cropped (not masked) image. The
 # process is as follow:
+#   - Compute the central clump and object labels. This is done with the
+#     option '--oneelemstdout' of Crop.
 #   - Crop the original mask image.
 #   - Crop the original mask image to a central region (core), in order to
 #     compute what is the central object id. This is necessary to unmask
@@ -626,46 +572,58 @@ EOF
 #     sky.
 #   - Mask all non zero pixels in the mask image as nan values.
 if [ x"$segment" != x ]; then
-
-  # Find the object and clump labels of the target.
-  find_central_label CLUMPS
-  find_central_label OBJECTS
-  clab=$(cat $tmpdir/cropped-core-clump-$objectid.txt)
-  olab=$(cat $tmpdir/cropped-core-object-$objectid.txt)
-
-  # If for any reason, a clump or object label couldn't be initialized at
-  # the given coordiante, simply ignore this step. But print a warning so
-  # the user is informed of the situation (and that this is a bug: 'clab'
-  # should be initialized!).
-  if [ $clab = "not-initialized" ]; then
-    cat <<EOF
+    clab="not-initialized"
+    olab="not-initialized"
+    # Find the object and clump labels of the target. Labels are saved into
+    # plain text files just in case they are necessary for debugging tasks.
+    cropped_core_clump=$tmpdir/cropped-core-clump-$objectid.fits
+    cropped_core_object=$tmpdir/cropped-core-object-$objectid.fits
+    clumplab=$(astcrop $segment --hdu=CLUMPS --mode=img --width=1,1 \
+               --widthinpix --oneelemstdout --center=$xcenter,$ycenter \
+               --output=$cropped_core_clump --quiet)
+    objectlab=$(astcrop $segment --hdu=OBJECTS --mode=img --width=1,1 \
+                --widthinpix --oneelemstdout --center=$xcenter,$ycenter \
+                --output=$cropped_core_object --quiet)
+    echo $clumplab > $tmpdir/cropped-core-clump-$objectid.txt
+    echo $objectlab > $tmpdir/cropped-core-object-$objectid.txt
+
+    # Read the labels
+    clab=$(cat $tmpdir/cropped-core-clump-$objectid.txt)
+    olab=$(cat $tmpdir/cropped-core-object-$objectid.txt)
+
+    # If for any reason, a clump or object label couldn't be initialized at
+    # the given coordiante, simply ignore this step. But print a warning so
+    # the user is informed of the situation (and that this is a bug: 'clab'
+    # should be initialized!).
+    if [ "$clab" = "not-initialized"   -o   "$olab" = "not-initialized" ]; then
+        cat <<EOF
 $scriptname: WARNING: a bug! Please contact us at 'bug-gnuastro@gnu.org' to 
fix the problem. For some reason, no clump or object label could be initialized 
for the given coordinate within the given Segment image.
 EOF
-      cropped_masked=$cropped
-  else
-
-      # Crop the object and clump image to same size as desired stamp.
-      cropclp=$tmpdir/cropped-clumps-$objectid.fits
-      cropobj=$tmpdir/cropped-objects-$objectid.fits
-      astcrop $segment --hdu=OBJECTS --mode=img \
-              --center=$xcenter,$ycenter \
-              --width=$xstampwidth,$ystampwidth --output=$cropobj $quiet
-      astcrop $segment --hdu=CLUMPS --mode=img \
-              --center=$xcenter,$ycenter \
-              --width=$xstampwidth,$ystampwidth --output=$cropclp $quiet
-
-      # Mask all the undesired regions.
-      cropped_masked=$tmpdir/cropped-masked-$objectid.fits
-      astarithmetic $cropped --hdu=1 set-i --output=$cropped_masked \
-                    $cropobj --hdu=1 set-o \
-                    $cropclp --hdu=1 set-c \
-                    \
-                    c o $olab ne 0 where c $clab eq -1 where 0 gt set-cmask \
-                    o o $olab eq 0 where set-omask \
-                    i omask cmask or nan where
-  fi
+        cropped_masked=$cropped
+
+    else
+        # Crop the object and clump image to same size as desired stamp.
+        cropclp=$tmpdir/cropped-clumps-$objectid.fits
+        cropobj=$tmpdir/cropped-objects-$objectid.fits
+        astcrop $segment --hdu=OBJECTS --mode=img \
+                --center=$xcenter,$ycenter \
+                --width=$xstampwidth,$ystampwidth --output=$cropobj $quiet
+        astcrop $segment --hdu=CLUMPS --mode=img \
+                --center=$xcenter,$ycenter \
+                --width=$xstampwidth,$ystampwidth --output=$cropclp $quiet
+
+        # Mask all the undesired regions.
+        cropped_masked=$tmpdir/cropped-masked-$objectid.fits
+        astarithmetic $cropped --hdu=1 set-i --output=$cropped_masked \
+                      $cropobj --hdu=1 set-o \
+                      $cropclp --hdu=1 set-c \
+                      \
+                      c o $olab ne 0 where c $clab eq -1 where 0 gt set-cmask \
+                      o o $olab eq 0 where set-omask \
+                      i omask cmask or nan where
+    fi
 else
-  cropped_masked=$cropped
+    cropped_masked=$cropped
 fi
 
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index fc94a36d..21f8f44f 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -25846,21 +25846,10 @@ Objects that are not the central object.
 Clumps (within the central object) that are not the central clump.
 @end itemize
 The result is that all objects and clumps that contaminate the central source 
are masked, while the diffuse flux of the central object remains.
-The size of the region that is used for identifying the central clump/object 
label can be specified by the option @option{--corewidth}, see below.
 The non masked object and clump labels are kept into the header of the output 
image.
 The keywords are @code{CLABEL} and @code{OLABEL}.
 If no segmentation image is used, then their values are set to @code{none}.
 
-@item -w INT
-@itemx --corewidth=INT
-Width of the region that is used to compute the central clump/object label 
over the segmentation image @option{--segment}, with the goal of not masking 
them.
-It is rarely necessary to change this option.
-
-If a segmentation image is provided, then it is necessary to not mask the 
central object+clump (see description of @option{--segment}).
-To do that, the clump and object images are cropped around the coordinate 
given to @option{--center}, but on the two labeled images.
-Assuming a single value exists within the crop, the IDs of the respective 
object and clump are found.
-Therefore it is necessary for this option to be small (the program will abort 
if more than one label exists within it).
-
 @item -N STR
 @itemx --normop=STR
 The operator for measuring the values within the ring defined by the option 
@option{--normradii}.
@@ -26124,17 +26113,6 @@ Objects that are not the central object.
 Clumps (within the central object) that are not the central clump.
 @end itemize
 The result is that all objects and clumps that contaminate the central source 
are masked, while the diffuse flux of the central object remains.
-The size of the region that is used for identifying the central clump/object 
label can be specified by the option @option{--corewidth}, see below.
-
-@item -w INT
-@itemx --corewidth=INT
-Width of the region that is used to compute the central clump/object label 
over the segmentation image @option{--segment}, with the goal of not masking 
them.
-It is rarely necessary to change this option.
-
-If a segmentation image is provided, then it is necessary to not mask the 
central object+clump (see description of @option{--segment}).
-To do that, the clump and object images are cropped around the coordinate 
given to @option{--center}, but on the two labeled images.
-Assuming a single value exists within the crop, the IDs of the respective 
object and clump are found.
-Therefore it is necessary for this option to be small (the program will abort 
if more than one label exists within it).
 
 @item -s FLT,FLT
 @itemx --sigmaclip=FLT,FLT



reply via email to

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