Why Learn It?• If you are pursuing a graduate degree in AOS, you will eventually run into Fortran: – An assignment – A research problem – Someone else’s code* *THIS is why commenting cod
Trang 2What is Fortran?
• One of the oldest programming languages still
in use
• Fortran77, Fortran90
• Used primarily by geophysical sciences:
– Good at math and manipulation of large data sets – Consistent with earliest NWP models
Trang 3I program in Fortran…
I program in Fortran…
Trang 4…
Trang 5Why Learn It?
• If you are pursuing a graduate degree in AOS, you will eventually run into Fortran:
– An assignment
– A research problem
– Someone else’s code*
*THIS is why commenting code is so important (later
on that)
Trang 6Your Typical Program
Trang 7Your Typical Program
c234567 PROGRAM MYPROGRAM
STOP END
Program Options
Declaration of Variables
MAIN CODE
MAIN CODE
The first seven columns
of the program code
are reserved for special
functions – most lines
of code begin on the
seventh column
Trang 8Your Typical Program
Trang 9Your Typical Program
c234567 PROGRAM MYPROGRAM
STOP END
Program Options
Declaration of Variables
MAIN CODE
MAIN CODE
All variables used
in the code have
to be declared at
the top, before
any of the main
code is run.
Trang 10Your Typical Program
Trang 11Your Typical Program
c234567 PROGRAM MYPROGRAM
STOP END
Program Options
Declaration of Variables
MAIN CODE
MAIN CODE This identifies the
end of the program
Trang 12A More Complicated Program
PROGRAM MYPROGRAM Options
Declaration of Variables
MAIN CODE CALL MYSUBROUTINE MAIN CODE
STOP END SUBROUTINE MYSUBROUTINE Options
Declaration of Variables
MAIN CODE CALL MYSUBROUTINE MAIN CODE
RETURN END
Trang 13A More Complicated Program
PROGRAM MYPROGRAM Options
Declaration of Variables
MAIN CODE CALL MYSUBROUTINE MAIN CODE
STOP END SUBROUTINE MYSUBROUTINE Options
Declaration of Variables MAIN CODE
RETURN END
Being able to trace the
flow of information is by
far the most important
thing to learn about
programming, in Fortran
or in any other language.
You will eventually have
to write code that is not
only able to perform
complicated functions,
but is readable to
someone else who
wishes to use it.
Trang 15• Numbers can be:
– Integers – This is any integer value (e.g -5, 0, 1) – “Real”/”Floating-Point”/etc – This is any number (e.g 5.6, 2.11245)
In general, “integer” variables are good for
keeping counts of things, while “real” variables are used for data.
Trang 17Logical Variables
• LOGICAL variables are either ‘true’ or ‘false’, and are usually used to determine if some set
of criteria has occurred or not
IF (Some variable is greater than 5) THEN
its_greater_than_five = TRUE ENDIF
Trang 18• Usually you are going to need a whole block of numbers representing your data set (e.g a
block of numbers representing vorticity at
every point in some domain)
• You can specify an array of any of these
variable types
Trang 19• The array has a type (INTEGER, REAL, etc.), a single name, and a set of dimensions:
VALUES(55,90)
Trang 22VALUES (1,2)
VALUES (1,2)
VALUES (1,3)
VALUES (1,3)
VALUES (1,2)
VALUES (1,2) VALUESVALUES(2,2)(2,2) VALUESVALUES(3,2)(3,2)
VALUES (2,3)
VALUES (2,3) VALUESVALUES(3,3)(3,3) VALUESVALUES(4,3)(4,3)Each element of VALUES is identifiable by
its index.
Trang 23VALUES (1,2)
VALUES (1,2)
VALUES (1,3)
VALUES (1,3)
VALUES (1,2)
VALUES (1,2) VALUESVALUES(2,2)(2,2) VALUESVALUES(3,2)(3,2)
VALUES (2,3)
VALUES (2,3) VALUESVALUES(3,3)(3,3) VALUESVALUES(4,3)(4,3)Each element of VALUES is identifiable by
its index.
Trang 24Arrays
Trang 25• Values in arrays have indices that correspond to their location in the array
• Loops provide the ability to go through each
member of the array individually:
For i = 1, 3
For j = 1, 2
VALUES(i,j) End
End
Trang 26• Arrays, loops, logical traps, etc.
• Poor coding technique will come back to bite you!
Trang 27• All coding languages allow you to sneak in
messages in the code itself to allow a reader to understand what is going on – these are called
Trang 28• All coding languages allow you to sneak in
messages in the code itself to allow a reader to understand what is going on – these are called
Trang 29In The Next Few Weeks