NOAA气象日监测数据均值计算python代码整理
- 氣象監(jiān)測(cè)數(shù)據(jù)下載(可下載最新及每日氣象數(shù)據(jù))
- NOAA氣象日監(jiān)測(cè)數(shù)據(jù)均值計(jì)算python代碼整理
- PostgreSQL數(shù)據(jù)庫(kù)導(dǎo)入EXCEL數(shù)據(jù)表
之前已經(jīng)介紹了如何下載氣象的每日監(jiān)測(cè)數(shù)據(jù),這里就整理介紹下如何基于這些數(shù)據(jù),計(jì)算月均值的數(shù)據(jù)。
Python版本為3.7.
1. 解壓并篩選數(shù)據(jù)
這里下載了十年的某幾個(gè)站點(diǎn)的監(jiān)測(cè)數(shù)據(jù),其中stations.csv為站點(diǎn)數(shù)據(jù)。
解壓縮后的為大量站點(diǎn)的年度數(shù)據(jù),所以需要先篩選出對(duì)應(yīng)站點(diǎn)的數(shù)據(jù)。
參數(shù)備注:files為station中的站點(diǎn)編號(hào);2011處為年份命名文件夾,這里手動(dòng)修改,沒寫循環(huán)。
篩選并轉(zhuǎn)儲(chǔ)到后續(xù)處理的文件夾,代碼如下:
import glob import shutil import sys import osoutdirectory = 'D:\Codes\pythontest\yxdl\data\\2011' file_names = glob.glob('F:\數(shù)據(jù)1\二維數(shù)據(jù)\downdata\dis\\2011\*.csv')files = ['42398099999','44478099999','42403099999','42295099999','44477099999', '42404099999','42406099999','41863099999','42299099999','42399099999','42423099999', '41862099999','55773099999','43399099999','41862199999','41862199999','41859099999', '41858099999','44461099999','44474099999','41850099999','41852099999']#判斷文件夾是否存在并創(chuàng)建 if os.path.exists(outdirectory):print('%s:存在'%outdirectory) else:os.mkdir(outdirectory)print('新建文件夾:%s'%outdirectory)#file = for i in range(0,len(files)):print(files[i])for file_name in file_names:filetemp = str.split(file_name,'\\')[-1]if (files[i]+'.csv')==filetemp:print('copying')shutil.copy(file_name, outdirectory)print("finish")處理完的目錄結(jié)構(gòu):
2. 計(jì)算月均值
然后通過pandas計(jì)算月均值。
參數(shù)備注:這里的2011也需要手動(dòng)改。
計(jì)算并存儲(chǔ)到均值計(jì)算目錄,代碼如下:
import pandas as pd import glob import shutil import sysoutdirectory = 'D:\Codes\pythontest\yxdl\data\mean\\2011' file_names = glob.glob('D:\Codes\pythontest\yxdl\data\\2011\*.csv') time = 2011 #csvfile ="D:\Codes\pythontest\yxdl\data\\2021\\55773099999.csv"import os #判斷文件夾是否存在并創(chuàng)建 if os.path.exists(outdirectory):print('%s:存在'%outdirectory) else:os.mkdir(outdirectory)print('新建文件夾:%s'%outdirectory)for csvfile in file_names:df = pd.read_csv(csvfile)#df = pd.DataFrame(file)# print(df.head())df['DATE'] = pd.to_datetime(df['DATE'],format='%Y-%m-%d')#print(df.head())#將時(shí)間作為索引df = df.set_index('DATE')#保留一位小數(shù)format1=lambda x:"%.1f"%x#按月進(jìn)行平均值統(tǒng)計(jì)dfM=df.resample('M').mean()dfM[['TEMP','MAX','MIN']]=dfM[['TEMP','MAX','MIN']].applymap(format1)#保留一位小數(shù)#保存月平均數(shù)據(jù)filetemp = str.split(csvfile,'\\')[-1]filetemp = str.split(filetemp,'.')[0]print(filetemp)outfile = outdirectory + '\\'+filetemp +'_'+str(time)+ '_mean.csv'dfM.to_csv(outfile,encoding='gbk')處理完的目錄結(jié)構(gòu):
3. 按站點(diǎn)進(jìn)行csv數(shù)據(jù)合并
先手動(dòng)新建站點(diǎn)合并文件夾hebing。
根據(jù)站點(diǎn)編號(hào),在hebing文件夾下創(chuàng)建對(duì)應(yīng)的文件夾。合并代碼如下:
import pandas as pd import os import globfindpath = 'D:\Codes\pythontest\yxdl\data\hebing'files = ['42398099999','44478099999','42403099999','42295099999','44477099999', '42404099999','42406099999','41863099999','42299099999','42399099999','42423099999', '41862099999','55773099999','43399099999','41862199999','41862199999','41859099999', '41858099999','44461099999','44474099999','41850099999','41852099999']for filename in files:directory = findpath+ '\\'+filename#判斷文件夾是否存在并創(chuàng)建if os.path.exists(directory):print('%s:存在'%directory)else:os.mkdir(directory)print('新建文件夾:%s'%directory)處理完的目錄結(jié)構(gòu):
然后復(fù)制移動(dòng)均值數(shù)據(jù),到剛剛在hebing下創(chuàng)建的各個(gè)站點(diǎn)文件夾目錄下:
參數(shù)備注:這里的filefind需要手動(dòng)改。
代碼如下:
import glob import shutil import sys import osoutdirectory = 'D:\Codes\pythontest\yxdl\data\hebing' file_names = glob.glob('D:\Codes\pythontest\yxdl\data\mean\*\*.csv')filefind = '55773099999'for filename in file_names:filetemp = str.split(filename,'\\')[-1]filetemp = str.split(filetemp,'.')[0]filetemp = str.split(filetemp,'_')[0]dtemp=outdirectory+'\\'+filetempprint(filetemp)if filefind == filetemp:print('copying')shutil.copy(filename, dtemp)print("finish")移動(dòng)完成后,即可合并每個(gè)站點(diǎn)目錄下的每年月均值csv文件。
參數(shù)備注:兩個(gè)編號(hào)處手動(dòng)改。
代碼如下:
import pandas as pd import os import glob#filepath = 'D:\Codes\pythontest\yxdl\data\hebing\\42398099999' file_names = glob.glob('D:\Codes\pythontest\yxdl\data\hebing\\44474099999\*.csv') outdirectory = 'D:\Codes\pythontest\yxdl\data\hebing\\44474099999\hebingout'import os #判斷文件夾是否存在并創(chuàng)建 if os.path.exists(outdirectory):print('%s:存在'%outdirectory) else:os.mkdir(outdirectory)print('新建文件夾:%s'%outdirectory)for inputfile in file_names:df = pd.read_csv(inputfile)print(1111)filetemp = str.split(inputfile,'\\')[-1]filetemp = str.split(filetemp,'.')[0]filetemp = str.split(filetemp,'_')[0]print(filetemp)outfile = outdirectory + '\\'+filetemp + '_hb.csv'df.to_csv(outfile, mode='a',index=False, header=False)然后手動(dòng)將每個(gè)站點(diǎn)合并好的數(shù)據(jù)拷貝到zong文件夾下,再合并一次。
這部分合并代碼如下:
OK,然后到這里數(shù)據(jù)就處理得差不多了。就可以導(dǎo)入數(shù)據(jù)庫(kù)了。具體參考博客PostgreSQL數(shù)據(jù)庫(kù)導(dǎo)入EXCEL數(shù)據(jù)表。
總結(jié)
以上是生活随笔為你收集整理的NOAA气象日监测数据均值计算python代码整理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信支付—微信H5支付「微信内部浏览器」
- 下一篇: Python2和Python3的兼容性写