Skip to content

Commit 5e68288

Browse files
committed
Add a scan example script
1 parent 714f511 commit 5e68288

7 files changed

Lines changed: 499 additions & 106 deletions

File tree

README.md

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,73 +13,57 @@ For demonstration purposes, this repository contains three Python packages, but
1313

1414
## Example usage
1515

16-
There is an example script, [`demo.py`](demo.py), that demonstrates how to use the `fix` function to fix a dataset. The script can be run with the following command:
16+
There is an example script, [`demo_fix.py`](demo_fix.py), that demonstrates how to use the `fix` function to fix a dataset. The script can be run with the following command:
1717

1818
```bash
19-
uv run --extra dev demo.py
19+
uv run --extra dev demo_fix.py
2020
```
2121

22-
Below is an example using Ipython:
22+
Additionally, there is a script, [`demo_scan.py`](demo_scan.py), that demonstrates how to scan a dataset for problems and generate a fixer configuration. The script can be run with the following command:
23+
24+
```bash
25+
uv run --extra dev demo_scan.py
26+
```
27+
28+
Below is an example of using the `fix` function in Ipython:
2329

2430
```python
25-
In [1]: import xarray as xr
31+
In [1]: from fixer import fix
2632

27-
In [2]: from fixer import fix
33+
In [2]: from fixer_cmip7.tests.test_fixes import create_test_dataset
2834

29-
In [3]: ds = xr.open_dataset(
30-
...: "~/climate_data/CMIP6/CMIP/BCC/BCC-ESM1/historical/r1i1p1f1/Amon/tas/gn/v20181214/tas_Amon_BCC-ESM1_historical_r1i1p1f1_gn_185001-201412.nc",
31-
...: chunks={"time": 100},
32-
...: )
33-
...:
35+
In [3]: ds = create_test_dataset()
3436

3537
In [4]: ds
3638
Out[4]:
37-
<xarray.Dataset> Size: 65MB
38-
Dimensions: (time: 1980, bnds: 2, lat: 64, lon: 128)
39+
<xarray.Dataset> Size: 200B
40+
Dimensions: (time: 1, y: 2, x: 3, bounds: 2)
3941
Coordinates:
40-
* time (time) object 16kB 1850-01-16 12:00:00 ... 2014-12-16 12:00:00
41-
* lat (lat) float64 512B -87.86 -85.1 -82.31 ... 82.31 85.1 87.86
42-
* lon (lon) float64 1kB 0.0 2.812 5.625 8.438 ... 351.6 354.4 357.2
43-
height float64 8B ...
44-
Dimensions without coordinates: bnds
42+
* time (time) object 8B 1850-01-16 12:00:00
43+
lat (y) float64 16B -85.1 -87.86
44+
lon (x) float64 24B 0.0 2.812 5.625
45+
height2m float64 8B 2.0
46+
Dimensions without coordinates: y, x, bounds
4547
Data variables:
46-
time_bnds (time, bnds) object 32kB dask.array<chunksize=(100, 2), meta=np.ndarray>
47-
lat_bnds (lat, bnds) float64 1kB dask.array<chunksize=(64, 2), meta=np.ndarray>
48-
lon_bnds (lon, bnds) float64 2kB dask.array<chunksize=(128, 2), meta=np.ndarray>
49-
tas (time, lat, lon) float32 65MB dask.array<chunksize=(100, 64, 128), meta=np.ndarray>
50-
Attributes: (12/49)
51-
Conventions: CF-1.7 CMIP-6.2
52-
activity_id: CMIP
53-
branch_method: Standard
54-
branch_time_in_child: 0.0
55-
branch_time_in_parent: 2110.0
56-
comment: The model integration starts from the piControl e...
57-
... ...
58-
title: BCC-ESM1 output prepared for CMIP6
59-
tracking_id: hdl:21.14100/e6e5eb74-9e45-44db-9dbe-d6236596b068
60-
variable_id: tas
61-
variant_label: r1i1p1f1
62-
license: CMIP6 model data produced by BCC is licensed unde...
63-
cmor_version: 3.3.2
64-
65-
In [5]: result = fix(
66-
...: ds,
67-
...: name="MIP-DRS7.CMIP7.CMIP.CCCma.CanESM6-MR.historical.r2i1p1f1.glb.mon.tas.tavg-h2m-hxy-u.g13s.v20250622",
68-
...: )
69-
...:
48+
time_bounds (time, bounds) object 16B 1850-01-01 00:00:00 1850-02-01 00:...
49+
lat_bounds (y, bounds) float64 32B -83.7 -86.48 -86.48 -90.0
50+
lon_bounds (x, bounds) float64 48B -1.406 1.406 1.406 4.219 4.219 7.031
51+
temp (time, y, x) float64 48B 24.47 24.25 24.01 24.82 24.21 24.61
7052

71-
In [6]: result
72-
Out[6]:
73-
<xarray.Dataset> Size: 65MB
74-
Dimensions: (time: 1980, lat: 64, lon: 128, bnds: 2)
53+
In [5]: fix(ds, name="MIP-DRS7.CMIP7.CMIP.CCCma.CanESM6-MR.historical.r2i1p1f1.glb.mon.tas.tavg-h2m-hxy-u.g13s.v20250622")
54+
Out[5]:
55+
<xarray.Dataset> Size: 176B
56+
Dimensions: (time: 1, lat: 2, lon: 3, bnds: 2)
7557
Coordinates:
76-
* time (time) object 16kB 1850-01-16 12:00:00 ... 2014-12-16 12:00:00
77-
* lat (lat) float64 512B -87.86 -85.1 -82.31 ... 82.31 85.1 87.86
78-
* lon (lon) float64 1kB 0.0 2.812 5.625 8.438 ... 351.6 354.4 357.2
58+
* time (time) object 8B 1850-01-16 12:00:00
59+
* lat (lat) float64 16B -85.1 -87.86
60+
* lon (lon) float64 24B 0.0 2.812 5.625
7961
height float64 8B 2.0
8062
Dimensions without coordinates: bnds
8163
Data variables:
82-
tas (time, lat, lon) float32 65MB dask.array<chunksize=(100, 64, 128), meta=np.ndarray>
83-
time_bnds (time, bnds) object 32kB dask.array<chunksize=(100, 2), meta=np.ndarray>
84-
lat_bnds (lat, bnds) float64 1kB dask.array<chunksize=(64, 2), meta=np.ndarray>
85-
lon_bnds (lon, bnds) float64 2kB dask.array<chunksize=(128, 2), meta=np.ndarray>
64+
tas (time, lat, lon) float32 24B 297.6 297.4 297.2 298.0 297.4 297.8
65+
time_bnds (time, bnds) object 16B 1850-01-01 00:00:00 1850-02-01 00:00:00
66+
lat_bnds (lat, bnds) float64 32B -83.7 -86.48 -86.48 -90.0
67+
lon_bnds (lon, bnds) float64 48B -1.406 1.406 1.406 4.219 4.219 7.031
68+
69+
```

