X-shooter Pipeline Reference Manual 3.8.15
xsh_utils_vector.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library 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: 2013-03-07 17:57:32 $
23 * $Revision: 1.2 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*----------------------------------------------------------------------------
32 Includes
33 ----------------------------------------------------------------------------*/
34#include "xsh_utils_vector.h"
35#include "xsh_utils_wrappers.h"
36#include "xsh_utils.h"
37#include "xsh_msg.h"
41/*
42 @brief extract subvector centered at a given element with a given half size
43 @param vin input data
44 @param pos center position
45 @param hsize hal vector size
46
47 @return extracted vector
48 * */
49
50cpl_vector*
51xsh_vector_extract_range(cpl_vector* vin,const cpl_size pos, const int hsize)
52{
53 cpl_vector* result;
54 int i=0;
55 int k=0;
56 int size_i=0;
57 int size_o=2*hsize+1;
58 int ref=(int)pos;
59 double* data_i=NULL;
60 double* data_o=NULL;
61
62 cpl_ensure(vin != NULL, CPL_ERROR_NULL_INPUT,NULL);
63 cpl_ensure(hsize > 0, CPL_ERROR_ILLEGAL_INPUT,NULL);
64 cpl_ensure(pos>hsize, CPL_ERROR_ILLEGAL_INPUT,NULL);
65 cpl_ensure(hsize > 0, CPL_ERROR_ILLEGAL_INPUT,NULL);
66 size_i=cpl_vector_get_size(vin);
67 cpl_ensure(pos+hsize < size_i, CPL_ERROR_ILLEGAL_INPUT,NULL);
68
69 result=cpl_vector_new(size_o);
70
71 data_i=cpl_vector_get_data(vin);
72 data_o=cpl_vector_get_data(result);
73
74 for(i=-hsize;i<=hsize;i++){
75 data_o[k]=data_i[ref+i];
76 k++;
77 }
78 return result;
79}
80/*
81 @brief generates a vector with factor times elements making local linear interpolation
82 @param vin data
83 @param factor upsampling factor
84
85 @return upsampled vector
86 * */
87cpl_vector*
88xsh_vector_upsample(cpl_vector* vin,const int factor)
89{
90 cpl_vector* result;
91 int i=0;
92 int j=0;
93 int size_i=0;
94 int size_o=0;
95 double* data_i=NULL;
96 double* data_o=NULL;
97 double y1=0;
98 double y2=0;
99 /*
100 double x1=0;
101 double x2=0;
102 */
103 double m=0;
104
105 cpl_ensure(vin != NULL, CPL_ERROR_NULL_INPUT,NULL);
106 cpl_ensure(factor > 0, CPL_ERROR_ILLEGAL_INPUT,NULL);
107
108 size_i=cpl_vector_get_size(vin);
109 size_o=(size_i-1)*factor+1;
110
111 result=cpl_vector_new(size_o);
112
113 data_i=cpl_vector_get_data(vin);
114 data_o=cpl_vector_get_data(result);
115 /* here we make a linear interpolation between data points */
116 for(i=0;i<size_i-1;i++){
117 y1=data_i[i];
118 y2=data_i[i+1];
119 //x1=i;
120 //x2=i+1;
121 m=(y2-y1)/factor;
122 for(j=0;j<factor;j++){
123 data_o[i*factor+j]= y1 + m * j;
124 }
125 }
126 data_o[size_o-1]=data_i[size_i-1];
127
128 return result;
129}
130
131/*
132 @brief generates a vector of values defined at each input wave pos that is a linear fit of values in range
133 (wave[0],wmin_max)---(wmax_min,wave[max])
134 @param vec_wave wave data
135 @param vec_flux flux data
136 @param wmin_max max value of start wave range up to which use data for fit of continuum
137 @param wmax_min min value of final wave range from which use data for fit of continuum
138 @return fit of continuum data vector
139 * */
140cpl_vector*
141xsh_vector_fit_slope(cpl_vector* vec_wave,cpl_vector* vec_flux,const double wmin_max,const double wmax_min,const int degree)
142{
143 cpl_vector* vec_slope=NULL;
144 int i=0;
145 int size=0;
146 int k=0;
147 double* pwave=NULL;
148 double* pflux=NULL;
149 double* pslope=NULL;
150 double* pvec_x=NULL;
151 double* pvec_y=NULL;
152 double mse=0;
153 cpl_polynomial* pfit=NULL;
154
155 cpl_vector* vec_x=NULL;
156 cpl_vector* vec_y=NULL;
157
158 /* input parameters consistency checks */
159 cpl_ensure(vec_wave,CPL_ERROR_NULL_INPUT,NULL);
160 cpl_ensure(vec_flux,CPL_ERROR_NULL_INPUT,NULL);
161 cpl_ensure(wmin_max<wmax_min,CPL_ERROR_INCOMPATIBLE_INPUT,NULL);
162 cpl_ensure(degree>0 && degree < 3,CPL_ERROR_INCOMPATIBLE_INPUT,NULL);
163
164 /* extract from vector values that are supposed to follow linear relation */
165 size=cpl_vector_get_size(vec_flux);
166 vec_x=cpl_vector_new(size);
167 vec_y=cpl_vector_new(size);
168 pwave=cpl_vector_get_data(vec_wave);
169 pflux=cpl_vector_get_data(vec_flux);
170 pvec_x=cpl_vector_get_data(vec_x);
171 pvec_y=cpl_vector_get_data(vec_y);
172
173 for(i=0;i<size;i++) {
174 if( pwave[i] <= wmin_max || pwave[i] >= wmax_min ) {
175 pvec_x[k]=pwave[i];
176 pvec_y[k]=pflux[i];
177 k++;
178 }
179 }
180
181 cpl_vector_set_size(vec_x,k);
182 cpl_vector_set_size(vec_y,k);
183
184 /* makes linear fit */
185 pfit=xsh_polynomial_fit_1d_create(vec_x,vec_y,degree,&mse);
186
187 /* define results */
188 vec_slope=cpl_vector_new(size);
189 pslope=cpl_vector_get_data(vec_slope);
190 for(i=0;i<size;i++) {
191 pslope[i] = cpl_polynomial_eval_1d( pfit, pwave[i], NULL);
192 }
193 xsh_free_vector(&vec_x);
194 xsh_free_vector(&vec_y);
195 xsh_free_polynomial(&pfit);
196 return vec_slope;
197}
198
199
static int degree
int size
DOUBLE ** ref
void xsh_free_polynomial(cpl_polynomial **p)
Deallocate a polynomial and set the pointer to NULL.
Definition: xsh_utils.c:2194
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
int m
Definition: xsh_detmon_lg.c:91
cpl_vector * xsh_vector_fit_slope(cpl_vector *vec_wave, cpl_vector *vec_flux, const double wmin_max, const double wmax_min, const int degree)
cpl_vector * xsh_vector_upsample(cpl_vector *vin, const int factor)
cpl_vector * xsh_vector_extract_range(cpl_vector *vin, const cpl_size pos, const int hsize)
cpl_polynomial * xsh_polynomial_fit_1d_create(const cpl_vector *x_pos, const cpl_vector *values, int degree, double *mse)