def unify_cube_format(self, params, output, time_constr): """ Ensure that all simulations have the same cube format i.e the same time coordinates and calendar, attributes, data type and auxillary coords. Time coordinate units and time point definitions can also be set. Example: units are currently set to be unified as 'days since 1950-01-01 00:00:00' and daily data is defined at the hour of midday. :param iris.cube.Cube params: Cube to reformat :param iris.cube.CubeList output: Cube list to contain reformatted cubes :param np.array t_constr: A two element array for specifying start and end year of data :return iris.cube.Cube: Reformatted cube """ cube = params.get() logger.debug('Unifying formatting for ' + cube.coord('simulation_label').points[0]) cube = format.change_calendar(cube, time_constr, new_units='days since 1950-01-01 ' '00:00:00') cube = format.add_extra_time_coords(cube) cube = format.unify_data_type(cube) cube = format.set_blank_attributes(cube) cube = format.change_time_points(cube, hr=12) # daily data = midday cube = format.change_time_bounds(cube) cube = format.remove_extra_time_coords( cube) # remove non-essential coord output.append(cube)
def monthly_mean_anomaly(cube): """ Creates monthly mean time series from all months mean over time period :param cube: iris.cube.Cube :return: monthly mean anomaly iris.cube.Cube time series """ logger.debug('getting monthly mean anomaly ' + cube.coord('simulation_label').points[0]) cube = format.add_extra_time_coords(cube) monthly_mean_anomaly_list = iris.cube.CubeList([]) all_months_mean = cube.aggregated_by(['month'], iris.analysis.MEAN) monthly_analysis_array = monthly_analysis(cube) monthly_mean_cube = monthly_analysis_array[0] for mon in np.arange(1, 13, 1): cube_mean = monthly_mean_cube.extract( iris.Constraint(month_number=mon)) all_month_mean = all_months_mean.extract( iris.Constraint(month_number=mon)) cube_mean_anomaly = cube_mean - all_month_mean cube_mean_anomaly.rename(cube_mean.name() + '_anomaly') for i in np.arange(0, len(cube_mean_anomaly.coord('time').points), 1): cube = cube_mean_anomaly[i] cube = format.remove_extra_time_coords(cube) monthly_mean_anomaly_list.append(cube) # month_anomaly = monthly_mean_anomaly_list.merge_cube() # month_anomaly = format.change_time_points(month_anomaly, dy=1, hr=00) return monthly_mean_anomaly_list
def monthly_analysis(cube): print('getting monthly mean ' + cube.coord('simulation_label').points[0]) cube = format.add_extra_time_coords(cube) monthly_mean_cube = cube.aggregated_by(['month', 'year'], iris.analysis.MEAN) monthly_mean_cube.rename(monthly_mean_cube.name() + '_mean') monthly_max_cube = cube.aggregated_by(['month', 'year'], iris.analysis.MAX) monthly_max_cube.rename(monthly_max_cube.name() + '_max') monthly_min_cube = cube.aggregated_by(['month', 'year'], iris.analysis.MIN) monthly_min_cube.rename(monthly_min_cube.name() + '_min') cube_list = iris.cube.CubeList( [monthly_mean_cube, monthly_max_cube, monthly_min_cube]) return cube_list
def annual_mean(cube): """ Creates year-by-year annual mean time series :param cube: iris.cube.Cube :return: annual mean iris.cube.Cube time series """ logger.debug('getting annual mean ' + cube.coord('simulation_label').points[0]) cube = format.add_extra_time_coords(cube) annual_mean_cube = cube.aggregated_by('year', iris.analysis.MEAN) annual_mean_cube.rename(annual_mean_cube.name() + '_annual_mean') return annual_mean_cube
def daily_anomaly(cube): print('getting daily anomaly ' + cube.coord('simulation_label').points[0]) daily_mean_cube = format.add_extra_time_coords(cube) daily_mean_anomaly_list = iris.cube.CubeList([]) all_months_mean = cube.aggregated_by(['month'], iris.analysis.MEAN) for mon in np.arange(1, 13, 1): cube_mean = daily_mean_cube.extract(iris.Constraint(month_number=mon)) all_month_mean = all_months_mean.extract( iris.Constraint(month_number=mon)) cube_mean_anomaly = cube_mean - all_month_mean cube_mean_anomaly.rename(cube_mean.long_name + ' Anomaly') for i in np.arange(0, len(cube_mean_anomaly.coord('time').points), 1): cube = cube_mean_anomaly[i] cube = format.remove_extra_time_coords(cube) daily_mean_anomaly_list.append(cube) return daily_mean_anomaly_list
def monthly_analysis(cube): """ Creates month-by-month annual mean/max/min time series :param cube: iris.cube.Cube :return: monthly analysis iris.cube.Cube time series """ logger.debug('getting monthly mean ' + cube.coord('simulation_label').points[0]) cube = format.add_extra_time_coords(cube) monthly_mean_cube = cube.aggregated_by(['month', 'year'], iris.analysis.MEAN) monthly_mean_cube.rename(monthly_mean_cube.name() + '_mean') monthly_max_cube = cube.aggregated_by(['month', 'year'], iris.analysis.MAX) monthly_max_cube.rename(monthly_max_cube.name() + '_max') monthly_min_cube = cube.aggregated_by(['month', 'year'], iris.analysis.MIN) monthly_min_cube.rename(monthly_min_cube.name() + '_min') cube_list = iris.cube.CubeList( [monthly_mean_cube, monthly_max_cube, monthly_min_cube]) return cube_list
def monthly_maximum_anomaly(cube): print('getting monthly maximum anomaly ' + cube.coord('simulation_label').points[0]) cube = format.add_extra_time_coords(cube) monthly_max_anomaly_list = iris.cube.CubeList([]) all_months_mean = cube.aggregated_by(['month'], iris.analysis.MEAN) monthly_analysis_array = monthly_analysis(cube) monthly_max_cube = monthly_analysis_array[1] for mon in np.arange(1, 13, 1): cube_max = monthly_max_cube.extract(iris.Constraint(month_number=mon)) all_month_mean = all_months_mean.extract( iris.Constraint(month_number=mon)) cube_max_anomaly = cube_max - all_month_mean cube_max_anomaly.rename(cube_max.name() + '_anomaly') for i in np.arange(0, len(cube_max_anomaly.coord('time').points), 1): cube = cube_max_anomaly[i] cube = format.remove_extra_time_coords(cube) monthly_max_anomaly_list.append(cube) # month_anomaly = monthly_mean_anomaly_list.merge_cube() # month_anomaly = format.change_time_points(month_anomaly, dy=1, hr=00) return monthly_max_anomaly_list
def unify_cube_format(self, params, output, time_constr): """ Ensure that all simulations have the same cube format i.e the same time coordinates and calendar, attributes, data type and auxillary coords. Time coordinate units and time point definitions can also be set. Example: units are currently set to be unified as 'days since 1950-01-01 00:00:00' and daily data is defined at the hour of midday. """ cube=params.get() print('Unifying formatting for ' +cube.coord('simulation_label').points[0]) cube = format.change_calendar(cube, time_constr, new_units='days since 1950-01-01 ' '00:00:00') cube = format.add_extra_time_coords(cube) cube = format.unify_data_type(cube) cube = format.set_blank_attributes(cube) cube = format.change_time_points(cube, hr=12) # daily data = midday cube = format.change_time_bounds(cube) cube = format.remove_extra_time_coords(cube)# remove non-essential coord output.append(cube)
def annual_mean(cube): print('getting annual mean ' + cube.coord('simulation_label').points[0]) cube = format.add_extra_time_coords(cube) annual_mean_cube = cube.aggregated_by('year', iris.analysis.MEAN) annual_mean_cube.rename(annual_mean_cube.name() + '_annual_mean') return annual_mean_cube