簡體   English   中英

Python - 從 netCDF 文件中讀取數據,時間為“自測量開始以來的秒數”

[英]Python - Read data from netCDF file with time as "seconds since" beginning of measurement

我需要從 netCDf 文件中提取值。 我對python很陌生,甚至對這種文件格式也很新。 我需要在特定位置(緯度、經度)提取時間序列數據。 我發現 UNIX 時間中有一個變量(稱為“base_time”),另一個變量(稱為“time”)為“自 2013-20-10 00:00:00 以來的秒數”(這是測量時間的開始) UTC)現在。

當我詢問數據集的變量時,我得到了這個:

<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

當我將值作為數組讀取時,例如

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

我可以看到 time 變量中有 2880 個(大小為 2880)個值,但 base_time 變量中只有一個(大小為 1)。 我認為這個答案正是我所需要的,但我只在需要轉換時間的部分遇到了麻煩。 當我做:

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

我收到錯誤:

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

而且我認為無論如何我都需要轉換時間變量(自測量開始以來的秒數),而不是轉換 UNIX 時間(因為 netCDf 文件中只有一個值?)。 我嘗試了 datetime.datetime 部分的一些變體,但我就是不明白。 我只需要將“自 2013-10-20 00:00:00 以來的秒數”轉換為可讀格式即可提取和繪制數據。 謝謝!

抱歉我說得太冗長了,但這是我最近剛剛遇到並掌握的東西。

在基於 numpy 的 python NetCDF4 api 中,NetCDF4.Variable 和它包含的 numpy 數據數組之間存在巨大差異。 您的代碼:

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

不是 NetCDF4 變量,即不是 time_var 而只是數據值,一個 numpy ndarray 數字,NetCDF 變量屬性丟失,在這種情況下為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)

應該按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM