File: vmmosfringes.c

package info (click to toggle)
cpl-plugin-vimos 2.9.15%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,560 kB
  • ctags: 6,341
  • sloc: ansic: 148,777; sh: 11,457; cpp: 724; makefile: 606; python: 287; perl: 10
file content (228 lines) | stat: -rw-r--r-- 6,301 bytes parent folder | download
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
/* $Id: vmmosfringes.c,v 1.2 2013-03-25 11:43:04 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-03-25 11:43:04 $
 * $Revision: 1.2 $
 * $Name: not supported by cvs2svn $
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <string.h>

#include <pilmemory.h>
#include <pilmessages.h>
#include <cpl_msg.h>
#include <piltranslator.h>

#include "vmimage.h"   
#include "vmwindowtable.h"


#define MAX_COMMENT_LENGTH (80)


int
VmSpFringCorr(VimosImage **mosImages, VimosWindowTable **winTables,
              int mosCount, int pixels, int interpolate)
{
  char        task[]               = "VmSpFringCorr";

  int                   xStart           = 0;
  int                   xEnd             = 0;
  int                   j, i, imaX, imaY;
  int                   left,right;
  int                   badObjects;
  int                   startFound;

  float                 valEnd,valStart,m,value;



  VimosImage           *meanSkyImage     = NULL;
  VimosImage          **subMosImages     = NULL;

  VimosWindowObject    *object;

  VimosWindowSlit      *wSlit            = NULL;

/*    char        comment[MAX_COMMENT_LENGTH]; */
/*    int                   startPixelSub    = 0; */
/*    float                 startWLenSub=7700.;  */
/*    float                 wLenStart,wLenInc; */
/*    double                dValue; */




  subMosImages = (VimosImage **)pil_calloc(mosCount, sizeof(VimosImage *));
  if (!subMosImages) { 
    cpl_msg_error(task, "Failure creating list of 2D sky images");
    return EXIT_FAILURE;
  }
    
  for (i = 0; i < mosCount; i++) {
    subMosImages[i] = duplicateImage(mosImages[i]);
    wSlit = winTables[i]->slits;
    
    while(wSlit) {
      object = wSlit->objs;
      while(object) {
	for(imaX=0; imaX<subMosImages[i]->xlen; imaX++){
	  left=right=1;
	  
	  /* starting end ending pixel */
	  xStart = wSlit->specStart + object->objStart - pixels;
	  xEnd = wSlit->specStart + object->objEnd + pixels;
	  
	  /* check pixels */
	  if(xStart-2<0) {
	    left=0;
	    xStart=0;
	  }
	  if(xEnd+2>wSlit->specEnd) {
	    right=0;
	    xEnd=wSlit->specEnd;
	  }
	  
	  if(interpolate) {
	    valStart=valEnd=0.;
	    /* simple linear interpolation */
	    if (left) {
	      for(j=xStart-2; j<=xStart; j++) 
		valStart += subMosImages[i]->data[imaX + j*subMosImages[i]->xlen];
	      valStart/=3;
	      value = valStart;
	      m = 0.;
	    }
	    if (right) {
	      for(j=xEnd+2; j>=xEnd; j--) 
		valEnd += subMosImages[i]->data[imaX + j*subMosImages[i]->xlen];
	      valEnd/=3;
	      value = valEnd;
	      m = 0.;
	    }

	    if(left && right) {
	      m = (valEnd-valStart)/((float)(xEnd)-(float)(xStart));
	      value = valStart;
	    }

	    for(imaY=xStart; imaY<=xEnd; imaY++)
	      subMosImages[i]->data[imaX + imaY*subMosImages[i]->xlen] = m*(imaY-xStart) + value;
	  }
	  else 
	    for(imaY=xStart; imaY<=xEnd; imaY++)
	      subMosImages[i]->data[imaX + imaY*subMosImages[i]->xlen] = -32000.;
	}
	object=object->next;
      }
      if (wSlit->next) wSlit = wSlit->next;
      else {
	while(wSlit->prev) wSlit = wSlit->prev; /* rewind wSlit */
	break;
      }
    }
  }
    
    
  /* compute mean sky image */
    
  meanSkyImage = frCombMedian(subMosImages,mosCount,1);
    

  /* Masking of blue wavelenght. FIXME - should be applicated? */
  
  /*
    readDoubleDescriptor(winTables[0]->descs, pilTrnGetKeyword("WlenStart"),&dValue, comment);
    wLenStart = (float) dValue;
    readDoubleDescriptor(winTables[0]->descs, pilTrnGetKeyword("WlenInc"),&dValue, comment);
    wLenInc = (float) dValue;
    startPixelSub = (startWLenSub - wLenStart) / wLenInc;
    
    for (i=0; i< meanSkyImage->xlen; i++)
    for (j=0; j< meanSkyImage->ylen; j++)
    if(i<startPixelSub-1) meanSkyImage->data[i+j*meanSkyImage->xlen] = 0.0;
  */
    


  if(!interpolate) {
    
    /* eventually interpolate in regions covered by the object in all 
       exposures that contains -32000 values even after the combination 
       FIXME - to be verified */

    badObjects=0;
    while(wSlit) {
      object = wSlit->objs;
      while(object) {
	for(imaX=0; imaX<meanSkyImage->xlen; imaX++){
	  startFound = 0;
	  if(!imaX) {
	    for(j=wSlit->specStart; j<=wSlit->specEnd; j++){
	      /* check if a -32000 region exist */
	      if(meanSkyImage->data[imaX + j*meanSkyImage->xlen] == -32000 && !startFound) {
		xStart=j-1;
		startFound=1;
	      }
	      if(meanSkyImage->data[imaX + j*meanSkyImage->xlen] != -32000 && startFound) { 
		xEnd=j;
		break;
	      }
	    }
	    if(startFound) badObjects++;
	  }  
	  
	  if(startFound) {
	    valStart = meanSkyImage->data[imaX + xStart*meanSkyImage->xlen];
	    valEnd = meanSkyImage->data[imaX + xEnd*meanSkyImage->xlen];
	    
	    /* again simple linear interpolation */
	    m = (valEnd-valStart)/((float)(xEnd)-(float)(xStart)); 
	    for(imaY=xStart+1; imaY<xEnd; imaY++)
	      meanSkyImage->data[imaX + imaY*meanSkyImage->xlen] = m*(imaY-xStart) + valStart;
	  }
	}
	object=object->next;  
      }
      wSlit=wSlit->next;
    }
    if(badObjects) cpl_msg_warning(task,"%d objects has been interpolated in central regions", badObjects);    
  }      
  
  /* finally subtract residual image from input images */
  for (i = 0; i < mosCount; i++)        
    imageArithLocal(mosImages[i], meanSkyImage, VM_OPER_SUB);
  
  /* cleaning */  
  for (j = 0; j < mosCount; j++)
    deleteImage(subMosImages[j]);
  pil_free(subMosImages);
  
  return EXIT_SUCCESS;
}