python - NetCDF:如何在每个时间步编写脚本?

标签 python plot netcdf

我想在每个时间步从 NetCDF 创建绘图图像。

我的 NetCDF 文件如下所示:

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";
}

我喜欢 Panoply 制作的情节,但我还没有想出如何编写脚本(我不想为此通过 GUI,因为我将要创建大约 1500 个情节)。我并不执着于 Panoply 本身,所以如果有人有更好的主意,请提出建议。我可以在 matplotlib 中解决这个问题,但这会花费我相当长的时间,而且看起来不如 Panoply 图好。我尽量避免自己做任何绘图,但也许有一些东西可以提供简单的 NetCDF 绘图,可以从脚本调用(我通常使用 Python 和 Bash)。

最佳答案

使用 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))

非脚本方法,如果您不介意在 Panoply 中单击几下:创建一个纬度/经度图,然后选择"file"->“导出动画”。您可以将各个时间步长输出为 JPG 或 PNG。

关于python - NetCDF:如何在每个时间步编写脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49035660/

相关文章:

r - 使用 R 转换为 NetCDF 时保留栅格变量名称

python - 为什么 int(float(str)) 工作但 int(str) 不工作

python - 如何为带存储桶的 seq2seq 模型设置 tfrecords 队列?

python - MatLab 到 Python 欧拉方程

python - 如何使用 matplotlib 在轴上绘制带有参数的函数

使用 r 中的单个函数返回两个图

python - "pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available"

python - 在 Google App Engine 的 ModelForm 中设置父级

python - 您可以在 nc 文件中根据多年的日期范围提取数据吗?

python - 重命名 pcraster Mapstack