Simple Program Construction in Fortran

Simple Program Construction in Fortran » History and Introduction to Fortran

Amateur and professional programmers alike have many languages at their disposal for use in coding something like our previous “what to wear” program. In many cases one might choose an object-oriented language such as Java to write said program, however here we will focus on the lesser-known Fortran, still a favorite among scientists and engineers for good reason.

IBM machine with man and woman

Fortran (Formula Translation) was developed in 1954 by a diverse team at IBM, in an effort to simplify the programming process so that others aside from computer programming experts could perform complex calculations without needing to directly modify assembly or machine language (0s and 1s). Or, as its founder John Backus would say, “I was lazy, and didn’t like writing programs, so I started work on a system to make it easier to write programs.”

Fortran achieved this first-ever, high-level programming interface through the creation of a compiler - a program, or set of programs, that turns algebra and logical expression-based, high-level code into assembly language. Fortran effectively reduced the number of necessary programming statements by a factor of 20 by having the compiler do the work of laborious tasks such as allocating memory to specific program operations. Prior machine language programs were written to a specific computer, but a Fortran program could be run on any computer with the Fortran compiler installed - and thus, an industry heavyweight was born.

Titan Supercomputer

Fortran still reigns as the language of choice in computationally-expensive fields such as computational fluid dynamics (weather and climate models!), finite element analysis, economic modeling and DNA sequencing. It is used to benchmark the speed and computing power of the world’s fastest supercomputers.

In Fortran, your code will be in a logical algorithmic format just like your pseudocode. It just needs to be translated from pseudocode to Fortran code proper.

1. Use app to gather forecast high temperature for today.
2. Use calendar to find whether it is a workday or weekend.
3. Catalog your entire wardrobe into short-sleeved, long-sleeved dress, t-shirt,
and long-sleeved tops, and shorts, pants, athletic shorts, and pajama pants. 4. If today is a weekend a. If the forecast temperature is at or above 70°F, select a t-shirt,
else select a long-sleeved top. b. If the forecast temperature is above 60°F, select athletic shorts,
else select pajama pants. else (today is a workday) a. If the forecast temperature is at or above 70°F, select a short-sleeved top,
else select a long-sleeved dress top. b. If the forecast temperature is above 60°F, select shorts,
else select pants. 5. Subtract today’s outfit from your clean wardrobe. 6. Look through clean clothes a. If no more clean clothes left i. Print out an alert to do laundry.

We have been using specific language to describe some of our pseudocode from the beginning of this lesson. You may not have noticed it, but the words will become a clear reminder of what the Fortran code will actually look like as we move forward. Pseudocode is meant to be an interface between human-readable language and computer-readable language. Because of this, we often intermix the code and the pseudocode. As you learn more about coding in your chosen language, your pseudocode will start to look more like code, until eventually, you may not need to pseudocode anymore.

All Fortran programs must start and end the same way, using “program”, “implicit none” and “end”:

program pick_outfit
implicit none

!this line is a comment, and will be ignored by the compiler

end

The “program” line indicates to the compiler that the code starts here and the name of the program is: “pick_outfit”. “implicit none” won’t mean anything to you right now, but it is suggested best practice that avoids some implied syntax from early versions of Fortran. The “end” line is the last line of the code that will be executed. Everything after the “end” line will be ignored.

In Fortran 90 and later, code can start anywhere on the line, with no indentation required. Note that in Fortran 77 and earlier, you will always indent code by 7 spaces before writing any executable code. There are only a few reasons to write anything in the first 6 columns of a Fortran 77 or earlier code, comments, continuations, and labels, but only comments will be covered herein. Comments start with a “!”. If you write a “!” in column one of the code, it is a comment and will be ignored by the compiler. It helps other programmers and users to read the code better and gives more detail when complex code may be too hard to understand on initial pass.

Starting just like we did with the pseudocode, let’s show you how to gather some weather data for use in the Fortran code.

Simple Program Construction in Fortran » Declaring Variables

