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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 #ifdef HAVE_CONFIG_H
00093 # include <config.h>
00094 #endif
00095
00096 #include <uves_response_utils.h>
00097 #include <uves_utils_wrappers.h>
00098 #include <uves_pfits.h>
00099 #include <uves_error.h>
00100 #include <uves_msg.h>
00101
00102 #include <cpl_test.h>
00103 #include <cpl.h>
00104 #include <string.h>
00105
00106
00107
00108
00109
00110
00111
00112
00113
00117
00119
00123
00124 static void
00125 test_load_3dtable(void)
00126 {
00127 cpl_table *flux_table = NULL;
00128 const char* dir_name="/media/VERBATIM/data7/flames/flames_uves_demo/ref/";
00129 const char* tab_name="flxstd.fits";
00130 char res_name[80];
00131 char ref_name[25];
00132 char ref_type[25];
00133
00134 char full_name[256];
00135 int nraws=0;
00136 int i=0;
00137 double ref_ra=0;
00138 double ref_dec=0;
00139 uves_propertylist* header=NULL;
00140 const char *columns[3] = {"LAMBDA", "BIN_WIDTH", "F_LAMBDA"};
00141 int ndata;
00142 int j=0;
00143 cpl_table* result=NULL;
00144 sprintf(full_name,"%s%s",dir_name,tab_name);
00145 uves_msg("full name: %s",full_name);
00146 flux_table=cpl_table_load(full_name,1,0);
00147 nraws=cpl_table_get_nrow(flux_table);
00148 header=uves_propertylist_load(full_name,0);
00149
00150 uves_msg("std: name \t type \t ra \t dec");
00151 for(i=0;i<nraws; i++) {
00152
00153 check( ref_ra = cpl_table_get_double(flux_table, "RA_DEG", i, NULL),
00154 "Could not read catalogue star right ascension");
00155
00156 check( ref_dec = cpl_table_get_double(flux_table, "DEC_DEG", i, NULL),
00157 "Could not read catalogue star declination");
00158
00159 check( sprintf(ref_name, cpl_table_get_string(flux_table, "OBJECT", i)),
00160 "Could not read reference object name");
00161
00162 check( sprintf(ref_type,cpl_table_get_string(flux_table, "TYPE", i)),
00163 "Could not read reference object type");
00164
00165 uves_msg("std: %s \t %s \t %f \t %f",ref_name,ref_type,ref_ra,ref_dec);
00166
00167 sprintf(res_name,"%s.fits",ref_name);
00168 check( ndata = cpl_table_get_int(flux_table, "NDATA", i, NULL),
00169 "Error reading length of flux array");
00170
00171 result=cpl_table_new(ndata);
00172 for(j = 0; j < 3; j++)
00173 {
00174 const cpl_array *data;
00175 int indx;
00176
00177 cpl_table_new_column(result, columns[j], CPL_TYPE_DOUBLE);
00178
00179 data = cpl_table_get_array(flux_table, columns[j], i);
00180
00181
00182
00183 uves_msg_debug("3d table array size = %" CPL_SIZE_FORMAT ", ndata = %d",
00184 cpl_array_get_size(data), ndata);
00185
00186 assure( cpl_array_get_size(data) >= ndata,
00187 CPL_ERROR_ILLEGAL_INPUT,
00188 "Flux table row %d: column '%s' depth (%" CPL_SIZE_FORMAT ") "
00189 "is less than NDATA (%d)",
00190 i, columns[j], cpl_array_get_size(data), ndata);
00191
00192 for (indx = 0; indx < ndata; indx++)
00193 {
00194
00195 cpl_table_set_double(result, columns[j], indx,
00196 cpl_array_get_float(data, indx, NULL));
00197 }
00198 }
00199
00200 uves_table_save(result, header, NULL,res_name, CPL_IO_DEFAULT);
00201 uves_free_table(&result);
00202 }
00203
00204
00205 cleanup:
00206 uves_free_table(&flux_table);
00207 uves_free_propertylist(&header);
00208
00209 return;
00210 }
00211
00212
00216
00217 static void
00218 test_3dtable(void)
00219 {
00220
00221 cpl_table *flux_table = NULL;
00222 uves_propertylist *raw_header = NULL;
00223 cpl_array *values = NULL;
00224 int depth = 10;
00225 int nrow = 2;
00226 const char *object[] = {"first std", "another standard star"};
00227 const char *type [] = {"NULL", "type 2"};
00228 const double lambda[] = {8000, 2000};
00229 const double fluxes[] = {1000000, 200};
00230 const double bin_width[] = {0.8, 0.0003};
00231 const double ra[] = {10, 80};
00232 const double dec[] = {-8, 0};
00233
00234 cpl_table *cat_flux = NULL;
00235 char *ref_name = NULL;
00236
00237 int i;
00238
00239
00240 flux_table = cpl_table_new(nrow);
00241
00242 cpl_table_new_column(flux_table, "RA_DEG", CPL_TYPE_DOUBLE);
00243 cpl_table_new_column(flux_table, "DEC_DEG", CPL_TYPE_DOUBLE);
00244
00245 cpl_table_new_column(flux_table, "OBJECT", CPL_TYPE_STRING);
00246 cpl_table_new_column(flux_table, "TYPE", CPL_TYPE_STRING);
00247
00248 cpl_table_new_column(flux_table, "NDATA", CPL_TYPE_INT);
00249
00250 cpl_table_new_column_array(flux_table, "LAMBDA", CPL_TYPE_FLOAT, depth);
00251 cpl_table_new_column_array(flux_table, "BIN_WIDTH", CPL_TYPE_FLOAT, depth);
00252 cpl_table_new_column_array(flux_table, "F_LAMBDA", CPL_TYPE_FLOAT, depth);
00253
00254 values = cpl_array_new(depth, CPL_TYPE_FLOAT);
00255
00256 for (i = 0; i < nrow; i++)
00257 {
00258 cpl_table_set_double(flux_table, "RA_DEG", i, ra[i]);
00259 cpl_table_set_double(flux_table, "DEC_DEG", i, dec[i]);
00260 cpl_table_set_string(flux_table, "OBJECT", i, object[i]);
00261 cpl_table_set_string(flux_table, "TYPE", i, type[i]);
00262 cpl_table_set_int (flux_table, "NDATA", i, depth);
00263
00264 cpl_array_fill_window_float(values, 0, depth, lambda[i]);
00265 cpl_table_set_array(flux_table, "LAMBDA", i, values);
00266
00267 cpl_array_fill_window_float(values, 0, depth, bin_width[i]);
00268 cpl_table_set_array(flux_table, "BIN_WIDTH", i, values);
00269
00270 cpl_array_fill_window_float(values, 0, depth, fluxes[i]);
00271 cpl_table_set_array(flux_table, "F_LAMBDA", i, values);
00272 }
00273
00274
00275 raw_header = uves_propertylist_new();
00276
00277
00278
00279 for (i = 0; i < nrow; i++)
00280 {
00281 double paccuracy = 60;
00282 double residual = 30;
00283
00284 uves_pfits_set_ra (raw_header, ra[i]+residual/3600);
00285 uves_pfits_set_dec(raw_header, dec[i]-residual/3600);
00286
00287 uves_free_table(&cat_flux);
00288 cpl_free(ref_name); ref_name = NULL;
00289 check_nomsg( cat_flux = uves_align(raw_header,
00290 flux_table,
00291 paccuracy,
00292 &ref_name));
00293
00294
00295
00296 while(ref_name[strlen(ref_name)-1] == ' ')
00297 {
00298
00299 ref_name[strlen(ref_name)-1] = '\0';
00300 }
00301
00302
00303
00304 assure( cpl_table_get_nrow(cat_flux) == depth, CPL_ERROR_ILLEGAL_OUTPUT,
00305 "Flux table has %" CPL_SIZE_FORMAT " rows, %d expected",
00306 cpl_table_get_nrow(cat_flux), depth);
00307
00308 assure( strcmp(ref_name, object[i]) == 0, CPL_ERROR_ILLEGAL_OUTPUT,
00309 "Found '%s'; Expected '%s'", ref_name, object[i]);
00310
00311 #if 0
00312 assure( float_equal(cpl_table_get_column_mean(cat_flux, "LAMBDA"),
00313 lambda[i], 0.001),
00314 CPL_ERROR_ILLEGAL_OUTPUT,
00315 "LAMBDA = %f; Expected = %f",
00316 cpl_table_get_column_mean(cat_flux, "LAMBDA"), lambda[i]);
00317
00318 assure( float_equal(cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"),
00319 bin_width[i], 0.001),
00320 CPL_ERROR_ILLEGAL_OUTPUT,
00321 "BIN_WIDTH = %f; Expected = %f",
00322 cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"), bin_width[i]);
00323
00324 assure( float_equal(cpl_table_get_column_mean(cat_flux, "F_LAMBDA"),
00325 fluxes[i], 0.001),
00326 CPL_ERROR_ILLEGAL_OUTPUT,
00327 "F_LAMBDA = %f; Expected = %f",
00328 cpl_table_get_column_mean(cat_flux, "F_LAMBDA"), fluxes[i]);
00329 #endif
00330 cpl_test_rel(cpl_table_get_column_mean(cat_flux, "LAMBDA"),
00331 lambda[i], 0.001);
00332
00333 cpl_test_rel(cpl_table_get_column_mean(cat_flux, "BIN_WIDTH"),
00334 bin_width[i], 0.001);
00335
00336 cpl_test_rel(cpl_table_get_column_mean(cat_flux, "F_LAMBDA"),
00337 fluxes[i], 0.001);
00338 }
00339
00340 cleanup:
00341 uves_free_table(&flux_table);
00342 uves_free_table(&cat_flux);
00343 uves_free_array(&values);
00344 uves_free_propertylist(&raw_header);
00345 cpl_free(ref_name);
00346
00347
00348 return;
00349 }
00350
00351
00355
00356
00357 int main(void)
00358 {
00359 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00360
00361 check_nomsg( test_3dtable() );
00362
00363
00364 cleanup:
00365 return cpl_test_end(0);
00366 }
00367