11 subroutine get_datacube_arrays(target_time, cme_number, base_file, found, clts, lons, mask, vr, vp, vt, br, bt, bp, md, temp)
13 double precision,
intent(in) :: target_time
14 integer,
intent(in) :: cme_number
15 character(len=*),
intent(in) :: base_file
16 integer,
intent(out) :: found
17 double precision,
allocatable,
intent(out) :: clts(:), lons(:)
18 real,
allocatable,
intent(out) :: vr(:), vt(:), vp(:)
19 real,
allocatable,
intent(out) :: br(:), bt(:), bp(:)
20 real,
allocatable,
intent(out) :: md(:), temp(:), mask(:)
22 double precision :: min_diff, latest_time, pi, file_time
23 integer :: unit, ios, size(9), size_2(9)
24 integer :: ierr, nfiles, n, i, j, ndata, pos
25 integer :: unit_tmp, file_num
27 integer :: clt_size, lon_size
28 character(len=256) :: binfile, cltsfile, lonsfile
29 character(len=256) :: output_dir, fullpath, cme_str
30 character(len=256) :: cmd, line, filename, bestfile
31 character(len=256),
allocatable :: tmpfile
32 character(len=256) :: base_name, number_str
33 character(len=256),
allocatable :: files(:)
34 character(len=20) :: time_str
35 logical :: file_exists, in_mask
38 if (
allocated(clts))
deallocate(clts)
39 if (
allocated(lons))
deallocate(lons)
40 if (
allocated(mask))
deallocate(mask)
41 if (
allocated(vr))
deallocate(vr)
42 if (
allocated(vp))
deallocate(vp)
43 if (
allocated(vt))
deallocate(vt)
44 if (
allocated(br))
deallocate(br)
45 if (
allocated(bt))
deallocate(bt)
46 if (
allocated(bp))
deallocate(bp)
47 if (
allocated(md))
deallocate(md)
48 if (
allocated(temp))
deallocate(temp)
50 pos = index(base_file,
'/', back=.true.)
52 output_dir = base_file(1:pos)
57 write(cme_str,
'(I0)') cme_number
58 fullpath = trim(output_dir)//
'datacube/cme_'//trim(cme_str)
59 tmpfile=trim(fullpath)//
'/file_list.txt'
62 inquire(file=trim(tmpfile), exist=file_exists)
63 if (.not. file_exists)
then
64 print *,
"No file list file found in directory: ", trim(fullpath)
69 open(newunit=unit_tmp, file=tmpfile, status=
'old', action=
'read', iostat=ierr)
71 print *,
"Error opening temp file"
77 read(unit_tmp,
'(A)', iostat=ios) line
79 if (trim(line) /=
'') nfiles = nfiles + 1
84 print *,
"No valid .bin files found"
91 allocate(files(nfiles), stat=ierr)
93 print *,
"Error allocating files array"
100 read(unit_tmp,
'(A)', iostat=ios) line
102 if (trim(line) /=
'')
then
103 files(j) = trim(line)
112 pos = index(files(j),
'/', back=.true.)
114 base_name = trim(files(j))
116 base_name = trim(files(j)(pos+1:))
119 pos = index(base_name,
'.bin')
121 number_str = base_name(1:pos-1)
122 read(number_str, *, iostat=ierr) file_num
123 file_time = file_num/60.0d0
125 if (file_time > latest_time) latest_time = file_time
130 if (target_time > latest_time)
then
137 min_diff = huge(min_diff)
140 pos = index(files(j),
'/', back=.true.)
142 base_name = trim(files(j))
144 base_name = trim(files(j)(pos+1:))
147 pos = index(base_name,
'.bin')
149 number_str = base_name(1:pos-1)
150 read(number_str, *, iostat=ierr) file_time
151 file_time = file_time/60.0d0
153 if (abs(file_time - target_time) < min_diff)
then
154 min_diff = abs(file_time - target_time)
161 if (len_trim(bestfile) == 0)
then
162 print *,
"Could not determine best matching file"
170 pos = index(base_file,
'/', back=.true.)
172 output_dir = base_file(1:pos)
177 write(cme_str,
'(I0)') cme_number
179 binfile = trim(output_dir) //
'datacube/cme_' // trim(cme_str) //
'/' // trim(bestfile)
180 inquire(file=binfile, exist=file_exists)
181 if (.not. file_exists)
then
182 print *,
"Binary datacube file does not exist: ", trim(binfile)
187 open(newunit=unit, file=binfile, status=
'old', access=
'stream', form=
'unformatted', action=
'read', iostat=ios)
189 print *,
"Error opening datacube binary file: ", trim(binfile)
195 allocate(mask(
size(1)), vr(
size(2)), vp(
size(3)), vt(
size(4)), br(
size(5)), bt(
size(6)), bp(
size(7)), md(
size(8)), temp(
size(9)), stat=ios)
197 print *,
"Error allocating arrays of size ",
size
220 pi = 4.0d0 * atan(1.0d0)
222 allocate(clts(clt_size), lons(lon_size))
224 clts(i) = dble(i-1) * pi / dble(clt_size)
228 lons(i) = -pi + dble(i-1) * 2*pi / dble(lon_size)
233 subroutine get_value(clt, lon_in, ts_magnetogram, clts, lons, mask, vr, vp, vt, br, bt, bp, temp, md, ndata, &
234 found, vr_val, vp_val, vt_val, br_val, bt_val, bp_val, md_val, temp_val)
236 double precision,
intent(in) :: clt, lon_in, ts_magnetogram
237 double precision,
intent(in) :: clts(:), lons(:)
238 real,
intent(in) :: mask(:), vr(:), vp(:), vt(:), br(:), bt(:), bp(:), temp(:), md(:)
239 integer,
intent(in) :: ndata
240 integer,
intent(out) :: found
241 double precision,
intent(out) :: vr_val, vp_val, vt_val, br_val, bt_val, bp_val, md_val, temp_val
243 double precision :: synoptic_correction, lon, pi
244 real :: val_1(8), val_2(8), val_min(8), val_max(8)
248 pi = 4.0d0 * atan(1.0d0)
249 synoptic_correction = 2*pi/27.27/24.0d0
251 lon = modulo(lon_in + synoptic_correction*ts_magnetogram + pi, 2*pi) -pi
255 vr_val = ieee_value(vr_val, ieee_quiet_nan)
256 vp_val = ieee_value(vp_val, ieee_quiet_nan)
257 vt_val = ieee_value(vt_val, ieee_quiet_nan)
258 br_val = ieee_value(br_val, ieee_quiet_nan)
259 bt_val = ieee_value(bt_val, ieee_quiet_nan)
260 bp_val = ieee_value(bp_val, ieee_quiet_nan)
261 md_val = ieee_value(md_val, ieee_quiet_nan)
262 temp_val = ieee_value(temp_val, ieee_quiet_nan)
271 if (nint(mask(j)) == i)
then
273 vr_val = vr(min(j,
size(vr)))
274 vp_val = vp(min(j,
size(vp)))
275 vt_val = vt(min(j,
size(vt)))
276 br_val = br(min(j,
size(br)))
277 bt_val = bt(min(j,
size(bt)))
278 bp_val = bp(min(j,
size(bp)))
279 md_val = md(min(j,
size(md)))
280 temp_val = temp(min(j,
size(temp)))
289 double precision,
intent(in) :: clt_val, lon_val
290 double precision,
intent(in) :: clts(:), lons(:)
292 double precision :: current_diff, min_diff
293 integer :: position_index
294 integer :: clt_index, lon_index, n
298 min_diff = abs(clts(1) - real(clt_val))
300 current_diff = abs(clts(n) - real(clt_val))
301 if (current_diff < min_diff)
then
302 min_diff = current_diff
309 min_diff = abs(lons(1) - real(lon_val))
311 current_diff = abs(lons(n) - real(lon_val))
312 if (current_diff < min_diff)
then
313 min_diff = current_diff
319 lon_index = lon_index - 1
320 clt_index = clt_index - 1
322 if (clt_index >= 0 .and. lon_index >= 0)
then
323 position_index = lon_index + clt_index *
size(lons)
subroutine get_value(clt, lon_in, ts_magnetogram, clts, lons, mask, vr, vp, vt, br, bt, bp, temp, md, ndata, found, vr_val, vp_val, vt_val, br_val, bt_val, bp_val, md_val, temp_val)
subroutine get_datacube_arrays(target_time, cme_number, base_file, found, clts, lons, mask, vr, vp, vt, br, bt, bp, md, temp)