00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "twiflat_ini.h"
00018
00019
00020
00021
00022
00023
00024
00025 static void parse_section_general(dictionary *, twiflat_config *, int *);
00026 static void parse_section_frames(dictionary *, twiflat_config *, int *);
00027 static void parse_section_badpix(dictionary *, twiflat_config *, int *);
00028
00029 static void parse_section_warpfix(dictionary *, twiflat_config *, int *);
00030 static void parse_section_cleanmean(dictionary *, twiflat_config *, int *);
00031
00032
00047
00048
00049
00050
00051 int generateTwiflat_ini_file(
00052 char * ini_name,
00053 char * name_i,
00054 char * name_o
00055 )
00056 {
00057 FILE * ini_file ;
00058
00059 if (file_exists(ini_name)) {
00060 cpl_msg_warning("generateTwiflat_ini_file","overwriting %s", ini_name) ;
00061 }
00062 if ((ini_file = fopen(ini_name, "w")) == (FILE*)NULL) {
00063 cpl_msg_error("generateTwiflat_ini_file","cannot create .ini file %s", ini_name) ;
00064 return -1 ;
00065 }
00066
00067 fprintf(ini_file,
00068 "#\n"
00069 "# Configuration file for generating twilight flatfields\n"
00070 "#\n") ;
00071
00072 fprintf(ini_file, "#\n\n[Eclipse]\n") ;
00073 fprintf(ini_file, "VersionNumber = %s\n\n", get_eclipse_version()) ;
00074 fprintf(ini_file,
00075 "#\n"
00076 "# Please read the algorithmic manual BEFORE you start using\n"
00077 "# this software. It is easy to misuse it without any knowledge.\n"
00078 "#\n");
00079 fprintf(ini_file,
00080 "\n"
00081 "#\n"
00082 "# ----- [General] configures various software stuff\n"
00083 "#\n") ;
00084 fprintf(ini_file,
00085
00086 "# This section is not mandatory. All eclipse routines\n"
00087 "# should be set once for all in a .eclipse-rc file.\n"
00088 "# If you choose to use the variables here, they will\n"
00089 "# override the settings you have in the environment.\n"
00090 "# See the eclipse installation manual to see what these variables\n"
00091 "# refer to.\n"
00092 "#\n"
00093 "# Here is a correspondance between ini and environment variables:\n"
00094 "#\n") ;
00095 fprintf(ini_file,
00096
00097 "# MaximumMemory E_MAXMEM\n"
00098 "# MaximumSwap E_MAXSWAP\n"
00099 "# TmpDirName E_TMPDIR\n"
00100 "# Verbose E_VERBOSE\n"
00101 "# Debug E_DEBUG\n"
00102 "# LogFileName E_LOGFILE\n"
00103 "#\n"
00104 "\n") ;
00105 fprintf(ini_file,
00106
00107 "[General]\n"
00108 "# MaximumMemory = 512 ; integer, maximum megs to allocate in RAM\n"
00109 "# MaximumSwap = 2048 ; integer, maximum megs to allocate in swap\n"
00110 "# TmpDirName = . ; path to temporary directory\n"
00111 "\n"
00112 "Verbose = no ; verbose mode activation\n"
00113 "Debug = no ; debug mode activation\n"
00114 "\n"
00115 "LogFile = yes ; activate message logging to a file\n"
00116 "LogFileName = /tmp/spiffi-log ; log file name\n"
00117 "\n"
00118 "\n") ;
00119 fprintf(ini_file,
00120 "#\n"
00121 "# the following are the names given in the argument of the python script\n"
00122 "#\n"
00123 "\n") ;
00124 fprintf(ini_file,
00125 "InFile = %s ; input file including the names of the stacked frames\n"
00126 "OutName = %s ; name of output fits file\n"
00127 , name_i, name_o ) ;
00128
00129 fprintf(ini_file,
00130 "#\n"
00131 "# The section [BadPix] indicates the positions of bad pixels from a bad pixel mask\n"
00132 "# The section [ColTilt] corrects for tilted spectra with respect to the image\n"
00133 "# The section [CleanMean] takes a average of the stacks of similar images by rejecting low and high\n"
00134 "# intensity values to get rid of the cosmics\n") ;
00135 fprintf(ini_file,
00136
00137 "\n"
00138 "[BadPix]\n"
00139 "Mask = mask.fits ; name of the bad pixel mask fits file\n"
00140 "[WarpFix]\n"
00141 "WarpFixInd = yes ; indicates if the warping of the spectra should be corrected or not\n"
00142 "PolyFile = poly.tfits ; name of the fits table containing the polynomial parameters\n"
00143 "kernel = tanh ; the kernel to be used: tanh, sinc, sinc2, lanczos, hamming, hann\n") ;
00144 fprintf(ini_file,
00145
00146 "[CleanMean]\n"
00147 "LoReject = 0.1 ; percentage of rejected low intensity pixels before averaging\n"
00148 "HiReject = 0.1 ; percentage of rejected high intensity pixels before averaging\n"
00149 "\n"
00150 "\n") ;
00151
00152 fclose(ini_file) ;
00153 return 0 ;
00154 }
00155
00156
00157
00168
00169
00170 twiflat_config * parse_twiflat_ini_file(char * ini_name)
00171 {
00172 dictionary * sym ;
00173 twiflat_config * cfg ;
00174 int status ;
00175
00176 if (!file_exists(ini_name)) {
00177 cpl_msg_error("parse_twiflat_ini_file","cannot find ini file [%s]: aborting", ini_name) ;
00178 return NULL ;
00179 }
00180 sym = iniparser_load(ini_name) ;
00181 if (sym == NULL) {
00182 cpl_msg_error("parse_twiflat_ini_file","in parsing ini file [%s]: aborting", ini_name) ;
00183 return NULL ;
00184 }
00185
00186 cfg = twiflat_cfg_create();
00187 if (cfg==NULL) {
00188 cpl_msg_error("parse_twiflat_ini_file","allocating twiflat cfg struct");
00189 iniparser_freedict(sym) ;
00190 return NULL ;
00191 }
00192
00193
00194
00195
00196
00197
00198 status = 0 ;
00199 parse_section_general (sym, cfg, &status);
00200 parse_section_frames (sym, cfg, &status);
00201 parse_section_badpix (sym, cfg, &status);
00202 parse_section_warpfix (sym, cfg, &status);
00203
00204 parse_section_cleanmean (sym, cfg, &status);
00205
00206 iniparser_freedict(sym);
00207
00208 if (status>0) {
00209 cpl_msg_error("parse_twiflat_ini_file","%d errors in ini file [%s]", status, ini_name);
00210 twiflat_cfg_destroy(cfg);
00211 cfg = NULL ;
00212 return NULL ;
00213 }
00214 return cfg ;
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 static void parse_section_general(
00235 dictionary * sym,
00236 twiflat_config * cfg,
00237 int *status
00238 )
00239 {
00240 char * cval ;
00241 int ival ;
00242
00243
00244
00245
00246 cval = iniparser_getstr(sym, "eclipse:versionnumber") ;
00247 if (cval!=NULL) {
00248 if (strcmp(cval, get_eclipse_version())) {
00249 cpl_msg_warning("parse_section_general","this ini file produced by version %s", cval);
00250 cpl_msg_warning("parse_section_general","you are running version %s", get_eclipse_version());
00251 }
00252 } else {
00253 cpl_msg_warning("parse_section_general","no eclipse version number found in file");
00254 }
00255
00256 ival = iniparser_getint(sym, "general:maximummemory", -1);
00257 if (ival>0) set_memory_parameter("max_ram", ival);
00258 ival = iniparser_getint(sym, "general:maximumswap", -1);
00259 if (ival>0) set_memory_parameter("max_swap", ival);
00260 set_verbose(iniparser_getboolean(sym, "general:verbose", 0));
00261 set_debug(iniparser_getboolean(sym, "general:debug", 0));
00262
00263 cval = iniparser_getstr(sym, "general:tmpdirname");
00264 if (cval!=NULL) {
00265 set_tmpdirname(cval);
00266 } else {
00267 set_tmpdirname(".");
00268 }
00269
00270 ival = iniparser_getboolean(sym, "general:logfile", 0);
00271 if (ival) {
00272 cval = iniparser_getstr(sym, "general:logfilename");
00273 if (cval!=NULL) {
00274 set_logfile(1);
00275 set_logfilename(cval);
00276 } else {
00277 set_logfile(0) ;
00278 }
00279 }
00280 cval = iniparser_getstr(sym, "general:outname");
00281 if (cval!=NULL) {
00282 strcpy (cfg -> outName , cval) ;
00283 } else {
00284 cpl_msg_error("parse_section_general:"," OutName in the .ini file was not found!\n") ;
00285 (*status)++ ;
00286 }
00287
00288 if (verbose_active())
00289 print_memory_parameters();
00290 return ;
00291 }
00292
00293 static void parse_section_frames(
00294 dictionary * sym,
00295 twiflat_config * cfg,
00296 int *status
00297 )
00298 {
00299 char * listname ;
00300 charmatrix * charm ;
00301 int i, j ;
00302 char * name,
00303 * type ;
00304 int nval, ntwi, ndark, non, noff ;
00305 char ** framelist ;
00306 int * frametypes ;
00307
00308 listname = iniparser_getstr(sym, "general:infile");
00309 if (is_ascii_list(listname)!=1) {
00310 cpl_msg_error("parse_section_frames","file [%s] is not an ASCII list: aborting", listname);
00311 (*status)++ ;
00312 return ;
00313 }
00314
00315
00316 charm = charmatrix_read(listname);
00317 if (charm==NULL) {
00318 cpl_msg_error("parse_section_frames","parsing input list [%s]", listname);
00319 (*status)++;
00320 return ;
00321 }
00322
00323
00324 nval = charm->ly ;
00325 for (j=0 ; j<charm->ly ; j++) {
00326
00327 name = charmatrix_elem(charm, 0, j);
00328 if (file_exists(name)!=1) {
00329 cpl_msg_warning("parse_section_frames","file [%s] declared in list does not exist", name);
00330 nval -- ;
00331 }
00332 }
00333 if (nval<1) {
00334 cpl_msg_error("parse_section_frames","no valid plane found in list [%s]", listname);
00335
00336 charmatrix_del(charm);
00337 (*status)++ ;
00338 return ;
00339 }
00340
00341
00342 framelist = cpl_malloc(nval * sizeof(char*));
00343 frametypes = cpl_malloc(nval * sizeof(int));
00344
00345
00346 i=0 ;
00347 ntwi = 0 ;
00348 ndark = 0 ;
00349 non = 0 ;
00350 noff = 0 ;
00351 for (j=0 ; j<charm->ly ; j++) {
00352 name = charmatrix_elem(charm, 0, j);
00353 if (file_exists(name)==1) {
00354
00355 framelist[i] = cpl_strdup(name);
00356
00357 if (charm->lx>1) {
00358
00359 type = charmatrix_elem(charm, 1, j);
00360 strlwc(type);
00361
00362 if (strstr(type, "tw")!=NULL) {
00363 frametypes[i] = FRAME_TWI ;
00364 ntwi++ ;
00365 } else if (strstr(type, "da")!= NULL) {
00366 frametypes[i] = FRAME_DARK ;
00367 ndark++ ;
00368 } else if (strstr(type, "on")!= NULL) {
00369 frametypes[i] = FRAME_INT_ON ;
00370 non++ ;
00371 } else if (strstr(type, "of")!= NULL) {
00372 frametypes[i] = FRAME_INT_OFF ;
00373 noff++ ;
00374 } else { cpl_msg_warning("parse_section_frames","frame type unknown: %s\n", type) ;
00375 }
00376 i++ ;
00377 }
00378 else
00379 {
00380 cpl_msg_error ("parse_section_frames","there must be file descriptions for this recipe\n") ;
00381 charmatrix_del(charm) ;
00382 cpl_free (frametypes) ;
00383 cpl_free (framelist) ;
00384 return ;
00385 }
00386 }
00387 }
00388 charmatrix_del(charm);
00389
00390
00391 cfg->framelist = framelist ;
00392 cfg->frametype = frametypes ;
00393 cfg->nframes = nval ;
00394 cfg->ntwi = ntwi ;
00395 cfg->ndark = ndark ;
00396 cfg->non = non ;
00397 cfg->noff = noff ;
00398
00399 return ;
00400 }
00401
00402 static void parse_section_badpix(
00403 dictionary * sym,
00404 twiflat_config * cfg,
00405 int *status )
00406 {
00407 char * cval ;
00408
00409 cval = iniparser_getstr(sym, "badpix:mask") ;
00410 if (cval != NULL)
00411 {
00412 strcpy (cfg -> mask , cval) ;
00413 }
00414 else
00415 {
00416 cpl_msg_error("parse_section_badpix:"," mask in the .ini file was not found!\n") ;
00417 (*status)++ ;
00418 }
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 static void parse_section_warpfix(
00458 dictionary * sym,
00459 twiflat_config * cfg,
00460 int * status )
00461 {
00462 int ival ;
00463 char * cval ;
00464
00465 ival = iniparser_getboolean(sym, "warpfix:warpfixind", -1) ;
00466 if (ival != -1)
00467 {
00468 cfg -> warpfixInd = ival ;
00469 }
00470 else
00471 {
00472 cpl_msg_error("parse_section_warpfix:"," WarpFixInd in the .ini file was not found!\n") ;
00473 (*status)++ ;
00474 }
00475 cval = iniparser_getstr(sym, "warpfix:polyfile");
00476 if (cval!=NULL) {
00477 strcpy (cfg -> polyFile , cval ) ;
00478 } else {
00479 cpl_msg_error("parse_section_warpfix:"," PolyFile in the .ini file was not found!\n") ;
00480 (*status)++ ;
00481 }
00482 cval = iniparser_getstr(sym, "warpfix:kernel") ;
00483 if (cval!=NULL)
00484 {
00485 strcpy (cfg -> kernel , cval ) ;
00486 }
00487 else
00488 {
00489 cpl_msg_error("parse_section_warpfix:"," kernel in the .ini file was not found!\n") ;
00490 (*status)++ ;
00491 }
00492 }
00493
00494
00495 static void parse_section_cleanmean(
00496 dictionary * sym,
00497 twiflat_config * cfg,
00498 int *status )
00499 {
00500 float dval ;
00501
00502 dval = iniparser_getdouble(sym, "cleanmean:loreject", -1.) ;
00503 if (dval!=-1.)
00504 {
00505 cfg -> loReject = dval ;
00506 }
00507 else
00508 {
00509 cpl_msg_error("parse_section_cleanmean:"," LoReject in the .ini file was not found!\n") ;
00510 (*status)++ ;
00511 }
00512 dval = iniparser_getdouble(sym, "cleanmean:hireject", -1.) ;
00513 if (dval!=-1.)
00514 {
00515 cfg -> hiReject = dval ;
00516 }
00517 else
00518 {
00519 cpl_msg_error("parse_section_cleanmean:"," hiReject in the .ini file was not found!\n") ;
00520 (*status)++ ;
00521 }
00522 return ;
00523 }
00524