Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up mon_data_space.py #176

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions src/eva/data/mon_data_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ def execute(self, dataset_config, data_collections, timing):

dims_arr = []
if self.is_stn_data(control_file[0]):
coords, dims, attribs, nvars, vars, scanpo, levs_dict, chans_dict, datatype_dict = (
coords, dims, attribs, vars, scanpo, levs_dict, chans_dict, datatype_dict = (
self.get_stn_ctl_dict(control_file[0]))
ndims_used = 2
dims_arr = ['xdef', 'ydef', 'zdef']
stn_data = True
else:
coords, dims, attribs, nvars, vars, scanpo, levs_dict, chans_dict, datatype_dict = (
coords, dims, attribs, vars, scanpo, levs_dict, chans_dict, datatype_dict = (
self.get_ctl_dict(control_file[0]))
ndims_used, dims_arr = self.get_ndims_used(dims)

Expand Down Expand Up @@ -116,21 +116,20 @@ def execute(self, dataset_config, data_collections, timing):
# Read station data file. Note that the variable dimensions
# will NOT be the same for different station data files.
darr, cycle_tm, dims, lat, lon = self.read_stn_ieee(filename, coords, dims,
ndims_used, dims_arr, nvars,
vars)
ndims_used, dims_arr, vars)
y_range = np.arange(1, dims['ydef']+1)

else:
# read data file
darr, cycle_tm = self.read_ieee(filename, coords, dims, ndims_used,
dims_arr, nvars, vars, gsistat=attribs['gsistat'])
dims_arr, vars, gsistat=attribs['gsistat'])

# add cycle as a variable to data array
cyc_darr = self.var_to_np_array(dims, ndims_used, dims_arr, cycle_tm)

# create dataset from file contents
timestep_ds = None
timestep_ds = self.load_dset(vars, nvars, coords, darr, dims, ndims_used,
timestep_ds = self.load_dset(vars, coords, darr, dims, ndims_used,
dims_arr, x_range, y_range, z_range, cyc_darr)

if attribs['sat']:
Expand Down Expand Up @@ -168,7 +167,7 @@ def execute(self, dataset_config, data_collections, timing):
# Drop coordinates not in requested list
# --------------------------------------
for x in range(len(coord_dict)):
if drop_coord[x]:
if drop_coord[x] and coord_dict[x][1] in list(ds.coords):
ds, chans_dict = \
self.subset_coordinate(ds, coord_dict[x][1], requested_coord[x], chans_dict)

Expand Down Expand Up @@ -305,7 +304,6 @@ def get_ctl_dict(self, control_file):
dict: Dictionary containing various coordinates and information.
dict: Dictionary containing dimension sizes.
dict: Dictionary containing sensor and satellite attributes.
int: Number of variables.
list: List of variable names.
list: List of scan positions.
dict: Dictionary containing channel information.
Expand Down Expand Up @@ -490,7 +488,7 @@ def get_ctl_dict(self, control_file):
'assim': datatype_assim}

fp.close()
return coords, dims, attribs, nvars, vars, scanpo, levs_dict, chans_dict, datatype_dict
return coords, dims, attribs, vars, scanpo, levs_dict, chans_dict, datatype_dict

# ----------------------------------------------------------------------------------------------

Expand All @@ -506,7 +504,6 @@ def get_stn_ctl_dict(self, control_file):
dict: Dictionary containing various coordinates and information.
dict: Dictionary containing dimension sizes.
dict: Dictionary containing sensor and satellite attributes.
int: Number of variables.
list: List of variable names.
list: List of scan positions.
dict: Dictionary containing channel information.
Expand Down Expand Up @@ -579,7 +576,6 @@ def get_stn_ctl_dict(self, control_file):
strs = lines[x].split()
if strs[-1] not in vars:
vars.append(strs[-1])
nvars = len(vars)

# set levels
dim_list.append(len(lev_vals))
Expand All @@ -604,11 +600,11 @@ def get_stn_ctl_dict(self, control_file):
'assim': datatype_assim}