The only weather data needed for this program is the forecast high temperature. The high temperature is a number that could be an integer or a real number depending on your forecast information. Most websites and apps will report just the rounded value of the temperature. In Fortran, it makes a difference what type of number you will be given as each type of number would require a different variable definition. In Fortran, the numeric variable types are:

  • Integer,
  • Real, and
  • Double

Integers have no decimal values, reals have as much as 7 digits of precision, and doubles have up to 15 digits of precision. In computer models of atmospheric processes, the higher precision is important for our temperature, so these values will be stored as real or double.

Question 1 of 1

Which variable type should we use to define our forecast high temperature?

The correct answer is a.

Because the forecast high temperature is generally displayed as the integer value, using the integer type is best.

This may seem like a moot point, but this is part of the reason why Fortran code is still some of the fastest code out there for computing on large datasets. Hopefully, this burden of knowing our variable types upfront can be appreciated when the code runs faster than it would in other languages.

program pick_outfit
implicit none

integer high_temp

end
Please make a selection.

Simple Program Construction in Fortran » Making Decisions

program pick_outfit
implicit none

integer high_temp
logical weekend

end

Neither of the variables we have declared are assigned values yet. Those two variables can be set with the same style of code:

! temperature in degrees Fahrenheit
high_temp = 64
! logical when weekend is .TRUE., it is a weekend, 
! when weekend is .FALSE., it is a workday
weekend = .TRUE.

The single equals sign (=) is used to assign values to variables of any type. Based on the code above it is a weekend with a forecast high temperature of 64˚F.

Let’s use that data to start making some of the decisions in our program.

The pseudocode we are trying to codify is below:

1. If today is a weekend
       a. If the forecast temperature is at or above 70˚F, select a t-shirt,
else select a long-sleeved top. b. If the forecast temperature is above 60˚F, select athletic shorts,
else select pajama pants. else (today is a workday) a. If the forecast temperature is at or above 70˚F, select a short-sleeved top,
else select a long-sleeved dress top. b. If the forecast temperature is above 60˚F, select shorts,
else select pants.

To codify these, we need to understand the Fortran if-else statement. These pseudocode snippets look a lot like what we are going to end up using:

if (logical expression) then
     statement
else
     statement
end if

The code bits that are bold and italicized are to be replaced by your code. The other code is the definition of an if-else statement. The logical expression is a test of a logical statement. For us, this would be:

if (weekend .EQ. .TRUE.) then

else

end if

Notice the difference between assigning values to variables and testing a logical expression. In Fortran 77 and below, logical expressions can be evaluated with:

  • .EQ. is equals
  • .NE. is not equal to
  • .GT. is greater than
  • .GE. is greater than or equal to
  • .LT. is less than
  • .LE. is less than or equal to

In Fortran 90 and above, logical expressions can be evaluated with:

  • = is equals
  • != is not equal to
  • > is greater than
  • >= is greater than or equal to
  • < is less than
  • <= is less than or equal to

The program will enter into the else portion of the if-else statement when the if’s logical expression evaluates to .FALSE.

Question 1 of 1

Can you rewrite the if-else statement below to be equivalent but use a different logical expression?


There is only one other way to write the code using the weekend logical variable and make the if portion of the if-else statement evaluate to a weekend.

if (weekend .NE. .FALSE.) then
else
end if
    

Simple Program Construction in Fortran » Making Decisions » Coding Your Own Decisions

Question 1 of 1

Based on the following pseudocode, type into the code in the box to add any one of the decision points for tops or bottoms.

if today is a weekend
     if the forecast temperature is at or above 70˚F...
     if the forecast temperature is above 60˚F... 
else (today is a workday)
     if the forecast temperature is at or above 70˚F...
     if the forecast temperature is above 60˚F...


All four if-else statements are written in the code below. You only had to write one of them, but they all follow the same general structure. Aside from starting on the 7th column for executable information, the indentation in our code doesn’t matter. We have indented further to help readability of nested statements. We are currently using 5 further spaces inward for indentation of nested code. The number of spaces is up to you.

program pick_outfit
implicit none

integer high_temp
logical weekend

high_temp = 64
weekend = .TRUE.

if (weekend .EQ. .TRUE.) then
     if (high_temp .GE. 70) then
     else
     end if
     if (high_temp .GE. 60) then
     else
     end if
