Skip to content

Commit d7ca2ba

Browse files
committed
Can't assume that the input's coordinates are in the same order as the output's dimensions.
* Closes monocongo#445
1 parent 9110827 commit d7ca2ba

File tree

3 files changed

+7
-15
lines changed

3 files changed

+7
-15
lines changed

notebooks/generate_fitting_parameters_nclimgrid.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246
" da_spi = xr.DataArray(\n",
247247
" data=spi,\n",
248248
" coords=da_precip.coords,\n",
249-
" dims=da_precip.dims,\n",
249+
" dims=(\"lat\", \"lon\", \"time\"),\n",
250250
" name=f\"spi_gamma_{str(scale).zfill(2)}\",\n",
251251
" )\n",
252252
" da_spi.attrs = {\n",
@@ -452,7 +452,7 @@
452452
" da_spi = xr.DataArray(\n",
453453
" data=spi,\n",
454454
" coords=da_precip.coords,\n",
455-
" dims=da_precip.dims,\n",
455+
" dims=(\"lat\", \"lon\", \"time\"),\n",
456456
" name=f\"spi_gamma_{str(scale).zfill(2)}\",\n",
457457
" )\n",
458458
" da_spi.attrs = {\n",

notebooks/muitprocess_spi_nclimgrid.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@
411411
" da_spi = xr.DataArray(\n",
412412
" data=spi,\n",
413413
" coords=da_precip.coords,\n",
414-
" dims=da_precip.dims,\n",
414+
" dims=(\"lat\", \"lon\", \"time\"),\n",
415415
" name=f\"spi_gamma_{str(scale).zfill(2)}\",\n",
416416
" )\n",
417417
" da_spi.attrs = {\n",

src/climate_indices/__spi__.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -582,14 +582,6 @@ def _compute_write_index(keyword_arguments):
582582

583583
# add an empty DataArray to the fitting Dataset if it's not been loaded
584584
if keyword_arguments["load_params"] is None:
585-
586-
dims = list(ds_precip.dims)
587-
time_index = dims.index("time")
588-
if keyword_arguments['periodicity'] == compute.Periodicity.monthly:
589-
dims[time_index] = "month"
590-
else: # daily
591-
dims[time_index] = "day"
592-
593585
fitting_var_attrs = {
594586
"alpha": {
595587
'description': 'shape parameter of the gamma distribution (also '
@@ -615,12 +607,12 @@ def _compute_write_index(keyword_arguments):
615607
},
616608
}
617609

610+
fitting_shape = [len(x) for x in ds_fitting.coords.values()]
618611
for var in _FITTING_VARIABLES:
619-
fitting_shape = [len(x) for x in ds_fitting.coords.values()]
620612
da_fitting = xr.DataArray(
621613
data=np.full(shape=fitting_shape, fill_value=np.NaN),
614+
# Assumes it is in the proper order.
622615
coords=ds_fitting.coords,
623-
dims=dims,
624616
name=fitting_var_names[var],
625617
attrs=fitting_var_attrs[var],
626618
)
@@ -809,7 +801,7 @@ def build_dataset_spi_grid(
809801
da_spi = xr.DataArray(
810802
data=index_values,
811803
coords=ds_example.coords,
812-
dims=ds_example.dims,
804+
dims=("lat", "lon", "time"),
813805
name=spi_var_name,
814806
attrs=var_attrs,
815807
)
@@ -862,7 +854,7 @@ def build_dataset_spi_divisions(
862854
da_spi = xr.DataArray(
863855
data=index_values,
864856
coords=ds_example.coords,
865-
dims=ds_example.dims,
857+
dims=("division", "time"),
866858
name=spi_var_name,
867859
attrs=var_attrs,
868860
)

0 commit comments

Comments
 (0)