GIRAFFE Pipeline Reference Manual

gisutils.c
1 /*
2  * This file is part of the GIRAFFE Pipeline
3  * Copyright (C) 2002-2019 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, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 
24 #include <math.h>
25 
26 #include "gialias.h"
27 #include "gisutils.h"
28 
29 
38 GiImage *
39 giraffe_integrate_flux(GiImage *spectrum, GiRange *limits)
40 {
41 
42  cxint i = 0;
43  cxint k = 0;
44  cxint first = 0;
45  cxint last = 0;
46  cxint nx = 0;
47  cxint status = 0;
48 
49  cxdouble wmin = 0.;
50  cxdouble wmax = 0.;
51  cxdouble wstep = 0.;
52  cxdouble fstart = 0.;
53  cxdouble fend = 0.;
54 
55  cpl_propertylist *properties = giraffe_image_get_properties(spectrum);
56 
57  cpl_image *_spectrum = giraffe_image_get(spectrum);
58  cpl_image *_flux = NULL;
59 
60  GiImage *flux = NULL;
61 
62 
63  if (properties == NULL || _spectrum == NULL) {
64  return NULL;
65  }
66 
67 
68  if (!cpl_propertylist_has(properties, GIALIAS_BINWLMIN)) {
69  return NULL;
70  }
71 
72  wmin = cpl_propertylist_get_double(properties, GIALIAS_BINWLMIN);
73 
74 
75  if (!cpl_propertylist_has(properties, GIALIAS_BINWLMAX)) {
76  return NULL;
77  }
78 
79  wmax = cpl_propertylist_get_double(properties, GIALIAS_BINWLMAX);
80 
81 
82  if (!cpl_propertylist_has(properties, GIALIAS_BINSTEP)) {
83  return NULL;
84  }
85 
86  wstep = cpl_propertylist_get_double(properties, GIALIAS_BINSTEP);
87 
88 
89  /*
90  * Determine the pixel limits for the integration from the
91  * given wavelength range.
92  */
93 
94  last = cpl_image_get_size_y(_spectrum) - 1;
95 
96  if (giraffe_range_get_min(limits) > wmin) {
97 
98  cxdouble pixel = (giraffe_range_get_min(limits) - wmin) / wstep;
99 
100  first = ceil(pixel);
101  fstart = pixel - first;
102  }
103 
104  if (giraffe_range_get_max(limits) < wmax) {
105 
106  cxdouble pixel = (giraffe_range_get_max(limits) - wmin) / wstep;
107 
108  last = floor(pixel);
109  fend = pixel - last;
110  }
111 
112 
113  /*
114  * Sum fluxes along the dispersion direction (image y-axis) for
115  * the defined window.
116  */
117 
118  nx = cpl_image_get_size_x(_spectrum);
119 
120  _flux = cpl_image_new(nx, 1, CPL_TYPE_DOUBLE);
121 
122  if (_flux == NULL) {
123  return NULL;
124  }
125  else {
126 
127  cxdouble *data = cpl_image_get_data(_spectrum);
128  cxdouble *fx = cpl_image_get_data(_flux);
129 
130  for (k = first; k < last; ++k) {
131 
132  for (i = 0; i < nx; i++) {
133  fx[i] += data[k * nx + i];
134  }
135 
136  }
137 
138  }
139 
140 
141  /*
142  * Add fluxes for the pixel fractions at the beginning and the end of
143  * the image window.
144  */
145 
146  if ((first - 1) >= 0) {
147 
148  cxint j = (first - 1) * nx;
149 
150  cxdouble *data = cpl_image_get_data(_spectrum);
151  cxdouble *fx = cpl_image_get_data(_flux);
152 
153 
154  for (i = 0; i < nx; i++) {
155  fx[i] += data[j + i] * fstart;
156  }
157  }
158 
159 
160  if ((last + 1 ) < cpl_image_get_size_y(_spectrum)) {
161 
162  cxint j = last * nx;
163 
164  cxdouble *data = cpl_image_get_data(_spectrum);
165  cxdouble *fx = cpl_image_get_data(_flux);
166 
167 
168  for (i = 0; i < nx; i++) {
169  fx[i] += data[j + i] * fend;
170  }
171  }
172 
173  flux = giraffe_image_new(CPL_TYPE_DOUBLE);
174 
175  status = giraffe_image_set(flux, _flux);
176  cpl_image_delete(_flux);
177 
178  if (status != 0) {
179  giraffe_image_delete(flux);
180  return NULL;
181  }
182 
183  status = giraffe_image_set_properties(flux, properties);
184 
185  if (status != 0) {
186  giraffe_image_delete(flux);
187  return NULL;
188  }
189 
190  return flux;
191 
192 }
void giraffe_image_delete(GiImage *self)
Destroys an image.
Definition: giimage.c:181
cpl_propertylist * giraffe_image_get_properties(const GiImage *self)
Get the properties of an image.
Definition: giimage.c:282
cpl_image * giraffe_image_get(const GiImage *self)
Gets the image data.
Definition: giimage.c:218
GiImage * giraffe_image_new(cpl_type type)
Creates an empty image container.
Definition: giimage.c:65
cxint giraffe_image_set(GiImage *self, cpl_image *image)
Sets the image data.
Definition: giimage.c:244
cxint giraffe_image_set_properties(GiImage *self, cpl_propertylist *properties)
Attaches a property list to an image.
Definition: giimage.c:312
cxdouble giraffe_range_get_min(const GiRange *const self)
Get the minimum of a range.
Definition: girange.c:167
cxdouble giraffe_range_get_max(const GiRange *const self)
Get the maximum of a range.
Definition: girange.c:213

This file is part of the GIRAFFE Pipeline Reference Manual 2.16.10.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Thu Dec 15 2022 21:18:51 by doxygen 1.9.1 written by Dimitri van Heesch, © 1997-2004