1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/*********************************************************************
Table Access Protocol (TAP) based download of given query.
Query is part of GNU Astronomy Utilities (Gnuastro) package.
Original author:
Mohammad akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2021-2025 Free Software Foundation, Inc.
Gnuastro is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Gnuastro is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
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/>.
**********************************************************************/
#include <config.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <gnuastro/wcs.h>
#include <gnuastro-internal/checkset.h>
#include "main.h"
#include "ui.h"
/* Basic sanity checks necessary in all TAP-based databases. */
void
tap_sanity_checks(struct queryparams *p)
{
/* Checks in case a raw query isn't given. */
if(p->query==NULL)
{
/* 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->center && p->radius==NULL && p->width==NULL)
error(EXIT_FAILURE, 0, "the '--radius' ('-r') or "
"'--width' ('-w') options are necessary with "
"the '--center' ('-C') option");
}
/* If no dataset is explicitly given, let the user know that a
catalog reference is necessary. */
if( p->information==0 && p->datasetuse==NULL )
error(EXIT_FAILURE, 0, "no '--dataset' specified! To get the "
"list of available datasets (tables) in this database, "
"please run with '--information' (or '-i'). Note that some "
"databases (like VizieR) have (tens of) thousands of "
"datasets. Hence, for a fast result, its best to limit the "
"downloaded and displayed information list by also adding "
"--limitinfo=\"SEARCH\" (where 'SEARCH' can be any string "
"in the description of the dataset, usually project or "
"author names) for example:\n\n"
" astquery %s -i --limitinfo=\"SEARCH_STRING\"\n\n"
"For more, see the documentation of 'astquery' and the "
"\"Available databases\" section of the book for more:\n\n"
" info astquery\n"
" info gnuastro \"Available databases\"\n",
p->databasestr);
}
}
/* The database string needs to be quoted if it contains a slash. */
static char *
tap_dataset_quote_if_necessary(struct queryparams *p)
{
int quote=0;
char *c, *quoted;
/* Parse the string for bad characters */
for(c=p->datasetuse; *c!='\0'; ++c)
if(*c=='/') { quote=1; break; }
/* Add quotes around the database string. */
if(quote)
{
/* Allocate the new string with quotes. */
if( asprintf("ed, "\"%s\"", p->datasetuse)<0 )
error(EXIT_FAILURE, 0, "%s: asprintf allocation ('quoted')",
__func__);
}
else quoted=p->datasetuse;
/* Return the possibly quoted string. */
return quoted;
}
/* Construct the query for metadata download. */
static char *
tap_query_construct_meta(struct queryparams *p)
{
char *querystr;
/* If a dataset is given, build the query to download the metadata of
that dataset. Otherwise, get the metadata of the full database. */
if(p->datasetuse)
{
if( asprintf(&querystr, "\"SELECT * FROM TAP_SCHEMA.columns "
"WHERE table_name = '%s'\"", p->datasetuse)<0 )
error(EXIT_FAILURE, 0, "%s: asprintf allocation ('querystr')",
__func__);
}
else
{
if(p->limitinfo)
{
if( asprintf(&querystr, "\"SELECT * FROM TAP_SCHEMA.tables "
"WHERE description LIKE '%%%s%%'\"", p->limitinfo)<0 )
error(EXIT_FAILURE, 0, "%s: asprintf allocation ('querystr')",
__func__);
}
else
gal_checkset_allocate_copy("\"SELECT * FROM TAP_SCHEMA.tables\"",
&querystr);
}
/* Clean up and return. */
return querystr;
}
/* Construct the spatial-constraints criteria if necessary. */
static char *
tap_query_construct_spatial(struct queryparams *p)
{
size_t ndim;
double width2, *center, *darray;
char *regionstr=NULL, *spatialstr=NULL;
double *ocenter=NULL, *owidth=NULL, *omin=NULL, *omax=NULL;
/* If the user wanted an overlap with an image, then calculate it. */
if(p->overlapwith)
{
/* Calculate the Sky coverage of the overlap dataset. */
gal_wcs_coverage(p->overlapwith, p->cp.hdu, &ndim, &ocenter,
&owidth, &omin, &omax, "--hdu");
/* Make sure a WCS existed in the file. */
if(owidth==NULL)
error(EXIT_FAILURE, 0, "%s (hdu %s): contains no WCS to "
"derive the sky coverage", p->overlapwith, p->cp.hdu);
}
/* For easy reading. */
center = p->overlapwith ? ocenter : p->center->array;
/* Write the region. */
if(p->radius)
{
darray=p->radius->array;
if( asprintf(®ionstr, "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( ®ionstr, "BOX('ICRS', %.8f, %.8f, %.8f, %.8f)",
center[0], center[1], darray[0], width2 )<0 )
error(EXIT_FAILURE, 0, "%s: asprintf allocation ('regionstr')",
__func__);
}
/* Build the final spatial constraint query string. Note on the
quotations: the final query is surrounded by single-quotes
('). However, we need the single quotes around 'ICRS' in this command
(both in the final string below and the ones above). So just before
the first 'ICRS', we end the single-quote and start a double quote and
keep it until the end. Finally, we add a single quote again so all
other components of the query can assume that the single-quote
environment is active.*/
if( asprintf(&spatialstr,
"1=CONTAINS( POINT('\"'ICRS', %s, %s), %s )\"'",
p->ra_name, p->dec_name, regionstr)<0 )
error(EXIT_FAILURE, 0, "%s: asprintf allocation ('querystr')",
__func__);
/* Clean up and return. */
free(regionstr);
if(p->overlapwith)
{ free(ocenter); free(owidth); free(omin); free(omax); }
return spatialstr;
}
static void
tap_query_construct_noblank(struct queryparams *p, char **outstr)
{
gal_list_str_t *tmp;
char *noblankstr=NULL, *prevstr=*outstr;
for(tmp=p->noblank; tmp!=NULL; tmp=tmp->next)
{
/* Write 'noblankstr'. */
if(prevstr)
{
if( asprintf(&noblankstr, "%s AND %s IS NOT NULL",
prevstr, tmp->v) < 0 )
error(EXIT_FAILURE, 0,
"%s: asprintf allocation ('noblankstr', 1)", __func__);
free(prevstr);
}
else
if( asprintf(&noblankstr, "%s IS NOT NULL", tmp->v) < 0 )
error(EXIT_FAILURE, 0,
"%s: asprintf allocation ('noblankstr', 2)", __func__);
/* Put the 'noblankstr' in previous-range string for the next
round.*/
prevstr=noblankstr;
}
/* Set the final output pointer. */
*outstr=noblankstr;
}
static void
tap_query_construct_range(struct queryparams *p, char **outstr)
{
double *darray;
gal_data_t *tmp;
char *rangestr=NULL, *prevstr=*outstr;
for(tmp=p->range; tmp!=NULL; tmp=tmp->next)
{
/* Write 'rangestr'. */
darray=tmp->array;
if(prevstr)
{
if( asprintf(&rangestr, "%s AND %s>=%g AND %s<=%g", prevstr,
tmp->name, darray[0], tmp->name, darray[1]) < 0 )
error(EXIT_FAILURE, 0,
"%s: asprintf allocation ('rangestr', 1)", __func__);
free(prevstr);
}
else
if( asprintf(&rangestr, "%s>=%g AND %s<=%g",
tmp->name, darray[0], tmp->name, darray[1]) < 0 )
error(EXIT_FAILURE, 0,
"%s: asprintf allocation ('rangestr', 2)", __func__);
/* Put the 'rangestr' in previous-range string for the next
round.*/
prevstr=rangestr;
}
/* Set the final output pointer. */
*outstr=rangestr;
}
static char *
tap_query_construct_sort(struct queryparams *p)
{
gal_list_str_t *tmp;
char *sortstr=NULL, *prevstr=NULL;
for(tmp=p->sort; tmp!=NULL; tmp=tmp->next)
{
/* Write 'sortstr'. */
if(prevstr)
{
if( asprintf(&sortstr, "%s,%s", prevstr, tmp->v) < 0 )
error(EXIT_FAILURE, 0,
"%s: asprintf allocation ('sortstr', 1)", __func__);
free(prevstr);
}
else
if( asprintf(&sortstr, "ORDER BY %s", tmp->v) < 0 )
error(EXIT_FAILURE, 0,
"%s: asprintf allocation ('sortstr', 2)", __func__);
/* Put the 'sortstr' in previous-range string for the next
round.*/
prevstr=sortstr;
}
/* Set the final output pointer. */
return sortstr;
}
/* Construct the query for data download. */
static char *
tap_query_construct_data(struct queryparams *p)
{
char *sortstr=NULL;
char *headstr=NULL, allcols[]="*";
char *datasetuse, *valuelimitstr=NULL;
char *querystr, *columns, *spatialstr=NULL;
/* If the dataset has special characters (like a slash) it needs to
be quoted. */
datasetuse=tap_dataset_quote_if_necessary(p);
/* If certain columns have been requested use them, otherwise
download all existing columns.*/
columns = p->columns ? ui_strlist_to_str(p->columns) : allcols;
/* If the user only wants the top few rows, only return them. */
if(p->head!=GAL_BLANK_SIZE_T)
if( asprintf(&headstr, "TOP %zu", p->head)<0 )
error(EXIT_FAILURE, 0, "%s: asprintf allocation ('head')",
__func__);
/* Build the 'noblank' and 'range' criteria. No blank goes first because
it is easier to check (for the server), thus the more time-consuming
range check can be done on fewer rows. */
if(p->noblank) tap_query_construct_noblank(p, &valuelimitstr);
if(p->range) tap_query_construct_range(p, &valuelimitstr);
/* If the user has asked for a spatial constraint. */
if(p->overlapwith || p->center)
spatialstr=tap_query_construct_spatial(p);
/* If the user has asked to sort the columns. */
if(p->sort) sortstr=tap_query_construct_sort(p);
/* Write the automatically generated query string. */
if( asprintf(&querystr, "'SELECT %s %s FROM %s %s %s %s %s %s'",
headstr ? headstr : "",
columns,
datasetuse,
( valuelimitstr || spatialstr ? "WHERE" : ""),
valuelimitstr ? valuelimitstr : "",
( valuelimitstr && spatialstr ? "AND" : "" ),
spatialstr ? spatialstr : "",
sortstr ? sortstr : "")<0 )
error(EXIT_FAILURE, 0, "%s: asprintf allocation ('querystr')",
__func__);
/* Clean up and return. */
if(datasetuse!=p->datasetuse) free(datasetuse);
if(valuelimitstr) free(valuelimitstr);
if(columns!=allcols) free(columns);
if(spatialstr) free(spatialstr);
return querystr;
}
void
tap_download(struct queryparams *p)
{
gal_list_str_t *url;
char *command, *querystr;
/* If the raw query has been given, use it. */
querystr = ( p->query
? p->query
: ( p->information
? tap_query_construct_meta(p)
: tap_query_construct_data(p) ) );
/* Go over the given URLs for this server. */
for(url=p->urls; url!=NULL; url=url->next)
{
/* Build the calling command. Note that the query quotes are
included by the function building it. */
if( asprintf(&command, "curl%s -o%s --form LANG=ADQL "
"--form FORMAT=fits --form REQUEST=doQuery "
"--form QUERY=%s %s", p->cp.quiet ? " -s" : "",
p->downloadname, querystr, url->v)<0 )
error(EXIT_FAILURE, 0, "%s: asprintf allocation ('command')",
__func__);
/* Print the calling command for the user to know. */
if(p->dryrun==1 || p->cp.quiet==0)
{
if(p->dryrun==0) printf("\n");
error(EXIT_SUCCESS, 0, "%s: %s",
p->dryrun ? "would run" : "running", command);
if(p->dryrun==0) printf("\nDownload status:\n");
}
/* Run the command: if it succeeds ('system' returns zero), then stop
parsing with a break. If it fails, then see if this is the last
URL or not. If its the last one ('url->next' is NULL), then exit
with a failure, otherwise, just let the user know that this
download failed, but continue with the next URLs. */
if(p->dryrun) break;
else
{
if(system(command)==EXIT_SUCCESS) break;
else
error(url->next ? EXIT_SUCCESS : 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')");
}
}
/* Keep the executed command (to put in the final file's meta-data). */
p->finalcommand=command;
/* Clean up. */
if(querystr!=p->query) free(querystr);
}
|