Python-从netCDF文件中读取数据,时间为“秒后".测量开始 [英] Python - Read data from netCDF file with time as "seconds since" beginning of measurement

查看:647
本文介绍了Python-从netCDF文件中读取数据,时间为“秒后".测量开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从netCDf文件中提取值.我是python的新手,甚至是此文件格式的新手.我需要在特定位置(纬度,经度)提取时间序列数据. 我发现在UNIX时间中有一个变量(称为"base_time"),另一个变量(称为"time")具有"seconds since 2013-20-10 00:00:00"(这是测量时间的开始) UTC).

I need to extract values from a netCDf file. I am pretty new to python and even newer this file format. I need to extract time series data at a specific location (lat, lon). I have found that there is a variable (called "base_time") in UNIX time and another variable (called "time") with "seconds since 2013-20-10 00:00:00" (which is the beginning of measurement time in UTC) for now.

当我查询数据集的变量时,我得到了:

When i interrogate the dataset's variables, I get this:

<type 'netCDF4.Variable'>
int32 base_time()
    units: seconds since 1970-01-01 00:00:00 00:00
unlimited dimensions:
current shape = ()
filling off

<type 'netCDF4.Variable'>
float64 time(time)
    units: seconds since 2013-10-20 00:00:00 00:00
    interval(sec): 30.0
unlimited dimensions: time
current shape = (2880,)
filling off

当我以数组形式读取值时,例如

When I read the values as arrays, like e.g.

time_var = dataset.variables['time'][:]

我可以看到time变量中有2880个(大小为2880)值,而base_time变量中只有一个(大小为1)值. 我认为这个答案确实可以正是我所需要的,但是我在需要转换时间的部分上只有麻烦.当我这样做时:

I can see that there are 2880 (size is 2880) values in the time variable, but just one (size is 1) in the base_time variable. I think that this answer does exactly what I need, but I have only trouble with the part where I need to convert the time. When I do:

dtime = netCDF4.num2date(time_var[:],time_var.units)

我得到了错误:

AttributeError: 'numpy.ndarray' object has no attribute 'units'

我认为无论如何我都需要转换时间变量(自测量开始的秒数),而不是转换UNIX时间(因为netCDf文件中只有一个值?). 我尝试了datetime.dateime部分的一些变体,但是我不明白.我只需要将自2013-10-20 00:00:00起的秒数"转换为可读格式,即可提取和绘制数据. 谢谢!

And I think that I need to convert the time variable (seconds since beginning of measurement) anyway, instead of converting the UNIX time (because there is only one value in the netCDf file?). I tried some variations of the datetime.dateime part but I just don't get it. I only need to convert the "seconds since 2013-10-20 00:00:00" to a readable format to be able to extract und plot the data. Thanks!

推荐答案

抱歉,很冗长,但这是我最近遇到并掌握的.

Sorry for being verbose but this is something I've just recently run across and grasped.

在基于numpy的python NetCDF4 api中,NetCDF4.Variable和它包含的numpy数据数组之间存在着深远的区别.您的代码:

In the python NetCDF4 api, which is based on numpy, there is a profound difference between a NetCDF4.Variable and the numpy data array which it contains. Your code:

time_var = dataset.variables['time'][:]

不是NetCDF4变量,即不是time_var,而是数据值,数字的numpy ndarray,NetCDF变量属性丢失,在这种情况下为units:

is not a NetCDF4 variable, i.e. not a time_var but just the data values, a numpy ndarray of numbers, the NetCDF variable attributes are lost, in this case units:

units: seconds since 2013-10-20 00:00:00 00:00.

您想要的是:

time_var = dataset.variables['time']

然后:

dtime = netCDF4.num2date(time_var[:],time_var.units)

应能按预期工作.

这篇关于Python-从netCDF文件中读取数据,时间为“秒后".测量开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