fp.close()
return coords, dims, attribs, nvars, vars, scanpo, levs_dict, chans_dict, datatype_dict
return coords, dims, attribs, vars, scanpo, levs_dict, chans_dict, datatype_dict

# ----------------------------------------------------------------------------------------------

def read_ieee(self, file_name, coords, dims, ndims_used, dims_arr, nvars, vars,
def read_ieee(self, file_name, coords, dims, ndims_used, dims_arr, vars,
file_path=None, gsistat=False):

"""
Expand All @@ -620,7 +616,6 @@ def read_ieee(self, file_name, coords, dims, ndims_used, dims_arr, nvars, vars,
dims (dict): Dictionary of dimension sizes.
ndims_used (int): Number of dimensions used.
dims_arr (list): List of dimension names used.
nvars (int): Number of variables.
vars (list): List of variable names.
file_path (str, optional): Path to the directory containing the file. Defaults to None.

Expand Down Expand Up @@ -667,7 +662,7 @@ def read_ieee(self, file_name, coords, dims, ndims_used, dims_arr, nvars, vars,

dimensions = [dims[dims_arr[0]], dims[dims_arr[1]]]

for x in range(nvars):
for x in range(len(vars)):

if load_data:
# satang variable is not used and a non-standard size
Expand All @@ -689,7 +684,7 @@ def read_ieee(self, file_name, coords, dims, ndims_used, dims_arr, nvars, vars,
rtn_array = np.append(rtn_array, [tarr], axis=0)

else: # ndims_used == 1|2
for x in range(nvars):
for x in range(len(vars)):
if load_data:
if ndims_used == 1:
if gsistat:
Expand All @@ -713,7 +708,7 @@ def read_ieee(self, file_name, coords, dims, ndims_used, dims_arr, nvars, vars,
# ----------------------------------------------------------------------------------------------

def read_stn_ieee(self, file_name, coords, dims, ndims_used, dims_arr,
nvars, vars, file_path=None):
vars, file_path=None):

"""
Read station data from an IEEE file and arrange it into a numpy array.
Expand All @@ -724,7 +719,6 @@ def read_stn_ieee(self, file_name, coords, dims, ndims_used, dims_arr,
dims (dict): Dictionary of dimension sizes.
ndims_used (int): Number of dimensions used.
dims_arr (list): List of dimension names used.
nvars (int): Number of variables.
vars (list): List of variable names.
file_path (str, optional): Path to the directory containing the file. Defaults to None.

Expand Down Expand Up @@ -772,7 +766,7 @@ def read_stn_ieee(self, file_name, coords, dims, ndims_used, dims_arr,
if nlev:
lat.append(record[1])
lon.append(record[2])
data = f.read_record('>f4').reshape([nvars, dims[dims_arr[0]]])
data = f.read_record('>f4').reshape([len(vars), dims[dims_arr[0]]])
numobs += 1
mylist.append(data)

Expand Down Expand Up @@ -901,15 +895,14 @@ def get_ndims_used(self, dims):

# ----------------------------------------------------------------------------------------------

def load_dset(self, vars, nvars, coords, darr, dims, ndims_used,
def load_dset(self, vars, coords, darr, dims, ndims_used,
dims_arr, x_range, y_range, z_range, cyc_darr=None):

"""
Create a dataset from various components.

Args:
vars (list): List of variable names.
nvars (int): Number of variables.
coords (dict): Dictionary of coordinates.
darr (numpy.ndarray): Numpy array of data.
dims (dict): Dictionary of dimension sizes.
Expand All @@ -928,7 +921,7 @@ def load_dset(self, vars, nvars, coords, darr, dims, ndims_used,
rtn_ds = None

new_coords = {}
for x in range(0, nvars):
for x in range(0, len(vars)):
if ndims_used == 1:
d = {
vars[x]: {"dims": (coords[dims_arr[0]]), "data": darr[x, :]}
Expand Down
Loading