Skip to content

Commit

Permalink
Snake case
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Mottram committed Jan 7, 2022
1 parent c7659e0 commit 4cde264
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 254 deletions.
140 changes: 70 additions & 70 deletions anomaly.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,128 +10,128 @@
import os


def _stationsAnomaly(year: int, stationsData: {},
baseline: [], stationIds: set) -> float:
def _stations_anomaly(year: int, stations_data: {},
baseline: [], station_ids: set) -> float:
"""Returns anomaly for the given stations for the given year
"""
anomaly = 0.0
hits = 0

for id in stationIds:
if id not in stationsData:
for sid in station_ids:
if sid not in stations_data:
continue
if year not in stationsData[id]:
if year not in stations_data[sid]:
continue
monthData = stationsData[id][year]['month']
for monthIndex in range(12):
if not baseline[monthIndex]:
month_data = stations_data[sid][year]['month']
for month_index in range(12):
if not baseline[month_index]:
continue
if monthData[monthIndex]['av'] > -80:
if monthData[monthIndex]['av'] < 80:
if month_data[month_index]['av'] > -80:
if month_data[month_index]['av'] < 80:
anomaly += \
monthData[monthIndex]['av'] - baseline[monthIndex]
month_data[month_index]['av'] - baseline[month_index]
hits += 1
if hits > 0:
return anomaly / float(hits)
return None


def updateGridAnomalies(grid: [], stationsData: {},
startYear: int, endYear: int) -> int:
def update_grid_anomalies(grid: [], stations_data: {},
start_year: int, end_year: int) -> int:
"""Calculates anomalies for each grid cell within a range of years
"""
ctr = 0
yearCtr = 0
for gridCell in grid:
gridCell['anomalies'] = {}
if not gridCell['stationIds']:
year_ctr = 0
for grid_cell in grid:
grid_cell['anomalies'] = {}
if not grid_cell['station_ids']:
continue
baseline = gridCell['baseline']
stationIds = gridCell['stationIds']
for year in range(startYear, endYear + 1, 1):
gridCell['anomalies'][year] = \
_stationsAnomaly(year, stationsData, baseline, stationIds)
if gridCell['anomalies'][year] is not None:
yearCtr += 1
baseline = grid_cell['baseline']
station_ids = grid_cell['station_ids']
for year in range(start_year, end_year + 1, 1):
grid_cell['anomalies'][year] = \
_stations_anomaly(year, stations_data, baseline, station_ids)
if grid_cell['anomalies'][year] is not None:
year_ctr += 1
ctr += 1
if yearCtr > 0:
return int(yearCtr * 100 / float(ctr))
if year_ctr > 0:
return int(year_ctr * 100 / float(ctr))
return 0


def getGlobalAnomalies(grid: [],
startYear: int, endYear: int) -> {}:
def get_global_anomalies(grid: [],
start_year: int, end_year: int) -> {}:
"""Returns global anomalies in the given year range
"""
result = {}
for year in range(startYear, endYear + 1, 1):
yearAnomaly = 0.0
for year in range(start_year, end_year + 1, 1):
year_anomaly = 0.0
ctr = 0
for gridCell in grid:
if gridCell['anomalies'].get(year):
yearAnomaly += gridCell['anomalies'][year]
for grid_cell in grid:
if grid_cell['anomalies'].get(year):
year_anomaly += grid_cell['anomalies'][year]
ctr += 1
if ctr > 0:
result[year] = yearAnomaly / float(ctr)
result[year] = year_anomaly / float(ctr)
else:
result[year] = None
return result


def plotGlobalAnomalies(grid: [],
startYear: int, endYear: int) -> None:
def plot_global_anomalies(grid: [],
start_year: int, end_year: int) -> None:
"""Plot anomalies graph
"""
anomalies = getGlobalAnomalies(grid, startYear, endYear)
anomalies = get_global_anomalies(grid, start_year, end_year)
series = []
minimumTemp = 99999999
maximumTemp = -99999999
for year in range(startYear, endYear + 1, 1):
minimum_temp = 99999999
maximum_temp = -99999999
for year in range(start_year, end_year + 1, 1):
series.append(anomalies[year])
if anomalies[year] > maximumTemp:
maximumTemp = anomalies[year]
if anomalies[year] < minimumTemp:
minimumTemp = anomalies[year]
if anomalies[year] > maximum_temp:
maximum_temp = anomalies[year]
if anomalies[year] < minimum_temp:
minimum_temp = anomalies[year]

