FORTRAN
a very simple template:
program example
real*8 variable
c this line is commented out.
variable=3.4
write(*,*)variable ! After the exclamation mark is commented out.
stop
end
Note: spacing is important in Fortran; capitalization (case-sensitivity) is not.
column1: blank or with a "c" to indicate the rest of the line is a comment.
columns2-5: statement label (a number)
column 6: reserved for a single character to indicate a continuation of the previous line.
columns: 7-72: program statements
columns: >73: not used
to compile a simple fortran routine: g77 example.F
to define a function:
real function acosd(x)
implicit none
real x
real raddeg
parameter (raddeg = 180./3.14159)
acosd = raddeg * acos(x)
return
end
to define a subroutine:
subroutine function(x)
...
end
to call a function:
call func(a,b) for void functions
t=smear(x) for functions that return a value
comparatives: 3.gt.1 1.lt.3
to raise to an exponent: x=3**1.5
if statements:
if (x.gt.y) then
write(*,*)'yay!'
endif
defining varbs:
integer s1,array(10)
real x,t/3.14/ (The "/3.14/" sets the value of the varb)
logical var/.false./
character*256 ericschar (Characters have size = 2^n.)
character*8 lilchar
complex x
double precision y
real rad (Before defining a paremter, declare its type.)
parameter (rad=2) (If you want to set the variable value in the initial declaration.)
to open a file:
character*256 arg1
call getarg(1,arg1)
open(13,file=arg1)
close(13)
Here we define a character called arg1.
Then we fill arg1 with the first argument of the command line.
Then we open a file that's name we specified in the first command line argument.
We could replace this with open(13,file='filename')
Then you have to close the file you opened.
Careful...we used the ID #13 here, but some ID #'s are illegal because they are default ID's for FORTRAN.
This is beacuse FORTRAN is required by law to suck really hard.
Another way to open a file in Super-K code:
first:
setenv RFLIST rflist.$$.`hostname`
echo '10{{"'$filedir$file'",LOCAL,,RED,,,"recl=5670 status=old"}}' >! $RFLIST
then in source code:
DATA LUNI1 /10/
CALL SKOPENF(LUNI1, 0, 'Z', IERR)
CALL SKCLOSEF(LUNI1)
A note about logical unit numbers (lun):
In the example above we wrote to lun=10 when we said "write(10,*)varb".
On some compilers you can not have lun's greater than 99.
Also, some lun's are reserved and you can not use them.
to book a paw histogram: call hbook1(10,'Zenith Angle',51,-1.0,1.0,0.0)
(ID#,title,number of bins,lower limit, upper limit, always 0.0)
to fill a paw hbook file: call hf1(50,QISMSK,1.0)
where 50 is the logical unit number, QISMSK is some varb, and 1.0 is always there for some reason.
To continue a line that is getting too long:
call set_rflist(10,file_name,'LOCAL',' ','RED',' ',' ',
& tab 'recl=5670',' ',' ')
or like this:
if(ichar(list(i)(j:j)).lt.ichar('0') .or.
> ichar(list(i)(j:j)).gt.ichar('9')) goto 100
or like this:
if ((abs(ipnu(1)).eq.14.or.abs(ipnu(1)).eq.16)
$ .and.abs(mode).le.30) then
scientific notation: 3e-3 (float) 5.d-1 (double) 4.q3 (quad)
Reading and Writing:
write to screen: write(*,*)'the value of varb is:',varb
write to file lun=10: write(10,*)varb
(If you haven't opened a file with lun=10, then this command will write to a file called "fort.10".)
read from screen: read(*,*)varb
read from file lun=10: read(10,*)varb
one way to get to label 1001 is like this:
goto 1001
but you can also be sent there like so:
call skread(10,*1002,*1001,*170,*10)
(The function skread can send you to label 1001 based on what happens when it reads in lun=10...note the astericks.)
Hex denoted with the z constructor: parameter (LV_TRGAVAIL_FLAG = z'80000000')
To run a shell script within a fortran program:
character*600 livetime_cmd
integer iret
...
c-----livetime.sh takes two arguments:
c-----the values of the varb, NRUNSK
livetime_cmd = './livetime.sh NRUNSK NRUNSK'
iret = system(livetime_cmd)
to provide an area of shared storage between different subroutines (in f77):
program main
integer :: a, b
real :: c
common /d/ a, b, c
...
end program main
subroutine jake()
integer :: a, b
real :: c
common /d/ a, b, c
...
end subroutine jake
f90 uses something called "modules" for this instead.
Back to Resources