MOONS Pipeline Reference Manual 0.13.2
moo_flx.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 <cpl.h>
29#include <ctype.h>
30#include <string.h>
31#include "moo_pfits.h"
32#include "moo_fits.h"
33#include "moo_fibres_table.h"
34#include "moo_flx.h"
35/*----------------------------------------------------------------------------*/
50/*----------------------------------------------------------------------------*/
51
54/*-----------------------------------------------------------------------------
55 Function codes
56 -----------------------------------------------------------------------------*/
57
58/*----------------------------------------------------------------------------*/
66/*----------------------------------------------------------------------------*/
68{
69 moo_flx* res = cpl_calloc(1,sizeof(moo_flx));
70 return res;
71}
72
73
74/*----------------------------------------------------------------------------*/
83/*----------------------------------------------------------------------------*/
84moo_flx* moo_flx_load(const cpl_frame* frame)
85{
86 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
87
88 const char* filename = cpl_frame_get_filename(frame);
89 cpl_ensure(filename != NULL, CPL_ERROR_NULL_INPUT,
90 NULL);
91 cpl_errorstate prev_state = cpl_errorstate_get();
92
93 moo_flx* res = moo_flx_new();
94 res->filename = cpl_strdup(filename);
95 res->primary_header = cpl_propertylist_load( filename,0);
96
97 cpl_size qnum = cpl_fits_find_extension(filename,
98 MOO_FLX_CATALOG_EXTNAME);
99 if ( qnum > 0){
100 res->catalog = cpl_table_load(res->filename,qnum,0);
101 }
102
103 if ( !cpl_errorstate_is_equal(prev_state)){
104 cpl_msg_info("moo_flx","flx load %d",cpl_error_get_code());
105 cpl_errorstate_set(prev_state);
106 }
107 return res;
108}
109
110
111static int _moo_strcmpi(char* s1, char* s2){
112 int i;
113
114 int size1 = strlen(s1);
115 int size2 = strlen(s2);
116 if(size1!=size2)
117 return -1;
118
119 for(i=0;i<size1;i++){
120 if(toupper(s1[i])!=toupper(s2[i]))
121 return s1[i]-s2[i];
122 }
123
124 return 0;
125}
126
127static void _moo_remove_spaces(const char *input, char *result)
128{
129 int i, j = 0;
130 for (i = 0; input[i] != '\0'; i++) {
131 if (!isspace((unsigned char) input[i])) {
132 result[j++] = input[i];
133 }
134 }
135 result[j] = '\0';
136}
137
138static int _moo_cmp_objname(const char* obj1, const char* obj2)
139{
140 int res = -1;
141
142 char* str1 = cpl_strdup(obj1);
143 char* str2 = cpl_strdup(obj2);
144
145 _moo_remove_spaces(obj1,str1);
146 _moo_remove_spaces(obj2,str2);
147
148 res = _moo_strcmpi(str1,str2);
149
150 cpl_free(str1);
151 cpl_free(str2);
152 return res;
153}
154/*----------------------------------------------------------------------------*/
164/*----------------------------------------------------------------------------*/
165cpl_table* moo_flx_get_obj(moo_flx* self, const char* objname)
166{
167 cpl_table* res = NULL;
168 cpl_ensure(objname!=NULL,CPL_ERROR_NULL_INPUT,NULL);
169 int nrow = cpl_table_get_nrow(self->catalog);
170 const char** names = cpl_table_get_data_string_const(
171 self->catalog,MOO_FLX_CATALOG_NAME);
172
173 for(int i=0; i<nrow; i++){
174 const char* catname = names[i];
175 if ( _moo_cmp_objname(catname,objname)==0){
176 cpl_size qnum = cpl_fits_find_extension(self->filename,
177 catname);
178 res = cpl_table_load(self->filename,qnum,0);
179 break;
180 }
181 }
182
183 return res;
184}
185
186/*----------------------------------------------------------------------------*/
195/*----------------------------------------------------------------------------*/
197{
198 if ( self!= NULL){
199 if ( self->filename != NULL){
200 cpl_free(self->filename);
201 }
202 if ( self->primary_header!= NULL){
203 cpl_propertylist_delete(self->primary_header);
204 }
205 if ( self->catalog != NULL){
206 cpl_table_delete(self->catalog);
207 }
208 cpl_free(self);
209 }
210}
211
moo_flx * moo_flx_load(const cpl_frame *frame)
Load a FLX frame and create a moo_flx.
Definition: moo_flx.c:84
cpl_table * moo_flx_get_obj(moo_flx *self, const char *objname)
Find the TABLE for a given object name.
Definition: moo_flx.c:165
moo_flx * moo_flx_new(void)
Create a new moo_flx
Definition: moo_flx.c:67
void moo_flx_delete(moo_flx *self)
Delete a moo_flx.
Definition: moo_flx.c:196
FLX format.
Definition: moo_flx.h:43