title = \
"Global Temperature Anomalies " + \
str(startYear) + ' - ' + str(endYear)
str(start_year) + ' - ' + str(end_year)
subtitle = "Source https://www.ncei.noaa.gov/pub/data/ghcn/v4"
Xlabel = 'Year'
Ylabel = 'Average Temperature Anomaly (Celcius)'
x_label = 'Year'
y_label = 'Average Temperature Anomaly (Celcius)'
indent = 0.34
vpos = 0.94
imageWidth = 1000
imageHeight = 1000
plotName = 'global_anomalies'
imageFormat = 'jpg'
imageFormat2 = 'jpeg'
filename = plotName + '.' + imageFormat
scriptFilename = plotName + '.gnuplot'
dataFilename = plotName + '.data'
with open(dataFilename, 'w+') as fp:
year = startYear
for temperatureAnomaly in series:
fp.write(str(year) + " " + str(temperatureAnomaly) + '\n')
image_width = 1000
image_height = 1000
plot_name = 'global_anomalies'
image_format = 'jpg'
image_format2 = 'jpeg'
filename = plot_name + '.' + image_format
script_filename = plot_name + '.gnuplot'
data_filename = plot_name + '.data'
with open(data_filename, 'w+') as fp_data:
year = start_year
for temperature_anomaly in series:
fp_data.write(str(year) + " " + str(temperature_anomaly) + '\n')
year += 1
script = \
"reset\n" + \
"set title \"" + title + "\"\n" + \
"set label \"" + subtitle + "\" at screen " + \
str(indent) + ", screen " + str(vpos) + "\n" + \
"set yrange [" + str(minimumTemp) + ":" + \
str(maximumTemp) + "]\n" + \
"set xrange [" + str(startYear) + ":" + str(endYear) + "]\n" + \
"set yrange [" + str(minimum_temp) + ":" + \
str(maximum_temp) + "]\n" + \
"set xrange [" + str(start_year) + ":" + str(end_year) + "]\n" + \
"set lmargin 9\n" + \
"set rmargin 2\n" + \
"set xlabel \"" + Xlabel + "\"\n" + \
"set ylabel \"" + Ylabel + "\"\n" + \
"set xlabel \"" + x_label + "\"\n" + \
"set ylabel \"" + y_label + "\"\n" + \
"set grid\n" + \
"set key right bottom\n" + \
"set terminal " + imageFormat2 + \
" size " + str(imageWidth) + "," + str(imageHeight) + "\n" + \
"set terminal " + image_format2 + \
" size " + str(image_width) + "," + str(image_height) + "\n" + \
"set output \"" + filename + "\"\n" + \
"plot \"" + dataFilename + "\" using 1:2 notitle with lines\n"
with open(scriptFilename, 'w+') as fp:
fp.write(script)
os.system('gnuplot ' + scriptFilename)
"plot \"" + data_filename + "\" using 1:2 notitle with lines\n"
with open(script_filename, 'w+') as fp_scr:
fp_scr.write(script)
os.system('gnuplot ' + script_filename)
113 changes: 57 additions & 56 deletions baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,95 +8,96 @@
__module_group__ = "Commandline Interface"


def _getBaselineForYear(id: str, stationsData: {}, year: int) -> []:
def _get_baseline_for_year(id: str, stations_data: {}, year: int) -> []:
"""Returns the baseline for a given station id
"""
baseline = [None] * 12
if year not in stationsData[id]:
if year not in stations_data[id]:
return baseline
monthData = stationsData[id][year]['month']
for monthIndex in range(12):
monthData2 = monthData[monthIndex]
if monthData2['qcflag'] == 'M':
month_data = stations_data[id][year]['month']
for month_index in range(12):
month_data2 = month_data[month_index]
if month_data2['qcflag'] == 'M':
# Flagged as error
continue
if monthData2['av'] > -80:
if monthData2['av'] < 80:
baseline[monthIndex] = monthData2['av']
if month_data2['av'] > -80:
if month_data2['av'] < 80:
baseline[month_index] = month_data2['av']
return baseline


