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 #ifdef HAVE_CONFIG_H
00027 #include <config.h>
00028 #endif
00029
00030
00035
00036
00040
00041
00042
00043
00044 #include <math.h>
00045 #include <xsh_dfs.h>
00046 #include <xsh_data_localization.h>
00047 #include <xsh_utils_table.h>
00048 #include <xsh_error.h>
00049 #include <xsh_msg.h>
00050 #include <xsh_pfits.h>
00051 #include <cpl.h>
00052
00053
00054
00055
00056
00057
00064
00065 xsh_localization* xsh_localization_create(void)
00066 {
00067 xsh_localization* result = NULL;
00068
00069 XSH_CALLOC(result, xsh_localization, 1);
00070 XSH_NEW_PROPERTYLIST(result->header);
00071
00072 cleanup:
00073 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00074 xsh_localization_free( &result);
00075 }
00076 return result;
00077 }
00078
00079
00080
00090
00091 xsh_localization* xsh_localization_load( cpl_frame* frame){
00092 cpl_table* table = NULL;
00093 cpl_propertylist* header = NULL;
00094 const char* tablename = NULL;
00095 xsh_localization *result = NULL;
00096 int pol_degree = 0;
00097 int k;
00098
00099
00100 XSH_ASSURE_NOT_NULL( frame);
00101
00102
00103 check( tablename = cpl_frame_get_filename( frame));
00104
00105 XSH_TABLE_LOAD( table, tablename);
00106
00107 check( result = xsh_localization_create());
00108
00109 check( header = cpl_propertylist_load( tablename,0));
00110 check(cpl_propertylist_append( result->header, header));
00111
00112
00113 xsh_get_table_value( table, XSH_LOCALIZATION_TABLE_DEGPOL,
00114 CPL_TYPE_INT, 0, &pol_degree);
00115 result->pol_degree = pol_degree ;
00116
00117 check( result->edguppoly = cpl_polynomial_new( 1));
00118 check( result->cenpoly = cpl_polynomial_new( 1));
00119 check( result->edglopoly = cpl_polynomial_new( 1));
00120
00121 for( k = 0 ; k <= pol_degree ; k++ ) {
00122 char colname[32];
00123 float coef ;
00124
00125 sprintf( colname, "%s%d", XSH_LOCALIZATION_TABLE_COLNAME_CENTER, k);
00126 check( xsh_get_table_value( table, colname, CPL_TYPE_FLOAT, 0, &coef));
00127 check( cpl_polynomial_set_coeff( result->cenpoly, &k, coef));
00128
00129 sprintf( colname, "%s%d", XSH_LOCALIZATION_TABLE_COLNAME_EDGLO, k);
00130 check( xsh_get_table_value(table, colname, CPL_TYPE_FLOAT, 0, &coef));
00131 check( cpl_polynomial_set_coeff( result->edglopoly, &k,coef));
00132
00133 sprintf( colname, "%s%d", XSH_LOCALIZATION_TABLE_COLNAME_EDGUP, k );
00134 check( xsh_get_table_value( table, colname, CPL_TYPE_FLOAT, 0, &coef));
00135 check( cpl_polynomial_set_coeff( result->edguppoly, &k, coef));
00136 }
00137
00138 cleanup:
00139 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00140 xsh_error_msg("can't load frame %s",cpl_frame_get_filename(frame));
00141 xsh_localization_free(&result);
00142 }
00143 xsh_free_propertylist( &header);
00144 XSH_TABLE_FREE( table);
00145 return result;
00146 }
00147
00148
00149
00156
00157 void xsh_localization_free( xsh_localization **list)
00158 {
00159
00160 if (list && *list){
00161
00162 xsh_free_polynomial(&(*list)->cenpoly);
00163 xsh_free_polynomial(&(*list)->edguppoly);
00164 xsh_free_polynomial(&(*list)->edglopoly);
00165 xsh_free_propertylist(&((*list)->header));
00166 cpl_free(*list);
00167 *list = NULL;
00168 }
00169 }
00170
00171
00172
00173
00182
00183 cpl_propertylist * xsh_localization_get_header(xsh_localization *list)
00184 {
00185 cpl_propertylist *res = NULL;
00186
00187 XSH_ASSURE_NOT_NULL( list);
00188 res = list->header;
00189 cleanup:
00190 return res;
00191 }
00192
00193
00194
00208
00209 cpl_frame* xsh_localization_save(xsh_localization *list,
00210 const char* filename,
00211 xsh_instrument * instrument)
00212 {
00213 cpl_table* table = NULL;
00214 cpl_frame * result = NULL ;
00215 int k ;
00216 int pol_degree ;
00217 char colname[32] ;
00218 const char * tag = NULL ;
00219
00220
00221 XSH_ASSURE_NOT_NULL(list);
00222 XSH_ASSURE_NOT_NULL(filename);
00223
00224 pol_degree = list->pol_degree;
00225
00226 XSH_ASSURE_NOT_ILLEGAL( pol_degree >= 0 ) ;
00227
00228
00229 check(table = cpl_table_new( 1));
00230
00231
00232 for( k = 0 ; k<= pol_degree ; k++ ) {
00233
00234 sprintf( colname, "%s%d", XSH_LOCALIZATION_TABLE_COLNAME_CENTER, k ) ;
00235 check(
00236 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
00237 sprintf( colname, "%s%d", XSH_LOCALIZATION_TABLE_COLNAME_EDGUP, k ) ;
00238 check(
00239 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
00240 sprintf( colname, "%s%d", XSH_LOCALIZATION_TABLE_COLNAME_EDGLO, k ) ;
00241 check(
00242 cpl_table_new_column(table, colname, CPL_TYPE_FLOAT));
00243 }
00244 check( cpl_table_new_column( table, XSH_LOCALIZATION_TABLE_DEGPOL,
00245 CPL_TYPE_INT ) ) ;
00246
00247
00248 for( k = 0 ; k <= pol_degree ; k++ ) {
00249 double coef ;
00250
00251
00252 check(coef = cpl_polynomial_get_coeff( list->cenpoly, &k));
00253 sprintf( colname, "%s%d", XSH_LOCALIZATION_TABLE_COLNAME_CENTER, k);
00254 check(cpl_table_set( table, colname, 0, coef));
00255
00256
00257 check(coef = cpl_polynomial_get_coeff( list->edguppoly, &k));
00258 sprintf( colname, "%s%d", XSH_LOCALIZATION_TABLE_COLNAME_EDGUP, k);
00259 check(cpl_table_set( table, colname, 0, coef));
00260
00261
00262 check(coef = cpl_polynomial_get_coeff( list->edglopoly, &k));
00263 sprintf( colname, "%s%d", XSH_LOCALIZATION_TABLE_COLNAME_EDGLO, k);
00264 check(cpl_table_set( table, colname, 0, coef));
00265 }
00266
00267 check( cpl_table_set(table, XSH_LOCALIZATION_TABLE_DEGPOL, 0,
00268 pol_degree));
00269
00270 check( cpl_table_save( table, list->header, NULL, filename, CPL_IO_DEFAULT));
00271
00272 tag = XSH_GET_TAG_FROM_ARM( XSH_LOCALIZATION, instrument);
00273 check(result=xsh_frame_product(filename,
00274 tag,
00275 CPL_FRAME_TYPE_TABLE,
00276 CPL_FRAME_GROUP_PRODUCT,
00277 CPL_FRAME_LEVEL_TEMPORARY));
00278
00279 cleanup:
00280 XSH_TABLE_FREE(table);
00281 return result ;
00282 }
00283