Set the Value of π (pi)
- Webmaster is using the value of 'M_PI' which is included in the header file 'math.h' of C-language.
real(8),parameter::pi=3.14159265358979323846D0
Get the character strings from command line
- This function can be used in 'gfortran' system.
- Character strings are read from command line as array of characters.
call getarg(1,dummy) call getarg(2,fnameR) call getarg(3,fnameW)
Convert the character string to the figure
- Character string dummy is converted in figure negv.
read(dummy,*) negv
Convert the figure to character string
- In the following case, real number vec(i) is converted in character string dummy with comma (,) and the format '15e7'.
- By adding the comma, the character string can be used in the case of csv saving conveniently.
write(dummy,'(",",e15.7)') vec(i)
Create the sequence named file and write down the data
- In the do-statement, outputting is carried out using sequence named file created by the program.
- In this case, files are named such as '_inp_xyz_01.txt', '_inp_xyz_02.txt'.... and those are saved as a character string filename and used.
do n=1,ntout+1 write (filename,'("_inp_xyz_",i2.2,".txt")') n open(13,file=filename,status='replace') do ne=1,NELT i=kakom(ne,1) write(13,*) x(i),y(i),tempe_s(i,n) end do close(13) end do
Read the data from the file with unknown row numbers
- Existing file fnameR is opened with unit number = 11.
- Since number of the rows is unknown, existence of the row is checked using the variable io.
- In this case, the range of do-loop is from 1 to nn. Because the limit of array of character string str is specified. Limit number of character is 100 (mm=100) and limit number of row is 1000 (nn=1000).
- If EOF is detected, processing exits from do-loop.
- After that, number of row in file is saved in the variable nd.
- The meaning of the variable io is shown below.
- Please don't forget to declare the integer io.
zero ( 0 ) | normal |
negative value | EOF |
positive value | error |
integer,parameter::nn=1000,mm=100 character(len=mm)::str(1:nn) integer::i,io,nd character(len=50)::fnameR call getarg(1,fnameR) open(11,file=fnameR,status='old') do i=1,nn read(11,'(a)',iostat=io) str(i) if(io<0)exit end do close(11) nd=i-1
Write the data to the file
- New file fnameW with unit number=12 is created and data is written on it.
- When outputting the character string str(i), 'trim( )' and 'adjustl( )' are used because of unnecessity of front and trailing blanks.
- 'trim( )' is a procedure to remove trailing blank characters of a string, and 'adjustl( )' is the procedure to left adjust a string.
- If set unit number=6 as a expression write(6,'(a)'), the results are shown in the screen. It is convenient as a checking method during calculation processing.
open(12, file=fnameW, status='replace') do i=1,nd write(12,'(a)') trim(adjustl(str(i))) end do close(12)
Specify the output format
- Specify output format using the string variable fmt3.
- Write the values on the string variable linebuf with the format specified by fmt3.
- It is necessary that string variable linebuf has enough length to store the characters.
fmt3="(i5,2(',',e15.7),3(',',e15.7))" write(linebuf,FMT=fmt3) i,x(i),z(i),hvec(i),q0vec(i),hvec(i)
Delete all blanks (spaces) in character string
- This is a sample to delete all blanks in character string. In this case, subroutine 'del_spaces' is used.
- Subroutine 'del_spaces' is released in nag's website and webmaster is using it.
- The usage is very simple. String variable linebuf which includes some blanks is passed to subroutine 'del_spaces.'
- When outputting by write-statement, 'trim( )' is required.
call del_spaces(linebuf) write (12,'(a)') trim(linebuf)
subroutine del_spaces(s) character (*), intent (inout) :: s character (len=len(s)) tmp integer i, j j = 1 do i = 1, len(s) if (s(i:i)==' ') cycle tmp(j:j) = s(i:i) j = j + 1 end do s = tmp(1:j-1) end subroutine del_spaces
Convert the under-bar to blank in character string
- Even if the case of data reading from 'csv' file, Fortran compiler treats the blank as a data separator at the time of data reading. So, webmaster has made the subroutine 'rep_ub' which has the function of replacing under bars '_' to blankis ' ' in character string. (Ex: This_is_a_pen. ⇒ This is a pen.)
- The usage is very simple. String variable dummy which includes some under bars is passed to subroutine 'rep_ub.'
- If array of strings are used, following procedure is necessary.
- Make copy of array element str1(i) as a string variable dummy.
- Pass a string variable dummy to subroutine 'rep_ub'.
- Make copy of returned string variable dummy as an array element str1(i).
- Above steps are required in the use of subroutine 'del_spaces'
dummy=str1(i) call rep_ub(dummy) str1(i)=dummy
subroutine rep_ub(s) !Replacing underbar with space character (*), intent (inout) :: s integer i do i = 1, len(s) if (s(i:i)=='_') s(i:i)=' ' end do end subroutine rep_ub
Set the Value of π (pi) using module
module defpi implicit none real(8),parameter::pi=3.14159265358979323846D0 end module defpi program f90_test use defpi implicit none .... .... .... end program