demo.py renamed to demo_fix.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
"""Test some stuff."""
1+
"""Demonstrate how to apply fixes to a dataset."""
22

33
# ruff: noqa: T201
4-
import xarray as xr
4+
5+
from __future__ import annotations
6+
57
from fixer import fix
8+
from fixer_cmip7.tests.test_fixes import create_test_dataset
69

710

811
def main() -> None:
9-
"""CMORize a file as an example."""
10-
ds = xr.open_dataset(
11-
"~/climate_data/CMIP6/CMIP/BCC/BCC-ESM1/historical/r1i1p1f1/Amon/tas/gn/v20181214/tas_Amon_BCC-ESM1_historical_r1i1p1f1_gn_185001-201412.nc",
12-
chunks={"time": 100},
13-
)
12+
"""CMORize a dataset as an example."""
13+
ds = create_test_dataset()
1414
print("Original:\n", ds)
15-
print("Converting..")
15+
print()
16+
17+
print("Fixing..")
1618
result = fix(
1719
ds,
1820
name="MIP-DRS7.CMIP7.CMIP.CCCma.CanESM6-MR.historical.r2i1p1f1.glb.mon.tas.tavg-h2m-hxy-u.g13s.v20250622",
1921
)
20-
print("Result:\n", result)
22+
print("Fixed dataset:\n", result)
2123
print("Saving to NetCDF..")
2224
result.to_netcdf("tas_fixed.nc")
2325

@@ -26,7 +28,7 @@ def main() -> None:
2628
import iris.loading # noqa: PLC0415
2729
except ImportError:
2830
return
29-
31+
print("Loading with Iris..")
3032
cube = iris.load_cube("tas_fixed.nc")
3133
print(cube.summary())
3234
if iris.loading.LOAD_PROBLEMS.problems:

0 commit comments

Comments
 (0)