Python读取一个文件夹下所有nc文件

转载:https://blog.csdn.net/go_with_the_wind/article/details/124002284
代码:

import numpy as np
import netCDF4 as nc
import os
path = '/polarRS_Test2data/polarRS_Test2data/CS2/Greenland/'
files = os.listdir(path)
x_list = np.array([]);
y_list = np.array([]);
h_list = np.array([]);
for file in files:
    f = nc.Dataset(path + file,'r')
    var_x = 'x'
    var_y = 'y'
    var_h = 'elevation'
    var_data_x = f.variables[var_x][:]
    var_data_y = f.variables[var_y][:]
    var_data_h = f.variables[var_h][:]
    x_list = np.concatenate((x_list, var_data_x),axis = 0)
    y_list = np.concatenate((y_list, var_data_y),axis = 0)
    h_list = np.concatenate((h_list, var_data_h),axis = 0)
import pandas as pd
df = pd.DataFrame({'x':x_list, 'y':y_list,'h':h_list})
df.to_csv('greenland_points.csv')

你可能感兴趣的:(#,python,python,机器学习,开发语言)