def test_finalize_only_and_no_output(self): self.add_path('my.zarr') writer = DatasetWriter('my.zarr', finalize_only=True, output_append=True, output_metadata=dict(comment='This dataset is a test.')) with self.assertRaises(FileNotFoundError) as e: writer.finalize_dataset() self.assertEqual(('output path not found: my.zarr',), e.exception.args)
def test_finalize_updates_metadata(self): self.add_path('my.zarr') writer = DatasetWriter('my.zarr', output_append=True, output_metadata=dict(comment='This dataset is a test.')) for i in range(3): ds = new_test_dataset(day=i + 1) writer.write_dataset(ds) with xr.open_zarr('my.zarr', consolidated=False) as ds: self.assertNotIn('comment', ds.attrs) writer.finalize_dataset() with xr.open_zarr('my.zarr', consolidated=False) as ds: self.assertIn('comment', ds.attrs) self.assertEqual('This dataset is a test.', ds.attrs['comment'])
def test_finalize_adjusts_metadata_with_time_bnds(self): self.add_path('my.zarr') writer = DatasetWriter('my.zarr', output_append=True, output_adjust_metadata=True) for i in range(3): ds = new_test_dataset(day=i + 1, add_time_bnds=True) writer.write_dataset(ds) writer.finalize_dataset() with xr.open_zarr('my.zarr', consolidated=False) as ds: self.assertIn('time_coverage_start', ds.attrs) self.assertEqual('2020-12-01 09:30:00', ds.attrs['time_coverage_start']) self.assertIn('time_coverage_end', ds.attrs) self.assertEqual('2020-12-03 10:30:00', ds.attrs['time_coverage_end'])
def test_finalize_only_and_consolidate_if_specified(self): self.add_path('my.zarr') ds = new_test_dataset(day=1) writer = DatasetWriter('my.zarr', output_overwrite=True) writer.write_dataset(ds) writer.finalize_dataset() self.assertTrue(os.path.isdir('my.zarr')) self.assertFalse(os.path.isfile('my.zarr/.zmetadata')) writer = DatasetWriter('my.zarr', output_consolidated=True, finalize_only=True) writer.finalize_dataset() self.assertTrue(os.path.isdir('my.zarr')) self.assertTrue(os.path.isfile('my.zarr/.zmetadata')) with open('my.zarr/.zmetadata') as fp: metadata = json.load(fp) self.assertIn('metadata', metadata) self.assertEqual({}, metadata['metadata'].get('.zattrs'))
def test_finalize_adjusts_metadata(self): self.add_path('my.zarr') writer = DatasetWriter('my.zarr', output_append=True, output_adjust_metadata=True, input_paths=['a.nc', 'z.zarr', 'b.nc']) for i in range(3): ds = new_test_dataset(day=i + 1) writer.write_dataset(ds) with xr.open_zarr('my.zarr', consolidated=False) as ds: self.assertNotIn('history', ds.attrs) self.assertNotIn('source', ds.attrs) self.assertNotIn('time_coverage_start', ds.attrs) self.assertNotIn('time_coverage_end', ds.attrs) writer.finalize_dataset() with xr.open_zarr('my.zarr', consolidated=False) as ds: self.assertIn('history', ds.attrs) self.assertIn('source', ds.attrs) self.assertEqual('a.nc, b.nc', ds.attrs['source']) self.assertIn('time_coverage_start', ds.attrs) self.assertEqual('2020-12-01 10:00:00', ds.attrs['time_coverage_start']) self.assertIn('time_coverage_end', ds.attrs) self.assertEqual('2020-12-03 10:00:00', ds.attrs['time_coverage_end'])