Fortran

Guide To Learn

Results and complete program

Finally, we approach the home stretch. Here’s how the output of our program appears:

City | Temp. | Humidity | Wind  | Clear | Total
Code | Score | Score    | Score | Score | Score
-----+-------+----------+-------+-------+------
EGLL   0.02     0.04     1.00     0.97     0.51
LAX    0.45     0.14     0.72     1.00     0.58
LYBE   0.15     0.13     0.80     0.05     0.28
MIA    1.00     0.00     0.77     0.86     0.66
MMMX   0.41     0.57     0.68     0.47     0.53
NYC    0.10     0.13     0.00     0.41     0.16
OIII   0.55     1.00     0.63     0.41     0.65
SEA    0.00     0.10     0.86     0.58     0.39
SKBO   0.14     0.01     0.50     0.00     0.16
ZGSZ   0.90     0.03     0.80     0.27     0.50

From this table, we can see which city has the highest score for each respective variable. For example, Miami (MIA) is the warmest but also the most humid city in the group, yielding scores of 1 and 0 for temperature and humidity, respectively. Tehran, Iran (OIII), has the lowest humidity, yielding the best humidity score, and gets all around decent scores for all other variables. Despite the high humidity, the overall winner is Miami, with a total score of 0.66, followed closely by Tehran, with a total score of 0.65. New York City (NYC) and Bogota, Colombia (SKBO), share the bottom place on the scoreboard, with a score of 0.16. New York City has the lowest score for wind speed and is overall cold relative to the other cities in the group. Bogota, on the other hand, is predominantly cloudy and humid.

To compute these scores, we’ll normalize the values of each variable such that the lowest value between all stations corresponds to a zero, and the highest value corresponds to a one. To normalize values means to bring them to some common scale. In this exercise, we normalize all values to a scale from 0 to 1. For example, a value of 20 in the range 0 to 50 corresponds to a normalized value of 0.4. The choice here for a normalized range is arbitrary and my personal choice. You could pick any other range, as long as the score that you build makes sense to you and your users.

The following listing shows the complete main program (src/weather_average .f 90), including the code that computes the scores for each city and variable.

Listing 9.6 The complete program to compute climate scores for 10 cities

program weather_average
 
  use mod_arrays, only: denan, normalize                      ❶
  use mod_average, only: average                              ❷
  use mod_weather_data, only: weather_data                    ❸
  implicit none
 
  type(weather_data) :: dataset                               ❸
  character(len=4), parameter :: cities(*) = &                ❹
    ['EGLL', 'LAX ', 'LYBE', 'MIA ', 'MMMX', &                ❹
     'NYC ', 'OIII', 'SEA ', 'SKBO', 'ZGSZ']                  ❹
  integer :: n
  integer, parameter :: nm = size(cities)
  real :: temperature(nm), humidity(nm), &                    ❺
          wind_speed(nm), clear_sky(nm)                       ❺
  real :: temperature_score(nm), humidity_score(nm), &        ❻
          wind_score(nm), clear_score(nm), &                  ❻
          total_score(nm)                                     ❻
 
  do n = 1, nm
    dataset = weather_data('data/processed/' // trim(cities(n)) // '.csv')
    temperature(n) = &                                        ❼
      average(denan(dataset % temperature))                   ❼
    humidity(n) = average(denan(dataset % humidity))          ❼
    wind_speed(n) = average(denan(dataset % wind_speed))      ❼
    clear_sky(n) = average(dataset % clear_sky)
  end do
 
  temperature_score = normalize(temperature)                  ❽
  humidity_score = 1 - normalize(humidity)                    ❽
  wind_score = normalize(wind_speed)                          ❽
  clear_score = normalize(clear_sky)                          ❽
  total_score = (temperature_score + humidity_score &         ❾
    + wind_score + clear_score) / 4                           ❾
 
  print *, 'City | Temp. | Humidity | Wind  | Clear | Total'
  print *, 'Code | Score | Score    | Score | Score | Score'
  print *, '-----+-------+----------+-------+-------+------'
  do n = 1, nm
    write(*,'(1x, a4, 3x, 5(f4.2, 5x))') cities(n), temperature_score(n),&
      humidity_score(n), wind_score(n), clear_score(n), total_score(n)
  end do
 
end program weather_average

❶ Imports utility functions to work on arrays

❷ Imports the generic “average”

❸ Derived type for reading the data from files

❹ Station codes to process

❺ Arrays to store the averages

❻ Arrays to store the scores

❼ Denans then averages each weather parameter.

❽ Computing the scores for each variable

❾ Total score

We take several steps in the main program. First, we loop through each station code, read the data from the file into the weather_data type, remove NaNs, and average the arrays. We then normalize the values to the range from 0 to 1–these are our scores. The total score is calculated as the arithmetic average of individual scores. Finally, we print the score table to the standard output.

Results and complete program

Leave a Reply

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

Scroll to top