1 | #!/usr/bin/env python3 |
---|
2 | |
---|
3 | import subprocess |
---|
4 | import os |
---|
5 | import sys |
---|
6 | import netCDF4 |
---|
7 | from netCDF4 import Dataset, stringtochar |
---|
8 | import numpy as np |
---|
9 | |
---|
10 | |
---|
11 | def cut(input_path, output_path): |
---|
12 | """Cycle through all netCDF files within input_path and remove all |
---|
13 | empty time levels. Output is saved in output_path""" |
---|
14 | |
---|
15 | if not os.path.isdir(input_path): |
---|
16 | raise OSError("Error: input path does not exist: {0}".format(input_path)) |
---|
17 | if not os.path.isdir(output_path): |
---|
18 | os.makedirs(output_path) |
---|
19 | |
---|
20 | list_input_data = \ |
---|
21 | [file for file in os.listdir(input_path) if os.path.splitext(file)[1] == '.nc'] |
---|
22 | |
---|
23 | for nc_file in list_input_data: |
---|
24 | file_in = input_path + "/" + nc_file |
---|
25 | ncfile = Dataset(file_in, "r", format="NETCDF4", encoding='ascii') |
---|
26 | time_dim = ncfile.dimensions["time"] |
---|
27 | time_var = ncfile.variables["time"][:] |
---|
28 | time_mask = ~np.ma.getmaskarray(time_var) |
---|
29 | |
---|
30 | print("process file ", file_in) |
---|
31 | if not time_dim.isunlimited() and any(time_mask): |
---|
32 | start_index = np.where(time_mask)[0][0] |
---|
33 | end_index = np.where(time_mask)[0][-1] |
---|
34 | file_out = output_path + "/" + nc_file |
---|
35 | |
---|
36 | ncks_command = "ncks -O -d time,{0},{1} {2} {3}".format( |
---|
37 | start_index, |
---|
38 | end_index, |
---|
39 | file_in, |
---|
40 | file_out, |
---|
41 | ) |
---|
42 | |
---|
43 | ncks_output = subprocess.run( |
---|
44 | ncks_command, |
---|
45 | stdout=subprocess.PIPE, |
---|
46 | stderr=subprocess.STDOUT, |
---|
47 | shell=True |
---|
48 | ) |
---|
49 | |
---|
50 | if ncks_output.returncode != 0: |
---|
51 | raise RuntimeError("Error: {0}".format(ncks_output.stdout)) |
---|
52 | |
---|
53 | |
---|
54 | if __name__ == '__main__': |
---|
55 | arg_list = sys.argv |
---|
56 | if arg_list.__len__() == 3: |
---|
57 | input_path = arg_list[1] |
---|
58 | output_path = arg_list[2] |
---|
59 | cut(input_path, output_path) |
---|
60 | else: |
---|
61 | sys.exit('Arguments are missing! ' + |
---|
62 | 'Usage:\n cut_empty_timesteps.py <input_path> <output_path>') |
---|