Skip to content

Commit 71a9c57

Browse files
author
Andy Barrett
committed
Merge branch 'main' into due-cryo-119-netcdf-coords
2 parents 3484ff3 + 284a9bc commit 71a9c57

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

nsidc-data-cookbook/how-to-guides/get_latitude_and_longitude.qmd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "How to get latitude and longitude for the grid cells of a NetCDF file"
33
author: Andrew P. Barrett
4-
date: last_modified
4+
date: last-modified
55
---
66

77
## Problem
@@ -13,7 +13,7 @@ import numpy as np
1313
1414
import rioxarray
1515
import xarray as xr
16-
from pyproj import CRS, Transform
16+
from pyproj import CRS, Transformer
1717
1818
1919
# Load dataset using decode_coords='all'
@@ -32,11 +32,11 @@ transform = Transformer.from_crs(source_crs, destination_crs)
3232
x2d, y2d = np.meshgrid(ds.x, ds.y)
3333
3434
# Calculate latitude and longitudes for each grid cell
35-
lat, lon = transformer.transform(x2d, y2d)
35+
lat, lon = transform.transform(x2d, y2d)
3636
```
3737

3838
## Discussion
39-
The workflow above assumes that the NetCDF files is CF-compliant and has a `grid_mapping` variable and projected coordinates defined. See the `rioxarray` [CRS management documentation](https://corteva.github.io/rioxarray/stable/getting_started/crs_management.html#) if this is not the case.
39+
The workflow above assumes that the NetCDF file is CF-compliant and has a `grid_mapping` variable and projected coordinates defined. See the `rioxarray` [CRS management documentation](https://corteva.github.io/rioxarray/stable/getting_started/crs_management.html#) if this is not the case.
4040

4141
If the dataset `rio.crs` is not set, the `source_crs` could be defined using the EPSG code or other projection information directly, without having to set the dataset crs.
4242

nsidc-data-cookbook/how-to-guides/netcdf_cf.qmd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: "How do I get the bounding box of a NetCDF file in latitude and longitude?"
33
author: Andrew P. Barrett
4-
date: last_modified
4+
date: last-modified
55
---
66

77
## Problem
8-
My data are in a NetCDF file but there are no latitude and longitude coordinates. How do I find the bounding box in latitude and longitude?.
8+
My data are in a NetCDF file but there are no latitude and longitude coordinates. How do I find the bounding box in latitude and longitude?
99

1010
## Solution
1111
### Using xarray and rioxarray
@@ -29,7 +29,7 @@ ds.rio.transform_bounds(4326)
2929

3030
As of [CF v1.8](https://cfconventions.org/Data/cf-conventions/cf-conventions-1.10/cf-conventions.html#grid-mappings-and-projections), latitude and longitude coordinate variables are no longer required in CF-compliant NetCDF files, as long as projected horizontal spatial coordinates (e.g. `x` and `y`, or `easting` and `northing`) and a `grid_mapping` variable is provided. The `grid_mapping` variable defines the coordinate reference system (CRS) of the projected horizontal coordinates.
3131

32-
The easiest, and frankly best, way to read and work with NetCDF files is to use `xarray`. `rioxarray` extends the `xarray` package to use CRS and make geospatial tasks, such as reprojecting and regridding, easier. By setting the keyword `decode-coords='all'`, `rioxarray` searches the `xarray.DataArray` or `xarray.Dataset` for the CRS. `rioxarray` also determines the `transform` or `geotransform`, which defines the image CRS that transforms cell coordiates (column and row) into projected coordinates (x, y). The `transform` is calculated from the coordinates of the data. See the `rioxarray` [documentaton](https://corteva.github.io/rioxarray/stable/getting_started/crs_management.html#) for more details.
32+
The easiest, and frankly best, way to read and work with NetCDF files is to use `xarray`. `rioxarray` extends the `xarray` package to use CRS and make geospatial tasks, such as reprojecting and regridding, easier. By setting the keyword `decode-coords='all'`, `rioxarray` searches the `xarray.DataArray` or `xarray.Dataset` for the CRS. `rioxarray` also determines the `transform` or `geotransform`, which defines the image CRS that transforms cell coordinates (column and row) into projected coordinates (x, y). The `transform` is calculated from the coordinates of the data. See the `rioxarray` [documentaton](https://corteva.github.io/rioxarray/stable/getting_started/crs_management.html#) for more details.
3333

3434
Both these spatial coordinates can be accessed as follows:
3535

@@ -50,7 +50,7 @@ Affine(25000.0, 0.0, -3850000.0,
5050

5151
If this information is not found, all is not lost. [`rio.write_crs`](https://corteva.github.io/rioxarray/stable/getting_started/crs_management.html#Setting-the-CRS) and [`rio.write_transform`](https://corteva.github.io/rioxarray/stable/getting_started/crs_management.html#Setting-the-transform-of-the-dataset) can be used to set the CRS and the transform of the dataset.
5252

53-
Once these variables are set, the horizontal spatial bounds of the dataset can be queried as described above. If all you want is the bounds in projected coordinates. These can be accessed using `ds.rio.bounds()`.
53+
Once these variables are set, the horizontal spatial bounds of the dataset can be queried as described above. If all you want is the bounds in projected coordinates, these can be accessed using `ds.rio.bounds()`.
5454

5555
```
5656
ds.rio.bounds()

0 commit comments

Comments
 (0)