Export the trajectory JSON file.
Note
It is assumed that all the data is present in the segments needed to propagate.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(mission_type), | intent(inout) | :: | me | |||
| character(len=*), | intent(in) | :: | filename |
plot file name [without extension] |
||
| logical, | intent(in), | optional | :: | only_first_rev |
to only do the first rev [Default is False] |
subroutine export_trajectory_json_file(me,filename,only_first_rev) implicit none class(mission_type),intent(inout) :: me character(len=*),intent(in) :: filename !! plot file name [without extension] logical,intent(in),optional :: only_first_rev !! to only do the first rev [Default is False] integer,parameter :: block_size = 512 !! number of segments formatted at a time (in parallel) integer :: iseg !! segment number counter integer :: nsegs_to_plot !! number of segments to plot real(wp),dimension(:),allocatable :: rdot, rmag logical :: accumulate_rdot !! to generate the rdot/rp/ra file real(wp),dimension(:),allocatable :: cumulative_et,cumulative_rdot,cumulative_r type(text_buffer),dimension(:),allocatable :: buf !! the JSON text of a block of segments integer :: iunit, istat, i1, i2 write(*,'(A)') ' * Export the trajectory JSON file.' ! optional arguments: nsegs_to_plot = size(me%segs) ! default export all the segments if (present(only_first_rev)) then if (only_first_rev) nsegs_to_plot = 8 ! only the first rev (8 segments) end if accumulate_rdot = me%generate_rp_ra_file do iseg = 1, nsegs_to_plot call destroy_traj(iseg) end do !==================================== ! now propagate the segments: !$OMP PARALLEL DO !...FIRSTPRIVATE(me) do iseg = 1, nsegs_to_plot call me%segs(iseg)%propagate(mode=2) ! [export points] end do !$OMP END PARALLEL DO !==================================== if (accumulate_rdot) then allocate(cumulative_et(0)) allocate(cumulative_rdot(0)) allocate(cumulative_r(0)) do iseg = 1, nsegs_to_plot associate (seg => me%segs(iseg)) call compute_rdot_vecs(seg%traj_inertial%x,seg%traj_inertial%y,seg%traj_inertial%z,& seg%traj_inertial%vx,seg%traj_inertial%vy,seg%traj_inertial%vz,& rmag, rdot) call append_traj_to_arrays(seg) end associate end do call me%export_rp_ra_json_file(cumulative_et, cumulative_r, cumulative_rdot, filename) end if ! write the JSON file directly (an array of segments). json-fortran is not used here, ! because building and printing the JSON structure is slow for large files (millions of ! numbers). the segments are formatted in parallel, a block at a time, and written in order. open(newunit=iunit, file=trim(filename)//'.json', access='stream', form='unformatted', & status='replace', iostat=istat) if (istat/=0) error stop 'error opening trajectory JSON file.' write(iunit) '{'//new_line('a')//' "segs": ['//new_line('a') allocate(buf(min(block_size, nsegs_to_plot))) do i1 = 1, nsegs_to_plot, block_size i2 = min(i1 + block_size - 1, nsegs_to_plot) !$OMP PARALLEL DO do iseg = i1, i2 call segment_trajectory_json(me%segs(iseg), iseg, iseg==nsegs_to_plot, buf(iseg-i1+1)) end do !$OMP END PARALLEL DO do iseg = i1, i2 associate (b => buf(iseg-i1+1)) write(iunit) b%s(1:b%n) end associate end do end do write(iunit) ' ]'//new_line('a')//'}'//new_line('a') close(iunit) do iseg = 1, nsegs_to_plot call destroy_traj(iseg) end do contains subroutine destroy_traj(iseg) integer,intent(in) :: iseg !! segment number call me%segs(iseg)%traj_inertial%destroy() call me%segs(iseg)%traj_rotating%destroy() call me%segs(iseg)%traj_se_rotating%destroy() end subroutine destroy_traj subroutine append_traj_to_arrays(seg) type(segment),intent(in) :: seg integer :: n, i n = size(seg%traj_inertial%et) if (seg%traj_inertial%et(1)<=seg%traj_inertial%et(2)) then ! forward propagated segment cumulative_et = [cumulative_et, seg%traj_inertial%et(1:n-1)] cumulative_rdot = [cumulative_rdot, rdot(1:n-1)] cumulative_r = [cumulative_r, rmag(1:n-1)] else ! some of the segments are propagated backwards, so we have to reverse them ! [note this is very inefficient... need to fix this] -TODO cumulative_et = [cumulative_et, seg%traj_inertial%et(n:2:-1)] cumulative_rdot = [cumulative_rdot, rdot(n:2:-1)] cumulative_r = [cumulative_r, rmag(n:2:-1)] end if end subroutine append_traj_to_arrays end subroutine export_trajectory_json_file