forked from coecms/nccompress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathncinfo
More file actions
executable file
·137 lines (114 loc) · 4.72 KB
/
Copy pathncinfo
File metadata and controls
executable file
·137 lines (114 loc) · 4.72 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
from netCDF4 import Dataset, MFDataset
import numpy as np
import numpy.ma as ma
import os
import sys
import math
import operator
import itertools as it
from warnings import warn
def ncinfo(files, hidedims, ignoretime, vars=None, history_only=False):
if isinstance(files, list):
try:
ncobj = MFDataset(files)
except Exception as e:
warn("Could not aggregate datasets, python library returned: "+str(e))
return
else:
print
print files
ncobj = Dataset(files,'r')
varnames = ncobj.variables.keys()
varname_maxlen = len(max(varnames, key=len))
if history_only:
varnames=[]
pr_varnames = []
pr_dimensions = []
pr_longnames = []
for varname in varnames:
var = ncobj.variables[varname]
if "time" == varname.lower():
if not var.ndim == 1:
warn("I don't understand two dimensional time dimensions")
continue
# Get our time axis
nsteps = len(var)
try:
unit = var.__getattribute__("units").partition(' ')[0]
except AttributeError:
unit = 'None'
if nsteps > 1:
print "Time steps: ",nsteps," x ",var[1]-var[0],unit
elif nsteps == 1:
print "Time : ",var[0],unit
continue
if ignoretime and "time" in varname.lower():
continue
if vars is not None:
if varname not in vars: continue
if var.ndim == 1:
dims = ncobj.variables[varname].dimensions
if hidedims and dims[0] == varname:
# This is a dimension variable, ignore
continue
if ignoretime and dims[0] == "time":
# Time bounds stuff also ignore
continue
# fmt = '{0:{1}} :: {2:<22} :: {3}'
try:
long_name = var.__getattribute__("long_name")
except AttributeError:
long_name = ''
pr_varnames.append(str(varname))
pr_dimensions.append(str(var.shape))
pr_longnames.append(str(long_name))
if not history_only and pr_varnames:
fmt = '{0:{1}} :: {2:{3}} :: {4}'
pr_varnames_maxlen = len(max(pr_varnames, key=len))
pr_dimensions_maxlen = len(max(pr_dimensions, key=len))
for varstr, dimstr, namestr in zip(pr_varnames, pr_dimensions, pr_longnames):
print fmt.format(varstr,pr_varnames_maxlen,dimstr,pr_dimensions_maxlen,namestr)
if 'history' in ncobj.ncattrs():
print 'history :: ' + ncobj.history[:]
else:
if history_only:
print 'No history found'
if __name__ == '__main__':
import getopt, os
import argparse
import copy
class DictAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
try:
k, v = values.split("=", 1)
except ValueError:
raise argparse.ArgumentError(self, "Format must be key=value")
# Implementation is from argparse._AppendAction
items = copy.copy(argparse._ensure_value(namespace, self.dest, {})) # Default mutables, use copy!
try:
items[k] = int(v)
except ValueError:
raise argparse.ArgumentError(self, "value must be an integer")
if items[k] < 0: raise argparse.ArgumentError(self, "value cannot be negative")
setattr(namespace, self.dest, items)
def positive_int(value):
ivalue = int(value)
if ivalue < 1:
raise argparse.ArgumentTypeError("%s is an invalid positive int value" % value)
return ivalue
parser = argparse.ArgumentParser(description="Output summary information about a netCDF file")
parser.add_argument("-v","--verbose", help="Verbose output", action='store_true')
parser.add_argument("-t","--time", help="Show time variables", action='store_true')
parser.add_argument("-d","--dims", help="Show dimensions", action='store_true')
parser.add_argument("-a","--aggregate", help="Aggregate multiple netCDF files into one dataset", action='store_true')
parser.add_argument("-va","--vars", help="Show info for only specify variables", action='append')
parser.add_argument("-hi","--history", help="Show only netcdf history", action='store_true')
parser.add_argument("inputs", help="netCDF files", nargs='+')
args = parser.parse_args()
verbose = args.verbose
if args.aggregate:
ncinfo(args.inputs, not args.dims, not args.time, args.vars, args.history)
else:
for ncinput in args.inputs:
ncinfo(ncinput, not args.dims, not args.time, args.vars, args.history)