Exemple #1
0
    def _write_variable_collection_main_(cls, vc, opened_or_path, write_mode,
                                         **kwargs):
        assert write_mode is not None

        dataset_kwargs = kwargs.get('dataset_kwargs', {})
        variable_kwargs = kwargs.get('variable_kwargs', {})

        # When filling a dataset, we use append mode.
        if write_mode == MPIWriteMode.FILL:
            mode = 'a'
        else:
            mode = 'w'

        # For an asynchronous write, treat everything like a single rank.
        if write_mode == MPIWriteMode.ASYNCHRONOUS:
            possible_ranks = [0]
        else:
            possible_ranks = vm.ranks

        # Write the data on each rank.
        for idx, rank_to_write in enumerate(possible_ranks):
            # The template write only occurs on the first rank.
            if write_mode == MPIWriteMode.TEMPLATE and rank_to_write != 0:
                pass
            # If this is not a template write, fill the data.
            elif write_mode == MPIWriteMode.ASYNCHRONOUS or vm.rank == rank_to_write:
                with driver_scope(cls,
                                  opened_or_path=opened_or_path,
                                  mode=mode,
                                  **dataset_kwargs) as dataset:
                    # Write global attributes if we are not filling data.
                    if write_mode != MPIWriteMode.FILL:
                        vc.write_attributes_to_netcdf_object(dataset)
                    # This is the main variable write loop.
                    variables_to_write = get_variables_to_write(vc)
                    for variable in variables_to_write:
                        # Load the variable's data before orphaning. The variable needs its parent to know which
                        # group it is in.
                        variable.load()
                        # Call the individual variable write method in fill mode. Orphaning is required as a
                        # variable will attempt to write its parent first.
                        with orphaned(variable, keep_dimensions=True):
                            variable.write(dataset,
                                           write_mode=write_mode,
                                           **variable_kwargs)
                    # Recurse the children.
                    for child in list(vc.children.values()):
                        if write_mode != MPIWriteMode.FILL:
                            group = nc.Group(dataset, child.name)
                        else:
                            group = dataset.groups[child.name]
                        child.write(group, write_mode=write_mode, **kwargs)
                    dataset.sync()
            vm.barrier()
Exemple #2
0
 def setUp(self):
     self.file1 = FILE_NAME1
     f = netCDF4.Dataset(self.file1, 'w')
     g1 = f.createGroup(HENRY_VII)
     g2 = g1.createGroup(MARGARET)
     g3 = g2.createGroup(JAMES_V_OF_SCOTLAND)
     g4 = g3.createGroup(MARY_I_OF_SCOTLAND)
     g5 = g4.createGroup(JAMES_VI_OF_SCOTLAND_AND_I_OF_ENGLAND)
     f.close()
     self.file2 = FILE_NAME2
     f = netCDF4.Dataset(self.file2, 'w')
     g1 = netCDF4.Group(f, DYNASTY)
     g2 = g1.createGroup(HENRY_VII)
     g3 = g1.createGroup(MARGARET)
     g4 = g1.createGroup(JAMES_V_OF_SCOTLAND)
     g5 = g1.createGroup(MARY_I_OF_SCOTLAND)
     g6 = g1.createGroup(JAMES_VI_OF_SCOTLAND_AND_I_OF_ENGLAND)
     f.close()