如何在python中使用netcdf flag_meansions属性

2024-06-17 13:33:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python创建一个netcdf文件,并希望将“flag_values”和“flag_meansions”属性分配给其中一个变量。变量名为“cs”,标志值应为(0,1),标志的含义应为(“STRATIFORM”,“conversative”)。在

为了在python中进行这些赋值,我使用了:

cs.flag_values = numpy.array((0,1))
cs.flag_meanings = numpy.array(['STRATIFORM','CONVECTIVE'])

“flag_values”赋值很好,但是“flag_meansions”赋值给了我一个由两个输入值串联起来的字符串,我不知道如何使这些字符串成为单独的实体。这是输出文件上ncdump的一部分:

^{pr2}$

当我尝试创建一个字符串数组并打印它时,我没有问题:

>>> a = np.array(['STRATIFORM','CONVECTIVE'])
>>> print a
['STRATIFORM' 'CONVECTIVE']

那么秘密是什么?在


Tags: 文件字符串numpy属性标志netcdfcsarray
3条回答

根据CF-conventions (Section 3.5, “Flags”)flag_meanings是一个字符串,其中的含义用空格隔开:

The flag_meanings attribute is a string whose value is a blank separated list of descriptive words or phrases, one for each flag value. Each word or phrase should consist of characters from the alphanumeric set and the following five: '_', '-', '.', '+', '@'. If multi-word phrases are used to describe the flag values, then the words within a phrase should be connected with underscores.

所以定义应该是

cs.flag_meaning = "convective stratiform"

我知道这是一个老帖子,但供将来参考。在

试试看

cs.flag_meaning = "Convective, Stratiform"

我认为只有netCDF4才支持字符串数组,您在创建文件时使用了正确的模块和正确的标志吗。您想要使用netcdf4 python模块(https://github.com/Unidata/netcdf4-python),并且需要将文件创建为:

from netCDF4 import Dataset
nc = Dataset('test.nc', 'w', format='NETCDF4')

NETCDF4应该是默认格式,但是如果它被重写(或者如果您没有使用NETCDF4库),我认为不支持字符串数组。在

相关问题 更多 >