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"
46 double parallactic_shiftX;
47 double parallactic_shiftY;
49 typedef struct _disp_data disp_data;
50 static double sinfo_disp_calc_N(
const disp_data* pdata,
double lambda);
52 sinfo_disp_prepare_data(disp_data* pdata,
62 sinfo_disp_calc(disp_data* pdata,
double lambda,
double *shiftx,
double *shiftY);
64 sinfo_atmo_rotate_point(
double* x_value,
double * y_value,
double rot_angle);
68 sinfo_disp_calc_N(
const disp_data* pdata,
double lambda)
70 double s = 1.0 /lambda;
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));
85 sinfo_disp_prepare_data(disp_data* pdata,
94 double ps,p2,p1,T,T2,T3;
98 T = Tc + 273.15;T2 = T * T; T3 = T2 * T;
99 ps = -10474 + (116.43 * T) - (0.43284 *T2) + (0.00053840 * T3);
102 pdata->d1 = (p1/T)*(1+p1*( (57.90e-8) - ((9.3250e-4)/T) + (0.25844/T2)));
103 pdata->d2 = (p2/T)*(1+p2*(1+3.7e-4*p2)*( (-2.37321e-3) + (2.23366/T) - (710.792/T2) + ((7.75141e-4)/T3) )) ;
104 pdata->N0 = sinfo_disp_calc_N(pdata, lambda0);
105 zenith = acos(1/airm);
106 pdata->predelta = ((tan(zenith)) / (PI_NUMB/180)) * 3600;
107 pdata->parallactic_shiftX = sin ( (parallactic)* (PI_NUMB/180) ) / pixelscale;
108 pdata->parallactic_shiftY = cos ( (parallactic)* (PI_NUMB/180) ) / pixelscale;
110 pdata->N0,pdata->d1, pdata->d2, pdata->parallactic_shiftX ,
111 pdata->parallactic_shiftY);
114 sinfo_disp_calc(disp_data* pdata,
double lambda,
double *shiftx,
double *shifty)
116 double n = sinfo_disp_calc_N(pdata, lambda);
117 double delta = pdata->predelta * (n - pdata->N0);
118 *shiftx = -delta * pdata->parallactic_shiftX;
119 *shifty = delta * pdata->parallactic_shiftY ;
123 sinfo_atm_dispersion_cube(cpl_imagelist* pCube,
135 cpl_error_code err = CPL_ERROR_NONE;
136 int cubesize = cpl_imagelist_get_size(pCube);
137 double * kernel = sinfo_generate_interpolation_kernel(
"default");
141 sinfo_disp_prepare_data(&ddata, centlambda, Tc, Rh, airm, p, parallactic, pixelscale);
143 for (i = 0; i < cubesize; i++)
148 cpl_image* pnewImage = 0;
150 cpl_image* plane = cpl_imagelist_get(pCube, i);
153 double lambda = centlambda - (centpix - i) * pixelsz;
154 sinfo_disp_calc(&ddata, lambda, &shiftx, &shifty);
160 pnewImage = sinfo_new_shift_image(
165 err = cpl_imagelist_set(pCube, pnewImage, i);
166 if (err != CPL_ERROR_NONE)
179 sinfo_atmo_load_polynom(
const char* filename)
181 const char* COL_NAME_TEMPLATE =
"col_%d";
182 const char* COL_NAME_VALUE =
"value";
183 cpl_polynomial* poly = NULL;
184 cpl_table* ptable = NULL;
186 ptable = cpl_table_load(filename, 1, 0);
192 cpl_size* expo = NULL;
194 dim = cpl_table_get_ncol(ptable) - 1;
195 poly = cpl_polynomial_new(dim );
196 nrows = cpl_table_get_nrow(ptable);
197 expo = cpl_malloc(dim *
sizeof(expo[0]));
198 memset(&expo[0], 0, dim *
sizeof(expo[0]));
199 for (i = 0; i < nrows; i++)
204 for (j = 0; j < dim; j++)
207 sprintf(col_name, COL_NAME_TEMPLATE, j);
208 expo[j] = cpl_table_get_int(ptable, col_name, i, &inull);
210 value = cpl_table_get(ptable, COL_NAME_VALUE, i, &inull);
211 cpl_polynomial_set_coeff(poly, expo, value);
212 if (cpl_error_get_code() != CPL_ERROR_NONE)
216 sinfo_free_polynomial(&poly);
223 sinfo_free_table(&ptable);
228 sinfo_atmo_rotate_point(
double* x_value,
double * y_value,
double rot_angle)
230 double newx = *x_value * cos(rot_angle) - *y_value * sin(rot_angle);
231 double newy = *x_value * sin(rot_angle) + *y_value * cos(rot_angle);
237 sinfo_atmo_apply_cube_polynomial_shift(
238 cpl_polynomial* poly,
239 cpl_imagelist* pCube,
246 cpl_imagelist* retcube = NULL;
247 cpl_error_code err = CPL_ERROR_NONE;
248 cpl_vector* vparams = NULL;
249 double * kernel = sinfo_generate_interpolation_kernel(
"default");
254 double l0_shift_x = 0;
255 double l0_shift_y = 0;
257 vparams = cpl_vector_new(2);
258 cpl_vector_set(vparams, 0, airmass);
259 cpl_vector_set(vparams, 1, lambda0);
260 err = cpl_error_get_code();
261 if (err == CPL_ERROR_NONE)
263 l0_shift_y = cpl_polynomial_eval(poly, vparams);
266 sinfo_atmo_rotate_point(&l0_shift_x, &l0_shift_y, parallactic);
267 cubesize = cpl_imagelist_get_size(pCube);
268 err = cpl_error_get_code();
270 if (err == CPL_ERROR_NONE)
272 retcube = cpl_imagelist_new();
273 for (i = 0; i < cubesize; i++)
276 double lambda = lambda0 - (centpix - i) * pixelsz;
280 cpl_vector_set(vparams, 1, lambda);
282 shift_y = cpl_polynomial_eval(poly, vparams);
283 err = cpl_error_get_code();
284 if (err == CPL_ERROR_NONE)
286 double res_shift_x = -(shift_x - l0_shift_x);
287 double res_shift_y = -(shift_y - l0_shift_y);
288 cpl_image* plane = NULL;
289 cpl_image* pimresult = NULL;
291 sinfo_atmo_rotate_point(&res_shift_x, &res_shift_y, parallactic);
292 plane = cpl_imagelist_get(pCube, i);
293 pimresult = sinfo_new_shift_image(
298 if (err == CPL_ERROR_NONE)
300 err = cpl_imagelist_set(retcube, pimresult, i);
305 cpl_error_get_where());
307 if (err != CPL_ERROR_NONE)
313 cpl_error_get_where());
315 if (err != CPL_ERROR_NONE)
319 if (err != CPL_ERROR_NONE)
321 sinfo_free_imagelist(&retcube);
323 cpl_error_get_where());
325 sinfoni_free_vector(&vparams);