def _getBaseline(id: str, stationsData: {},
startYear: int, endYear: int) -> []:
def _get_baseline(id: str, stations_data: {},
start_year: int, end_year: int) -> []:
"""Returns the baseline for a given station id
"""
if id not in stationsData:
if id not in stations_data:
return [None] * 12

if startYear == endYear:
return _getBaselineForYear(id, stationsData, startYear)
else:
baseline = [0.0] * 12
hits = [0] * 12
for year in range(startYear, endYear, 1):
if year not in stationsData[id]:
if start_year == end_year:
return _get_baseline_for_year(id, stations_data, start_year)

baseline = [0.0] * 12
hits = [0] * 12
for year in range(start_year, end_year, 1):
if year not in stations_data[id]:
continue
month_data = stations_data[id][year]['month']
for month_index in range(12):
month_data2 = month_data[month_index]
if month_data2['qcflag'] == 'M':
# Flagged as error
continue
monthData = stationsData[id][year]['month']
for monthIndex in range(12):
monthData2 = monthData[monthIndex]
if monthData2['qcflag'] == 'M':
# Flagged as error
continue
if monthData2['av'] > -80:
if monthData2['av'] < 80:
baseline[monthIndex] += monthData2['av']
hits[monthIndex] += 1
if month_data2['av'] > -80:
if month_data2['av'] < 80:
baseline[month_index] += month_data2['av']
hits[month_index] += 1

for monthIndex in range(12):
if hits[monthIndex] > 0:
baseline[monthIndex] /= float(hits[monthIndex])
else:
baseline[monthIndex] = None
for month_index in range(12):
if hits[month_index] > 0:
baseline[month_index] /= float(hits[month_index])
else:
baseline[month_index] = None
return baseline


def _baselineForStations(stationsData: {},
startYear: int, endYear: int,
stationIds: set) -> []:
def _baseline_for_stations(stations_data: {},
start_year: int, end_year: int,
station_ids: set) -> []:
"""Returns a baseline for the given range of years
and the given station ids
"""
if not stationIds:
if not station_ids:
return [None] * 12, False

baseline = [0.0] * 12
hits = [0] * 12
for id in stationIds:
stationBaseline = _getBaseline(id, stationsData, startYear, endYear)
for monthIndex in range(12):
if stationBaseline[monthIndex] is not None:
baseline[monthIndex] += stationBaseline[monthIndex]
hits[monthIndex] += 1
for sid in station_ids:
station_baseline = \
_get_baseline(sid, stations_data, start_year, end_year)
for month_index in range(12):
if station_baseline[month_index] is not None:
baseline[month_index] += station_baseline[month_index]
hits[month_index] += 1

for monthIndex in range(12):
if hits[monthIndex] > 0:
baseline[monthIndex] /= float(hits[monthIndex])
for month_index in range(12):
if hits[month_index] > 0:
baseline[month_index] /= float(hits[month_index])
else:
baseline[monthIndex] = None
baseline[month_index] = None

return baseline, True


def updateGridBaselines(grid: [], stationsData: {},
startYear: int, endYear: int) -> int:
def update_grid_baselines(grid: [], stations_data: {},
start_year: int, end_year: int) -> int:
"""Calculates reference baselines for each grid cell
"""
ctr = 0
for gridCell in grid:
if gridCell['stationIds']:
gridCell['baseline'], hasData = \
_baselineForStations(stationsData, startYear, endYear,
gridCell['stationIds'])
if hasData:
for grid_cell in grid:
if grid_cell['station_ids']:
grid_cell['baseline'], has_data = \
_baseline_for_stations(stations_data, start_year, end_year,
grid_cell['station_ids'])
if has_data:
ctr += 1
return ctr
Loading

0 comments on commit 4cde264

Please sign in to comment.