Advertisement
rric

ongoing_global_crisis

Nov 27th, 2023 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.44 KB | None | 0 0
  1. # Visualizes temperature data from the HISTALP project,
  2. # see http://www.zamg.ac.at/histalp/dataset/station/csv.php
  3. # Copyright 2022, 2023 Roland Richter                    [Processing.py]
  4.  
  5.  
  6. from __future__ import division, print_function
  7.  
  8. def read_histalp_data(file_name, factor = 1.0):
  9.     """Read a file in HISTALP format, and return a dictionary (keys: 1815, 2022,
  10.        ...) of dictionaries (keys: 'jan', 'feb', ...).
  11.  
  12.    Example:
  13.    hist_data = read_histalp_data("HISTALP_AT_LIN_T01_1816_2022.csv")
  14.    mean_tmpr = hist_data[1974]['mar']   # mean temperature of March 1974
  15.  
  16.    Arguments:
  17.    file_name -- the name of the HISTALP file
  18.    factor -- number to multiply all values with; only necessary for temperature (0.1)
  19.    """
  20.     csv_lines = loadStrings(file_name) # Processing.py function to read lines from a file
  21.     hist_data = {}
  22.     is_first_line = True
  23.    
  24.     for csv_line in csv_lines:
  25.         # ignore all lines starting with a '#' character, as these are comments
  26.         if csv_line.startswith('#'):
  27.             pass
  28.         else:
  29.             csv_entries = csv_line.strip().split(';')
  30.             # the first non-comment line contains the column headers
  31.             if is_first_line:
  32.                 column_header = csv_entries
  33.                 is_first_line = False
  34.             else:
  35.                 year = int(csv_entries[0])
  36.                 year_dict = {}
  37.                 # go through all entries of a line (except the first);
  38.                 # store the value (multiplied with the factor),
  39.                 # or 'nan' ('not a number') to indicate missing values
  40.                 for k in range(1, len(csv_entries)):
  41.                     entry = csv_entries[k]
  42.                     header = column_header[k]
  43.                     if entry != "999999":
  44.                         year_dict[header] = factor * float(entry)
  45.                     else:
  46.                         year_dict[header] = "nan"
  47.  
  48.                 hist_data[year] = year_dict
  49.  
  50.     return hist_data
  51.    
  52. # Mean temperature for Linz is available for 207 years ...
  53. FILE_NAME = "HISTALP_AT_LIN_T01_1816_2022.csv"
  54. FIRST_YEAR = 1816
  55. LAST_YEAR = 2022
  56.  
  57.  
  58. def setup():
  59.     # Let each year be 4 pixels wide; 207 * 4 = 828
  60.     size(828, 550)
  61.    
  62.     global histalp_data, FILE_NAME, FIRST_YEAR, LAST_YEAR, color_of
  63.    
  64.     histalp_data = read_histalp_data(FILE_NAME, 0.1)
  65.     min_tmpr = max_tmpr = histalp_data[FIRST_YEAR]['jan-dec']
  66.  
  67.     for yr in range(FIRST_YEAR+1, LAST_YEAR+1):
  68.         tmpr = histalp_data[yr]['jan-dec']
  69.         if tmpr < min_tmpr:
  70.             min_tmpr = tmpr
  71.         if tmpr > max_tmpr:
  72.             max_tmpr = tmpr
  73.    
  74.     print("Temperature range:", min_tmpr, "to", max_tmpr)
  75.    
  76.     color_of = {}
  77.     cold = color(113, 166, 210)  # Iceberg
  78.     warm = color(207,  16,  32)  # Lava
  79.  
  80.     for yr in range(FIRST_YEAR, LAST_YEAR+1):
  81.         mean_tmpr = histalp_data[yr]['jan-dec']
  82.         amount = (mean_tmpr-min_tmpr) / (max_tmpr-min_tmpr)
  83.         color_of[yr] = lerpColor(cold, warm, amount)
  84.    
  85.     textSize(16)
  86.    
  87.  
  88. def draw():
  89.     background(0)
  90.    
  91.     global histalp_data, FIRST_YEAR, LAST_YEAR, color_of
  92.    
  93.     mouse_yr = int(round(lerp(FIRST_YEAR, LAST_YEAR, mouseX/float(width))))
  94.              
  95.     for x in range(0, width):
  96.         yr = int(round(lerp(FIRST_YEAR, LAST_YEAR, x/float(width))))
  97.         stroke(color_of[yr])
  98.  
  99.         if yr == mouse_yr:
  100.             line(x, 0, x, height-1)
  101.         else:
  102.             line(x, 100, x, height-101)
  103.        
  104.         if x == mouseX:
  105.             if mouseX < width/2:
  106.                 textAlign(LEFT)
  107.             else:
  108.                 textAlign(RIGHT)
  109.             text(nf(yr, 4) + ": " + nfp(histalp_data[yr]['jan-dec'], 1, 1) + u" °C",
  110.                  x, height-50)
  111.  
  112. # ----------------------------------------------------------------------
  113. # This program is free software: you can redistribute it and/or modify
  114. # it under the terms of the GNU General Public License as published by
  115. # the Free Software Foundation, either version 3 of the License, or
  116. # (at your option) any later version.
  117. #
  118. # This program is distributed in the hope that it will be useful,
  119. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  120. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  121. # GNU General Public License for more details.
  122. #
  123. # You should have received a copy of the GNU General Public License
  124. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement