本文簡要介紹 python 語言中 scipy.io.netcdf_file
的用法。
用法:
class scipy.io.netcdf_file(filename, mode='r', mmap=None, version=1, maskandscale=False)#
NetCDF 數據的文件對象。
A
netcdf_file
對象有兩個標準屬性:方麵和變量.兩者的值都是字典,分別將維度名稱映射到它們的關聯長度,將變量名稱映射到變量。應用程序不應該修改這些字典。所有其他屬性對應於 NetCDF 文件中定義的全局屬性。全局文件屬性是通過分配給
netcdf_file
對象的屬性來創建的。- filename: 字符串或file-like
字符串 -> 文件名
- mode: {‘r’, ‘w’, ‘a’},可選
read-write-append模式,默認為‘r’
- mmap: 無或布爾,可選
讀取時是否映射文件名。當 filename 是文件名時默認為 True,當 filename 是 file-like 對象時默認為 False。注意,在使用 mmap 時,返回的數據數組直接引用磁盤上的 mmap 數據,隻要引用存在,文件就不能關閉。
- version: {1, 2},可選
讀取/寫入的 netcdf 版本,其中 1 表示經典格式和 2 意味著64 位偏移格式.默認值為 1。請參閱這裏了解更多信息。
- maskandscale: 布爾型,可選
是否根據屬性自動縮放和/或屏蔽數據。默認為假。
參數 ::
注意:
該模塊相對於其他模塊的主要優勢在於它不需要將代碼鏈接到NetCDF 庫。該模塊派生自 pupynere 。
NetCDF 文件是一種自說明的二進製數據格式。該文件包含說明文件中的維度和變量的元數據。有關NetCDF 文件的更多詳細信息,請參閱這裏。 NetCDF 數據結構包含三個主要部分:
Dimensions
Variables
Attributes
維度部分記錄了變量使用的每個維度的名稱和長度。然後,變量將指示它使用的維度和任何屬性,例如數據單位,以及包含變量的數據值。最好包含一個與維度同名的變量以提供該軸的值。最後,屬性部分將包含附加信息,例如文件創建者的名稱或用於收集數據的工具。
將數據寫入NetCDF 文件時,通常需要指明“記錄維度”。記錄維度是變量的無界維度。例如,溫度變量可能具有緯度、經度和時間的維度。如果想隨著時間的推移將更多溫度數據添加到NetCDF 文件中,那麽溫度變量應該將時間維度標記為記錄維度。
此外,NetCDF 文件頭包含數據在文件中的位置,因此可以高效地進行訪問,而無需將不必要的數據加載到內存中。它使用
mmap
模塊來創建映射到磁盤上數據的 Numpy 數組,目的相同。請注意,當使用
netcdf_file
打開 mmap=True(默認為隻讀)的文件時,其返回的數組直接引用磁盤上的數據。如果此類數組處於活動狀態,則不應關閉該文件,並且在詢問時也無法徹底關閉該文件。如果要在文件關閉後對其進行處理,您可能需要複製從映射的 Netcdf 文件獲取的數據數組,請參閱下麵的示例。例子:
要創建 NetCDF 文件:
>>> from scipy.io import netcdf_file >>> import numpy as np >>> f = netcdf_file('simple.nc', 'w') >>> f.history = 'Created for a test' >>> f.createDimension('time', 10) >>> time = f.createVariable('time', 'i', ('time',)) >>> time[:] = np.arange(10) >>> time.units = 'days since 2008-01-01' >>> f.close()
注意
arange(10)
到time[:]
的分配。公開時間變量的切片允許在對象中設置數據,而不是讓arange(10)
覆蓋time
變量。要讀取我們剛剛創建的NetCDF 文件:
>>> from scipy.io import netcdf_file >>> f = netcdf_file('simple.nc', 'r') >>> print(f.history) b'Created for a test' >>> time = f.variables['time'] >>> print(time.units) b'days since 2008-01-01' >>> print(time.shape) (10,) >>> print(time[-1]) 9
NetCDF 文件以隻讀方式打開時,返回直接引用磁盤上 memory-mapped 數據的數組:
>>> data = time[:]
如果要在文件關閉後處理數據,則需要將其複製到主存:
>>> data = time[:].copy() >>> del time >>> f.close() >>> data.mean() 4.5
NetCDF 文件也可以用作上下文管理器:
>>> from scipy.io import netcdf_file >>> with netcdf_file('simple.nc', 'r') as f: ... print(f.history) b'Created for a test'
相關用法
- Python SciPy io.whosmat用法及代碼示例
- Python SciPy io.savemat用法及代碼示例
- Python SciPy io.loadmat用法及代碼示例
- Python SciPy io.mminfo用法及代碼示例
- Python SciPy io.mmread用法及代碼示例
- Python SciPy io.hb_read用法及代碼示例
- Python SciPy io.FortranFile用法及代碼示例
- Python SciPy io.readsav用法及代碼示例
- Python SciPy io.mmwrite用法及代碼示例
- Python SciPy io.hb_write用法及代碼示例
- Python SciPy interpolate.make_interp_spline用法及代碼示例
- Python SciPy interpolate.krogh_interpolate用法及代碼示例
- Python SciPy interpolative.reconstruct_matrix_from_id用法及代碼示例
- Python SciPy interpolate.InterpolatedUnivariateSpline用法及代碼示例
- Python SciPy interpolate.BSpline用法及代碼示例
- Python SciPy integrate.quad_vec用法及代碼示例
- Python SciPy interpolative.reconstruct_interp_matrix用法及代碼示例
- Python SciPy interpolate.LSQSphereBivariateSpline用法及代碼示例
- Python SciPy interpolate.griddata用法及代碼示例
- Python SciPy integrate.cumulative_trapezoid用法及代碼示例
- Python SciPy interpolate.splder用法及代碼示例
- Python SciPy interpolate.LinearNDInterpolator用法及代碼示例
- Python SciPy interpolate.PPoly用法及代碼示例
- Python SciPy interpolate.NdBSpline用法及代碼示例
- Python SciPy interpolate.pade用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.io.netcdf_file。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。