def col_cmd(main_arguments): """ Main routine for handling calls to the collocate ('col') command. :param main_arguments: The command line arguments (minus the col command) """ from cis.exceptions import ClassNotFoundError, CISError from cis.collocation.col import Collocate output_file = main_arguments.output data_reader = DataReader() missing_data_for_missing_samples = False if main_arguments.samplevariable is not None: sample_data = data_reader.read_data_list(main_arguments.samplefiles, main_arguments.samplevariable, main_arguments.sampleproduct)[0] else: sample_data = data_reader.read_coordinates(main_arguments.samplefiles, main_arguments.sampleproduct) missing_data_for_missing_samples = True try: col = Collocate(sample_data, missing_data_for_missing_samples) except IOError as e: __error_occurred("There was an error reading one of the files: \n" + str(e)) col_name = main_arguments.samplegroup['collocator'][0] if main_arguments.samplegroup[ 'collocator'] is not None else None col_options = main_arguments.samplegroup['collocator'][1] if main_arguments.samplegroup[ 'collocator'] is not None else {} kern_name = main_arguments.samplegroup['kernel'][0] if main_arguments.samplegroup['kernel'] is not None else None kern_options = main_arguments.samplegroup['kernel'][1] if main_arguments.samplegroup['kernel'] is not None else None for input_group in main_arguments.datagroups: variables = input_group['variables'] filenames = input_group['filenames'] product = input_group["product"] if input_group["product"] is not None else None data = data_reader.read_data_list(filenames, variables, product) data_writer = DataWriter() try: output = col.collocate(data, col_name, col_options, kern_name, kern_options) data_writer.write_data(output, output_file) except ClassNotFoundError as e: __error_occurred(str(e) + "\nInvalid collocation option.") except (CISError, IOError) as e: __error_occurred(e)
def test_GIVEN_GriddedData_WHEN_collocate_THEN_collocator_called_correctly(self): sample = gridded_data.make_from_cube(mock.make_square_5x3_2d_cube()) out_name = "output-name" col_name = "dummy" col_options = {} mock_collocator = MagicMock() mock_collocator.collocate = MagicMock(return_value=GriddedDataList([sample])) mock_constraint = MagicMock() mock_kernel = MagicMock() mock_collocator_factory = CollocatorFactory() mock_collocator_factory.get_collocator_instances_for_method = MagicMock( return_value=(mock_collocator, mock_constraint, mock_kernel) ) col = Collocate(sample, out_name, collocator_factory=mock_collocator_factory) data = gridded_data.make_from_cube(mock.make_square_5x3_2d_cube()) output = col.collocate(data, col_name, col_options) assert_that(mock_collocator_factory.get_collocator_instances_for_method.call_count, is_(1)) factory_call_args = mock_collocator_factory.get_collocator_instances_for_method.call_args_list[0][0] assert_that(factory_call_args[0], is_(col_name)) assert_that(factory_call_args[1], is_(None)) col_options["missing_data_for_missing_sample"] = False assert_that(factory_call_args[2], is_(col_options)) assert_that(factory_call_args[3], is_(None)) assert_that(factory_call_args[4], is_(True)) assert_that(factory_call_args[5], is_(True)) assert_that(mock_collocator.collocate.call_count, is_(1)) collocator_args = mock_collocator.collocate.call_args_list[0][0] assert_that(collocator_args[0], is_(sample)) assert_that(collocator_args[1], is_(data)) assert_that(collocator_args[2], is_(mock_constraint)) assert_that(collocator_args[3], is_(mock_kernel)) assert_that(isinstance(output, GriddedDataList)) assert_that(len(output), is_(1)) assert_that(output[0].data.flatten().tolist(), is_(sample.data.flatten().tolist()))