@@ -24,7 +24,6 @@ class Dataset:
2424 An object that represent an entire complete dataset --
2525 a collection of Variables and the Grid that they are stored on.
2626 """
27-
2827 def __init__ (self ,
2928 ncfile = None ,
3029 grid = None ,
@@ -68,35 +67,55 @@ def __init__(self,
6867 # raise ValueError("don't create from a file")
6968 warnings .warn ("Creating a Dataset from a netcdfile directly is deprecated. "
7069 "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 )
7272
7373 if ncfile is not None :
7474 if (grid is not None or
7575 variables is not None or
7676 attributes is not None ):
7777 raise ValueError ("You can create a Dataset from a file, or from raw data"
7878 "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.
8781 self .filename = None
8882 self .grid = grid
8983 self .variables = {} if variables is None else variables
9084 self .attributes = {} if attributes is None else attributes
9185
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+
92112 @classmethod
93113 def from_netCDF (cls ,
94114 filename = None ,
95115 grid_file = None ,
96116 variable_files = None ,
97117 grid_topology = None ):
98118 """
99-
100119 NOTE: only loading from a single file is currently implemented.
101120 you can create a DATaset by hand, by loading the grid and
102121 variables separately, and then adding them
@@ -117,20 +136,13 @@ def from_netCDF(cls,
117136 :type grid_topology: mapping with keys of topology components and values are
118137 variable names.
119138 """
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:
124140 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 )
134146 return ds
135147
136148 def __getitem__ (self , key ):
@@ -147,10 +159,15 @@ def __str__(self):
147159 return descp
148160
149161
150- def _load_variables (self , ds ):
162+ def _variables_from_netCDF (self , ds ):
151163 """
152164 load up the variables in the nc file
165+
166+ :param ds: initialized netCDF dataset
153167 """
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.
154171 variables = {}
155172 for k in ds .variables .keys ():
156173 # find which netcdf variables are used to define the grid
0 commit comments