WriteDataToCSV Subroutine

public subroutine WriteDataToCSV(VarNames, Values, FileString, append_in, vfmt_in)

Uses

  • proc~~writedatatocsv~~UsesGraph proc~writedatatocsv WriteDataToCSV module~modgvec_globals MODgvec_Globals proc~writedatatocsv->module~modgvec_globals iso_fortran_env iso_fortran_env module~modgvec_globals->iso_fortran_env

Subroutine to write

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: VarNames(:)

Variable names,

real(kind=wp), intent(in) :: Values(:,:)

variable data

character(len=*), intent(in) :: FileString

Output file name

logical, intent(in), optional :: append_in

append data

character(len=*), intent(in), optional :: vfmt_in

value format


Calls

proc~~writedatatocsv~~CallsGraph proc~writedatatocsv WriteDataToCSV interface~getfreeunit GETFREEUNIT proc~writedatatocsv->interface~getfreeunit interface~getfreeunit->interface~getfreeunit

Source Code

SUBROUTINE WriteDataToCSV(VarNames,Values,FileString,append_in,vfmt_in)
! MODULES
USE MODgvec_Globals,ONLY:Unit_stdOut,GETFREEUNIT
! IMPLICIT VARIABLE HANDLING
IMPLICIT NONE
!-----------------------------------------------------------------------------------------------------------------------------------
! INPUT VARIABLES
CHARACTER(LEN=*),INTENT(IN)   :: VarNames(:)          !! Variable names,
REAL(wp),INTENT(IN)           :: Values(:,:)      !! variable data
CHARACTER(LEN=*),INTENT(IN)   :: FileString              !! Output file name
LOGICAL,INTENT(IN),OPTIONAL   :: append_in                  !! append data
CHARACTER(LEN=*),INTENT(IN),OPTIONAL   :: vfmt_in           !! value format
!-----------------------------------------------------------------------------------------------------------------------------------
! OUTPUT VARIABLES
!-----------------------------------------------------------------------------------------------------------------------------------
! LOCAL VARIABLES
INTEGER                       :: nVal                    !! Number of output Values
INTEGER                       :: nPlot                   !! number of 1D values
INTEGER                        :: iVal,iPlot,ioUnit
LOGICAL                        :: append
CHARACTER(LEN=10)              :: vfmt
!===================================================================================================================================
nVal=SIZE(VarNames,1)
IF(SIZE(Values,1).NE.nVal) STOP 'number of values /= nVariables in csv output'
nPlot=SIZE(Values,2)
ioUnit=GETFREEUNIT()
IF(PRESENT(append_in))THEN
  append=append_in
ELSE
  append=.FALSE.
END IF
IF(PRESENT(vfmt_in))THEN
  vfmt=vfmt_in
ELSE
  vfmt='e23.15'
END IF
IF(append)THEN
  WRITE(UNIT_stdOut,'(A)',ADVANCE='NO')'     APPEND DATA TO CSV FILE "'//TRIM(FileString)//'" ...'
  OPEN(UNIT     = ioUnit       ,&
       FILE     = TRIM(FileString)   ,&
       STATUS   = 'OLD'   ,&
       POSITION = 'APPEND'   ,&
       ACCESS   = 'SEQUENTIAL' )
ELSE
  WRITE(UNIT_stdOut,'(A)',ADVANCE='NO')'   WRITE DATA TO CSV FILE    "'//TRIM(FileString)//'" ...'
  OPEN(UNIT     = ioUnit       ,&
       FILE     = TRIM(FileString)   ,&
       STATUS   = 'REPLACE'   ,&
       ACCESS   = 'SEQUENTIAL' )
END IF


DO iVal=1,nVal-1
  WRITE(ioUnit,'(A,1X,(","))',ADVANCE='NO' )  '"'//TRIM(varNames(iVal))//'"'
END DO
WRITE(ioUnit,'(A)')  '"'//TRIM(varNames(nVal))//'"'

DO iPlot=1,nPlot
  WRITE(ioUnit,'(*('//TRIM(vfmt)//',:,","))') Values(:,iPlot)+SIGN(1.0e-99,Values(:,iPlot))
END DO

CLOSE(ioUnit)

WRITE(UNIT_stdOut,'(A)',ADVANCE='YES')"   DONE"
END SUBROUTINE WriteDataToCSV