-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonths_to_days.py
executable file
·63 lines (49 loc) · 1.81 KB
/
months_to_days.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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2021 W. Traylor <[email protected]>
#
# SPDX-License-Identifier: MIT
#
# Convert time axis of given NetCDF file from "months since" to "days
# since", starting to count from 60,000 years BP (the beginning of the
# HadCM3B simulation).
# Arguments:
# 1) Input NetCDF file.
# 2) Output NetCDF file.
#
# Author: Wolfgang Traylor, SBiK-F ([email protected])
# License: See LICENSE file in repository.
import os
import sys
import numpy
import xarray as xr
from extract_years_from_filename import extract_years
if len(sys.argv) != 3:
sys.exit('Please provide exactly two argument:\n'
f'{sys.argv[0]} <in.nc> <out.nc>')
in_file = sys.argv[1]
out_file = sys.argv[2]
if not os.path.isfile(in_file):
sys.exit(f'File does not exist: {in_file}')
if os.path.isfile(out_file):
sys.exit(f'Output file already exists: {out_file}')
start_day = extract_years(in_file)[0] * 365
# Prepare a vector of values for the time axis in "days since".
# It has 30,000 entries for the 2,400 years of transient monthly data in the
# NetCDF file.
month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
total_month_count = 30000
days_axis = numpy.zeros(total_month_count, dtype='i') # reserve memory
days_axis[0] = start_day + 15
for i in range(len(days_axis)-1):
current_month = i % 12
days_axis[i+1] = days_axis[i] + month_lengths[current_month]
# Now days_axis contains the Julian day for the +/- middle of each month:
# [15, 43, 74, 104, ...]
# Write the new time axis into the NetCDF file.
# Note that the "unit" attribute must be changed externally.
ds = xr.open_dataset(in_file, decode_times=False).load()
ds["time"].values = days_axis
ds.to_netcdf(out_file, mode='w', engine='netcdf4')
ds.close()
# Exit with success code.
sys.exit(0)