{"action":"created","comment":{"url":"https://api.github.com/repos/pydata/xarray/pulls/comments/74310229","id":74310229,"diff_hunk":"@@ -49,3 +64,131 @@ def set_cdms2_attrs(var, attrs):\n     cdms2_var = cdms2.createVariable(var.values, axes=axes, id=dataarray.name)\n     set_cdms2_attrs(cdms2_var, var.attrs)\n     return cdms2_var\n+\n+\n+def _pick_attrs(attrs, keys):\n+    \"\"\" Return attrs with keys in keys list\n+    \"\"\"\n+    return dict((k, v) for k, v in attrs.items() if k in keys)\n+\n+\n+def _get_iris_args(attrs):\n+    \"\"\" Converts the xarray attrs into args that can be passed into Iris\n+    \"\"\"\n+    # iris.unit is deprecated in Iris v1.9\n+    import cf_units\n+    args = {'attributes': _filter_attrs(attrs, iris_forbidden_keys)}\n+    args.update(_pick_attrs(attrs, ('standard_name', 'long_name',)))\n+    unit_args = _pick_attrs(attrs, ('calendar',))\n+    if 'units' in attrs:\n+        args['units'] = cf_units.Unit(attrs['units'], **unit_args)\n+    return args\n+\n+\n+# TODO: Add converting bounds from xarray to Iris and back\n+def to_iris(dataarray):\n+    \"\"\" Convert a DataArray into a Iris Cube\n+    \"\"\"\n+    # Iris not a hard dependency\n+    import iris\n+    try:\n+        from iris.fileformats.netcdf import parse_cell_methods\n+    except ImportError:\n+        # prior to v1.10\n+        from iris.fileformats._pyke_rules.compiled_krb.fc_rules_cf_fc \\\n+            import _parse_cell_methods as parse_cell_methods\n+\n+    dim_coords = []\n+    aux_coords = []\n+\n+    for coord_name in dataarray.coords:\n+        coord = encode(dataarray.coords[coord_name])\n+        coord_args = _get_iris_args(coord.attrs)\n+        coord_args['var_name'] = coord_name\n+        axis = None\n+        if coord.dims:\n+            axis = dataarray.get_axis_num(coord.dims)\n+        if coord_name in dataarray.dims:\n+            iris_coord = iris.coords.DimCoord(coord.values, **coord_args)\n+            dim_coords.append((iris_coord, axis))\n+        else:\n+            iris_coord = iris.coords.AuxCoord(coord.values, **coord_args)\n+            aux_coords.append((iris_coord, axis))\n+\n+    args = _get_iris_args(dataarray.attrs)\n+    args['var_name'] = dataarray.name\n+    args['dim_coords_and_dims'] = dim_coords\n+    args['aux_coords_and_dims'] = aux_coords\n+    if 'cell_methods' in dataarray.attrs:\n+        args['cell_methods'] = parse_cell_methods(\n+            dataarray.name, dataarray.attrs['cell_methods'])\n+\n+    cube = iris.cube.Cube(dataarray.to_masked_array(), **args)\n+    return cube\n+\n+\n+def _iris_obj_to_attrs(obj):\n+    \"\"\" Return a dictionary of attrs when given a Iris object\n+    \"\"\"\n+    attrs = {'standard_name': obj.standard_name,\n+             'long_name': obj.long_name}\n+    if obj.units.calendar:\n+        attrs['calendar'] = obj.units.calendar\n+    if obj.units.origin != '1':\n+        attrs['units'] = obj.units.origin\n+    attrs.update(obj.attributes)\n+    return dict((k, v) for k, v in attrs.items() if v is not None)\n+\n+\n+def _iris_cell_methods_to_str(cell_methods_obj):\n+    \"\"\" Converts a Iris cell methods into a string\n+    \"\"\"\n+    cell_methods = []\n+    for cell_method in cell_methods_obj:\n+        names = ''.join(['{}: '.format(n) for n in cell_method.coord_names])\n+        intervals = ' '.join(['interval: {}'.format(interval)\n+                              for interval in cell_method.intervals])\n+        comments = ' '.join(['comment: {}'.format(comment)\n+                             for comment in cell_method.comments])\n+        extra = ' '.join([intervals, comments]).strip()\n+        if extra:\n+            extra = ' ({})'.format(extra)\n+        cell_methods.append(names + cell_method.method + extra)\n+    return ' '.join(cell_methods)\n+\n+\n+def from_iris(cube):\n+    \"\"\" Convert a Iris cube into an DataArray\n+    \"\"\"\n+    import iris.exceptions\n+    name = cube.var_name","path":"xarray/convert.py","position":161,"original_position":161,"commit_id":"338ef6b7242c06d5107d2224c1df4cdb2a4b951c","original_commit_id":"e42aeb2adacc0d3be9e93c94f07b2dde6bae3429","user":{"login":"nparley","id":6215361,"avatar_url":"https://avatars.githubusercontent.com/u/6215361?v=3","gravatar_id":"","url":"https://api.github.com/users/nparley","html_url":"https://github.com/nparley","followers_url":"https://api.github.com/users/nparley/followers","following_url":"https://api.github.com/users/nparley/following{/other_user}","gists_url":"https://api.github.com/users/nparley/gists{/gist_id}","starred_url":"https://api.github.com/users/nparley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nparley/subscriptions","organizations_url":"https://api.github.com/users/nparley/orgs","repos_url":"https://api.github.com/users/nparley/repos","events_url":"https://api.github.com/users/nparley/events{/privacy}","received_events_url":"https://api.github.com/users/nparley/received_events","type":"User","site_admin":false},"body":"A quick search in the Iris source gives this:\r\n\r\n```python\r\n    @property\r\n    def var_name(self):\r\n        \"\"\"The CF variable name for the object.\"\"\"\r\n        return self._var_name\r\n\r\n    @var_name.setter\r\n    def var_name(self, name):\r\n        if name is not None:\r\n            if not name:\r\n                raise ValueError('An empty string is not a valid CF variable '\r\n                                 'name.')\r\n            elif set(name).intersection(string.whitespace):\r\n                raise ValueError('{!r} is not a valid CF variable name because'\r\n                                 ' it contains whitespace.'.format(name))\r\n        self._var_name = name\r\n```\r\n\r\nSo would conclude that's it's not an empty string or None","created_at":"2016-08-10T19:09:35Z","updated_at":"2016-08-10T19:09:35Z","html_url":"https://github.com/pydata/xarray/pull/814#discussion_r74310229","pull_request_url":"https://api.github.com/repos/pydata/xarray/pulls/814","_links":{"self":{"href":"https://api.github.com/repos/pydata/xarray/pulls/comments/74310229"},"html":{"href":"https://github.com/pydata/xarray/pull/814#discussion_r74310229"},"pull_request":{"href":"https://api.github.com/repos/pydata/xarray/pulls/814"}}},"pull_request":{"url":"https://api.github.com/repos/pydata/xarray/pulls/814","id":64922505,"html_url":"https://github.com/pydata/xarray/pull/814","diff_url":"https://github.com/pydata/xarray/pull/814.diff","patch_url":"https://github.com/pydata/xarray/pull/814.patch","issue_url":"https://api.github.com/repos/pydata/xarray/issues/814","number":814,"state":"open","locked":false,"title":"xarray to and from iris","user":{"login":"nparley","id":6215361,"avatar_url":"https://avatars.githubusercontent.com/u/6215361?v=3","gravatar_id":"","url":"https://api.github.com/users/nparley","html_url":"https://github.com/nparley","followers_url":"https://api.github.com/users/nparley/followers","following_url":"https://api.github.com/users/nparley/following{/other_user}","gists_url":"https://api.github.com/users/nparley/gists{/gist_id}","starred_url":"https://api.github.com/users/nparley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nparley/subscriptions","organizations_url":"https://api.github.com/users/nparley/orgs","repos_url":"https://api.github.com/users/nparley/repos","events_url":"https://api.github.com/users/nparley/events{/privacy}","received_events_url":"https://api.github.com/users/nparley/received_events","type":"User","site_admin":false},"body":"Added a first go at a converter from xarray.DataArrays objects to Iris.cube.Cube objects. Uses the same template as the cdms2 conversion. ","created_at":"2016-04-01T10:07:44Z","updated_at":"2016-08-10T19:09:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f90f64aff71ba90337d9f918a3d23fe6d83c99df","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/pydata/xarray/pulls/814/commits","review_comments_url":"https://api.github.com/repos/pydata/xarray/pulls/814/comments","review_comment_url":"https://api.github.com/repos/pydata/xarray/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pydata/xarray/issues/814/comments","statuses_url":"https://api.github.com/repos/pydata/xarray/statuses/338ef6b7242c06d5107d2224c1df4cdb2a4b951c","head":{"label":"nparley:master","ref":"master","sha":"338ef6b7242c06d5107d2224c1df4cdb2a4b951c","user":{"login":"nparley","id":6215361,"avatar_url":"https://avatars.githubusercontent.com/u/6215361?v=3","gravatar_id":"","url":"https://api.github.com/users/nparley","html_url":"https://github.com/nparley","followers_url":"https://api.github.com/users/nparley/followers","following_url":"https://api.github.com/users/nparley/following{/other_user}","gists_url":"https://api.github.com/users/nparley/gists{/gist_id}","starred_url":"https://api.github.com/users/nparley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nparley/subscriptions","organizations_url":"https://api.github.com/users/nparley/orgs","repos_url":"https://api.github.com/users/nparley/repos","events_url":"https://api.github.com/users/nparley/events{/privacy}","received_events_url":"https://api.github.com/users/nparley/received_events","type":"User","site_admin":false},"repo":{"id":54669732,"name":"xarray","full_name":"nparley/xarray","owner":{"login":"nparley","id":6215361,"avatar_url":"https://avatars.githubusercontent.com/u/6215361?v=3","gravatar_id":"","url":"https://api.github.com/users/nparley","html_url":"https://github.com/nparley","followers_url":"https://api.github.com/users/nparley/followers","following_url":"https://api.github.com/users/nparley/following{/other_user}","gists_url":"https://api.github.com/users/nparley/gists{/gist_id}","starred_url":"https://api.github.com/users/nparley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nparley/subscriptions","organizations_url":"https://api.github.com/users/nparley/orgs","repos_url":"https://api.github.com/users/nparley/repos","events_url":"https://api.github.com/users/nparley/events{/privacy}","received_events_url":"https://api.github.com/users/nparley/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nparley/xarray","description":"N-D labeled arrays and datasets in Python","fork":true,"url":"https://api.github.com/repos/nparley/xarray","forks_url":"https://api.github.com/repos/nparley/xarray/forks","keys_url":"https://api.github.com/repos/nparley/xarray/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nparley/xarray/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nparley/xarray/teams","hooks_url":"https://api.github.com/repos/nparley/xarray/hooks","issue_events_url":"https://api.github.com/repos/nparley/xarray/issues/events{/number}","events_url":"https://api.github.com/repos/nparley/xarray/events","assignees_url":"https://api.github.com/repos/nparley/xarray/assignees{/user}","branches_url":"https://api.github.com/repos/nparley/xarray/branches{/branch}","tags_url":"https://api.github.com/repos/nparley/xarray/tags","blobs_url":"https://api.github.com/repos/nparley/xarray/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nparley/xarray/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nparley/xarray/git/refs{/sha}","trees_url":"https://api.github.com/repos/nparley/xarray/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nparley/xarray/statuses/{sha}","languages_url":"https://api.github.com/repos/nparley/xarray/languages","stargazers_url":"https://api.github.com/repos/nparley/xarray/stargazers","contributors_url":"https://api.github.com/repos/nparley/xarray/contributors","subscribers_url":"https://api.github.com/repos/nparley/xarray/subscribers","subscription_url":"https://api.github.com/repos/nparley/xarray/subscription","commits_url":"https://api.github.com/repos/nparley/xarray/commits{/sha}","git_commits_url":"https://api.github.com/repos/nparley/xarray/git/commits{/sha}","comments_url":"https://api.github.com/repos/nparley/xarray/comments{/number}","issue_comment_url":"https://api.github.com/repos/nparley/xarray/issues/comments{/number}","contents_url":"https://api.github.com/repos/nparley/xarray/contents/{+path}","compare_url":"https://api.github.com/repos/nparley/xarray/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nparley/xarray/merges","archive_url":"https://api.github.com/repos/nparley/xarray/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nparley/xarray/downloads","issues_url":"https://api.github.com/repos/nparley/xarray/issues{/number}","pulls_url":"https://api.github.com/repos/nparley/xarray/pulls{/number}","milestones_url":"https://api.github.com/repos/nparley/xarray/milestones{/number}","notifications_url":"https://api.github.com/repos/nparley/xarray/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nparley/xarray/labels{/name}","releases_url":"https://api.github.com/repos/nparley/xarray/releases{/id}","deployments_url":"https://api.github.com/repos/nparley/xarray/deployments","created_at":"2016-03-24T19:47:06Z","updated_at":"2016-03-24T19:47:07Z","pushed_at":"2016-08-10T18:56:42Z","git_url":"git://github.com/nparley/xarray.git","ssh_url":"git@github.com:nparley/xarray.git","clone_url":"https://github.com/nparley/xarray.git","svn_url":"https://github.com/nparley/xarray","homepage":"http://xarray.pydata.org","size":6988,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"pydata:master","ref":"master","sha":"e889b88fe4c8863cc0eea093dc4eff1542e2c786","user":{"login":"pydata","id":1284191,"avatar_url":"https://avatars.githubusercontent.com/u/1284191?v=3","gravatar_id":"","url":"https://api.github.com/users/pydata","html_url":"https://github.com/pydata","followers_url":"https://api.github.com/users/pydata/followers","following_url":"https://api.github.com/users/pydata/following{/other_user}","gists_url":"https://api.github.com/users/pydata/gists{/gist_id}","starred_url":"https://api.github.com/users/pydata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pydata/subscriptions","organizations_url":"https://api.github.com/users/pydata/orgs","repos_url":"https://api.github.com/users/pydata/repos","events_url":"https://api.github.com/users/pydata/events{/privacy}","received_events_url":"https://api.github.com/users/pydata/received_events","type":"Organization","site_admin":false},"repo":{"id":13221727,"name":"xarray","full_name":"pydata/xarray","owner":{"login":"pydata","id":1284191,"avatar_url":"https://avatars.githubusercontent.com/u/1284191?v=3","gravatar_id":"","url":"https://api.github.com/users/pydata","html_url":"https://github.com/pydata","followers_url":"https://api.github.com/users/pydata/followers","following_url":"https://api.github.com/users/pydata/following{/other_user}","gists_url":"https://api.github.com/users/pydata/gists{/gist_id}","starred_url":"https://api.github.com/users/pydata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pydata/subscriptions","organizations_url":"https://api.github.com/users/pydata/orgs","repos_url":"https://api.github.com/users/pydata/repos","events_url":"https://api.github.com/users/pydata/events{/privacy}","received_events_url":"https://api.github.com/users/pydata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pydata/xarray","description":"N-D labeled arrays and datasets in Python","fork":false,"url":"https://api.github.com/repos/pydata/xarray","forks_url":"https://api.github.com/repos/pydata/xarray/forks","keys_url":"https://api.github.com/repos/pydata/xarray/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pydata/xarray/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pydata/xarray/teams","hooks_url":"https://api.github.com/repos/pydata/xarray/hooks","issue_events_url":"https://api.github.com/repos/pydata/xarray/issues/events{/number}","events_url":"https://api.github.com/repos/pydata/xarray/events","assignees_url":"https://api.github.com/repos/pydata/xarray/assignees{/user}","branches_url":"https://api.github.com/repos/pydata/xarray/branches{/branch}","tags_url":"https://api.github.com/repos/pydata/xarray/tags","blobs_url":"https://api.github.com/repos/pydata/xarray/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pydata/xarray/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pydata/xarray/git/refs{/sha}","trees_url":"https://api.github.com/repos/pydata/xarray/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pydata/xarray/statuses/{sha}","languages_url":"https://api.github.com/repos/pydata/xarray/languages","stargazers_url":"https://api.github.com/repos/pydata/xarray/stargazers","contributors_url":"https://api.github.com/repos/pydata/xarray/contributors","subscribers_url":"https://api.github.com/repos/pydata/xarray/subscribers","subscription_url":"https://api.github.com/repos/pydata/xarray/subscription","commits_url":"https://api.github.com/repos/pydata/xarray/commits{/sha}","git_commits_url":"https://api.github.com/repos/pydata/xarray/git/commits{/sha}","comments_url":"https://api.github.com/repos/pydata/xarray/comments{/number}","issue_comment_url":"https://api.github.com/repos/pydata/xarray/issues/comments{/number}","contents_url":"https://api.github.com/repos/pydata/xarray/contents/{+path}","compare_url":"https://api.github.com/repos/pydata/xarray/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pydata/xarray/merges","archive_url":"https://api.github.com/repos/pydata/xarray/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pydata/xarray/downloads","issues_url":"https://api.github.com/repos/pydata/xarray/issues{/number}","pulls_url":"https://api.github.com/repos/pydata/xarray/pulls{/number}","milestones_url":"https://api.github.com/repos/pydata/xarray/milestones{/number}","notifications_url":"https://api.github.com/repos/pydata/xarray/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pydata/xarray/labels{/name}","releases_url":"https://api.github.com/repos/pydata/xarray/releases{/id}","deployments_url":"https://api.github.com/repos/pydata/xarray/deployments","created_at":"2013-09-30T17:21:10Z","updated_at":"2016-08-08T16:08:49Z","pushed_at":"2016-08-10T18:56:43Z","git_url":"git://github.com/pydata/xarray.git","ssh_url":"git@github.com:pydata/xarray.git","clone_url":"https://github.com/pydata/xarray.git","svn_url":"https://github.com/pydata/xarray","homepage":"http://xarray.pydata.org","size":7536,"stargazers_count":410,"watchers_count":410,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":118,"mirror_url":null,"open_issues_count":188,"forks":118,"open_issues":188,"watchers":410,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pydata/xarray/pulls/814"},"html":{"href":"https://github.com/pydata/xarray/pull/814"},"issue":{"href":"https://api.github.com/repos/pydata/xarray/issues/814"},"comments":{"href":"https://api.github.com/repos/pydata/xarray/issues/814/comments"},"review_comments":{"href":"https://api.github.com/repos/pydata/xarray/pulls/814/comments"},"review_comment":{"href":"https://api.github.com/repos/pydata/xarray/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pydata/xarray/pulls/814/commits"},"statuses":{"href":"https://api.github.com/repos/pydata/xarray/statuses/338ef6b7242c06d5107d2224c1df4cdb2a4b951c"}}}}
{"actor":{"display_login":"nparley"}}