html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue https://github.com/pydata/xarray/issues/567#issuecomment-216932304,https://api.github.com/repos/pydata/xarray/issues/567,216932304,MDEyOklzc3VlQ29tbWVudDIxNjkzMjMwNA==,950575,2016-05-04T17:01:19Z,2016-05-04T17:01:19Z,CONTRIBUTOR,"@shoyer this made into `netcdf4` and some people in my group would like to have this in `xarray` too. If you think it is worth it I can put a PR together for this. ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,105688738 https://github.com/pydata/xarray/issues/567#issuecomment-139239983,https://api.github.com/repos/pydata/xarray/issues/567,139239983,MDEyOklzc3VlQ29tbWVudDEzOTIzOTk4Mw==,950575,2015-09-10T13:50:12Z,2015-09-10T13:50:12Z,CONTRIBUTOR,"How about the function below? (Stolen from [pyaxiom](https://github.com/axiom-data-science/pyaxiom/blob/master/pyaxiom/netcdf/dataset.py#L10-L47).) ``` python def get_variables_by_attributes(ds, strict=False, **kwargs): """""" Returns variables that match specific conditions. * Can pass in key=value parameters and variables are returned that contain all of the matches. ex.: vs = nc.get_variables_by_attributes(axis='X') * Can pass in key=callable parameter and if the callable returns True. The callable should accept a single parameter, the attribute value. None is returned as the attribute valuewhen the attribute does not exist on the variable. ex.: # Get Axis variables vs = nc.get_variables_by_attributes(axis=lambda v: v in ['X', 'Y', 'Z', 'T']) # Get variable that don't have a ""axis"" attribute vs = nc.get_variables_by_attributes(axis=lambda v: v is None) # Get variable that have a ""grid_mapping"" attribute vs = nc.get_variables_by_attributes(axis=lambda v: v is not None) * strict : True/False If True will return only 1 variable if only one is found. Default is False. """""" vs = [] has_value_flag = False for vname, var in ds.iteritems(): for k, v in kwargs.items(): if callable(v): has_value_flag = v(getattr(var, k, None)) if has_value_flag is False: break elif hasattr(var, k) and getattr(var, k) == v: has_value_flag = True else: has_value_flag = False break if has_value_flag is True: vs.append(ds[vname]) if strict: if len(vs) == 1: vs = vs[0] else: msg = ""Expected only one variable. Got {!r}"".format raise ValueError(msg(vs)) return vs ``` See it in action [here](http://nbviewer.ipython.org/gist/ocefpaf/1633cb2f61b61db35ad0). ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,105688738