[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 89be245 7/9: Crop and Table: --polygon now acc
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 89be245 7/9: Crop and Table: --polygon now accepts filenames |
Date: |
Fri, 21 May 2021 23:39:19 -0400 (EDT) |
branch: master
commit 89be245e212d9aeede9ca9fc02fe698890b95f86
Author: Natáli D. Anzanello <natali.anzanello@ufrgs.br>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Crop and Table: --polygon now accepts filenames
Until now, we used the '--polygonfile' option to specify the DS9 region
filename containing the polygon.
With this commit, the '--polygon' option also accepts a filename of a DS9
region file. Therefore, the '--polygonfile' option was removed. To do this,
a check in 'gal_options_parse_colon_sep_csv' (in 'lib/options.c') was added.
---
bin/crop/args.h | 13 -----------
bin/crop/main.h | 1 -
bin/crop/ui.c | 66 ++++++++++++++----------------------------------------
bin/crop/ui.h | 1 -
bin/table/args.h | 15 +------------
bin/table/main.h | 1 -
bin/table/ui.c | 35 -----------------------------
bin/table/ui.h | 1 -
lib/ds9.c | 19 ++++++++--------
lib/gnuastro/ds9.h | 2 +-
lib/options.c | 10 +++++++--
11 files changed, 37 insertions(+), 127 deletions(-)
diff --git a/bin/crop/args.h b/bin/crop/args.h
index dd1b7d3..7eddd38 100644
--- a/bin/crop/args.h
+++ b/bin/crop/args.h
@@ -307,19 +307,6 @@ struct argp_option program_options[] =
GAL_OPTIONS_NOT_MANDATORY,
GAL_OPTIONS_NOT_SET
},
- {
- "polygonfile",
- UI_KEY_POLYGONFILE,
- "REG",
- 0,
- "Input polygon filename made in DS9.",
- UI_GROUP_REGION,
- &p->polygonfile,
- GAL_TYPE_STRING,
- GAL_OPTIONS_RANGE_ANY,
- GAL_OPTIONS_NOT_MANDATORY,
- GAL_OPTIONS_NOT_SET
- },
diff --git a/bin/crop/main.h b/bin/crop/main.h
index 919d186..f18a88c 100644
--- a/bin/crop/main.h
+++ b/bin/crop/main.h
@@ -99,7 +99,6 @@ struct cropparams
gal_data_t *polygon; /* Input string of polygon vertices. */
uint8_t polygonout; /* ==1: Keep the inner polygon region. */
uint8_t polygonsort; /* Don't sort polygon vertices. */
- char *polygonfile; /* Name of input polygon file. */
/* Internal */
size_t numin; /* Number of input images. */
diff --git a/bin/crop/ui.c b/bin/crop/ui.c
index 4c2e18b..b22e4c9 100644
--- a/bin/crop/ui.c
+++ b/bin/crop/ui.c
@@ -270,51 +270,6 @@ ui_parse_coordinate_mode(struct argp_option *option, char
*arg,
/**************************************************************/
/*************** Sanity Check *******************/
/**************************************************************/
-
-/* Do polygon-related sanity checks that can be very low-level. */
-static void
-ui_check_polygon_from_ds9(struct cropparams *p)
-{
- int ds9regmode;
-
- /* This is only relevant when a region file is actually given. */
- if(p->polygonfile)
- {
- /* These two options cannot be called together. */
- if(p->polygon)
- error(EXIT_FAILURE, errno, "'--polygon' and '--polygonfile' "
- "cannot be given together. With the first you specify the "
- "polygon vertices directly on the command-line. With the "
- "second, you give a DS9 region file and the polygon "
- "vertices are read from that.");
- else
- {
- /* Extract the polygon and the coordinate mode. */
- p->polygon=gal_ds9_reg_read_polygon(p->polygonfile,
- &ds9regmode);
- switch(ds9regmode)
- {
- case GAL_DS9_COORD_MODE_IMG: p->mode=IMGCROP_MODE_IMG; break;
- case GAL_DS9_COORD_MODE_WCS: p->mode=IMGCROP_MODE_WCS; break;
- default:
- error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at "
- "'%s' to fix the problem. The output coordinate mode "
- "of 'gal_ds9_reg_read_polygon' (%d) isn't recognized "
- "by this function", __func__, PACKAGE_BUGREPORT,
- ds9regmode);
- }
- }
-
- /* Clean up. */
- free(p->polygonfile);
- p->polygonfile=NULL;
- }
-}
-
-
-
-
-
/* Read and check ONLY the options. When arguments are involved, do the
check in 'ui_check_options_and_arguments'. */
static void
@@ -323,10 +278,23 @@ ui_read_check_only_options(struct cropparams *p)
double *darray;
int i, checksum;
- /* If a DS9 region file should be used for the polygon, read it. This is
- done first for two reasons. 1) It can change the value of '--mode'. 2)
- It will set the '--polygon' option's value (if not set). */
- ui_check_polygon_from_ds9(p);
+ /* If it's on the polygon mode and a filename was passed to polygon, then
+ we have to set the mode. It can change the value of '--mode', so this
+ is checked first. */
+ if(p->polygon && p->polygon->status)
+ {
+ switch(p->polygon->status)
+ {
+ case GAL_DS9_COORD_MODE_IMG: p->mode=IMGCROP_MODE_IMG; break;
+ case GAL_DS9_COORD_MODE_WCS: p->mode=IMGCROP_MODE_WCS; break;
+ default:
+ error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at "
+ "'%s' to fix the problem. The output coordinate mode "
+ "of 'gal_ds9_reg_read_polygon' (%d) isn't recognized "
+ "by this function", __func__, PACKAGE_BUGREPORT,
+ p->polygon->status);
+ }
+ }
/* Make sure that only one of the crop definitions is given. */
checksum = ( (p->center!=NULL)
diff --git a/bin/crop/ui.h b/bin/crop/ui.h
index d6f4326..9a34fa2 100644
--- a/bin/crop/ui.h
+++ b/bin/crop/ui.h
@@ -70,7 +70,6 @@ enum option_keys_enum
UI_KEY_HENDWCS,
UI_KEY_POLYGONOUT,
UI_KEY_POLYGONSORT,
- UI_KEY_POLYGONFILE,
UI_KEY_CHECKCENTER,
UI_KEY_PRIMARYIMGHDU,
};
diff --git a/bin/table/args.h b/bin/table/args.h
index 91991d3..7e07bff 100644
--- a/bin/table/args.h
+++ b/bin/table/args.h
@@ -224,7 +224,7 @@ struct argp_option program_options[] =
{
"polygon",
UI_KEY_POLYGON,
- "FLT:FLT[,...]",
+ "STR,FLT:FLT[,...]",
0,
"Polygon for '--inpolygon' or '--outpolygon'.",
UI_GROUP_OUTROWS,
@@ -236,19 +236,6 @@ struct argp_option program_options[] =
gal_options_parse_colon_sep_csv
},
{
- "polygonfile",
- UI_KEY_POLYGONFILE,
- "REG",
- 0,
- "Polygon filename for '--inpolygon' or '--outpolygon'.",
- UI_GROUP_OUTROWS,
- &p->polygonfile,
- GAL_TYPE_STRING,
- GAL_OPTIONS_RANGE_ANY,
- GAL_OPTIONS_NOT_MANDATORY,
- GAL_OPTIONS_NOT_SET
- },
- {
"equal",
UI_KEY_EQUAL,
"STR,FLT[,...]",
diff --git a/bin/table/main.h b/bin/table/main.h
index 11235bd..065302a 100644
--- a/bin/table/main.h
+++ b/bin/table/main.h
@@ -98,7 +98,6 @@ struct tableparams
gal_data_t *inpolygon; /* Columns to check if inside polygon. */
gal_data_t *outpolygon; /* Columns to check if outside polygon. */
gal_data_t *polygon; /* Values of vertices of the polygon. */
- char *polygonfile; /* Name of input polygon file */
gal_data_t *equal; /* Values to keep in output. */
gal_data_t *notequal; /* Values to not include in output. */
char *sort; /* Column name or number for sorting. */
diff --git a/bin/table/ui.c b/bin/table/ui.c
index 50845b9..2751230 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -222,37 +222,6 @@ parse_opt(int key, char *arg, struct argp_state *state)
/**************************************************************/
/*************** Sanity Check *******************/
/**************************************************************/
-/* Do polygon-related sanity checks */
-static void
-ui_check_polygon_from_ds9(struct tableparams *p)
-{
- int ds9regmode;
-
- /* This is only relevant when a region file is actually given. */
- if(p->polygonfile)
- {
- /* These two options cannot be called together. */
- if(p->polygon)
- error(EXIT_FAILURE, errno, "'--polygon' and '--polygonfile' "
- "cannot be given together. With the first you specify the "
- "polygon vertices directly on the command-line. With the "
- "second, you give a DS9 region file and the polygon "
- "vertices are read from that.");
-
- /* Extract the polygon and the coordinate mode. */
- else
- p->polygon=gal_ds9_reg_read_polygon(p->polygonfile, &ds9regmode);
-
- /* Clean up. */
- free(p->polygonfile);
- p->polygonfile=NULL;
- }
-}
-
-
-
-
-
/* Read and check ONLY the options. When arguments are involved, do the
check in 'ui_check_options_and_arguments'. */
static void
@@ -261,10 +230,6 @@ ui_read_check_only_options(struct tableparams *p)
double *darr;
gal_data_t *tmp;
- /* If a polygon filename is given, use it. This is done first because it
- can set the '--polygon' option's value. */
- ui_check_polygon_from_ds9(p);
-
/* Check if the format of the output table is valid, given the type of
the output. */
gal_tableintern_check_fits_format(p->cp.output, p->cp.tableformat);
diff --git a/bin/table/ui.h b/bin/table/ui.h
index 8f13311..a898b19 100644
--- a/bin/table/ui.h
+++ b/bin/table/ui.h
@@ -73,7 +73,6 @@ enum option_keys_enum
UI_KEY_ROWRANDOM,
UI_KEY_INPOLYGON,
UI_KEY_OUTPOLYGON,
- UI_KEY_POLYGONFILE,
UI_KEY_CATCOLUMNRAWNAME,
};
diff --git a/lib/ds9.c b/lib/ds9.c
index 707665f..fbea66b 100644
--- a/lib/ds9.c
+++ b/lib/ds9.c
@@ -40,22 +40,20 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
/* Read the polygon specified in the given DS9 region file and parse it in
the standard format. */
gal_data_t *
-gal_ds9_reg_read_polygon(char *filename, int *coordmode)
+gal_ds9_reg_read_polygon(char *filename)
{
FILE *fp;
char *polygonstr;
gal_data_t *out=NULL;
size_t commacounter=0;
size_t plinelen, linesize=10, lineno=0;
+ int coordmode=GAL_DS9_COORD_MODE_INVALID;
char *c, *line, *ds9regstart="# Region file format: DS9";
char *polygonformaterr="It is expected for the line to have "
"this format: 'polygon(AAA,BBB,...)'. Where 'AAA' and 'BBB' "
"are numbers and '...' signifies that any number of points "
"are possible";
- /* Initialize 'coordmode'. */
- *coordmode=GAL_DS9_COORD_MODE_INVALID;
-
/* Allocate size to the lines on the file and check if it was sucessfull.
The getline function reallocs the necessary memory. */
errno=0;
@@ -92,13 +90,13 @@ gal_ds9_reg_read_polygon(char *filename, int *coordmode)
if( !strcmp(line, "fk5\n") || !strcmp(line, "image\n") )
{
/* Make sure it hasn't been called more than once. */
- if(*coordmode!=GAL_DS9_COORD_MODE_INVALID)
+ if(coordmode!=GAL_DS9_COORD_MODE_INVALID)
error_at_line(EXIT_FAILURE, 0, filename, lineno,
"more than one coordinate line defined");
/* Set the proper mode. */
- if(!strcmp(line, "fk5\n")) *coordmode=GAL_DS9_COORD_MODE_WCS;
- else *coordmode=GAL_DS9_COORD_MODE_IMG;
+ if(!strcmp(line, "fk5\n")) coordmode=GAL_DS9_COORD_MODE_WCS;
+ else coordmode=GAL_DS9_COORD_MODE_IMG;
/* Stop parsing the file if the polygon has also been found by
this point (we don't need any more information, no need to
@@ -135,12 +133,12 @@ gal_ds9_reg_read_polygon(char *filename, int *coordmode)
/* Stop parsing the file if the coordinate mode has also been
found by this point (we don't need any more information, no
need to waste the user's CPU and time). */
- if(*coordmode!=GAL_DS9_COORD_MODE_INVALID) break;
+ if(coordmode!=GAL_DS9_COORD_MODE_INVALID) break;
}
}
/* If no coordinate mode was found in the file, print an error. */
- if(*coordmode==GAL_DS9_COORD_MODE_INVALID)
+ if(coordmode==GAL_DS9_COORD_MODE_INVALID)
error(EXIT_FAILURE, 0, "%s: no coordinate mode found! "
"We expect one line to be either 'fk5' or 'image'",
filename);
@@ -152,6 +150,9 @@ gal_ds9_reg_read_polygon(char *filename, int *coordmode)
"file given to '--polygonfile' option. %s", filename,
polygonformaterr);
+ /* Write the coordinate mode into the status component. */
+ out->status = coordmode;
+
/* Clean up and return. */
free(line);
fclose(fp);
diff --git a/lib/gnuastro/ds9.h b/lib/gnuastro/ds9.h
index 41e5719..3aef2fe 100644
--- a/lib/gnuastro/ds9.h
+++ b/lib/gnuastro/ds9.h
@@ -63,7 +63,7 @@ enum gal_ds9_coord_modes
gal_data_t *
-gal_ds9_reg_read_polygon(char *filename, int *coordmode);
+gal_ds9_reg_read_polygon(char *filename);
diff --git a/lib/options.c b/lib/options.c
index 2dbbb04..6149840 100644
--- a/lib/options.c
+++ b/lib/options.c
@@ -31,6 +31,7 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
#include <gnuastro/wcs.h>
#include <gnuastro/git.h>
#include <gnuastro/txt.h>
+#include <gnuastro/ds9.h>
#include <gnuastro/fits.h>
#include <gnuastro/list.h>
#include <gnuastro/data.h>
@@ -1529,8 +1530,13 @@ gal_options_parse_colon_sep_csv(struct argp_option
*option, char *arg,
error_at_line(EXIT_FAILURE, 0, filename, lineno, "no value "
"given to '--%s'", option->name);
- /* Parse the desired format and put it in this option's pointer. */
- dataset=gal_options_parse_colon_sep_csv_raw(arg, filename, lineno);
+ /* Check if the argument is a string (it contains a ':' or a ',') or
+ a filename. Then parse the desired format and put it in this
+ option's pointer. */
+ if( strchr(arg, ',')==NULL && strchr(arg, ':')==NULL )
+ dataset=gal_ds9_reg_read_polygon(arg);
+ else
+ dataset=gal_options_parse_colon_sep_csv_raw(arg, filename, lineno);
/* Add the given dataset to the end of an existing dataset. */
existing = *(gal_data_t **)(option->value);
- [gnuastro-commits] master updated (27df4f2 -> 3dff1e4), Mohammad Akhlaghi, 2021/05/21
- [gnuastro-commits] master d3cfd8e 1/9: Crop: added option to crop polygon from ds9 file, Mohammad Akhlaghi, 2021/05/21
- [gnuastro-commits] master 326b42a 2/9: Crop: polished the implementation of new --polygonname option, Mohammad Akhlaghi, 2021/05/21
- [gnuastro-commits] master 05c660a 3/9: Library: new ds9.h library functions for parsing ds9 files, Mohammad Akhlaghi, 2021/05/21
- [gnuastro-commits] master cbf8a79 4/9: Table: added the --polygonname option, Mohammad Akhlaghi, 2021/05/21
- [gnuastro-commits] master c3d437d 8/9: Book: adjusted --polygon option, Mohammad Akhlaghi, 2021/05/21
- [gnuastro-commits] master 564da39 5/9: Book: new --polygonname option and new ds9 library added, Mohammad Akhlaghi, 2021/05/21
- [gnuastro-commits] master 89be245 7/9: Crop and Table: --polygon now accepts filenames,
Mohammad Akhlaghi <=
- [gnuastro-commits] master d2bca9d 6/9: Crop and Table: --polygonfile new name for --polygonname, Mohammad Akhlaghi, 2021/05/21
- [gnuastro-commits] master 3dff1e4 9/9: Book: edited gal_ds9_reg_read_polygon from DS9 Library, corrected NEWS, Mohammad Akhlaghi, 2021/05/21