Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Visualizes temperature data from the HISTALP project,
- # see http://www.zamg.ac.at/histalp/dataset/station/csv.php
- # Copyright 2022, 2023 Roland Richter [Processing.py]
- from __future__ import division, print_function
- def read_histalp_data(file_name, factor = 1.0):
- """Read a file in HISTALP format, and return a dictionary (keys: 1815, 2022,
- ...) of dictionaries (keys: 'jan', 'feb', ...).
- Example:
- hist_data = read_histalp_data("HISTALP_AT_LIN_T01_1816_2022.csv")
- mean_tmpr = hist_data[1974]['mar'] # mean temperature of March 1974
- Arguments:
- file_name -- the name of the HISTALP file
- factor -- number to multiply all values with; only necessary for temperature (0.1)
- """
- csv_lines = loadStrings(file_name) # Processing.py function to read lines from a file
- hist_data = {}
- is_first_line = True
- for csv_line in csv_lines:
- # ignore all lines starting with a '#' character, as these are comments
- if csv_line.startswith('#'):
- pass
- else:
- csv_entries = csv_line.strip().split(';')
- # the first non-comment line contains the column headers
- if is_first_line:
- column_header = csv_entries
- is_first_line = False
- else:
- year = int(csv_entries[0])
- year_dict = {}
- # go through all entries of a line (except the first);
- # store the value (multiplied with the factor),
- # or 'nan' ('not a number') to indicate missing values
- for k in range(1, len(csv_entries)):
- entry = csv_entries[k]
- header = column_header[k]
- if entry != "999999":
- year_dict[header] = factor * float(entry)
- else:
- year_dict[header] = "nan"
- hist_data[year] = year_dict
- return hist_data
- # Mean temperature for Linz is available for 207 years ...
- FILE_NAME = "HISTALP_AT_LIN_T01_1816_2022.csv"
- FIRST_YEAR = 1816
- LAST_YEAR = 2022
- def setup():
- # Let each year be 4 pixels wide; 207 * 4 = 828
- size(828, 550)
- global histalp_data, FILE_NAME, FIRST_YEAR, LAST_YEAR, color_of
- histalp_data = read_histalp_data(FILE_NAME, 0.1)
- min_tmpr = max_tmpr = histalp_data[FIRST_YEAR]['jan-dec']
- for yr in range(FIRST_YEAR+1, LAST_YEAR+1):
- tmpr = histalp_data[yr]['jan-dec']
- if tmpr < min_tmpr:
- min_tmpr = tmpr
- if tmpr > max_tmpr:
- max_tmpr = tmpr
- print("Temperature range:", min_tmpr, "to", max_tmpr)
- color_of = {}
- cold = color(113, 166, 210) # Iceberg
- warm = color(207, 16, 32) # Lava
- for yr in range(FIRST_YEAR, LAST_YEAR+1):
- mean_tmpr = histalp_data[yr]['jan-dec']
- amount = (mean_tmpr-min_tmpr) / (max_tmpr-min_tmpr)
- color_of[yr] = lerpColor(cold, warm, amount)
- textSize(16)
- def draw():
- background(0)
- global histalp_data, FIRST_YEAR, LAST_YEAR, color_of
- mouse_yr = int(round(lerp(FIRST_YEAR, LAST_YEAR, mouseX/float(width))))
- for x in range(0, width):
- yr = int(round(lerp(FIRST_YEAR, LAST_YEAR, x/float(width))))
- stroke(color_of[yr])
- if yr == mouse_yr:
- line(x, 0, x, height-1)
- else:
- line(x, 100, x, height-101)
- if x == mouseX:
- if mouseX < width/2:
- textAlign(LEFT)
- else:
- textAlign(RIGHT)
- text(nf(yr, 4) + ": " + nfp(histalp_data[yr]['jan-dec'], 1, 1) + u" °C",
- x, height-50)
- # ----------------------------------------------------------------------
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement