如何在 python 中使用 netcdf flag_meanings 属性

how to use netcdf flag_meanings attribute in python

我正在 python 中创建一个 netcdf 文件,并想将 'flag_values' 和 'flag_meanings' 属性分配给其中一个变量。变量名是'cs',flag_values应该是(0,1),flag_meanings应该是('STRATIFORM','CONVECTIVE').

为了在 python 中进行这些分配,我使用了:

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

'flag_values' 赋值工作正常,但 'flag_meanings' 赋值给了我两个输入值的串联字符串,我不知道如何让字符串成为单独的实体.这是输出文件中 ncdump 的一部分:

cs:flag_values = 0L, 1L ;
cs:flag_meanings = "STRATIFORMCONVECTIVE" ;

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

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

那么秘诀是什么?

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

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

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

我知道这是旧的 post,但供将来参考。

试试,

cs.flag_meaning = "Convective, Stratiform"

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

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"