【wrf-python】批量读取多个nc文件中共同的变量并保存为csv文件

发布时间:2023年12月24日
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")

文章来源:https://blog.csdn.net/A18040554844/article/details/135183926
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。