generate_eclipse_data Subroutine

public subroutine generate_eclipse_data(me, fileprefix, filetype)

Propagate all the segments with a dense time step and export the Eclipsing data w.r.t. Earth. In this file, any point <0 is in eclipse.

Based on plot_trajectory

TODO: use the data from export_trajectory_json_file, so that should be run first

Type Bound

mission_type

Arguments

Type IntentOptional Attributes Name
class(mission_type), intent(inout) :: me
character(len=*), intent(in) :: fileprefix

file prefix for the csv file (case name will be added)

integer, intent(in), optional :: filetype

type of output file: 1=csv [default] or 2=json


Calls

proc~~generate_eclipse_data~~CallsGraph proc~generate_eclipse_data mission_type%generate_eclipse_data icrf_frame icrf_frame proc~generate_eclipse_data->icrf_frame proc~destroy_trajectory trajectory%destroy_trajectory proc~generate_eclipse_data->proc~destroy_trajectory proc~get_case_name mission_type%get_case_name proc~generate_eclipse_data->proc~get_case_name proc~get_sun_fraction get_sun_fraction proc~generate_eclipse_data->proc~get_sun_fraction proc~propagate_segment segment%propagate_segment proc~generate_eclipse_data->proc~propagate_segment proc~write_json_real_array write_json_real_array proc~generate_eclipse_data->proc~write_json_real_array transform transform proc~generate_eclipse_data->transform two_body_rotating_frame two_body_rotating_frame proc~generate_eclipse_data->two_body_rotating_frame proc~int_to_string int_to_string proc~get_case_name->proc~int_to_string apparent_position apparent_position proc~get_sun_fraction->apparent_position cubic_shadow_model cubic_shadow_model proc~get_sun_fraction->cubic_shadow_model from_j2000body_to_j2000ssb from_j2000body_to_j2000ssb proc~get_sun_fraction->from_j2000body_to_j2000ssb proc~propagate_segment->icrf_frame proc~propagate_segment->transform proc~propagate_segment->two_body_rotating_frame first_call first_call proc~propagate_segment->first_call integrate integrate proc~propagate_segment->integrate proc~identity_stm identity_stm proc~propagate_segment->proc~identity_stm proc~set_segment_outputs segment%set_segment_outputs proc~propagate_segment->proc~set_segment_outputs proc~add_real_list text_buffer%add_real_list proc~write_json_real_array->proc~add_real_list proc~add_str text_buffer%add_str proc~write_json_real_array->proc~add_str proc~clear text_buffer%clear proc~write_json_real_array->proc~clear proc~add_real_list->proc~add_str proc~add_real text_buffer%add_real proc~add_real_list->proc~add_real proc~reserve text_buffer%reserve proc~add_real_list->proc~reserve proc~add_str->proc~reserve proc~add_real->proc~add_str proc~add_real->proc~reserve proc~add_digits add_digits proc~add_real->proc~add_digits proc~scaled scaled proc~add_real->proc~scaled proc~add_digits->proc~add_str proc~two_prod two_prod proc~scaled->proc~two_prod proc~split split proc~two_prod->proc~split

Called by

proc~~generate_eclipse_data~~CalledByGraph proc~generate_eclipse_data mission_type%generate_eclipse_data proc~halo_solver_main halo_solver_main proc~halo_solver_main->proc~generate_eclipse_data

Source Code

    subroutine generate_eclipse_data(me,fileprefix,filetype)

        class(mission_type),intent(inout) :: me
        character(len=*),intent(in) :: fileprefix !! file prefix for the csv file (case name will be added)
        integer,intent(in),optional :: filetype !! type of output file: 1=csv [default] or 2=json

        integer :: i,k,iunit,iseg,istat,istart,istep,n,n_total,n_shadow
        integer :: ifiletype
        logical :: do_se !! also compute the Sun-Earth rotating frame positions (JSON file)
        character(len=:),allocatable :: full_filename
        integer,dimension(:),allocatable :: offset !! index of each segment's first point in the arrays below, minus 1
        real(wp),dimension(:),allocatable :: et_se  !! epochs of all the points [sec], in time order within each segment
        real(wp),dimension(:),allocatable :: phi_se !! sun fraction of all the points (>0 in sunlight, <0 in eclipse)
        real(wp),dimension(:),allocatable :: x_se, y_se, z_se !! Sun-Earth rotating frame positions of all the points
        real(wp),dimension(:),allocatable :: et_shadow, phi_shadow !! only the points in eclipse (for the JSON file)
        real(wp),dimension(6) :: rv_moon !! inertial state of a point
        type(icrf_frame) :: inertial
        type(two_body_rotating_frame) :: se_rotating
        real(wp),dimension(6) :: x_se_rotating
        logical :: status_ok

        integer,parameter :: FILETYPE_CSV = 1
        integer,parameter :: FILETYPE_JSON = 2
        character(len=*),dimension(*),parameter :: file_ext = ['.csv ', &
                                                               '.json']

        if (present(filetype)) then
            ifiletype = filetype
        else
            ifiletype = FILETYPE_JSON
        end if
        if (ifiletype/=FILETYPE_CSV .and. ifiletype/=FILETYPE_JSON) &
            error stop 'invalid filetype for eclipse file'
        do_se = ifiletype==FILETYPE_JSON .and. me%eclipse_file_se_positions
        full_filename = trim(fileprefix)//'_'//me%get_case_name()//&
                        trim(file_ext(ifiletype))

        write(*,'(A)') ' * Generating eclipse file: '//full_filename

        ! destroy all trajectories first:
        do iseg = 1, size(me%segs)
            call me%segs(iseg)%traj_inertial%destroy()
            call me%segs(iseg)%traj_rotating%destroy()
            call me%segs(iseg)%traj_se_rotating%destroy()
        end do

        !====================================
        ! now propagate the segments:
        ! [export points at a fixed time step]
