簡體   English   中英

在 Python 中使用 lat long time 變量創建 netcdf 文件時出錯

[英]Error while creating netcdf file using lat long time variable in Python

我已經編寫了這段代碼來使用 lat long time 變量(例如 precip)編寫 NETCDF 文件。 我正在從 csv 文件中讀取所有數據集。 因此,我制作了兩個 csv 文件:(1)包含降水時間序列數據(第 11 行,第 9 列)和(2)包含緯度和經度(例如 X、Y)。 當我運行代碼時,NETCDF 文件正在生成,但它沒有以正確的方式寫入。 維度、時間和其他屬性信息出現錯誤。 我的代碼如下所示

import pandas as pd
import numpy as np
import netCDF4

stn_precip='stn_precip.csv'
orig_precip='precip_data.csv'
stations = pd.read_csv(stn_precip)
stncoords = stations.iloc[:,:]
orig = pd.read_csv(orig_precip)

lons = stncoords['X']
lats = stncoords['Y']
nstations = np.size(lons)

ncout = netCDF4.Dataset('precip_3.nc', 'w')

ncout.createDimension('station',nstations)
ncout.createDimension('time',orig.shape[0])

lons_out = lons.tolist()
lats_out = lats.tolist()
time_out = orig.index.tolist()

lats = ncout.createVariable('latitude',np.dtype('float32').char,('station',))
lons = ncout.createVariable('longitude',np.dtype('float32').char,('station',))
time = ncout.createVariable('time',np.dtype('float32').char,('time',))
precip = ncout.createVariable('precip',np.dtype('float32').char,('time', 'station'))

lats[:] = lats_out
lons[:] = lons_out
time[:] = time_out
precip[:] = orig
ncout.close()

csv 文件包含這樣的數據

在此處輸入圖像描述

在此處輸入圖像描述

使用給定數據執行上述代碼后,我得到了這個(似乎錯誤)

<xarray.Dataset>
Dimensions:    (station: 9, time: 10)
Coordinates:
  * time       (time) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Dimensions without coordinates: station
Data variables:
    latitude   (station) float32 ...
    longitude  (station) float32 15.875 15.875 15.875 15.875 15.875 15.875 ...
    precip     (time, station) float32 ...

我想要像這樣的NETCDF output

Dimensions:  (lat: 1, lon: 9, time: 11)
Coordinates:
  * time     (time) datetime64[ns] 1901-01-01 1901-01-02 1901-01-03 ...
  * lon      (lon) float64 80.875..............................82.875
  * lat      (lat) float64 15.875..............................15.875
Data variables:
    precip       (time, lat, lon) float32 ...
Attributes:
    CDI:          Climate Data Interface version 1.9.5 (http://mpimet.mpg.de/...
    Conventions:  CF-1.6
    history:      Sun Dec 30 02:15:30 2018: cdo -f nc import_binary rf.ctl RF...
    CDO:          Climate Data Operators version 1.9.5 (http://mpimet.mpg.de/...

好吧,您不能將時間、緯度、經度作為數據的維度,因為……它們不是數據的維度。 沉淀數據的尺寸(想想 function 的)站和時間,所以這些是你的尺寸。 這是因為您沒有緯度和經度獨立變化的網格。 相反,它們都隨着您正在查看的車站而變化,這就是車站是一個維度的原因。

不過,聽起來您可能想要的是 lat/lon 正確顯示為precip變量的坐標。 為此,您希望根據 netCDF 的氣候和預報(CF) 元數據約定的規定,向您的precip變量添加與值'latitude longitude' coordinates的屬性。 這告訴遵循約定的工具, latitudelongitude是輔助坐標變量。 下面的代碼:

import numpy as np
import netCDF4

nstations = 3
lats = np.linspace(25, 50, nstations)
lons = np.linspace(-120, -60, nstations)
time_out = np.arange(5)
precip_out = np.random.randn(time_out.size, nstations)

ncout = netCDF4.Dataset('precip_3.nc', 'w')

ncout.createDimension('station',nstations)
ncout.createDimension('time', time_out.size)

lons_out = lons.tolist()
lats_out = lats.tolist()

lats = ncout.createVariable('latitude', 'float32', ('station',))
lons = ncout.createVariable('longitude', 'float32' , ('station',))
time = ncout.createVariable('time', 'float32', ('time',))
precip = ncout.createVariable('precip', 'float32', ('time', 'station'))
precip.coordinates = 'latitude longitude'

lats[:] = lats_out
lons[:] = lons_out
time[:] = time_out
precip[:] = precip_out
ncout.close()

為我產生以下 xarray output :

Dimensions:    (station: 3, time: 5)
Coordinates:
    latitude   (station) float32 ...
    longitude  (station) float32 ...
  * time       (time) float32 0.0 1.0 2.0 3.0 4.0
Dimensions without coordinates: station
Data variables:
    precip     (time, station) float32 ...

暫無
暫無

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

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