@@ -823,31 +823,30 @@ def describe(self):
823
823
Print a string repr to screen.
824
824
"""
825
825
text = "Axes:\n "
826
+ axes = self .axes
826
827
for key in _AXIS_NAMES :
827
- axes = apply_mapper (_get_axis_coord , self ._obj , key , error = False )
828
- text += f"\t { key } : { axes } \n "
828
+ text += f"\t { key } : { axes [key ] if key in axes else []} \n "
829
829
830
830
text += "\n Coordinates:\n "
831
+ coords = self .coordinates
831
832
for key in _COORD_NAMES :
832
- coords = apply_mapper (_get_axis_coord , self ._obj , key , error = False )
833
- text += f"\t { key } : { coords } \n "
833
+ text += f"\t { key } : { coords [key ] if key in coords else []} \n "
834
834
835
835
text += "\n Cell Measures:\n "
836
- for measure in _CELL_MEASURES :
837
- if isinstance ( self . _obj , Dataset ):
838
- text += f" \t { measure } : unsupported \n "
839
- else :
840
- measures = apply_mapper ( _get_measure , self . _obj , measure , error = False )
841
- text += f"\t { measure } : { measures } \n "
836
+ if isinstance ( self . _obj , Dataset ) :
837
+ measures = { key : "unsupported" for key in _CELL_MEASURES }
838
+ else :
839
+ measures = self . cell_measures
840
+ for key in _CELL_MEASURES :
841
+ text += f"\t { key } : { measures [ key ] if key in measures else [] } \n "
842
842
843
843
text += "\n Standard Names:\n "
844
844
if isinstance (self ._obj , DataArray ):
845
845
text += "\t unsupported\n "
846
846
else :
847
- stdnames = sorted (self .get_standard_names ())
848
- for name in stdnames :
849
- if name not in _COORD_NAMES :
850
- text += f"\t { name } : { _get_with_standard_name (self ._obj , name )} \n "
847
+ for key , value in sorted (self .standard_names .items ()):
848
+ if key not in _COORD_NAMES :
849
+ text += f"\t { key } : { value } \n "
851
850
852
851
print (text )
853
852
@@ -871,101 +870,92 @@ def keys(self) -> Set[str]:
871
870
-------
872
871
Set of valid key names that can be used with __getitem__ or .cf[key].
873
872
"""
874
- varnames = [
875
- key
876
- for key in _AXIS_NAMES + _COORD_NAMES
877
- if apply_mapper (_get_axis_coord , self ._obj , key , error = False )
878
- ]
873
+
874
+ varnames = list (self .axes ) + list (self .coordinates )
879
875
if not isinstance (self ._obj , Dataset ):
880
- measures = [
881
- key
882
- for key in _CELL_MEASURES
883
- if apply_mapper (_get_measure , self ._obj , key , error = False )
884
- ]
885
- if measures :
886
- varnames .extend (measures )
887
-
888
- varnames .extend (self .get_standard_names ())
876
+ varnames .extend (list (self .cell_measures ))
877
+ varnames .extend (list (self .standard_names ))
878
+
889
879
return set (varnames )
890
880
891
881
@property
892
- def axes (self ) -> Set [str ]:
882
+ def axes (self ) -> Dict [str , List [ str ] ]:
893
883
"""
894
- Property that returns valid Axis names for ``.cf[]``.
884
+ Property that returns a dictionary mapping valid Axis standard names for ``.cf[]``
885
+ to variable names.
895
886
896
887
This is useful for checking whether a key is valid for indexing, i.e.
897
888
that the attributes necessary to allow indexing by that key exist.
898
889
However, it will only return the Axis names, not Coordinate names.
899
890
900
891
Returns
901
892
-------
902
- Set of valid Axis names that can be used with ``__getitem__`` or ``.cf[key]``.
893
+ Dictionary of valid Axis names that can be used with ``__getitem__`` or ``.cf[key]``.
903
894
Will be ("X", "Y", "Z", "T") or a subset thereof.
904
895
"""
905
- varnames = [
906
- key
896
+ vardict = {
897
+ key : apply_mapper ( _get_axis_coord , self . _obj , key , error = False )
907
898
for key in _AXIS_NAMES
908
- if apply_mapper (_get_axis_coord , self ._obj , key , error = False )
909
- ]
899
+ }
910
900
911
- return set ( varnames )
901
+ return { k : sorted ( v ) for k , v in vardict . items () if v }
912
902
913
903
@property
914
- def coordinates (self ) -> Set [str ]:
904
+ def coordinates (self ) -> Dict [str , List [ str ] ]:
915
905
"""
916
- Property that returns valid Coordinate names for .cf[].
906
+ Property that returns a dictionary mapping valid Coordinate standard names for ``.cf[]``
907
+ to variable names.
917
908
918
909
This is useful for checking whether a key is valid for indexing, i.e.
919
910
that the attributes necessary to allow indexing by that key exist.
920
911
However, it will only return the Coordinate names, not Axis names.
921
912
922
913
Returns
923
914
-------
924
- Set of valid Coordinate names that can be used with ``__getitem__`` or ``.cf[key]``.
915
+ Dictionary of valid Coordinate names that can be used with ``__getitem__`` or ``.cf[key]``.
925
916
Will be ("longitude", "latitude", "vertical", "time") or a subset thereof.
926
917
"""
927
- varnames = [
928
- key
918
+ vardict = {
919
+ key : apply_mapper ( _get_axis_coord , self . _obj , key , error = False )
929
920
for key in _COORD_NAMES
930
- if apply_mapper (_get_axis_coord , self ._obj , key , error = False )
931
- ]
921
+ }
932
922
933
- return set ( varnames )
923
+ return { k : sorted ( v ) for k , v in vardict . items () if v }
934
924
935
925
@property
936
- def cell_measures (self ) -> Set [str ]:
926
+ def cell_measures (self ) -> Dict [str , List [ str ] ]:
937
927
"""
938
- Property that returns valid cell measure names for .cf[].
928
+ Property that returns a dictionary mapping valid cell measure standard names for ``.cf[]``
929
+ to variable names.
939
930
940
931
This is useful for checking whether a key is valid for indexing, i.e.
941
932
that the attributes necessary to allow indexing by that key exist.
942
933
However, it will only return the cell measure names.
943
934
944
935
Returns
945
936
-------
946
- Set of valid cell measure names that can be used with __getitem__ or .cf[key].
937
+ Dictionary of valid cell measure names that can be used with __getitem__ or .cf[key].
947
938
"""
948
- assert isinstance (self ._obj , DataArray ), "this works with DataArrays"
939
+ assert isinstance (self ._obj , DataArray ), "this only works with DataArrays"
949
940
950
- measures = [
951
- key
941
+ measures = {
942
+ key : apply_mapper ( _get_measure , self . _obj , key , error = False )
952
943
for key in _CELL_MEASURES
953
- if apply_mapper (_get_measure , self ._obj , key , error = False )
954
- ]
944
+ }
955
945
956
- return set ( measures )
946
+ return { k : sorted ( v ) for k , v in measures . items () if v }
957
947
958
948
def get_standard_names (self ) -> List [str ]:
959
949
960
950
warnings .warn (
961
- "Now called `standard_names` and ` get_standard_names` will be removed in a future version." ,
951
+ "` get_standard_names` will be removed in a future version in favor of `standard_names` ." ,
962
952
DeprecationWarning ,
963
953
)
964
954
965
- return self .standard_names
955
+ return list ( self .standard_names . keys ())
966
956
967
957
@property
968
- def standard_names (self ) -> List [str ]:
958
+ def standard_names (self ) -> Dict [ str , List [str ] ]:
969
959
"""
970
960
Returns a sorted list of standard names in Dataset.
971
961
@@ -977,21 +967,20 @@ def standard_names(self) -> List[str]:
977
967
978
968
Returns
979
969
-------
980
- list of standard names in dataset
970
+ Dictionary of standard names in dataset
981
971
"""
982
972
if isinstance (self ._obj , Dataset ):
983
973
variables = self ._obj .variables
984
974
elif isinstance (self ._obj , DataArray ):
985
975
variables = self ._obj .coords
986
- return sorted (
987
- set (
988
- [
989
- v .attrs ["standard_name" ]
990
- for k , v in variables .items ()
991
- if "standard_name" in v .attrs
992
- ]
993
- )
994
- )
976
+
977
+ vardict : Dict [str , List [str ]] = dict ()
978
+ for k , v in variables .items ():
979
+ if "standard_name" in v .attrs :
980
+ std_name = v .attrs ["standard_name" ]
981
+ vardict [std_name ] = vardict .setdefault (std_name , []) + [k ]
982
+
983
+ return {k : sorted (v ) for k , v in vardict .items ()}
995
984
996
985
def get_associated_variable_names (self , name : Hashable ) -> Dict [str , List [str ]]:
997
986
"""
0 commit comments