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 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
|
/*
* This file is part of the UVES Pipeline
* Copyright (C) 2002, 2003, 2004, 2005 European Southern Observatory
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
*/
/*
* $Author: amodigli $
* $Date: 2013-08-08 12:31:13 $
* $Revision: 1.58 $
* $Name: not supported by cvs2svn $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/*----------------------------------------------------------------------------*/
/**
* @defgroup flames_dfs DFS related functions
*/
/*----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#include <flames_dfs.h>
#include <flames_midas_def.h>
#include <flames_pfits.h>
#include <flames_utils.h>
#include <uves_utils_wrappers.h>
#include <uves_propertylist.h>
#include <uves_dfs.h>
#include <uves_pfits.h>
#include <uves_chip.h>
#include <uves_dump.h>
#include <uves_msg.h>
#include <uves_error.h>
#include <cpl.h>
#include <string.h>
#include <stdbool.h>
/*-----------------------------------------------------------------------------
Functions prototypes
-----------------------------------------------------------------------------*/
/**@{*/
static void
load_raw_image_fib(const char *filename,
cpl_type type, bool blue,
cpl_image *raw_image[2],
uves_propertylist *raw_header[2],
uves_propertylist *rotated_header[2],
cpl_table* ext_tbl[2]);
static void
fibremask_fill(uves_propertylist *header, enum uves_chip chip, bool warn_tech,cpl_table* ozpoz);
/*----------------------------------------------------------------------------*/
/**
@brief Write HISTORY keyword
@param header to write to
@param name descriptor name
@param val value to write
**/
/*----------------------------------------------------------------------------*/
static void
set_header_int_1(uves_propertylist *header,
const char *name, int val)
{
const char *val_str = uves_sprintf("%d", val);
flames_dfs_set_history_val(header, 'I', name, val_str);
uves_free_string_const(&val_str);
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Write HISTORY keyword
@param header to write to
@param name descriptor name
@param val value to write
**/
/*----------------------------------------------------------------------------*/
void
flames_set_header_char_1(uves_propertylist *header,
const char *name, char val)
{
const char *s = uves_sprintf("%c", val);
flames_dfs_set_history_val(header, 'C', name, s);
uves_free_string_const(&s);
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Write HISTORY int array
@param header to write to
@param name descriptor name
@param val1 value to write
@param val2 value to write
@param val3 value to write
@param val4 value to write
@param val5 value to write
@param val6 value to write
@param val7 value to write
@param val8 value to write
**/
/*----------------------------------------------------------------------------*/
static void
set_header_int_9(uves_propertylist *header,
const char *name,
int val1, int val2, int val3, int val4,
int val5, int val6, int val7, int val8, int val9)
{
const char *s = uves_sprintf("%d %d %d %d %d %d %d %d %d",
val1, val2, val3, val4, val5, val6, val7, val8, val9);
flames_dfs_set_history_val(header, 'I', name, s);
uves_free_string_const(&s);
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Loads a raw frame of a type used by the order position recipe
@param frames The input frame set
@param raw_filename (output) Filename of raw frame
@param raw_image (output) The loaded image(s)
@param raw_header (output) The raw image header(s)
@param rotated_header (output) Header(s) containing keywords to describe
the output rotated frame(s)
@param ext_tbl (output) tables to describe the fiber caracteristics
@param blue (output) Flag indicating if the raw frame is blue (true) or red (false)
@return CPL_ERROR_NONE iff OK
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
flames_load_ofpos(const cpl_frameset *frames,
const char **raw_filename,
cpl_image *raw_image[2],
uves_propertylist *raw_header[2],
uves_propertylist *rotated_header[2],
cpl_table *ext_tbl[2],
bool *blue)
{
const char *tags[] = {FLAMES_FIB_FF_ODD,
FLAMES_FIB_FF_EVEN,
FLAMES_FIB_FF_ALL};
int number_of_tags = sizeof(tags) / sizeof(char *);
int indx;
check( *raw_filename = uves_find_frame(frames, tags, number_of_tags, &indx,
NULL),
"Could not find raw frame (%s, %s or %s) in SOF",
tags[0], tags[1], tags[2]);
*blue = (indx == 0) || (indx == 2);
/* Load the image */
check( load_raw_image_fib(*raw_filename,
CPL_TYPE_DOUBLE,
*blue,
raw_image,
raw_header,
rotated_header,ext_tbl),
"Error loading image from file '%s'", *raw_filename);
cleanup:
if (cpl_error_get_code() != CPL_ERROR_NONE)
{
*raw_filename = NULL;
}
return cpl_error_get_code();
}
/*----------------------------------------------------------------------------*/
/**
@brief Loads a raw frame of a given type
@param frames The input frame set
@param raw_filename (output) Filename of raw frame
@param raw_image (output) The loaded image(s)
@param raw_header (output) The raw image header(s)
@param rotated_header (output) Header(s) containing keywords to describe
the output rotated frame(s)
@param ext_tbl (output) tables to describe the fiber caracteristics
@param tag frame tag
@return CPL_ERROR_NONE iff OK
The function searches the frame set for a frame with TAG=tag.
Results are returned using the last five parameters (NULL on error).
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
flames_load_frame(const cpl_frameset *frames,
const char **raw_filename,
cpl_image *raw_image[2],
uves_propertylist *raw_header[2],
uves_propertylist *rotated_header[2],
cpl_table *ext_tbl[2],
const char* tag)
{
const char *tags[1];
int indx;
tags[0] = tag;
check( *raw_filename = uves_find_frame(frames, tags, 1, &indx, NULL),
"Could not find raw frame (%s) in SOF", tags[0]);
/* Load the image */
check( load_raw_image_fib(*raw_filename,
CPL_TYPE_DOUBLE,
false,
raw_image,
raw_header,
rotated_header,ext_tbl),
"Error loading image from file '%s'", *raw_filename);
cleanup:
if (cpl_error_get_code() != CPL_ERROR_NONE)
{
*raw_filename = NULL;
}
return cpl_error_get_code();
}
/*----------------------------------------------------------------------------*/
/**
@brief Loads a raw frame of a given type
@param frames The input frame set
@param raw_filename (output) Filename of raw frame
@param raw_image (output) The loaded image(s)
@param raw_header (output) The raw image header(s)
@param rotated_header (output) Header(s) containing keywords to describe
the output rotated frame(s)
@param ext_tbl (output) tables to describe the fiber caracteristics
@param indx frame index
@return CPL_ERROR_NONE iff OK
The function searches the frame set for a frame with TAG=tag.
Results are returned using the last five parameters (NULL on error).
*/
/*----------------------------------------------------------------------------*/
cpl_error_code
flames_load_frame_index(const cpl_frameset *frames,
const char **raw_filename,
cpl_image *raw_image[2],
uves_propertylist *raw_header[2],
uves_propertylist *rotated_header[2],
cpl_table *ext_tbl[2],
const int indx)
{
const cpl_frame* frm=NULL;
frm = cpl_frameset_get_frame_const(frames,indx);
*raw_filename=cpl_frame_get_filename(frm);
/* Load the image */
check( load_raw_image_fib(*raw_filename,
CPL_TYPE_DOUBLE,
false,
raw_image,
raw_header,
rotated_header,ext_tbl),
"Error loading image from file '%s'", *raw_filename);
cleanup:
if (cpl_error_get_code() != CPL_ERROR_NONE)
{
*raw_filename = NULL;
frm=NULL;
}
return cpl_error_get_code();
}
/*----------------------------------------------------------------------------*/
/**
@brief Load the ordef image
@param frames The input frame set
@param chip_name CCD chip name
@param ordef_filename (output) Filename of ordef image
@param ordef (output) The ordef image
@param mdark_header (output) FITS header
@param chip CCD chip
*/
/*----------------------------------------------------------------------------*/
void
flames_load_ordef(const cpl_frameset *frames, const char *chip_name,
const char **ordef_filename, cpl_image **ordef,
uves_propertylist **ordef_header, enum uves_chip chip)
{
const char *tags[2];
int number_of_tags = sizeof(tags) / sizeof(char *);
int extension;
int indx;
const bool flames = true;
*ordef = NULL;
*ordef_header = NULL;
tags[0] = FLAMES_ORDEF(flames, chip);
extension = FLAMES_ORDEF_EXTENSION(flames, chip);
check( *ordef_filename = uves_find_frame(frames, tags, number_of_tags, &indx, NULL),
"Could not find %s or %s in frame set", tags[0], tags[1]);
/* Load the ordef image */
check( *ordef = cpl_image_load(*ordef_filename,
CPL_TYPE_DOUBLE, /* Convert to this type */
0, /* plane number */
extension /* Extension number */
),
"Could not load master dark from extension %d of file '%s'",
extension, *ordef_filename);
/* Load the header */
check( *ordef_header = uves_propertylist_load(*ordef_filename,
extension),
"Could not load header from extension %d of file '%s'",
extension, *ordef_filename);
check_nomsg( uves_warn_if_chip_names_dont_match(*ordef_header, chip_name, chip) );
/* FLAMES MIDAS C-code wants the START (CRVAL) to be 1 */
check_nomsg( flames_reset_crval_to_one(ordef_header));
/* The DPR keywords were (correctly) removed by cpl_dfs_setup_product_header(),
But flames_dfs_write_descr() depends on these, therefore add */
check_nomsg( uves_pfits_set_dpr_catg(*ordef_header, "MOS"));
check_nomsg( uves_pfits_set_dpr_type(*ordef_header, "LAMP,ORDERDEF,SimCal"));
check_nomsg( uves_pfits_set_dpr_catg(*ordef_header, "CALIB"));
check_nomsg( flames_dfs_write_descr(*ordef_header, chip, true,NULL) );
cleanup:
if (cpl_error_get_code() != CPL_ERROR_NONE)
{
*ordef_filename = NULL;
uves_free_image(ordef);
uves_free_propertylist(ordef_header);
}
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Load a table
@param frames The input frameset
@param filename File containing the table
@param extension The table extension
@param table Loaded table
@param pheader Loaded primary header, if non-NULL
@param theader Loaded table header, if non-NULL
@param tag The table TAG
@return CPL_ERROR_NONE iff OK
The function...
*/
/*----------------------------------------------------------------------------*/
void
flames_load_table(const cpl_frameset *frames,
const char **filename,
cpl_table **table,
uves_propertylist **pheader,
uves_propertylist **theader,
const int extension,
const char* tag)
{
const char *tags[1];
int indx;
tags[0] = tag;
/* Get filename */
check( *filename = uves_find_frame(frames, tags, 1, &indx, NULL),
"No line reference table (%s) found in SOF", tag);
/* Load table */
check( *table = cpl_table_load(*filename,
extension,
1), /* Mark identified
invalid values? (1=yes) */
"Error loading line reference table from extension %d of file '%s'",
extension, *filename);
if(uves_erase_invalid_table_rows(*table, NULL) > 0) {
uves_msg_warning("Table in extension %d of file '%s' "
"contains invalid rows", extension, *filename);
}
/* Load headers if requested */
if (pheader != NULL)
{
check( *pheader = uves_propertylist_load(*filename, 0),
"Could not load primary header of FLAMES table in '%s'", *filename);
}
if (theader != NULL)
{
check( *theader = uves_propertylist_load(*filename, 1),
"Could not load header of FLAMES table in '%s'", *filename);
}
cleanup:
if (cpl_error_get_code() != CPL_ERROR_NONE)
{
uves_free_table(table);
if (pheader != NULL) uves_free_propertylist(pheader);
if (theader != NULL) uves_free_propertylist(theader);
}
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Loads a UVES raw image
@param filename Filename of raw frame
@param blue Flag indicating if the raw frame is blue (true) or red (false)
@param type Load the image as this type
@param raw_image (output) The loaded image(s)
@param raw_header (output) The raw image header(s)
@param rotated_header (output) Header(s) containing the proper CDELT, CRPIX...
@param ext_tbl (output) Extension table with FIBER mode info.
@return CPL_ERROR_NONE iff OK
The function loads a raw blue or red image from the specified file.
Results are returned through the arrays @em raw_image and @em raw_header,
which must be 2d arrays. If red, array index 0 is REDL and array index 1 is REDU.
If blue, array index 1 is not used.
The loaded images are rotated into standard orientation, and pre- and
overscan areas are removed (see @c uves_crop_and_rotate() ).
@note The provided arrays must have size at least two.
*/
/*----------------------------------------------------------------------------*/
static void
load_raw_image_fib(const char *filename,
cpl_type type, bool blue,
cpl_image *raw_image[2],
uves_propertylist *raw_header[2],
uves_propertylist *rotated_header[2],
cpl_table* ext_tbl[2])
{
cpl_image *image = NULL;
uves_propertylist *primary_header = NULL;
uves_propertylist *ext_header = NULL;
int nextensions;
int plane = 0; /* Only one plane in UVES raw files */
/* Initialize parameters */
raw_image[0] = NULL;
raw_image[1] = NULL;
raw_header[0] = NULL;
raw_header[1] = NULL;
rotated_header[0] = NULL;
rotated_header[1] = NULL;
ext_tbl[0]=NULL;
ext_tbl[1]=NULL;
check( nextensions = uves_get_nextensions(filename),
"Error reading number of extensions of file '%s'", filename);
uves_msg_low("Raw frame is %s, file '%s' has %d extensions",
(blue) ? "blue" : "red", filename, nextensions);
/* If the raw frame is blue, or if it's an old format red frame */
if (blue || (!blue && nextensions == 0))
{
int extension;
enum uves_chip chip;
bool new_format = false;
uves_msg("frame is blue or old format");
assure( nextensions == 0, CPL_ERROR_ILLEGAL_INPUT,
"Unrecognized format of file '%s'. No extensions expected. %d found.",
filename, nextensions);
extension = 0;
check( image = cpl_image_load(filename,
CPL_TYPE_DOUBLE,
plane,
extension
), "Could not load image from extension %d of file '%s' ",
extension, filename);
/* Load the header */
check( raw_header[0] = uves_propertylist_load(filename,
extension),
"Could not load header from extension %d of file '%s'",
extension, filename);
/* Get blue (or lower red) chip */
chip = (blue) ? UVES_CHIP_BLUE : UVES_CHIP_REDL;
check( raw_image[0] = uves_crop_and_rotate(image, raw_header[0],
chip, raw_header[0],
new_format,
&rotated_header[0]),
"Error splitting image");
check_nomsg( flames_dfs_write_descr(raw_header[0], chip, true,ext_tbl[0]) );
if (!blue)
{
const uves_propertylist *redl_header;
/* Upper red chip, use again the primary header */
check( raw_header[1] = uves_propertylist_duplicate(raw_header[0]),
"Error duplicating FITS header");
/* Get upper red chip */
chip = UVES_CHIP_REDU;
redl_header = raw_header[0];
check( raw_image[1] = uves_crop_and_rotate(image, raw_header[1],
chip, redl_header,
new_format,
&rotated_header[1]),
"Error splitting red image");
check_nomsg( flames_dfs_write_descr(raw_header[1], chip, true,ext_tbl[0]) );
}
else
{
raw_image[1] = NULL;
raw_header[1] = NULL;
rotated_header[1] = NULL;
}
}
else if (blue || (!blue && nextensions == 2))
{
enum uves_chip chip;
int extension;
bool new_format = false;
uves_msg_debug("Frame is red, old format, flames-uves");
assure( nextensions == 2, CPL_ERROR_UNSUPPORTED_MODE,
"File '%s' (red frame) has %d extensions. 0 or 2 HDUs expected",
filename, nextensions);
uves_msg_debug("Old red format, flames-uves");
check(ext_tbl[0]=cpl_table_load(filename,1,0),"loading %s ext table1", filename);
check(ext_tbl[1]=cpl_table_load(filename,2,0),"loading %s ext table2", filename);
/* Load primary header */
extension = 0;
check( primary_header = uves_propertylist_load(filename,
extension),
"Could not load header from extension %d of file '%s'",
extension, filename);
check( image = cpl_image_load(filename,
CPL_TYPE_DOUBLE,
plane,
extension
), "Could not load image from extension %d of file '%s' ",
extension, filename);
/* Load the header */
check( raw_header[0] = uves_propertylist_load(filename,
extension),
"Could not load header from extension %d of file '%s'",
extension, filename);
/* Get blue (or lower red) chip */
chip = (blue) ? UVES_CHIP_BLUE : UVES_CHIP_REDL;
/* Remove pre-, overscan */
check( raw_image[0] = uves_crop_and_rotate(image, raw_header[0],
chip, raw_header[0],
new_format,
&rotated_header[0]),
"Error splitting image");
check_nomsg( flames_dfs_write_descr(raw_header[0], chip, true,ext_tbl[0]) );
if (!blue)
{
const uves_propertylist *redl_header;
/* Upper red chip, use again the primary header */
check( raw_header[1] = uves_propertylist_duplicate(raw_header[0]),
"Error duplicating FITS header");
/* Get upper red chip */
chip = UVES_CHIP_REDU;
redl_header = raw_header[0];
check( raw_image[1] = uves_crop_and_rotate(image, raw_header[1],
chip, redl_header,
new_format,
&rotated_header[1]),
"Error splitting red image");
check_nomsg( flames_dfs_write_descr(raw_header[1], chip, true,ext_tbl[0]) );
}
else
{
raw_image[1] = NULL;
raw_header[1] = NULL;
rotated_header[1] = NULL;
}
}
else
/* Red, new format. Must have 2 or 4 extensions */
{
int extension;
bool new_format = true;
assure( nextensions == 2 ||
nextensions == 4, CPL_ERROR_UNSUPPORTED_MODE,
"File '%s' (red frame) has %d extensions. 0, 2 or 4 HDUs expected",
filename, nextensions);
uves_msg("New red format");
uves_msg_debug("New red format");
/* Load primary header */
extension = 0;
check( primary_header = uves_propertylist_load(filename,
extension),
"Could not load header from extension %d of file '%s'",
extension, filename);
for (extension = 1; extension <= 2; extension++)
{
/* In the FITS file, REDU is stored
in extension 1, and REDL is stored in
extension 2 */
enum uves_chip chip = (extension == 1) ? UVES_CHIP_REDU : UVES_CHIP_REDL;
int indx = uves_chip_get_index(chip);
/* Load the extension header */
uves_free_propertylist(&ext_header);
check( ext_header = uves_propertylist_load(filename,
extension),
"Could not load header from extension %d of file '%s'",
extension, filename);
/* Merge with primary header */
check( raw_header[indx] = uves_propertylist_duplicate(primary_header),
"Error cloning primary header");
if (!uves_propertylist_is_empty(ext_header))
{
check( uves_propertylist_copy_property_regexp(raw_header[indx],
ext_header, ".*", 0),
"Error merging primary header with extension %d header",
extension);
}
}
if (nextensions == 4) {
check(ext_tbl[0]=cpl_table_load(filename,3,0),"loading %s ext table1", filename);
check(ext_tbl[1]=cpl_table_load(filename,4,0),"loading %s ext table2", filename);
}
for (extension = 1; extension <= 2; extension++)
{
enum uves_chip chip = (extension == 1) ? UVES_CHIP_REDU : UVES_CHIP_REDL;
int indx = uves_chip_get_index(chip);
int indx_redl = uves_chip_get_index(UVES_CHIP_REDL);
uves_free_image(&image);
check( image = cpl_image_load(filename,
type,
plane,
extension),
"Could not load image from extension %d of file '%s' ",
extension, filename);
/* Remove pre-, overscan */
check( raw_image[indx] = uves_crop_and_rotate(image,
raw_header[indx],
chip, raw_header[indx_redl],
new_format,
&rotated_header[indx]),
"Error splitting red image");
check_nomsg( flames_dfs_write_descr(raw_header[indx], chip, true,ext_tbl[0]) );
}
}/* New format */
cleanup:
uves_free_image(&image);
uves_free_propertylist(&primary_header);
uves_free_propertylist(&ext_header);
if (cpl_error_get_code() != CPL_ERROR_NONE)
{
uves_free_image (&raw_image[0]);
uves_free_image (&raw_image[1]);
uves_free_propertylist(&raw_header[0]);
uves_free_propertylist(&raw_header[1]);
uves_free_propertylist(&rotated_header[0]);
uves_free_propertylist(&rotated_header[1]);
}
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Write value to HISTORY keywords
@param plist property list to write to
@param type 'I' for integer, 'C' for char
@param name name
@param val value as string, e.g. '5 4 3 23 1' for an integer array
This function adds
HISTORY 'name','I'
HISTORY value
HISTORY
If an entry with value prefix "'name','I'" already exists, it will be updated.
*/
/*----------------------------------------------------------------------------*/
void
flames_dfs_set_history_val(uves_propertylist *header, char type,
const char *name, const char *val)
{
char *new_name = NULL;
cpl_property *existing = NULL;
cpl_property *next = NULL;
const long header_size = uves_propertylist_get_size(header);
int i;
uves_msg_debug("Searching for %s", name);
for (i = 0;
existing == NULL && i < header_size-2; i++)
{
cpl_property *p = uves_propertylist_get(header, i);
const char *pname = cpl_property_get_name(p);
if (strcmp(pname, "HISTORY") == 0)
{
const char *pval;
check( pval = cpl_property_get_string(p),
"Error reading property value");
/* match the string "'name','I" (for any type I) */
if (strlen(pval) >= 1 + strlen(name) + 4 &&
pval[0] == '\'' &&
strncmp(pval+1, name, strlen(name)) == 0 &&
pval[strlen(name)+1] == '\'')
{
assure( strncmp(pval+strlen(name)+1, "','", 3) == 0 &&
pval[strlen(name)+4] == type,
CPL_ERROR_TYPE_MISMATCH,
"Keyword '%s' has wrong type, '%c' expected",
pval, type);
/* Remember the next position and stop searching */
existing = uves_propertylist_get(header, i+1);
next = uves_propertylist_get(header, i+2);
assure( strcmp(cpl_property_get_name(next), "HISTORY") == 0,
CPL_ERROR_ILLEGAL_INPUT,
"Missing HISTORY='' termination of keyword '%s'",
name);
}
}
}
uves_msg_debug("HISTORY %s keyword %sfound", name, (existing == NULL) ? "not " : "");
assure( strlen("HISTORY ") + strlen(val) <= 80, CPL_ERROR_ILLEGAL_INPUT,
"String 'HISTORY %s' length (%zu) is more than 80", val,
strlen("HISTORY ") + strlen(val));
if (existing != NULL)
{
check( cpl_property_set_string(existing, val),
"Error updating HISTORY keyword with value '%s'", val);
check( cpl_property_set_string(next, ""),
"Error updating HISTORY keyword with value ''");
}
else
/* append */
{
new_name = uves_sprintf("'%s','%c'", name, type);
check( uves_propertylist_append_string(header, "HISTORY", new_name),
"Error writing HISTORY keyword with value '%s'", new_name);
check( uves_propertylist_append_string(header, "HISTORY", val),
"Error updating HISTORY keyword with value '%s'", val);
check( uves_propertylist_append_string(header, "HISTORY", ""),
"Error updating HISTORY keyword with value ''");
}
cleanup:
cpl_free(new_name);
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Write descriptors special for FLAMES reduction
@param header to write to
@param warn_tech if true, prints warning when DPR TECH is unknown
@param chip CCD chip
**/
/*----------------------------------------------------------------------------*/
void
flames_dfs_write_descr(uves_propertylist *header, enum uves_chip chip,
bool warn_tech, cpl_table* ozpoz)
{
check( flames_set_header_char_1(header, FLAMES_CHIPCHOICE, uves_chip_tochar(chip)),
"Error writing chip choice descriptor");
check( fibremask_fill(header, chip, warn_tech,ozpoz),
"Error writing fibre mask to raw frame header");
cleanup:
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Set fibremask keyword using ozpoz table info
@param table ozpoz table
@return fibremask string
**/
/*----------------------------------------------------------------------------*/
void flames_set_fibremask(cpl_table* table, char* fibremask,
const int size, const int mode) {
int f0_bcal = 0;
int f1_b3 = 0;
int f2_b135 = 0;
int f3_b37 = 0;
int f4_b169 = 0;
int f5_b69 = 0;
int f6_b201 = 0;
int f7_b103 = 0;
int f8_b235 = 0;
if (mode ==1) {
f0_bcal = 1;
}
//cpl_table_dump(table,0,9,stdout);
f1_b3 = cpl_table_and_selected_int(table, "BUTTON", CPL_EQUAL_TO, 3);
//uves_msg("f1_b3=%d",f1_b3);
cpl_table_select_all(table);
f2_b135 = cpl_table_and_selected_int(table, "BUTTON", CPL_EQUAL_TO, 135);
//uves_msg("f2_b135=%d",f2_b135);
cpl_table_select_all(table);
f3_b37 = cpl_table_and_selected_int(table, "BUTTON", CPL_EQUAL_TO, 37);
//uves_msg("f3_b37=%d",f3_b37);
cpl_table_select_all(table);
f4_b169 = cpl_table_and_selected_int(table, "BUTTON", CPL_EQUAL_TO, 169);
//uves_msg("f4_b169=%d",f4_b169);
cpl_table_select_all(table);
f5_b69 = cpl_table_and_selected_int(table, "BUTTON", CPL_EQUAL_TO, 69);
//uves_msg("f5_b69=%d",f5_b69);
cpl_table_select_all(table);
f6_b201 = cpl_table_and_selected_int(table, "BUTTON", CPL_EQUAL_TO, 201);
//uves_msg("f6_b201=%d",f6_b201);
cpl_table_select_all(table);
f7_b103 = cpl_table_and_selected_int(table, "BUTTON", CPL_EQUAL_TO, 103);
//uves_msg("f7_b103=%d",f7_b103);
cpl_table_select_all(table);
f8_b235 = cpl_table_and_selected_int(table, "BUTTON", CPL_EQUAL_TO, 235);
//uves_msg("f8_b235=%d",f8_b235);
cpl_table_select_all(table);
snprintf(fibremask, size, "%d,%d,%d,%d,%d,%d,%d,%d,%d", f0_bcal, f1_b3,
f2_b135, f3_b37, f4_b169, f5_b69, f6_b201, f7_b103, f8_b235);
//uves_msg("fibremask=%s string of size %d", fibremask, size);
return;
}
/*----------------------------------------------------------------------------*/
/**
@brief Set fibremask keyword
@param header to write to
@param chip CCD chip
**/
/*----------------------------------------------------------------------------*/
static void fibremask_fill(uves_propertylist *header, enum uves_chip chip,
bool warn_tech, cpl_table* ozpoz)
{
const char * const maxfibres = "MAXFIBRES";
const char * const fibremask = "FIBREMASK";
const char *dpr_tech = " ";
const char *dpr_type = " ";
int maxf = 0;
const int size = 40;
char mask[size];
int simc = 0;
int check = 0;
int wlen = 0;
if (uves_propertylist_contains(header, UVES_DPR_TECH)) {
check_nomsg( dpr_tech = uves_pfits_get_dpr_tech(header));
}
if (uves_propertylist_contains(header, UVES_DPR_TYPE)) {
check_nomsg( dpr_type = uves_pfits_get_dpr_type(header));
}
if (uves_propertylist_contains(header, UVES_GRATWLEN(chip))) {
check_nomsg(
wlen = uves_round_double( uves_pfits_get_gratwlen(header, chip)));
}
if (uves_propertylist_contains(header, UVES_OCS_SIMCAL)) {
check_nomsg( simc = uves_pfits_get_ocs_simcal(header));
}
switch (wlen) {
case 520:
if (strcmp(dpr_tech, "ECHELLE") == 0) {
if (strstr(dpr_type, "FLAT") != NULL) {
check( set_header_int_1(header, maxfibres, 0),
"Could not write %s keyword", maxfibres);
check = 2;
} else if (strstr(dpr_type, "LAMP,SFLAT") != NULL) {
check( set_header_int_1(header, maxfibres, 0),
"Could not write %s keyword", maxfibres);
check = 2;
} else if (strstr(dpr_type, "LAMP,FMTCHK") != NULL) {
check = 3;
} else {
check = 1;
}
} else if (strstr(dpr_type, "LAMP,FLAT") != NULL) {
if (strstr(dpr_type, "OzPoz") != NULL) {
if (strstr(dpr_type, "ODD") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else if (strstr(dpr_type, "EVEN") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else if (strstr(dpr_type, "ALL") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else {
check = 1;
}
} else if (strstr(dpr_type, "SimCal") != NULL) {
if (strstr(dpr_type, "ODD") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else if (strstr(dpr_type, "EVEN") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else if (strstr(dpr_type, "ALL") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else {
check = 1;
}
} else if (strstr(dpr_type, "NASMYTH") != NULL) {
maxf = 9;
if (simc == 0) {
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
} else {
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
}
check = 0;
} else {
check = 1;
}
} else if (strstr(dpr_type, "LAMP,WAVE,OzPoz") != NULL) {
maxf = 0;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
}
else if (strstr(dpr_type, "LAMP,WAVE,SimCal") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
}
else if (strstr(dpr_type, "OBJECT,OzPoz") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
}
else if (strstr(dpr_type, "OBJECT,COMBINED") != NULL) {
maxf = 9;
if (simc == 0) {
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
} else {
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
}
check = 0;
}
else if (strstr(dpr_type, "OBJECT,SimCal") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
}
else if (strstr(dpr_type, "LAMP,FMTCHK,OzPoz") != NULL
|| strstr(dpr_type, "LAMP,FMTCHK,SimCal") != NULL
|| strstr(dpr_type, "LAMP,FMTCHK") != NULL) {
check = 3;
}
else if (strstr(dpr_type, "LAMP,ORDERDEF,OzPoz") != NULL) {
maxf = 9;
snprintf(mask, size, "%s", "0,0,0,0,1,0,0,0,0");
check = 0;
}
else if (strstr(dpr_type, "LAMP,ORDERDEF,SimCal") != NULL) {
maxf = 9;
snprintf(mask, size, "%s", "1,0,0,0,0,0,0,0,0");
check = 0;
} else {
check = 1;
}
break;
case 580:
case 860:
if (strcmp(dpr_tech, "ECHELLE") == 0) {
if (strstr(dpr_type, "FLAT") != NULL) {
check( set_header_int_1(header, maxfibres, 0),
"Could not write %s keyword", maxfibres);
check = 2;
} else if (strstr(dpr_type, "LAMP,SFLAT") != NULL) {
check( set_header_int_1(header, maxfibres, 0),
"Could not write %s keyword", maxfibres);
check = 2;
} else if (strstr(dpr_type, "LAMP,FMTCHK") != NULL) {
check = 3;
} else {
check = 1;
}
} else if (strstr(dpr_type, "LAMP,FLAT") != NULL) {
if (strstr(dpr_type, "OzPoz") != NULL) {
if (strstr(dpr_type, "ODD") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else if (strstr(dpr_type, "EVEN") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else if (strstr(dpr_type, "ALL") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else {
check = 1;
}
} else if (strstr(dpr_type, "SimCal") != NULL) {
if (strstr(dpr_type, "ODD") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else if (strstr(dpr_type, "EVEN") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else if (strstr(dpr_type, "ALL") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
} else {
check = 1;
}
}
else if (strstr(dpr_type, "NASMYTH") != NULL) {
maxf = 9;
if (simc == 0) {
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
} else {
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
}
check = 0;
} else {
check = 1;
}
} else if (strstr(dpr_type, "LAMP,WAVE,OzPoz") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
}
else if (strstr(dpr_type, "OBJECT,OzPoz") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
}
else if (strstr(dpr_type, "LAMP,WAVE,OzPoz") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
}
else if (strstr(dpr_type, "OBJECT,COMBINED") != NULL) {
maxf = 9;
if (simc == 0) {
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,0);
//uves_msg("computed fibremask='%s'",mask);
} else {
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
}
check = 0;
}
else if (strstr(dpr_type, "OBJECT,SimCal") != NULL) {
maxf = 9;
//uves_msg("expected fibremask='%s'",mask);
flames_set_fibremask(ozpoz, mask, size,1);
//uves_msg("computed fibremask='%s'",mask);
check = 0;
}
else if (strstr(dpr_type, "LAMP,FMTCHK,OzPoz") != NULL
|| strstr(dpr_type, "LAMP,FMTCHK,SimCal") != NULL
|| strstr(dpr_type, "LAMP,FMTCHK") != NULL) {
check = 3;
}
else if (strstr(dpr_type, "LAMP,ORDERDEF,OzPoz") != NULL) {
maxf = 9;
snprintf(mask, size, "%s", "0,0,0,0,1,0,0,0,0");
check = 0;
}
else if (strstr(dpr_type, "LAMP,ORDERDEF,SimCal") != NULL) {
maxf = 9;
snprintf(mask, size, "%s", "1,0,0,0,0,0,0,0,0");
check = 0;
} else {
check = 1;
}
break;
default:
uves_msg_warning("Unrecognized (non-standard) wavelength: %d", wlen);
break;
}
if (check == 0) {
check(
set_header_int_9(header, fibremask, mask[0*2] - '0', mask[1*2] - '0', mask[2*2] - '0', mask[3*2] - '0', mask[4*2] - '0', mask[5*2] - '0', mask[6*2] - '0', mask[7*2] - '0', mask[8*2] - '0'),
"Could not write %s keyword", maxfibres);
check( set_header_int_1(header, maxfibres, maxf),
"Could not write %s keyword", maxfibres);
} else if (check == 3) {
uves_msg("Type %s tech %s is known and "
"does not require FIBREMASK & MAXFIBRES descriptors", dpr_tech, dpr_type);
} else if (check == 2) {
uves_msg("Type %s tech %s is known and "
"requires FIBREMASK & MAXFIBRES descriptors", dpr_tech, dpr_type);
}
else {
if (warn_tech) {
uves_msg_warning("Type '%s' tech '%s' is unknown!", dpr_type, dpr_tech);
}
check( set_header_int_1(header, maxfibres, maxf),
"Could not write %s keyword", maxfibres);
}
cleanup: return;
}
/*---------------------------------------------------------------------------*/
/**
@brief Set extension to fits if bdf
@param filename to check for extension
@return outname newly allocated filename with corrected extension
**/
/*--------------------------------------------------------------------------*/
char *
flames_fix_estention(const char* filename)
{
if (strlen(filename) > 4 &&
strcmp(filename + strlen(filename) - 4, ".bdf") == 0) {
int n = strlen(filename);
char *outname = cpl_malloc((n + 2)*sizeof(char));
strcpy(outname, filename);
outname[n - 3] = 'f';
outname[n - 2] = 'i';
outname[n - 1] = 't';
outname[n - 0] = 's';
outname[n + 1] = '\0';
return outname;
}
else {
return cpl_strdup(filename);
}
}
/*---------------------------------------------------------------------------*/
/**
@brief Create FLAMES pipeline product
@param frames @see uves_frameset_insert
@param group @see uves_frameset_insert
@param type @see uves_frameset_insert
@param level @see uves_frameset_insert
@param filename input/output FITS filename
@param tag @see uves_frameset_insert
@param raw_header @see uves_frameset_insert
@param parameters @see uves_frameset_insert
@param recipe_id @see uves_frameset_insert
@param pipeline @see uves_frameset_insert
@param qc @see uves_frameset_insert
@param start_time @see uves_frameset_insert
@param dump_paf @see uves_frameset_insert
@param stats_mask @see uves_frameset_insert
This function is like uves_frameset_insert() but supports 3d, 4d images
**/
/*--------------------------------------------------------------------------*/
void
flames_frameset_insert(cpl_frameset *frames,
cpl_frame_group group,
cpl_frame_type type,
cpl_frame_level level,
const char *filename,
const char *tag,
const uves_propertylist *raw_header,
const cpl_parameterlist *parameters,
const char *recipe,
const char *pipeline,
cpl_table **qc,
const char *start_time,
bool dump_paf,
unsigned stats_mask)
{
void *buffer;
cpl_table *table = NULL;
cpl_image *out_image = NULL;
uves_propertylist *primary_header = NULL;
uves_propertylist *extension_header = NULL;
/* Pseudocode:
If image
Load the image buffer, determine dimension (2d, 3d, 4d)
Call uves_frameset_insert() to create an empty image with the correct
header.
Load the file, write the image data, don't change the header. Save.
If table
Load table + headers
Call uves_frameset_insert()
*/
if (type == CPL_FRAME_TYPE_IMAGE) {
int image;
int dattype;
int dimension;
int naxis[4];
double crpix[4] = {1, 1, 1, 1};
double crval[4] = {1, 1, 1, 1};
double cdelt[4] = {1, 1, 1, 1};
const char *bunit = NULL;
const char *ctype1 = NULL;
const char *ctype2 = NULL;
const char *ctype3 = NULL;
const char *ctype4 = NULL;
int size;
/* Get type */
{
int info[2];
assure( 0 == SCFINF(filename, 4, info),
CPL_ERROR_ILLEGAL_OUTPUT,
"Could not get %s image data type", filename);
dattype = info[1];
uves_msg_debug("dattype is %d", dattype);
}
/* Open */
assure( 0 == SCFOPN(filename, dattype, 0, F_IMA_TYPE, &image),
CPL_ERROR_ILLEGAL_OUTPUT,
"Could not open image %s", filename);
/* Get dimension */
{
int maxvals = 4;
int felem = 1;
int unit, null;
assure( 0 == SCDRDI(image, "NPIX", felem, maxvals, &dimension, naxis, &unit, &null),
CPL_ERROR_ILLEGAL_OUTPUT,
"Could not read NAXIS from %s", filename);
uves_msg_debug("Image dimension is %d", dimension);
}
/* Load pixel data */
{
int actsize;
cpl_type ctype;
switch (dimension) {
case 1: size = naxis[0]; break;
case 2: size = naxis[0] * naxis[1]; break;
case 3: size = naxis[0] * naxis[1] * naxis[2]; break;
case 4: size = naxis[0] * naxis[1] * naxis[2] * naxis[3]; break;
default:
assure( false, CPL_ERROR_UNSUPPORTED_MODE,
"Dimension is %d", dimension);
break;
}
ctype = flames_midas_image_dtype_to_cpltype(dattype);
switch(ctype) {
case CPL_TYPE_INT:
buffer = cpl_malloc(sizeof(int) * size);
out_image = cpl_image_wrap_int(size, 1, buffer);
break;
case CPL_TYPE_FLOAT:
buffer = cpl_malloc(sizeof(float) * size);
out_image = cpl_image_wrap_float(size, 1, buffer);
break;
default:
assure( false, CPL_ERROR_UNSUPPORTED_MODE,
"Type is %s", uves_tostring_cpl_type(type));
break;
}
assure_mem( buffer );
assure( 0 == SCFGET(image, 1, size, &actsize, buffer),
CPL_ERROR_ILLEGAL_OUTPUT,
"Could not load image %s data", filename);
assure( actsize == size, CPL_ERROR_ILLEGAL_OUTPUT,
"Tried to read %d pixels from %s, got %d",
size, filename, actsize);
assure( 0 == SCFCLO(image), CPL_ERROR_ILLEGAL_OUTPUT,
"Could not close file %s", filename);
}
/* Create empty file with correct header */
check( primary_header = uves_propertylist_load(filename, 0),
"Could not load primary header from %s", filename);
/* Cannot write stats (DATAMIN, DATAMAX, ...) inside uves_frameset_insert(),
need the image buffer for that
*/
if (stats_mask != 0) {
check_nomsg( uves_dfs_write_statistics(out_image, primary_header,
stats_mask));
}
if (dimension >= 1 &&
uves_propertylist_contains(primary_header, "CRPIX1")) {
crpix[0] = uves_propertylist_get_double(primary_header, "CRPIX1");
}
if (dimension >= 2 &&
uves_propertylist_contains(primary_header, "CRPIX2")) {
crpix[1] = uves_propertylist_get_double(primary_header, "CRPIX2");
}
if (dimension >= 3 &&
uves_propertylist_contains(primary_header, "CRPIX3")) {
crpix[2] = uves_propertylist_get_double(primary_header, "CRPIX3");
}
if (dimension >= 4 &&
uves_propertylist_contains(primary_header, "CRPIX4")) {
crpix[3] = uves_propertylist_get_double(primary_header, "CRPIX4");
}
if (dimension >= 1 &&
uves_propertylist_contains(primary_header, "CRVAL1")) {
crval[0] = uves_propertylist_get_double(primary_header, "CRVAL1");
}
if (dimension >= 2 &&
uves_propertylist_contains(primary_header, "CRVAL2")) {
crval[1] = uves_propertylist_get_double(primary_header, "CRVAL2");
}
if (dimension >= 3 &&
uves_propertylist_contains(primary_header, "CRVAL3")) {
crval[2] = uves_propertylist_get_double(primary_header, "CRVAL3");
}
if (dimension >= 4 &&
uves_propertylist_contains(primary_header, "CRVAL4")) {
crval[3] = uves_propertylist_get_double(primary_header, "CRVAL4");
}
if (dimension >= 1 &&
uves_propertylist_contains(primary_header, "CDELT1")) {
cdelt[0] = uves_propertylist_get_double(primary_header, "CDELT1");
}
if (dimension >= 2 &&
uves_propertylist_contains(primary_header, "CDELT2")) {
cdelt[1] = uves_propertylist_get_double(primary_header, "CDELT2");
}
if (dimension >= 3 &&
uves_propertylist_contains(primary_header, "CDELT3")) {
cdelt[2] = uves_propertylist_get_double(primary_header, "CDELT3");
}
if (dimension >= 4 &&
uves_propertylist_contains(primary_header, "CDELT4")) {
cdelt[3] = uves_propertylist_get_double(primary_header, "CDELT4");
}
if (dimension >= 1 &&
uves_propertylist_contains(primary_header, "CTYPE1")) {
ctype1 = uves_propertylist_get_string(primary_header, "CTYPE1");
}
if (dimension >= 2 &&
uves_propertylist_contains(primary_header, "CTYPE2")) {
ctype2 = uves_propertylist_get_string(primary_header, "CTYPE2");
}
if (dimension >= 3 &&
uves_propertylist_contains(primary_header, "CTYPE3")) {
ctype3 = uves_propertylist_get_string(primary_header, "CTYPE3");
}
if (dimension >= 4 &&
uves_propertylist_contains(primary_header, "CTYPE4")) {
ctype4 = uves_propertylist_get_string(primary_header, "CTYPE4");
}
if (uves_propertylist_contains(primary_header, "BUNIT")) {
bunit = uves_propertylist_get_string(primary_header, "BUNIT");
}
{
int i;
for (i = 0; i < uves_propertylist_get_size(primary_header); i++) {
cpl_property *p = uves_propertylist_get(primary_header, i);
if (strcmp(cpl_property_get_name(p), "HISTORY") == 0) {
const char *value = cpl_property_get_string(p);
if (strstr(value, "ESO-DESCRIPTORS START")) {
cpl_property_set_string(p, "EDS");
/* This is to avoid that MIDAS' INTAPE/FITS command
warns about the header not being in MIDAS format */
}
}
}
}
check_nomsg( uves_frameset_insert(frames,
NULL, /* Image */
group,
type,
level,
filename,
tag,
raw_header,
primary_header,
NULL,
parameters,
recipe,
pipeline,
qc,
start_time,
dump_paf,
0));
/* Write pixels data */
{
int unit;
assure( 0 == SCFOPN(filename, dattype, 0, F_IMA_TYPE, &image),
CPL_ERROR_ILLEGAL_OUTPUT,
"Could not open image %s", filename);
/* The image should now have NAXIS = 0, overwrite with correct dimension/size */
assure( 0 == SCDWRI(image, "NPIX", naxis,
1, dimension, &unit),
CPL_ERROR_ILLEGAL_OUTPUT,
"Could not set size of %dd image %s", dimension, filename);
assure_nomsg( 0 == SCDWRD(image, "CRPIX", crpix, 1, dimension, &unit),
CPL_ERROR_ILLEGAL_OUTPUT);
assure_nomsg( 0 == SCDWRD(image, "CRVAL", crval, 1, dimension, &unit),
CPL_ERROR_ILLEGAL_OUTPUT);
assure_nomsg( 0 == SCDWRD(image, "CDELT", cdelt, 1, dimension, &unit),
CPL_ERROR_ILLEGAL_OUTPUT);
if (bunit == NULL) {
bunit = " ";
}
assure_nomsg( 0 == SCDWRC(image, "BUNIT", 1, bunit, 1, strlen(bunit)+1, &unit),
CPL_ERROR_ILLEGAL_OUTPUT);
if (ctype1 != NULL) {
assure_nomsg( 0 == SCDWRC(image, "CTYPE1", 1, ctype1, 1, strlen(ctype1)+1, &unit),
CPL_ERROR_ILLEGAL_OUTPUT);
}
if (ctype2 != NULL) {
assure_nomsg( 0 == SCDWRC(image, "CTYPE2", 1, ctype2, 1, strlen(ctype2)+1, &unit),
CPL_ERROR_ILLEGAL_OUTPUT);
}
if (ctype3 != NULL) {
assure_nomsg( 0 == SCDWRC(image, "CTYPE3", 1, ctype3, 1, strlen(ctype3)+1, &unit),
CPL_ERROR_ILLEGAL_OUTPUT);
}
if (ctype4 != NULL) {
assure_nomsg( 0 == SCDWRC(image, "CTYPE4", 1, ctype4, 1, strlen(ctype4)+1, &unit),
CPL_ERROR_ILLEGAL_OUTPUT);
}
assure( 0 == SCFPUT(image, 1, size, buffer), CPL_ERROR_ILLEGAL_OUTPUT,
"Could not rewrite image data to %dd image %s", dimension, filename);
assure( 0 == SCFCLO(image), CPL_ERROR_ILLEGAL_OUTPUT,
"Could not close file %s", filename);
}
}
else if (type == CPL_FRAME_TYPE_TABLE) {
check( table = cpl_table_load(filename, 1, 1),
"Could not load table %s", filename);
check( primary_header = uves_propertylist_load(filename, 0),
"Could not load primary header from %s", filename);
check( extension_header = uves_propertylist_load(filename, 1),
"Could not load 1st extension header from %s", filename);
check_nomsg( uves_frameset_insert(frames,
table,
group,
type,
level,
filename,
tag,
raw_header,
primary_header,
extension_header,
parameters,
recipe,
pipeline,
qc,
start_time,
dump_paf,
stats_mask));
}
cleanup:
uves_free_table(&table);
uves_free_image(&out_image);
uves_free_propertylist(&primary_header);
uves_free_propertylist(&extension_header);
return;
}
cpl_error_code
flames_clean_tmp_products_ofpos(enum uves_chip chip, const int cubify)
{
char command[1024];
//REDU ok
// sprintf(command,"rm -f slitff_*_bound*.fits slitff_*_badpixel*.fits bp_b_set_?_mf???*.fits b_set_?_mf???*.fits *odd*.fits *even*.fits *all*.fits middumm*.fits bkg_*.fits mbias_*.fits ord_gue_*.fits first.fits third.fits flames_create_full_ot.fits rof*.fits sp_red*.fits");
uves_msg("cip=%d vs %d",chip,UVES_CHIP_REDU);
if(chip == UVES_CHIP_REDL) {
uves_msg("cubify=%d",cubify);
if (cubify==0) {
sprintf(command,"rm -f bp_ordef_redl.fits ordef_redl.fits b_ordef_redl.fits b_ordef_redl_*.fits odd*.fits even*.fits all*.fits trap_redl.fits b*odd_l*.fits b*even_l*.fits b*all_l*.fits *set?*_redl*.fits odd*_sigma.fits bp_b_set_?_mf???*.fits b_set_?_mf???*.fits middumm*.fits bkg_*.fits mbias_*.fits ord_gue_*.fits first.fits third.fits flames_create_full_ot.fits rofl.fits sp_redl.fits");
} else {
sprintf(command,"rm -f bp_ordef_redl.fits ordef_redl.fits b_ordef_redl.fits b_ordef_redl_*.fits first.fits third.fits flames_create_full_ot.fits trap_redl.fits sp_redl.fits rofl.fits bkg_l.fits mbias_redl.fits ord_gue_redl.fits odd*.fits b*odd_l*.fits even*.fits b*even_l*.fits all*.fits b*all_l*.fits middumm*.fits *set?_*redl*.fits b_*set_l_mf???*.fits b_odd_l*.fits b_even_l*.fits b_all_l*.fits fibreff_l_data0?.fits fibreff_l_sigma0?.fits fibreff_l_badpixel0?.fits slitff_l_data0?.fits slitff_l_sigma0?.fits slitff_l_bound0?.fits slitff_l_badpixel0?.fits slitff_l_bound0?.fits");
}
} else {
if (cubify==0) {
sprintf(command,"rm -f bp_ordef_redu.fits ordef_redu.fits b_ordef_redu.fits b_ordef_redu_*.fits odd*.fits even*.fits all*.fits trap_redu.fits *odd_u*.fits *even_u*.fits *all_u*.fits xt_*u.fits *set?*_redu*.fits odd*_sigma.fits bp_b_set_?_mf???*.fits b_set_?_mf???*.fits middumm*.fits bkg_*.fits mbias_*.fits ord_gue_*.fits first.fits third.fits flames_create_full_ot.fits rofu.fits sp_redu.fits");
} else {
sprintf(command,"rm -f bp_ordef_redu.fits ordef_redu.fits b_ordef_redu.fits b_ordef_redu_*.fits xt_*u.fits first.fits third.fits flames_create_full_ot.fits trap_redu.fits sp_redu.fits rofu.fits bkg_u.fits mbias_redu.fits ord_gue_redu.fits odd*.fits b*odd_u*.fits even*.fits b*even_u*.fits all*.fits b*all_u*.fits middumm*.fits *set?_*redu*.fits b_*set_u_mf???*.fits b_odd_u*.fits b_even_u*.fits b_all_u*.fits fibreff_u_data0?.fits fibreff_u_sigma0?.fits fibreff_u_badpixel0?.fits slitff_u_data0?.fits slitff_u_sigma0?.fits slitff_u_bound0?.fits slitff_u_badpixel0?.fits slitff_u_bound0?.fits");
}
}
system(command);
uves_msg("end cleanup");
return cpl_error_get_code();
}
cpl_error_code
flames_clean_tmp_products_sci(enum uves_chip chip, const int cubify)
{
char command[1024];
if(chip == UVES_CHIP_REDL) {
if (cubify==0) {
sprintf(command,"rm -f sp_redl.fits sci_l.fits cor_shape_l.fits m_tbl_redl.fits trap_redl.fits mbias_l.fits ordef_l.fits bkg_l.fits ext?.fits *sci_l*.fits *pack.fits middumm*.fits fibreff_l_data0?.fits fibreff_l_sigma0?.fits fibreff_l_badpixel0?.fits fibreff_l_dtc.fits fibreff_l_sgc.fits fibreff_l_bpc.fits fibreff_l_norm.fits fibreff_l_nsigma.fits fibreff_l_common.fits slitff_l_data0?.fits slitff_l_sigma0?.fits slitff_l_bound0?.fits slitff_l_badpixel0?.fits slitff_l_dtc.fits slitff_l_sgc.fits slitff_l_bpc.fits slitff_l_bnc.fits slitff_l_norm.fits slitff_l_nsigma.fits slitff_l_common.fits mwfxb_*mask.fits fxb_*extco*.fits wfxb_*extco*.fits");
} else {
sprintf(command,"rm -f sp_redl.fits sci_l.fits cor_shape_l.fits m_tbl_redl.fits trap_redl.fits mbias_l.fits ordef_l.fits bkg_l.fits ext?.fits *b_sci_l*.fits middumm*.fits *fxb_l_*0*.fits fibreff_l_data0?.fits fibreff_l_sigma0?.fits fibreff_l_badpixel0?.fits fibreff_l_dtc.fits fibreff_l_sgc.fits fibreff_l_bpc.fits fibreff_l_nsigma.fits fibreff_l_norm.fits fibreff_l_common.fits slitff_l_data0?.fits slitff_l_sigma0?.fits slitff_l_bound0?.fits slitff_l_badpixel0?.fits slitff_l_dtc.fits slitff_l_sgc.fits slitff_l_bpc.fits slitff_l_bnc.fits slitff_l_norm.fits slitff_l_common.fits b_set_l_mf???*.fits *fxb_l_*extco*.fits");
}
} else {
if (cubify==0) {
sprintf(command,"rm -f sp_redu.fits sci_u.fits cor_shape_u.fits m_tbl_redu.fits trap_redu.fits mbias_u.fits ordef_u.fits bkg_u.fits ext?.fits *sci_u*.fits *pack.fits middumm*.fits fibreff_u_data0?.fits fibreff_u_sigma0?.fits fibreff_u_badpixel0?.fits fibreff_u_dtc.fits fibreff_u_sgc.fits fibreff_u_bpc.fits fibreff_u_norm.fits fibreff_u_nsigma.fits fibreff_u_common.fits slitff_u_data0?.fits slitff_u_sigma0?.fits slitff_u_bound0?.fits slitff_u_badpixel0?.fits slitff_u_dtc.fits slitff_u_sgc.fits slitff_u_bpc.fits slitff_u_bnc.fits slitff_u_norm.fits slitff_u_nsigma.fits slitff_u_common.fits mwfxb_*mask.fits fxb_*extco*.fits wfxb_*extco*.fits");
} else {
sprintf(command,"rm -f sp_redu.fits sci_u.fits cor_shape_u.fits m_tbl_redu.fits trap_redu.fits mbias_u.fits ordef_u.fits bkg_u.fits ext?.fits *b_sci_u*.fits middumm*.fits *fxb_u_*0*.fits fibreff_u_data0?.fits fibreff_u_sigma0?.fits fibreff_u_badpixel0?.fits fibreff_u_dtc.fits fibreff_u_sgc.fits fibreff_u_bpc.fits fibreff_u_nsigma.fits fibreff_u_norm.fits fibreff_u_common.fits slitff_u_data0?.fits slitff_u_sigma0?.fits slitff_u_bound0?.fits slitff_u_badpixel0?.fits slitff_u_dtc.fits slitff_u_sgc.fits slitff_u_bpc.fits slitff_u_bnc.fits slitff_u_norm.fits slitff_u_common.fits b_set_u_mf???*.fits *fxb_u_*extco*.fits");
}
}
system(command);
return cpl_error_get_code();
}
/**@}*/
|