3

I would like to create plot images from a NetCDF at each time step.

My NetCDF files look like this:

netcdf file:/C:/home/data/cmorph/test/reduced_cmorph_adjusted_spi_pearson_01.nc {
  dimensions:
    time = UNLIMITED;   // (240 currently)
    lat = 120;
    lon = 360;
  variables:
    float spi_pearson_01(time=240, lat=120, lon=360);
      :_FillValue = NaNf; // float
      :valid_min = -3.09; // double
      :valid_max = 3.09; // double
      :long_name = "Standard Precipitation Index (Pearson Type III distribution), 1-month scale";
      :_ChunkSizes = 1, 120, 360; // int

    int time(time=240);
      :units = "days since 1800-01-01 00:00:00";
      :_ChunkSizes = 1024; // int
      :_CoordinateAxisType = "Time";

    float lat(lat=120);
      :units = "degrees_north";
      :_CoordinateAxisType = "Lat";

    float lon(lon=360);
      :units = "degrees_east";
      :_CoordinateAxisType = "Lon";

  // global attributes:
  :title = "CMORPH Version 1.0BETA Version, daily precip from 00Z-24Z";
  :history = "Wed Feb 28 07:30:01 2018: C:\\home\\miniconda\\Library\\bin\\ncks.exe --dmn lon,0,,4 --dmn lat,0,,4 CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_1998_2017.nc cmorph_reduced_adjusted.nc";
  :NCO = "4.7.1";
  :_CoordSysBuilder = "ucar.nc2.dataset.conv.DefaultConvention";
}

I like the plots produced by Panoply but I haven't worked out how to script it (I don't want to go through the GUI for this since I'll have roughly 1500 plots to create). I'm not wedded to Panoply per se, so if someone has a better idea please advise. I could hammer this out in matplotlib but it'd take me quite a while and wouldn't look as good as the Panoply plots. I'm trying to avoid doing much if any of the plotting myself, but maybe there's something out there that provides easy plotting of NetCDFs which can be called from a script (I typically use Python and Bash).

2 Answers 2

4

Example using xarray:

import xarray as xr
import matplotlib
import matplotlib.pyplot as plt

matplotlib.use('Agg')
file_name = "reduced_cmorph_adjusted_spi_pearson_01.nc"
with xr.open_dataset(file_name) as ds:
    for t in range(ds.time.shape[0]):
        da = ds.spi_pearson_01.isel(time=t)
        plt.figure()
        da.plot()
        plt.savefig('frame{}.png'.format(t))

Non-scripting method if you don't mind using a few clicks in Panoply: create a lat/lon plot and then choose File->Export Animation . You can output individual time steps as JPG or PNG.

Sign up to request clarification or add additional context in comments.

4 Comments

I've run up against an issue using the xarray portion of the answer above, at da.plot(): ... File "C:\home\Anaconda3\lib\site-packages\xarray\plot\utils.py", line 154, in _determine_cmap_params vmin = calc_data.min() File "C:\home\Anaconda3\lib\site-packages\numpy\core_methods.py", line 29, in _amin return umr_minimum(a, axis, None, out, keepdims) ValueError: zero-size array to reduction operation minimum which has no identity
I am not sure about the origins of that error. Looking at the code, I would use matplotlib noninteractive backend, matplotlib.use('Agg') but that's probably not the cause.
Well, that was a good guess anyway, I've had to do that before when using matplotlib. I gave it a whirl here but still no joy.
I have amended answer anyway. Also added in plt.figure() which I found was necessary to initialize each plot. This code definitely works for me (using vanilla netcdf file as substitute input), however I am not using Anaconda and my lib versions may be different.
3

I'm kind of assuming you don't want to insert 1500 figures in a report or talk and therefore the purpose of this is just to investigate the file slice by slice. If this is the case I would simply open the file using

ncview file.nc 

This allows you to step through the slices, animate, pass the cursor over the slices to see the values and click on a point to see a timeseries. If you don't have it, you can install it easily with apt-get (ubuntu, mint etc) with

sudo apt-get install ncview

2 Comments

Thanks, Adrian, solid answer. I'm working with a scientist who wants to make comparisons between multiple datasets at an indeterminate number of time steps. I thought it'd be good to gin up a script that produces plots at each time step so he can quickly pick and choose from a collection of plot images.I typically use Panoply for plots so that's why I asked about doing this with Panoply.Does ncview have the ability to output plots as image files? A cursory look at the docs didn't reveal this feature, if I've missed something please advise. Thanks again for your help.
Yes, but it is a bit crude and also manual as far as I know - you can use the "print" option and then dump to a file, but that will not be helpful for you I think as it is not automated. Usually I would open the files in separate ncview incidences, or examine the difference between two files using "cdo sub f1.nc f2.nc diff.nc" and then viewing the difference in ncview. I'm just trying to imagine someone laying 1500 plots on their desk and comparing them to another 1500 plots! ;-) Better to "surf" the file digitially in my view...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.