Fortran2
'Fortran2: Getting the most from Fortran'
Following On
These notes follow on from the Fortran1 course. You may want to look back at those notes and examples, or if you're feeling confident, just dive in here!
To get the example programs login in to a Linux box and type:
svn co http://source.ggy.bris.ac.uk/subversion-open/fortran2/trunk fortran2
Allocatable Arrays
We'll begin by looking at 'allocatable arrys.
Previously, we declared the size of our arrays when we wrote our code. This is all well and good, and at least we knew where we stood. However, this exact specifiation of the shape of all things can become a painful limitation. Suppose you were writing a program to process a list of University employees. The number of employees will almost certainly vary from one month to the next. So, we can only know the number of records to read in at runtime. This sort of thing starts to crop up a lot when we aim to write more general-purpose, flexible & robust programs.
Fear not! however, as trusty Fortran90 is here to help. In Fortran90 we can write array declarions of the form:
<type>,allocatable,dimension(:) :: my1dAllocatableArray ! a 1D array of type <type>, which I will give a size to later
Let's go to an example:
cd fortran2/examples/example1
and take a look inside the file allocatable.f90.
In this example, we have a 2D grid and corresponding arrays of longitude and latitude coordinates, the sizes of which we will read from a namelist file at runtime. We will then allocate an appropriate amount of space, fill in the oordinate values according to the number of grid-cells specified in the file and report the whole lot to standard-out. Our arrays are declared with the allocatable attribute:
real,allocatable,dimension(:) :: x_coords ! x-coordinate values real,allocatable,dimension(:) :: y_coords ! y-coordinate values real,allocatable,dimension(:,:) :: data ! 2D data on grid
We've covered namelists before, so reading the values of the variables nx and ny is straighforward. Once we know these values we can request memory space for our arrays. Note that it is an excellent idea to check whether this request was satisfied. If it were not for some reason--if you request a huge amount of memory, your computer may not be able to oblige--we probably want the program to stop, lest bad things may happen!
allocate(x_coords(nx),stat=errstat) if (errstat /= 0) then write (*,*) 'ERROR: could not allocate x_coords array' stop endif
A 2d grid. Read x and y dimensions from a namelist. fill out array values according to 360 degrees/num cells etc.
Have written example. Will write up notes - GW.
User Derived Types
DAB radio. Channel has frequency and name. Array of channels.
Have written example, will write up notes - GW.
Modules
Variables and subroutines. Use (with only attributes). Group together related info/routines.
Libraries
Appendices
Including NetCDF
Have written examples, will write up notes - GW.