Skip to content

Commit 42585b8

Browse files
committed
First version of the function for automatic download of the calibration file.
1 parent 7e6321c commit 42585b8

2 files changed

Lines changed: 244 additions & 3 deletions

File tree

stix/idl/demo/stx_imaging_demo.pro

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,39 @@ aux_data = stx_create_auxiliary_data(aux_fits_file, time_range)
7373

7474
stop
7575

76+
;************************************* DOWNLOAD CALIBRATION FITS FILE *********************************
77+
78+
; This file is used for creating a structure containing daily calibration data to be used for ELUT
79+
; correction (see next section)
80+
path_calib_file = stx_get_calibration_file(time_range[0], time_range[1], out_dir=out_dir)
81+
82+
stop
83+
84+
;;********************************** CONSTRUCT CALIBRATION DATA STRUCTURE *****************************
85+
86+
; Create a structure containing daily calibration data to use for ELUT correction:
87+
; - TIME_START: start time of the calibration file which is used to derive gain/offset values
88+
; - TIME_END: end time of the calibration file which is used to derive gain/offset values
89+
; - T_MEAN: mid point of the time range of the calibration file which is used to derive gain/offset values
90+
; - ENERGY_BIN_LOW: actual lower energy edges (in keV) of the science channels for each pixel
91+
; - ENERGY_BIN_HIGH: actual higher energy edges (in keV) of the science channels for each pixel
92+
; - GAIN: gain value for each pixel
93+
; - OFFSET: offset value for each pixel
94+
; - LIVE_TIME: live time of the calibration file
95+
; - ELUT_NAME: name of the ELUT that was utilized onboard when the calibration file was recorded
96+
97+
stx_read_calibration_data, path_calib_file, calib_data=calib_data
98+
99+
stop
100+
76101
;*************************************** ESTIMATE FLARE LOCATION **************************************
77102

78103
; Returns the coordinates of the estimated flare location (arcsec, Helioprojective Cartesian coordinates
79104
; from Solar Orbiter vantage point) in the 'flare_loc' keyword. These coordinates are used for setting the
80105
; center of the maps to be reconstructed
81106

82107
stx_estimate_flare_location, path_sci_file, time_range, aux_data, flare_loc=flare_loc, $
83-
path_bkg_file=path_bkg_file
108+
path_bkg_file=path_bkg_file, calib_data=calib_data
84109

85110
stop
86111

@@ -102,7 +127,7 @@ xy_flare = mapcenter
102127
; '6a','6b','6c','5a','5b','5c','4a','4b','4c','3a','3b','3c'])
103128

104129
vis=stx_construct_calibrated_visibility(path_sci_file, time_range, energy_range, mapcenter, subc_index=subc_index, $
105-
path_bkg_file=path_bkg_file, xy_flare=xy_flare)
130+
path_bkg_file=path_bkg_file, xy_flare=xy_flare, calib_data=calib_data)
106131

107132
stop
108133

@@ -178,7 +203,8 @@ stop
178203

179204
; Expectation Maximization algorithm from STIX counts (see Massa P. et al (2019) for details). Takes as input a
180205
; summed pixel data structure
181-
pixel_data_summed = stx_construct_pixel_data_summed(path_sci_file, time_range, energy_range, path_bkg_file=path_bkg_file, /silent)
206+
pixel_data_summed = stx_construct_pixel_data_summed(path_sci_file, time_range, energy_range, path_bkg_file=path_bkg_file, $
207+
calib_data=calib_data, /silent)
182208

