Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Reads a CSV file containg weather data of the HISTALP project,
- # see http://www.zamg.ac.at/histalp/dataset/station/csv.php
- # Copyright 2022, 2023 Roland Richter [Mu: Python 3]
- def read_histalp_meta(file_name):
- """Read a file in HISTALP format, and return a dictionary with meta data
- Example:
- hist_meta = read_histalp_meta("HISTALP_AT_LIN_T01_1816_2022.csv")
- hist_sttn = hist_meta['station'] # name of station is Linz
- Arguments:
- file_name -- the name of the HISTALP file
- """
- csv_file = open(file_name, 'r')
- hist_meta = {}
- for csv_line in csv_file:
- # In HISTALP CSV files, a line starting with a '#' character is a comment;
- # such lines starting may contain "key: value" entries, e.g.
- # "# parameter: Mean temperature"
- # "# source: http://www.zamg.ac.at/histalp"
- # "# station: Linz-Stadt"
- if csv_line.startswith('#'):
- # The first ':' character always separates key from value; however,
- # some values might contain the ':' character as well, as in
- # "# source: http://www.zamg.ac.at/histalp"
- # Hence, split into *at most* two parts: first the key, second the value.
- csv_entries = csv_line[1:].split(':', 1)
- if len(csv_entries) == 2:
- key = csv_entries[0].strip()
- hist_meta[key] = csv_entries[1].strip()
- else:
- pass
- csv_file.close()
- return hist_meta
- 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_file = open(file_name, 'r')
- hist_data = {}
- is_first_line = True
- for csv_line in csv_file:
- # 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
- csv_file.close()
- return hist_data
- name = "HISTALP_AT_LIN_T01_1816_2022.csv" # Mean temperature
- # name = "HISTALP_AT_LIN_R01_1852_2022.csv" # Precipitation Totals
- histalp_meta = read_histalp_meta(name)
- print(histalp_meta)
- # Temperate data is given in units of 0,1 °C; convert it to ordinary °C
- if histalp_meta["unit"].startswith("0,1"):
- histalp_data = read_histalp_data(name, 0.1)
- histalp_meta["unit"] = "°C"
- else:
- histalp_data = read_histalp_data(name)
- print("Data available for", histalp_data.keys())
- year = int(input("Which year are you interested in? "))
- if year not in histalp_data:
- print("Sorry, no data for", year)
- else:
- print("Year", year, "has entries for", histalp_data[year].keys())
- month = input("Which month or period are you interested in? ")
- if month not in histalp_data[year]:
- print("Sorry, no data for", month, year)
- else:
- param = histalp_meta["parameter"]
- statn = histalp_meta["station"]
- punit = histalp_meta["unit"]
- print(param, "in", statn, ",", month, year, ":",
- histalp_data[year][month], punit)
- # ----------------------------------------------------------------------
- # 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