MSC321 Scientific Programming for Atmospheric Science



Fortran Sample Code

The following links are very short fortran program meant to illustrate the basic usage of some Fortran statements.
  1. Variable declaration and simple manipulations
    1. integer variables
    2. real variables
    3. real kind comparison
    4. complex variables
    5. character variables
  2. Branching
    1. if statement
    2. select case statement
  3. Loops
    1. indexed-do loops
    2. do while loops
    3. infinite loops
    4. cycling versus exiting loops
    5. nested loops
  4. Subroutines and functions in modules
    1. function in a module
    2. subroutine in a module
    3. variable scoping in procedures
  5. Arrays
    1. 1D arrays
    2. Passing Arrays to Subroutines
    3. Array inquiry functions
    4. Array allocation
  6. Derived data types
    1. derived data type in a module
  7. File I/O
    1. Module fileio.f90 is very useful for doing array-oriented I/O in Fortran. It encapsulates the essential of opening and closing files, and doing input and output of Fortran arrays in both ASCII and binary formats. The code testio.f90 illustrates how the module fileio can be used. Currently it is coded to deal with double precision arrays, but that can be easily extended. If all arrays written in binary are of the same shape, then reading and writing the data in fortran, and passing it to matlab are straightforward operations, see matlab script fortread.m.
  8. Timing
    1. timing module to perform manual profiling on code segments.
  9. Source code file management
      Makefile to manage the creation of executables from source codes. The following commands are useful:
    1. make example will create executable example from source code example.f90
    2. "make clean" to delete all module and object files

Useful Software and Tools

  1. Student who want to install Linux software on Mac laptops should visit Mac Ports. It greatly simplified installing open-source software on Macs. The web page guides through installing MacPorts and using it to install packages.
  2. Unix Primer These notes a crash course in navigating UNIX file systems, editing with emacs, and compiling. Other web resources can also be handy:
    1. UNIX Tutorial for Beginners
    2. A Basic UNIX Tutorial
    3. INTRODUCTION TO UNIX
  3. Editors for text editing.
    1. vim/gvim
      vim graphical help
      A vim graphical cheat sheet . A PDF version for easy printing is available thanks to Brendan Kidwell.
    2. emacs
      emacs reference card
      emacs_cheat
    3. gedit
    All these editors are available on a variety of platforms. All of them have a graphical user interface that make them easy to learn while getting useful work done. Finally, vim and emac can be run on Linux, Windows and Macs, they have a context sensitive editor to help in programming (they highlights the C/C++/Fortran statements which helps you identify problems quickly).
  4. gfortran is a fortran compiler that is available through gcc (for Macs visit hpc.sourceforge.net) . You can install it on your laptop if you want to program when no internet connection is available. If you have installed a recent version of gcc, chances are that gfortran is already available on your system. A final note is that if the software is installed in a non-standard directory tree, e.g. /usr/local, then you have to make sure that the executables are in your path, and the shared libraries are in your load library paths. The following are the commands you have to include in your .bashrc file:
    #modify the PATH to find the new executable
     export PATH=$PATH:/home/username/softdir/bin
    #modify the LD_LIBRARY_PATH to find the new shared libraries
     export LD_LIBRARY_PATH=$PATH:/home/username/softdir/lib
    #modify the MANPATH to find the new manual pages
     export MANPATH=$MANPATH:/home/username/softdir/man
    
    Here /home/username/softdir is the root directory where the softwarre has been installed. At the command line issue the command source .bashrc.
  5. Cygwin is an application interface and a collection of tools which provide a Linux environment and commands for WINDOWS. In addition it has a free implementation of the X Window system which will allow you seemless access to the UNIX server. Installion is pretty easy but can take a little while depending on available bandwidth. Although the unix commands are useful, it is Cygwin/X that is the most valuable to allow you access to Unix/Linux servers; make sure it is installed.
  6. Octave is a high-level language, primarily intended for numerical computations and visualization. Its language is mostly compatible with Matlab. It is available for all platforms (Windows, MacOS, and Linux).
  7. Debugging for weeding out programming bugs.
  8. make for compiling executables with multiple source files. A template Makefile can be downloaded. It is useful resource to avoid long typing, remember the names of compiler options and libraries. It is best to "save as" as opposed to clicking as the latter mangles the tab and line endings.
  9. printpdf is a useful bash script to print code to a pdf-file for later viewing or printing. The script invoke the enscript utility and passes it options to print in landscape format with 2 pages of output per page. enscript writes a postscript version of the file and a separate program "ps2pdf" transforms it to pdf format.