!$OMP PARALLEL DO
        do iseg = 1, size(me%segs)
            call me%segs(iseg)%propagate(mode=2,tstep=me%eclipse_dt_step)
        end do
!$OMP END PARALLEL DO
        !====================================

        ! size the arrays for all the points at once (appending to them one
        ! point at a time is quadratic in the number of points):
        allocate(offset(size(me%segs)))
        n_total = 0
        do iseg = 1, size(me%segs)
            offset(iseg) = n_total
            n_total = n_total + size(me%segs(iseg)%traj_inertial%et)
        end do
        allocate(et_se(n_total))
        allocate(phi_se(n_total))
        if (do_se) then
            allocate(x_se(n_total))
            allocate(y_se(n_total))
            allocate(z_se(n_total))
        end if

        ! compute the sun fraction (and the Sun-Earth rotating frame position) of each point.
        ! each segment fills its own slice of the arrays, so the segments are done in parallel
        ! (each one with its own copy of the ephemeris).
!$OMP PARALLEL DO PRIVATE(i,k,n,istart,istep,rv_moon,inertial,se_rotating,x_se_rotating,status_ok)
        do iseg = 1, size(me%segs)
            associate (traj => me%segs(iseg)%traj_inertial)
                n = size(traj%et)
                if (traj%et(n) > traj%et(1)) then
                    istart = 1;  istep = 1   ! forward propagated
                else
                    istart = n;  istep = -1  ! backward propagated: reverse it, so the points are in time order
                end if
                do k = 1, n
                    i = istart + (k-1)*istep
                    rv_moon = [traj%x(i), traj%y(i), traj%z(i), traj%vx(i), traj%vy(i), traj%vz(i)]
                    et_se(offset(iseg)+k)  = traj%et(i)
                    phi_se(offset(iseg)+k) = get_sun_fraction(me%segs(iseg)%eph, traj%et(i), &
                                                              rv_moon, me%r_eclipse_bubble)
                    if (do_se) then
                        ! also convert to sun-earth rotating (from inertial):
                        se_rotating = two_body_rotating_frame(primary_body=body_sun,&
                                                              secondary_body=body_earth,&
                                                              center=center_at_secondary_body,&
                                                              et=traj%et(i))
                        inertial = icrf_frame(b=body_moon)
                        call inertial%transform(rv_moon,se_rotating,traj%et(i),me%segs(iseg)%eph,&
                                                x_se_rotating,status_ok)
                        if (.not. status_ok) error stop 'transformation error in generate_eclipse_data'
                        x_se(offset(iseg)+k) = x_se_rotating(1)
                        y_se(offset(iseg)+k) = x_se_rotating(2)
                        z_se(offset(iseg)+k) = x_se_rotating(3)
                    end if
                end do
            end associate
        end do
!$OMP END PARALLEL DO

        ! save file:
        select case (ifiletype)
        case (FILETYPE_CSV)
            open(newunit=iunit,file=full_filename,status='REPLACE',iostat=istat)
            if (istat/=0) error stop 'error opening eclipse csv file.'
            write(iunit,'(A30,A,1X,A30)',iostat=istat) 'ET (sec)', ',', 'PHI'
            do k = 1, n_total
                ! phi >0 means the spacecraft is in sunlight (written as 0)
                write(iunit,'(*(E30.16E3,A,1X))',iostat=istat) et_se(k), ',', min(0.0_wp, phi_se(k))
            end do
            close(iunit,iostat=istat)
        case (FILETYPE_JSON)
            ! only the points where phi < 0 (in shadow):
            n_shadow = 0
            do k = 1, n_total
                if (phi_se(k) < 0.0_wp) n_shadow = n_shadow + 1
            end do
            allocate(et_shadow(n_shadow))
            allocate(phi_shadow(n_shadow))
            n_shadow = 0
            do k = 1, n_total
                if (phi_se(k) < 0.0_wp) then
                    n_shadow = n_shadow + 1
                    et_shadow(n_shadow)  = et_se(k)
                    phi_shadow(n_shadow) = phi_se(k)
                end if
            end do
            ! write the JSON file directly: json-fortran is not used here, because
            ! printing large arrays with it is slow (most of the time of this routine).
            ! the numbers are formatted in parallel, a block at a time (see [[write_json_real_array]]).
            open(newunit=iunit, file=full_filename, access='stream', form='unformatted', &
                 status='replace', iostat=istat)
            if (istat/=0) error stop 'error opening eclipse json file.'
            write(iunit) '{'
            call write_json_real_array(iunit, 'et',     et_shadow, last=.false.)
            call write_json_real_array(iunit, 'phi',    phi_shadow, last=.false.)
            call write_json_real_array(iunit, 'phi_se', phi_se, last=.false.) ! also the full S-E trajectory data for plotting
            call write_json_real_array(iunit, 'et_se',  et_se, last=.not. do_se)
            if (do_se) then
                call write_json_real_array(iunit, 'x_se_rotating', x_se, last=.false.)
                call write_json_real_array(iunit, 'y_se_rotating', y_se, last=.false.)
                call write_json_real_array(iunit, 'z_se_rotating', z_se, last=.true.)
            end if
            write(iunit) new_line('a')//'}'//new_line('a')
            close(iunit)
        end select

    end subroutine generate_eclipse_data