【问题标题】:Total Size of New Array Must Be Unchanged (netCDF)新数组的总大小必须保持不变 (netCDF)
【发布时间】:2016-06-09 19:22:09
【问题描述】:

我在将 CSV 文件转换为 NetCDF 时遇到问题。我在 Python 中使用 numpy 和 netCDF4 来尝试实现这一目标。

错误:

Traceback (most recent call last):
File "csv2nc.py", line 47, in <module>
tmaxsfc[:] = temps
File "netCDF4.pyx", line 3167, in netCDF4.Variable.__setitem__ (netCDF4.c:39349)
ValueError: total size of new array must be unchanged

脚本:

import numpy as np
import netCDF4

data = np.loadtxt('/home/weather/Dropbox/d0_tmaxs', delimiter=',', skiprows=0)

temp = data[:,1]
lat = data[:,2]
lon = data[:,3]

with netCDF4.Dataset('tmaxsfc.nc', mode="w", format='NETCDF4') as ds:
# some file-level meta-data attributes:
ds.Conventions = "CF-1.6"
ds.title = 'Maximum Temperature values for Day 0'
ds.institution = 'weather'
ds.source = ''

#print(lon.shape)

lats = np.array(lat,np.float32).reshape(255)
lons = np.array(lon,np.float32).reshape(255)
temps = np.array(temp,np.float32).reshape(255)

ds.createDimension('latitude', 255)
ds.createDimension('longitude', 255)

tmaxsfc = ds.createVariable('tmaxsfc', 'f4', ('latitude', 'longitude',))
tmaxsfc[:] = temps
tmaxsfc.units = 'F'
tmaxsfc.long_name = 'Temperature'

d0_tmaxs 文本文件示例:

20160226,40,36.65408,-83.21783
20160226,35.1508,41.00928,-74.73628
20160226,31,43.77714,-71.75598
20160226,23.8302,44.41944,-72.01944
20160226,22,39.5803,-79.3394

不管怎样,这个文本文件有 255 行。我做错了什么?

【问题讨论】:

    标签: python csv numpy netcdf


    【解决方案1】:

    我对@9​​87654321@ 有点生疏了,但没有

    tmaxsfc = ds.createVariable('tmaxsfc', 'f4', ('latitude', 'longitude',))
    

    意味着tmaxsfc 是一个二维数组,255 x 255?您正在按经度的纬度网格上定义它。

    但看起来您的输入会显示在散点图上,有 255 个点,每个点由纬度、经度和温度定义。

    【讨论】:

      【解决方案2】:

      所以错误来自temp 是具有 255 个元素的一维数组,但 tmaxsfc 变量被定义为二维变量。您要做的是制作一个单一维度,例如 station,所有 3 个 1D 变量都可以是以下函数:

      import numpy as np
      import netCDF4
      
      data = np.loadtxt('/home/weather/Dropbox/d0_tmaxs', delimiter=',', skiprows=0)
      
      temp = np.array(data[:,1], dtype=np.float32).reshape(255)
      lat = np.array(data[:,2], dtype=np.float32).reshape(255)
      lon = np.array(data[:,3], dtype=np.float32).reshape(255)
      
      with netCDF4.Dataset('tmaxsfc.nc', mode="w", format='NETCDF4') as ds:
          # some file-level meta-data attributes:
          ds.Conventions = "CF-1.6"
          ds.title = 'Maximum Temperature values for Day 0'
          ds.institution = 'weather'
          ds.source = ''
      
          ds.createDimension('station', 255)
          dims = ('station',)
      
          tmaxsfc = ds.createVariable('tmaxsfc', np.float32, dims) 
          tmaxsfc[:] = temps
          tmaxsfc.units = 'F'
          tmaxsfc.long_name = 'Temperature'
      
          lon_var = ds.createVariable('longitude', np.float32, dims)
          lon_var[:] = lon
          lon.units = 'degrees_east'
      
          lat_var = ds.createVariable('latitude', np.float32, dims)
          lat_var[:] = lat
          lat.units = 'degress_north'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-16
        • 2020-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-18
        • 2019-04-25
        相关资源
        最近更新 更多