Introduction to Fortran

Fortran and CESM

The Community Earth System Model (CESM) runs mostly on Fortran code. You may need to edit some of its code directly to make different calculations or output different variables that aren’t readily available from the original source code. You may also need to pass the Fortran code some namelist variables which we’ll talk about later. Having skills in Fortran can help you code the output you are looking for. Most of what you need to know to understand and modify the Fortran code within CESM you have already learned in this lesson.

Let’s look at an example and try to identify and understand what is happening. This is real code from the CESM model.

Question 1 of 2

What is the following code snippet doing? Pseudocode your answer on paper and compare it to the following to select which is the most accurate.

 integer ncol, pcols  real precip(pcols) real snowab(pcols) real precab(pcols) real cldmax(pcols)  do i = 1,ncol      precip(i) = 0.0      precab(i) = 0.0      snowab(i) = 0.0      cldmax(i) = 0.0 end do 

The correct answer is a.

Two integers and 4 real arrays with pcols elements are declared, then we loop through each array and set all of the elements to 0.0 one at a time.

This is a really good skill to hone over time. It becomes easier the more you do it, and you will get much quicker reading code and transforming it into human language. Let’s try another example.

Please make a selection.

Question 2 of 2

Based on the following code snippet, declare the necessary variables and arrays. You can assume that the code you write would be placed above this code. You do not need to set your variables and arrays, just declare them.

 do k = 1, rows      do i = 1, cols           cldm(i) = cldn(i,k)           if(cldm(i) > mincld) then                icwc(i) = cwat(i,k) / cldm(i)           else                icwc(i) = 0.0           end if      end do end do 

 !integer variables integer i, k, rows, cols !real variables real mincld !real arrays real cldm(cols), cldn(cols, rows), icwc(cols) 

It is good practice to separate variables from arrays for readability. If necessary, you can declare similar variables on the same line separated by commas. From the code within the question, one cannot truly determine the variable type of "mincld" aside from it has to be numeric.

Looking back to the question code, let's see what this code is doing. The first two lines tell you that we will iterate through a two dimensional array since there is a nested do loop. The first loop will go from 1 to rows and the second loop will go from 1 to cols. We'll set cldm(i) to equal cldn(i,k). Then if cldm(i) is greater than mincld, icwc(i) needs to be set to cwat(i,k)/cldm(i). Else, icwc(i) should be set to 0.0.