From 8a4f4297b53c17835c408aea65ead0b809c31ae3 Mon Sep 17 00:00:00 2001 From: "edward.safford" Date: Thu, 9 Nov 2023 20:55:14 +0000 Subject: [PATCH] Ref #161 Changes to mon_data_space.py for MinMon support. --- src/eva/data/mon_data_space.py | 51 ++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/eva/data/mon_data_space.py b/src/eva/data/mon_data_space.py index b273a150..f7f31741 100644 --- a/src/eva/data/mon_data_space.py +++ b/src/eva/data/mon_data_space.py @@ -9,9 +9,10 @@ # -------------------------------------------------------------------------------------------------- - +import struct import os import numpy as np +import array from xarray import Dataset, concat, merge, align from scipy.io import FortranFile @@ -162,9 +163,10 @@ def execute(self, dataset_config, data_collections, timing): ds, chans_dict = \ self.subset_coordinate(ds, coord_dict[x][1], requested_coord[x], chans_dict) - # Conditionally add channel, level, and scan related variables - # ------------------------------------------------------------ - ds = self.loadConditionalItems(ds, chans_dict, levs_dict, scanpo) + # Conditionally add channel, level, scan, and iteration related variables + # ----------------------------------------------------------------------- + iterations = x_range if 'Iteration' in coords.values() else None + ds = self.loadConditionalItems(ds, chans_dict, levs_dict, scanpo, iterations) # Rename variables with group rename_dict = {} @@ -448,7 +450,9 @@ def get_ctl_dict(self, control_file): levs_dict = {'levels': levs, 'levels_assim': level_assim, 'levels_nassim': level_nassim} + fp.close() + return coords, dims, attribs, nvars, vars, scanpo, levs_dict, chans_dict # ---------------------------------------------------------------------------------------------- @@ -640,14 +644,17 @@ def read_ieee(self, file_name, coords, dims, ndims_used, dims_arr, nvars, vars, for x in range(nvars): if load_data: if ndims_used == 1: - arr = f.read_reals(dtype=np.dtype('>f4')) + with open(filename, 'rb') as infile: + binary_data = infile.read() + + arr = array.array('f') + arr.frombytes(binary_data) else: arr = np.transpose(f.read_reals(dtype=np.dtype('>f4')).reshape(dimensions)) else: arr = zarray rtn_array = np.append(rtn_array, [arr], axis=0) - if load_data: f.close() return rtn_array, cycle_tm @@ -864,22 +871,41 @@ def load_dset(self, vars, nvars, coords, darr, dims, ndims_used, # create dataset from file components rtn_ds = None + new_coords = {} for x in range(0, nvars): if ndims_used == 1: d = { vars[x]: {"dims": (coords[dims_arr[0]]), "data": darr[x, :]} } + + # MinMon plots require both the 'allgnorm' data and log('allgnorm'). + if vars[x] == 'allgnorm': + d.update({ + "log_gnorm": {"dims": (coords[dims_arr[0]]), "data": np.log(darr[x, :])} + }) + new_coords = { + coords[dims_arr[0]]: x_range + } if ndims_used == 2: d = { vars[x]: {"dims": (coords[dims_arr[0]], coords[dims_arr[1]]), "data": darr[x, :, :]} } + new_coords = { + coords[dims_arr[0]]: x_range, + coords[dims_arr[1]]: y_range + } if ndims_used == 3: d = { vars[x]: {"dims": (coords[dims_arr[0]], coords[dims_arr[1]], coords[dims_arr[2]]), "data": darr[x, :, :]} } + new_coords = { + coords[dims_arr[0]]: x_range, + coords[dims_arr[1]]: y_range, + coords[dims_arr[2]]: z_range + } if 'Channel' in coords.values(): d.update({"Channel": {"dims": ("Channel"), "data": channo}}) @@ -887,12 +913,6 @@ def load_dset(self, vars, nvars, coords, darr, dims, ndims_used, new_ds = Dataset.from_dict(d) rtn_ds = new_ds if rtn_ds is None else rtn_ds.merge(new_ds) - # Define new coordinates - new_coords = { - coords[dims_arr[0]]: x_range, - coords[dims_arr[1]]: y_range - } - # Add the new coordinates to the dataset rtn_ds = rtn_ds.assign_coords(new_coords) @@ -931,7 +951,7 @@ def load_dset(self, vars, nvars, coords, darr, dims, ndims_used, # ---------------------------------------------------------------------------------------------- - def loadConditionalItems(self, dataset, chans_dict, levs_dict, scanpo): + def loadConditionalItems(self, dataset, chans_dict, levs_dict, scanpo, iterations=None): """ Add channel, level, and scan related variables to the dataset. @@ -941,7 +961,7 @@ def loadConditionalItems(self, dataset, chans_dict, levs_dict, scanpo): chans_dict (dict): Dictionary of channel components. levs_dict (dict): Dictionary of level components. scanpo (list): List of scan positions. - + iterations (list): List of iterations. Returns: xarray.Dataset: Dataset with added scan-related variables. """ @@ -973,6 +993,9 @@ def loadConditionalItems(self, dataset, chans_dict, levs_dict, scanpo): scan_array[:, x] = np.array([scanpo]) dataset['scan'] = (['Scan', 'Channel'], scan_array) + if iterations is not None: + dataset['iteration'] = (['Iteration'], iterations) + return dataset # ----------------------------------------------------------------------------------------------