forked from cpthackray/pythonHgBenchmark
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper_functions.py
executable file
·91 lines (73 loc) · 3.06 KB
/
helper_functions.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
import xarray as xr
from calendar import monthrange
import numpy as np
from math import log10, floor
import sys
def open_Hg (fn_OLD, fn_NEW):
""" Open GEOSChem.* netcdf files for Hg species as an xarray dataset.
Can take either list of monthly-averaged files (including *), or a
single time-concatenated file for each simulation.
Parameters
----------
fn_OLD : string
Reference model filename(s). If it includes an asterisk, will open as
multi-file dataset.
fn_NEW : string
New model filename(s). If it includes an asterisk, will open as
multi-file dataset.
"""
# check if dataset contains '*', that means it is multi-file
if '*' in fn_OLD: # multiple files to open
ds_OLD = xr.open_mfdataset(fn_OLD)
else: # single filename
ds_OLD = xr.open_dataset(fn_OLD)
if '*' in fn_NEW: # multiple files to open
ds_NEW = xr.open_mfdataset(fn_NEW)
else: # single filename
ds_NEW = xr.open_dataset(fn_NEW)
return ds_OLD, ds_NEW
def ds_sel_yr (ds, varname, Year):
""" If a year is given, then subset load for that year. Otherwise load all data into variable
Parameters
----------
ds : xarray dataset
Dataset of simulation to extract data from
varname : string
Name of parameter to extract
Year : int
Subset of year(s) to analyze model data
"""
if Year is not None: # take average over subset of years
var_yr = ds[varname].sel(time=ds.time.dt.year.isin(Year))
else: # use all years
var_yr = ds[varname]
return var_yr
def annual_avg (var_to_avg):
""" Take annual average from monthly data for multi-year data, accounting for the difference in day number
Parameters
----------
var_to_avg : xarray dataArray
variable to average
"""
time_v = var_to_avg.time
#first check that the data is in monthly time resolution
diff = time_v.dt.month[1]- time_v.dt.month[0]
if diff > 1: # more than monthly time difference
print("Simulation needs to be in monthly-averaged time resolution. This data is super-monthly resolution")
sys.exit(1)
elif diff<1: # less than monthly time difference
print("Simulation needs to be in monthly-averaged time resolution. This data is sub-monthly resolution")
sys.exit(1)
else:
# calculate number of days per month
days_in_month = time_v.dt.days_in_month
wgts = days_in_month.groupby("time.year") / days_in_month.groupby("time.year").sum()
# Calculate the numerator for weighted average
obs_sum = (var_to_avg * wgts).resample(time="AS").sum(dim="time").squeeze()
# Calculate the denominator for weighted average
ones_out = (wgts).resample(time="AS").sum(dim="time").squeeze()
# Return the weighted average
return obs_sum / ones_out
def round_sig(x, sig=2):
# Rounding one number to specific number of significant digits
return round(x, sig-int(floor(log10(abs(x))))-1)