183209
em_map = stx_em(pixel_data_summed, aux_data, imsize=imsize, pixel=pixel, xy_flare=xy_flare, mapcenter=mapcenter)
184210

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
;+
2+
;
3+
; name:
4+
; stx_get_calibration_file
5+
;
6+
; :description:
7+
; This procedure checks the STIX data archive for a given observation time and finds any
8+
; STIX CAL calibration file. If no file is present for the input date, the procedure searches for
9+
; the calibration file which is closest in time. If multiple files are present for that date,
10+
; this procedure downloads the one with largest duration.
11+
;
12+
; :categories:
13+
; template, example
14+
;
15+
; :params:
16+
; start_time : in, required, type="string"
17+
; the start time of the observation
18+
; end_time : in, required, type="string"
19+
; the end time of the observation
20+
;
21+
; :keywords
22+
; out_dir: path of the folder where the STIX calibration FITS files are saved. Default is the current directory
23+
;
24+
; clobber: 0 or 1. If set to 0, the code does not download the file again if it is already present in 'out_dir'.
25+
;
26+
; :returns:
27+
; Path of the downloaded STIX calibration FITS file
28+
;
29+
; :examples:
30+
; out_file = stx_get_calibration_file('09-May-23 06:14:37.094', '09-May-23 06:36:12.194')
31+
;
32+
; :history:
33+
; 24-Mar-2026 - Massa P. (FHNW), first release
34+
;-
35+
function stx_get_calibration_file, start_time, end_time, out_dir=out_dir, clobber=clobber
36+
37+
cd, current=current
38+
39+
default, out_dir, current
40+
default, clobber, 0
41+
42+
day_in_s = 86400.d ;; Number of seconds in a day. To be used later to identify the most appropriate calibration file
43+
44+
site = 'http://dataarchive.stix.i4ds.net'
45+
date_path = get_fid(start_time,end_time,/full,delim='/')
46+
47+
type_path = '/fits/CAL/'
48+
path = type_path + date_path[0] +'/CAL'
49+
filter = '*stix-cal-energy*.fits'
50+
found_files=sock_find(site,filter,path=path,count=count)
51+
52+
if count eq 0 then begin
53+
message, "No STIX calibration files found for this day ("+date_path[0]+"). Searching for calibration file which is closest in time.", /continue
54+
55+
;; Forward in time (for 30 days max - arbitrary choice)
56+
day_n_forward=1
57+
count_forward=0
58+
while (day_n_forward le 30) and (count_forward eq 0) do begin
59+
60+
this_start_time_forward = anytim(anytim(start_time) + day_n_forward * day_in_s, /vms)
61+
this_end_time_forward = anytim(anytim(end_time) + day_n_forward * day_in_s, /vms)
62+
63+
date_path_forward = get_fid(this_start_time_forward,this_end_time_forward,/full,delim='/')
64+
65+
path = type_path + date_path_forward[0] +'/CAL'
66+
found_files_forward = sock_find(site,filter,path=path,count=count_forward)
67+
68+
day_n_forward += 1
69+
70+
endwhile
71+
72+
;; Backward in time (for 30 days max - arbitrary choice)
73+
day_n_backward=1
74+
count_backward=0
75+
while (day_n_backward le 30) and (count_backward eq 0) do begin
76+
77+
this_start_time_backward = anytim(anytim(start_time) - day_n_backward * day_in_s, /vms)
78+
this_end_time_backward = anytim(anytim(end_time) - day_n_backward * day_in_s, /vms)
79+
80+
date_path_backward = get_fid(this_start_time_backward,this_end_time_backward,/full,delim='/')
81+
82+
path = type_path + date_path_backward[0] +'/CAL'
83+
found_files_backward = sock_find(site,filter,path=path,count=count_backward)
84+
85+
day_n_backward += 1
86+
87+
endwhile
88+
89+
if (count_forward eq 0) and (count_backward eq 0) then begin
90+
91+
message, "No STIX calibration file was found in the month before or after " +date_path[0]+". Please, download a calibration file manually."
92+
93+
endif else begin
94+
95+
diff_days = day_n_forward - day_n_backward
96+
;; if the difference is negative, it means that the closest file is the one found forward in time
97+
;; if the difference is positive, it means that the closest file is the one found backward in time
98+
;; if the difference is zero, we arbitrarily select the file found forward in time
99+
100+
if diff_days le 0 then begin
101+
102+
count = count_forward
103+
found_files = found_files_forward
104+
date_path = date_path_forward
105+
106+
endif else begin
107+
108+
count = count_backward
109+
found_files = found_files_backward
110+
date_path = date_path_backward
111+
112+
endelse
113+
114+
endelse
115+
116+
endif
117+
118+
message, "Download STIX calibration file recorded at " +date_path[0], /continue
119+
120+
len_path = STRLEN(site+path)
121+
122+
if count eq 1 then begin
123+
124+
selected_file = found_files[0]
125+
126+
;; Check that the ELUT that was onboard when the calibration file was recorded is the same as the on that was on board
127+
;; during the input time range. If not, raise an error
128+
129+
;; Find start time of the calibration file
130+
len_full_path = STRLEN(found_files[0])
131+
filename = STRMID(found_files[0], len_path+1, len_full_path)
132+
133+
string_dates = STRMID(filename, STRLEN('solo_CAL_stix-cal-energy_'), STRLEN(filename)-STRLEN('solo_CAL_stix-cal-energy_')-9) ;; 9 is the number of characters in the string '_V02.fits'
134+
135+
mid_part = STRPOS(string_dates, '-')
136+
137+
this_start_time_file = STRMID(string_dates, 0, mid_part)
138+
year = STRMID(this_start_time_file, 0, 4)
139+
month = STRMID(this_start_time_file, 4, 2)
140+
day = STRMID(this_start_time_file, 6, 2)
141+
hour = STRMID(this_start_time_file, 9, 2)
142+
min = STRMID(this_start_time_file, 11, 2)
143+
sec = STRMID(this_start_time_file, 13, 2)
144+
start_time_file = year + '-' + month + '-' + day + 'T' + hour + ':' + min + ':' + sec
145+
146+
;; Compare ELUT
147+
elut_file = stx_date2elut_file(start_time_file)
148+
elut_time_range = stx_date2elut_file(start_time)
149+
if elut_file ne elut_time_range then message, $
150+
"The closest-in-time calibration file was acquired with an onboard ELUT that differs from the one used during the selected time range. Please, manually download a different calibration file."
151+
152+
;; Download file
153+
sock_copy, selected_file, out_name, local_file=out_file, out_dir = out_dir, clobber=clobber
154+
155+
endif else begin
156+
157+
message, "More than 1 STIX calibration file found for this day ("+date_path[0]+").", /continue
158+
159+
start_time_file = []
160+
end_time_file = []
161+
162+
;; Extract file names
163+
for i = 0,n_elements(found_files)-1 do begin
164+
165+
len_full_path = STRLEN(found_files[i])
166+
filename = STRMID(found_files[i], len_path+1, len_full_path)
167+
168+
string_dates = STRMID(filename, STRLEN('solo_CAL_stix-cal-energy_'), STRLEN(filename)-STRLEN('solo_CAL_stix-cal-energy_')-9) ;; 9 is the number of characters in the string '_V02.fits'
169+
170+
mid_part = STRPOS(string_dates, '-')
171+
172+
this_start_time_file = STRMID(string_dates, 0, mid_part)
173+
year = STRMID(this_start_time_file, 0, 4)
174+
month = STRMID(this_start_time_file, 4, 2)
175+
day = STRMID(this_start_time_file, 6, 2)
176+
hour = STRMID(this_start_time_file, 9, 2)
177+
min = STRMID(this_start_time_file, 11, 2)
178+
sec = STRMID(this_start_time_file, 13, 2)
179+
start_time_file = [start_time_file, anytim(year + '-' + month + '-' + day + 'T' + hour + ':' + min + ':' + sec)]
180+
181+
this_end_time_file = STRMID(string_dates, mid_part+1, STRLEN(string_dates))
182+
year = STRMID(this_end_time_file, 0, 4)
183+
month = STRMID(this_end_time_file, 4, 2)
184+
day = STRMID(this_end_time_file, 6, 2)
185+
hour = STRMID(this_end_time_file, 9, 2)
186+
min = STRMID(this_end_time_file, 11, 2)
187+
sec = STRMID(this_end_time_file, 13, 2)
188+
end_time_file = [end_time_file, anytim(year + '-' + month + '-' + day + 'T' + hour + ':' + min + ':' + sec)]
189+
190+
endfor
191+
192+
;; Select file with largest duration
193+
duration = end_time_file - start_time_file
194+
195+
idx_file = where(duration eq max(duration))
196+
selected_file = found_files[idx_file]
197+
198+
;; Check that the ELUT that was onboard when the calibration file was recorded is the same as the on that was on board
199+
;; during the input time range. If not, raise an error
200+
201+
;; Compare ELUT
202+
elut_file = stx_date2elut_file(start_time_file[idx_file])
203+
elut_time_range = stx_date2elut_file(start_time)
204+
if elut_file ne elut_time_range then message, $
205+
"The closest-in-time calibration file was acquired with an onboard ELUT that differs from the one used during the selected time range. Please, manually download a different calibration file."
206+
207+
sock_copy, selected_file, out_name, local_file=out_file, out_dir = out_dir, clobber=clobber
208+
209+
endelse
210+
211+
return, out_file
212+
213+
214+
215+
end

0 commit comments

Comments
 (0)