forked from cpthackray/pythonHgBenchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeanSurfaceTGM.py
135 lines (105 loc) · 5.53 KB
/
MeanSurfaceTGM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
import pandas as pd
import xbpch
import cartopy.crs as ccrs
from matplotlib import colorbar, colors
import statistics
from sklearn.metrics import r2_score
#%matplotlib inline
# Define a function for Surface TGM with default variables set for such
def HgMeanSurfaceTGM (Dataset_OLD, Dataset_NEW, Variable=['IJ_AVG_S_Hg0', 'IJ_AVG_S_Hg2'],
Units="$ng/m^2$", Unit_Conversion=8.93, Title="Surface TGM"):
""" Plot the mean surface TGM for mercury for both the reference and new models.
Produce the absolute and percent differences for the reference and new models.
**Note: This function can also be used in much the same way as the General Graphing Function, if only the
datasets are specified as everything else needed for this function is specified in it's default values.
Args:
Dataset_OLD (str) : Reference Model bpch file
Dataset_NEW (str) : New Model bpch file
Variable (str) : Names of the variable/s you are choosing to take the mean over time with e.g.
['Variable 1', 'Variable 2', etc].
Units (str) : Name of the units the data is in.
Unit_Conversion (float) : Conversion factor that converts your data to your preferred unit.
Title (str) : Title of your graph.
"""
# Read in the data for the observed sites
AnHgObs= pd.read_csv('data/TGMSiteAnnual.csv',skiprows=[0], na_values=(-9999))
AnHgObs.columns=['SiteID', 'Lat', 'Lon','Alt', 'TGM', 'Hg0']
# Set levels for the colorbar in order to have a nonlinear scale.
Levels= (0.75, 0.95, 1.15, 1.35, 1.55, 1.75, 2.30, 2.90, 3.50)
# Make a variable for the unit conversion factor to obtain ng/m^3
Unit_Conversion= 8.93
# Create an if statement to differentiate between only one variable and a list of variables.
if type(Variable) is list and len(Variable) > 1:
OLD_sum=0
NEW_sum=0
# Create a for loop to add each variable, extracting the mean of the variable in respect to time
# at surface level.
for i in range(len(Variable)):
tmpVar1 = ((Dataset_OLD[Variable[i]].isel(lev=0).mean('time')) * Unit_Conversion)
OLD_sum = OLD_sum + tmpVar1
tmpVar2 = ((Dataset_NEW[Variable[i]].isel(lev=0).mean('time')) * Unit_Conversion)
NEW_sum = NEW_sum + tmpVar2
else:
# Extract the mean of only one variable in respect to time at surface level
NEW_sum = ((Dataset_NEW[Variable].isel(lev=0).mean('time')) * Unit_Conversion)
OLD_sum = ((Dataset_OLD[Variable].isel(lev=0).mean('time')) * Unit_Conversion)
# Find the absolute difference between the reference and new model.
Abs_diff = NEW_sum - OLD_sum
# Find the absolute maximum value of the absolute difference.
Abs_MaxVal= np.max(np.abs(Abs_diff.values))
# Find the percent difference of the models.
Perc_diff = (Abs_diff / OLD_sum)*100
# Find the absolute maximum value of the percent difference.
Perc_MaxVal= np.max(np.abs(Perc_diff.values))
# Plot the four graphs as subplots.
plt.figure(figsize=(20,10))
# Plot the reference model and use a geographical map.
ax = plt.subplot(221, projection=ccrs.PlateCarree())
im=OLD_sum.plot.contourf(x='lon',y='lat',ax=ax, transform=ccrs.PlateCarree(), levels= Levels, cmap='viridis',
cbar_kwargs={'orientation':'horizontal',
'ticklocation':'auto',
'label':"Not Linear " + Units})
# Add a title
plt.title(' Reference Model Version: '+Title)
# Add the coastlines.
ax.coastlines()
# Plot the new model using a geographical map.
ax = plt.subplot(222, projection=ccrs.PlateCarree())
im= NEW_sum.plot.contourf(x='lon',y='lat', cmap='viridis', transform=ccrs.PlateCarree(), ax=ax, levels= Levels,
cbar_kwargs={'orientation':'horizontal',
'ticklocation':'auto',
'label':"Not Linear " + Units})
# Add a title.
plt.title('New Model Version: '+ Title)
# Add the coastlines.
ax.coastlines()
# Plot the absolute difference using a geograpical map.
ax = plt.subplot(223, projection=ccrs.PlateCarree())
im= Abs_diff.plot.imshow(x='lon',y='lat', ax=ax,transform=ccrs.PlateCarree(), cmap='RdBu',
vmin=-Abs_MaxVal, vmax=Abs_MaxVal,
cbar_kwargs={'orientation':'horizontal',
'ticklocation':'auto',
'label': Units})
# Add a title.
plt.title("Absolute Difference")
# Add the coastlines.
ax.coastlines()
# Plot the percent difference using a geographical map.
ax = plt.subplot(224, projection=ccrs.PlateCarree())
im= Perc_diff.plot.imshow(x='lon',y='lat',ax=ax,transform=ccrs.PlateCarree(), cmap='RdBu',
vmin=(-Perc_MaxVal), vmax=(Perc_MaxVal),
cbar_kwargs={'orientation':'horizontal',
'ticklocation':'auto',
'label':"%" })
# Add a title.
plt.title("Percent Difference (%)")
# Add the coastlines.
ax.coastlines()
# Show the four subplots.
TGMGraph= plt.show()
# Return the four graphs.
return