MODULE module_basic ! Module with basic functions !!!!!!! Subroutines/Functions ! attachString: Subroutine to attach a subStribg to an existing String ! ErrMsg: Subroutine to stop execution and provide an error message ! ErrWarnMsg: Function to print error/warning message ! ItoS: Function to transform an integer to String ! Nstrings: Function to repeat a number of times a given string ! removeChar: Subroutine to remove a given character from a string ! removeNONnum: Subroutine to remove non numeric characters from a string ! split: Subroutine which provides the values from a string [String] which has been split by a given ! character [charv] a given number of values [Nvalues] is expected ! StoI: Function to transform a String to an integer ! StoL: Function to transform a String to a boolean ! StoR: Function to transform a String to a real ! vectorR_KS: Function to transform a vector of reals to a string of characters USE module_definitions CONTAINS SUBROUTINE removeNONnum(String, newString) ! Subroutine to remove non numeric characters from a string IMPLICIT NONE CHARACTER(LEN=*), INTENT(IN) :: String CHARACTER(LEN=*), INTENT(OUT) :: newString ! Local INTEGER :: ic, inc, Lstring !!!!!!! Variables ! String: string to remove non-numeric characters ! newString: resultant string Lstring = LEN_TRIM(String) newString = '' inc = 1 DO ic=1, Lstring IF (ICHAR(String(ic:ic)) >= ICHAR('0') .AND. ICHAR(String(ic:ic)) <= ICHAR('9')) THEN newString(inc:inc) = String(ic:ic) inc = inc + 1 END IF END DO END SUBROUTINE removeNONnum SUBROUTINE attachString(String, subString) ! Subroutine to attach a subStribg to an existing String IMPLICIT NONE CHARACTER(LEN=1000), INTENT(INOUT) :: String CHARACTER(LEN=*), INTENT(IN) :: subString ! Local INTEGER :: LString, LsubString CHARACTER(LEN=50) :: fname !!!!!!! Variables ! Sting: String to increase ! subString: String to attach fname = 'attachString' LString = LEN_TRIM(String) LsubString = LEN_TRIM(subString) String(1:LString + LsubString) = String(1:LString) // TRIM(subString) END SUBROUTINE attachString SUBROUTINE removeChar(String, charv) ! Subroutine to remove a given character from a string IMPLICIT NONE CHARACTER(LEN=*), INTENT(INOUT) :: String CHARACTER(LEN=1), INTENT(IN) :: charv ! Local INTEGER :: ic, inc, Lstring CHARACTER(LEN=1000) :: newString !!!!!!! Variables ! String: string to remove a character ! charv: character to remove Lstring = LEN_TRIM(String) newString = '' inc = 1 DO ic=1, Lstring IF (String(ic:ic) /= charv) THEN newString(inc:inc) = String(ic:ic) inc = inc + 1 END IF END DO String = '' String(1:inc) = newString(1:inc) END SUBROUTINE removeChar CHARACTER(len=1000) FUNCTION vectorR_KS(d1, vector) ! Function to transform a vector of reals(r_k) to a string of characters IMPLICIT NONE INTEGER, INTENT(in) :: d1 REAL(r_k), DIMENSION(d1), INTENT(in) :: vector ! Local INTEGER :: iv CHARACTER(len=50) :: RS !!!!!!! Variables ! d1: length of the vector ! vector: values to transform fname = 'vectorR_KS' vectorR_KS = '' DO iv=1, d1 WRITE(RS, '(F50.25)')vector(iv) IF (iv == 1) THEN vectorR_KS = TRIM(RS) ELSE vectorR_KS = TRIM(vectorR_KS) // ', ' // TRIM(RS) END IF END DO END FUNCTION vectorR_KS CHARACTER(LEN=50) FUNCTION ItoS(Ival) ! ItoS: Function to transform an integer to String IMPLICIT NONE INTEGER, INTENT(IN) :: Ival ! Local CHARACTER(LEN=50) :: itoS0 WRITE(ItoS0,'(I50)')Ival CALL removeNONnum(ItoS0, ItoS) END FUNCTION ItoS CHARACTER(len=1000) FUNCTION Nstrings(Strval, Ntimes) ! Function to repeat a number of times a given string IMPLICIT NONE CHARACTER(LEN=50), INTENT(in) :: Strval INTEGER, INTENT(in) :: Ntimes ! Local INTEGER :: i !!!!!!! Variables ! Strval: String to repeat ! Ntimes: number of repetitions fname = 'Nstrings' Nstrings = '' Do i=1, Ntimes Nstrings = TRIM(Nstrings) // TRIM(Strval) END DO END FUNCTION Nstrings SUBROUTINE split(String,charv,Nvalues,values) ! Subroutine which provides the values from a string [String] which has been split by a given ! character [charv] a given number of values [Nvalues] is expected IMPLICIT NONE CHARACTER(LEN=1000), INTENT(IN) :: String CHARACTER(LEN=1), INTENT(IN) :: charv INTEGER, INTENT(IN) :: Nvalues CHARACTER(LEN=200), INTENT(OUT), DIMENSION(Nvalues) :: values ! Local INTEGER :: i, ibeg, iend, Lstring CHARACTER(LEN=3) :: numS CHARACTER(LEN=1000) :: newString !!!!!!! Variables ! String: String to split ! charv: Character to use ! Nvalues: number of values ! values: vector with the given values (up to 200 characters) fname = 'split' newString = String ibeg = 1 Lstring = LEN_TRIM(String) DO i=1,Nvalues-1 iend = INDEX(newString(ibeg:Lstring), charv) IF (iend == 0) THEN WRITE (numS,"(I3)")Nvalues - 1 msg = "String '" // TRIM(String) // "' does not have " // TRIM(numS) // " '" // charv // "' !!" CALL ErrMsg(msg, fname, -1) END IF values(i) = newString(ibeg:ibeg+iend-2) ibeg = ibeg+iend END DO values(Nvalues) = newString(ibeg:Lstring) END SUBROUTINE split SUBROUTINE ErrMsg(msg, funcn, errN) ! Subroutine to stop execution and provide an error message IMPLICIT NONE CHARACTER(LEN=*), INTENT(in) :: msg, funcn INTEGER, INTENT(in) :: errN ! Local CHARACTER(LEN=50) :: emsg !!!!!!! Variables ! msg: message to print with the error ! funcn: name of the funciton, section to localize the error ! errN: number of the error provided for a given FORTRAN function emsg = 'ERORR -- error -- ERROR -- error' IF (errN /= 0) THEN PRINT *,TRiM(emsg) PRINT *,' ' // TRIM(funcn) // ': ' // TRIM(msg) // ' !!' PRINT *,' error number:', errN STOP END IF RETURN END SUBROUTINE ErrMsg CHARACTER(LEN=50) FUNCTION ErrWarnMsg(msg) ! Function to print error/warning message IMPLICIT NONE CHARACTER(LEN=3), INTENT(in) :: msg ! Local fname = 'ErrWarnMsg' IF (msg == 'err') THEN ErrWarnMsg = 'ERROR -- error -- ERROR -- error' ELSE IF (msg == 'wrn') THEN ErrWarnMsg = 'WARNING -- warning -- WARNING -- warning' ELSE PRINT *,'ERROR -- error -- ERROR -- error' PRINT *,' ' // TRIM(fname) // ": '" // TRIM(msg) // "' does not exist!!" STOP END IF END FUNCTION ErrWarnMsg INTEGER FUNCTION StoI(String) ! Function to transform a String to an integer IMPLICIT NONE CHARACTER(LEN=200), INTENT(IN) :: String READ(String,'(I200)')StoI END FUNCTION StoI REAL FUNCTION StoR(String) ! Function to transform a String to a real IMPLICIT NONE CHARACTER(LEN=200), INTENT(IN) :: String READ(String,'(F200.0)')StoR END FUNCTION StoR LOGICAL FUNCTION StoL(String) ! Function to transform a String to a boolean IMPLICIT NONE CHARACTER(LEN=200), INTENT(IN) :: String IF (TRIM(String) == '.T.' .OR. TRIM(String) == '.true.' .OR. TRIM(String) == '.TRUE.' & .OR. TRIM(String) == 'yes' .OR. TRIM(String) == 'YES' .OR. TRIM(String) == 'y' ) THEN StoL = .TRUE. ELSE StoL = .FALSE. END IF END FUNCTION StoL END MODULE module_basic