gisutils.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 # include <config.h>
00030 #endif
00031
00032 #include <math.h>
00033
00034 #include "gialias.h"
00035 #include "gisutils.h"
00036
00037
00046 GiImage *
00047 giraffe_integrate_flux(GiImage *spectrum, GiRange *limits)
00048 {
00049
00050 cxint i;
00051 cxint first;
00052 cxint last;
00053 cxint nx;
00054 cxint status = 0;
00055
00056 cxdouble wmin;
00057 cxdouble wmax;
00058 cxdouble wstep;
00059 cxdouble fstart;
00060 cxdouble fend;
00061
00062 cpl_propertylist *properties = giraffe_image_get_properties(spectrum);
00063
00064 cpl_image *_spectrum = giraffe_image_get(spectrum);
00065 cpl_image *_flux = NULL;
00066
00067 GiImage *flux = NULL;
00068
00069
00070 if (properties == NULL || _spectrum == NULL) {
00071 return NULL;
00072 }
00073
00074
00075 if (!cpl_propertylist_has(properties, GIALIAS_BINWLMIN)) {
00076 return NULL;
00077 }
00078
00079 wmin = cpl_propertylist_get_double(properties, GIALIAS_BINWLMIN);
00080
00081
00082 if (!cpl_propertylist_has(properties, GIALIAS_BINWLMAX)) {
00083 return NULL;
00084 }
00085
00086 wmax = cpl_propertylist_get_double(properties, GIALIAS_BINWLMAX);
00087
00088
00089 if (!cpl_propertylist_has(properties, GIALIAS_BINSTEP)) {
00090 return NULL;
00091 }
00092
00093 wstep = cpl_propertylist_get_double(properties, GIALIAS_BINSTEP);
00094
00095
00096
00097
00098
00099
00100
00101 first = 0;
00102 last = cpl_image_get_size_y(_spectrum) - 1;
00103
00104 if (giraffe_range_get_min(limits) > wmin) {
00105
00106 cxdouble pixel = (giraffe_range_get_min(limits) - wmin) / wstep;
00107
00108 first += ceil(pixel);
00109 fstart = pixel - first;
00110 }
00111
00112 if (giraffe_range_get_max(limits) < wmax) {
00113
00114 cxdouble pixel = last - (wmax - giraffe_range_get_max(limits)) / wstep;
00115
00116 last = floor(pixel);
00117 fend = pixel - last;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 nx = cpl_image_get_size_x(_spectrum);
00127
00128 _flux = cpl_image_collapse_window_create(_spectrum, 1, first + 1, nx,
00129 last + 1, 0);
00130
00131 if (_flux == NULL) {
00132 return NULL;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 if ((first - 1) >= 0) {
00142
00143 cxint j = (first - 1) * nx;
00144
00145 cxdouble *data = cpl_image_get_data(_spectrum);
00146 cxdouble *fx = cpl_image_get_data(_flux);
00147
00148
00149 for (i = 0; i < nx; i++) {
00150 fx[i] += data[j + i] * fstart;
00151 }
00152 }
00153
00154
00155 if ((last + 1 ) < cpl_image_get_size_y(_spectrum)) {
00156
00157 cxint j = (last + 1) * nx;
00158
00159 cxdouble *data = cpl_image_get_data(_spectrum);
00160 cxdouble *fx = cpl_image_get_data(_flux);
00161
00162
00163 for (i = 0; i < nx; i++) {
00164 fx[i] += data[j + i] * fend;
00165 }
00166 }
00167
00168 flux = giraffe_image_new(CPL_TYPE_DOUBLE);
00169
00170 status = giraffe_image_set(flux, _flux);
00171 cpl_image_delete(_flux);
00172
00173 if (status != 0) {
00174 giraffe_image_delete(flux);
00175 return NULL;
00176 }
00177
00178 status = giraffe_image_set_properties(flux, properties);
00179
00180 if (status != 0) {
00181 giraffe_image_delete(flux);
00182 return NULL;
00183 }
00184
00185 return flux;
00186
00187 }