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