如何获取存储在 NetCDF 文件中的数据的有效位数?
How can I get the number of significant digits of the data stored in a NetCDF file?
我需要知道存储在 NetCDF 文件中的数据的精度。
我认为有可能知道这个精度,因为当我使用 ncdump 转储 NetCDF 文件时,显示的有效位数取决于我使用的特定 NetCDF 文件。
因此,对于一个文件,我得到:
Ts = -0.2121478, -0.08816089, -0.4285178, -0.3446428, -0.4800949,
-0.4332879, -0.2057121, -0.06589077, -0.001647412, 0.007711744,
另一个:
Ts = -2.01, -3.6, -1, -0.53, -1.07, -0.7, -0.56, -1.3, -0.93, -1.41, -0.83,
-0.8, -2.13, -2.91, -1.13, -1.2, -2.23, -1.77, -2.93, -0.7, -2.14, -1.36,
我还必须说,在任何属性中都没有关于精度的信息,无论是变量的全局属性还是局部属性。您可以在 NetCDF 文件 header 的转储中看到这一点:
netcdf pdo {
dimensions:
time = UNLIMITED ; // (809 currently)
variables:
double time(time) ;
time:units = "months since 1900-01-01" ;
time:calendar = "gregorian" ;
time:axis = "T" ;
double Ts(time) ;
Ts:missing_value = NaN ;
Ts:name = "Ts" ;
// global attributes:
:Conventions = "CF-1.0" ;
}
有谁知道如何获取存储在 NetCDF 文件中的数据的有效位数?
这是一个棘手的问题:ncdump
(以及许多其他漂亮的数字生成器)所做的只是从小数部分中删除尾随零,但这是否说明了实数(observed/calculated/..) 值的精度?用三位小数精度测量的东西可能是 1.100
,但 ncdump
仍会将其打印为 1.1
。如果你想知道真正的(物理?)意义,它确实必须作为一个属性包含在内,或者在别处记录。
对于大量数字,计算数字小数部分中有效数字的最大位数可能是精度的第一个指示。如果这就是您要找的东西,类似这样的东西可能适用于 Python:
import numpy as np
a = np.array([1.01, 2.0])
b = np.array([1.10, 1])
c = np.array([10., 200.0001])
d = np.array([1, 2])
def count_max_significant_fraction(array):
# Return zero for any integer type array:
if issubclass(array.dtype.type, np.integer):
return 0
decimals = [s.rstrip('0').split('.')[1] for s in array.astype('str')]
return len(max(decimals, key=len))
print( count_max_significant_fraction(a) ) # prints "2"
print( count_max_significant_fraction(b) ) # prints "1"
print( count_max_significant_fraction(c) ) # prints "4"
print( count_max_significant_fraction(d) ) # prints "0"
我建议您采用 NCO 使用的约定并将精度属性命名为 "number_of_significant_digits" and/or "least_significant_digit"。术语在 here.
开始的冗长精确讨论中定义
我需要知道存储在 NetCDF 文件中的数据的精度。
我认为有可能知道这个精度,因为当我使用 ncdump 转储 NetCDF 文件时,显示的有效位数取决于我使用的特定 NetCDF 文件。
因此,对于一个文件,我得到:
Ts = -0.2121478, -0.08816089, -0.4285178, -0.3446428, -0.4800949, -0.4332879, -0.2057121, -0.06589077, -0.001647412, 0.007711744,
另一个:
Ts = -2.01, -3.6, -1, -0.53, -1.07, -0.7, -0.56, -1.3, -0.93, -1.41, -0.83, -0.8, -2.13, -2.91, -1.13, -1.2, -2.23, -1.77, -2.93, -0.7, -2.14, -1.36,
我还必须说,在任何属性中都没有关于精度的信息,无论是变量的全局属性还是局部属性。您可以在 NetCDF 文件 header 的转储中看到这一点:
netcdf pdo {
dimensions:
time = UNLIMITED ; // (809 currently)
variables:
double time(time) ;
time:units = "months since 1900-01-01" ;
time:calendar = "gregorian" ;
time:axis = "T" ;
double Ts(time) ;
Ts:missing_value = NaN ;
Ts:name = "Ts" ;
// global attributes:
:Conventions = "CF-1.0" ;
}
有谁知道如何获取存储在 NetCDF 文件中的数据的有效位数?
这是一个棘手的问题:ncdump
(以及许多其他漂亮的数字生成器)所做的只是从小数部分中删除尾随零,但这是否说明了实数(observed/calculated/..) 值的精度?用三位小数精度测量的东西可能是 1.100
,但 ncdump
仍会将其打印为 1.1
。如果你想知道真正的(物理?)意义,它确实必须作为一个属性包含在内,或者在别处记录。
对于大量数字,计算数字小数部分中有效数字的最大位数可能是精度的第一个指示。如果这就是您要找的东西,类似这样的东西可能适用于 Python:
import numpy as np
a = np.array([1.01, 2.0])
b = np.array([1.10, 1])
c = np.array([10., 200.0001])
d = np.array([1, 2])
def count_max_significant_fraction(array):
# Return zero for any integer type array:
if issubclass(array.dtype.type, np.integer):
return 0
decimals = [s.rstrip('0').split('.')[1] for s in array.astype('str')]
return len(max(decimals, key=len))
print( count_max_significant_fraction(a) ) # prints "2"
print( count_max_significant_fraction(b) ) # prints "1"
print( count_max_significant_fraction(c) ) # prints "4"
print( count_max_significant_fraction(d) ) # prints "0"
我建议您采用 NCO 使用的约定并将精度属性命名为 "number_of_significant_digits" and/or "least_significant_digit"。术语在 here.
开始的冗长精确讨论中定义