else
     if (high_temp .GE. 70) then
     else
     end if
     if (high_temp .GE. 60) then
     else
     end if
end if

end

Simple Program Construction in Fortran » Arrays of Clothing

You may have thought to yourself that you might want to just declare a character variable for top and bottom. This would work just fine, but then you would have to interpret that top or bottom to figure out what to subtract from your wardrobe. That is an extra step. And extra steps are more computing time and more room for mistakes in your code. So there has to be another way to do this that will work.

Let’s start with our wardrobe though.

Catalog your entire wardrobe into short-sleeved, long-sleeved dress, t-shirt,
and long-sleeved tops, and shorts, pants, athletic shorts, and pajama pants.

Question 1 of 2

Based on our pseudocode, how many different types of clothing do we have to select from?

The correct answer is d.

We have eight different types of clothing:

  • Short-sleeved tops
  • Long-sleeved dress tops
  • T-shirts
  • Long-sleeved tops
  • Shorts
  • Pants
  • Athletic shorts
  • Pajama pants

As was stated before, we don’t need to track individual pieces of clothing, just the number of each type of clothing. This is part of how we can convert our wardrobe into numbers.

Please make a selection.

The other part of this that makes things even easier is to make is what is called an array. We can make an array of clothing that is we’ll call your wardrobe. It can be conceptualized much like we did before with the table of wardrobe data:

Tops

Bottoms

Short-sleeved

Long-sleeved dress

T-shirt

Long-sleeved

Shorts

Pants

Athletic shorts

Pajama pants

6

3

3

2

5

5

3

2

We don’t need all of this information in the array, just the numbers. Let’s declare this array much like we would a variable. It even gets declared at the top of the program before any lines of code get executed.

integer wardrobe(8)

When declaring an array, you have to assign it to one of the basic variable types and give it a length (called the number of elements). In this case, we have an integer because you can only have whole pieces of clothing, and it has 8 elements inside of it to match the number of types of clothing we have. You can think of this 8 element array (wardrobe) just like the above table. In fact, you can assign values to the array, in a very similar fashion to this table.

integer wardrobe(8)

!The first four elements in the array are for tops
!wardrobe(1) = short-sleeved top
!wardrobe(2) = long-sleeved dress top
!wardrobe(3) = t-shirt
!wardrobe(4) = long-sleeved top

!the last four elements in the array are for bottoms
!wardrobe(5) = shorts
!wardrobe(6) = pants
!wardrobe(7) = athletic shorts
!wardrobe(8) = pajama pants

wardrobe = (/ 6, 3, 3, 2, 5, 5, 3, 2 /)

To get an individual value from the array, you can call it by its element number or index. To find out how many athletic shorts are clean in your wardrobe right now, you could call:

integer clean_athletic_shorts

clean_athletic_shorts = wardrobe(7)

If you printed out the value of clean_athletic_shorts, it would be 3.

All Fortran arrays (unless otherwise identified) will start from an index of 1 and end at the index given in the declaration of the array (8).

Question 2 of 2

Based on the wardrobe definition below, fill in the following statements.

Tops

Bottoms

Short-sleeved

Long-sleeved dress

T-shirt

Long-sleeved

Shorts

Pants

Athletic shorts

Pajama pants

6

3

3

2

5

5

3

2

a) To find out how many clean t-shirts we have, we would call wardrobe with an index of .
b) If you had selected an outfit that consisted of a long-sleeved top and a pair of athletic shorts, you could reassign the entire array to have , , , , , , ,
c) If you wanted to subtract a pair of pants from your wardrobe, you could make a call on wardrobe with index and assign its new value to .

“wardrobe(3)” would give you the number of clean t-shirts you have.

wardrobe = (/ 6, 3, 3, 1, 5, 5, 2, 2 /)

wardrobe(6) = 4

That last example did the job, but wasn’t it painful to get through? Isn’t there a better way to make the wardrobe get smaller based on your outfit?

Simple Program Construction in Fortran » Arithmetic for Outfits

All the different types of arithmetic are available in Fortran.

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • ** (power)
  • SQRT(x) (square root)

