MOONS Pipeline Reference Manual 0.13.2
moo_region.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27#include <math.h>
28#include <string.h>
29#include <cpl.h>
30#include <hdrl.h>
31#include "moo_detector.h"
32#include "moo_utils.h"
33#include "moo_fits.h"
34#include "moo_region.h"
35/*----------------------------------------------------------------------------*/
40/*----------------------------------------------------------------------------*/
41
44/*-----------------------------------------------------------------------------
45 Function codes
46 -----------------------------------------------------------------------------*/
47
48/*----------------------------------------------------------------------------*/
57/*----------------------------------------------------------------------------*/
58moo_region *
60{
61 moo_region *res = (moo_region *)cpl_calloc(1, sizeof(moo_region));
62
63 return res;
64}
65
66/*----------------------------------------------------------------------------*/
75/*----------------------------------------------------------------------------*/
76void
77moo_region_delete(moo_region *self)
78{
79 if (self != NULL) {
80 cpl_free(self);
81 }
82}
83
84
85/*----------------------------------------------------------------------------*/
95/*----------------------------------------------------------------------------*/
96moo_region *
97moo_region_load(const char *value)
98{
99 cpl_ensure(value != NULL, CPL_ERROR_NULL_INPUT, NULL);
100
101 moo_region *res = NULL;
102 int a, b, c, d;
103 int nb = sscanf(value, "%d,%d,%d,%d", &a, &b, &c, &d);
104 if (nb == 4) {
105 res = moo_region_new();
106 res->xb = a;
107 res->yb = b;
108 res->xu = c;
109 res->yu = d;
110 }
111 return res;
112}
113
114/*----------------------------------------------------------------------------*/
120/*----------------------------------------------------------------------------*/
121void
122moo_region_dump(moo_region *self)
123{
124 if (self != NULL) {
125 cpl_msg_info("moo_region", "REGION %d %d %d %d", self->xb, self->yb,
126 self->xu, self->yu);
127 }
128}
129
130/*----------------------------------------------------------------------------*/
141/*----------------------------------------------------------------------------*/
142moo_regionlist *
144{
145 cpl_ensure(size > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
146 moo_regionlist *res =
147 (moo_regionlist *)cpl_calloc(1, sizeof(moo_regionlist));
148
149 res->size = size;
150 res->list = (moo_region **)cpl_calloc(size, sizeof(moo_region *));
151
152 return res;
153}
154/*----------------------------------------------------------------------------*/
162/*----------------------------------------------------------------------------*/
163void
164moo_regionlist_delete(moo_regionlist *self)
165{
166 if (self != NULL) {
167 for (int i = 0; i < self->size; i++) {
168 moo_region_delete(self->list[i]);
169 }
170 cpl_free(self->list);
171 cpl_free(self);
172 }
173}
174
175/*----------------------------------------------------------------------------*/
185/*----------------------------------------------------------------------------*/
186moo_regionlist *
187moo_regionlist_load(const char *value)
188{
189 cpl_ensure(value != NULL, CPL_ERROR_NULL_INPUT, NULL);
190
191 char *token;
192 const char *delim = ":";
193 /* get the first token */
194 char *cvalue = cpl_strdup(value);
195 token = strtok(cvalue, delim);
196 moo_regionlist *res = moo_regionlist_new(100);
197 int i = 0;
198 /* walk through other tokens */
199 while (token != NULL) {
200 moo_region *reg = moo_region_load(token);
201 res->list[i] = reg;
202 i++;
203 token = strtok(NULL, delim);
204 }
205 res->size = i;
206 cpl_free(cvalue);
207
208 return res;
209}
210
211/*----------------------------------------------------------------------------*/
223/*----------------------------------------------------------------------------*/
224moo_regionlist *
225moo_regionlist_load_layout(const cpl_frame *layout_frame,
227 int ntas)
228{
229 cpl_ensure(layout_frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
230 cpl_ensure(ntas >= 1 && ntas <= 2, CPL_ERROR_ILLEGAL_INPUT, NULL);
231
232 const char *layout_name = NULL;
233 layout_name = cpl_frame_get_filename(layout_frame);
234
235 cpl_table *layout =
236 moo_fits_load_extension_table(layout_name, NULL,
237 moo_detector_get_extname(type, ntas));
238
239 int nrow = cpl_table_get_nrow(layout);
240 moo_regionlist *res = moo_regionlist_new(nrow);
241
242 /* walk through other tokens */
243 for (int i = 0; i < nrow; i++) {
244 moo_region *reg = moo_region_new();
245 int llx = cpl_table_get_int(layout, MOO_REGION_LAYOUT_LLX, i, NULL);
246 int lly = cpl_table_get_int(layout, MOO_REGION_LAYOUT_LLY, i, NULL);
247 int urx = cpl_table_get_int(layout, MOO_REGION_LAYOUT_URX, i, NULL);
248 int ury = cpl_table_get_int(layout, MOO_REGION_LAYOUT_URY, i, NULL);
249 reg->xb = llx;
250 reg->yb = lly;
251 reg->xu = urx;
252 reg->yu = ury;
253 res->list[i] = reg;
254 }
255 res->size = nrow;
256 cpl_table_delete(layout);
257
258 return res;
259}
260
261/*----------------------------------------------------------------------------*/
267/*----------------------------------------------------------------------------*/
268void
269moo_regionlist_dump(moo_regionlist *self)
270{
271 if (self != NULL) {
272 cpl_msg_info("moo_region", "REGION_LIST %d", self->size);
273 for (int i = 0; i < self->size; i++) {
274 moo_region_dump(self->list[i]);
275 }
276 }
277}
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
Definition: moo_detector.c:137
enum _moo_detector_type_ moo_detector_type
The type code type.
Definition: moo_detector.h:64
cpl_table * moo_fits_load_extension_table(const char *filename, const char *name, const char *detectorname)
Load a table from FITS file.
Definition: moo_fits.c:359
moo_regionlist * moo_regionlist_new(int size)
Create a new moo_region.
Definition: moo_region.c:143
void moo_regionlist_dump(moo_regionlist *self)
dump a moo_regionlist
Definition: moo_region.c:269
moo_regionlist * moo_regionlist_load_layout(const cpl_frame *layout_frame, moo_detector_type type, int ntas)
load a moo_regionlist
Definition: moo_region.c:225
void moo_region_dump(moo_region *self)
dump a moo_region
Definition: moo_region.c:122
moo_region * moo_region_load(const char *value)
load a moo_region
Definition: moo_region.c:97
void moo_region_delete(moo_region *self)
Delete a moo_region.
Definition: moo_region.c:77
moo_region * moo_region_new(void)
Create a new moo_region.
Definition: moo_region.c:59
moo_regionlist * moo_regionlist_load(const char *value)
load a moo_regionlist
Definition: moo_region.c:187
void moo_regionlist_delete(moo_regionlist *self)
Delete a moo_regionlist.
Definition: moo_region.c:164