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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
|
/*********************************************************************
Table - View and manipulate a FITS table structures.
Table is part of GNU Astronomy Utilities (Gnuastro) package.
Original author:
Mohammad Akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2019-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 <string.h>
#include <stdlib.h>
#include <gnuastro/wcs.h>
#include <gnuastro/type.h>
#include <gnuastro/pointer.h>
#include <gnuastro/statistics.h>
#include <gnuastro-internal/checkset.h>
#include <gnuastro-internal/arithmetic-set.h>
#include "main.h"
#include "ui.h"
#include "arithmetic.h"
/*********************************************************************/
/******************** List operations *********************/
/*********************************************************************/
static struct arithmetic_token *
arithmetic_add_new_to_end(struct arithmetic_token **list)
{
struct arithmetic_token *tmp, *node;
/* Allocate a new node. */
errno=0;
node=malloc(sizeof *node);
if(node==NULL)
error(EXIT_FAILURE, errno, "%s: couldn't allocate new node (%zu bytes)",
__func__, sizeof *node);
/* Initialize its elements. */
node->next=NULL;
node->loadcol=NULL;
node->name_def=NULL;
node->name_use=NULL;
node->constant=NULL;
node->id_at_usage=NULL;
node->index=GAL_BLANK_SIZE_T;
node->num_at_usage=GAL_BLANK_SIZE_T;
node->operator=GAL_ARITHMETIC_OP_INVALID;
/* If the list already has elements, go to the last node in the list and
add this node. */
if(*list)
{
for(tmp=*list;tmp->next!=NULL;tmp=tmp->next) {}
tmp->next=node;
}
else
*list=node;
/* Return a pointer to this node (to use temporarily). */
return node;
}
void
arithmetic_token_free(struct arithmetic_token *list)
{
struct arithmetic_token *tmp;
while(list!=NULL)
{
/* Free allocated elements first. */
if(list->name_def) free(list->name_def);
if(list->name_use) free(list->name_use);
/* Set the next list node, and free this one. */
tmp=list->next;
free(list);
list=tmp;
}
}
/*********************************************************************/
/******************** User-interface *********************/
/*********************************************************************/
static char *
arithmetic_operator_name(int operator)
{
char *out=gal_arithmetic_operator_string(operator);
/* If the operator wasn't in the library, see if it was defined here. */
if(out==NULL)
switch(operator)
{
case ARITHMETIC_TABLE_OP_SET: out="set"; break;
case ARITHMETIC_TABLE_OP_WCSTOIMG: out="wcs-to-img"; break;
case ARITHMETIC_TABLE_OP_IMGTOWCS: out="img-to-wcs"; break;
case ARITHMETIC_TABLE_OP_DATETOSEC: out="date-to-sec"; break;
case ARITHMETIC_TABLE_OP_DISTANCEFLAT: out="distance-flat"; break;
case ARITHMETIC_TABLE_OP_DATETOMILLISEC:
out="date-to-millisec"; break;
case ARITHMETIC_TABLE_OP_DISTANCEONSPHERE:
out="distance-on-sphere"; break;
case ARITHMETIC_TABLE_OP_SORTEDTOINTERVAL:
out="sorted-to-interval"; break;
case ARITHMETIC_TABLE_OP_EQJ2000TOFLAT:
out="eq-j2000-to-flat"; break;
case ARITHMETIC_TABLE_OP_EQJ2000FROMFLAT:
out="eq-j2000-from-flat"; break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to fix "
"the problem. %d is not a recognized operator code", __func__,
PACKAGE_BUGREPORT, operator);
}
return out;
}
static void
arithmetic_init_wcs(struct tableparams *p, char *operator)
{
/* If a WCS hasn't been read yet, read it.*/
if(p->wcs==NULL)
{
/* A small sanity check. */
if(p->wcsfile==NULL || p->wcshdu==NULL)
error(EXIT_FAILURE, 0, "'--wcsfile' and '--wcshdu' are necessary "
"for the '%s' operator", operator);
/* Read the WCS. */
p->wcs=gal_wcs_read(p->wcsfile, p->wcshdu, p->cp.wcslinearmatrix,
0, 0, &p->nwcs, "--wcshdu");
if(p->wcs==NULL)
error(EXIT_FAILURE, 0, "%s (hdu: %s): no WCS could be read by "
"WCSLIB", p->wcsfile, p->wcshdu);
}
}
/* Set the operator code from the given string. */
static int
arithmetic_set_operator(struct tableparams *p, char *string,
size_t *num_operands, int *inlib)
{
/* Use the library's main function for its own operators. */
int op=gal_arithmetic_set_operator(string, num_operands);
/* Mark if this operator is in the library or not. */
*inlib = op==GAL_ARITHMETIC_OP_INVALID ? 0 : 1;
/* Set the operator and number of operands. */
if( op==GAL_ARITHMETIC_OP_INVALID )
{
/* Simple operators. */
if( !strcmp(string, "wcs-to-img"))
{ op=ARITHMETIC_TABLE_OP_WCSTOIMG; *num_operands=0; }
else if( !strcmp(string, "img-to-wcs"))
{ op=ARITHMETIC_TABLE_OP_IMGTOWCS; *num_operands=0; }
else if( !strcmp(string, "eq-j2000-to-flat"))
{ op=ARITHMETIC_TABLE_OP_EQJ2000TOFLAT; *num_operands=0; }
else if( !strcmp(string, "eq-j2000-from-flat"))
{ op=ARITHMETIC_TABLE_OP_EQJ2000FROMFLAT; *num_operands=0; }
else if( !strcmp(string, "date-to-sec"))
{ op=ARITHMETIC_TABLE_OP_DATETOSEC; *num_operands=0; }
else if( !strcmp(string, "date-to-millisec"))
{ op=ARITHMETIC_TABLE_OP_DATETOMILLISEC; *num_operands=0; }
else if( !strcmp(string, "distance-flat"))
{ op=ARITHMETIC_TABLE_OP_DISTANCEFLAT; *num_operands=0; }
else if( !strcmp(string, "distance-on-sphere"))
{ op=ARITHMETIC_TABLE_OP_DISTANCEONSPHERE; *num_operands=0; }
else if( !strcmp(string, "sorted-to-interval"))
{ op=ARITHMETIC_TABLE_OP_SORTEDTOINTERVAL; *num_operands=0; }
else
{ op=GAL_ARITHMETIC_OP_INVALID; *num_operands=GAL_BLANK_INT; }
}
/* Operator specific operations. */
switch(op)
{
case ARITHMETIC_TABLE_OP_WCSTOIMG:
case ARITHMETIC_TABLE_OP_IMGTOWCS:
arithmetic_init_wcs(p, string);
break;
}
/* Return the operator. */
return op;
}
/* When a column identifier (name or number) is identified, we need to make
sure if that the identifier belongs to the first input or not. */
static void
arithmetic_init_addcol(char *token, struct arithmetic_token *node,
gal_list_str_t **colstoread, size_t *totcalled,
gal_data_t *colinfo, size_t numcols)
{
char *id;
int inmaininput=0;
size_t i, num=GAL_BLANK_SIZE_T;
/* This needs to be defined after 'num'. */
void *numptr=#
/* Set the identifier. */
id = ( (token[0]=='$' && isdigit(token[1]))
? &token[1] /* Column num. (starting with '$'). */
: token ); /* Column name, just add it. */
/* See if the column identifier is a number, then 'num' will not be a
blank value. */
if( gal_type_from_string(&numptr, id, GAL_TYPE_SIZE_T) )
num=GAL_BLANK_SIZE_T;
/* See if the column exists in the main input catalog. */
if(num==GAL_BLANK_SIZE_T) /* ID is a string. */
{
for(i=0;i<numcols;++i)
if( colinfo[i].name && !strcasecmp(id, colinfo[i].name) )
{ inmaininput=1; break; }
}
else /* ID is a number. */
if(num<=numcols) inmaininput=1;
/* If the given ID is in the main input, then add it to the list of
columns to read ('colstoread') from the main input. Otherwise, keep
the ID for usage at the time of arithmetic (can happen when
'--catcolumnfile' is used).*/
if(inmaininput)
{
gal_list_str_add(colstoread, id, 1);
node->index=*totcalled;
*totcalled+=1;
}
else
{
gal_checkset_allocate_copy(id, &node->id_at_usage);/* Future usage. */
node->num_at_usage=num; /* Avoid converting to a number again! */
node->index=GAL_BLANK_SIZE_T; /* Crash if not used properly! */
}
}
/* Initialize each column from an arithmetic operation. */
void
arithmetic_init(struct tableparams *p, struct arithmetic_token **arith,
gal_list_str_t **colstoread, size_t *totcalled,
char *expression, gal_data_t *colinfo, size_t numcols)
{
void *num;
size_t one=1;
uint8_t ntype;
gal_data_t *col;
struct arithmetic_token *atmp, *node=NULL;
struct gal_options_common_params *cp=&p->cp;
char *token=NULL, *saveptr, *delimiter=" \t";
/* Parse all the given tokens. */
token=strtok_r(expression, delimiter, &saveptr);
while(token!=NULL)
{
/* Allocate and initialize this arithmetic token. */
node=arithmetic_add_new_to_end(arith);
/* See if the token is an operator, if not check other cases. */
node->operator=arithmetic_set_operator(p, token,
&node->num_operands,
&node->inlib);
if(node->operator==GAL_ARITHMETIC_OP_INVALID)
{
/* Token is a single number.*/
if( (num=gal_type_string_to_number(token, &ntype)) )
node->constant=gal_data_alloc(num, ntype, 1, &one, NULL, 0,
-1, 1, NULL, NULL, NULL);
/* This is a 'set-' operator. */
else if( !strncmp(token, GAL_ARITHMETIC_SET_PREFIX,
GAL_ARITHMETIC_SET_PREFIX_LENGTH) )
{
node->num_operands=0;
node->operator=ARITHMETIC_TABLE_OP_SET;
gal_checkset_allocate_copy(token, &node->name_def);
}
/* This is a 'load-col' operator. */
else if( (col=gal_arithmetic_load_col(token, cp->searchin,
cp->ignorecase,
cp->minmapsize,
cp->quietmmap))!=NULL )
node->loadcol=col;
/* Non-pre-defined situation.*/
else
{
/* See if this is an already defined name. */
for(atmp=*arith; atmp!=NULL; atmp=atmp->next)
if( atmp->name_def
&& strcmp(token,
( atmp->name_def
+ GAL_ARITHMETIC_SET_PREFIX_LENGTH) )==0 )
gal_checkset_allocate_copy(token, &node->name_use);
/* If it wasn't found to be a pre-defined name, then its a
column operand (column number or name). */
if(node->name_use==NULL)
arithmetic_init_addcol(token, node, colstoread,
totcalled, colinfo, numcols);
}
}
/* Go to the next token. */
token=strtok_r(NULL, delimiter, &saveptr);
}
}
/* Set the final index of each package of columns (possibly containing
processing columns that will change in number and contents). */
void
arithmetic_indexs_final(struct tableparams *p)
{
size_t startind;
size_t i, numcols;
struct column_pack *tmp;
struct arithmetic_token *atmp;
/* Set the column array that will allow removal of some read columns
(where operations will be done). */
p->colarray=gal_list_data_to_array_ptr(p->table, &p->numcolarray);
/* go over each package of columns. */
for(tmp=p->colpack;tmp!=NULL;tmp=tmp->next)
{
/* If we are on an arithmetic operation. */
if(tmp->arith)
{
for(atmp=tmp->arith;atmp!=NULL;atmp=atmp->next)
if(atmp->index!=GAL_BLANK_SIZE_T)
{
/* Small sanity check. */
if(p->colmatch[atmp->index]!=1)
error(EXIT_FAILURE, 0, "arithmetic operands can "
"(currently) only correspond to a single "
"column, but more than one match was found "
"for a column you used in column arithmetic");
/* Update the index in the full list of read columns. */
numcols=0;
for(i=0;i<atmp->index;++i) numcols+=p->colmatch[i];
atmp->index=numcols;
}
}
/* A simple column. */
else
{
/* See where the starting column for this patch of simple columns
is. */
startind=0;
for(i=0;i<tmp->start;++i) startind+=p->colmatch[i];
/* How many of the read columns are associated with the this
patch of columns. */
numcols=0;
for(i=0;i<tmp->numsimple;++i) numcols+=p->colmatch[tmp->start+i];
/* Update the values. */
tmp->start=startind;
tmp->numsimple=numcols;
}
}
}
/*********************************************************************/
/******************** Low-level tools *********************/
/*********************************************************************/
static void
arithmetic_placeholder_name(gal_data_t *col)
{
static size_t counter=0;
/* Increment counter next time this function is called. */
++counter;
/* Free any possibly existing metadata. */
if(col->name) free(col->name);
if(col->unit) free(col->unit);
if(col->comment) free(col->comment);
/* Set the new meta-data. */
errno=0;
if( asprintf(&col->name, "ARITH_%zu", counter)==-1 )
error(EXIT_FAILURE, errno, "%s: asprintf error for name", __func__);
if( asprintf(&col->unit, "arith_unit_%zu", counter)==-1)
error(EXIT_FAILURE, errno, "%s: asprintf error for unit", __func__);
if( asprintf(&col->comment, "Column from arithmetic operation %zu",
counter)==-1 )
error(EXIT_FAILURE, errno, "%s: asprintf error for comment", __func__);
}
static gal_data_t *
arithmetic_stack_pop(gal_data_t **stack, int operator, char *errormsg)
{
size_t i;
gal_data_t *out, *top=*stack;
/* Update the stack (where necessary). */
switch(operator)
{
/* Operators don't need to pop the dataset from the stack (they just
need the metadata). */
case GAL_ARITHMETIC_OP_INDEX:
case GAL_ARITHMETIC_OP_COUNTER:
out=gal_data_alloc_empty(top->ndim, top->minmapsize, top->quietmmap);
for(i=0;i<top->ndim;++i) out->dsize[i]=top->dsize[i];
out->size=top->size;
break;
/* Operators that actually need the data. */
default:
out=*stack;
if(*stack)
*stack=(*stack)->next;
else
error(EXIT_FAILURE, 0, "not enough operands for '%s'%s",
arithmetic_operator_name(operator), errormsg?errormsg:"");
}
/* Arithmetic changes the contents of a dataset, so the old name (in the
FITS 'EXTNAME' keyword) or units must not be used beyond this
point. Furthermore, in Arithmetic, the 'name' element is used to
identify variables (with the 'set-' operator). */
if(out->name) { free(out->name); out->name=NULL; }
if(out->unit) { free(out->unit); out->unit=NULL; }
if(out->comment) { free(out->comment); out->comment=NULL; }
/* Remove the 'next' element to break from the stack and return. */
out->next=NULL;
return out;
}
/* Wrapper function to pop operands within the 'set-' operator. */
static gal_data_t *
arithmetic_stack_pop_wrapper_set(void *in)
{
struct gal_arithmetic_set_params *tprm
= (struct gal_arithmetic_set_params *)in;
gal_data_t **stack=(gal_data_t **)tprm->params;
return arithmetic_stack_pop(stack, ARITHMETIC_TABLE_OP_SET, NULL);
}
/* For the 'set-' operator. */
static int
arithmetic_set_name_used_later(void *in, char *name)
{
struct gal_arithmetic_set_params *p=(struct gal_arithmetic_set_params *)in;
struct arithmetic_token *tokens = (struct arithmetic_token *)(p->tokens);
struct arithmetic_token *token;
size_t counter=0;
/* If the name exists after the current token, then return 1. Note that
we have already separated the usage of names set with 'set-' in the
'name_use' element of the 'token' structure. */
for(token=tokens;token!=NULL;token=token->next)
{
if( token->name_use
&& counter > p->tokencounter
&& strcmp(token->name_use, name)==0 )
return 1;
/* Increment the counter (has to be after the check above because it
may not always get to the 'counter' variable). */
++counter;
}
/* If we get to this point, it means that the name doesn't exist. */
return 0;
}
/* Set the converted column metadata. */
static void
arithmetic_update_metadata(gal_data_t *col, char *name, char *unit,
char *comment)
{
if(col)
{
if(col->name) free(col->name);
if(col->unit) free(col->unit);
if(col->comment) free(col->comment);
gal_checkset_allocate_copy(name, &col->name);
gal_checkset_allocate_copy(unit, &col->unit);
gal_checkset_allocate_copy(comment, &col->comment);
}
}
/*********************************************************************/
/******************** Operations *********************/
/*********************************************************************/
static void
arithmetic_wcs(struct tableparams *p, gal_data_t **stack, int operator)
{
gal_data_t *tmp;
char errormsg[100];
struct wcsprm *wcs=p->wcs;
size_t i, ndim=p->wcs->naxis;
gal_data_t *coord[3]={NULL, NULL, NULL};
/* Prepare the (possibly necessaary!) error message for the number of
popped operand. */
sprintf(errormsg, " (input WCS has %zu dimensions)", ndim);
/* Pop all the necessary datasets and make sure they are
double-precision. NOTE: the top dataset on the stack is the
highest-dimensional dataset. */
for(i=0;i<ndim;++i)
{
tmp=arithmetic_stack_pop(stack, operator, errormsg);
tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_FLOAT64);
coord[ndim-i-1]=tmp;
}
/* Define the list of coordinates. */
if(coord[1]) coord[0]->next=coord[1];
if(coord[2]) coord[1]->next=coord[2];
/* Final preparations. */
if(operator==ARITHMETIC_TABLE_OP_WCSTOIMG)
{
/* Do the conversion. */
gal_wcs_world_to_img(coord[0], wcs, 1);
/* For image coordinates, we don't need much precision. */
for(i=0;i<ndim;++i)
coord[i]=gal_data_copy_to_new_type_free(coord[i],
GAL_TYPE_FLOAT32);
/* Set the names, units and comments for each dataset. */
arithmetic_update_metadata(coord[0], "X", "pixel",
"Converted from WCS");
arithmetic_update_metadata(coord[1], "Y", "pixel",
"Converted from WCS");
arithmetic_update_metadata(coord[2], "Z", "pixel",
"Converted from WCS");
}
else
{
gal_wcs_img_to_world(coord[0], wcs, 1);
arithmetic_update_metadata(coord[0], wcs->ctype[0], wcs->cunit[0],
"Converted from pixel coordinates");
arithmetic_update_metadata(coord[1], coord[1]?wcs->ctype[1]:NULL,
coord[1]?wcs->cunit[1]:NULL,
"Converted from pixel coordinates");
arithmetic_update_metadata(coord[2], coord[2]?wcs->ctype[2]:NULL,
coord[2]?wcs->cunit[2]:NULL,
"Converted from pixel coordinates");
}
/* Reverse the column orders and put them on the stack. */
for(i=0;i<ndim;++i)
{
coord[i]->next=NULL;
gal_list_data_add(stack, coord[i]);
}
}
static void
arithmetic_curved_flat(struct tableparams *p, gal_data_t **stack,
int operator)
{
struct wcsprm *wcs=NULL;
char *ctype[2], *cunit[2];
gal_data_t *w1, *w2, *proj, *refw1, *refw2;
double ref[2], cdelt[2]={1.0f,1.0f}, pc[4]={-1,0,0,1};
char **strarr, ctype1[9], ctype2[9]; /* 9=8+1 (keylength + '\0') */
/* Pop the necessary datasets from the stack (order is important). */
proj=arithmetic_stack_pop(stack, operator, NULL); /* 1st: projection.*/
refw2=arithmetic_stack_pop(stack, operator, NULL);/* 2nd: Ref. Dec. */
refw1=arithmetic_stack_pop(stack, operator, NULL);/* 3rd: Ref. RA. */
w2=arithmetic_stack_pop(stack, operator, NULL); /* 4th: RA column. */
w1=arithmetic_stack_pop(stack, operator, NULL); /* 5th: Dec column. */
/* Basic sanity checks. */
strarr=proj->array;
if(proj->size!=1 || refw1->size!=1 || refw2->size!=1)
error(EXIT_FAILURE, 0, "the first, second and third popped operands "
"from the '%s' operator should have a single element (they "
"should not be columns, but a single value), but they "
"respectively have %zu, %zu, %zu columns. Recall that the "
"first popped operand is the right-most operand (closest to "
"the operator", arithmetic_operator_name(operator),
proj->size, refw1->size, refw2->size);
if(proj->type!=GAL_TYPE_STRING)
error(EXIT_FAILURE, 0, "the first popped operand to the '%s' "
"operator must be a string, not numeric. It should be the "
"three character projection identifier of the WCS standard "
"in FITS (for example 'TAN' or 'MOL')",
arithmetic_operator_name(operator));
if(gal_wcs_projection_name_to_id(strarr[0])==GAL_WCS_PROJECTION_INVALID)
error(EXIT_FAILURE, 0, "the first popped operand to the '%s' "
"operator ('%s') could not be interpretted as a pre-defined "
"projection in the WCS stanadard of FITS",
arithmetic_operator_name(operator), strarr[0]);
/* Convert the numeric inputs into the float64 and put the reference
values into the expected array.*/
refw1=gal_data_copy_to_new_type_free(refw1, GAL_TYPE_FLOAT64);
refw2=gal_data_copy_to_new_type_free(refw2, GAL_TYPE_FLOAT64);
w1=gal_data_copy_to_new_type_free(w1, GAL_TYPE_FLOAT64);
w2=gal_data_copy_to_new_type_free(w2, GAL_TYPE_FLOAT64);
ref[0]=((double *)(refw1->array))[0];
ref[1]=((double *)(refw2->array))[0];
/* Construct the CTYPEi strings */
sprintf(ctype1, "%s---%s", "RA", strarr[0]);
sprintf(ctype2, "%s--%s", "DEC", strarr[0]);
ctype[0]=ctype1;
ctype[1]=ctype2;
cunit[0]="deg";
cunit[1]="deg";
/* Build the WCS structure for the conversion. */
wcs=gal_wcs_create(ref, ref, cdelt, pc, cunit, ctype, 2,
GAL_WCS_LINEAR_MATRIX_PC);
/* For a check:
int nkeyrec;
char *wcsstr;
wcsstr=gal_wcs_write_wcsstr(wcs, &nkeyrec);
printf("%s\n", wcsstr); exit(0);
*/
/* Put the second world coordinate as the next token of the first, and do
the conversion. */
w1->next=w2;
switch(operator)
{
case ARITHMETIC_TABLE_OP_EQJ2000TOFLAT:
gal_wcs_world_to_img(w1, wcs, 1);
break;
case ARITHMETIC_TABLE_OP_EQJ2000FROMFLAT:
gal_wcs_img_to_world(w1, wcs, 1);
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at '%s' to "
"fix the problem. The integer '%d' is not a recognized "
"operator identifier", __func__, PACKAGE_BUGREPORT, operator);
}
/* Put the output datasets in the stack in reverse order. */
w1->next=w2->next=NULL;
gal_list_data_add(stack, w1);
gal_list_data_add(stack, w2);
/* Clean up (the coordinate conversion was done in place, so 'w1' and
'w2' are the output datasets). */
gal_wcs_free(wcs);
gal_data_free(proj);
gal_data_free(refw1);
gal_data_free(refw2);
}
static double
arithmetic_distance_flat(double a1, double a2, double b1, double b2)
{
double d1=a1-b1, d2=a2-b2;
return sqrt(d1*d1 + d2*d2);
}
static void
arithmetic_distance(struct tableparams *p, gal_data_t **stack,
int operator)
{
size_t i, j;
double *o, *a1, *a2, *b1, *b2;
gal_data_t *a, *b, *tmp, *out;
char *colname=NULL, *colcomment=NULL;
double (*distance_func)(double, double, double, double)=NULL;
/* Pop the columns for point 'b'.*/
tmp=arithmetic_stack_pop(stack, operator, NULL);
tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_FLOAT64);
b=arithmetic_stack_pop(stack, operator, NULL);
b=gal_data_copy_to_new_type_free(b, GAL_TYPE_FLOAT64);
b->next=tmp;
/* Pop the columns for point 'a'.*/
tmp=arithmetic_stack_pop(stack, operator, NULL);
tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_FLOAT64);
a=arithmetic_stack_pop(stack, operator, NULL);
a=gal_data_copy_to_new_type_free(a, GAL_TYPE_FLOAT64);
a->next=tmp;
/* Make sure the sizes are consistant: note that one point can have a
single coordinate, but we don't know which one. */
if(a->size!=a->next->size)
error(EXIT_FAILURE, 0, "the sizes of the third and fourth operands "
"of the '%s' operator (respectively containing %zu and %zu "
"numbers) must be equal", arithmetic_operator_name(operator),
a->next->size, a->size);
if(b->size!=b->next->size)
error(EXIT_FAILURE, 0, "the sizes of the first and second operands "
"of the '%s' operator (respectively containing %zu and %zu "
"numbers) must be equal", arithmetic_operator_name(operator),
b->next->size, b->size);
/* Basic settings based on the operator. */
switch(operator)
{
case ARITHMETIC_TABLE_OP_DISTANCEFLAT:
colname="dist-flat";
distance_func=arithmetic_distance_flat;
colcomment="Distance measured on a flat surface.";
break;
case ARITHMETIC_TABLE_OP_DISTANCEONSPHERE:
colname="dist-spherical";
distance_func=gal_wcs_angular_distance_deg;
colcomment="Distance measured on a great circle.";
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to "
"fix the problem. The operator code %d isn't recognized",
__func__, PACKAGE_BUGREPORT, operator);
}
/* Make the output array based on the largest size. */
out=gal_data_alloc(NULL, GAL_TYPE_FLOAT64, 1,
(a->size>b->size ? &a->size : &b->size), NULL, 0,
p->cp.minmapsize, p->cp.quietmmap, colname, NULL,
colcomment);
/* Measure the distances (if the dataset isn't empty: it can be!). */
if(out->size>0 && out->array)
{
o=out->array;
a1=a->array; a2=a->next->array;
b1=b->array; b2=b->next->array;
if(a->size==1 || b->size==1) /* One of them is a single point. */
for(i=0;i<a->size;++i)
for(j=0;j<b->size;++j)
o[a->size>b->size?i:j] = distance_func(a1[i], a2[i], b1[j],
b2[j]);
else /* Both have the same length. */
for(i=0;i<a->size;++i) /* (all were originally from same table) */
o[i] = distance_func(a1[i], a2[i], b1[i], b2[i]);
}
/* Clean up and put the output dataset onto the stack. */
gal_list_data_free(a);
gal_list_data_free(b);
gal_list_data_add(stack, out);
}
/* Convert the ISO date format to seconds since Unix time. */
static void
arithmetic_datetosec(struct tableparams *p, gal_data_t **stack,
int operator)
{
size_t i, v;
int64_t *iarr;
gal_data_t *out;
double subsec=NAN;
char *subsecstr=NULL;
char *unit=NULL, *name=NULL, *comment=NULL;
/* Input dataset. */
gal_data_t *in=arithmetic_stack_pop(stack, operator, NULL);
char **strarr=in->array;
/* Make sure the input has a 'string' type. */
if(in->type!=GAL_TYPE_STRING)
error(EXIT_FAILURE, 0, "the operand given to 'date-to-sec' "
"should have a string type, but it is '%s'",
gal_type_name(in->type, 1));
/* Output metadata. */
switch(operator)
{
case ARITHMETIC_TABLE_OP_DATETOSEC:
unit="sec";
name="UNIXSEC";
comment="Unix seconds (from 00:00:00 UTC, 1 January 1970)";
break;
case ARITHMETIC_TABLE_OP_DATETOMILLISEC:
unit="msec";
name="UNIXMILLISEC";
comment="Unix milli-seconds (from 00:00:00 UTC, 1 January 1970)";
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s "
"to fix the problem. The operator code %d isn't "
"recognized", __func__, PACKAGE_BUGREPORT, operator);
}
/* Allocate the output dataset. */
out=gal_data_alloc(NULL, GAL_TYPE_INT64, 1, &in->size, NULL, 1,
p->cp.minmapsize, p->cp.quietmmap, name, unit,
comment);
/* Convert each input string into number of seconds. Note that it is
possible to have an empty dataset, in that case, we shouldn't do
anything.*/
if(out->size>0 && out->array)
{
iarr=out->array;
for(i=0; i<in->size; ++i)
{
/* Read the number of seconds and sub-seconds and write into the
output. */
v=gal_fits_key_date_to_seconds(strarr[i], &subsecstr,
&subsec);
iarr[i] = ( v==GAL_BLANK_SIZE_T
? GAL_BLANK_INT64
: ( operator == ARITHMETIC_TABLE_OP_DATETOSEC
? v
: (isnan(subsec)
? v*1000
: v*1000 + (int64_t)(subsec*1000) ) ) );
}
}
/* Clean up and put the resulting calculation back on the stack. */
if(in) gal_data_free(in);
gal_list_data_add(stack, out);
}
/* Convert the ISO date format to seconds since Unix time. */
static void
arithmetic_sortedtointerval(struct tableparams *p, gal_data_t **stack,
int operator)
{
int f32out;
size_t i, size;
double *in, *min, *max;
gal_data_t *min_d, *max_d;
/* Input dataset. */
gal_data_t *in_d=arithmetic_stack_pop(stack, operator, NULL);
/* Make sure the input is sorted and is numeric */
if(in_d->type==GAL_TYPE_STRING)
error(EXIT_FAILURE, 0, "the operand given to 'sortedtointerval' "
"should have a numeric data type");
if(gal_statistics_is_sorted(in_d, 1)==0)
error(EXIT_FAILURE, 0, "the operand given to 'sortedtointerval' "
"should be sorted");
/* Convert the input to double precision for internal processing. In the
end we'll convert the outputs to float32 unless the input was
originally float64. */
f32out = in_d->type!=GAL_TYPE_FLOAT64;
in_d=gal_data_copy_to_new_type_free(in_d, GAL_TYPE_FLOAT64);
/* Allocate the output columns (minimum and maximum of each interval). */
min_d=gal_data_alloc(NULL, GAL_TYPE_FLOAT64, 1, in_d->dsize, NULL, 0,
in_d->minmapsize, in_d->quietmmap,
NULL, NULL, NULL);
max_d=gal_data_alloc(NULL, GAL_TYPE_FLOAT64, 1, in_d->dsize, NULL, 0,
in_d->minmapsize, in_d->quietmmap,
NULL, NULL, NULL);
/* Fill the minimum and maximum intervals. Just note that the minimum
value of the first interval and maximum of the last element need
special attention (we'll extrapolate from the after/before elements to
preserve constant spacing in case that is the case). */
in=in_d->array;
size=in_d->size;
min=min_d->array;
max=max_d->array;
min[0] = in[0] - (in[1] - in[0] )/2;
max[0] = in[0] + (in[1] - in[0] )/2;
min[size-1] = in[size-1] - (in[size-1] - in[size-2])/2;
max[size-1] = in[size-1] + (in[size-1] - in[size-2])/2;
for(i=1;i<size-1;++i)
{
min[i] = max[i-1];
max[i] = in[i] + (in[i+1]-in[i])/2;
}
/* Convert the output to float32, unless the input was float64. */
if(f32out)
{
min_d=gal_data_copy_to_new_type_free(min_d, GAL_TYPE_FLOAT32);
max_d=gal_data_copy_to_new_type_free(max_d, GAL_TYPE_FLOAT32);
}
/* Clean up and put the resulting calculation back on the stack. */
max_d->next=min_d;
gal_data_free(in_d);
gal_list_data_add(stack, max_d);
}
/*********************************************************************/
/******************** Operations *********************/
/*********************************************************************/
static void
arithmetic_operator_run(struct tableparams *p,
struct arithmetic_token *token,
struct gal_arithmetic_set_params *setprm,
gal_data_t **stack)
{
int flags=GAL_ARITHMETIC_FLAGS_BASIC;
gal_data_t *o, *d1=NULL, *d2=NULL, *d3=NULL, *d4=NULL, *d5=NULL;
/* Set the operating-mode flags if necessary. */
if(p->cp.quiet) flags |= GAL_ARITHMETIC_FLAG_QUIET;
if(p->envseed) flags |= GAL_ARITHMETIC_FLAG_ENVSEED;
/* If this operator is in the library, we should pop everything here. */
if(token->inlib)
{
/* Pop the necessary number of operators. Note that the
operators are poped from a linked list (which is
last-in-first-out). So for the operators which need a
specific order, the first poped operand is actally the
last (right most, in in-fix notation) input operand.*/
switch(token->num_operands)
{
case 0:
break;
case 1:
d1=arithmetic_stack_pop(stack, token->operator, NULL);
break;
case 2:
d2=arithmetic_stack_pop(stack, token->operator, NULL);
d1=arithmetic_stack_pop(stack, token->operator, NULL);
break;
case 3:
d3=arithmetic_stack_pop(stack, token->operator, NULL);
d2=arithmetic_stack_pop(stack, token->operator, NULL);
d1=arithmetic_stack_pop(stack, token->operator, NULL);
break;
case 4:
d4=arithmetic_stack_pop(stack, token->operator, NULL);
d3=arithmetic_stack_pop(stack, token->operator, NULL);
d2=arithmetic_stack_pop(stack, token->operator, NULL);
d1=arithmetic_stack_pop(stack, token->operator, NULL);
break;
case 5:
d5=arithmetic_stack_pop(stack, token->operator, NULL);
d4=arithmetic_stack_pop(stack, token->operator, NULL);
d3=arithmetic_stack_pop(stack, token->operator, NULL);
d2=arithmetic_stack_pop(stack, token->operator, NULL);
d1=arithmetic_stack_pop(stack, token->operator, NULL);
break;
case -1:
error(EXIT_FAILURE, 0, "operators with a variable number of "
"operands are not yet implemented. Please contact us at "
"%s to include them", PACKAGE_BUGREPORT);
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s "
"to fix the problem. '%zu' is not recognized as an "
"operand counter (with '%s')", __func__, PACKAGE_BUGREPORT,
token->num_operands,
arithmetic_operator_name(token->operator));
}
/* Run the arithmetic operation. Note that 'gal_arithmetic' is a
variable argument function (like printf). So the number of
arguments it uses depend on the operator. In other words, when the
operator doesn't need three operands, the extra arguments will be
ignored. */
o=gal_arithmetic(token->operator, p->cp.numthreads, flags,
d1, d2, d3, d4, d5);
if(o) gal_list_data_add(stack, o); /* o==NULL for 'free' opertor. */
/* Reset the meta-data for the element that was just put on the
stack. */
arithmetic_placeholder_name(*stack);
}
/* This operator is specific to this program (Table). */
else
{
switch(token->operator)
{
case ARITHMETIC_TABLE_OP_WCSTOIMG:
case ARITHMETIC_TABLE_OP_IMGTOWCS:
arithmetic_wcs(p, stack, token->operator);
break;
case ARITHMETIC_TABLE_OP_EQJ2000TOFLAT:
case ARITHMETIC_TABLE_OP_EQJ2000FROMFLAT:
arithmetic_curved_flat(p, stack, token->operator);
break;
case ARITHMETIC_TABLE_OP_DATETOSEC:
case ARITHMETIC_TABLE_OP_DATETOMILLISEC:
arithmetic_datetosec(p, stack, token->operator);
break;
case ARITHMETIC_TABLE_OP_DISTANCEFLAT:
case ARITHMETIC_TABLE_OP_DISTANCEONSPHERE:
arithmetic_distance(p, stack, token->operator);
break;
case ARITHMETIC_TABLE_OP_SORTEDTOINTERVAL:
arithmetic_sortedtointerval(p, stack, token->operator);
break;
case ARITHMETIC_TABLE_OP_SET:
gal_arithmetic_set_name(setprm, token->name_def);
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to "
"fix the problem. The operator code %d is not recognized",
__func__, PACKAGE_BUGREPORT, token->operator);
}
}
}
/* When the column identifier (name or number) wasn't in the main input
table (for example, it was added with '--catcolumnfile'), we need this
function to find the column to work with. */
gal_data_t *
arithmetic_read_at_usage(struct tableparams *p,
struct arithmetic_token *token)
{
uint8_t pid;
size_t i, one=1;
gal_data_t *tmp;
int hasnewline=0;
char *c, **strarr;
/* If the number is blank, then we have a name and should return the
first column in the table that has the given name. */
if(token->num_at_usage==GAL_BLANK_SIZE_T)
{
/* Search all the existing columns in the table. */
for(i=0; i<p->numcolarray; ++i)
if( p->colarray[i]->name
&& strcasecmp(p->colarray[i]->name, token->id_at_usage)==0 )
return p->colarray[i];
/* If control reaches here, then the requested name didn't exist in
the table, if the next token is the equatorial on flat operator,
then we should also check known projection identifiers. */
if( token->next
&& ( token->next->operator==ARITHMETIC_TABLE_OP_EQJ2000TOFLAT
|| token->next->operator==ARITHMETIC_TABLE_OP_EQJ2000FROMFLAT) )
{
/* If the projection is recognized, add it to the stack
otherwise, just let the default error finish the program. */
pid=gal_wcs_projection_name_to_id(token->id_at_usage);
if(pid!=GAL_WCS_PROJECTION_INVALID)
{
tmp=gal_data_alloc(NULL, GAL_TYPE_STRING, 1, &one, NULL, 0,
p->cp.minmapsize, p->cp.quietmmap, NULL,
NULL, NULL);
strarr=tmp->array;
gal_checkset_allocate_copy(token->id_at_usage, &strarr[0]);
return tmp;
}
}
/* If control reaches here, then the requested name didn't exist in
the table. This may happen because the user broke an arithmetic
command into muliple lines and forgot to put a back-slash. If so,
the string will have a new-line within it.*/
for(c=token->id_at_usage;*c!='\0';++c)
if(*c=='\n') hasnewline=1;
/* print the error message. */
error(EXIT_FAILURE, 0, "'%s' doesn't correspond to the name of "
"any column in the table until this column arithmetic%s",
token->id_at_usage, hasnewline?". Because there is a "
"new-line in the name, it is highly probable that it "
"wasn't actually a name. This can happen when you break "
"an arithmetic command into multiple lines, but have "
"forgot to put a back-slash ('\\') at the end of a "
"broken line":"");
}
/* We already have the desired column number. */
else
if(token->num_at_usage > p->numcolarray)
error(EXIT_FAILURE, 0, "%zu is an invalid column number: the "
"table only has %zu columns before this column arithmetic "
"step", token->num_at_usage, p->numcolarray);
else
return p->colarray[token->num_at_usage-1];
/* If control reaches here, there is a bug! In then end, simply return
NULL to avoid compiler warnings. */
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to find the "
"cause and fix it. This function should not reach this point",
__func__, PACKAGE_BUGREPORT);
return NULL;
}
/* Apply reverse polish mechanism for this column. */
static void
arithmetic_reverse_polish(struct tableparams *p,
struct column_pack *outpack)
{
struct arithmetic_token *token;
gal_data_t *single, *stack=NULL;
struct gal_arithmetic_set_params setprm={0};
/* Initialize the arithmetic functions/pointers. */
setprm.params=&stack;
setprm.tokens=outpack->arith;
setprm.pop=arithmetic_stack_pop_wrapper_set;
setprm.used_later=arithmetic_set_name_used_later;
/* Go through all the tokens given to this element. */
for(token=outpack->arith;token!=NULL;token=token->next)
{
/* We are on an operator. */
if(token->operator!=GAL_ARITHMETIC_OP_INVALID)
arithmetic_operator_run(p, token, &setprm, &stack);
/* We are on a named variable. */
else if( token->name_use )
gal_list_data_add(&stack,
gal_arithmetic_set_copy_named(&setprm,
token->name_use));
/* We are on a column loaded from another file. */
else if( token->loadcol )
{
gal_list_data_add(&stack, token->loadcol);
token->loadcol=NULL;
}
/* Constant number: just put it on top of the stack. */
else if(token->constant)
{
gal_list_data_add(&stack, token->constant);
token->constant=NULL;
}
/* The column wasn't in the main input. */
else if(token->id_at_usage)
{
gal_list_data_add(&stack, arithmetic_read_at_usage(p, token));
token->num_at_usage=GAL_BLANK_SIZE_T;
free(token->id_at_usage);
token->id_at_usage=NULL;
}
/* A column from the table. */
else if(token->index!=GAL_BLANK_SIZE_T)
{
if(p->colarray[token->index]->ndim!=1)
error(EXIT_FAILURE, 0, "column arithmetic currently only works "
"on single-valued columns, not vector columns");
gal_list_data_add(&stack, p->colarray[token->index]);
}
/* Un-recognized situation. */
else
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to "
"fix the problem. The token can't be identified as an "
"operator, constant or column", __func__, PACKAGE_BUGREPORT);
/* Increment the token counter. */
++setprm.tokencounter;
}
/* Put everything that remains in the stack (reversed) into the final
table. Just note that 'gal_list_data_add' behaves differently for
lists, so we'll add have to manually set the 'next' element to NULL
before adding the column to the final table. */
gal_list_data_reverse(&stack);
while(stack!=NULL)
{
/* Keep the top element in 'single' and move 'stack' to the next
element. */
single=stack;
stack=stack->next;
/* A small sanity check. */
if(single->size==1 && p->table && single->size!=p->table->size)
error(EXIT_FAILURE, 0, "the arithmetic operation resulted in a "
"single value, but other columns have also been requested "
"which have more elements/rows");
/* Set 'single->next' to NULL so it isn't treated as a list, and set
all flags to zero (the arithmetic operation may have changed what
the flags pointed to). */
single->flag=0;
single->next=NULL;
gal_list_data_add(&p->table, single);
}
}
/*********************************************************************/
/******************** High-level *********************/
/*********************************************************************/
void
arithmetic_operate(struct tableparams *p)
{
size_t i;
struct column_pack *outpack;
/* Set the final indexs and define 'colarray'. */
arithmetic_indexs_final(p);
/* From now on, we will be looking for columns from the index in
'colarray', so to keep things clean, we'll set all the 'next' elements
to NULL. */
for(i=0; i<p->numcolarray; ++i) p->colarray[i]->next=NULL;
/* We'll also reset the output table pointer, to fill it in as we
progress. */
p->table=NULL;
/* Go over each package of columns. */
for(outpack=p->colpack; outpack!=NULL; outpack=outpack->next)
{
if(outpack->arith)
arithmetic_reverse_polish(p, outpack);
else
{
for(i=0;i<outpack->numsimple;++i)
gal_list_data_add(&p->table, p->colarray[outpack->start+i]);
}
}
/* Reverse the final output to be in the proper order. Note that all the
column contents have either been moved into the new table, or have
already been freed. */
gal_list_data_reverse(&p->table);
/* Clean up. */
ui_colpack_free(p->colpack);
if(p->colarray) free(p->colarray);
p->colarray=NULL;
p->colpack=NULL;
}
|