Usefull tips for FORTRAN
Few tips for FORTRAN:
character (len = 20) :: arg1
call getarg (1, arg1)
if (len_trim(arg1) == 0) then
print*,'No Arg1 provided' stop
endif
read (arg1, *) nsteps
In this example FORTRAN takes the first arguments and read as number of steps. Always use the warning if loop for multiple argument, sometimes one might miss to give inputs of one argument and it becomes difficult to debug the code.
Use bash shell command in FORTRAN
character (len = 1024) :: command
command = 'cp file1 folder1/'
call system ( trim(command))
In this example file1 is copied to folder1/
Print integer padded by zeros
Let say we would like to print 4 digit integer as
0001
0002
....
9999
only thing we have use a formatting string I4.4 and we are done.
open(unit,file='filename')
nline = 0
do
read (unit, *, end = 10)
nlines = nlines + 1
end do
10 close(unit)
Get arguments from command line
character (len = 20) :: arg1
call getarg (1, arg1)
if (len_trim(arg1) == 0) then
print*,'No Arg1 provided' stop
endif
read (arg1, *) nsteps
In this example FORTRAN takes the first arguments and read as number of steps. Always use the warning if loop for multiple argument, sometimes one might miss to give inputs of one argument and it becomes difficult to debug the code.
Use bash shell command in FORTRAN
character (len = 1024) :: command
command = 'cp file1 folder1/'
call system ( trim(command))
In this example file1 is copied to folder1/
Print integer padded by zeros
Let say we would like to print 4 digit integer as
0001
0002
....
9999
only thing we have use a formatting string I4.4 and we are done.
Get number of lines in a data file
open(unit,file='filename')
nline = 0
do
read (unit, *, end = 10)
nlines = nlines + 1
end do
10 close(unit)
Comments
Post a Comment