import xarray
import os
path = "/copy_merge/"
comb_ds = xr.Dataset()
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".nc"):
ds = xr.open_dataset(os.path.join(root, file))
tSK_var = ds['TSK']
comb_ds[file] = tSK_var
comb_ds.to_netcdf("combined_data1.nc")
path = "combined_data1.nc"
ds= xr.open_dataset(path)
df=ds.to_dataframe()
df
df.to_csv("output1.csv")
对经纬度范围和时间进行切片保存
import xarray as xr
import os
path = "/mnt/hgfs/J/0hsyPaper202406/4data/4-wrfout-231106/copy_merge/"
comb_ds = xr.Dataset()
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".nc"):
ds = xr.open_dataset(os.path.join(root, file))
t2 = ds['T2'][1:,::]
# print(tsk.dims)
comb_ds[file] = t2
# print(comb_ds)
comb_ds.to_netcdf("combined_data2.nc")
path = "combined_data2.nc"
ds= xr.open_dataset(path)
ds1=ds.sel(time=slice(1,276), south_north=slice(9,108), west_east = slice(9,88))
ds1.to_netcdf("new_t2.nc")
df1=xr.open_dataset("new_t2.nc")
df2=df1.to_dataframe()
df2.to_csv("output_T2.csv")