11"""Tests for metadata_annotator.annotate.py."""
22
3- import os
3+ from os .path import join as path_join
4+ from os .path import split as path_split
45from unittest .mock import patch
56
67import numpy as np
@@ -630,8 +631,37 @@ def test_get_referenced_variables(sample_varinfo_test02):
630631 )
631632
632633
633- def test_update_dimension_variables (sample_netcdf4_file_test04 , sample_varinfo_test04 ):
634+ def test_update_dimension_variables (
635+ varinfo_config_file ,
636+ temp_dir ,
637+ ):
634638 """Ensure the dimension variables are updated correctly."""
639+ file_name = path_join (temp_dir , 'test_input_04.nc' )
640+ sample_datatree = xr .DataTree (xr .Dataset ())
641+ sample_datatree ['/sub_group' ] = xr .Dataset (
642+ attrs = {'short_name' : 'TEST04' },
643+ data_vars = {
644+ 'variable_one' : xr .DataArray (
645+ np .ones ((3 , 3 )),
646+ attrs = {'dimensions' : 'y x' },
647+ dims = ['y_old' , 'x_old' ],
648+ ),
649+ 'x_old' : xr .DataArray (
650+ np .array ([0 , 1 , 2 ]),
651+ dims = ['x_old' ],
652+ ),
653+ 'y_old' : xr .DataArray (
654+ np .array ([0 , 1 , 2 ]),
655+ dims = ['y_old' ],
656+ ),
657+ },
658+ )
659+ sample_datatree .to_netcdf (file_name , encoding = None )
660+
661+ varinfo = VarInfoFromNetCDF4 (
662+ file_name , config_file = varinfo_config_file , short_name = 'TEST04'
663+ )
664+
635665 expected_x_attributes = {
636666 'standard_name' : 'projection_x_coordinate' ,
637667 'long_name' : 'x coordinate of projection' ,
@@ -656,16 +686,14 @@ def test_update_dimension_variables(sample_netcdf4_file_test04, sample_varinfo_t
656686 'metadata_annotator.annotate.get_dimension_index_map' ,
657687 return_value = {'/sub_group/y' : 0 , '/sub_group/x' : 0 },
658688 ):
659- with xr .open_datatree (
660- sample_netcdf4_file_test04 , decode_times = False
661- ) as test_datatree :
689+ with xr .open_datatree (file_name , decode_times = False ) as test_datatree :
662690 variables_to_create = {'/sub_group/x' , '/sub_group/y' }
663691 items_to_update = {'/sub_group/variable_one' }
664692 update_dimension_variables (
665693 test_datatree ,
666694 items_to_update ,
667695 variables_to_create ,
668- sample_varinfo_test04 ,
696+ varinfo ,
669697 )
670698 assert (
671699 test_datatree ['sub_group' ].dataset ['x' ].attrs == expected_x_attributes
@@ -755,20 +783,32 @@ def test_construct_dim_path(ancestor_path, dim_name, expected):
755783@pytest .mark .parametrize (
756784 'variable_path, expected' , [('/variable_one' , True ), ('/variable_two' , False )]
757785)
758- def test_has_dimension_override (sample_varinfo_test06 , variable_path , expected ):
786+ def test_has_dimension_override (varinfo_config_file , temp_dir , variable_path , expected ):
759787 """Ensure variables correctly evaluated to contain non-null dimensions override."""
760- assert has_dimension_override (sample_varinfo_test06 , variable_path ) == expected
788+ file_name = path_join (temp_dir , 'test_input.nc' )
789+ sample_datatree = xr .DataTree (
790+ dataset = xr .Dataset (
791+ attrs = {'short_name' : 'TEST06' },
792+ data_vars = {
793+ 'variable_one' : xr .DataArray ([]),
794+ 'variable_two' : xr .DataArray ([]),
795+ },
796+ ),
797+ )
798+ sample_datatree .to_netcdf (file_name , encoding = None )
799+ varinfo = VarInfoFromNetCDF4 (
800+ file_name , config_file = varinfo_config_file , short_name = 'TEST06'
801+ )
802+ assert has_dimension_override (varinfo , variable_path ) == expected
761803
762804
763805@pytest .mark .parametrize (
764806 'variables_to_create, expected' ,
765807 [
766808 (
767- {'/x_root' , '/ y_root' , '/sub_group/x ' , '/sub_group/y ' },
768- {'/x_root' , '/ y_root' , '/sub_group/x ' , '/sub_group/y' },
809+ {'/y_root' , '/sub_group/y ' , '/missing/dimension/z ' },
810+ {'/y_root' , '/sub_group/y' },
769811 ),
770- ({'/x_root' , '/y_root' }, {'/x_root' , '/y_root' }),
771- ({'/sub_group/x' , '/sub_group/y' }, {'/sub_group/x' , '/sub_group/y' }),
772812 (set (), set ()),
773813 ],
774814)
@@ -803,23 +843,29 @@ def test_ensure_dimension_node_exists(sample_netcdf4_file_test07, variable_path)
803843 creates it successfully.
804844 """
805845 with xr .open_datatree (sample_netcdf4_file_test07 ) as datatree :
806- group_path , variable_name = os . path . split (variable_path )
846+ group_path , variable_name = path_split (variable_path )
807847 assert variable_name not in datatree [group_path ]
808848 ensure_dimension_node_exists (datatree , variable_path )
809849 assert variable_name in datatree [group_path ]
810850
811851
812- def test_copy_shared_dimensions_to_parent (sample_netcdf4_file_test08 ):
852+ def test_copy_shared_dimensions_to_parent (temp_dir ):
813853 """Ensure dimension variables are copied to their configured parent group."""
814- with xr .open_datatree (sample_netcdf4_file_test08 ) as test_datatree :
854+ file_name = path_join (temp_dir , 'test_input.nc' )
855+ sample_datatree = xr .DataTree (dataset = xr .Dataset ())
856+ sample_datatree ['/sub_group' ] = xr .Dataset (
857+ data_vars = {'variable_one' : xr .DataArray (np .ones ((3 , 3 )), dims = ['y' , 'x' ])}
858+ )
859+ sample_datatree .to_netcdf (file_name , encoding = None )
860+ with xr .open_datatree (file_name ) as test_datatree :
815861 variables_to_create = set (['/x' , '/y' ])
816862 # This inital check confirms the dimension variables are not present in the
817863 # configured parent group before being processed.
818864 for variable in variables_to_create :
819- group_path , variable_name = os . path . split (variable )
865+ group_path , variable_name = path_split (variable )
820866 assert variable_name not in test_datatree [group_path ]
821867
822868 copy_shared_dimensions_to_parent (test_datatree , variables_to_create )
823869 for variable in variables_to_create :
824- group_path , variable_name = os . path . split (variable )
870+ group_path , variable_name = path_split (variable )
825871 assert variable_name in test_datatree [group_path ]
0 commit comments