5
! NAME: baches_dark_subtraction.prg
6
! Procedure to subtract dark current from a FITS or BDF frame
7
! If the input file is a FITS it is converted to BDF with COMPUTE/BACHES
8
! and bias_subtracted if master_bias exists.
9
! It requires that a table master_dark.tbl exists and the
10
! master dark frames refered in master_dark.tbl. This master_dark.tbl is
11
! generated with the command
12
! "DARK/BACHES master_dark10,master_dark20"
15
! - Frame in FITS or BDF format with a spectrum in rows
16
! - Frame in BDF format with result
19
! A new frame_dark.bdf = frame.bdf - master_dark.bdf
21
! USAGE: DARK/BACHES frame.bdf frame_dark.bdf
22
! or: DARK/BACHES frame.fit frame_dark.bdf
24
!*******************************************************************************
28
! p1 REQUIRED: BDF or FITS frame containing an spectrum in rows
29
! p2 OPTIONAL: BDF frame with result of subtraction (default: frame_dark)
31
! Edit here to change defaults:
33
DEFINE/PARA p1 ? IMA "ENTER frame to be dark subtracted: "
34
DEFINE/PARA p2 ? IMA "ENTER name for result frame:"
36
! Convert paramaters into variables:
38
DEFINE/LOCA cmd/c/1/20 "dark_subtraction"
39
DEFINE/LOCA input/c/1/40 {p1}
40
DEFINE/LOCA output/c/1/40 {p2}
41
DEFINE/LOCA frame/c/1/40
42
DEFINE/LOCA master_table/c/1/40 "master_dark.tbl"
43
DEFINE/LOCA bias/c/1/40 "master_bias.bdf"
44
DEFINE/LOCA dark/c/1/40
45
DEFINE/LOCA exposure/i/1/1 0
46
DEFINE/LOCA frame_exposure/i/1/1 0
47
DEFINE/LOCA dark_exposure/i/1/1 0
48
DEFINE/LOCA coeficient/r/1/1 0.
49
DEFINE/LOCA best_coef/r/1/1 0.
50
DEFINE/LOCA current_coef/r/1/1 0.
51
DEFINE/LOCA nrow/i/1/1 0
52
DEFINE/LOCA loop/i/1/1 0
53
DEFINE/LOCA best_row/i/1/1 0
54
DEFINE/LOCA basename/c/1/40 "" ? +lower_levels
55
DEFINE/LOCA ext/c/1/10 "" ? +lower_levels
57
! Check if master dark_current table
59
IF m$exist(master_table) .eq. 0 THEN
60
WRITE/OUT "{cmd}: ERROR: {master_table} does not exist!!"
61
WRITE/OUT "{cmd}: recreate it with MDARK/BACHES dark*.bdf"
66
! Gets basename and extension of input file
71
! Convert FITS format into MIDAS BDF format
73
IF ext .ne. ".bdf" THEN
74
WRITE/OUT "{cmd}: converting {input} into {frame}"
75
COMPUTE/BACHESfits {input} {frame}
79
IF m$exist(bias) .eq. 1 THEN
80
COMPUTE/IMA {frame}b = {frame} - {bias}
81
RENAME {frame}b {frame}
82
WRITE/OUT "{cmd}: creating {frame} with {bias} subtracted"
84
WRITE/OUT "{cmd}: {bias} not found. No bias substructed"
88
! Check number of entries in master dark_current table
90
SHOW/TAB {master_table} >NULL
92
WRITE/OUT "{cmd}: number of entries in {master_table} = {nrow}"
94
! Get the exposure time
96
frame_exposure = m$value({frame},O_TIME(7))
97
WRITE/OUT "{cmd}: exposure time for {frame}: {frame_exposure}"
99
! Find the closest dark_current to our frame
103
exposure = {{master_table},:EXPOSURE,@{loop}}
104
IF exposure .gt. frame_exposure THEN
105
current_coef = exposure / frame_exposure
107
current_coef = frame_exposure / exposure
109
dark = "{{master_table},:FRAME,@{loop}}"
110
WRITE/OUT "{cmd}: file {dark}, exposure {exposure}, coeficient {current_coef}"
111
IF {loop} .eq. 1 THEN
112
best_coef = current_coef
115
IF best_coef .gt. current_coef THEN
116
best_coef = current_coef
123
dark = "{{master_table},:FRAME,@{best_row}}"
124
WRITE/OUT "{cmd}: best dark in file {dark} with coeficient {best_coef}"
126
dark_exposure = m$value({dark},O_TIME(7))
127
coeficient = frame_exposure / dark_exposure
128
COMPUTE/IMAG my_master_dark = ({dark} * {coeficient})
129
COMPUTE/IMAG {output} = ({frame} - my_master_dark)
130
DELETE/IMA my_master_dark NO
132
WRITE/OUT "{cmd}: Exposure value in {frame} = {frame_exposure}"
133
WRITE/OUT "{cmd}: Exposure value in {dark} = {dark_exposure}"
134
WRITE/OUT "{cmd}: Coeficient for my_master_dark = {coeficient}"