python读取文件大小、时间,Python-从netCDF文件中读取数据,时间为“秒后”。测量开始...

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:

int32 base_time()

units: seconds since 1970-01-01 00:00:00 00:00

unlimited dimensions:

current shape = ()

filling off

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'][:]

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)

I get the error:

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

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.

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'][:]

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.

What you want is:

time_var = dataset.variables['time']

Then:

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

Should work as expected.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值