Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
bashrc2 committed Jul 27, 2023
1 parent e538815 commit 58065b1
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 36 deletions.
171 changes: 155 additions & 16 deletions anomaly.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import os


def _stations_anomaly(year: int, stations_data: {},
baseline: [], station_ids: set) -> float:
def _stations_anomaly(year: int, stations_data: {}, stations_locations: {},
baseline: [], station_ids: set,
min_latitude: float, max_latitude: float,
month_number: int) -> float:
"""Returns anomaly for the given stations for the given year
"""
anomaly = 0.0
Expand All @@ -22,12 +24,24 @@ def _stations_anomaly(year: int, stations_data: {},
continue
if year not in stations_data[sid]:
continue
if stations_locations[sid]['latitude'] < min_latitude or \
stations_locations[sid]['latitude'] > max_latitude:
continue
month_data = stations_data[sid][year]['month']
for month_index in range(12):
if not baseline[month_index]:
continue
if month_data[month_index]['av'] > -80:
if month_data[month_index]['av'] < 80:
if month_number > -1:
month_index = month_number
if baseline[month_index]:
if month_data[month_index]['av'] > -80 and \
month_data[month_index]['av'] < 80:
anomaly += \
month_data[month_index]['av'] - baseline[month_index]
hits += 1
else:
for month_index in range(12):
if not baseline[month_index]:
continue
if month_data[month_index]['av'] > -80 and \
month_data[month_index]['av'] < 80:
anomaly += \
month_data[month_index]['av'] - baseline[month_index]
hits += 1
Expand All @@ -36,23 +50,34 @@ def _stations_anomaly(year: int, stations_data: {},
return None


def update_grid_anomalies(grid: [], stations_data: {},
start_year: int, end_year: int) -> int:
def update_grid_anomalies(grid: [], stations_data: {}, stations_locations: {},
start_year: int, end_year: int,
min_latitude: float, max_latitude: float) -> int:
"""Calculates anomalies for each grid cell within a range of years
"""
ctr = 0
year_ctr = 0
for grid_cell in grid:
grid_cell['anomalies'] = {}
grid_cell['anomalies_monthly'] = {}
if not grid_cell['station_ids']:
continue
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)
_stations_anomaly(year, stations_data, stations_locations,
baseline, station_ids,
min_latitude, max_latitude, -1)
if grid_cell['anomalies'][year] is not None:
year_ctr += 1
grid_cell['anomalies_monthly'][year] = []
for month_index in range(12):
mnth_anom = \
_stations_anomaly(year, stations_data, stations_locations,
baseline, station_ids, min_latitude,
max_latitude, month_index)
grid_cell['anomalies_monthly'][year].append(mnth_anom)
ctr += 1
if year_ctr > 0:
return int(year_ctr * 100 / float(ctr))
Expand All @@ -78,9 +103,44 @@ def get_global_anomalies(grid: [],
return result


def get_monthly_anomalies(grid: [],
start_year: int, end_year: int) -> {}:
"""Returns monthly anomalies in the given year range
"""
result = {}
for year in range(start_year, end_year + 1, 1):
months_anomaly = [0,0,0,0,0,0,0,0,0,0,0,0]
months_ctr = [0,0,0,0,0,0,0,0,0,0,0,0]
ctr = 0
for grid_cell in grid:
if grid_cell['anomalies_monthly'].get(year):
months_list = grid_cell['anomalies_monthly'][year]
idx = 0
for month_anom in months_list:
if month_anom is None:
idx += 1
continue
months_anomaly[idx] += month_anom
months_ctr[idx] += 1
idx += 1
ctr += 1
for month_index in range(12):
if months_ctr[month_index] > 0:
months_anomaly[month_index] /= months_ctr[month_index]
else:
months_anomaly[month_index] = None
if ctr > 0:
result[year] = months_anomaly
else:
result[year] = None
return result


def plot_global_anomalies(grid: [],
start_year: int, end_year: int) -> None:
"""Plot anomalies graph
start_year: int, end_year: int,
baseline_start: int, baseline_end: int,
min_latitude: float, max_latitude: float) -> None:
"""Plot yearly anomalies graph
"""
anomalies = get_global_anomalies(grid, start_year, end_year)
series = []
Expand All @@ -95,10 +155,12 @@ def plot_global_anomalies(grid: [],

title = \
"Global Temperature Anomalies " + \
str(start_year) + ' - ' + str(end_year)
str(start_year) + ' - ' + str(end_year) + \
' for latitude range ' + str(min_latitude) + ' - ' + str(max_latitude)
subtitle = "Source https://www.ncei.noaa.gov/pub/data/ghcn/v4"
x_label = 'Year'
y_label = 'Average Temperature Anomaly (Celcius)'
y_label = 'Average Temperature Anomaly (Celcius) ' + \
'relative to ' + str(baseline_start) + '-' + str(baseline_end)
indent = 0.34
vpos = 0.94
image_width = 1000
Expand All @@ -109,7 +171,7 @@ def plot_global_anomalies(grid: [],
filename = plot_name + '.' + image_format
script_filename = plot_name + '.gnuplot'
data_filename = plot_name + '.data'
with open(data_filename, 'w+') as fp_data:
with open(data_filename, 'w+', encoding='utf-8') as fp_data:
year = start_year
for temperature_anomaly in series:
fp_data.write(str(year) + " " + str(temperature_anomaly) + '\n')
Expand All @@ -132,6 +194,83 @@ def plot_global_anomalies(grid: [],
" size " + str(image_width) + "," + str(image_height) + "\n" + \
"set output \"" + filename + "\"\n" + \
"plot \"" + data_filename + "\" using 1:2 notitle with lines\n"
with open(script_filename, 'w+') as fp_scr:
with open(script_filename, 'w+', encoding='utf-8') as fp_scr:
fp_scr.write(script)
os.system('gnuplot ' + script_filename)


def plot_monthly_anomalies(grid: [],
start_year: int, end_year: int,
baseline_start: int, baseline_end: int,
min_latitude: float, max_latitude: float) -> None:
"""Plot monthly anomalies graph
"""
anomalies = get_monthly_anomalies(grid, start_year, end_year)
series = []
minimum_temp = 99999999
maximum_temp = -99999999
for year in range(start_year, end_year + 1, 1):
series.append(anomalies[year])
for month_index in range(12):
if anomalies[year][month_index] is None:
continue
if anomalies[year][month_index] > maximum_temp:
maximum_temp = anomalies[year][month_index]
if anomalies[year][month_index] < minimum_temp:
minimum_temp = anomalies[year][month_index]

title = \
"Monthly Temperature Anomalies " + \
str(start_year) + ' - ' + str(end_year) + \
' for latitude range ' + str(min_latitude) + ' - ' + str(max_latitude)
subtitle = "Source https://www.ncei.noaa.gov/pub/data/ghcn/v4"
x_label = 'Month'
y_label = 'Average Monthly Temperature Anomaly (Celcius) ' + \
'relative to ' + str(baseline_start) + '-' + str(baseline_end)
indent = 0.34
vpos = 0.94
image_width = 1000
image_height = 1000
plot_name = 'monthly_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+', encoding='utf-8') as fp_data:
for month_index in range(12):
line = str(month_index + 1)
for temperature_anomaly in series:
if temperature_anomaly[month_index] is not None:
line += " " + str(temperature_anomaly[month_index])
else:
line += " 0"
fp_data.write(line + '\n')
script = \
"reset\n" + \
"set title \"" + title + "\"\n" + \
"set label \"" + subtitle + "\" at screen " + \
str(indent) + ", screen " + str(vpos) + "\n" + \
"set yrange [" + str(minimum_temp) + ":" + \
str(maximum_temp) + "]\n" + \
"set xrange [1:12]\n" + \
"set lmargin 9\n" + \
"set rmargin 2\n" + \
"set xlabel \"" + x_label + "\"\n" + \
"set ylabel \"" + y_label + "\"\n" + \
"set grid\n" + \
"set key right bottom\n" + \
"set terminal " + image_format2 + \
" size " + str(image_width) + "," + str(image_height) + "\n" + \
"set output \"" + filename + "\"\n"
script += "plot "
for year in range(start_year, end_year+1, 20):
script += \
"\"" + data_filename + "\" using 1:" + str(2+year-start_year) + \
" title \"" + str(year) + "\" with lines"
if year < end_year+1:
script += ', '
script += '\n'
with open(script_filename, 'w+', encoding='utf-8') as fp_scr:
fp_scr.write(script)
os.system('gnuplot ' + script_filename)
19 changes: 13 additions & 6 deletions baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ def _get_baseline(id: str, stations_data: {},
return baseline


def _baseline_for_stations(stations_data: {},
def _baseline_for_stations(stations_data: {}, station_locations: {},
start_year: int, end_year: int,
station_ids: set) -> []:
station_ids: set,
min_latitude: float, max_latitude: float) -> []:
"""Returns a baseline for the given range of years
and the given station ids
"""
Expand All @@ -72,6 +73,9 @@ def _baseline_for_stations(stations_data: {},
baseline = [0.0] * 12
hits = [0] * 12
for sid in station_ids:
if station_locations[sid]['latitude'] < min_latitude or \
station_locations[sid]['latitude'] > max_latitude:
continue
station_baseline = \
_get_baseline(sid, stations_data, start_year, end_year)
for month_index in range(12):
Expand All @@ -88,16 +92,19 @@ def _baseline_for_stations(stations_data: {},
return baseline, True


def update_grid_baselines(grid: [], stations_data: {},
start_year: int, end_year: int) -> int:
def update_grid_baselines(grid: [], stations_data: {}, station_locations: {},
start_year: int, end_year: int,
min_latitude: float, max_latitude: float) -> int:
"""Calculates reference baselines for each grid cell
"""
ctr = 0
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'])
_baseline_for_stations(stations_data, station_locations,
start_year, end_year,
grid_cell['station_ids'],
min_latitude, max_latitude)
if has_data:
ctr += 1
return ctr
6 changes: 3 additions & 3 deletions parseData.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
__module_group__ = "Commandline Interface"


def load_data(filename: str, start_year: int, end_year: int) -> ({}, {}):
def load_data(filename: str) -> ({}, {}):
"""Loads data from file
"""
lines = []
try:
with open(filename, 'r') as fp_load:
with open(filename, 'r', encoding='utf-8') as fp_load:
lines = fp_load.readlines()
except BaseException:
except OSError:
print('Unable to open ' + filename)
return None, None
years = {}
Expand Down
4 changes: 2 additions & 2 deletions parseStations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def load_station_locations(filename: str, grid: []) -> {}:
"""
lines = []
try:
with open(filename, 'r') as fp_loc:
with open(filename, 'r', encoding='utf-8') as fp_loc:
lines = fp_loc.readlines()
except BaseException:
except OSError:
print('Unable to open ' + filename)
return None
station_locations = {}
Expand Down
40 changes: 31 additions & 9 deletions tempgraph2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from anomaly import update_grid_anomalies
from anomaly import get_global_anomalies
from anomaly import plot_global_anomalies
from anomaly import get_monthly_anomalies
from anomaly import plot_monthly_anomalies


def str2bool(value) -> bool:
Expand Down Expand Up @@ -64,6 +66,12 @@ def str2bool(value) -> bool:
parser.add_argument('--cellsVertical', dest='cellsVertical', type=int,
default=36,
help='Number of cells down the grid')
parser.add_argument('--minLatitude', dest='minLatitude', type=float,
default=0,
help='Minimum latitude')
parser.add_argument('--maxLatitude', dest='maxLatitude', type=float,
default=90,
help='Maximum latitude')
parser.add_argument("--debug", type=str2bool, nargs='?',
const=True, default=False,
help="Show debug")
Expand All @@ -73,9 +81,9 @@ def str2bool(value) -> bool:

args = parser.parse_args()

debug = False
DEBUG = False
if args.debug:
debug = True
DEBUG = True

if args.tests:
run_all_tests()
Expand Down Expand Up @@ -107,26 +115,40 @@ def str2bool(value) -> bool:
save_grid_as_kml(grid_cells, 'grid.kml')
print('Saved grid as KML')

if args.minLatitude >= args.maxLatitude:
args.minLatitude = 0
args.maxLatitude = 90

print('Loading data from ' + args.filename)
stations_data, years_data = \
load_data(args.filename, args.startYear, args.endYear)
load_data(args.filename)
if not stations_data:
print('No data')
sys.exit()
print(str(len(stations_data.items())) + ' stations data loaded')
print('Calculating reference baseline between ' +
str(args.baselineStart) + ' and ' + str(args.baselineEnd))
ctr = update_grid_baselines(grid_cells, stations_data,
args.baselineStart, args.baselineEnd)
print(str(ctr) + ' grid baselines updated')
CTR = update_grid_baselines(grid_cells, stations_data, station_locations,
args.baselineStart, args.baselineEnd,
args.minLatitude, args.maxLatitude)
print(str(CTR) + ' grid baselines updated')
print('Calculating grid anomalies between ' +
str(args.startYear) + ' and ' + str(args.endYear))
percent = update_grid_anomalies(grid_cells, stations_data,
args.startYear, args.endYear)
percent = update_grid_anomalies(grid_cells, stations_data, station_locations,
args.startYear, args.endYear,
args.minLatitude, args.maxLatitude)
print(str(percent) + '% grid anomalies updated')
print('Calculating global anomalies between ' +
str(args.startYear) + ' and ' + str(args.endYear))
globalAnom = get_global_anomalies(grid_cells, args.startYear, args.endYear)
plot_global_anomalies(grid_cells, args.startYear, args.endYear)
plot_global_anomalies(grid_cells, args.startYear, args.endYear,
args.baselineStart, args.baselineEnd,
args.minLatitude, args.maxLatitude)
print('Calculating monthly anomalies between ' +
str(args.startYear) + ' and ' + str(args.endYear))
monthlyAnom = get_monthly_anomalies(grid_cells, args.startYear, args.endYear)
plot_monthly_anomalies(grid_cells, args.endYear-100, args.endYear,
args.baselineStart, args.baselineEnd,
args.minLatitude, args.maxLatitude)
print('Done')
sys.exit()

0 comments on commit 58065b1

Please sign in to comment.