All of these types of arithmetic functions can be run on any numeric variable type.

high_temp_2times = high_temp + high_temp
high_temp_times3 = high_temp * 3
high_temp_in_C = (high_temp - 32) * 5/9

Arithmetic can even be run on arrays. This is where the big payoff comes from using an array for our wardrobe and also setting an array for our outfit.

wardrobe = (/ 6, 3, 3, 2, 5, 5, 3, 2 /)
outfit = (/ 0, 0, 0, 1, 0, 0, 1, 0 /)

wardrobe = wardrobe - outfit

Because these two arrays have the same dimensions, the math can work out great! If you printed out the clean wardrobe after these commands, you would find it would look like this:

(6, 3, 3, 1, 5, 5, 2, 2)

This is why we wanted to make sure that the outfit was numeric. It just makes life that much easier.

Here’s our code now:

program pick_outfit
implicit none

integer high_temp
logical weekend
integer wardrobe(8)
integer outfit(8)

!The first four elements in the array are for tops
!wardrobe(1) = short-sleeved top
!wardrobe(2) = long-sleeved dress top
!wardrobe(3) = t-shirt
!wardrobe(4) = long-sleeved top

!the last four elements in the array are for bottoms
!wardrobe(5) = shorts
!wardrobe(6) = pants
!wardrobe(7) = athletic shorts
!wardrobe(8) = pajama pants

high_temp = 64
weekend = .TRUE.
wardrobe = (/ 6, 3, 3, 2, 5, 5, 3, 2 /)
!outfit not set yet, so just setting it to all zeroes for safety
outfit = (/ 0, 0, 0, 0, 0, 0, 0, 0 /)

if (weekend .EQ. .TRUE.) then
     if (high_temp .GE. 70) then
          outfit(3) = 1
     else
          outfit(4) = 1
     end if
     if (high_temp .GE. 60) then
          outfit(7) = 1
     else
          outfit(8) = 1
     end if
else
     if (high_temp .GE. 70) then
          outfit(1) = 1
     else
          outfit(2) = 1
     end if
     if (high_temp .GE. 60) then
          outfit(5) = 1
     else
          outfit(6) = 1
     end if
end if

wardrobe = wardrobe - outfit

end

So we are so close to having an outfit. What are we still missing? We can’t see the outfit that our program chose yet. We need to print it out onto our screen so we can see what the program decided.

Simple Program Construction in Fortran » Print Out Outfit

Printing out variables and arrays is a very simple process. You can use the print command. If we wanted to print out our outfit, we could just print out our outfit array like so:

print *, outfit

Question 1 of 1

Based on our current code, what would you expect the output to be from the new print statement?

program pick_outfit
implicit none

integer high_temp
logical weekend
integer wardrobe(8)
integer outfit(8)

!The first four elements in the array are for tops
!wardrobe(1) = short-sleeved top
!wardrobe(2) = long-sleeved dress top
!wardrobe(3) = t-shirt
!wardrobe(4) = long-sleeved top

!the last four elements in the array are for bottoms
!wardrobe(5) = shorts
!wardrobe(6) = pants
!wardrobe(7) = athletic shorts
!wardrobe(8) = pajama pants

high_temp = 64
weekend = .TRUE.
wardrobe = (/ 6, 3, 3, 2, 5, 5, 3, 2 /)
!outfit not set yet, so just setting it to all zeroes for safety
outfit = (/ 0, 0, 0, 0, 0, 0, 0, 0 /)

if (weekend .EQ. .TRUE.) then
     if (high_temp .GE. 70) then
          outfit(3) = 1
     else
          outfit(4) = 1
     end if
     if (high_temp .GE. 60) then
          outfit(7) = 1
     else
          outfit(8) = 1
     end if
else
     if (high_temp .GE. 70) then
          outfit(1) = 1
     else
          outfit(2) = 1
     end if
     if (high_temp .GE. 60) then
          outfit(5) = 1
     else
          outfit(6) = 1
     end if
end if

print *, outfit

end

The correct answer is c.

Hope you weren’t expecting to see a list of clothing options come out of this. We left that out for now as the math was more important to us than the actual name of the clothing pieces.

