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 #ifdef HAVE_CONFIG_H
00029 # include <config.h>
00030 #endif
00031
00032
00033
00034
00035
00036 #include "sinfo_stack_ini_by_cpl.h"
00037 #include "sinfo_pro_types.h"
00038 #include "sinfo_raw_types.h"
00039 #include "sinfo_ref_types.h"
00040 #include "sinfo_functions.h"
00041
00042 #include "sinfo_file_handling.h"
00043
00044
00045
00046
00047 static void sinfo_stack_free_alloc(stack_config_n * cfg);
00048
00049
00050 static void
00051 parse_section_frames(stack_config_n *,
00052 cpl_frameset* sof,
00053 cpl_frameset** raw,
00054 int* status,
00055 fake* fk);
00056
00057 static void
00058 parse_section_cleanmean(stack_config_n *, cpl_parameterlist* cpl_cfg);
00059 static void
00060 parse_section_flatfield(stack_config_n *, cpl_parameterlist* cpl_cfg);
00061 static void
00062 parse_section_badpixel(stack_config_n *, cpl_parameterlist* cpl_cfg);
00063 static void
00064 parse_section_interleaving(stack_config_n *);
00065 static void
00066 parse_section_gaussconvolution(stack_config_n *, cpl_parameterlist* cpl_cfg);
00067 static void
00068 parse_section_shiftframes(stack_config_n *);
00069 static void
00070 parse_section_warpfix(stack_config_n *, cpl_parameterlist* cpl_cfg);
00071 static void
00072 parse_section_qclog(stack_config_n *, cpl_parameterlist* cpl_cfg);
00080
00081
00092
00093
00094 stack_config_n * sinfo_parse_cpl_input_stack(cpl_parameterlist* cpl_cfg,
00095 cpl_frameset* sof,
00096 cpl_frameset** raw,
00097 fake* fk)
00098 {
00099 stack_config_n * cfg =sinfo_stack_cfg_create_n();
00100 int status=0;
00101
00102
00103
00104
00105
00106
00107
00108
00109 parse_section_cleanmean (cfg, cpl_cfg);
00110 parse_section_flatfield (cfg, cpl_cfg);
00111 parse_section_badpixel (cfg, cpl_cfg);
00112 parse_section_interleaving (cfg);
00113 parse_section_gaussconvolution (cfg, cpl_cfg);
00114 parse_section_shiftframes (cfg);
00115 parse_section_warpfix (cfg, cpl_cfg);
00116 parse_section_qclog (cfg, cpl_cfg);
00117 parse_section_frames (cfg, sof, raw, &status, fk);
00118 if (status > 0) {
00119 sinfo_msg_error("parsing cpl input");
00120 sinfo_stack_cfg_destroy_n(cfg);
00121 cfg = NULL ;
00122 return NULL ;
00123 }
00124 return cfg ;
00125 }
00135 static void
00136 parse_section_frames(stack_config_n * cfg,
00137 cpl_frameset* sof, cpl_frameset** raw_set,int* status,
00138 fake* fk)
00139 {
00140
00141 int i;
00142 const char * name;
00143 char file[FILE_NAME_SZ];
00144 int nobj, noff, ndark ;
00145 int nditherobj, nditheroff ;
00146 int found_sky ;
00147 int found_ref ;
00148 int found_dither ;
00149 int found_dark ;
00150 int nraw=0;
00151 int nsof=0;
00152
00153 cpl_frame* frame = NULL;
00154
00155 char spat_res[FILE_NAME_SZ];
00156 char lamp_status[FILE_NAME_SZ];
00157 char band[FILE_NAME_SZ];
00158 int ins_set=0;
00159 char* tag=NULL;
00160 char* do_class=NULL;
00161 nsof = cpl_frameset_get_size(sof);
00162
00163
00164 do_class=fk->pro_class;
00165
00166 if(strcmp(do_class,"DEFAULT") == 0) {
00167 sinfo_extract_raw_stack_frames(sof,raw_set);
00168 } else if (strcmp(do_class,PRO_FIBRE_NS_STACKED_DIST) == 0) {
00169 sinfo_is_fibres_on_off(sof,*raw_set);
00170 } else if (strcmp(do_class,PRO_FIBRE_NS_STACKED) == 0) {
00171 sinfo_contains_frames_kind(sof,*raw_set,(char*)PRO_FIBRE_NS_STACKED);
00172 } else if (strcmp(do_class,RAW_STACKED_SLITPOS) == 0) {
00173 sinfo_extract_raw_stack_frames(sof,raw_set);
00174 } else {
00175 sinfo_extract_raw_frames_type(sof,raw_set,do_class);
00176 }
00177 nraw = cpl_frameset_get_size(*raw_set);
00178 if (nraw < 1) {
00179 sinfo_msg_error("Too few raw frames present in frameset!");
00180 (*status)++;
00181 return;
00182 }
00183
00184
00185
00186 cfg->framelist = cpl_malloc(nraw * sizeof(char*));
00187 cfg->frametype = cpl_malloc(nraw * sizeof(int));
00188 cfg->frameposition = cpl_malloc(nraw * sizeof(int));
00189
00190 for (i=0;i<nraw;i++) {
00191 cfg->framelist[i]=NULL;
00192 cfg->frametype[i]=-1;
00193 cfg->frameposition[i]=-1;
00194 }
00195
00196 found_sky = 0 ;
00197 found_ref = 0 ;
00198 found_dither = 0 ;
00199 found_dark = 0 ;
00200 nobj = 0 ;
00201 noff = 0 ;
00202 ndark = 0 ;
00203 nditherobj = 0 ;
00204 nditheroff = 0 ;
00205
00206
00207
00208
00209
00210 for (i=0 ; i<nraw ; i++) {
00211 frame = cpl_frameset_get_frame(*raw_set,i);
00212 name=cpl_frame_get_filename(frame);
00213 if(sinfo_file_exists((char*)name)==1) {
00214
00215 if(cpl_frame_get_tag(frame) != NULL) {
00216
00217 tag= (char*) cpl_frame_get_tag(frame);
00218 if((sinfo_frame_is_on(frame) == 0) ||
00219 (sinfo_frame_is_sky(frame) == 1))
00220 {
00221 cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
00222 cfg->frametype[i] = FRAME_OFF ;
00223 found_sky = 1;
00224 if (sinfo_frame_is_dither(frame))
00225 {
00226 cfg->frameposition[i] = FRAME_POS2 ;
00227 nditheroff++ ;
00228 }
00229 else
00230 {
00231 cfg->frameposition[i] = FRAME_POS1 ;
00232 noff++ ;
00233 }
00234 }
00235 else if(strstr(tag,RAW_REF) != NULL)
00236 {
00237 cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
00238 cfg->frametype[i] = FRAME_REF ;
00239 found_ref=1;
00240 if (sinfo_frame_is_dither(frame))
00241 {
00242 cfg->frameposition[i] = FRAME_POS2 ;
00243 }
00244 else
00245 {
00246 cfg->frameposition[i] = FRAME_POS1 ;
00247 }
00248 }
00249 else if(sinfo_is_dark(tag))
00250 {
00251 cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
00252 cfg->frametype[i] = FRAME_DRK ;
00253 cfg->frameposition[i] = FRAME_POS1 ;
00254 found_dark=1;
00255 ndark++;
00256 sinfo_msg("Frame is sinfo_dark on\n");
00257 }
00258 else
00259 {
00260
00261 cfg->framelist[i]=cpl_strdup(cpl_frame_get_filename(frame));
00262 cfg->frametype[i] = FRAME_ON ;
00263 found_ref=1;
00264 if (sinfo_frame_is_dither(frame))
00265 {
00266 cfg->frameposition[i] = FRAME_POS2 ;
00267 found_dither=1;
00268 nditherobj++;
00269 }
00270 else
00271 {
00272 cfg->frameposition[i] = FRAME_POS1 ;
00273 nobj++;
00274 }
00275 }
00276 }
00277 else
00278 {
00279
00280 cfg->frametype[i] = FRAME_ON ;
00281
00282 cfg->frameposition[i] = FRAME_POS1 ;
00283
00284 nobj ++ ;
00285 }
00286 }
00287
00288
00289 }
00290
00291 sinfo_msg("Noff= %d Nobj= %d Nditheroff= %d Nditherobj= %d",
00292 noff,nobj,nditheroff,nditherobj);
00293
00294
00295
00296
00297 cfg->nframes = nraw ;
00298 cfg->nobj = nobj ;
00299 cfg->noff = noff ;
00300 cfg->ndark = ndark ;
00301 cfg->nditherobj = nditherobj ;
00302 cfg->nditheroff = nditheroff ;
00303 cfg->contains_sky = found_sky ;
00304 cfg->contains_ref = found_ref ;
00305 cfg->contains_dither = found_dither ;
00306 cfg->contains_dark = found_dark ;
00307
00308
00309 frame = cpl_frameset_get_frame(*raw_set,0);
00310
00311 sinfo_get_spatial_res(frame,spat_res);
00312
00313
00314
00315 if(sinfo_frame_is_on(frame) == 1) {
00316 strcpy(lamp_status,"on");
00317 } else {
00318 strcpy(lamp_status,"off");
00319 }
00320 switch(sinfo_frame_is_on(frame))
00321 {
00322 case 0:
00323 strcpy(lamp_status,"on");
00324 break;
00325 case 1:
00326 strcpy(lamp_status,"off");
00327 break;
00328 case -1:
00329 strcpy(lamp_status,"undefined");
00330 break;
00331 default:
00332 strcpy(lamp_status,"undefined");
00333 break;
00334
00335
00336 }
00337 sinfo_get_band(frame,band);
00338
00339
00340 sinfo_msg("spatial resolution: %s lamp status: %s band: %s",
00341 spat_res, lamp_status, band);
00342
00343 sinfo_get_ins_set(band,&ins_set);
00344 frame = cpl_frameset_get_frame(*raw_set,0);
00345 tag=(char*)cpl_frame_get_tag(frame);
00346
00347
00348
00349
00350 if(fk->frm_switch==1) {
00351
00352
00353
00354
00355
00356 cfg->maskInd = fk->mask_index;
00357 cfg->indind = fk->ind_index;
00358 cfg->flatInd = fk->flat_index;
00359 cfg -> loReject = fk->low_rej;
00360 cfg -> hiReject = fk->hig_rej;
00361 cfg -> warpfixInd = fk->wfix_index;
00362
00363
00364
00365
00366
00367
00368
00369
00370 }
00371
00372
00373
00374
00375
00376 if(cfg -> flatInd) {
00377 if(NULL != cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP)) {
00378 frame = cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP);
00379 strcpy(file,cpl_frame_get_filename(frame));
00380 strcpy(cfg -> flatfield1, file);
00381 } else if(NULL != cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP1)) {
00382 frame = cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP1);
00383 strcpy(file,cpl_frame_get_filename(frame));
00384 strcpy(cfg -> flatfield1, file);
00385 } else {
00386 sinfo_msg_error("Frame %s not found!", PRO_MASTER_FLAT_LAMP);
00387 sinfo_msg_error("Frame %s not found!", PRO_MASTER_FLAT_LAMP1);
00388 sinfo_stack_free_alloc(cfg);
00389 (*status)++;
00390 return;
00391 }
00392
00393 if(found_dither) {
00394 if(NULL != cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP2)) {
00395 frame = cpl_frameset_find(sof,PRO_MASTER_FLAT_LAMP2);
00396 strcpy(file,cpl_frame_get_filename(frame));
00397 strcpy(cfg -> flatfield2, file);
00398 } else {
00399 sinfo_msg("Frame %s not found!", PRO_MASTER_FLAT_LAMP2);
00400 }
00401 }
00402 }
00403
00404
00405 if(cfg->maskInd != 0) {
00406 if(strstr(do_class,"FIBRE_NS") != NULL) {
00407 if(NULL != cpl_frameset_find(sof,PRO_BP_MAP_DI)) {
00408 frame = cpl_frameset_find(sof,PRO_BP_MAP_DI);
00409 strcpy(file,cpl_frame_get_filename(frame));
00410 strcpy(cfg -> mask, file);
00411 } else {
00412 sinfo_msg_error("Frame %s not found!", PRO_BP_MAP_DI);
00413 sinfo_stack_free_alloc(cfg);
00414 (*status)++;
00415 return;
00416 }
00417
00418 } else {
00419
00420 if(NULL != cpl_frameset_find(sof,PRO_MASTER_BP_MAP)) {
00421 frame = cpl_frameset_find(sof,PRO_MASTER_BP_MAP);
00422 strcpy(file,cpl_frame_get_filename(frame));
00423 strcpy(cfg -> mask, file);
00424 } else {
00425 sinfo_msg_error("Frame %s not found!", PRO_MASTER_BP_MAP);
00426 sinfo_stack_free_alloc(cfg);
00427 (*status)++;
00428 return;
00429 }
00430
00431 }
00432
00433 if (strcmp(do_class,RAW_STACKED_SLITPOS) == 0) {
00434 cfg -> indind = 1;
00435 }
00436
00437 if(cfg -> indind == 0) {
00438 if(NULL != cpl_frameset_find(sof,PRO_SLIT_POS)) {
00439 frame = cpl_frameset_find(sof,PRO_SLIT_POS);
00440 strcpy(file,cpl_frame_get_filename(frame));
00441 strcpy(cfg -> slitposList, file);
00442 sinfo_msg("Using %s to interpolate bad pixels",
00443 PRO_SLIT_POS);
00444 cpl_error_reset();
00445 } else if(NULL != cpl_frameset_find(sof,PRO_SLIT_POS_GUESS)) {
00446 frame = cpl_frameset_find(sof,PRO_SLIT_POS_GUESS);
00447 strcpy(file,cpl_frame_get_filename(frame));
00448 strcpy(cfg -> slitposList, file);
00449 sinfo_msg("Using %s to interpolated bad pixels",
00450 PRO_SLIT_POS_GUESS);
00451 cpl_error_reset();
00452 } else {
00453 sinfo_msg_error("Frame %s nor %s found!",
00454 PRO_SLIT_POS,PRO_SLIT_POS_GUESS);
00455 sinfo_stack_free_alloc(cfg);
00456 (*status)++;
00457 return;
00458 }
00459
00460
00461 }
00462 if(cfg -> maskInd == 2) {
00463 if(NULL != cpl_frameset_find(sof,PRO_INDEX_LIST)) {
00464 frame = cpl_frameset_find(sof,PRO_INDEX_LIST);
00465 strcpy(file,cpl_frame_get_filename(frame));
00466 strcpy(cfg ->indexlist, file);
00467 } else {
00468 sinfo_msg_error("Frame %s not found!", PRO_INDEX_LIST);
00469 sinfo_stack_free_alloc(cfg);
00470 (*status)++;
00471 return;
00472 }
00473
00474 }
00475 }
00476
00477 cfg -> warpfixInd = fk->wfix_index;
00478 if(cfg->warpfixInd != 0) {
00479 if(NULL != cpl_frameset_find(sof,PRO_DISTORTION)) {
00480 frame = cpl_frameset_find(sof,PRO_DISTORTION);
00481 strcpy(file,cpl_frame_get_filename(frame));
00482 strcpy(cfg -> polyFile, file);
00483 } else {
00484 sinfo_msg_error("Frame %s not found!", PRO_DISTORTION);
00485 sinfo_stack_free_alloc(cfg);
00486 (*status)++;
00487 return;
00488 }
00489 }
00490
00491 if(NULL != cpl_frameset_find(sof,PRO_MASTER_DARK)) {
00492 frame = cpl_frameset_find(sof,PRO_MASTER_DARK);
00493 strcpy(file,cpl_frame_get_filename(frame));
00494 strcpy(cfg -> mdark, file);
00495 cfg->mdark_ind=1;
00496
00497 } else {
00498 sinfo_msg("Frame %s not found", PRO_MASTER_DARK);
00499 cfg->mdark_ind=0;
00500 }
00501
00502
00503 return ;
00504 }
00505
00513 static void
00514 parse_section_cleanmean(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
00515 {
00516
00517 cpl_parameter* p;
00518 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.low_rejection");
00519 cfg -> loReject = cpl_parameter_get_double(p);
00520
00521 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.high_rejection");
00522 cfg -> hiReject = cpl_parameter_get_double(p);
00523
00524 return ;
00525 }
00532 static void
00533 parse_section_flatfield(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
00534 {
00535
00536 cpl_parameter* p;
00537 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.flat_index");
00538 cfg -> flatInd = cpl_parameter_get_bool(p);
00539
00540 }
00547 static void
00548 parse_section_badpixel(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
00549 {
00550 cpl_parameter* p;
00551
00552
00553 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.ind_index");
00554 cfg -> indind = cpl_parameter_get_bool(p);
00555
00556 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.mask_index");
00557 cfg -> maskInd = cpl_parameter_get_int(p);
00558
00559 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.mask_rad");
00560 cfg -> maxRad = cpl_parameter_get_int(p);
00561
00562
00563
00564 cfg -> sigmaFactor = 3.;
00565
00566 }
00572 static void
00573 parse_section_interleaving(stack_config_n * cfg)
00574 {
00575 cfg -> interInd = 0;
00576 cfg -> noRows = 400;
00577
00578 }
00585 static void
00586 parse_section_gaussconvolution(stack_config_n * cfg,cpl_parameterlist* cpl_cfg)
00587 {
00588
00589 cpl_parameter* p;
00590 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.gauss_index");
00591 cfg -> gaussInd = cpl_parameter_get_bool(p);
00592
00593 p = cpl_parameterlist_find(cpl_cfg,"sinfoni.stacked.kernel_half_width");
00594 cfg -> hw = cpl_parameter_get_int(p);
00595
00596 }
00602 static void
00603 parse_section_shiftframes(stack_config_n * cfg)
00604 {
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617 cfg -> sfInd = 0;
00618 cfg -> sfType = 1;
00619 cfg -> sfOrder = 2;
00620
00621 }
00628 static void
00629 parse_section_warpfix(stack_config_n * cfg,cpl_parameterlist * cpl_cfg)
00630 {
00631
00632 cpl_parameter* p;
00633
00634 p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.warpfix_ind");
00635 cfg -> warpfixInd = cpl_parameter_get_bool(p);
00636
00637
00638 p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.warpfix_kernel");
00639 strcpy(cfg -> kernel, cpl_parameter_get_string(p));
00640
00641 return ;
00642 }
00643
00650 static void
00651 parse_section_qclog(stack_config_n * cfg,cpl_parameterlist * cpl_cfg)
00652 {
00653
00654 cpl_parameter* p;
00655
00656 p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.qc_thresh_min");
00657 cfg -> qc_thresh_min = cpl_parameter_get_int(p);
00658
00659 p = cpl_parameterlist_find(cpl_cfg, "sinfoni.stacked.qc_thresh_max");
00660 cfg -> qc_thresh_max = cpl_parameter_get_int(p);
00661
00662 return ;
00663 }
00669 void
00670 sinfo_stack_free(stack_config_n ** cfg)
00671 {
00672 if(*cfg != NULL) {
00673 sinfo_stack_free_alloc(*cfg);
00674 sinfo_stack_cfg_destroy_n(*cfg);
00675 *cfg = NULL;
00676 }
00677 return;
00678
00679 }
00680
00686 static void
00687 sinfo_stack_free_alloc(stack_config_n * cfg)
00688 {
00689 int i=0;
00690 for (i=0; i< cfg->nframes; i++) {
00691 if(cfg->framelist[i] != NULL) {
00692 cpl_free(cfg->framelist[i]);
00693 cfg->framelist[i]=NULL;
00694 }
00695 }
00696 if(cfg->frametype != NULL) {
00697 cpl_free(cfg->frametype);
00698 cfg->frametype=NULL;
00699 }
00700 if(cfg->framelist != NULL) {
00701 cpl_free(cfg->framelist);
00702 cfg->framelist=NULL;
00703 }
00704 if(cfg->frameposition != NULL) {
00705 cpl_free(cfg->frameposition);
00706 cfg->frameposition=NULL;
00707 }
00708 }