Fortran

Guide To Learn

Designing the aircraft dashboard

Fortran’s formatting rules are quite tedious, so let’s learn them by tackling a practical problem. Consider the following scenario. You’re an aircraft flight instruments engineer, and you’re tasked with implementing the on-screen display of several key parameters related to the aircraft’s in-flight state. The design team has instructed you to display only certain parameters and to a limited precision:

  • Latitude and longitude (real) –With a precision up to 0.00001 degree (about a meter on the equator)
  • Altitude in meters –With a precision up to 0.1 m
  • Engine load in percentage –Represented with integer values between 0 and 100 (There are four engines total, so this parameter is an array of four elements. It should always be displayed with all three digits.)
  • Airborne (logical) status –To show whether the aircraft is airborne. (This parameter will be displayed as T or F depending on the aircraft state.)

Furthermore, each parameter should be separated by at least two empty spaces to make them easier to read. Note that this is a much simplified version of flight instruments, even for the most rudimentary aircraft. However, it’s all we need from an example to learn how formatting data works.

In our implementation, we’ll assume that the plane is flying over Stockholm, Sweden, at an altitude of close to 12 km, with a velocity of 267.5 m/s. We’ll hardcode the example values in the declaration for simplicity. In the production code, the data would come from some external subroutine, like get_aircraft_parameters or something similar. We don’t have to worry about how that works, and we’ll focus on just formatting the data for readable display. Let’s begin with the full program, as shown in the following listing, which we’ll break down step by step.

Listing 6.5 Formatting airplane state parameters for display on the dashboard

program dashboard
  use iso_fortran_env, only: dash => output_unit            ❶
  implicit none
 
  real :: lat = 59.329444, lon = 18.068611, alt = 11678.3   ❷
  integer :: eng(4) = [96, 96, 95, 97]                      ❷
  logical :: airborne = .true.                              ❷
 
  character(len=:), allocatable :: dashfmt                  ❸
 
  dashfmt = '(2(f9.5, 2x), f7.1, 2x, 4(i3.3, 2x), l)'       ❹
  write (dash, dashfmt) lat, lon, alt, eng, airborne        ❺
 
end program dashboard

❶ Imports standard output unit as dash

❷ Sets parameter values

❸ Declares the format string as an allocatable character variable

❹ Assigns the format string value

❺ Writes parameter values to the dashboard using given format

This program is quite simple. We first declare and initialize the aircraft parameters, then we assign the formatting string to dashfmt, and finally we apply dashfmt in the write statement to format the data to our liking. The key question we seek to answer is, What value of dashfmt will correctly display the flight parameters as per the design specification sheet? The result will look like this:

59.32944   18.06861  11678.3  096  096  095  097  T

Though I’ve already given you the answer in listing 6.5, we don’t yet understand how this formatting string works. We’ll dive into the details of its syntax in the following subsection.

Designing the aircraft dashboard

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top