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 = 0;
00051 cxint k = 0;
00052 cxint first = 0;
00053 cxint last = 0;
00054 cxint nx = 0;
00055 cxint status = 0;
00056
00057 cxdouble wmin = 0.;
00058 cxdouble wmax = 0.;
00059 cxdouble wstep = 0.;
00060 cxdouble fstart = 0.;
00061 cxdouble fend = 0.;
00062
00063 cpl_propertylist *properties = giraffe_image_get_properties(spectrum);
00064
00065 cpl_image *_spectrum = giraffe_image_get(spectrum);
00066 cpl_image *_flux = NULL;
00067
00068 GiImage *flux = NULL;
00069
00070
00071 if (properties == NULL || _spectrum == NULL) {
00072 return NULL;
00073 }
00074
00075
00076 if (!cpl_propertylist_has(properties, GIALIAS_BINWLMIN)) {
00077 return NULL;
00078 }
00079
00080 wmin = cpl_propertylist_get_double(properties, GIALIAS_BINWLMIN);
00081
00082
00083 if (!cpl_propertylist_has(properties, GIALIAS_BINWLMAX)) {
00084 return NULL;
00085 }
00086
00087 wmax = cpl_propertylist_get_double(properties, GIALIAS_BINWLMAX);
00088
00089
00090 if (!cpl_propertylist_has(properties, GIALIAS_BINSTEP)) {
00091 return NULL;
00092 }
00093
00094 wstep = cpl_propertylist_get_double(properties, GIALIAS_BINSTEP);
00095
00096
00097
00098
00099
00100
00101
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 = (giraffe_range_get_max(limits) - wmin) / 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_new(nx, 1, CPL_TYPE_DOUBLE);
00129
00130 if (_flux == NULL) {
00131 return NULL;
00132 }
00133 else {
00134
00135 cxdouble *data = cpl_image_get_data(_spectrum);
00136 cxdouble *fx = cpl_image_get_data(_flux);
00137
00138 for (k = first; k < last; ++k) {
00139
00140 for (i = 0; i < nx; i++) {
00141 fx[i] += data[k * nx + i];
00142 }
00143
00144 }
00145
00146 }
00147
00148
00149
00150
00151
00152
00153
00154 if ((first - 1) >= 0) {
00155
00156 cxint j = (first - 1) * nx;
00157
00158 cxdouble *data = cpl_image_get_data(_spectrum);
00159 cxdouble *fx = cpl_image_get_data(_flux);
00160
00161
00162 for (i = 0; i < nx; i++) {
00163 fx[i] += data[j + i] * fstart;
00164 }
00165 }
00166
00167
00168 if ((last + 1 ) < cpl_image_get_size_y(_spectrum)) {
00169
00170 cxint j = last * nx;
00171
00172 cxdouble *data = cpl_image_get_data(_spectrum);
00173 cxdouble *fx = cpl_image_get_data(_flux);
00174
00175
00176 for (i = 0; i < nx; i++) {
00177 fx[i] += data[j + i] * fend;
00178 }
00179 }
00180
00181 flux = giraffe_image_new(CPL_TYPE_DOUBLE);
00182
00183 status = giraffe_image_set(flux, _flux);
00184 cpl_image_delete(_flux);
00185
00186 if (status != 0) {
00187 giraffe_image_delete(flux);
00188 return NULL;
00189 }
00190
00191 status = giraffe_image_set_properties(flux, properties);
00192
00193 if (status != 0) {
00194 giraffe_image_delete(flux);
00195 return NULL;
00196 }
00197
00198 return flux;
00199
00200 }