@@ -24,7 +24,6 @@ class Dataset:
24
24
An object that represent an entire complete dataset --
25
25
a collection of Variables and the Grid that they are stored on.
26
26
"""
27
-
28
27
def __init__ (self ,
29
28
ncfile = None ,
30
29
grid = None ,
@@ -68,35 +67,55 @@ def __init__(self,
68
67
# raise ValueError("don't create from a file")
69
68
warnings .warn ("Creating a Dataset from a netcdfile directly is deprecated. "
70
69
"Please use Dataset.from_netCDF() instead. "
71
- "Or one of the utilities in gridded.io" , DeprecationWarning )
70
+ "Or use one of the utilities in gridded.io" ,
71
+ DeprecationWarning )
72
72
73
73
if ncfile is not None :
74
74
if (grid is not None or
75
75
variables is not None or
76
76
attributes is not None ):
77
77
raise ValueError ("You can create a Dataset from a file, or from raw data"
78
78
"but not both." )
79
- self .nc_dataset = get_dataset (ncfile )
80
- self .filename = self .nc_dataset .filepath ()
81
- self .grid = Grid .from_netCDF (filename = self .filename ,
82
- dataset = self .nc_dataset ,
83
- grid_topology = grid_topology )
84
- self .variables = self ._load_variables (self .nc_dataset )
85
- self .attributes = get_dataset_attrs (self .nc_dataset )
86
- else : # no file passed in -- create from grid and variables
79
+ self ._init_from_netCDF (ncfile )
80
+ else : # Create from grid and variables -- this is what should usually happen.
87
81
self .filename = None
88
82
self .grid = grid
89
83
self .variables = {} if variables is None else variables
90
84
self .attributes = {} if attributes is None else attributes
91
85
86
+
87
+ def _init_from_netCDF (self ,
88
+ filename = None ,
89
+ grid_file = None ,
90
+ variable_files = None ,
91
+ grid_topology = None ):
92
+ """
93
+ internal implementation -- users should call the .from_netCDF()
94
+ classmethod -- see its docstring for usage.
95
+
96
+ This is used to initialize a dataset from a netCDF file --
97
+ done this way, so it can be called from more than one place.
98
+ """
99
+ if (grid_file is not None ) or (variable_files is not None ):
100
+ raise NotImplementedError ("Loading from separate netcdf files is not yet supported" )
101
+
102
+ self .nc_dataset = get_dataset (filename )
103
+ self .filename = self .nc_dataset .filepath ()
104
+ self .grid = Grid .from_netCDF (filename = self .filename ,
105
+ dataset = self .nc_dataset ,
106
+ grid_topology = grid_topology )
107
+ # fixme: this should load the depth and time, and then the variables.
108
+ self .variables = self ._variables_from_netCDF (self .nc_dataset )
109
+ self .attributes = get_dataset_attrs (self .nc_dataset )
110
+
111
+
92
112
@classmethod
93
113
def from_netCDF (cls ,
94
114
filename = None ,
95
115
grid_file = None ,
96
116
variable_files = None ,
97
117
grid_topology = None ):
98
118
"""
99
-
100
119
NOTE: only loading from a single file is currently implemented.
101
120
you can create a DATaset by hand, by loading the grid and
102
121
variables separately, and then adding them
@@ -117,20 +136,13 @@ def from_netCDF(cls,
117
136
:type grid_topology: mapping with keys of topology components and values are
118
137
variable names.
119
138
"""
120
- if (grid_file is not None ) or (variable_files is not None ):
121
- raise NotImplementedError ("Loading from separate netcdf files is not yet supported" )
122
-
123
- # create an empty DAtaset:
139
+ # create an empty Dataset:
124
140
ds = cls ()
125
-
126
- ds .nc_dataset = get_dataset (filename )
127
- ds .filename = ds .nc_dataset .filepath ()
128
- ds .grid = Grid .from_netCDF (filename = ds .filename ,
129
- dataset = ds .nc_dataset ,
130
- grid_topology = grid_topology )
131
- ds .variables = ds ._load_variables (ds .nc_dataset )
132
- ds .attributes = get_dataset_attrs (ds .nc_dataset )
133
-
141
+ # initialize it
142
+ ds ._init_from_netCDF (filename ,
143
+ grid_file ,
144
+ variable_files ,
145
+ grid_topology )
134
146
return ds
135
147
136
148
def __getitem__ (self , key ):
@@ -147,10 +159,15 @@ def __str__(self):
147
159
return descp
148
160
149
161
150
- def _load_variables (self , ds ):
162
+ def _variables_from_netCDF (self , ds ):
151
163
"""
152
164
load up the variables in the nc file
165
+
166
+ :param ds: initialized netCDF dataset
153
167
"""
168
+ # fixme: this needs work
169
+ # It *should* have already gotten the grid and depth and time,
170
+ # and then the variables can be loaded directly.
154
171
variables = {}
155
172
for k in ds .variables .keys ():
156
173
# find which netcdf variables are used to define the grid
0 commit comments