output_array_to_csv Subroutine

public subroutine output_array_to_csv(file_id, array, space_ix, time_label, space_label)

Arguments

Type IntentOptional AttributesName
integer, intent(in) :: file_id
real, intent(in) :: array(:,:)
integer, intent(in), optional :: space_ix(:)
character(len=*), intent(in) :: time_label(:)
character(len=*), intent(in), optional :: space_label(:)

Calls

proc~~output_array_to_csv~~CallsGraph proc~output_array_to_csv output_array_to_csv proc~log_error log_error proc~output_array_to_csv->proc~log_error proc~log_message log_message proc~log_error->proc~log_message proc~log_write log_write proc~log_message->proc~log_write proc~log_format_message log_format_message proc~log_message->proc~log_format_message proc~to_string to_string proc~log_write->proc~to_string proc~date_time_str date_time_str proc~log_format_message->proc~date_time_str proc~colourise colourise proc~log_format_message->proc~colourise proc~string_index string_index proc~colourise->proc~string_index

Called by

proc~~output_array_to_csv~~CalledByGraph proc~output_array_to_csv output_array_to_csv proc~output_write_space_time_csv output_write_space_time_csv proc~output_write_space_time_csv->proc~output_array_to_csv proc~output_write_time output_write_time proc~output_write_time->proc~output_write_space_time_csv proc~output_write_monthly output_write_monthly proc~output_write_monthly->proc~output_write_time proc~output_write_daily output_write_daily proc~output_write_daily->proc~output_write_time proc~output_write_annual output_write_annual proc~output_write_annual->proc~output_write_time proc~output_day output_day proc~output_day->proc~output_write_daily proc~output_year output_year proc~output_year->proc~output_write_monthly proc~output_year->proc~output_write_daily proc~output_year->proc~output_write_annual proc~output_month output_month proc~output_month->proc~output_write_monthly proc~output_month->proc~output_write_daily proc~time_finish_year time_finish_year proc~time_finish_year->proc~output_year proc~time_process_day time_process_day proc~time_process_day->proc~output_day proc~time_process_month time_process_month proc~time_process_month->proc~output_month proc~time_process_month->proc~time_process_day proc~time_process_years time_process_years proc~time_process_years->proc~time_finish_year proc~time_process_years->proc~time_process_month program~swim swim program~swim->proc~time_process_years

Contents

Source Code


Source Code

  subroutine output_array_to_csv(file_id, array, space_ix, time_label, space_label)
    ! Write 2D real array to csv in file_id with a space and time index
    ! Both space_ix and time_label must be either the same length as array or of length 1
    ! to repeat over all rows.
    integer, intent(in) :: file_id
    real, intent(in) :: array(:, :)
    integer, intent(in), optional :: space_ix(:)
    character(len=*), intent(in) :: time_label(:)
    character(len=*), intent(in), optional :: space_label(:)
    character(len=identifier_max_length) :: tsfmt
    integer :: sh(2), i, ii, lens, lent

    ! Check input
    if (rank(array) /= 2) call log_error("output_array_to_csv", &
      "output_array_to_csv array is not 2D.")
    sh = shape(array)
    lens = 1
    if (present(space_ix)) then
      lens = size(space_ix)
      if (lens /= sh(2) .and. lens /= 1) call log_error("output_array_to_csv", &
        "space_ix not the right length.")
    else if (present(space_label)) then
      lens = size(space_label)
      if (lens /= sh(2) .and. lens /= 1) &
        call log_error("output_array_to_csv", "space_label not the right length.")
    else
      call log_error("output_array_to_csv", "space_ix or space_label have to be given.")
    end if
    lent = size(time_label)
    if (lent /= sh(2) .and. lent /= 1) &
      call log_error("output_array_to_csv", "time_label not the right length or 1.")
    ! Time, space format
    tsfmt = "(a, ',', "//trim(output_space_index_format)//", ',')"
    do i = 1, sh(2)
      ! Time, space indeces
      if (present(space_ix)) then
        write(file_id, tsfmt, advance="no") time_label(min(i, lent)), &
          space_ix(min(i, lens))
      else
        write(file_id, "(a, ', ', a, ', ')", advance="no") time_label(min(i, lent)), &
          trim(space_label(min(i, lens)))
      end if
      do ii = 1, sh(1)-1
        write(file_id, "("//trim(output_float_format)//", ',')", advance="no") &
          array(ii, i)
      end do
      ! Last column without ,
      write(file_id, "("//trim(output_float_format)//")") array(sh(1), i)
    end do
  end subroutine output_array_to_csv