@@ -905,6 +905,23 @@ def get_variable_by_axis(self, var, axis, coords=None):
905905 See Also
906906 --------
907907 get_x, get_y, get_z, get_t"""
908+
909+ def get_coord (cname , raise_error = True ):
910+ try :
911+ return coords [cname ]
912+ except KeyError :
913+ if cname not in self .ds .coords :
914+ if raise_error :
915+ raise
916+ return None
917+ ret = self .ds .coords [cname ]
918+ try :
919+ idims = var .psy .idims
920+ except AttributeError : # got xarray.Variable
921+ idims = {}
922+ return ret .isel (** {d : sl for d , sl in idims .items ()
923+ if d in ret .dims })
924+
908925 axis = axis .lower ()
909926 if axis not in list ('xyzt' ):
910927 raise ValueError ("Axis must be one of X, Y, Z, T, not {0}" .format (
@@ -949,23 +966,23 @@ def get_variable_by_axis(self, var, axis, coords=None):
949966 if axis == 'x' :
950967 for cname in filter (lambda cname : re .search ('lon' , cname ),
951968 coord_names ):
952- return coords [ cname ]
953- return coords . get (coord_names [- 1 ])
969+ return get_coord ( cname )
970+ return get_coord (coord_names [- 1 ], raise_error = False )
954971 elif axis == 'y' and len (coord_names ) >= 2 :
955972 for cname in filter (lambda cname : re .search ('lat' , cname ),
956973 coord_names ):
957- return coords [ cname ]
958- return coords . get (coord_names [- 2 ])
974+ return get_coord ( cname )
975+ return get_coord (coord_names [- 2 ], raise_error = False )
959976 elif (axis == 'z' and len (coord_names ) >= 3 and
960977 coord_names [- 3 ] not in tnames ):
961- return coords . get (coord_names [- 3 ])
978+ return get_coord (coord_names [- 3 ], raise_error = False )
962979 elif axis == 't' and tnames :
963980 tname = next (iter (tnames ))
964981 if len (tnames ) > 1 :
965982 warn ("Found multiple matches for time coordinate in the "
966983 "coordinates: %s. I use %s" % (', ' .join (tnames ), tname ),
967984 PsyPlotRuntimeWarning )
968- return coords . get (tname )
985+ return get_coord (tname , raise_error = False )
969986
970987 @docstrings .get_sectionsf ("CFDecoder.get_x" , sections = [
971988 'Parameters' , 'Returns' ])
0 commit comments