我有一个包含许多列的csv文件。我想将纬度,经度和时间列定义为维度,并将其他列设置为Xarray数据集中的变量。在每个点(拉丁对Lon和隆对)之间,相距10公里。在下面的代码中,我无法在数据框中定义像素大小。有可能吗?
示例文件:https://www.dropbox.com/s/5lwzrkiqn2rxxrp/file_2018-01-01_01.csv?dl=0
代码
import os
import pandas as pd
import glob
import xarray
path = r'/path/file' # use your path
all_files = glob.glob(os.path.join(path, "*.csv"))
colnames = ['time','Lat','Lon','Alt','Temperature', 'Relative Humidity', 'Wind speed', 'Wind direction', 'Short-wave irradiation']
for filename in all_files:
namecsv = os.path.splitext(os.path.basename(filename))[0]
df = pd.read_csv(filename, header=0, error_bad_lines=False, names = colnames, sep=',')
df["time"]= pd.to_datetime(df["time"])
xr = df.set_index(['Lat', 'Lon', 'time']).to_xarray()
# add variable attribute metadata
#xr['time'].attrs={'units':'hours since 2018-01-01'}
xr['Lat'].attrs={'units':'degrees', 'long_name':'Latitude'}
xr['Lon'].attrs={'units':'degrees', 'long_name':'Longitude'}
xr['Alt'].attrs={'units':'m', 'long_name':'depth'}
xr['Temperature'].attrs={'units':'k', 'long_name':'Air Temperature'}
xr['Relative Humidity'].attrs={'units':'%', 'long_name':'Air Relative Humidity'}
xr['Wind speed'].attrs={'units':'m/s', 'long_name':'Wind speed'}
xr['Wind direction'].attrs={'units':'deg', 'long_name':'Wind direction'}
xr['Short-wave irradiation'].attrs={'units':'Wh/m2', 'long_name':'Short-wave irradiation'}
# Save NetCDF file
xr.to_netcdf('/path/to/save/' + namecsv + '.nc')
del namecsv, df, xr
情节
import xarray as xr
da = xr.open_dataset('/home/file_2018-01-01_01.csv.nc').load()
da.Temperature[0].plot(yincrease=False)
结果
我想要什么: