def _create_coord_list(self, filename): import numpy as np coords = CoordList() time_data = read(filename, 'time')['time'] try: alt_data = read(filename, 'altitude')['altitude'] except InvalidVariableError: alt_data = read(filename, 'range')['range'] len_y = alt_data.shape[1] time_arr = utils.expand_1d_to_2d_array(time_data[:], len_y, axis=1) t_coord = Coord(time_arr, get_metadata(time_data), axis='x') t_coord.convert_to_std_time() coords.append(t_coord) #alt_arr = utils.expand_1d_to_2d_array(alt_data[:], len_x, axis=0) alt_arr = alt_data[:, :, 0] #eliminate "angle" axis #alt_arr = alt_data #eliminate "angle" axis coords.append(Coord(alt_arr, get_metadata(alt_data), axis='y')) lat_data = read(filename, 'latitude')['latitude'] lat_arr = np.ones(alt_arr.shape) * lat_data[:] coords.append(Coord(lat_arr, get_metadata(lat_data))) lon_data = read(filename, 'longitude')['longitude'] lon_arr = np.ones(alt_arr.shape) * lon_data[:] coords.append(Coord(lon_arr, get_metadata(lon_data))) return coords
def _create_coord_list(self, filename): import numpy as np coords = CoordList() time_data = read(filename, 'time')['time'] len_x = time_data.shape[0] try: alt_data = read(filename, 'altitude')['altitude'] except InvalidVariableError: alt_data = read(filename, 'range')['range'] len_y = alt_data.shape[0] time_arr = utils.expand_1d_to_2d_array(time_data[:], len_y, axis=1) t_coord = Coord(time_arr, get_metadata(time_data), axis='x') t_coord.convert_to_std_time() coords.append(t_coord) alt_arr = utils.expand_1d_to_2d_array(alt_data[:], len_x, axis=0) coords.append(Coord(alt_arr, get_metadata(alt_data), axis='y')) lat_data = read(filename, 'latitude')['latitude'] lat_arr = np.ones(alt_arr.shape) * lat_data[:] coords.append(Coord(lat_arr, get_metadata(lat_data))) lon_data = read(filename, 'longitude')['longitude'] lon_arr = np.ones(alt_arr.shape) * lon_data[:] coords.append(Coord(lon_arr, get_metadata(lon_data))) return coords
def _add_aux_coordinate(dim_coords, filename, aux_coord_name, length): """ Add an auxiliary coordinate to a list of (reshaped) dimension coordinates :param dim_coords: CoordList of one-dimensional coordinates representing physical dimensions :param filename: The data file containing the aux coord :param aux_coord_name: The name of the aux coord to add to the coord list :param length: The length of the data dimensions which this auxiliary coordinate should span :return: A CoordList of reshaped (2D) physical coordinates plus the 2D auxiliary coordinate """ from cis.data_io.Coord import Coord from cis.utils import expand_1d_to_2d_array from cis.data_io.netcdf import read # We assume that the auxilliary coordinate is the same shape across files d = read(filename, [aux_coord_name])[aux_coord_name] # Reshape to the length given aux_data = expand_1d_to_2d_array(d[:], length, axis=0) # Get the length of the auxiliary coordinate len_y = d[:].size for dim_coord in dim_coords: dim_coord.data = expand_1d_to_2d_array(dim_coord.data, len_y, axis=1) all_coords = dim_coords + [Coord(aux_data, get_metadata(d))] return all_coords
def create_coords(self, filenames, variable=None): """ Reads the coordinates and data if required from the files :param filenames: List of filenames to read coordinates from :param variable: load a variable for the data :return: Coordinates """ from iris.cube import Cube from iris.coords import DimCoord from cis.data_io.netcdf import read from cis.utils import concatenate data_variables, variable_selector = self._load_data(filenames, variable) aux_coords = self._create_coordinates_list(data_variables, variable_selector) dim_coords = [(DimCoord(np.arange(len(aux_coords[0].points)), var_name='obs'), (0,))] if variable is None: raise ValueError("Must specify variable") aux_coord_name = variable_selector.find_auxiliary_coordinate(variable) if aux_coord_name is not None: # We assume that the auxilliary coordinate is the same shape across files v = read(filenames[0], [aux_coord_name])[aux_coord_name] aux_meta = get_metadata(v) # We have to assume the shape here... dim_coords.append((DimCoord(v[:], var_name=aux_coord_name, units=aux_meta.units, long_name=aux_meta.long_name), (1,))) cube_meta = get_metadata(data_variables[variable][0]) return Cube(concatenate([d[:] for d in data_variables[variable]]), units=cube_meta.units, var_name=variable, long_name=cube_meta.long_name, dim_coords_and_dims=dim_coords, aux_coords_and_dims=[(c, (0,)) for c in aux_coords])
def _add_available_aux_coords(self, cube, filenames): from iris.aux_factory import HybridPressureFactory from iris.coords import AuxCoord from cis.data_io.netcdf import read ps_filenames = [ f.replace('concbc', 'ps_TL95L80_192x48NH_3hr') for f in filenames ] # These will be the same for all files hybrid_a = read(ps_filenames[0], 'a')['a'] hybrid_b = read(ps_filenames[0], 'b')['b'] hybrid_a_coord = AuxCoord( points=hybrid_a[:], long_name='vertical coordinate formula term: a(k)', units='Pa') hybrid_b_coord = AuxCoord( points=hybrid_b[:], long_name='vertical coordinate formula term: b(k)', units='1') # This needs to be from each file and then merged surface_pressure_cube = _get_cubes( ps_filenames, 'ps', callback=self.load_multiple_files_callback).concatenate_cube() surface_pressure = AuxCoord(points=surface_pressure_cube.data, standard_name='surface_air_pressure', long_name='surface pressure', units='Pa') # First convert the hybrid coefficients to hPa, so that air pressure will be in hPa hybrid_a_coord.convert_units('hPa') surface_pressure.convert_units('hPa') cube.add_aux_coord(surface_pressure, (0, 2, 3)) cube.add_aux_coord(hybrid_a_coord, (1, )) cube.add_aux_coord(hybrid_b_coord, (1, )) cube.add_aux_factory( HybridPressureFactory(delta=hybrid_a_coord, sigma=hybrid_b_coord, surface_air_pressure=surface_pressure))
def create_coords(self, filenames, variable=None): """ Reads the coordinates and data if required from the files :param filenames: List of filenames to read coordinates from :param variable: load a variable for the data :return: Coordinates """ from iris.cube import Cube from iris.coords import DimCoord from cis.data_io.netcdf import read from cis.utils import concatenate data_variables, variable_selector = self._load_data( filenames, variable) aux_coords = self._create_coordinates_list(data_variables, variable_selector) dim_coords = [(DimCoord(np.arange(len(aux_coords[0].points)), var_name='obs'), (0, ))] if variable is None: raise ValueError("Must specify variable") aux_coord_name = variable_selector.find_auxiliary_coordinate(variable) if aux_coord_name is not None: # We assume that the auxilliary coordinate is the same shape across files v = read(filenames[0], [aux_coord_name])[aux_coord_name] aux_meta = get_metadata(v) # We have to assume the shape here... dim_coords.append((DimCoord(v[:], var_name=aux_coord_name, units=aux_meta.units, long_name=aux_meta.long_name), (1, ))) cube_meta = get_metadata(data_variables[variable][0]) return Cube(concatenate([d[:] for d in data_variables[variable]]), units=cube_meta.units, var_name=variable, long_name=cube_meta.long_name, dim_coords_and_dims=dim_coords, aux_coords_and_dims=[(c, (0, )) for c in aux_coords])