gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 0290432: Query: functions for querying Gaia mo


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 0290432: Query: functions for querying Gaia moved to gaia.c
Date: Sun, 27 Sep 2020 18:32:51 -0400 (EDT)

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

    Query: functions for querying Gaia moved to gaia.c
    
    So far, the only database that 'astquery' can submit queries to is
    Gaia. Because of this, the functions to work on the Gaia catalog were
    defined in the core `query.c' file. However, we will soon be adding many
    more databases, and its hard to manage them all in one file.
    
    With this commit, to allow easy addition of new databases, the Gaia-specfic
    functions have been moved to 'gaia.c' and 'gaia.h'.
---
 bin/query/Makefile.am         |   4 +-
 bin/query/{query.c => gaia.c} | 126 ++-----------------------
 bin/query/{query.h => gaia.h} |  15 +--
 bin/query/query.c             | 214 +-----------------------------------------
 bin/query/query.h             |   6 +-
 bin/query/ui.c                |  39 ++++++++
 bin/query/ui.h                |   3 +-
 7 files changed, 61 insertions(+), 346 deletions(-)

diff --git a/bin/query/Makefile.am b/bin/query/Makefile.am
index 89f1f10..49d88f4 100644
--- a/bin/query/Makefile.am
+++ b/bin/query/Makefile.am
@@ -39,9 +39,9 @@ bin_PROGRAMS = astquery
 astquery_LDADD = $(top_builddir)/bootstrapped/lib/libgnu.la -lgnuastro \
                  $(MAYBE_NORPATH)
 
-astquery_SOURCES = main.c ui.c query.c
+astquery_SOURCES = main.c ui.c query.c gaia.c
 
-EXTRA_DIST = main.h authors-cite.h args.h ui.h query.h
+EXTRA_DIST = main.h authors-cite.h args.h ui.h query.h gaia.h
 
 
 
diff --git a/bin/query/query.c b/bin/query/gaia.c
similarity index 70%
copy from bin/query/query.c
copy to bin/query/gaia.c
index 94a488e..d07407a 100644
--- a/bin/query/query.c
+++ b/bin/query/gaia.c
@@ -1,5 +1,5 @@
 /*********************************************************************
-Query - Retreive data from a remote data server.
+Gaia Query: retrieve tables from Gaia catalog.
 Query is part of GNU Astronomy Utilities (Gnuastro) package.
 
 Original author:
@@ -25,62 +25,21 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <stdio.h>
 #include <errno.h>
 #include <error.h>
-#include <stdlib.h>
 #include <string.h>
 
 #include <gnuastro/wcs.h>
-#include <gnuastro/pointer.h>
-
 #include <gnuastro-internal/checkset.h>
 
 #include "main.h"
-#include "query.h"
-
-
-
-
-
-char *
-query_strlist_to_str(gal_list_str_t *input)
-{
-  char *out=NULL;
-  gal_list_str_t *node;
-  size_t n, nn, nnodes=0, alllen=0;
 
-  /* First calculate the full length of all nodes. */
-  for(node=input; node!=NULL; node=node->next)
-    {
-      /* We'll add two extra for each. One for the ',' that must come in
-         between it and the next one. One just for a buffer, incase we
-         haven't accounted for something. */
-      alllen += strlen(node->v) + 2;
-      ++nnodes;
-    }
+#include "ui.h"
 
-  /* Allocate the output string. */
-  out=gal_pointer_allocate(GAL_TYPE_STRING, alllen, 1, "out", __func__);
-
-  /* Write all the strings into the allocated space. */
-  n=nn=0;
-  for(node=input; node!=NULL; node=node->next)
-    {
-      if(nn++==nnodes-1)
-        sprintf(out+n, "%s", node->v);
-      else
-        n += sprintf(out+n, "%s,", node->v);
-    }
-
-  /* Return the merged string. */
-  return out;
-}
 
 
 
 
-
-/* Gaia database. */
-void
-query_gaia_sanitycheck(struct queryparams *p)
+static void
+gaia_sanitycheck(struct queryparams *p)
 {
   /* Make sure that atleast one type of constraint is specified. */
   if(p->query==NULL && p->center==NULL && p->overlapwith==NULL)
@@ -130,7 +89,7 @@ query_gaia_sanitycheck(struct queryparams *p)
 
 
 void
-query_gaia(struct queryparams *p)
+gaia_query(struct queryparams *p)
 {
   size_t ndim;
   gal_data_t *tmp;
@@ -140,7 +99,7 @@ query_gaia(struct queryparams *p)
   double *ocenter=NULL, *owidth=NULL, *omin=NULL, *omax=NULL;
 
   /* Make sure everything is fine. */
-  query_gaia_sanitycheck(p);
+  gaia_sanitycheck(p);
 
 
   /* If the raw query has been given, use it. */
@@ -150,7 +109,7 @@ query_gaia(struct queryparams *p)
     {
       /* If certain columns have been requested use them, otherwise
          download all existing columns.*/
-      columns = p->columns ? query_strlist_to_str(p->columns) : allcols;
+      columns = p->columns ? ui_strlist_to_str(p->columns) : allcols;
 
       /* If the user wanted an overlap with an image, then calculate it. */
       if(p->overlapwith)
@@ -231,74 +190,3 @@ query_gaia(struct queryparams *p)
           p->cp.quiet==0 ? "" : " (the command can be printed "
           "if you don't use the option '--quiet', or '-q')");
 }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-void
-query_check_download(struct queryparams *p)
-{
-  size_t len;
-  int status=0;
-  char *logname;
-  fitsfile *fptr;
-
-  /* Open the FITS file and if the status value is still zero, it means
-     everything worked properly. */
-  fits_open_file(&fptr, p->cp.output, READONLY, &status);
-  if(status==0) fits_close_file(fptr, &status);
-  else
-    {
-      /* Add a '.log' suffix to the output filename. */
-      len=strlen(p->cp.output);
-      logname=gal_pointer_allocate(GAL_TYPE_STRING, len+10, 1,
-                                   __func__, "logname");
-      sprintf(logname, "%s.log", p->cp.output);
-
-      /* Rename the output file to the logname file and let the user
-         know. */
-      rename(p->cp.output, logname);
-      error(EXIT_FAILURE, 0, "the requested dataset could not be retreived! "
-            "For more, please see '%s'", logname);
-    }
-}
-
-
-
-
-
-void
-query(struct queryparams *p)
-{
-  /* Download the dataset. */
-  switch(p->database)
-    {
-    case QUERY_DATABASE_GAIA: query_gaia(p); break;
-    default:
-      error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to address "
-            "the problem. '%d' is not a recognized database code", __func__,
-            PACKAGE_BUGREPORT, p->database);
-    }
-
-  /* Make sure that the result is a readable FITS file, otherwise, abort
-     with an error. */
-  query_check_download(p);
-
-  /* Let the user know that things went well. */
-  if(p->cp.quiet==0)
-    printf("Query output written to: %s\n", p->cp.output);
-}
diff --git a/bin/query/query.h b/bin/query/gaia.h
similarity index 83%
copy from bin/query/query.h
copy to bin/query/gaia.h
index dc5409b..018ff6e 100644
--- a/bin/query/query.h
+++ b/bin/query/gaia.h
@@ -1,5 +1,5 @@
 /*********************************************************************
-Query - Retreive data from a remote data server.
+Gaia Query: retrieve tables from Gaia catalog.
 Query is part of GNU Astronomy Utilities (Gnuastro) package.
 
 Original author:
@@ -20,17 +20,12 @@ General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/
-#ifndef query_H
-#define query_H
-
-enum query_databases
-{
-  QUERY_DATABASE_INVALID,
-  QUERY_DATABASE_GAIA,
-};
+#ifndef GAIA_H
+#define GAIA_H
 
+#include "main.h"
 
 void
-query(struct queryparams *p);
+gaia_query(struct queryparams *p);
 
 #endif
diff --git a/bin/query/query.c b/bin/query/query.c
index 94a488e..260e702 100644
--- a/bin/query/query.c
+++ b/bin/query/query.c
@@ -33,222 +33,12 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 #include <gnuastro-internal/checkset.h>
 
-#include "main.h"
+#include "gaia.h"
 #include "query.h"
 
 
 
 
-
-char *
-query_strlist_to_str(gal_list_str_t *input)
-{
-  char *out=NULL;
-  gal_list_str_t *node;
-  size_t n, nn, nnodes=0, alllen=0;
-
-  /* First calculate the full length of all nodes. */
-  for(node=input; node!=NULL; node=node->next)
-    {
-      /* We'll add two extra for each. One for the ',' that must come in
-         between it and the next one. One just for a buffer, incase we
-         haven't accounted for something. */
-      alllen += strlen(node->v) + 2;
-      ++nnodes;
-    }
-
-  /* Allocate the output string. */
-  out=gal_pointer_allocate(GAL_TYPE_STRING, alllen, 1, "out", __func__);
-
-  /* Write all the strings into the allocated space. */
-  n=nn=0;
-  for(node=input; node!=NULL; node=node->next)
-    {
-      if(nn++==nnodes-1)
-        sprintf(out+n, "%s", node->v);
-      else
-        n += sprintf(out+n, "%s,", node->v);
-    }
-
-  /* Return the merged string. */
-  return out;
-}
-
-
-
-
-
-/* Gaia database. */
-void
-query_gaia_sanitycheck(struct queryparams *p)
-{
-  /* Make sure that atleast one type of constraint is specified. */
-  if(p->query==NULL && p->center==NULL && p->overlapwith==NULL)
-    error(EXIT_FAILURE, 0, "no '--query', '--center' or '--overlapwith' "
-          "specified. At least one of these options are necessary in the "
-          "Gaia dataset");
-
-  /* If '--center' is given, '--radius' is also necessary. */
-  if(p->center || p->overlapwith)
-    {
-      /* Make sure the radius is given, and that it isn't zero. */
-      if(p->overlapwith==NULL && p->radius==NULL && p->width==NULL)
-        error(EXIT_FAILURE, 0, "the '--radius' ('-r') or '--width' ('-w') "
-              "options are necessary with the '--center' ('-C') option");
-
-      /* Make sure a dataset is also given. */
-      if( p->datasetstr==NULL)
-        error(EXIT_FAILURE, 0, "the '--dataset' ('-s') option is necessary "
-              "with the '--center' ('-C') option");
-
-      /* Use simpler names for the commonly used datasets. */
-      if( !strcmp(p->datasetstr, "dr2") )
-        {
-          free(p->datasetstr);
-          gal_checkset_allocate_copy("gaiadr2.gaia_source", &p->datasetstr);
-        }
-      else if( !strcmp(p->datasetstr, "dr1") )
-        {
-          free(p->datasetstr);
-          gal_checkset_allocate_copy("gaiadr1.gaia_source", &p->datasetstr);
-        }
-      else if( !strcmp(p->datasetstr, "hipparcos") )
-        {
-          free(p->datasetstr);
-          gal_checkset_allocate_copy("public.hipparcos", &p->datasetstr);
-        }
-      else if( !strcmp(p->datasetstr, "tyco2") )
-        {
-          free(p->datasetstr);
-          gal_checkset_allocate_copy("public.tyco2", &p->datasetstr);
-        }
-    }
-}
-
-
-
-
-
-void
-query_gaia(struct queryparams *p)
-{
-  size_t ndim;
-  gal_data_t *tmp;
-  double width2, *center, *darray;
-  char *tmpstr, *regionstr, *rangestr=NULL;
-  char *command, *columns, allcols[]="*", *querystr;
-  double *ocenter=NULL, *owidth=NULL, *omin=NULL, *omax=NULL;
-
-  /* Make sure everything is fine. */
-  query_gaia_sanitycheck(p);
-
-
-  /* If the raw query has been given, use it. */
-  if(p->query)
-    querystr=p->query;
-  else
-    {
-      /* If certain columns have been requested use them, otherwise
-         download all existing columns.*/
-      columns = p->columns ? query_strlist_to_str(p->columns) : allcols;
-
-      /* If the user wanted an overlap with an image, then calculate it. */
-      if(p->overlapwith)
-        gal_wcs_coverage(p->overlapwith, p->cp.hdu, &ndim, &ocenter,
-                         &owidth, &omin, &omax);
-
-      /* For easy reading. */
-      center = p->overlapwith ? ocenter : p->center->array;
-
-      /* Write the region. */
-      if(p->radius)
-        {
-          darray=p->radius->array;
-          if( asprintf(&regionstr, "CIRCLE('ICRS', %.8f, %.8f, %g)",
-                       center[0], center[1], darray[0])<0 )
-            error(EXIT_FAILURE, 0, "%s: asprintf allocation ('regionstr')",
-                  __func__);
-        }
-      else if(p->width || p->overlapwith)
-        {
-          darray = p->overlapwith ? owidth : p->width->array;
-          width2 = ( (p->overlapwith || p->width->size==2)
-                     ? darray[1] : darray[0] );
-          if( asprintf( &regionstr, "BOX('ICRS', %.8f, %.8f, %.8f, %.8f)",
-                        center[0], center[1], darray[0], width2 )<0 )
-            error(EXIT_FAILURE, 0, "%s: asprintf allocation ('regionstr')",
-                  __func__);
-        }
-
-      /* Set the range criteria on the requested columns. */
-      if(p->range)
-        for(tmp=p->range; tmp!=NULL; tmp=tmp->next)
-          {
-            darray=tmp->array;
-            if( asprintf(&tmpstr, "%s%sAND %s>=%g AND %s<=%g",
-                         rangestr==NULL ? "" : rangestr,
-                         rangestr==NULL ? "" : " ",
-                         tmp->name, darray[0], tmp->name, darray[1]) < 0 )
-              error(EXIT_FAILURE, 0, "%s: asprintf allocation ('tmpstr')",
-                    __func__);
-            free(rangestr);
-            rangestr=tmpstr;
-          }
-
-      /* Write the automatically generated query string. */
-      if( asprintf(&querystr,  "SELECT %s "
-                   "FROM %s "
-                   "WHERE 1=CONTAINS( POINT('ICRS', ra, dec), %s ) %s",
-                   columns, p->datasetstr, regionstr,
-                   rangestr ? rangestr : "")<0 )
-        error(EXIT_FAILURE, 0, "%s: asprintf allocation ('querystr')",
-              __func__);
-
-      /* Clean up. */
-      free(regionstr);
-      if(columns!=allcols) free(columns);
-      if(p->overlapwith)
-        {free(ocenter); free(owidth); free(omin); free(omax);}
-    }
-
-
-  /* Build the calling command. */
-  if( asprintf(&command, "curl -o%s --form LANG=ADQL --form FORMAT=fits "
-               "--form REQUEST=doQuery --form QUERY=\"%s\" "
-               "https://gea.esac.esa.int/tap-server/tap/sync";, p->cp.output,
-               querystr)<0 )
-    error(EXIT_FAILURE, 0, "%s: asprintf allocation ('command')", __func__);
-
-
-  /* Print the calling command for the user to know. */
-  if(p->cp.quiet==0)
-    printf("Running: %s\n", command);
-
-  /* Run the command. */
-  if(system(command))
-    error(EXIT_FAILURE, 0, "the query download command %sfailed%s\n",
-          p->cp.quiet==0 ? "printed above " : "",
-          p->cp.quiet==0 ? "" : " (the command can be printed "
-          "if you don't use the option '--quiet', or '-q')");
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 void
 query_check_download(struct queryparams *p)
 {
@@ -287,7 +77,7 @@ query(struct queryparams *p)
   /* Download the dataset. */
   switch(p->database)
     {
-    case QUERY_DATABASE_GAIA: query_gaia(p); break;
+    case QUERY_DATABASE_GAIA: gaia_query(p); break;
     default:
       error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to address "
             "the problem. '%d' is not a recognized database code", __func__,
diff --git a/bin/query/query.h b/bin/query/query.h
index dc5409b..28d1df0 100644
--- a/bin/query/query.h
+++ b/bin/query/query.h
@@ -20,8 +20,10 @@ General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/
-#ifndef query_H
-#define query_H
+#ifndef QUERY_H
+#define QUERY_H
+
+#include "main.h"
 
 enum query_databases
 {
diff --git a/bin/query/ui.c b/bin/query/ui.c
index 13962b8..7bce4de 100644
--- a/bin/query/ui.c
+++ b/bin/query/ui.c
@@ -29,6 +29,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <string.h>
 
 #include <gnuastro/fits.h>
+#include <gnuastro/pointer.h>
 
 #include <gnuastro-internal/timing.h>
 #include <gnuastro-internal/options.h>
@@ -234,6 +235,44 @@ ui_parse_database(struct argp_option *option, char *arg,
 
 
 
+char *
+ui_strlist_to_str(gal_list_str_t *input)
+{
+  char *out=NULL;
+  gal_list_str_t *node;
+  size_t n, nn, nnodes=0, alllen=0;
+
+  /* First calculate the full length of all nodes. */
+  for(node=input; node!=NULL; node=node->next)
+    {
+      /* We'll add two extra for each. One for the ',' that must come in
+         between it and the next one. One just for a buffer, incase we
+         haven't accounted for something. */
+      alllen += strlen(node->v) + 2;
+      ++nnodes;
+    }
+
+  /* Allocate the output string. */
+  out=gal_pointer_allocate(GAL_TYPE_STRING, alllen, 1, "out", __func__);
+
+  /* Write all the strings into the allocated space. */
+  n=nn=0;
+  for(node=input; node!=NULL; node=node->next)
+    {
+      if(nn++==nnodes-1)
+        sprintf(out+n, "%s", node->v);
+      else
+        n += sprintf(out+n, "%s,", node->v);
+    }
+
+  /* Return the merged string. */
+  return out;
+}
+
+
+
+
+
 
 
 
diff --git a/bin/query/ui.h b/bin/query/ui.h
index 599f952..c320e33 100644
--- a/bin/query/ui.h
+++ b/bin/query/ui.h
@@ -64,7 +64,8 @@ enum option_keys_enum
 
 
 
-
+char *
+ui_strlist_to_str(gal_list_str_t *input);
 
 void
 ui_read_check_inputs_setup(int argc, char *argv[], struct queryparams *p);



reply via email to

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