-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_time.py
executable file
·55 lines (34 loc) · 1.27 KB
/
change_time.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Author: Mathias Hauser
#Date:
import netCDF4 as nc
import sys
def main(filename):
"""
change the calendar of the SSTs and Ice cover data
"""
with nc.Dataset(filename, 'a') as ncf:
# check if the units are right
units = ncf.variables['time'].units
if units != "days since 0-01-01 00:00:00" and units != "days since 0-1-1 00:00:00":
raise RuntimeError("units is not 'days since 0-01-01 00:00:00'")
# check if the calendar is right
calendar = ncf.variables['time'].calendar
if calendar != "365_day":
raise RuntimeError("calendar is not '365_day'")
# we want a more current calendar
new_units = "days since 1850-01-01 00:00:00"
# need to subtract days
time_offset = 365 * 1850
# adjust
ncf.variables['time_bnds'][:] -= time_offset
# we want the time in the middle of the month
ncf.variables['time'][:] = ncf.variables['time_bnds'][:].mean(axis=1)
# adjust the units
ncf.variables['time'].units = new_units
if __name__ == '__main__':
if len(sys.argv) < 2:
raise ValueError("command line argument needed")
filename = sys.argv[1]
main(filename)