30#include "moo_psf_single.h"
72 moo_psf_single *res = cpl_calloc(1,
sizeof(moo_psf_single));
91 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
92 cpl_ensure(extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
96 res->extname = extname;
97 res->filename = filename;
118 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
119 if (self->header == NULL) {
120 self->header = cpl_propertylist_new();
122 cpl_propertylist_append_double(self->header, MOO_PFITS_CRPIX1, 1.0);
123 cpl_propertylist_append_string(self->header, MOO_PFITS_CTYPE1,
"LINEAR");
124 cpl_propertylist_append_double(self->header, MOO_PFITS_CRPIX2, crpix2);
125 cpl_propertylist_append_string(self->header, MOO_PFITS_CTYPE2,
"LINEAR");
126 cpl_propertylist_append_double(self->header, MOO_PFITS_CRVAL1, 1.0);
127 cpl_propertylist_append_double(self->header, MOO_PFITS_CRVAL2, 0.0);
128 cpl_propertylist_append_double(self->header, MOO_PFITS_CD1_1, 1.0);
129 cpl_propertylist_append_double(self->header, MOO_PFITS_CD1_2, 0.0);
130 cpl_propertylist_append_string(self->header, MOO_PFITS_CUNIT1,
131 MOO_PSF_SINGLE_CUNIT1);
132 cpl_propertylist_append_double(self->header, MOO_PFITS_CD2_1, 0.);
133 cpl_propertylist_append_double(self->header, MOO_PFITS_CD2_2, cd2_2);
135 return cpl_error_get_code();
154 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
155 cpl_ensure_code(crpix2 != NULL, CPL_ERROR_NULL_INPUT);
156 cpl_ensure_code(cd2_2 != NULL, CPL_ERROR_NULL_INPUT);
158 if (self->header == NULL) {
160 moo_fits_load_extension_header(self->filename,
161 MOO_PSF_SINGLE_PREFIX_EXTNAME,
167 return cpl_error_get_code();
171_moo_psf_single_reproject_model_fibre(cpl_image *image,
173 cpl_image *centroids,
180 int nx = cpl_image_get_size_x(image);
181 int ny = cpl_image_get_size_y(model);
183 double *data = cpl_image_get_data(result);
184 int *contrib_data = cpl_image_get_data(contrib);
186 for (
int x = 1; x <= nx; x++) {
188 double centroid = cpl_image_get(centroids, x, numfib, &rej);
189 if (!isnan(centroid)) {
190 for (
int y = 1; y <= ny; y++) {
191 double ypos = centroid + (y - crpix2) * cd2_2;
192 double flux = cpl_image_get(model, x, y, &rej);
193 int pix = (int)floor(ypos + 0.5);
194 data[(pix - 1) * nx + x - 1] += flux;
195 contrib_data[(pix - 1) * nx + x - 1] += 1;
202moo_psf_single_reproject_model(moo_psf_single *self,
208 cpl_image *result = NULL;
210 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
211 cpl_ensure(det != NULL, CPL_ERROR_NULL_INPUT, NULL);
212 cpl_ensure(loc != NULL, CPL_ERROR_NULL_INPUT, NULL);
213 cpl_ensure(health != NULL, CPL_ERROR_NULL_INPUT, NULL);
215 cpl_image *contrib = NULL;
217 cpl_errorstate prestate = cpl_errorstate_get();
221 cpl_image *image = hdrl_image_get_image(himage);
223 cpl_ensure(fcentroids != NULL, CPL_ERROR_NULL_INPUT, NULL);
224 cpl_ensure(himage != NULL, CPL_ERROR_NULL_INPUT, NULL);
226 int nx = hdrl_image_get_size_x(himage);
227 int ny = hdrl_image_get_size_y(himage);
228 int nb_fibres = cpl_image_get_size_y(fcentroids);
231 double crpix2, cd2_2;
233 cpl_msg_debug(__func__,
"Use nx %d ny %d crpix2 %f cd2_2 %f", nx, ny,
235 result = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
236 contrib = cpl_image_new(nx, ny, CPL_TYPE_INT);
246#if (__GNUC__ == 9) && (__GNUC_MINOR__ < 3)
247#pragma omp parallel shared(nb_fibres, health, cube, image, fcentroids, \
248 crpix2, cd2_2, result, contrib, indexes)
250#pragma omp parallel default(none) \
251 shared(nb_fibres, health, cube, image, fcentroids, crpix2, cd2_2, result, \
257 for (
int i = 1; i <= nb_fibres; i++) {
258 int idx = cpl_array_get_cplsize(indexes, i - 1, NULL);
260 cpl_msg_debug(__func__,
"reproject fibre %d", i);
263 cpl_image *model = cpl_imagelist_get(cube, i - 1);
264 _moo_psf_single_reproject_model_fibre(image, i, fcentroids,
265 model, crpix2, cd2_2,
272 double *data = cpl_image_get_data(result);
273 int *contrib_data = cpl_image_get_data(contrib);
275 for (
int i = 0; i < nx * ny; i++) {
276 if (contrib_data[i] != 0) {
277 data[i] /= contrib_data[i];
281 cpl_image_delete(contrib);
283 if (!cpl_errorstate_is_equal(prestate)) {
284 cpl_msg_error(__func__,
"Error in PSF file %s ext %s", self->filename,
286 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
287 cpl_image_delete(result);
290 cpl_errorstate_set(prestate);
309 cpl_error_code status = CPL_ERROR_NONE;
310 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
314 int nb_fibres = cpl_imagelist_get_size(cube);
315 double crpix2, cd2_2;
318#pragma omp parallel default(none) shared(nb_fibres, cube, cd2_2)
322 for (
int f = 1; f <= nb_fibres; f++) {
323 cpl_image *model = cpl_imagelist_get(cube, f - 1);
324 int nx = cpl_image_get_size_x(model);
325 int ny = cpl_image_get_size_y(model);
326 for (
int i = 1; i <= nx; i++) {
329 for (
int j = 1; j <= ny; j++) {
330 sum += cpl_image_get(model, i, j, &rej);
333 for (
int j = 1; j <= ny; j++) {
334 double val = cpl_image_get(model, i, j, &rej);
335 cpl_image_set(model, i, j, val / sum);
359 if (self->header != NULL) {
360 cpl_propertylist_delete(self->header);
362 if (self->cube != NULL) {
363 cpl_imagelist_delete(self->cube);
385 MOO_PSF_SINGLE_PREFIX_EXTNAME,
386 self->extname, CPL_TYPE_FLOAT,
407 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
408 cpl_ensure_code(stream != NULL, CPL_ERROR_NULL_INPUT);
410 fprintf(stream,
"---MOO_PSF_SINGLE\n");
411 fprintf(stream,
"filename %s extname %s\n", self->filename, self->extname);
413 return CPL_ERROR_NONE;
431 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
432 cpl_ensure(self->filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
433 cpl_ensure(self->extname != NULL, CPL_ERROR_NULL_INPUT, NULL);
435 if (self->cube == NULL) {
438 MOO_PSF_SINGLE_PREFIX_EXTNAME,
439 self->extname, CPL_TYPE_FLOAT);
cpl_imagelist * moo_fits_load_extension_cube(const char *filename, const char *name, const char *detectorname, cpl_type type)
Load a cube from FITS file.
cpl_error_code moo_fits_write_extension_cube(cpl_imagelist *cube, const char *filename, const char *name, const char *detectorname, cpl_type type, cpl_propertylist *header)
Write a cube as extension in FITS file.
cpl_image * moo_loc_single_get_f_centroids(moo_loc_single *self)
Get image of fit centroids.
cpl_error_code moo_psf_single_set_cube_ref(moo_psf_single *self, double crpix2, double cd2_2)
Set cube parameters.
void moo_psf_single_delete(moo_psf_single *self)
Delete a moo_psf_single.
cpl_error_code moo_psf_single_get_cube_ref(moo_psf_single *self, double *crpix2, double *cd2_2)
Set cube parameters.
moo_psf_single * moo_psf_single_new(void)
Create a new moo_psf_single
void moo_psf_single_save(const moo_psf_single *self, const char *filename)
Save a moo_psf_single to a FITS file.
cpl_error_code moo_psf_single_dump(const moo_psf_single *self, FILE *stream)
Dump structural information of PSF_SINGLE.
cpl_error_code moo_psf_single_normalize(moo_psf_single *self)
Normalize cube model profile.
cpl_imagelist * moo_psf_single_get_cube(moo_psf_single *self)
Get cube.
moo_psf_single * moo_psf_single_create(const char *filename, const char *extname)
Create a new moo_psf_single from the given PSF filename.
hdrl_image * moo_single_get_image(moo_single *self)
Get the IMAGE part (DATA,ERR) of single DET.
double moo_pfits_get_crpix2(const cpl_propertylist *plist)
find out the CRPIX2 value
double moo_pfits_get_cd2_2(const cpl_propertylist *plist)
find out the CD2_2 value