Ejemplo n.º 1
0
    def test_data_1d(self):
        """
        Test that one-dimensional variable data is successfully written to
        file and can be retrieved on a later read. Ensures correct ordering
        of data in the file.
        """
        # Simple test with only one datapoint.
        reader1 = NetCDFReader(DATASET1)
        data1 = reader1.collect_untimed_data(READ_VAR_NAME)
        reader1.close()

        self.assertEqual(data1.shape, (1, 1))
        self.assertEqual(data1[0, 0], 1)

        # Test with multiple values in one dimension.
        reader5 = NetCDFReader(DATASET5)
        data5 = reader5.collect_untimed_data(READ_VAR_NAME)
        reader5.close()

        self.assertEqual(data5.shape, (5, 1))
        # Check a few values to demonstrate that the return value lines up
        # with expected value.
        self.assertEqual(data5[0, 0], 1)
        self.assertEqual(data5[1, 0], 2)
        self.assertEqual(data5[2, 0], 3)
        self.assertEqual(data5[3, 0], 4)
        self.assertEqual(data5[4, 0], 5)
Ejemplo n.º 2
0
    def test_read_single(self):
        """
        Test that a very simple dataset can be read successfully, opening the
        dataset and returning the only datapoint inside.
        """
        reader = NetCDFReader(DATASET1)
        data = reader.collect_untimed_data(READ_VAR_NAME)
        reader.close()

        self.assertEqual(data.shape, (1, 1))
        self.assertEqual(data[0, 0], 1)
Ejemplo n.º 3
0
    def test_data_3d(self):
        """
        Test that three-dimensional (level/latitude/longitude) data is
        handled properly, and can be successfully retrieved in the right
        order on a later read.
        """
        reader12 = NetCDFReader(DATASET12)
        data12 = reader12.collect_untimed_data(READ_VAR_NAME)
        reader12.close()

        self.assertEqual(data12.shape, (3, 2, 2))
        # Check a few values to demonstrate that the return value lines up
        # with expected value.
        self.assertEqual(data12[0, 0, 0], 1)
        self.assertEqual(data12[0, 0, 1], 2)
        self.assertEqual(data12[0, 1, 0], 3)
        self.assertEqual(data12[1, 0, 0], 5)
        self.assertEqual(data12[1, 1, 0], 7)
Ejemplo n.º 4
0
    def test_untimed_data_2d(self):
        """
        Test that time independent data is returned correctly with dimensions
        of length greater than 1, and respects the dimensions on which it was
        built by being returned in the correct order.
        """
        reader12 = NetCDFReader(TIME_DATASET12)
        data12 = reader12.collect_untimed_data(READ_VAR_NAME)
        reader12.close()

        self.assertEqual(data12.shape, (3, 2, 2))
        # Check that various points in the data match the values expected
        # at those points, based on the arrays that defined the data.
        self.assertEqual(data12[0, 0, 0], 1)
        self.assertEqual(data12[0, 0, 1], 2)
        self.assertEqual(data12[1, 1, 0], 7)
        self.assertEqual(data12[1, 1, 1], 8)
        self.assertEqual(data12[2, 0, 1], 10)
        self.assertEqual(data12[2, 1, 0], 11)
Ejemplo n.º 5
0
def save_from_dataset(dataset_parent: str,
                      var_name: str,
                      time_seg: Optional[int],
                      config: 'ArrheniusConfig') -> bool:
    """
    Produce a set of image outputs based on a dataset, written by a
    previous run of the Arrhenius model that used config as its
    configuration set. This dataset is stored in the directory given by
    the path dataset_parent.

    The images produced are under the variable var_name in the dataset,
    and only in the time unit given by time_seg. If time_seg is 0, then
    one image will be produced containing averages over the datapoints
    in all time units. If time_seg is None, then an image will be produced
    for every valid time segment.

    Returns True iff a new image was produced by this call, i.e. if it
    did not exist prior to the call.

    :param dataset_parent:
        A path to the directory containing the dataset
    :param var_name:
        The variable from the dataset that will be used to generate the images
    :param time_seg:
        An integer specifying which time unit to use data from
    :param config:
        Configuration options for the previously-run model run
    :return:
        True iff a new image file was created
    """
    run_id = config.run_id()
    # Locate or create a directory to contain image files.

    if time_seg is None:
        # Assume at least one image needs to be produced, and immediately
        # read in data in preparation for that.
        dataset_path = path.join(dataset_parent, run_id + ".nc")
        reader = NetCDFReader(dataset_path)
        data = reader.collect_untimed_data(var_name)
        reader.close()

        # Write all images for variable var_name to the proper destination.
        return write_image_type(data, dataset_parent, var_name, config)
    else:
        # Create an output directory for the new images and get its name.
        parent_path = \
            get_image_directory(dataset_parent, run_id, var_name,
                                config.colorbar(), create=True)

        # Detect if the desired image file already exists.
        base_name = var_name + "_" + str(time_seg)
        file_name = image_file_name(base_name, config) + ".png"
        img_path = path.join(parent_path, file_name)

        created = not Path(img_path).is_file()

        if created:
            # Locate the dataset and read the desired variable from it.
            dataset_path = path.join(dataset_parent, run_id + ".nc")
            reader = NetCDFReader(dataset_path)
            data = reader.collect_untimed_data(var_name)

            # Extract only the requested parts of the data.
            if time_seg == 0:
                selected_time_data = data.mean(axis=0)
            else:
                selected_time_data = data[time_seg - 1]

            # Write the new image file.
            img_writer = ModelImageRenderer(selected_time_data)
            img_writer.save_image(img_path, config.colorbar())
            reader.close()

        return created