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
|
/* $Id: vmmosflat.c,v 1.3 2013-08-22 16:58:58 cgarcia Exp $
*
* This file is part of the VIMOS Pipeline
* Copyright (C) 2002-2004 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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* $Author: cgarcia $
* $Date: 2013-08-22 16:58:58 $
* $Revision: 1.3 $
* $Name: not supported by cvs2svn $
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pilmemory.h>
#include <pilmessages.h>
#include <cpl_msg.h>
#include <piltranslator.h>
#include "vmimage.h"
#include "vmtable.h"
#include "vmgrismtable.h"
#include "vmextractiontable.h"
#include "vmadf.h"
#include "vmmath.h"
#include "vmfit.h"
#include "vmmosflat.h"
#define EXTRA 20.
#define REBIN_LOWER_THRESH 1.e-4
/**
* @name vmmosflat MOS Flat Fields
*
* The module provides the functions for creating and applying a
* spectroscopic master spectral flat field.
*/
/**@{*/
/**
* @memo
* Normalize a spectral Master Flat Field.
*
* @return Pair of images, the first the normalized flat field, the
* second a misterious "count" image that nobody apparently
* know about its meaning.
*
* @param imageData Master spectral Flat to be normalized.
* @param extractionTable Input Extraction Table
* @param polyDegX Degree of polynomial fitting any spectral
* flat field trend along the X (spatial)
* direction.
* @param polyDegY Degree of polynomial fitting any spectral
* flat field trend along the Y (dispersion)
* direction.
*/
VimosImage *
VmSpNormFF(VimosImage *imageData, VimosExtractionTable *extractionTable,
int normMethod, int polyDegX, int polyDegY, FilterMethod method,
int xSize, int ySize)
{
VimosImage *ffData;
if (normMethod == 1)
ffData = VmSpNormPoly(imageData, extractionTable, polyDegX, polyDegY);
else
ffData = VmSpNormSmooth(imageData, extractionTable, xSize, ySize, method);
return ffData;
}
/**
* @memo
* Normalize a spectral Master Flat Field.
*
* @return Normalized flat field.
*
* @param imageData Master spectral Flat to be normalized.
* @param extractionTable Input Extraction Table
* @param polyDegX Degree of polynomial fitting any spectral
* flat field trend along the X (spatial)
* direction.
* @param polyDegY Degree of polynomial fitting any spectral
* flat field trend along the Y (dispersion)
* direction.
*/
VimosImage *
VmSpNormPoly(VimosImage *imageData, VimosExtractionTable *extractionTable,
int polyDegX, int polyDegY)
{
int i, j, k;
int xOut, yOut;
int numRows;
int numPoints;
VimosUlong32 index;
int nPixBelow, nPixAbove;
int imageXlen, imageYlen;
int ycount=0;
double *coeffsX = NULL;
double *coeffsY = NULL;
double ffVal;
double ffValX;
double ffValY;
double ffSum;
double datVal;
double xp;
double xOutF;
double frac;
VimosImage *ffData;
VimosImage *countData;
VimosDpoint *bufferX = NULL;
VimosDpoint *bufferY = NULL;
VimosExtractionSlit *slit;
char modName[] ="VmSpNormPoly";
cpl_msg_debug(modName, "Normalize Flat Field");
imageXlen = imageData->xlen;
imageYlen = imageData->ylen;
countData = newImageAndAlloc(imageXlen, imageYlen);
ffData = newImageAndAlloc(imageXlen, imageYlen);
for (xOut = 0; xOut < imageXlen; xOut++) {
for (yOut = 0; yOut < imageYlen; yOut++) {
ffData->data[xOut+imageXlen*yOut] = 0.0;
countData->data[xOut+imageXlen*yOut] = 0.0;
}
}
slit = extractionTable->slits;
readIntDescriptor(extractionTable->descs, "ESO PRO SPECT LLEN LO",
&nPixBelow, NULL);
readIntDescriptor(extractionTable->descs, "ESO PRO SPECT LLEN HI",
&nPixAbove, NULL);
numPoints = nPixBelow+nPixAbove+1;
bufferY = newDpoint(numPoints);
while (slit) {
numRows = slit->numRows;
for (i = 0 ; i < numPoints; i++) {
bufferY[i].x = i;
bufferY[i].y = 0.0;
}
deleteDpoint(bufferX);
bufferX = newDpoint(numRows);
for (i = 1; i < numRows-1; i++) {
bufferX[i-1].x = slit->ccdX->data[i];
bufferX[i-1].y = 0.0;
}
ffSum = 0.0;
for (i = 2; i < numRows-2; i++) {
ycount = 0;
for (j = -nPixBelow+1; j < nPixAbove; j++) {
yOut = slit->ccdY->data[i] + j;
xOutF = slit->ccdX->data[i]+computeDistModel1D(slit->crvPol[i],yOut);
xOut = xOutF;
frac = xOutF-xOut;
if (xOut >= 0 && xOut+1 < imageXlen && yOut >=0 && yOut < imageYlen) {
index = xOut+yOut*imageXlen;
datVal = ( (1.0-frac)*imageData->data[index] +
frac*imageData->data[index+1] );
bufferX[i-2].y += datVal;
bufferY[ycount].y += datVal;
ycount++;
ffSum += datVal;
}
}
}
if (coeffsX != NULL) {
pil_free(coeffsX);
coeffsX = NULL;
}
if (coeffsY != NULL) {
pil_free(coeffsY);
coeffsY = NULL;
}
coeffsX = fit1DPoly(polyDegX, bufferX, numRows-4, NULL);
coeffsY = fit1DPoly(polyDegY, bufferY, ycount, NULL);
for (i = 1; i < numRows-1; i++) {
ycount = 0;
for (j = -nPixBelow+1; j < nPixAbove; j++) {
yOut = slit->ccdY->data[i] + j;
xOutF = computeDistModel1D(slit->crvPol[i], yOut);
xOutF = slit->ccdX->data[i]+xOutF;
xOut = xOutF;
if ( (xOut >= 0) && (xOut < imageXlen) &&
(yOut >= 0) && (yOut < imageYlen) ) {
frac = xOutF-xOut;
ffValX = coeffsX[0];
for (k = 1; k <= polyDegX; k++) {
xp = ipow(slit->ccdX->data[i], k) ;
ffValX += coeffsX[k] * xp ;
}
ffValY = coeffsY[0];
for (k = 1; k <= polyDegY; k++) {
xp = ipow(1.0*(ycount+1), k) ;
ffValY += coeffsY[k] * xp ;
}
ycount++;
ffVal = ffValX*ffValY/ffSum;
index = xOut+yOut*imageXlen;
ffData->data[index] += (1.0-frac)*ffVal;
ffData->data[index+1] += frac*ffVal;
countData->data[index] += 1.0-frac;
countData->data[index+1] += frac;
}
}
/* TBD CHECK ON FLAT_TOL and write new table */
}
slit = slit->next;
}
deleteDpoint(bufferX);
deleteDpoint(bufferY);
for (xOut = 0; xOut < imageXlen; xOut++) {
for (yOut = 0; yOut < imageYlen; yOut++) {
index = xOut+yOut*imageXlen;
if (countData->data[index] > 0.0) {
ffData->data[index] /= countData->data[index];
ffData->data[index] = imageData->data[index] / ffData->data[index];
}
else {
ffData->data[index] = 1.0;
}
}
}
copyAllDescriptors(imageData->descs, &(ffData->descs));
deleteImage(countData);
return ffData;
}
/**
* @memo
* Normalize a spectral Master Flat Field.
*
* @return One image containing the first the normalized flat field.
*
* @param imageData Master spectral Flat to be normalized.
* @param extractionTable Input Extraction Table
* @param xSize size in x of the smoothing filter box
* @param ySize size in y of the smoothing filter box
* @param method smoothing filter method
*
*/
VimosImage *
VmSpNormSmooth(VimosImage *imageData, VimosExtractionTable *extractionTable,
int xSize, int ySize, FilterMethod method)
{
int i, j;
int xOut, yOut;
int numRows;
int numPoints;
VimosUlong32 index;
int nPixBelow, nPixAbove;
int imageXlen, imageYlen;
double xOutF;
double frac;
double smoothedValue;
VimosImage *extractedSpectrum;
VimosImage *smoothedSpectrum;
VimosImage *countData;
VimosImage *ffData;
VimosExtractionSlit *slit;
char modName[] ="VmSpNormSmooth";
cpl_msg_info(modName, "Normalize Flat Field");
imageXlen = imageData->xlen;
imageYlen = imageData->ylen;
countData = newImageAndAlloc(imageXlen, imageYlen);
ffData = newImageAndAlloc(imageXlen, imageYlen);
for (xOut = 0; xOut < imageXlen; xOut++) {
for (yOut = 0; yOut < imageYlen; yOut++) {
countData->data[xOut+imageXlen*yOut] = 0.0;
ffData->data[xOut+imageXlen*yOut] = 0.0;
}
}
slit = extractionTable->slits;
readIntDescriptor(extractionTable->descs, "ESO PRO SPECT LLEN LO",
&nPixBelow, NULL);
readIntDescriptor(extractionTable->descs, "ESO PRO SPECT LLEN HI",
&nPixAbove, NULL);
numPoints = nPixBelow+nPixAbove+1;
while (slit) {
numRows = slit->numRows;
if (numRows < 2) {
slit = slit->next;
continue;
}
extractedSpectrum = newImageAndAlloc(numRows, nPixBelow+nPixAbove+1);
for (i = 0; i < numRows; i++) {
for (j = -nPixBelow; j < nPixAbove+1; j++) {
yOut = slit->ccdY->data[i] + j;
xOutF = slit->ccdX->data[i]+computeDistModel1D(slit->crvPol[i],yOut);
xOut = xOutF;
frac = xOutF-xOut;
if (xOut >= 0 && xOut+1 < imageXlen && yOut >=0 && yOut < imageYlen) {
index = xOut+yOut*imageXlen;
/* "Warp" spectrum to a rectangular area */
extractedSpectrum->data[i+(j+nPixBelow)*numRows] =
((1.0-frac) * imageData->data[index] +
frac*imageData->data[index+1] );
}
}
}
/* smooth it */
smoothedSpectrum = VmFrFilter(extractedSpectrum, 1, ySize, method, 0);
/* "Warp" back to output image */
for (i = 0; i < numRows; i++) {
for (j = -nPixBelow; j < nPixAbove+1; j++) {
yOut = slit->ccdY->data[i] + j;
xOutF = slit->ccdX->data[i]+computeDistModel1D(slit->crvPol[i],yOut);
xOut = xOutF;
frac = xOutF-xOut;
if (xOut >= 0 && xOut+1 < imageXlen && yOut >=0 && yOut < imageYlen) {
index = xOut+yOut*imageXlen;
smoothedValue = smoothedSpectrum->data[i+(j+nPixBelow)*numRows];
if ((1.0-frac)*smoothedValue > REBIN_LOWER_THRESH) {
ffData->data[index] += (1.0-frac)*smoothedValue;
countData->data[index] += 1.0-frac;
}
if (frac*smoothedValue > REBIN_LOWER_THRESH) {
ffData->data[index+1] += frac*smoothedValue;
countData->data[index+1] += frac;
}
}
}
}
deleteImage(extractedSpectrum);
deleteImage(smoothedSpectrum);
slit = slit->next;
}
/* Correct weights and normalize */
for (xOut = 0; xOut < imageXlen; xOut++) {
for (yOut = 0; yOut < imageYlen; yOut++) {
index = xOut+yOut*imageXlen;
if (countData->data[index] > 0.0) {
ffData->data[index] /= countData->data[index];
ffData->data[index] = imageData->data[index] / ffData->data[index];
}
else {
ffData->data[index] = 1.0;
}
}
}
copyAllDescriptors(imageData->descs, &(ffData->descs));
deleteImage(countData);
return ffData;
}
VimosImage **
VmSpStackFF(VimosImage **flatImages, int numFlat,
VimosExtractionTable *extrTable, int fuzz)
{
char modName[] = "VmSpStack";
VimosTable *adf;
VimosAdfType adfType;
VimosImage *inputImage;
VimosImage **outputImages=NULL;
VimosImage *zeroOrderIma=NULL;
VimosImage *firstOrderIma;
VimosDpoint *minY;
VimosDpoint *maxY;
VimosDpoint *zeroY;
VimosExtractionSlit *slit;
int doZero;
int i, j;
int imaXlen;
int imaYlen;
int xOut;
int yOut;
int nSlits;
int initialize = 1;
int zWidth = 10;
char comment[80];
cpl_msg_info(modName, "Stacking %d flat fields", numFlat);
outputImages = (VimosImage **) pil_malloc(2*sizeof(VimosImage *));
if (readIntDescriptor(extrTable->descs, pilTrnGetKeyword("ZeroOrderFlag"),
&doZero, comment) == VM_FALSE) {
cpl_msg_error(modName, "Cannot find descriptor %s",
pilTrnGetKeyword("ZeroOrderFlag"));
return NULL;
}
if (doZero && numFlat > 1)
{
for (i=0; i<numFlat; i++)
{
inputImage = flatImages[i];
imaXlen = inputImage->xlen;
imaYlen = inputImage->ylen;
/* create empty ADF */
adfType = VM_ADF_TYPE_UDF;
adf = newADF();
/* read ADF from image */
readADF(adf, inputImage);
if (!strcmp(adf->name, VM_ADF_IMA)) {
adfType = VM_ADF_TYPE_IMA;
}
if (!strcmp(adf->name, VM_ADF_MOS)) {
adfType = VM_ADF_TYPE_MOS;
}
if (!strcmp(adf->name, VM_ADF_IFU)) {
adfType = VM_ADF_TYPE_IFU;
}
if (adfType == VM_ADF_TYPE_UDF) {
return NULL;
}
if (adfType != VM_ADF_TYPE_MOS) {
cpl_msg_error(modName, "Trying to stack exposures other than MOS...");
return NULL;
}
else {
/* the first time around, create output image */
if (initialize)
{
zeroOrderIma = newImageAndAlloc(imaXlen,imaYlen);
copyAllDescriptors(adf->descs, &(zeroOrderIma)->descs);
firstOrderIma = newImageAndAlloc(imaXlen,imaYlen);
copyAllDescriptors(adf->descs, &(firstOrderIma)->descs);
initialize = 0;
}
/* determine frame region that is effectively exposed */
determineExposedMosArea(adf,extrTable,&nSlits,&minY,&maxY,&zeroY);
/* write in the output image the appropriate section of input image */
for (j = 0; j < nSlits; j++)
{
for (xOut = floor(minY->x) - fuzz;
xOut <= ceil(maxY->x) + fuzz; xOut++)
{
for (yOut=floor(minY->y)-fuzz; yOut<=ceil(maxY->y)+fuzz; yOut++)
{
if (xOut >= 0 && xOut < imaXlen && yOut >=0 && yOut < imaYlen) {
firstOrderIma->data[xOut+imaXlen*yOut] =
inputImage->data[xOut+imaXlen*yOut];
}
}
for(yOut=floor(zeroY->y-zWidth);yOut<=ceil(zeroY->y+zWidth); yOut++)
{
if (xOut >= 0 && xOut < imaXlen && yOut >=0 && yOut < imaYlen) {
zeroOrderIma->data[xOut+imaXlen*yOut] =
inputImage->data[xOut+imaXlen*yOut];
}
}
}
minY = minY->prev;
maxY = maxY->prev;
zeroY = zeroY->prev;
}
deleteADF(adf);
}
}
/*
* Check whether some slits are left in the extraction table. The
* remaining slits are the ones that are never illuminated by any
* of the identified shutter positions. I believe that this should
* never happen, because in that case the next routines would try
* to use such slits, looking for them in the stacked images to
* try fitting the optical distorsion and the spectral curvature
* models. (C.Izzo)
*/
if (extrTable->slits) {
slit = extrTable->slits;
j = 0;
while (slit) {
j++;
slit = slit->next;
}
cpl_msg_error(modName, "%d unexposed slits found!", j);
deleteImage(firstOrderIma);
deleteImage(zeroOrderIma);
pil_free(outputImages);
return NULL;
}
outputImages[0] = firstOrderIma;
outputImages[1] = zeroOrderIma;
}
else
{
outputImages[0] = duplicateImage(flatImages[0]);
copyAllDescriptors(flatImages[0]->descs, &(outputImages[0])->descs);
outputImages[1] = NULL;
}
return outputImages;
}
VimosImage *
VmSpApplyFF(VimosImage *inputImage, VimosImage *flatImage,
VimosExtractionTable *extrTable)
{
VimosImage *normImage;
VimosUlong32 numPoints;
VimosUlong32 index;
VimosExtractionSlit *slit;
VimosDpoint *nearPoints = NULL;
float xZero,yZero;
float zeroWidth;
double dataVal;
double *curve = NULL;
int imageXlen, imageYlen;
int numRows;
int doZero;
int replaceWidth;
int xOut, yOut;
int i, j, k;
int interpOrder = 2;
char modName[] = "VmSpApplyFF";
cpl_msg_debug(modName,"Applying Flat Field");
imageXlen = inputImage->xlen;
imageYlen = inputImage->ylen;
numPoints = imageXlen*imageYlen;
if (readIntDescriptor(extrTable->descs, pilTrnGetKeyword("ZeroOrderFlag"),
&doZero, NULL) == VM_FALSE) {
cpl_msg_error(modName, "Cannot find descriptor %s",
pilTrnGetKeyword("ZeroOrderFlag"));
return NULL;
}
/* interpolation to remove the zero order contamination (if necessary) */
if (doZero)
{
if (readFloatDescriptor(extrTable->descs,
pilTrnGetKeyword("ZeroOrderWidth"),
&zeroWidth, NULL) == VM_FALSE)
{
cpl_msg_error(modName, "Cannot find descriptor %s",
pilTrnGetKeyword("ZeroOrderWidth"));
return NULL;
}
replaceWidth = ceil(zeroWidth) + 1;
slit = extrTable->slits;
nearPoints = newDpoint(2 * replaceWidth);
/* loop over all slits */
while (slit) {
/* spatial length of slit */
numRows = slit->numRows;
for (i = 0; i < numRows; i++ )
{
yZero = slit->ccdY->data[i] + slit->zeroY->data[i];
xZero = slit->ccdX->data[i] + slit->zeroX->data[i];
if (yZero<0 || yZero>=imageYlen || xZero<0 || xZero>=imageXlen) {
continue;
}
/* build up the array of points below and above the zero order
contamination, needed to carry out the interpolation */
xOut = xZero;
/* the pixels below the contaminated area */
for (j = 0; j < replaceWidth; j++)
{
yOut = (int) yZero - (int) (1.5 * replaceWidth) + j;
index = xOut+yOut*imageXlen;
dataVal = inputImage->data[index];
/* and store in buffer */
nearPoints[j].x = yOut;
nearPoints[j].y = dataVal;
}
/* and the pixels above the contaminated area */
for (j = 0; j < replaceWidth; j++)
{
yOut = (int) yZero + ceil(0.5 * replaceWidth) + j;
index = xOut+yOut*imageXlen;
dataVal = inputImage->data[index];
/* and store in buffer */
nearPoints[j+replaceWidth].x = yOut;
nearPoints[j+replaceWidth].y = dataVal;
}
/* do a poly fit to the data */
curve = fit1DPoly(interpOrder, nearPoints, 2*replaceWidth, NULL);
if (curve == NULL) {
return NULL;
}
/* and compute the interpolated values in the contaminated area */
for (j = 0; j < replaceWidth; j++)
{
yOut = (int) yZero - (int)(0.5 * replaceWidth) + j;
index = xOut+yOut*imageXlen;
dataVal = 0.;
for (k = 0; k <= interpOrder; k++) {
dataVal += ipow((double)yOut,k) * curve[k];
}
inputImage->data[index] = dataVal;
}
}
/* go to next slit */
slit = slit->next;
}
}
normImage = newImageAndAlloc(imageXlen, imageYlen);
for (index = 0; index < numPoints; index++) {
if (flatImage->data[index] != 0.)
{
normImage->data[index] = inputImage->data[index] /
flatImage->data[index];
}
else
{
normImage->data[index] = inputImage->data[index];
}
}
copyAllDescriptors(inputImage->descs, &(normImage->descs));
return normImage;
}
VimosBool determineExposedMosArea(VimosTable *adf,
VimosExtractionTable *extTable, int *nSlits,
VimosDpoint **minY, VimosDpoint **maxY, VimosDpoint **zeroY)
{
const char modName[] = "determineExposedMosArea";
int i,j,k;
int nPixBelow;
int nPixAbove;
int crvOrder;
float xMin;
float xMax;
float yMin;
float yMax;
float yZero;
float delXmin = 0.;
float delXmax = 0.;
float delXlo;
float delXhi;
char comment[80];
VimosExtractionSlit *exSlit;
VimosExtractionSlit *expoSlit;
VimosBool firstTime;
/* validate input */
if (extTable == NULL) {
cpl_msg_error(modName, "NULL input table");
return (VM_FALSE);
}
exSlit = extTable->slits;
expoSlit = determineExposedSlits(adf, &(exSlit), nSlits);
if (expoSlit == NULL) {
cpl_msg_error(modName, "Function determineExposedSlits failure");
return(VM_FALSE);
}
extTable->slits = exSlit;
*minY = newDpoint(*nSlits);
if (*minY == NULL) {
cpl_msg_error(modName, "Function newDpoint failure");
return(VM_FALSE);
}
*maxY = newDpoint(*nSlits);
if (*maxY == NULL) {
cpl_msg_error(modName, "Function newDpoint failure");
return(VM_FALSE);
}
*zeroY = newDpoint(*nSlits);
if (*zeroY == NULL) {
cpl_msg_error(modName, "Function newDpoint failure");
return(VM_FALSE);
}
if (!readIntDescriptor(extTable->descs, "ESO PRO SPECT LLEN LO", &nPixBelow,
comment)) {
cpl_msg_error(modName, "Cannot read descriptor %s", "ESO PRO SPECT LLEN LO");
return(VM_FALSE);
}
if (!readIntDescriptor(extTable->descs, "ESO PRO SPECT LLEN HI", &nPixAbove,
comment)) {
cpl_msg_error(modName, "Cannot read descriptor %s", "ESO PRO SPECT LLEN HI");
return(VM_FALSE);
}
firstTime = VM_TRUE;
while (expoSlit) {
/*
* Smallest rectangle containing the slit (that may be oblique).
* Note that this method doesn't necessarily work for curved
* slits where the max or min Y may not correspond to the slit
* ends. For the same reason, in this case yZero would not be
* the Y coordinate of the slit center. Nevertheless, this is
* quite tolerable (C.Izzo)
*/
xMin = expoSlit->ccdX->data[0];
xMax = expoSlit->ccdX->data[(expoSlit->numRows)-1];
yMin = expoSlit->ccdY->data[0];
yMax = expoSlit->ccdY->data[(expoSlit->numRows)-1];
yZero = 0.5 * (yMin + yMax);
crvOrder = expoSlit->crvPol[0]->order;
for (i = 0; i <= (nPixAbove+nPixBelow); i++) {
delXlo = 0.;
delXhi = 0.;
k = i - nPixBelow;
/*
* Note: in computing the X position due to the curvature, the
* offset parameter is not used (C.Izzo)
*/
for (j = 0; j <= crvOrder; j++) {
delXlo += ipow((double) k, j) *
expoSlit->crvPol[0]->coefs[j];
delXhi += ipow((double) k, j) *
expoSlit->crvPol[(expoSlit->numRows)-1]->coefs[j];
}
if (i) {
if (delXlo < delXmin) delXmin = delXlo;
if (delXhi > delXmax) delXmax = delXhi;
}
else {
/* Initialization */
delXmin = delXlo;
delXmax = delXhi;
}
}
/*
* Smallest rectangle containg the whole spectrum:
*/
yMax = MAX(yMin, yMax) + nPixAbove;
yMin = MIN(yMin, yMax) - nPixBelow;
xMax += delXmax;
xMin += delXmin;
/*
* Here yZero is upgraded to be the middle point Y coordinate
* of the slit for the first order, plus the middle point Y
* diff coordinate of the slit for the zeroth order. In total,
* is the Y coordinate of the middle point of the slit for the
* zeroth order. Note that it could be set like that since the
* beginning, as it wasn't used in the meantime. (C.Izzo)
*/
yZero += 0.5 * (expoSlit->zeroY->data[0] +
expoSlit->zeroY->data[(expoSlit->numRows)-1]);
if (firstTime == VM_TRUE) {
firstTime = VM_FALSE;
}
else {
*minY = (*minY)->next;
*maxY = (*maxY)->next;
*zeroY = (*zeroY)->next;
}
(*minY)->x = (double) xMin;
(*minY)->y = (double) yMin;
(*maxY)->x = (double) xMax;
(*maxY)->y = (double) yMax;
(*zeroY)->y = (double) yZero;
/*
* Note that in this way the pointer is not set back to the
* beginning of the list. The last values of *minY, *maxY
* and *zeroY are returned, being passed by address. This
* must be taken into account in the caller (C.Izzo)
*/
expoSlit = expoSlit->next;
}
return (VM_TRUE);
}
VimosExtractionSlit *determineExposedSlits(VimosTable *adf,
VimosExtractionSlit **exSlit, int *nSlits)
{
const char modName[] = "determineExposedSlits";
char comment[80];
int quadNum;
float yMin;
float yMax;
float refLow;
float refHigh;
float slYmin;
float slYmax;
VimosExtractionSlit *lastSlit;
VimosExtractionSlit *lastLostSlit;
VimosExtractionSlit *lostSlit;
VimosExtractionSlit *returnSlit;
VimosExtractionSlit *t1Slit;
VimosExtractionSlit *t2Slit;
VimosBool firstTime, foundNoIllum, firstNoIllum;
/*
* find out the minimum and maximum y mask coordinate in the
* illuminated area
*/
if (readIntDescriptor(adf->descs, pilTrnGetKeyword("Quadrant"),
&quadNum, comment) == VM_FALSE) {
cpl_msg_error(modName, "Cannot read descriptor %s",
pilTrnGetKeyword("Quadrant"));
return NULL;
}
if (readFloatDescriptor(adf->descs, pilTrnGetKeyword("MshuPosH", quadNum),
&yMax, comment) == VM_FALSE) {
cpl_msg_error(modName, "Cannot read descriptor %s",
pilTrnGetKeyword("MshuPosH", quadNum));
return NULL;
}
if (readFloatDescriptor(adf->descs, pilTrnGetKeyword("MshuPosL", quadNum),
&yMin, comment) == VM_FALSE) {
cpl_msg_error(modName, "Cannot read descriptor %s",
pilTrnGetKeyword("MshuPosL", quadNum));
return NULL;
}
if (readFloatDescriptor(adf->descs, pilTrnGetKeyword("MshuRefH", quadNum),
&refHigh, comment) == VM_FALSE) {
cpl_msg_error(modName, "Cannot read descriptor %s",
pilTrnGetKeyword("MshuRefH", quadNum));
return NULL;
}
if (readFloatDescriptor(adf->descs, pilTrnGetKeyword("MshuRefL", quadNum),
&refLow, comment) == VM_FALSE) {
cpl_msg_error(modName, "Cannot read descriptor %s",
pilTrnGetKeyword("MshuRefL", quadNum));
return NULL;
}
/* correct the position of the shutters as given by the ICS for the
positioning offset between VMMCS and ICS */
yMin = yMin - refLow;
yMax = refHigh - yMax;
*nSlits = 0;
lastSlit = NULL;
lostSlit = NULL;
t1Slit = NULL;
firstTime = VM_TRUE;
if (!slitMinMaxY(*exSlit, &slYmin, &slYmax)) {
cpl_msg_error(modName, "Function slitMinMaxY failure");
return(NULL);
}
/* FIXME:
* Note that this check may lead to "lost slits" for
* any one of the shutter positions: a slit at the limit
* of the windows may have a minimum inside a window,
* and a maximum inside another. This slit would be
* rejected in both cases (C.Izzo)
*/
/* determination of the new entry point in the slits list */
/* skip those slits located outside the shutter window */
while (slYmin < yMin || slYmin > yMax || slYmax < yMin || slYmax > yMax) {
if (firstTime) {
/* FIXME:
* As soon as the "while" is entered (and this can be only
* if the very first slit is outside the shutter window),
* the list of rejected lists (lostSlit) is initialized
* to the input slit list. The temporary pointer t1Slit
* is positioned to the next element, which is also checked
* and we stay in the "while" as long as slits are outside
* the shutter window. (C.Izzo)
*/
lostSlit = *exSlit;
/* t1Slit = newExtractionSlit(); This is unnecessary (memory leak) (C.Izzo) */
t1Slit = (*exSlit)->next;
firstTime = VM_FALSE;
}
else {
t1Slit = t1Slit->next;
}
if (t1Slit == NULL) {
/* FIXME:
* I believe that this error message is inappropriate:
* we reach this point as soon as all slits are rejected,
* that is, no slits are found within the shutter window.
* This is an acceptable situation. A NULL is correctly
* returned, but it is the higher level function that
* should decide how to deal with this (legal) case. (C.Izzo)
*/
/* cpl_msg_error(modName, "NULL next pointer in the structure"); */
cpl_msg_error(modName, "No slits are found within this shutter window");
return (NULL);
}
if (!slitMinMaxY(t1Slit, &slYmin, &slYmax)) {
cpl_msg_error(modName, "Function slitMinMaxY failure");
return(NULL);
}
}
/*
* Here t1Slit points to the first slit found within the shutter
* window. The only case in which it might be t1Slit = NULL is
* that the "while" was never entered (i.e., the first slit is
* inside the shutter window) (C.Izzo)
*/
if (t1Slit) {
lastSlit = t1Slit;
returnSlit = t1Slit;
lastLostSlit = t1Slit->prev;
lastLostSlit->next = NULL; /* To set lastLostSlit->next = NULL is */
t1Slit->prev->next = NULL; /* equivalent to set t1Slit->prev->next = NULL,
therefore one of the 2 lines is superfluous.
(C.Izzo) */
/* NOTE: The *exSlit list is not duplicated when we set t1Slit
to point to one element of the slit list. Therefore, to set
t1Slit->prev->next = NULL is equivalent to cut the original
list at this point. We have then a list of rejected slits
starting at *exSlit, and ending with the first accepted slit
(excluded). (C.Izzo) */
}
else {
/*
* The "while" was never entered (i.e., the first slit is
* inside the shutter window) (C.Izzo)
*/
lastSlit = *exSlit;
returnSlit = *exSlit;
/* FIXME:
* lastLostSlit in this case is NOT initialized. It contains
* garbage. This might be a bug to solve (C.Izzo)
*/
}
/*
* Also returnSlit points currently to the first illuminated slit.
* To complete the separation of the list of illuminated and not
* illuminated slits, its previous element must be set to NULL.
* (C.Izzo)
*/
returnSlit->prev = NULL;
firstTime = VM_TRUE;
foundNoIllum = VM_FALSE;
/* determination of the remaining exposed slits */
if (lastSlit->next != NULL) { /* Does another slit exists, after the */
do { /* last illuminated one? (C.Izzo) */
/* t2Slit = newExtractionSlit(); This is unnecessary (memory leak) (C.Izzo)
if (t2Slit == NULL) {
cpl_msg_error(modName, "Function newExtractionSlit failure");
return(NULL);
} ***/
t2Slit = lastSlit->next; /* Points to next slit to check (C.Izzo) */
if (!slitMinMaxY(t2Slit, &slYmin, &slYmax)) {
cpl_msg_error(modName, "Function slitMinMaxY failure");
return(NULL);
}
firstNoIllum = VM_TRUE;
/* skip those slits located outside the shutter window */
while (slYmin < yMin || slYmin > yMax || slYmax < yMin || slYmax > yMax) {
foundNoIllum = VM_TRUE;
/*
* lostSlit is NULL only if no slit was lost at the
* beginning of the list (C.Izzo)
*/
if (!lostSlit) {
/*
* If it is so, then we have found the beginning of the list
* of lost slits. Then, cut it away from the original list
* (C.Izzo)
*/
lostSlit = t2Slit;
lostSlit->prev = NULL;
firstNoIllum = VM_FALSE;
}
else if (firstNoIllum) {
/*
* Instead, if this is just another rejected slit, and it is
* the first one to be found in this round, just append it
* to the list of lost slits. This was surely initialized,
* because we get here only if some slits were rejected
* at the beginning of the list (C.Izzo)
*/
lastLostSlit->next = t2Slit;
t2Slit->prev = lastLostSlit;
firstNoIllum = VM_FALSE;
}
t2Slit = t2Slit->next;
if (t2Slit == NULL) {
/* close linked list */
lastSlit->next = NULL;
/*
* *exSlit, passed by address, is also returned: it contains
* now just the rejected slits (the only necessary for further
* checks whith different shutter positions). But note: now
* the original list is destroyed (C.Izzo).
*/
*exSlit = lostSlit;
return returnSlit;
}
else {
if (!slitMinMaxY(t2Slit, &slYmin, &slYmax)) {
cpl_msg_error(modName, "Function slitMinMaxY failure");
return(NULL);
}
}
}
if (foundNoIllum) {
/*
* If the last "while" was entered, upgrade the position
* of the last lost slit. (C.Izzo)
*/
lastLostSlit = t2Slit->prev;
lastLostSlit->next = NULL;
t2Slit->prev->next = NULL; /* Superfluous, see note above (C.Izzo) */
foundNoIllum = VM_FALSE;
}
t2Slit->prev = lastSlit;
if (firstTime) {
/*
* Disconnect the still-to-be-checked slit list from the
* original list, and connect it to the list of rejected
* (C.Izzo)
*/
t2Slit->prev = returnSlit;
returnSlit->next = t2Slit;
/*
* Initialization of the returned number of illuminated
* slits. Every time we get here, we terminated an interval
* of rejected slits. The first round was at the beginning
* of the program, there we have found the first illuminated
* slit. If we get here we have found another illuminated
* slit. That makes 2 (C.Izzo).
*/
(*nSlits) = 2;
firstTime = VM_FALSE;
}
else {
/*
* Every time we get here, we terminated an interval
* of rejected slits. One illuminated slit (the one that
* interrupted the scan) is counted (C.Izzo)
*/
lastSlit->next = t2Slit;
(*nSlits)++;
}
lastSlit = t2Slit;
} while (t2Slit->next != NULL);
}
else {
returnSlit->next = NULL;
(*nSlits) = 1;
}
*exSlit = lostSlit;
return returnSlit;
}
VimosBool slitMinMaxY(VimosExtractionSlit *exSlit, float *slYmin, float *slYmax)
{
float min, max;
int i;
min = max = exSlit->maskY->data[0];
for (i = 1; i < exSlit->numRows; i++) {
if (exSlit->maskY->data[i] < min) min = exSlit->maskY->data[i];
if (exSlit->maskY->data[i] > max) max = exSlit->maskY->data[i];
}
*slYmax = max;
*slYmin = min;
return (VM_TRUE);
}
/**@}*/
|