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 can_not_convert_time_without_since_in_units_with_no_units(): times = numpy.array([0, 1]) units = "" time_stamp_info = "1601-01-01" coord = Coord(times, Metadata(units=units)) coord.convert_to_std_time(time_stamp_info)
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 _create_time_coord(self, timestamp, time_variable_name, data_variables, coord_axis='T', standard_name='time'): """ Create a time coordinate, taking into account the fact that each file may have a different timestamp. :param timestamp: Timestamp or list of timestamps for :param time_variable_name: Name of the time variable :param data_variables: Dictionary containing one or multiple netCDF data variables for each variable name :param coord_axis: Axis, default 'T' :param standard_name: Coord standard name, default 'time' :return: Coordinate """ from cis.data_io.Coord import Coord from six.moves import zip_longest timestamps = listify(timestamp) time_variables = data_variables[time_variable_name] time_coords = [] # Create a coordinate for each separate file to account for differing timestamps for file_time_var, timestamp in zip_longest(time_variables, timestamps): metadata = get_metadata(file_time_var) metadata.standard_name = standard_name coord = Coord(file_time_var, metadata, coord_axis) coord.convert_to_std_time(timestamp) time_coords.append(coord) return Coord.from_many_coordinates(time_coords)
def can_not_convert_time_without_since_in_units_with_no_units(): times = numpy.array([0, 1]) units = "" time_stamp_info = "1601-01-01" coord = Coord(times, Metadata(units=units)) coord.convert_to_std_time(time_stamp_info)
def create_coords(self, filenames, usr_variable=None): from cis.data_io.netcdf import read_many_files_individually, get_metadata, get_netcdf_file_variables from cis.data_io.Coord import Coord, CoordList from cis.data_io.ungridded_data import UngriddedCoordinates, UngriddedData from cis.exceptions import InvalidVariableError # We have to read it once first to find out which variables are in there. We assume the set of coordinates in # all the files are the same file_variables = get_netcdf_file_variables(filenames[0]) def get_axis_std_name(var): axis=None lvar = var.lower() if lvar.startswith('lon'): axis = 'x', 'longitude' if lvar.startswith('lat'): axis = 'y', 'latitude' if lvar == 'G_ALT' or lvar == 'altitude' or lvar == 'pressure_altitude': axis = 'z', 'altitude' if lvar == 'time': axis = 't', 'time' if lvar == 'p' or lvar == 'pressure' or lvar == 'static_pressure': axis = 'p', 'air_pressure' return axis all_coord_variables = [(v, get_axis_std_name(v)) for v in file_variables if get_axis_std_name(v) is not None] # Get rid of any duplicates coord_variables = [] for v in all_coord_variables: if v is None or v[1][1] not in [x[1][1] for x in coord_variables]: coord_variables.append(v) all_variables = coord_variables.copy() if usr_variable is not None: all_variables.append((usr_variable, '')) logging.info("Listing coordinates: " + str(all_variables)) coords = CoordList() var_data = read_many_files_individually(filenames, [v[0] for v in all_variables]) for name, axis_std_name in coord_variables: try: meta = get_metadata(var_data[name][0]) if meta.standard_name is None: meta.standard_name = axis_std_name[1] coord = Coord(var_data[name], meta, axis=axis_std_name[0]) if meta.standard_name == 'time': # Converting units to CIS std time coord.convert_to_std_time() coords.append(coord) except InvalidVariableError: pass if usr_variable is None: res = UngriddedCoordinates(coords) else: res = UngriddedData(var_data[usr_variable], get_metadata(var_data[usr_variable][0]), coords) return res
def can_convert_time_with_since_in_units(): times = numpy.array([0, 1]) units = "Days since 1601-01-01" coord = Coord(times, Metadata(units=units)) coord.convert_to_std_time() assert_that(coord.data_flattened[0], is_(366.0), "time point")
def can_convert_time_with_since_in_units(): times = numpy.array([0, 1]) units = "days since 1601-01-01" coord = Coord(times, Metadata(units=units)) coord.convert_to_std_time() assert_that(coord.data_flattened[0], is_(366.0), "time point")
def can_convert_time_without_since_in_units(): times = numpy.array([0, 1]) units = "Days from the file reference point 1601-01-01" time_stamp_info = "1601-01-01" coord = Coord(times, Metadata(units=units)) coord.convert_to_std_time(time_stamp_info) assert_that(coord.data_flattened[0], is_(366.0), "time point")
def can_convert_time_without_since_in_units(): times = numpy.array([0, 1]) units = "Days from the file reference point 1601-01-01" time_stamp_info = "1601-01-01" coord = Coord(times, Metadata(units=units)) coord.convert_to_std_time(time_stamp_info) assert_that(coord.data_flattened[0], is_(366.0), "time point")
def create_coords(self, filenames, usr_variable=None): from cis.data_io.netcdf import read_many_files_individually, get_metadata, get_netcdf_file_variables from cis.data_io.Coord import Coord, CoordList from cis.data_io.ungridded_data import UngriddedCoordinates, UngriddedData from cis.exceptions import InvalidVariableError # We have to read it once first to find out which variables are in there. We assume the set of coordinates in # all the files are the same file_variables = get_netcdf_file_variables(filenames[0]) def get_axis_std_name(var): axis = None lvar = var.lower() if lvar.startswith('lon'): axis = 'x', 'longitude' if lvar.startswith('lat'): axis = 'y', 'latitude' if lvar == 'G_ALT' or lvar == 'altitude' or lvar == 'pressure_altitude': axis = 'z', 'altitude' if lvar == 'time': axis = 't', 'time' if lvar == 'p' or lvar == 'pressure' or lvar == 'static_pressure': axis = 'p', 'air_pressure' return axis all_coord_variables = [(v, get_axis_std_name(v)) for v in file_variables if get_axis_std_name(v) is not None] # Get rid of any duplicates coord_variables = [] for v in all_coord_variables: if v is None or v[1][1] not in [x[1][1] for x in coord_variables]: coord_variables.append(v) all_variables = coord_variables.copy() if usr_variable is not None: all_variables.append((usr_variable, '')) logging.info("Listing coordinates: " + str(all_variables)) coords = CoordList() var_data = read_many_files_individually(filenames, [v[0] for v in all_variables]) for name, axis_std_name in coord_variables: try: meta = get_metadata(var_data[name][0]) if meta.standard_name is None: meta.standard_name = axis_std_name[1] coord = Coord(var_data[name], meta, axis=axis_std_name[0]) if meta.standard_name == 'time': # Remove the mask from the time coordinate since this screws up date conversion... coord.data = coord.data.data # Converting units to CIS std time coord.convert_to_std_time() coords.append(coord) except InvalidVariableError: pass if usr_variable is None: res = UngriddedCoordinates(coords) else: res = UngriddedData(var_data[usr_variable][0][0, ...], get_metadata(var_data[usr_variable][0]), coords) return res
def can_not_convert_time_without_since_in_units_but_no_timestamp(): times = numpy.array([0, 1]) units = "Days from the file reference point 1601-01-01" coord = Coord(times, Metadata(units=units)) coord.convert_to_std_time()
def can_not_convert_time_without_since_in_units_but_no_timestamp(): times = numpy.array([0, 1]) units = "Days from the file reference point 1601-01-01" coord = Coord(times, Metadata(units=units)) coord.convert_to_std_time()