So this isn’t the most useful Fortran print statement for us to know our outfit for the day. There are a few ways we could print out the human-readable option for our outfit. The simplest right now looks like this:


if (outfit(1) .EQ. 1) then
     print *, “Short-sleeved Top“
if (outfit(2) .EQ. 1) then
     print *, “Long-sleeved Dress Top“
if (outfit(3) .EQ. 1) then
     print *, “T-shirt“
if (outfit(4) .EQ. 1) then
     print *, “Long-sleeved Top“
if (outfit(5) .EQ. 1) then
     print *, “Shorts”
if (outfit(6) .EQ. 1) then
     print *, “Pants”
if (outfit(7) .EQ. 1) then
     print *, “Athletic Shorts”
if (outfit(8) .EQ. 1) then
     print *, “Pajama Pants

Isn’t there a simpler way to do that though? That just seems like a lot of coding to make it print out two pieces of clothing.

Please make a selection.

There is a another way!

Simple Program Construction in Fortran » Looping Through Arrays

It would be significantly easier to make an array of the pieces of clothing as character variables as well as keeping the wardrobe and outfits available in the integer formats.

character clothing_types(8)
clothing_types = (/ Short-sleeved top, Long-sleeved dress top, T-shirt, Long-sleeved top,
Shorts, Pants, Athletic shorts, Pajama pants /)

Then since the clothing_types, wardrobe, and outfit all have the same number of elements (8), we can loop through one of them and use the element index as a way to call the other arrays data.

However, first, we need to understand looping. Loops are iterations of the exact same process either until a criteria is met, while a criteria is true, or for all of the elements in an array. In Fortran, the most common type of looping structure is

do var = start, end
     expression
end do

where the var in this case would be an iterator that counts from start to end.

Question 1 of 2

If we wanted to loop through our outfit array, what would our start and end values be?

a) Start
b) End

Because our outfit array has 8 elements and Fortran arrays, unless otherwise specified, start at 1, we would start at 1 and end at 8.

Challenge Question

Write a do loop to check your clean wardrobe for any clothing types that you have run out of since selecting your outfit. If you find any clothing types that you have run out of, print out an alert for yourself to do laundry.


do j = 1, 8
     if (wardrobe(j) .EQ. 0) then
          print *, “Do laundry today!”
     end if
end do

Notice that the print statement is wrapped in double quotes (“). This is a way to tell the program that these words should not be interpreted as code, but just output “as is”.

Simple Program Construction in Fortran » Running Subroutines

Did you notice that we didn’t really gather any weather information? We just put it in there magically. We don’t have the skills to be able to gather that data in this lesson, but let’s suggest a structure useful for making this happen. Because gathering weather data can be thought of as a different process than picking out clothing, we can separate the programs as two separate subroutines.

So let’s think of the weather data grab as a subroutine. We aren’t going to code the weather data grab subroutine, we will just assume the subprogram returns the high temperature variable to the main program. The high temperature variable is “passed” into the subroutine on the “call” line below.

Main Program Content

integer high_temp

print *, “Before subroutine -- High temperature forecast is: “
print *, high_temp

call weather_data_grab(high_temp)

print *, “After subroutine -- High temperature forecast is: “
print *, high_temp

Subroutine Content

subroutine weather_data_grab (htemp)
integer htemp
   .
   .            
   .            
return
end

The subroutine has to be named the same as the call from the main program. However, it is best to name the variable differently in the subroutine than in the main program as there can be confusion between the two if they are named exactly the same way. The important part of the subroutine is the return statement which ensures that the main program gets the passed in variables back. This means the subroutine can change the value of the passed in variables and return them to the main program with different values than what they started as.

Question 1 of 1

If the above code was our entire program, how many lines of output would be printed to screen?

The correct answer is d.

There will only be 4 lines of output from the program. You may have been tempted to say there were only 3 because one of the lines won’t “output” anything visibly, but in reality it is outputting a line break, you just can’t “see” it. Here is what the output will look like.

Before subroutine -- High temperature forecast is:

After subroutine -- High temperature forecast is:
64
Please make a selection.

