pkl⽂件的读取、写⼊与合并
pkl⽂件的读取、写⼊与合并
1.pkl⽂件的读取
import pickle
inf = pickle.load(open('D:\\projects\\ncData\\weekly_average.pkl',"rb+"))
print(inf.keys())#查看pkl⽂件中的键值
print(inf)#读取数据
for var in inf.keys():
data3=inf[var][:]
print(var,data3.shape)
#查看各键值对应的shape
2.pkl⽂件的写⼊
import pickle
with open('D:\\projects\\ncData\\test_sst_u_v_wind_u_v_current_pre_weekly_average_20190101-20201228.pkl',"rb+")as f2:
data_daily_inference2=pickle.ad())
for i in range(0,100):
data_daily_inference2['SST'][i]= data_daily_inference2['SST'][i]-273.15# 选择key为'SST'的⼀维数组进⾏更改,开尔⽂转换成摄⽒度 f2.write(data_daily_inference2['SST'][i])# 将转换完的SST写⼊原⽂件
f2.close()#关闭⽂档
print(data_daily_inference2['SST'])#查看转换后的数据
2.pkl⽂件的合并
import pickle
import numpy as np
import pandas as pd
with open("D:\\projects\\ncData\\weekly1.pkl",'rb+')as f:
a = pickle.load(f)#待合并的pkl⽂件a
with open("D:\\projects\\ncData\\weekly2.pkl",'rb+')as f:
b = pickle.load(f)#待合并的pkl⽂件b
#根据所需数组维度和空间使⽤s建⽴好zero数组,本案例中有7个key
#a⽂件各数组shape为[104,20,100;;b⽂件各数组shape为[2087,20,100];故合并后的新⽂件数组shape为[2191,20,100]
dates_weekly = pd.date_range(start='1979-01-01', end='2020-12-31', freq="w")
m_tem = np.zeros((2191,20,100), dtype=np.float32)
m_P_SURF = np.zeros((2191,20,100), dtype=np.float32)
m_U_WIND = np.zeros((2191,20,100), dtype=np.float32)
m_V_WIND = np.zeros((2191,20,100), dtype=np.float32)
m_U_CURRENT = np.zeros((2191,20,100), dtype=np.float32)
m_V_CURRENT = np.zeros((2191,20,100), dtype=np.float32)
#使⽤numpy.append对同维度数组进⾏延伸,本案例将a中变量加到b⽂件中,键值⼀⼀对应保持不变,沿x轴进⾏延伸
m_tem = np.append(b['SST'], a['SST'], axis=0)
import pickle
m_P_SURF = np.append(b['P_SURF'], a['P_SURF'], axis=0)
m_U_WIND = np.append(b['U_WIND'], a['U_WIND'], axis=0)
m_V_WIND = np.append(b['V_WIND'], a['V_WIND'], axis=0)
m_U_CURRENT = np.append(b['U_CURRENT'], a['U_CURRENT'], axis=0)
m_V_CURRENT = np.append(b['V_CURRENT'], a['V_CURRENT'], axis=0)
dataset_weekly ={"LAT": a['LAT'],"LON": a['LON'],"DATE": dates_weekly,"SST": m_tem,"U_WIND": m_U_WIND,"V_WIND": m_V_WIND, "U_CURRENT": m_U_CURRENT,"V_CURRENT": m_V_CURRENT,"P_SURF": m_P_SURF}
with open("D:\\projects\\ncData\\concat.pkl",
'wb')as f:
pickle.dump(dataset_weekly, f, protocol=4)#写⼊新的pkl⽂件
f.close()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论