Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit c5fba1b

Browse files
committed
open_mfdataset not playing well with encoding time when re-saving
annoying issue with xarray pydata/xarray#2436
1 parent 8f93505 commit c5fba1b

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

preprocessing/split_by_year.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,35 @@
22

33
import cftime
44
import xarray
5+
import glob
56

67
"""
78
Split up an nc multifile dataset into single file per year.
89
"""
910
class SplitByYear:
1011

11-
def __init__(self, input_dir, output_filepath_prefix, years = itertools.chain(range(1980, 2000), range(2020, 2040), range(2060, 2080))) -> None:
12-
self.input_dir = input_dir
12+
def __init__(self, input_filepath_prefix, output_filepath_prefix, years = itertools.chain(range(1980, 2000), range(2020, 2040), range(2060, 2080))) -> None:
13+
self.input_filepath_prefix = input_filepath_prefix
1314
self.output_filepath_prefix = output_filepath_prefix
1415
self.years = years
1516

1617
pass
1718

18-
def run(self):
19-
output_files = []
19+
def gcm_file(self, year):
20+
if (year % 10) <= 8:
21+
start = (year // 10) * 10 - 1
22+
else:
23+
start = year
24+
25+
end = start + 10
2026

21-
input = xarray.open_mfdataset(str(self.input_dir/"*.nc"))
27+
return f"{start}1201-{end}1130.nc"
2228

29+
def run(self):
30+
output_files = []
2331

2432
for year in self.years:
33+
input = xarray.load_dataset(f"{self.input_filepath_prefix}_{self.gcm_file(year)}.nc")
2534
single_year_input = input.sel(time=slice(cftime.Datetime360Day(year, 12, 1, 12, 0, 0, 0) , cftime.Datetime360Day(year+1, 11, 30, 12, 0, 0, 0)))
2635

2736
output_filepath = f"{self.output_filepath_prefix}_{year}1201-{year+1}1130.nc"

split-gcm.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
def get_args():
88
parser = argparse.ArgumentParser(description='Regrid GCM data to match the CPM data',
99
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
10-
parser.add_argument('--input', dest='input_dir', type=Path, required=True,
11-
help='Path to directory holding raw files')
10+
parser.add_argument('--input-prefix', dest='input_prefix', type=Path, required=True,
11+
help='Prefix of input files to split up (so filepath up to the date part)')
1212
parser.add_argument('--output-prefix', dest='output_prefix', type=str, required=True,
1313
help='Prefix of output files including directory path')
1414
parser.add_argument('--years', dest='years', nargs='+', type=int, required=True,
@@ -22,6 +22,6 @@ def get_args():
2222

2323
os.makedirs(os.path.dirname(args.output_prefix), exist_ok=True)
2424

25-
outputs = SplitByYear(args.input_dir, args.output_prefix, args.years).run()
25+
outputs = SplitByYear(args.input_prefix, args.output_prefix, args.years).run()
2626

2727
print(outputs)

0 commit comments

Comments
 (0)