SINFONI Pipeline Reference Manual  2.6.0
sinfo_atmo_disp.c
1 /*
2  * This file is part of the ESO SINFONI Pipeline
3  * Copyright (C) 2004-2009 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 
20 /*
21  * $Author: amodigli $
22  * $Date: 2011-12-08 16:15:40 $
23  * $Revision: 1.7 $
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.h> /* allows the program compilation */
28 #endif
29 
30 #include <cpl.h>
31 #include <string.h>
32 #include <math.h>
33 #include <sinfo_msg.h>
34 #include <sinfo_utils_wrappers.h>
35 #include "sinfo_atmo_disp.h"
36 #include "sinfo_resampling.h"
37 #include "sinfo_image_ops.h"
38 
39 struct _disp_data
40 {
41  double p1;
42  double d1;
43  double d2;
44  double N0;
45  double predelta;
46  double parallactic_shiftX;
47  double parallactic_shiftY;
48 };
49 typedef struct _disp_data disp_data;
50 static double sinfo_disp_calc_N(const disp_data* pdata, double lambda);
51 static void
52 sinfo_disp_prepare_data(disp_data* pdata,
53  double lambda0,
54  double Tc,
55  double rh,
56  double airm,
57  double p,
58  double parallactic,
59  double pixelscale );
60 
61 static void
62 sinfo_disp_calc(disp_data* pdata, double lambda, double *shiftx, double *shiftY);
63 static void
64 sinfo_atmo_rotate_point(double* x_value, double * y_value, double rot_angle);
65 /*------------------------------------------------------------------------------------*/
66 
67 static double
68 sinfo_disp_calc_N(const disp_data* pdata, double lambda)
69 {
70  double s = 1.0 /lambda;
71  double s2 = s * s;
72  double s3 = s2 * s;
73  double s4 = s3 * s;
74  double s5 = s4 * s;
75  double s6 = s5 * s;
76 
77  double a = 83939.7/(130 - s2);
78  double b = 4547.3/(38.99 - s2);
79  double c = 6487.31 + 58.058*s2 - 0.71150*s4 + 0.08851*s6;
80  double N = 1e-8 * ( ((2371.34 + a + b) * pdata->d1) + ( c * pdata->d2));
81  return N;
82 
83 }
84 
85 static void
86 sinfo_disp_prepare_data(disp_data* pdata,
87  double lambda0,
88  double Tc,
89  double rh,
90  double airm,
91  double p,
92  double parallactic,
93  double pixelscale )
94 {
95  double ps,p2,p1,T,T2,T3;
96  double zenith;
97  // const double PI_NUMBer = 3.1415926535;
98 
99  T = Tc + 273.15;T2 = T * T; T3 = T2 * T;
100  ps = -10474 + (116.43 * T) - (0.43284 *T2) + (0.00053840 * T3);
101  p2 = (rh/100)*ps;
102  p1 = p - p2;
103  pdata->d1 = (p1/T)*(1+p1*( (57.90e-8) - ((9.3250e-4)/T) + (0.25844/T2)));
104  pdata->d2 = (p2/T)*(1+p2*(1+3.7e-4*p2)*( (-2.37321e-3) + (2.23366/T) - (710.792/T2) + ((7.75141e-4)/T3) )) ;
105  pdata->N0 = sinfo_disp_calc_N(pdata, lambda0);
106  zenith = acos(1/airm);
107  pdata->predelta = ((tan(zenith)) / (PI_NUMB/180)) * 3600;
108  pdata->parallactic_shiftX = sin ( (parallactic)* (PI_NUMB/180) ) / pixelscale;
109  pdata->parallactic_shiftY = cos ( (parallactic)* (PI_NUMB/180) ) / pixelscale;
110  sinfo_msg_warning("atm disp N0[%f] d1[%f] d2[%f] pshiftX[%f] pshiftY[%f]",
111  pdata->N0,pdata->d1, pdata->d2, pdata->parallactic_shiftX ,
112  pdata->parallactic_shiftY);
113 }
114 
115 static void
116 sinfo_disp_calc(disp_data* pdata, double lambda, double *shiftx, double *shifty)
117 {
118  double n = sinfo_disp_calc_N(pdata, lambda);
119  double delta = pdata->predelta * (n - pdata->N0);
120  *shiftx = -delta * pdata->parallactic_shiftX;
121  *shifty = delta * pdata->parallactic_shiftY ;
122 }
123 
124 static void
125 sinfo_atmo_rotate_point(double* x_value, double * y_value, double rot_angle)
126 {
127  double newx = *x_value * cos(rot_angle) - *y_value * sin(rot_angle);
128  double newy = *x_value * sin(rot_angle) + *y_value * cos(rot_angle);
129  *x_value = newx;
130  *y_value = newy;
131 }
153 cpl_error_code
154 sinfo_atm_dispersion_cube(cpl_imagelist* pCube,
155  int centpix,
156  double centlambda,
157  double Tc,
158  double Rh,
159  double airm,
160  double p,
161  double parallactic,
162  double pixelscale,
163  double pixelsz
164 )
165 {
166  cpl_error_code err = CPL_ERROR_NONE;
167  int cubesize = cpl_imagelist_get_size(pCube);
168  double * kernel = sinfo_generate_interpolation_kernel("default");
169  disp_data ddata;
170  int i = 0;
171 
172  sinfo_disp_prepare_data(&ddata, centlambda, Tc, Rh, airm, p, parallactic, pixelscale);
173 
174  for (i = 0; i < cubesize; i++)
175  {
176  double shiftx = 0;
177  double shifty = 0;
178 
179  cpl_image* pnewImage = 0;
180  // 1. get an image
181  cpl_image* plane = cpl_imagelist_get(pCube, i);
182 
183  // 2. calculate dispersion and shift
184  double lambda = centlambda - (centpix - i) * pixelsz;
185  sinfo_disp_calc(&ddata, lambda, &shiftx, &shifty);
186  // 3. aplly shift
187  // int szx = cpl_image_get_size_x(plane);
188  //int szy = cpl_image_get_size_y(plane);
189  //if (i % 10 == 0)
190  sinfo_msg_warning(" shift image #%d, dx[%f] dy[%f]", i, shiftx, shifty);
191  pnewImage = sinfo_new_shift_image(
192  plane,
193  shiftx,
194  shifty,
195  kernel);
196  err = cpl_imagelist_set(pCube, pnewImage, i);
197  if (err != CPL_ERROR_NONE)
198  break;
199  }
200  cpl_free(kernel);
201  return err;
202 
203 }
204 
215 cpl_polynomial*
216 sinfo_atmo_load_polynom(const char* filename)
217 {
218 
219  cpl_polynomial* poly = NULL;
220  cpl_table* ptable = NULL;
221 
222  ptable = cpl_table_load(filename, 1, 0);
223  if (ptable)
224  {
225  int dim = 0;
226  int nrows = 0;
227  int i = 0;
228  cpl_size* expo = NULL;
229 
230  dim = cpl_table_get_ncol(ptable) - 1;
231  poly = cpl_polynomial_new(dim );
232  nrows = cpl_table_get_nrow(ptable);
233  expo = cpl_malloc(dim * sizeof(expo[0]));
234  memset(&expo[0], 0, dim * sizeof(expo[0]));
235  const char* COL_NAME_TEMPLATE = "col_%d";
236  const char* COL_NAME_VALUE = "value";
237  for (i = 0; i < nrows; i++)
238  {
239  int j = 0;
240  int inull = 0;
241  double value = 0;
242  for (j = 0; j < dim; j++)
243  {
244  char col_name[255];
245  sprintf(col_name, COL_NAME_TEMPLATE, j);
246  expo[j] = cpl_table_get_int(ptable, col_name, i, &inull);
247  }
248  value = cpl_table_get(ptable, COL_NAME_VALUE, i, &inull);
249  cpl_polynomial_set_coeff(poly, expo, value);
250  if (cpl_error_get_code() != CPL_ERROR_NONE)
251  {
252  if (poly)
253  {
254  sinfo_free_polynomial(&poly);
255  }
256  break;
257  }
258  }
259  cpl_free(expo);
260  }
261  sinfo_free_table(&ptable);
262  return poly;
263 }
264 
282 cpl_imagelist*
283 sinfo_atmo_apply_cube_polynomial_shift(
284  cpl_polynomial* poly,
285  cpl_imagelist* pCube,
286  double lambda0,
287  double airmass,
288  double parallactic, // should be in radian
289  double pixelsz,
290  int centpix)
291 {
292  cpl_imagelist* retcube = NULL;
293 
294  cpl_vector* vparams = NULL;
295  double * kernel = sinfo_generate_interpolation_kernel("default");
296  int cubesize = 0;
297 
298  // the following two parameters are necessary for computing the shift
299  // in case when polynom for H+K band is used for H or K
300  double l0_shift_x = 0; // shift for the central point by X
301  double l0_shift_y = 0; // shift for the central point by Y
302 
303  vparams = cpl_vector_new(2);
304  cpl_vector_set(vparams, 0, airmass);
305  cpl_vector_set(vparams, 1, lambda0);
306 
307  if (CPL_ERROR_NONE == cpl_error_get_code())
308  {
309 
310  l0_shift_y = cpl_polynomial_eval(poly, vparams); // North - South
311  l0_shift_x = 0; // (EAST-WEST direction)
312  // rotate the shift
313  sinfo_atmo_rotate_point(&l0_shift_x, &l0_shift_y, parallactic);
314  cubesize = cpl_imagelist_get_size(pCube);
315 
316  }
317  if (CPL_ERROR_NONE == cpl_error_get_code())
318  {
319  retcube = cpl_imagelist_new();
320  for (int i = 0; i < cubesize; i++)
321  {
322  // calculate the wavelength
323  double lambda = lambda0 - (centpix - i) * pixelsz;
324 
325 
326  cpl_vector_set(vparams, 1, lambda);
327  // calc the shift
328 
329  double shift_y = cpl_polynomial_eval(poly, vparams); // North - South
330  double shift_x = 0;
331  if (CPL_ERROR_NONE == cpl_error_get_code())
332  {
333  double res_shift_x = -(shift_x - l0_shift_x);
334  double res_shift_y = -(shift_y - l0_shift_y);
335  cpl_image* plane = NULL;
336  cpl_image* pimresult = NULL;
337  // rotate the shift
338  sinfo_atmo_rotate_point(&res_shift_x, &res_shift_y, parallactic);
339  plane = cpl_imagelist_get(pCube, i);
340  pimresult = sinfo_new_shift_image(
341  plane,
342  res_shift_x, // x shift
343  res_shift_y, // y shift
344  kernel);
345  if (CPL_ERROR_NONE == cpl_error_get_code())
346  {
347  cpl_imagelist_set(retcube, pimresult, i);
348  }
349  else
350  {
351  sinfo_msg_error("Error sinfo_new_shift_image, %s",
352  cpl_error_get_where());
353  }
354  if (CPL_ERROR_NONE != cpl_error_get_code())
355  break;
356  }
357  else
358  {
359  sinfo_msg_error("Error polynomial_eval, %s",
360  cpl_error_get_where());
361  }
362  if (CPL_ERROR_NONE != cpl_error_get_code())
363  break;
364  }
365  }
366  if (CPL_ERROR_NONE != cpl_error_get_code())
367  {
368  sinfo_free_imagelist(&retcube);
369  sinfo_msg_error("Error during shift planes in the cube, %s",
370  cpl_error_get_where());
371  }
372  sinfoni_free_vector(&vparams);
373  cpl_free(kernel);
374  return retcube;
375 }
#define sinfo_msg_error(...)
Print an error message.
Definition: sinfo_msg.h:69
#define sinfo_msg_warning(...)
Print an warning message.
Definition: sinfo_msg.h:93