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
|
/*===========================================================================
Copyright (C) 2001 European Southern Observatory (ESO)
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, Inc., 675 Massachusetss Ave, Cambridge,
MA 02139, USA.
Corresponding concerning ESO-MIDAS should be addressed as follows:
Internet e-mail: midas@eso.org
Postal address: European Southern Observatory
Data Management Division
Karl-Schwarzschild-Strasse 2
D 85748 Garching bei Muenchen
GERMANY
===========================================================================*/
/* Program : mainstandfast.c */
/* Author : G. Mulas - ITAL_FLAMES Consortium */
/* Date : */
/* */
/* Purpose : Missing */
/* */
/* */
/* Input: see interface */
/* */
/* Output: */
/* */
/* DRS Functions called: */
/* none */
/* */
/* Pseudocode: */
/* Missing */
/* */
/* Version : */
/* Last modification date: 2002/08/05 */
/* Who When Why Where */
/* AMo 02-08-05 Add header header */
/*-------------------------------------------------------------------------*/
/* C functions include files */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* MIDAS include files */
#include <flames_readallff.h>
#include <flames_optsynth.h>
#include <flames_writesynth.h>
#include <flames_scatter.h>
#include <flames_write_spectra.h>
#include <flames_prepstand.h>
#include <flames_dostandard.h>
#include <flames_readback.h>
#include <flames_readframe.h>
#include <flames_freeordpos.h>
#include <flames_ffslitmultiply.h>
#include <flames_midas_def.h>
#include <flames_writeback.h>
#include <flames_freeallflats.h>
#include <flames_freespectrum.h>
#include <flames_computeback.h>
#include <flames_freeslitflats.h>
#include <flames_freeframe.h>
/* FLAMES-UVES include files */
#include <flames_newmatrix.h>
#include <flames_mainstandfast.h>
#include <uves_msg.h>
#include <flames_readordpos.h>
#include <flames_readslitflats.h>
int flames_mainstandfast(const char *IN_A,
const cpl_frameset *fibff_set,
const cpl_frameset *slitff_set,
const char *IN_D,
const char *IN_E,
const char *BASENAME,
const double *MAXDISCARDFRACT,
const int *MAXBACKITERS,
const int *BKGPOL,
const char *BKGFITINLINE, //was int*
const char *BKGFITMETHOD,
const char *BKGBADSCAN, //was int*
const int *BKGBADWIN,
const double *BKGBADMAXFRAC,
const int *BKGBADMAXTOT,
const double *SIGMA,
const double *WINDOW,
double *OUTPUTD,
int *OUTPUTI)
{
char output[200];
int i=0;
int unit=0;
int actvals=0;
frame_mask **mask=0;
int bxdegree=0, bydegree=0;
double kappa=0;
double kappa2=0;
int chisqpixels=0, nfittedparams=0;
int maxbackiters=0;
double maxdiscardfract=0;
double chisquare=0;
frame_data **backframe;
frame_data **normcover;
flames_err status=0;
double ***pfibrecentre=0;
double halfwinsize=0, phalfwinsize=0;
flames_frame *ScienceFrame=0;
allslitflats *Slit_FF=0;
allflats *Shifted_FF=0;
orderpos *Order=0;
//char allfile[CATREC_LEN+2];
//char slitsfile[CATREC_LEN+2];
const cpl_frameset* allfile=fibff_set;
const cpl_frameset* slitsfile=slitff_set;
char backfile[CATREC_LEN+2];
char infile[CATREC_LEN+2];
char catfile[CATREC_LEN+2];
char orderfile[CATREC_LEN+2];
int32_t n=0;
int32_t iframe=0;
int nval=0;
int null=0;
int32_t iorder=0, ifibre=0, ix=0;
scatterswitch bkgswitch=USEALL;
scatterswitch2 bkgswitch2=NOBADSCAN;
int badwinxsize=0;
int badwinysize=0;
double badfracthres=0;
int badtotthres=0;
char keytype=0;
int noelem=0;
int bytelem=0;
char bkgfitmethod[CATREC_LEN+1];
int32_t orderoffset=0;
int32_t realfirstorder=0;
int32_t reallastorder=0;
int32_t slitfirstorder=0;
int32_t slitlastorder=0;
int32_t negativepixels=0;
char bkginline=TRUE;
frame_data *fdvecbuf1=0;
frame_data *fdvecbuf2=0;
frame_data *fdvecbuf3=0;
frame_mask *fmvecbuf1=0;
frame_mask *fmvecbuf2=0;
frame_mask *fmvecbuf3=0;
frame_data pixelvalue=0;
int32_t lastiyixindex=0;
int32_t iorderifibreixstart=0;
int32_t iorderifibreixend=0;
int32_t iorderifibreindex=0;
int32_t iorderifibreixoffset=0;
int32_t iorderifibreixindex=0;
int32_t ixiorderifibreindex=0;
memset(output, 0, 200);
memset(bkgfitmethod, 0, CATREC_LEN+1);
bkgswitch = USEALL;
SCSPRO("standfast"); /* Get into MIDAS Env. */
/* Read once and for all, here at the beginning, what MIDAS keywords we
need */
/* get input frame name from MIDAS env.*/
SCKGETC(IN_A,1,CATREC_LEN+1,&nval,infile);
/* read input table name */
SCKGETC(IN_D,1,160,&nval,backfile);
/* read order table name */
SCKGETC(IN_E,1,160,&nval,orderfile);
/* read output base name */
if (SCKGETC(BASENAME,1,160,&nval,catfile)!=0) {
/* problems reading BASENAME */
SCTPUT("Error reading the base name for output spectra");
return flames_midas_fail();
}
/* initialise MAXDISCARDFRACT from keyword */
if (SCKRDD(MAXDISCARDFRACT, 1, 1, &actvals, &maxdiscardfract, &unit,
&null) != 0) {
/* problems reading MAXDISCARDFRACT */
SCTPUT("Error reading the MAXDISCARDFRACT keyword");
return flames_midas_fail();
}
/* initialise MAXBACKITERS from keyword */
if (SCKRDI(MAXBACKITERS, 1, 1, &actvals, &maxbackiters, &unit,
&null) != 0) {
/* problems reading MAXBACKITERS */
SCTPUT("Error reading the MAXBACKITERS keyword");
return flames_midas_fail();
}
/* initialise background fitting scalars */
if (SCKRDI(BKGPOL, 1, 1, &actvals, &bxdegree, &unit, &null)
!= 0) {
/* problems reading xdegree */
SCTPUT("Error reading the x degree of the background polynomial");
return flames_midas_fail();
}
if (SCKRDI(BKGPOL, 2, 1, &actvals, &bydegree, &unit, &null)
!= 0) {
/* problems reading xdegree */
SCTPUT("Error reading the y degree of the background polynomial");
return flames_midas_fail();
}
/* Is inline background fitting and subtraction required? */
if (SCKFND_string(BKGFITINLINE, &keytype, &noelem, &bytelem) != 0) {
/* SCKFND failed, major problems */
SCTPUT("Internal MIDAS error in mainopt: SCKFND failed");
return flames_midas_fail();
}
switch(keytype) {
case 'C':
/* it exists and it is a character keyword, do read it */
if (SCKGETC(BKGFITINLINE, 1, CATREC_LEN, &nval, bkgfitmethod)
!= 0) {
/* problems reading BKGFITINLINE */
SCTPUT("Warning: error reading the BKGFITINLINE keyword, falling back to \
default");
}
else {
/* is the string long enough to be unambiguous? */
if (nval<1) {
SCTPUT("Warning: BKGFITINLINE is ambiguous, falling back to default");
}
else {
/* convert bkgfitmethod to upper case, to ease the subsequent check */
for (i=0; i<=(nval-1); i++) bkgfitmethod[i] = toupper(bkgfitmethod[i]);
/* compare as many letters as we have with the expected values */
if (strncmp("YES", bkgfitmethod, (size_t) nval) == 0)
bkginline = TRUE;
else if (strncmp("NO", bkgfitmethod, (size_t) nval)
== 0) bkginline = FALSE;
else {
SCTPUT("Warning: unsupported BKGFITINLINE value, falling back to \
default");
}
}
}
break;
case ' ':
/* the keyword does not exist at all */
SCTPUT("Warning: BKGFITINLINE is undefined, falling back to default");
break;
default:
/* the keyword is of the wrong type */
SCTPUT("Warning: BKGFITINLINE is not a string, falling back to default");
break;
}
/* before trying to read it, make sure that BKGFITMETHOD exists and it is
of the appropriate type */
if (SCKFND_string(BKGFITMETHOD, &keytype, &noelem, &bytelem) != 0) {
/* SCKFND failed, give up! */
SCTPUT("Internal MIDAS error in mainopt: SCKFND failed");
return flames_midas_fail();
}
switch(keytype) {
case 'C':
/* it exists and it is a character keyword, go ahead and read it */
if (SCKGETC(BKGFITMETHOD, 1, CATREC_LEN, &nval, bkgfitmethod)
!= 0) {
/* problems reading BKGFITMETHOD */
SCTPUT("Warning: error reading the BKGFITMETHOD keyword, falling back to \
default");
}
else {
/* is the string long enough to be unambiguous? */
if (nval<2) {
/* no, fall back to the default */
SCTPUT("Warning: BKGFITMETHOD is ambiguous, falling back to default");
}
else {
/* convert bkgfitmethod to upper case, to ease the subsequent check */
for (i=0; i<=(nval-1); i++) bkgfitmethod[i] = toupper(bkgfitmethod[i]);
/* compare as many letters as we have with the expected values */
if (strncmp("ALL", bkgfitmethod, (size_t) nval) == 0)
bkgswitch = USEALL;
else if (strncmp("MEDIAN", bkgfitmethod, (size_t) nval)
== 0) bkgswitch = USEMEDIAN;
else if (strncmp("MINIMUM", bkgfitmethod, (size_t) nval)
== 0) bkgswitch = USEMINIMUM;
else if (strncmp("AVERAGE", bkgfitmethod, (size_t) nval)
== 0) bkgswitch = USEAVERAGE;
else {
SCTPUT("Warning: unsupported BKGFITMETHOD value, falling back to \
default");
}
}
}
break;
case ' ':
/* the keyword does not exist at all */
SCTPUT("Warning: BKGFITMETHOD is undefined, falling back to default");
break;
default:
/* the keyword is of the wrong type */
SCTPUT("Warning: BKGFITMETHOD is not a string, falling back to default");
break;
}
/* before trying to read it, make sure that BKGFITMETHOD exists and it is
of the appropriate type */
if (SCKFND_string(BKGBADSCAN, &keytype, &noelem, &bytelem) != 0) {
/* SCKFND failed, give up! */
SCTPUT("Internal MIDAS error in mainopt: SCKFND failed");
return flames_midas_fail();
}
switch(keytype) {
case 'C':
/* it exists and it is a character keyword, go ahead and read it */
if (SCKGETC(BKGBADSCAN, 1, CATREC_LEN, &nval, bkgfitmethod)
!= 0) {
/* problems reading BKGFITMETHOD */
SCTPUT("Warning: error reading the BKGBADSCAN keyword, falling back to \
default");
}
else {
/* is the string long enough to be unambiguous? */
if (nval<1) {
/* no, fall back to the default */
SCTPUT("Warning: BKGBADSCAN is ambiguous, falling back to default");
}
else {
/* convert bkgfitmethod to upper case, to ease the subsequent check */
for (i=0; i<=(nval-1); i++) bkgfitmethod[i] = toupper(bkgfitmethod[i]);
/* compare as many letters as we have with the expected values */
if (strncmp("NONE", bkgfitmethod, (size_t) nval) == 0)
bkgswitch2 = NOBADSCAN;
else if (strncmp("FRACTION", bkgfitmethod, (size_t) nval)
== 0) bkgswitch2 = FRACBADSCAN;
else if (strncmp("ABSOLUTE", bkgfitmethod, (size_t) nval)
== 0) bkgswitch2 = ABSBADSCAN;
else {
SCTPUT("Warning: unsupported BKGBADSCAN value, falling back to \
default");
}
}
}
break;
case ' ':
/* the keyword does not exist at all */
SCTPUT("Warning: BKGBADSCAN is undefined, falling back to default");
break;
default:
/* the keyword is of the wrong type */
SCTPUT("Warning: BKGBADSCAN is not a string, falling back to default");
break;
}
/* if neighborhood bad pixel scanning was requested, read the other
keywords needed */
if (bkgswitch2 == FRACBADSCAN) {
if ((SCKRDI(BKGBADWIN, 1, 1, &actvals, &badwinxsize, &unit, &null) != 0)
|| (SCKRDI(BKGBADWIN, 2, 1, &actvals, &badwinysize, &unit, &null)
!= 0)) {
SCTPUT("Error reading the BKGBADWIN keyword");
return flames_midas_fail();
}
if (SCKRDD(BKGBADMAXFRAC, 1, 1, &actvals, &badfracthres, &unit,
&null) != 0) {
SCTPUT("Error reading the BKGBADMAXFRAC keyword");
return flames_midas_fail();
}
/* check the values read for consistence */
if ((badwinxsize < 0) || (badwinysize < 0)) {
SCTPUT("Warning: BKGBADWIN values must be non negative, disabling \
BKGBADSCAN");
bkgswitch2 = NOBADSCAN;
}
if (badfracthres < 0) {
SCTPUT("Warning: BKGBADMAXFRAC value must be non negative, disabling \
BKGBADSCAN");
bkgswitch2 = NOBADSCAN;
}
}
else if (bkgswitch2 == ABSBADSCAN) {
if ((SCKRDI(BKGBADWIN, 1, 1, &actvals, &badwinxsize, &unit, &null) != 0)
|| (SCKRDI(BKGBADWIN, 2, 1, &actvals, &badwinysize, &unit, &null)
!= 0)) {
SCTPUT("Error reading the BKGBADWIN keyword");
return flames_midas_fail();
}
if (SCKRDI(BKGBADMAXTOT, 1, 1, &actvals, &badtotthres, &unit,
&null) != 0) {
SCTPUT("Error reading the BKGBADMAXTOT keyword");
return flames_midas_fail();
}
/* check the values read for consistence */
if ((badwinxsize < 0) || (badwinysize < 0)) {
SCTPUT("Warning: BKGBADWIN values must be non negative, disabling \
BKGBADSCAN");
bkgswitch2 = NOBADSCAN;
}
if (badtotthres < 0) {
SCTPUT("Warning: BKGBADMAXTOT value must be non negative, disabling \
BKGBADSCAN");
bkgswitch2 = NOBADSCAN;
}
}
/* read the kappa factor to be used later in kappa-sigma clipping */
if ((status=SCKRDD(SIGMA, 1, 1, &actvals, &kappa, &unit, &null))!=0) {
/* something went wrong while reading the kappa-sigma factor */
sprintf(output, "Error %d while reading SIGMA keyword", status);
SCTPUT(output);
return flames_midas_fail();
}
/* compute once and for all the square of kappa, as we will be using that */
kappa2 = kappa*kappa;
/* read the integration window size, to be used later in
standard extraction */
if ((status=SCKRDD(WINDOW, 1, 1, &actvals, &halfwinsize, &unit, &null))
!=0) {
/* something went wrong while reading the kappa-sigma factor */
sprintf(output, "Error %d while reading WINDOW keyword", status);
SCTPUT(output);
return flames_midas_fail();
}
/* compute once and for all halfwinsize, as we will be using that */
halfwinsize /= 2;
/* Link the MIDAS names of the frames to the physical ones */
if(!(ScienceFrame = calloc(1, sizeof(flames_frame)))) {
SCTPUT("Allocation error during ScienceFrame memory allocation");
return flames_midas_fail();
}
/* Read the data of the frames and put them in the right C structures */
/* let's read the Science Frame */
sprintf(output, "I'm reading the frame %s", infile);
SCTPUT(output);
if (readframe(ScienceFrame, infile) != NOERR) {
SCTPUT("Error while reading the frame");
return flames_midas_fail();
}
/* Read the table data and then put them in the C structures */
sprintf(output,"Reading the order/fibre table...");
SCTPUT(output);
if(!(Order = calloc(1, sizeof(orderpos)))) {
SCTPUT("Allocation error during the allocation of Order structure");
return flames_midas_fail();
}
/* use the readordpos function to read the descriptors */
if (readordpos(orderfile, Order) != NOERR) {
SCTPUT("Error while reading the order table");
return flames_midas_fail();
}
/* does the order table match the Science frame chip? */
if(Order->chipchoice != ScienceFrame->chipchoice) {
/* no, it doesn't */
SCTPUT("Error: chip mismatch between Science frame and order table");
return flames_midas_fail();
}
/* initialise firstorder and lastorder in ScienceFrame from Order */
ScienceFrame->firstorder = Order->firstorder;
ScienceFrame->lastorder = Order->lastorder;
ScienceFrame->tab_io_oshift = Order->tab_io_oshift;
/* allocate and initialise the frame which will contain the
estimated background. This will remain zero if no background fitting
was required*/
lastiyixindex = (ScienceFrame->subrows*ScienceFrame->subcols)-1;
backframe = fdmatrix(0, ScienceFrame->subrows-1, 0,
ScienceFrame->subcols-1);
memset(&backframe[0][0], 0,
ScienceFrame->subrows*ScienceFrame->subcols*sizeof(frame_data));
if (bkginline==TRUE) {
/* Inline background fitting and subtraction required */
SCTPUT("*** Step 1: Background fitting and subtraction ***\n");
sprintf(output, "I'm reading the background table %s", backfile);
SCTPUT(output); /* Display text w/o storing it into MIDAS logfile */
/* read in the background table */
if ((status=readback(&(ScienceFrame->back), backfile, bxdegree, bydegree))
!= NOERR) {
/* something went wrong while reading the background table */
sprintf(output, "Error %d while reading the background table", status);
SCTPUT(output);
return flames_midas_fail();
}
/* Calculate the fit model of the background */
SCTPUT("Start the background fitting procedure");
if (ScienceFrame->back.Window_Number > 0) {
if (scatter(ScienceFrame, Order, bkgswitch, bkgswitch2, badwinxsize,
badwinysize, badfracthres, badtotthres, kappa2,
(int32_t) maxbackiters, maxdiscardfract, OUTPUTI)) {
SCTPUT("Error executing the scatter function");
return flames_midas_fail();
}
/* compute the estimated background */
if (computeback(ScienceFrame, backframe) != NOERR) {
SCTPUT("Error computing fitted background");
return flames_midas_fail();
}
/* subtract the estimated background from the data frame */
/* the error of the estimated background is assumed to be negligible */
SCTPUT("Subtracting fitted background from Science Frame");
fdvecbuf1 = ScienceFrame->frame_array[0];
fdvecbuf2 = backframe[0];
for (ix=0; ix<=lastiyixindex; ix++) {
fdvecbuf1[ix] -= fdvecbuf2[ix];
}
/* some more black magic to make the thing more robust: scan the frame
for any negative pixel values; if any are found, compare them to the
standard deviation: if the negative value is not compatible with zero,
mark that pixel as bad */
negativepixels=0;
fdvecbuf1 = ScienceFrame->frame_array[0];
fdvecbuf2 = ScienceFrame->frame_sigma[0];
fmvecbuf1 = ScienceFrame->badpixel[0];
for (ix=0; ix<=lastiyixindex; ix++) {
if (fmvecbuf1[ix]==0 && (pixelvalue=fdvecbuf1[ix])<0) {
if ((pixelvalue*pixelvalue)>4*fdvecbuf2[ix]) {
fmvecbuf1[ix]=1;
negativepixels++;
}
}
}
if (negativepixels!=0) {
sprintf(output, "Warning: %d pixels result lower than fitted \
background", negativepixels);
SCTPUT(output);
SCTPUT("either they are unmasked bad pixels or some contamination is");
SCTPUT("skewing background determination");
}
SCTPUT("Writing fitted background frame to middumma.bdf");
if (writeback(ScienceFrame, "middumma.bdf", backframe)!=NOERR)
SCTPUT("Warning: error writing background frame to disk");
}
else {
SCTPUT("Error: no regions available for background estimation");
return flames_midas_fail();
}
}
/* Allocate memory for the Shifted_FF structure */
if(!(Shifted_FF = calloc(1, sizeof(allflats)))) {
SCTPUT("Allocation error during Shifted_FF structure memory allocation");
return flames_midas_fail();
}
/* let's read the fibre FFs */
sprintf(output, "I'm reading the fibre FF frames");
SCTPUT(output);
if (readallff(allfile, Shifted_FF) != NOERR) {
SCTPUT("Error while reading the fibre Flat Field frames");
return flames_midas_fail();
}
/* is this fibre FF frames set shiftable? */
if (Shifted_FF->shiftable != 'y') {
sprintf(output, "The fibre FF set is not slit-flatfielded");
SCTPUT(output);
return flames_midas_fail();
}
if (ScienceFrame->nflats != 0) {
/* I need to deallocate the yshift vector and to reallocate it with
a different size */
free_dvector(ScienceFrame->yshift, 0, ScienceFrame->nflats-1);
}
/* set ScienceFrame->nflats equal to Shifted_FF->nflats, and allocate
ScienceFrame->yshift accordingly */
ScienceFrame->nflats = Shifted_FF->nflats;
ScienceFrame->yshift = dvector(0, ScienceFrame->nflats-1);
/* here, in the "fast" version, set the yshifts to zero
with the simulated data the shifts ARE zero! */
for (iframe=0; iframe <= (Shifted_FF->nflats-1); iframe++)
ScienceFrame->yshift[iframe]=0;
/* we must multiply the shifted FF frames by the slit FF, hence allocate
and load the latter */
if(!(Slit_FF = calloc(1, sizeof(allslitflats)))) {
SCTPUT("Allocation error during Slit_FF memory allocation");
return flames_midas_fail();
}
/* let's read the slit FFs */
sprintf(output, "I'm reading the slit FF frames");
SCTPUT(output);
if ((status = readslitflats(slitsfile, Slit_FF)) != NOERR) {
/* problems reading slitflats members from disk */
SCTPUT("Error while reading the slit Flat Field frames");
return flames_midas_fail();
}
orderoffset = Slit_FF->tab_io_oshift-Order->tab_io_oshift;
slitfirstorder = Slit_FF->firstorder;
slitlastorder = Slit_FF->lastorder;
realfirstorder = Order->firstorder;
if (orderoffset > 0) realfirstorder += orderoffset;
reallastorder = Slit_FF->lastorder+orderoffset;
if (reallastorder > Order->lastorder) reallastorder = Order->lastorder;
if (realfirstorder > reallastorder) {
/* this should really never happen with sane data, complain and exit */
strcpy(output, "Error: The orders in the slit FF(s) and in the order \
table being used do not overlap at all!");
SCTPUT(output);
return flames_midas_fail();
}
/* make sure we do not attempt extraction for uncovered fibres */
fmvecbuf1 = Shifted_FF->goodfibres[0][0];
if (Order->firstorder<realfirstorder) {
iorderifibreixstart = 0;
iorderifibreixend = ((realfirstorder-Order->firstorder)*
Shifted_FF->maxfibres*Shifted_FF->subcols)-1;
for (ix=iorderifibreixstart; ix<=iorderifibreixend; ix++) {
fmvecbuf1[ix] = BADSLICE;
}
}
if (Order->lastorder>reallastorder) {
iorderifibreixstart = (reallastorder-Order->firstorder+1)*
Shifted_FF->maxfibres*Shifted_FF->subcols;
iorderifibreixend = ((Order->lastorder-Order->firstorder)*
Shifted_FF->maxfibres*Shifted_FF->subcols)-1;
for (ix=iorderifibreixstart; ix<=iorderifibreixend; ix++) {
fmvecbuf1[ix] = BADSLICE;
}
}
/* correct for the ycorrection, if present */
if (Order->corrected=='t') {
for (iframe=0; iframe<=(Slit_FF->nflats-1);iframe++)
Slit_FF->slit[iframe].yshift -= Order->ycorrection;
}
/* do multiply the shifted fibre FF frames by the slit FF, in place
to save memory */
SCTPUT("Multiplying the shifted FF frame(s) by the slit FF frame(s)");
if (ffslitmultiply(Slit_FF, Order, Shifted_FF, Shifted_FF) != NOERR) {
SCTPUT("Error multiplying the shifted fibre FF by the slit FF");
return flames_midas_fail();
}
/* copy the normalisation factors from the slit FF structure before
freeing it for good */
normcover = fdmatrix(0, Slit_FF->lastorder-Slit_FF->firstorder,
0, Slit_FF->subcols-1);
memcpy(normcover[0], Slit_FF->normfactor[0],
(size_t) ((Slit_FF->lastorder-Slit_FF->firstorder+1)*
Slit_FF->subcols)*sizeof(frame_data));
/* finally free for good the memory for the slit FF */
if (freeslitflats(Slit_FF)!=NOERR) {
SCTPUT("Error while freeing the memory for the Slit_FF structure");
return flames_midas_fail();
}
free(Slit_FF);
SCTPUT("\n*** Step 2: prepare merged bad pixel mask and final \
initialisations ***\n");
/* allocate the global, merged bad pixel mask and the upper and lower
integration limits */
mask=fmmatrix(0,ScienceFrame->subrows-1,0,ScienceFrame->subcols-1);
if(!mask) {
SCTPUT("Error allocating mask matrix");
return flames_midas_fail();
}
pfibrecentre = d3tensor(0, Order->lastorder-Order->firstorder,
0, ScienceFrame->maxfibres-1,
0, ScienceFrame->subcols-1);
if(!pfibrecentre) {
SCTPUT("Error allocating pfibrecentre tensor");
return flames_midas_fail();
}
/* it is pointless to use an integration window larger than fibre width */
if (halfwinsize>Order->halfibrewidth) {
SCTPUT("Warning: reducing window size to fibre width");
halfwinsize = Order->halfibrewidth;
}
if (fabs(ScienceFrame->substepy) < DEPSILON) {
/* the y step is too small, exit */
SCTPUT("Error: the STEP in y is too small");
return flames_midas_fail();
}
phalfwinsize = fabs(halfwinsize/ScienceFrame->substepy);
/* initialise the global, merged bad pixel mask to be used
in the subsequent standard extraction, initialise the lookup tables for
lit fibres in the ScienceFrame and allocate the arrays to store the
allocated spectra again in ScienceFrame */
if (prepstand(ScienceFrame, Shifted_FF, Order, pfibrecentre, normcover,
orderoffset, realfirstorder, reallastorder, mask,
phalfwinsize)!=NOERR) {
/* something went wrong in prepstand */
SCTPUT("Error in prepstand");
return flames_midas_fail();
}
/* Going to do the real spectrum extraction */
SCTPUT("\n*** Step 3: standard extraction proper ***\n");
if (dostandard(ScienceFrame, Order, Shifted_FF, mask, pfibrecentre,
phalfwinsize, realfirstorder, reallastorder) != NOERR) {
SCTPUT("Error in dostandard");
return flames_midas_fail();
}
/* free here pfibrecentre, unneeded from now on */
free_d3tensor(pfibrecentre, 0, Order->lastorder-Order->firstorder,
0, ScienceFrame->maxfibres-1,
0, ScienceFrame->subcols-1);
/* free the old bad pixel mask, replace it with the global mask */
free_fmmatrix(ScienceFrame->badpixel, 0, ScienceFrame->subrows-1, 0,
ScienceFrame->subcols-1);
ScienceFrame->badpixel = mask;
/* build the fitted Science Frame, do it in place to save memory */
if (optsynth(ScienceFrame, Shifted_FF, Order, &backframe, &chisquare,
&chisqpixels, &nfittedparams)!=NOERR){
SCTPUT("Error computing the fitted frame");
return flames_midas_fail();
}
SCKWRD(OUTPUTD, &chisquare, 1, 1, &unit);
SCKWRI(OUTPUTI, &chisqpixels, 1, 1, &unit);
SCKWRI(OUTPUTI, &nfittedparams, 2, 1, &unit);
SCTPUT("Writing the synthesized data frame to middummb.bdf");
SCTPUT("Writing the synthesized sigma frame to middummc.bdf");
SCTPUT("Writing the overall mask to middummd.bdf");
if (writesynth(ScienceFrame, "middummb.bdf", "middummc.bdf", "middummd.bdf")
!= NOERR)
SCTPUT("Warning: error writing dummy debugging frames");
SCTPUT("\n*** Step 4: write extrated spectra to disk, clean up and \
exit ***\n");
/* let's free a bit of memory */
/* free the estimated background frame, we don't need it any more */
free_fdmatrix(backframe, 0, ScienceFrame->subrows-1, 0,
ScienceFrame->subcols-1);
/* free the normcover array */
free_fdmatrix(normcover, 0, Order->lastorder-Order->firstorder,
0, ScienceFrame->subcols-1);
/* Creating the output catalog of the spectra extracted */
//Fixme: the next 3 lines are useless as that catalog is not used elsewhere
//SCTPUT("Creating catalog of the spectra extracted");
//SCCCRE("spectra.cat",F_IMA_TYPE,0);
//SCTPUT("Created catalog of Spectra");
/* can these spectra be normalised? */
if ((Order->corrected == 't')&&(Shifted_FF->normalised == 'y')) {
/* yes, compensation was performed already */
SCTPUT("Correcting for normalisation factors");
fdvecbuf1 = Shifted_FF->normfactors[0][0];
fdvecbuf2 = Shifted_FF->normsigmas[0][0];
fmvecbuf1 = Shifted_FF->goodfibres[0][0];
fdvecbuf3 = ScienceFrame->spectrum[0][0];
frame_data* fdvecbuf4 = ScienceFrame->normsigma[0][0];
frame_data* fdvecbuf5 = ScienceFrame->specsigma[0][0];
frame_data* fdvecbuf6 = ScienceFrame->normspec[0][0];
frame_mask* fmvecbuf2 = ScienceFrame->specmask[0][0];
frame_mask* fmvecbuf3 = ScienceFrame->normmask[0][0];
for (ifibre=0; ifibre<=(ScienceFrame->maxfibres-1); ifibre++) {
/* is this fibre lit? */
if ((ScienceFrame->fibremask[ifibre] == TRUE) &&
(Shifted_FF->fibremask[ifibre] == TRUE)) {
for (iorder=0; iorder<=(Order->lastorder-Order->firstorder); iorder++) {
iorderifibreindex = (iorder*ScienceFrame->maxfibres)+ifibre;
iorderifibreixoffset = iorderifibreindex*ScienceFrame->subcols;
for (ix=0; ix<=(ScienceFrame->subcols-1); ix++) {
iorderifibreixindex = iorderifibreixoffset+ix;
ixiorderifibreindex = (ix*(1+Order->lastorder-Order->firstorder)*
ScienceFrame->maxfibres)+iorderifibreindex;
/* can this fibre be corrected here? */
if ((fmvecbuf2[ixiorderifibreindex]==1) &&
(fmvecbuf1[iorderifibreixindex]==GOODSLICE)) {
/* yes, therefore correct and propagate errors */
frame_data normvalue = fdvecbuf1[iorderifibreixindex];
frame_data normvalue2 = normvalue*normvalue;
pixelvalue = ScienceFrame->spectrum[ix][iorder][ifibre];
fdvecbuf4[ixiorderifibreindex] =
(fdvecbuf5[ixiorderifibreindex]/normvalue2) +
((pixelvalue*pixelvalue)*fdvecbuf2[iorderifibreixindex]/
(normvalue2*normvalue2));
fdvecbuf6[ixiorderifibreindex] =
fdvecbuf3[ixiorderifibreindex]/normvalue;
fmvecbuf3[ixiorderifibreindex] = 1;
}
/* no, mark this slice as bad */
else {
fmvecbuf3[ixiorderifibreindex] = 0;
fdvecbuf6[ixiorderifibreindex] = 0;
fdvecbuf4[ixiorderifibreindex] = 0;
}
}
}
}
}
}
else {
SCTPUT("Warning: no correction factors available");
fdvecbuf1 = ScienceFrame->normspec[0][0];
fdvecbuf2 = ScienceFrame->normsigma[0][0];
fmvecbuf1 = ScienceFrame->normmask[0][0];
for (ifibre=0; ifibre<=(ScienceFrame->maxfibres-1); ifibre++) {
/* is this fibre lit? */
if ((ScienceFrame->fibremask[ifibre] == TRUE) &&
(Shifted_FF->fibremask[ifibre] == TRUE)) {
for (iorder=0; iorder<=(Order->lastorder-Order->firstorder); iorder++) {
iorderifibreindex = (iorder*ScienceFrame->maxfibres)+ifibre;
for (ix=0; ix<=(ScienceFrame->subcols-1); ix++) {
ixiorderifibreindex = (ix*(1+Order->lastorder-Order->firstorder)*
ScienceFrame->maxfibres)+iorderifibreindex;
fmvecbuf1[ixiorderifibreindex] = 0;
fdvecbuf1[ixiorderifibreindex] = 0;
fdvecbuf2[ixiorderifibreindex] = 0;
}
}
}
}
}
for (n=0; n<ScienceFrame->maxfibres; n++) {
if(ScienceFrame->fibremask[n] == TRUE) {
if (n==0) sprintf(output,"Writing spectrum for the first fibre...");
else if (n==1) sprintf(output,"Writing spectrum for the second fibre...");
else if (n==2) sprintf(output,"Writing spectrum for the third fibre...");
else sprintf(output,"Writing spectrum for the %d-th fibre...", n+1);
SCTPUT(output);
status=Write_Spectra(ScienceFrame, n, catfile);
if (status) SCTPUT("Error during the writing on disk");
else if (n==0) sprintf(output,"Spectrum for the first fibre written");
else if (n==1) sprintf(output,"Spectrum for the second fibre written");
else if (n==2) sprintf(output,"Spectrum for the third fibre written");
else sprintf(output,"Spectrum for the %d-th fibre written", n+1);
SCTPUT(output);
}
}
/* finally free for good the ScienceFrame (including the spectrum),
Shifted_FF and Order */
if (free_spectrum(ScienceFrame)!=NOERR) {
SCTPUT("Error while freeing the spectrum in the ScienceFrame structure");
return flames_midas_fail();
}
if (freeframe(ScienceFrame)!=NOERR) {
SCTPUT("Error while freeing the memory of the ScienceFrame structure");
return flames_midas_fail();
}
free(ScienceFrame);
if (freeallflats(Shifted_FF)!=NOERR) {
SCTPUT("Error while freeing the memory of the Shifted_FF structure");
return flames_midas_fail();
}
free(Shifted_FF);
if (freeordpos(Order)!=NOERR) {
SCTPUT("Error while freeing the memory of the Order structure");
return flames_midas_fail();
}
free(Order);
SCTPUT("\n*** Standard extraction complete ***\n");
return SCSEPI();
}
|