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
|
/*********************************************************************
Arithmetic - Do arithmetic operations on images.
Arithmetic is part of GNU Astronomy Utilities (Gnuastro) package.
Original author:
Mohammad Akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2015-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 <math.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <string.h>
#include <stdlib.h>
#include <gnuastro/wcs.h>
#include <gnuastro/fits.h>
#include <gnuastro/tiff.h>
#include <gnuastro/array.h>
#include <gnuastro-internal/checkset.h>
#include <gnuastro-internal/arithmetic-set.h>
#include "main.h"
#include "operands.h"
/**********************************************************************/
/************ General info on operands ***************/
/**********************************************************************/
size_t
operands_num(struct arithmeticparams *p)
{
size_t counter=0;
struct operand *tmp=NULL;
for(tmp=p->operands;tmp!=NULL;tmp=tmp->next)
++counter;
return counter;
}
/**********************************************************************/
/************ Adding to and popping from stack ***************/
/**********************************************************************/
/* When the operand to be added is a list, we don't have to worry about
filenames (external datasets) because lists can only be added */
static void
operands_add_list(struct arithmeticparams *p, gal_data_t *data)
{
gal_data_t *tmp;
struct operand *newnode;
/* First, reverse the list so when we add them, they have the proper
order. */
gal_list_data_reverse(&data);
/* Go over the list and add them to the stack. */
while(data)
{
/* Shift the 'data' pointer to the next element. */
tmp=data;
data=data->next;
/* Allocate space for the new operand. */
errno=0;
newnode=malloc(sizeof *newnode);
if(newnode==NULL)
error(EXIT_FAILURE, errno, "%s: allocating %zu bytes for 'newnode'",
__func__, sizeof *newnode);
/* Set the basic parameters. */
newnode->data=tmp;
newnode->hdu=NULL;
newnode->filename=NULL;
newnode->data->next=NULL;
/* Add this dataset to the top of the stack. */
newnode->next=p->operands;
p->operands=newnode;
}
}
void
operands_add(struct arithmeticparams *p, char *filename, gal_data_t *data)
{
int readwcs;
size_t ndim, *dsize;
struct operand *newnode;
/* In case 'data' is a list then there is no file name or HDU involved,
the given datasets were produced internally, so things are easier. */
if(data && data->next)
{
operands_add_list(p, data);
return;
}
/* Some operators might not actually return any dataset (data=NULL), in
such cases filename will also be NULL (since the operand was not added
from the command-line). So, we shouldn't add anything to the stack. */
if(data || filename)
{
/* Allocate space for the new operand. */
errno=0;
newnode=malloc(sizeof *newnode);
if(newnode==NULL)
error(EXIT_FAILURE, errno, "%s: allocating %zu bytes for 'newnode'",
__func__, sizeof *newnode);
/* If the 'filename' is the name of a dataset, then use a copy of it.
otherwise, do the basic analysis. */
if( filename
&& gal_arithmetic_set_is_name(p->setprm.named, filename) )
{
newnode->filename=NULL;
newnode->data=gal_arithmetic_set_copy_named(&p->setprm, filename);
}
else
{
/* Set the basic parameters. */
newnode->data=data;
newnode->filename=filename;
/* See if a HDU must be read or not. */
if(filename != NULL
&& ( gal_fits_file_recognized(filename)
|| gal_tiff_name_is_tiff(filename) ) )
{
/* Set the HDU for this filename. */
if(p->globalhdu)
gal_checkset_allocate_copy(p->globalhdu, &newnode->hdu);
else
newnode->hdu=gal_list_str_pop(&p->hdus);
/* If no WCS is set yet, use the WCS of this image and remove
possibly extra dimensions if necessary. */
readwcs = (p->wcsfile && !strcmp(p->wcsfile,"none")) ? 0 : 1;
if(readwcs && p->refdata.wcs==NULL)
{
/* If the HDU is an image, read its size. */
dsize = ( gal_fits_hdu_format(filename, newnode->hdu,
"--hdu")==IMAGE_HDU
? gal_fits_img_info_dim(filename,
newnode->hdu, &ndim,
"--hdu")
: NULL);
/* Read the WCS. */
p->refdata.wcs=gal_wcs_read(filename, newnode->hdu,
p->cp.wcslinearmatrix,
0, 0, &p->refdata.nwcs,
"--hdu");
/* Remove extra (length of 1) dimensions (if we had an
image HDU). */
if(dsize)
{
ndim=gal_dimension_remove_extra(ndim, dsize,
p->refdata.wcs);
free(dsize);
}
/* Let the user know that the WCS is read. */
if(p->refdata.wcs && !p->cp.quiet)
printf(" - WCS: %s (hdu %s).\n", filename,
newnode->hdu);
}
}
else newnode->hdu=NULL;
}
/* Make the link to the previous list. */
newnode->next=p->operands;
p->operands=newnode;
}
}
gal_data_t *
operands_pop(struct arithmeticparams *p, char *operator)
{
size_t i;
gal_data_t *data;
char *filename, *hdu;
struct operand *operands=p->operands;
/* If the operand linked list has finished, then give an error and
exit. */
if(operands==NULL)
error(EXIT_FAILURE, 0, "not enough operands for the '%s' operator",
operator);
/* Set the dataset. If filename is present then read the file
and fill in the array, if not then just set the array. */
if(operands->filename)
{
/* Set the HDU and filename */
hdu=operands->hdu;
filename=operands->filename;
/* Read the dataset and remove possibly extra dimensions. */
data=gal_array_read_one_ch(filename, hdu, NULL, p->cp.minmapsize,
p->cp.quietmmap, "--hdu");
data->ndim=gal_dimension_remove_extra(data->ndim, data->dsize, NULL);
/* When the reference data structure's dimensionality is non-zero, it
means that this is not the first image read. So, write its basic
information into the reference data structure for future
checks. */
if(p->refdata.ndim==0)
{
/* Set the dimensionality. */
p->refdata.ndim=(data)->ndim;
/* Allocate the dsize array. */
errno=0;
p->refdata.dsize=malloc(p->refdata.ndim
* sizeof *p->refdata.dsize);
if(p->refdata.dsize==NULL)
error(EXIT_FAILURE, errno, "%s: allocating %zu bytes for "
"p->refdata.dsize", __func__,
p->refdata.ndim * sizeof *p->refdata.dsize);
/* Write the values into it. */
for(i=0;i<p->refdata.ndim;++i)
p->refdata.dsize[i]=data->dsize[i];
}
/* Report the read image if desired: */
if(!p->cp.quiet) printf(" - Read: %s (hdu %s).\n", filename, hdu);
/* Free the HDU string: */
if(hdu) free(hdu);
/* Add to the number of popped FITS images: */
++p->popcounter;
}
else
data=operands->data;
/* Arithmetic changes the contents of a dataset, so the old name and
metadata (in the FITS 'EXTNAME' keyword for example) must not be used
beyond this point. Furthermore, in Arithmetic, the 'name' element is
used to identify variables (with the 'set-' operator). */
if(data->name) { free(data->name); data->name=NULL; }
if(data->unit) { free(data->unit); data->unit=NULL; }
if(data->comment) { free(data->comment); data->comment=NULL; }
/* Remove this node from the queue, return the data structure. */
p->operands=operands->next;
free(operands);
return data;
}
/* Wrapper to use the 'operands_pop' function with the 'set-' operator. */
gal_data_t *
operands_pop_wrapper_set(void *in)
{
struct gal_arithmetic_set_params *tprm
= (struct gal_arithmetic_set_params *)in;
struct arithmeticparams *p=(struct arithmeticparams *)tprm->params;
return operands_pop(p, "set");
}
|