Skip to content

Commit e4b550f

Browse files
authored
Improve time handling (#3)
Create test to make sure the attributes are properly created Create the dataset by assembling coordinates and DataArrays Properly handle time dimensions for reading Handle multiple time dimensions when writing Update readme
1 parent 803f15b commit e4b550f

10 files changed

Lines changed: 650 additions & 234 deletions

File tree

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ jobs:
1717
steps:
1818
- name: Install dependencies
1919
run: |
20-
apt update
21-
apt install -y ca-certificates
20+
apt-get update
21+
apt-get install -y ca-certificates
2222
2323
- uses: actions/checkout@v5
2424

README.md

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,27 @@ You need to install GRASS independently.
2121
>>> import xarray as xr
2222
>>> test_ds = xr.open_dataset("/home/lc/grassdata/nc_spm_08_grass7/PERMANENT/", raster=["boundary_county_500m", "elevation"])
2323
>>> test_ds
24-
<xarray.Dataset> Size: 244kB
25-
Dimensions: (y: 150, x: 135)
24+
<xarray.Dataset> Size: 253kB
25+
Dimensions: (y: 150, x: 140)
2626
Coordinates:
2727
* y (y) float32 600B 2.2e+05 2.2e+05 ... 2.207e+05
28-
* x (x) float32 540B 6.383e+05 6.383e+05 ... 6.39e+05
28+
* x (x) float32 560B 6.383e+05 6.383e+05 ... 6.39e+05
2929
Data variables:
30-
boundary_county_500m (y, x) float64 162kB ...
31-
elevation (y, x) float32 81kB ...
30+
boundary_county_500m (y, x) float64 168kB ...
31+
elevation (y, x) float32 84kB ...
3232
Attributes:
33-
crs_wkt: PROJCRS["NAD83(HARN) / North Carolina",BASEGEOGCRS["NAD83(HARN...
33+
crs_wkt: PROJCRS["NAD83(HARN) / North Carolina",BASEGEOGCRS["NAD83(H...
34+
Conventions: CF-1.13-draft
35+
history: 2025-10-31 18:22:16.644873+00:00: Created with xarray-grass...
36+
source: GRASS database. project: <nc_spm_08_grass7>, mapset:<PERMAN...
37+
3438
```
3539

3640
You can choose which maps you want to load with the `raster`, `raster_3d`, `strds` and `str3ds` parameters to `open_dataset`.
3741
Those accept either a single string or an iterable.
3842
If none of those are specified, the whole mapset will be loaded, ignoring single maps that are already registered in either a `strds` or `str3ds`;
3943
those maps will be loaded into the Xarray Dataset for being part of the GRASS Space Time Dataset.
40-
As of version 0.2.0, any time-stamp associated to a single map not registered in a stds is ignored.
44+
Any time-stamp associated to a single map not registered in a stds is ignored.
4145

4246
The extent and resolution of the resulting `Dataset` is defined by the region setting of GRASS, set with the `g.region` GRASS tool.
4347
Note that in GRASS the 3D resolution is independent from the 2D resolution.
@@ -46,8 +50,12 @@ The coordinates in the Xarray `Dataset` correspond to the center of the GRASS ce
4650

4751
If run from outside a GRASS session, `xarray-grass` will automatically create a session in the requested project and mapset.
4852
If run from within GRASS, only maps from accessible mapsets could be loaded.
49-
In GRASS, you can list the accessible mapsets with `g.mapsets`.
53+
You can list the accessible mapsets with `g.mapsets` from GRASS.
54+
55+
In GRASS, the time dimension of various STDSs is not homogeneous, as it is for the spatial coordinates.
56+
To reflect this, xarray-grass will create one time dimension for each STDS loaded.
5057

58+
From within a grass session, it is possible to access various mapsets.
5159

5260
## CF conventions attributes mapping
5361

@@ -64,11 +72,65 @@ The attributes of the coordinates are in line with CF Conventions.
6472

6573
### Dataset attributes
6674

67-
The only attributes set at the dataset level are `crs_wkt` and `Conventions`.
75+
The attributes set at the dataset level are:
76+
- `crs_wkt` from the g.proj command
77+
- `Conventions`, the CF Convention version
78+
- `history`, the time of creation and version of xarray-grass
79+
- `source`, the name of the current grass project and mapset
6880

6981
## Writing an Xarray Dataset or DataArray to GRASS
7082

71-
TODO.
83+
84+
```python
85+
import xarray as xr
86+
from xarray_grass import to_grass
87+
88+
mapset = "/home/lc/grassdata/nc_spm_08_grass7/PERMANENT/" # Or pathlib.Path
89+
90+
test_ds = xr.open_dataset(
91+
mapset,
92+
raster=["boundary_county_500m", "elevation"],
93+
strds="LST_Day_monthly@modis_lst",
94+
)
95+
96+
print(test_ds)
97+
98+
# Let's write the modis time series back into the PERMANENT mapset
99+
100+
da_modis = test_ds["LST_Day_monthly"]
101+
# xarray-grass needs the CRS information to write to GRASS
102+
da_modis.attrs["crs_wkt"] = test_ds.attrs["crs_wkt"]
103+
104+
to_grass(
105+
dataset=da_modis,
106+
mapset=mapset,
107+
dims={
108+
"LST_Day_monthly": {"start_time": "start_time_LST_Day_monthly"},
109+
},
110+
overwrite=False
111+
)
112+
```
113+
114+
The above `print` statement should return this:
115+
116+
```
117+
<xarray.Dataset> Size: 3MB
118+
Dimensions: (y: 165, x: 179, start_time_LST_Day_monthly: 24)
119+
Coordinates:
120+
* y (y) float32 660B 2.196e+05 ... 2.212e+05
121+
* x (x) float32 716B 6.377e+05 ... 6.395e+05
122+
* start_time_LST_Day_monthly (start_time_LST_Day_monthly) datetime64[ns] 192B ...
123+
end_time_LST_Day_monthly (start_time_LST_Day_monthly) datetime64[ns] 192B ...
124+
Data variables:
125+
boundary_county_500m (y, x) float64 236kB ...
126+
elevation (y, x) float32 118kB ...
127+
LST_Day_monthly (start_time_LST_Day_monthly, y, x) int32 3MB ...
128+
Attributes:
129+
crs_wkt: PROJCRS["NAD83(HARN) / North Carolina",BASEGEOGCRS["NAD83(H...
130+
Conventions: CF-1.13-draft
131+
history: 2025-11-01 02:10:24.652838+00:00: Created with xarray-grass...
132+
source: GRASS database. project: <nc_spm_08_grass7_test>, mapset:<P...
133+
```
72134

73135
## Roadmap
74136

@@ -86,7 +148,7 @@ TODO.
86148
- [x] Write to STRDS
87149
- [x] Write to 3D raster
88150
- [x] Write to STR3DS
89-
- [ ] Honour the `dims` argument: transpose if dimensions are not in the expected order
151+
- [ ] Transpose if dimensions are not in the expected order
90152
- [ ] Support time units for relative time
91153
- [ ] Support `end_time`
92154
- [ ] Accept writing into a specific mapset (GRASS 8.5)

0 commit comments

Comments
 (0)