创建变量以创建 netCDF 文件时出错

编程入门 行业动态 更新时间:2024-10-26 00:22:59
本文介绍了创建变量以创建 netCDF 文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我能够计算出以下代码所示的经纬度维度声明.我想我已经接近获得 netCDF 文件了.但是,我收到一个错误.

I was able to figure out the lat, lon dimension declaration as indicated in the following code. I think I am close to obtain the netCDF file. But, I get an error.

import numpy as np import netCDF4 import os # load the data path='C:\Users\.spyder2' os.chdir(path) # this load the file into a Nx3 array (three columns) data = np.loadtxt('TRMM_1998_01_0100_newntcl.csv', delimiter=',') # create a netcdf Data object with netCDF4.Dataset('TEST_file.nc', mode="w", format='NETCDF4') as ds: # some file-level meta-data attributes: ds.Conventions = "CF-1.6" ds.title = 'Non TC precipitation' ds.institution = 'USU' lat = data[:,0] # the first column lon = data[:,1] # the second column precip = data[:,2] # the third column nlat = lat.reshape( (161, 321) ) nlon = lon.reshape( (161, 321) ) # time = ds.createDimension('time', 0) ds.createDimension('latitude', 161) ds.createDimension('longitude', 321) precip = ds.createVariable('precip', 'f4', ('latitude', 'longitude')) precip[:] = data[:,:] ## adds some attributes precip.units = 'mm' precip.long_name = 'Precipitation'

错误:

Traceback (most recent call last): File "<ipython-input-101-483efc7d87e2>", line 42, in <module> precip[:] = data[:,:] File "netCDF4.pyx", line 3066, in netCDF4.Variable.__setitem__ (netCDF4.c:38720) File "netCDF4.pyx", line 3177, in netCDF4.Variable._put (netCDF4.c:39523) IndexError: size of data array does not conform to slice

如果您能在这里澄清一下或向我提供一些纠正方法的线索,我将不胜感激.

I appreciate if you can clarify here a bit or provide me some clues of what's happening to correct it.

提前致谢,

推荐答案

创建维度是在没有分配给变量的情况下完成的.只需在定义维度时删除 lat= 和 lon=,然后您就可以在创建变量时引用这些维度.

Creating dimensions is done with no assignment to a variable. Just remove the lat= and lon= when defining dimensions and then you can reference the dimensions while creating a variable.

ds.createDimension('latitude', 161) ds.createDimension('longitude', 321) precip = ds.createVariable('precip, 'f4', ('latitude', 'longitude',))

此外,当您在脚本中多次重复使用 lat 时,请注意范围问题.在处理实际数据、命名维度和维度大小时,最好使用唯一名称.我通常做一些类似lat_arr(用于数据)、nlat(用于尺寸大小)和lat 或latitude> 用于维度名称.

Also, be careful with scoping issues as you re-use lat several times in the script. Best to use unique names when dealing with the actual data, named dimensions, and dimension sizes. I usually do something like lat_arr (for the data), nlat (for dimension size), and lat or latitude for the dimension name.

更多推荐

创建变量以创建 netCDF 文件时出错

本文发布于:2023-10-29 07:46:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539127.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   文件   netCDF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!