def rxnday_func(self, ci, num_days, freq, center_mean_on_last_day): climdex = get_package("climdex.pcic") if num_days == 1: return climdex.climdex_rx1day(ci, freq) elif num_days == 5: return climdex.climdex_rx5day(ci, freq, center_mean_on_last_day)
def threshold_func(self, threshold, ci): climdex = get_package("climdex.pcic") if threshold == 10.0: return climdex.climdex_r10mm(ci) if threshold == 20.0: return climdex.climdex_r20mm(ci) else: return climdex.climdex_rnnmm(ci, threshold)
def _handler(self, request, response): ( base_range, cal, date_fields, date_format, loglevel, max_missing_days, min_base_data_fraction_present, n, northern_hemisphere, output_file, prec_column, prec_file, prec_name, prec_qtiles, quantiles, tavg_column, tavg_file, tavg_name, temp_qtiles, tmax_column, tmax_file, tmax_name, tmin_column, tmin_file, tmin_name, vector_name, ) = process_inputs_alpha(request.inputs, raw_inputs, self.workdir) validate_vectors([ base_range, date_fields, temp_qtiles, prec_qtiles, max_missing_days, ]) log_handler( self, response, "Starting Process", logger, log_level=loglevel, process_step="start", ) climdex = get_package("climdex.pcic") robjects.r("library(PCICt)") log_handler( self, response, "Prepare parameters for climdexInput.raw", logger, log_level=loglevel, process_step="prepare_params", ) params = self.prepare_parameters( request, tmax_name, tmin_name, prec_name, tavg_name, tmax_column, tmin_column, prec_column, tavg_column, date_fields, date_format, cal, prec_file, tavg_file, tmax_file, tmin_file, ) log_handler( self, response, "Processing climdexInput.raw", logger, log_level=loglevel, process_step="process", ) try: ci = climdex.climdexInput_raw( **params, base_range=robjects.r(base_range), n=n, northern_hemisphere=northern_hemisphere, quantiles=robjects.r(quantiles), temp_qtiles=robjects.r(temp_qtiles), prec_qtiles=robjects.r(prec_qtiles), max_missing_days=robjects.r(max_missing_days), min_base_data_fraction_present=min_base_data_fraction_present, ) except RRuntimeError as e: raise ProcessError(msg=f"{type(e).__name__}: {str(e)}") log_handler( self, response, "Saving climdexInput as R data file", logger, log_level=loglevel, process_step="save_rdata", ) output_path = os.path.join(self.workdir, output_file) r_valid_name(vector_name) save_python_to_rdata(vector_name, ci, output_path) log_handler( self, response, "Building final output", logger, log_level=loglevel, process_step="build_output", ) response.outputs["climdexInput"].file = output_path # Clear R global env robjects.r("rm(list=ls())") return response
def _handler(self, request, response): climdex_input, freq, loglevel, month_type, output_file = process_inputs_alpha( request.inputs, mmdmt_inputs, self.workdir) log_handler( self, response, "Starting Process", logger, log_level=loglevel, process_step="start", ) climdex = get_package("climdex.pcic") vectors = [] counter = 1 total = len(climdex_input) for input in climdex_input: log_handler( self, response, f"Loading climdexInput from R data file {counter}/{total}", logger, log_level=loglevel, process_step="load_rdata", ) cis = load_cis(input) log_handler( self, response, f"Processing {month_type} for file {counter}/{total}", logger, log_level=loglevel, process_step="process", ) for ci_name, ci in cis.items(): try: robjects.r.assign("ci", ci) temps = robjects.r( f"climdex.{month_type}(ci, freq='{freq}')") except RRuntimeError as e: raise ProcessError(msg=f"{type(e).__name__}: {str(e)}") vector_name = f"{month_type}_{freq}{counter}_{ci_name}" robjects.r.assign(vector_name, temps) vectors.append(vector_name) counter += 1 log_handler( self, response, f"Saving {month_type} vector to R data file", logger, log_level=loglevel, process_step="save_rdata", ) output_path = os.path.join(self.workdir, output_file) robjects.r["save"](*vectors, file=output_path) log_handler( self, response, "Building final output", logger, log_level=loglevel, process_step="build_output", ) response.outputs["rda_output"].file = output_path # Clear R global env robjects.r("rm(list=ls())") log_handler( self, response, "Process Complete", logger, log_level=loglevel, process_step="complete", ) return response
def _handler(self, request, response): ci_name, climdex_single_input, loglevel, output_file = process_inputs_alpha( request.inputs, avail_indices_inputs, self.workdir) log_handler( self, response, "Starting Process", logger, log_level=loglevel, process_step="start", ) climdex = get_package("climdex.pcic") log_handler( self, response, "Loading climdexInput from R data file", logger, log_level=loglevel, process_step="load_rdata", ) ci = get_robj(climdex_single_input, ci_name) log_handler( self, response, "Processing climdex_get_available_indices", logger, log_level=loglevel, process_step="process", ) try: robjects.r.assign("ci", ci) avail_indices = climdex.climdex_get_available_indices(ci, False) except RRuntimeError as e: raise ProcessError(msg=f"{type(e).__name__}: {str(e)}") avail_processes = self.available_processes(avail_indices) log_handler( self, response, "Building final output", logger, log_level=loglevel, process_step="build_output", ) response.outputs["avail_processes"].data = avail_processes # Clear R global env robjects.r("rm(list=ls())") log_handler( self, response, "Process Complete", logger, log_level=loglevel, process_step="complete", ) return response
def test_get_package_err(package): with pytest.raises(ProcessError) as e: get_package(package) assert str(vars(e)["_excinfo"][1]) == f"R package, {package}, is not installed"
def test_get_package(package): pkg = get_package(package) assert pkg.__dict__["__rname__"] == package
def _handler(self, request, response): ( data_file, data_vector, loglevel, output_file, quantiles_vector, vector_name, ) = process_inputs_alpha(request.inputs, quantile_inputs, self.workdir) validate_vectors([quantiles_vector]) r_valid_name(vector_name) log_handler( self, response, "Starting Process", logger, log_level=loglevel, process_step="start", ) climdex = get_package("climdex.pcic") log_handler( self, response, "Loading R data file", logger, log_level=loglevel, process_step="load_rdata", ) if data_file: data = self.unpack_data_file(data_file, data_vector) else: data = robjects.r(data_vector) log_handler( self, response, "Processing climdex.quantile", logger, log_level=loglevel, process_step="process", ) try: quantiles = robjects.r(quantiles_vector) quantile_vector = climdex.climdex_quantile(data, quantiles) except RRuntimeError as e: raise ProcessError(msg=f"{type(e).__name__}: {str(e)}") log_handler( self, response, "Saving quantile as R data file", logger, log_level=loglevel, process_step="save_rdata", ) output_path = os.path.join(self.workdir, output_file) save_python_to_rdata(vector_name, quantile_vector, output_path) log_handler( self, response, "Building final output", logger, log_level=loglevel, process_step="build_output", ) response.outputs["rda_output"].file = output_path response.outputs["output_vector"].data = str(quantile_vector) # Clear R global env robjects.r("rm(list=ls())") log_handler( self, response, "Process Complete", logger, log_level=loglevel, process_step="complete", ) return response
def _handler(self, request, response): climdex_input, loglevel, output_file, threshold = process_inputs_alpha( request.inputs, rmm_inputs, self.workdir) log_handler( self, response, "Starting Process", logger, log_level=loglevel, process_step="start", ) climdex = get_package("climdex.pcic") vectors = [] counter = 1 total = len(climdex_input) for input in climdex_input: log_handler( self, response, f"Loading climdexInput from R data file {counter}/{total}", logger, log_level=loglevel, process_step="load_rdata", ) cis = load_cis(input) log_handler( self, response, f"Processing the annual count of days where daily precipitation is more than {threshold}mm per day for file {counter}/{total}", logger, log_level=loglevel, process_step="process", ) for ci_name, ci in cis.items(): try: robjects.r.assign("ci", ci) count_days = self.threshold_func(threshold, ci) except RRuntimeError as e: raise ProcessError(msg=f"{type(e).__name__}: {str(e)}") vector_name = f"r{threshold}mm{counter}_{ci_name}" robjects.r.assign(vector_name, count_days) vectors.append(vector_name) counter += 1 log_handler( self, response, f"Saving climdex.r{threshold}mm outputs to R data file", logger, log_level=loglevel, process_step="save_rdata", ) output_path = os.path.join(self.workdir, output_file) robjects.r["save"](*vectors, file=output_path) log_handler( self, response, "Building final output", logger, log_level=loglevel, process_step="build_output", ) response.outputs["rda_output"].file = output_path # Clear R global env robjects.r("rm(list=ls())") log_handler( self, response, "Process Complete", logger, log_level=loglevel, process_step="complete", ) return response