Subroutines are common in larger programs as each “problem” will be separated into separate subroutines. So knowing how subroutines are called and variables are returned helps you know how to navigate around the subroutines to find where your data is flowing.

In our Fortran program, we can now replace the “high_temp = 64” line with the subroutine call from above like so:

program pick_outfit
implicit none

integer high_temp
logical weekend
integer wardrobe(8)
integer outfit(8)
integer i, j

!The first four elements in the array are for tops
!wardrobe(1) = short-sleeved top
!wardrobe(2) = long-sleeved dress top
!wardrobe(3) = t-shirt
!wardrobe(4) = long-sleeved top

!the last four elements in the array are for bottoms
!wardrobe(5) = shorts
!wardrobe(6) = pants
!wardrobe(7) = athletic shorts
!wardrobe(8) = pajama pants

call weather_data_grab(high_temp)

weekend = .TRUE.
wardrobe = (/ 6, 3, 3, 2, 5, 5, 3, 2 /)
!outfit not set yet, so just setting it to all zeroes for safety
outfit = (/ 0, 0, 0, 0, 0, 0, 0, 0 /)

if (weekend .EQ. .TRUE.) then
     if (high_temp .GE. 70) then
          outfit(3) = 1
     else
          outfit(4) = 1
     end if
     if (high_temp .GE. 60) then
          outfit(7) = 1
     else
          outfit(8) = 1
     end if
else
     if (high_temp .GE. 70) then
          outfit(1) = 1
     else
          outfit(2) = 1
     end if
     if (high_temp .GE. 60) then
          outfit(5) = 1
     else
          outfit(6) = 1
     end if
end if

wardrobe = wardrobe - outfit

do i = 1, 8
     if (outfit(i) .EQ. 1) then
          print *, clothing_type(i)
     end if
end do

do j = 1, 8
     if (wardrobe(j) .EQ. 0) then
          print *, “Do laundry today!”
     end if
end do

end

Simple Program Construction in Fortran » Completed Fortran Program

That’s all the code we would need to run our program for a single day. We could wrap our entire program in a do loop that would keep iterating from day to day to start the outfit selection process over and over again, but that is a little outside of the scope of our programming abilities. So, here is the entire program written in Fortran and its equivalent in pseudocode.

Fortran Code Content

full

Pseudocode Content

full

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.

Fortran and CESM » Namelists

We mentioned namelists before, and now is our chance to discuss them. Fortran is a compiled programming language. This means all of your code is directly built into a machine language. This is one of the reasons that Fortran is faster than some other languages. However, once your program is compiled, if you change any code, you will have to recompile the entire program, which could take time for large code. When you don’t want to have to recompile code all the time, you can write your initial code to have choices built in to it that can be controlled by what is called a namelist.

Let’s give you an example because this might be hard to think about without one. Let’s assume we have some Fortran code that is written for BOTH Fahrenheit and Celsius, but you only want to output in one of those temperature scales.

if (namelist_Fahrenheit .EQ. .TRUE.) then
     temp = ...
     expressions
else 
     temp = ...
     expressions
end if

print *, temp

After compiling this code into test_code.exe, you would have to use a namelist file to control whether your output was in Fahrenheit or Celsius. You would do that with a command run from the Unix command line like so:

test_code.exe < namelist.in

The contents of namelist.in would look something like:

& namelist.in
      namelist_Fahrenheit = .TRUE.
      namelist_start_hour = 12
      namelist_pi = 3.1415
      namelist_output_filename = ‘output.nc’
/

In this example we started all of the variable names with “namelist_”, but this is not a requirement.

Note that the namelist variables don’t need to be declared. They are declared by the syntax used to set them.

  • Logicals are set to “.TRUE.” or “.FALSE.” (without the quotes)
  • Integers are set to numeric values without decimal places
  • Reals are set to numeric values with decimal places
  • Characters are set to strings wrapped in single quotes like ‘output.nc‘

These namelist variables decide how the compiled code is then run, which saves us a lot of time compiling. In the CESM model, namelists are used to help define the input and output files, the time steps, parameterizations choices, and other key characteristics for running the model.

Back to Top