68 moo_cube *res = cpl_calloc(1,
sizeof(moo_cube));
74moo_cube_load(
const cpl_frame *frame)
76 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
77 const char *filename = cpl_frame_get_filename(frame);
78 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT, NULL);
80 cpl_errorstate prev_state = cpl_errorstate_get();
83 for (
int i = 0; i < 2; i++) {
84 for (
int j = 0; j < 3; j++) {
86 cpl_propertylist *header = NULL;
87 header = moo_fits_load_extension_header(filename, NULL, extname);
88 res->data_header[i * 3 + j] = header;
92 res->data[i * 3 + j] =
99 if (!cpl_errorstate_is_equal(prev_state)) {
123 cpl_propertylist *header)
125 cpl_ensure_code(ntas >= 1 && ntas <= 2, CPL_ERROR_ILLEGAL_INPUT);
126 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
127 cpl_ensure_code(header != NULL, CPL_ERROR_NULL_INPUT);
129 self->data[(ntas - 1) * 3 + type] = data;
130 self->data_header[(ntas - 1) * 3 + type] = header;
132 return CPL_ERROR_NONE;
152 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
153 cpl_ensure_code(self->filename != NULL, CPL_ERROR_NULL_INPUT);
154 cpl_error_code status = CPL_ERROR_NONE;
155 cpl_errorstate prev_state = cpl_errorstate_get();
157 cpl_propertylist *header = cpl_propertylist_new();
158 cpl_propertylist_append_string(header, MOO_PFITS_EXTNAME, extname);
161 cpl_imagelist_save(list, self->filename, CPL_TYPE_FLOAT, header,
165 cpl_propertylist_save(header, self->filename, CPL_IO_EXTEND);
169 if (!cpl_errorstate_is_equal(prev_state)) {
170 cpl_msg_error(
"moo_cube",
"Error for adding data to %s %s (%s)",
171 self->filename, extname,
172 cpl_error_get_message_default(cpl_error_get_code()));
173 cpl_msg_info(
"test",
"test debug cube");
174 status = cpl_error_get_code();
194 cpl_propertylist_save(self->primary_header, filename, CPL_IO_CREATE);
195 for (
int i = 0; i < 2; i++) {
196 for (
int j = 0; j < 3; j++) {
199 NULL, extname, CPL_TYPE_FLOAT,
200 self->data_header[i * 3 + j]);
208_moo_coefs_normalise(cpl_imagelist *coefs, cpl_image *flux, cpl_image *exptime)
210 if (coefs != NULL && flux != NULL && exptime != NULL) {
211 int size = cpl_imagelist_get_size(coefs);
213 for (
int k = 0; k < size; k++) {
214 cpl_image *coef = cpl_imagelist_get(coefs, k);
215 cpl_image_divide(coef, flux);
219 for (
int k = 1; k < size; k++) {
220 cpl_image *coef = cpl_imagelist_get(coefs, k);
221 cpl_image *power = cpl_image_power_create(exptime, k);
222 cpl_image_multiply(coef, power);
223 cpl_image_delete(power);
226 return CPL_ERROR_NONE;
240 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
241 cpl_ensure_code(map != NULL, CPL_ERROR_NULL_INPUT);
242 for (
int i = 0; i < 6; i++) {
243 _moo_coefs_normalise(self->data[i], map->flux[i], map->exptime[i]);
245 return CPL_ERROR_NONE;
250static cpl_imagelist *
251_moo_cube_merge(cpl_imagelist *coefsa,
253 cpl_imagelist *coefsb,
256 cpl_imagelist *result = NULL;
258 if (coefsa != NULL && coefsb != NULL) {
259 int size = cpl_imagelist_get_size(coefsa);
260 result = cpl_imagelist_duplicate(coefsa);
261 cpl_image *coefa = cpl_imagelist_get(coefsa, 0);
262 cpl_image *coefb = cpl_imagelist_get(coefsb, 0);
263 int nx = cpl_image_get_size_x(coefa);
264 int ny = cpl_image_get_size_y(coefa);
265 for (
int j = 1; j <= ny; j++) {
266 for (
int i = 1; i <= nx; i++) {
267 int reja = cpl_image_is_rejected(coefa, i, j);
268 int rejb = cpl_image_is_rejected(coefb, i, j);
271 for (
int k = 0; k < size; k++) {
273 cpl_image *ib = cpl_imagelist_get(coefsb, k);
274 cpl_image *rb = cpl_imagelist_get(result, k);
275 double v = cpl_image_get(ib, i, j, &rej);
276 cpl_image_set(rb, i, j, v);
283 double fa = cpl_image_get(fluxa, i, j, &rej);
284 double fb = cpl_image_get(fluxb, i, j, &rej);
286 for (
int k = 0; k < size; k++) {
287 cpl_image *ib = cpl_imagelist_get(coefsb, k);
288 cpl_image *rb = cpl_imagelist_get(result, k);
289 double v = cpl_image_get(ib, i, j, &rej);
290 cpl_image_set(rb, i, j, v);
313 moo_saturate_map *saturatea,
315 moo_saturate_map *saturateb)
317 moo_cube *result = NULL;
318 cpl_ensure(cubea != NULL, CPL_ERROR_NULL_INPUT, NULL);
319 cpl_ensure(saturatea != NULL, CPL_ERROR_NULL_INPUT, NULL);
320 cpl_ensure(cubeb != NULL, CPL_ERROR_NULL_INPUT, NULL);
321 cpl_ensure(saturateb != NULL, CPL_ERROR_NULL_INPUT, NULL);
324 result->primary_header = cpl_propertylist_duplicate(cubea->primary_header);
325 for (
int i = 0; i < 6; i++) {
326 result->data[i] = _moo_cube_merge(cubea->data[i], saturatea->flux[i],
327 cubeb->data[i], saturateb->flux[i]);
347 if (self->primary_header != NULL) {
348 cpl_propertylist_delete(self->primary_header);
350 for (i = 0; i < 6; i++) {
351 if (self->data[i] != NULL) {
352 cpl_imagelist_delete(self->data[i]);
354 if (self->data_header[i] != NULL) {
355 cpl_propertylist_delete(self->data_header[i]);
358 cpl_free(self->filename);
moo_cube * moo_cube_new(void)
Create a new moo_cube.
cpl_error_code moo_cube_add_data(moo_cube *self, cpl_imagelist *list, moo_detector_type type, int ntas)
Add CPL_IMAGELIST extension to CUBE filename and update moo_cube structure.
void moo_cube_delete(moo_cube *self)
Delete a moo_cube.
moo_cube * moo_cube_merge(moo_cube *cubea, moo_saturate_map *saturatea, moo_cube *cubeb, moo_saturate_map *saturateb)
Merging two cubes.
cpl_error_code moo_cube_normalise(moo_cube *self, moo_saturate_map *map)
Normalise a moo_cube using saturate map.
void moo_cube_save(moo_cube *self, const char *filename)
Save a moo_cube to a FITS file.
cpl_error_code moo_cube_set_data(moo_cube *self, moo_detector_type type, int ntas, cpl_imagelist *data, cpl_propertylist *header)
set cube data for relevant extension
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
enum _moo_detector_type_ moo_detector_type
The type code type.
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.
int moo_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value