13 subroutine get_datacube_arrays(target_time, cme_number, base_file, found, clts, lons, mask, vr, vp, vt, br, bt, bp, md, temp)
15 double precision,
intent(in) :: target_time
16 integer,
intent(in) :: cme_number
17 character(len=*),
intent(in) :: base_file
18 integer,
intent(out) :: found
19 double precision,
allocatable,
intent(out) :: clts(:), lons(:)
20 real,
allocatable,
intent(out) :: vr(:), vt(:), vp(:)
21 real,
allocatable,
intent(out) :: br(:), bt(:), bp(:)
22 real,
allocatable,
intent(out) :: md(:), temp(:), mask(:)
23 character(len=256) :: binfile, cltsfile, lonsfile
24 character(len=256) :: output_dir, fullpath, cme_str
25 integer :: unit, ios, size(9), size_2(9)
26 character(len=256) :: cmd, line, filename, bestfile
27 character(len=256),
allocatable :: tmpfile
28 integer :: ierr, nfiles, n, i, j, ndata, pos
29 double precision :: file_time
30 logical :: file_exists, in_mask
31 integer :: unit_tmp, file_num
32 character(len=256) :: base_name, number_str
33 character(len=256),
allocatable :: files(:)
34 integer :: clt_size, lon_size
35 double precision :: min_diff, latest_time
36character(len=20) :: time_str
40 if (
allocated(clts))
deallocate(clts)
41 if (
allocated(lons))
deallocate(lons)
42 if (
allocated(mask))
deallocate(mask)
43 if (
allocated(vr))
deallocate(vr)
44 if (
allocated(vp))
deallocate(vp)
45 if (
allocated(vt))
deallocate(vt)
46 if (
allocated(br))
deallocate(br)
47 if (
allocated(bt))
deallocate(bt)
48 if (
allocated(bp))
deallocate(bp)
49 if (
allocated(md))
deallocate(md)
50 if (
allocated(temp))
deallocate(temp)
52 pos = index(base_file,
'/', back=.true.)
54 output_dir = base_file(1:pos)
59 write(cme_str,
'(I0)') cme_number
60 fullpath = trim(output_dir)//
'datacube/cme_'//trim(cme_str)
61 tmpfile=trim(fullpath)//
'/file_list.txt'
64 inquire(file=trim(tmpfile), exist=file_exists)
65 if (.not. file_exists)
then
66 print *,
"No file list file found in directory: ", trim(fullpath)
71 open(newunit=unit_tmp, file=tmpfile, status=
'old', action=
'read', iostat=ierr)
73 print *,
"Error opening temp file"
79 read(unit_tmp,
'(A)', iostat=ios) line
81 if (trim(line) /=
'') nfiles = nfiles + 1
86 print *,
"No valid .bin files found"
93 allocate(files(nfiles), stat=ierr)
95 print *,
"Error allocating files array"
102 read(unit_tmp,
'(A)', iostat=ios) line
104 if (trim(line) /=
'')
then
105 files(j) = trim(line)
114 pos = index(files(j),
'/', back=.true.)
116 base_name = trim(files(j))
118 base_name = trim(files(j)(pos+1:))
121 pos = index(base_name,
'.bin')
123 number_str = base_name(1:pos-1)
124 read(number_str, *, iostat=ierr) file_num
125 file_time = file_num/60.0d0
127 if (file_time > latest_time) latest_time = file_time
132 if (target_time > latest_time)
then
139 min_diff = huge(min_diff)
142 pos = index(files(j),
'/', back=.true.)
144 base_name = trim(files(j))
146 base_name = trim(files(j)(pos+1:))
149 pos = index(base_name,
'.bin')
151 number_str = base_name(1:pos-1)
152 read(number_str, *, iostat=ierr) file_time
153 file_time = file_time/60.0d0
155 if (abs(file_time - target_time) < min_diff)
then
156 min_diff = abs(file_time - target_time)
163 if (len_trim(bestfile) == 0)
then
164 print *,
"Could not determine best matching file"
172 pos = index(base_file,
'/', back=.true.)
174 output_dir = base_file(1:pos)
179 write(cme_str,
'(I0)') cme_number
181 binfile = trim(output_dir) //
'datacube/cme_' // trim(cme_str) //
'/' // trim(bestfile)
182 inquire(file=binfile, exist=file_exists)
183 if (.not. file_exists)
then
184 print *,
"Binary datacube file does not exist: ", trim(binfile)
189 open(newunit=unit, file=binfile, status=
'old', access=
'stream', form=
'unformatted', action=
'read', iostat=ios)
191 print *,
"Error opening datacube binary file: ", trim(binfile)
197 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)
199 print *,
"Error allocating arrays of size ",
size
223 allocate(clts(clt_size), lons(lon_size))
225 clts(i) = dble(i-1) *
dpi / dble(clt_size)
229 lons(i) = -
dpi + dble(i-1) * 2*
dpi / dble(lon_size)
236 subroutine get_value(clt, lon_in, ts_magnetogram, clts, lons, mask, vr, vp, vt, br, bt, bp, temp, md, ndata, &
237 found, vr_val, vp_val, vt_val, br_val, bt_val, bp_val, md_val, temp_val)
239 double precision,
intent(in) :: clt, lon_in, ts_magnetogram
240 double precision,
intent(in) :: clts(:), lons(:)
241 real,
intent(in) :: mask(:), vr(:), vp(:), vt(:), br(:), bt(:), bp(:), temp(:), md(:)
242 integer,
intent(in) :: ndata
243 integer,
intent(out) :: found
244 double precision,
intent(out) :: vr_val, vp_val, vt_val, br_val, bt_val, bp_val, md_val, temp_val
246 real :: val_1(8), val_2(8), val_min(8), val_max(8)
247 double precision :: synoptic_correction, lon
250 synoptic_correction = 2*
dpi/27.27/24.0d0
252 lon = modulo(lon_in + synoptic_correction*ts_magnetogram +
dpi, 2*
dpi) -
dpi
256 vr_val = ieee_value(vr_val, ieee_quiet_nan)
257 vp_val = ieee_value(vp_val, ieee_quiet_nan)
258 vt_val = ieee_value(vt_val, ieee_quiet_nan)
259 br_val = ieee_value(br_val, ieee_quiet_nan)
260 bt_val = ieee_value(bt_val, ieee_quiet_nan)
261 bp_val = ieee_value(bp_val, ieee_quiet_nan)
262 md_val = ieee_value(md_val, ieee_quiet_nan)
263 temp_val = ieee_value(temp_val, ieee_quiet_nan)
272 if (nint(mask(j)) == i)
then
274 vr_val = vr(min(j,
size(vr)))
275 vp_val = vp(min(j,
size(vp)))
276 vt_val = vt(min(j,
size(vt)))
277 br_val = br(min(j,
size(br)))
278 bt_val = bt(min(j,
size(bt)))
279 bp_val = bp(min(j,
size(bp)))
280 md_val = md(min(j,
size(md)))
281 temp_val = temp(min(j,
size(temp)))
292 double precision,
intent(in) :: clt_val, lon_val
293 double precision,
intent(in) :: clts(:), lons(:)
294 integer :: position_index
295 integer :: clt_index, lon_index, n
296 double precision :: current_diff, min_diff
300 min_diff = abs(clts(1) - real(clt_val))
302 current_diff = abs(clts(n) - real(clt_val))
303 if (current_diff < min_diff)
then
304 min_diff = current_diff
311 min_diff = abs(lons(1) - real(lon_val))
313 current_diff = abs(lons(n) - real(lon_val))
314 if (current_diff < min_diff)
then
315 min_diff = current_diff
321 lon_index = lon_index - 1
322 clt_index = clt_index - 1
324 if (clt_index >= 0 .and. lon_index >= 0)
then
325 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)