ismip / ismip6-antarctic-ocean-forcing

Scripts for generating ocean forcing (primarily Antarctic) for the ISMIP6 activity
MIT License
8 stars 4 forks source link

Error with maksing imbie with "descartes module version 1.1.0" #39

Closed inwoo-park closed 7 months ago

inwoo-park commented 7 months ago

Hi, Developers.

I am In-Woo Park and currently use ISSM (Ice-sheet and Sea-level System Model).

Now, I have been reading Jourdain's 2019 paper and try to make depeth dependent ocean temperature and salinity using this module "ismip6-antarctic-ocean-forcing".

However, I have encountered the following error:

Loading basin geometry...
/drive2/project-issm/ISMIP6/ismip6-antarctic-ocean-forcing/ismip6_ocean_forcing/imbie/images.py:71: ShapelyDeprecationWarning: The 'cascaded_union()' function is deprecated. Use 'unary_union()' instead.
  combinedShape = shapely.ops.cascaded_union(featureShapes)
  Writing basin images...
    A-Ap
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/drive2/project-issm/ISMIP6/ismip6-antarctic-ocean-forcing/ismip6_ocean_forcing/__main__.py", line 71, in <module>
    main()
  File "/drive2/project-issm/ISMIP6/ismip6-antarctic-ocean-forcing/ismip6_ocean_forcing/__main__.py", line 64, in main
    make_imbie_masks(config)
  File "/drive2/project-issm/ISMIP6/ismip6-antarctic-ocean-forcing/ismip6_ocean_forcing/imbie/make_imbie_masks.py", line 49, in make_imbie_masks
    images.write_basin_images(res, bedFileName, basins)
  File "/drive2/project-issm/ISMIP6/ismip6-antarctic-ocean-forcing/ismip6_ocean_forcing/imbie/images.py", line 61, in write_basin_images
    _write_basin_image(res, basinShape, name, nx, ny, dx)
  File "/drive2/project-issm/ISMIP6/ismip6-antarctic-ocean-forcing/ismip6_ocean_forcing/imbie/images.py", line 102, in _write_basin_image
    ax.add_patch(PolygonPatch(basinShape, fc=color, ec=color, linewidth=0))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/inwoo/externalpackages/miniforge3/envs/ismip6/lib/python3.11/site-packages/descartes/patch.py", line 87, in PolygonPatch
    return PathPatch(PolygonPath(polygon), **kwargs)
                     ^^^^^^^^^^^^^^^^^^^^
  File "/home/inwoo/externalpackages/miniforge3/envs/ismip6/lib/python3.11/site-packages/descartes/patch.py", line 62, in PolygonPath
    vertices = concatenate([
                           ^
  File "/home/inwoo/externalpackages/miniforge3/envs/ismip6/lib/python3.11/site-packages/descartes/patch.py", line 63, in <listcomp>
    concatenate([asarray(t.exterior)[:, :2]] +
                 ~~~~~~~~~~~~~~~~~~~^^^^^^^
IndexError: too many indices for array: array is 0-dimensional, but 2 were indexed

I found that the error origniates from the new version descartes==1.1.0 that currently being used in pypi and conda repositories. Therefore, I made the following adjustments to the code in ismip6_ocean_forcing/imbie/images.py on my local machine.

diff --git a/ismip6_ocean_forcing/imbie/images.py b/ismip6_ocean_forcing/imbie/images.py
old mode 100644
new mode 100755
index 504954f..b33ad39
--- a/ismip6_ocean_forcing/imbie/images.py
+++ b/ismip6_ocean_forcing/imbie/images.py
@@ -3,6 +3,8 @@ import xarray
 import shapely.geometry
 import shapely.ops
 from descartes import PolygonPatch
+from importlib.metadata import version # Check specific module version.
+import numpy as np
 import os.path
 import matplotlib.pyplot as plt

@@ -95,7 +97,10 @@ def _write_basin_image(res, basinShape, name, nx, ny, dx):
     color = 'black'

     if basinShape.geom_type == 'Polygon':
-        ax.add_patch(PolygonPatch(basinShape, fc=color, ec=color, linewidth=0))
+        if version('descartes') >= '1.1.0':
+            ax.add_patch(PolygonPatch(basinShape.__geo_interface__, fc=color, ec=color, linewidth=0))
+        else:
+            ax.add_patch(PolygonPatch(basinShape, fc=color, ec=color, linewidth=0))
     elif basinShape.geom_type == 'MultiPolygon':
         for poly in basinShape:
             ax.add_patch(PolygonPatch(poly, fc=color, ec=color, linewidth=0))

I hope that this diff comment helps you to fix your code.

Best regards, Inwoo

xylar commented 7 months ago

@inwoo-park, this is great!

I would be in favor of requiring descartes >=1.1.0 and including your first fix.

xylar commented 7 months ago

Thank you again. This should now be fixed.

inwoo-park commented 7 months ago

@xylar Thank you for reflecting my suggestion!