read_constellations Function

private function read_constellations(self, file) result(ok)

Read the constellations of file (found with find_data_file; '' for none) into self%lines and self%groups, in place of those read before. False (those kept) if it can't be found or read.

Arguments

Type IntentOptional Attributes Name
type(SkySphere), intent(inout) :: self
character(len=*), intent(in) :: file

constellation file ('' for none)

Return Value logical


Calls

proc~~read_constellations~~CallsGraph proc~read_constellations read_constellations proc~find_data_file find_data_file proc~read_constellations->proc~find_data_file proc~sky_direction sky_direction proc~read_constellations->proc~sky_direction proc~find_in_dirs find_in_dirs proc~find_data_file->proc~find_in_dirs

Called by

proc~~read_constellations~~CalledByGraph proc~read_constellations read_constellations proc~set_constellations SkySphere%set_constellations proc~set_constellations->proc~read_constellations

Source Code

    logical function read_constellations(self, file) result(ok)
        type(SkySphere), intent(inout) :: self
        character(len=*), intent(in) :: file                !! constellation file ('' for none)
        character(len=:), allocatable :: path
        character(len=1024) :: line
        real(c_float), allocatable :: lines(:,:), tmp(:,:)
        type(Constellation), allocatable :: groups(:), gtmp(:)
        real(wp) :: v(4)
        integer :: unit, status, n, ng, k

        ok = .true.
        if (len_trim(file) == 0) then
            self%constellation_file = ''
            self%n_lines = 0
            self%n_groups = 0
            return
        end if
        ok = .false.
        path = find_data_file(file)
        if (len(path) == 0) then
            write(*, '(a)') 'vkframes: constellation file '//trim(file)//' not found (set VKFRAMES_DATA_PATH)'
            return
        end if
        open(newunit=unit, file=path, status='old', action='read', iostat=status)
        if (status /= 0) then
            write(*, '(a)') 'vkframes: could not open the constellation file '//path
            return
        end if
        allocate(lines(8, 1024), groups(128))
        n = 0
        ng = 0
        do
            read(unit, '(a)', iostat=status) line
            if (status /= 0) exit
            k = scan(line, achar(13))                       ! (CRLF line ends)
            if (k > 0) line(k:) = ''
            line = adjustl(line)
            if (line(1:2) == 'N ') then                     ! a constellation
                if (ng == size(groups)) then
                    allocate(gtmp(2 * ng))
                    gtmp(1:ng) = groups(1:ng)
                    call move_alloc(gtmp, groups)
                end if
                ng = ng + 1
                groups(ng) = Constellation(trim(adjustl(line(3:))), n + 1, n)
                cycle
            end if
            if (len_trim(line) == 0 .or. line(1:1) == '#') cycle
            k = index(line, '#')                            ! (a comment after the line)
            if (k > 0) line(k:) = ''
            read(line, *, iostat=status) v                  ! dec1, ra1, dec2, ra2 (degrees, hours)
            if (status /= 0) cycle
            if (n == size(lines, 2)) then
                allocate(tmp(8, 2 * n))
                tmp(:, 1:n) = lines(:, 1:n)
                call move_alloc(tmp, lines)
            end if
            n = n + 1
            lines(:, n) = real([sky_direction(v(2), v(1)), 0.0_wp, sky_direction(v(4), v(3)), 0.0_wp], c_float)
            if (ng > 0) groups(ng)%last = n
        end do
        close(unit)
        call move_alloc(lines, self%lines)
        call move_alloc(groups, self%groups)
        self%n_lines = n
        self%n_groups = ng
        self%constellation_file = trim(file)
        ok = .true.
    end function read_constellations