Skip to content

Commit ce16236

Browse files
authored
Add xproj (#12)
1 parent 16ee717 commit ce16236

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/earth/xproj.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
1+
---
2+
jupytext:
3+
text_representation:
4+
format_name: myst
5+
kernelspec:
6+
display_name: Python 3
7+
name: python
8+
---
9+
110
# xproj: CRSIndex
11+
12+
```{seealso}
13+
Learn more at the [xproj](https://xproj.readthedocs.io) documentation
14+
```
15+
16+
## Highlights
17+
18+
1. `xproj` provides a `CRSIndex` to handle "coordinate reference system" (CRS) information.
19+
1. CRS information is commonly stored in the attributes of a _scalar_ coordinate variable, usually called `"spatial_ref"` or one that is annotated as a "grid mapping variable" according to the CF conventions.
20+
1. The purpose of `CRSIndex` is to handle _alignment_, not indexing. That is, we would like errors to be raised when adding or concatenating (for example) two datasets with different CRS. Such operations are nonsensical without reprojecting the data to a common CRS.
21+
22+
## Example
23+
24+
### Assigning
25+
26+
```{code-cell}
27+
%xmode minimal
28+
29+
import xarray as xr
30+
import xproj # registers the .proj accessor
31+
32+
xr.set_options(display_expand_data=False, display_expand_attrs=False, display_expand_indexes=True)
33+
34+
ds = xr.tutorial.load_dataset("air_temperature")
35+
ds
36+
```
37+
38+
This dataset has no CRS information.
39+
`xproj` uses Xarray's accessor interface to provide functionality. We use that to assign a CRS --- the common WGS84 global geodetic CRS ([EPSG code: 4326](https://epsg.io/4326)).
40+
41+
```{code-cell}
42+
ds_wgs84 = ds.proj.assign_crs(spatial_ref="epsg:4326")
43+
ds_wgs84
44+
```
45+
46+
Note the CRS information in the repr for CRSIndex associated with `spatial_ref` above.
47+
48+
### Alignment
49+
50+
_Alignment_ means making sure the objects conform to the same coordinate reference frame.
51+
When executing multi-object operations (e.g. binary operations, concatenation, merging etc.), Xarray automatically aligns these objects using Indexes.
52+
53+
To illustrate we will artificially create a new dataset with a different CRS:
54+
55+
```{code-cell}
56+
ds_wgs72 = ds_wgs84.proj.assign_crs(spatial_ref="epsg:4322", allow_override=True)
57+
ds_wgs72
58+
```
59+
60+
Now lets add the two:
61+
62+
```{code-cell}
63+
---
64+
tags: [raises-exception]
65+
---
66+
ds_wgs84 + ds_wgs72
67+
```
68+
69+
How about concatenating?
70+
71+
```{code-cell}
72+
---
73+
tags: [raises-exception]
74+
---
75+
xr.concat([ds_wgs84, ds_wgs72], dim="newdim")
76+
```

0 commit comments

Comments
 (0)