-1

I am trying to create a contour plot showing annual dissolved iron concentrations for the years to 1860-1900 and am trying to exclude all values above a certain amount (0.000012) because these relate to a specific region which is throwing my data off.

Below is the script I am using, and each individual part works (before this I have of course imported all the relevant modules).

dissolved_iron = iris.load_cube('/home/em379/data/dis2/regriddedfiles/HadGEM2-ES_dfe_piControl_r1i1p1_regridded.nc')

dissolved_iron_timeslice = dissolved_iron.extract(iris.Constraint(time = lambda v: 1860 <= v <= 1900))

masked_dissolved_iron_timeslice = ma.masked_greater(dissolved_iron_timeslice.data, 0.000012)

qplt.contourf(masked_dissolved_iron_timeslice,25)

plt.show(block = False)

When I get to the step qplt.contourf(masked_dissolved_iron_timeslice,25), Python returns the error:

AttributeError: 'numpy.ndarray' object has no attribute 'dim_coords':

>>> qplt.contourf(masked_dissolved_iron_timeslice,25)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/Iris-1.7.0_dev-py2.7.egg/iris/quickplot.py", line 184, in contourf
    result = iplt.contourf(cube, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/Iris-1.7.0_dev-py2.7.egg/iris/plot.py", line 579, in contourf
    result = _draw_2d_from_points('contourf', None, cube, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/Iris-1.7.0_dev-py2.7.egg/iris/plot.py", line 275, in _draw_2d_from_points
    plot_defn = _get_plot_defn(cube, mode, ndims=2)
  File "/usr/local/lib/python2.7/dist-packages/Iris-1.7.0_dev-py2.7.egg/iris/plot.py", line 127, in _get_plot_defn
    for dim_coord in cube.dim_coords:
AttributeError: 'MaskedArray' object has no attribute 'dim_coords'
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • 2
    Could you provide a [minimal example](http://stackoverflow.com/help/mcve) and the *full* error traceback, please? – jonrsharpe Jan 16 '15 at 10:45
  • (A bit more code would be nice otherwise, we have to ask you to try things and it takes more time/effort on your part and ours). Does the contourf give an error if you put the unmasked data in? – Joel Jan 16 '15 at 11:04
  • @jonrsharpe Sorry I don't know anything about this really or how to phrase my question! I have posted the full traceback error in the question. Contourf works fine without the unmasked data – Ellie Morris Jan 16 '15 at 13:59
  • Could you provide an example that can actually be run? If you don't include the `import`s, for example, we don't know what e.g. `ma` is. Some minimal input data might help, too. – jonrsharpe Jan 16 '15 at 14:02
  • For reference and future questions, including the imports would have given a hint that qplt was actually ``import iris.quickplot as qplt`` and from there we would be able to determine that iris' quickplot contourf function takes *cubes* as arguments, not numpy arrays. – pelson Feb 04 '15 at 11:33

1 Answers1

1

The error gives us a clue as to what is going on:

AttributeError: 'MaskedArray' object has no attribute 'dim_coords'

This to me reads: "you're trying to do a cube thing with a MaskedArray".

So, looking at your code, that is indeed the case:

masked_dissolved_iron_timeslice = \
            ma.masked_greater(dissolved_iron_timeslice.data, 0.000012)

masked_dissolved_iron_timeslice is now a numpy masked array not an iris cube. The solution, if you just want to look at your data, is to change from using iris' plotting functionality, to using matplotlib's:

# Use plt, not qplt:
plt.contourf(masked_dissolved_iron_timeslice, 25)
plt.show()

Of course, if you want a little more context (and to maintain a cube) you can update the existing cube's data with:

dissolved_iron_timeslice = ...
dissolved_iron_timeslice.data = \
        ma.masked_greater(dissolved_iron_timeslice.data, 0.000012)

Or if you don't want to modify the existing cube:

dissolved_iron_timeslice = ...
ma_iron_t = dissolved_iron_timeslice.copy()
ma_iron_t.data = \
        ma.masked_greater(dissolved_iron_timeslice.data, 0.000012)

With these cubes, you can continue to use qplt and get the automatic labelling/colorbar/colormap/title goodness that Iris provides.

HTH

pelson
  • 21,252
  • 4
  • 92
  • 99