请选择 进入手机版 | 继续访问电脑版
爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 197|回复: 0

[源代码] 新手尝试用python读取站点降水量并计算月降水量,记录下

[复制链接]

新浪微博达人勋

发表于 3 天前 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 Zhuming 于 2020-11-22 17:17 编辑

# -*- coding: <encoding name> -*-
# Author:Stelliformzm@163.com
# Instructions:Entry level,for reference only
# Description:*.txt,Micaps data,Monthly precipitation is calculated by daily precipitation;multiprocessing
# details:ID,lat,lon,dem,year,mon,day,pre1(8-20h),pre2(20-8h),pre3(20-8h)
import os
import glob
import pandas as pd
from multiprocessing import Pool

# Better for using os.walk()
file_dirs = [
    r'G:\meteorologyData\output\pre_summer\6',r'G:\meteorologyData\output\pre_summer\7',r'G:\meteorologyData\output\pre_summer\8',\
    r'G:\meteorologyData\output\pre_autumn\9',r'G:\meteorologyData\output\pre_autumn\10',r'G:\meteorologyData\output\pre_autumn\11',\
    r'G:\meteorologyData\output\pre_winter\12',r'G:\meteorologyData\output\pre_winter\1',r'G:\meteorologyData\output\pre_winter\2',\
        ]
outfile_dir = r'G:/meteorologyData/output/output'
station = [
    '56038','56079','56144','56146','56152','56167','56172','56173','56178','56182',\
    '56187','56188','56247','56251','56257','56357','56374','56385','56386','56444','56459','56462',\
    '56475','56479','56485','56492','56533','56543','56548','56565','56571','56586','56643','56651',\
    '56664','56666','56671','56684','56691','56739','56748','56751','56778','56792','56793','56856',\
    '56875','56886','56946','56951','56954','56964','56966','56969','56977','56991','57206','57237',\
    '57306','57313','57328','57348','57405','57411','57432','57502','57503','57512','57516','57517',\
    '57520','57523','57536','57606','57608','57612','57625','57633','57647','57707','57710','57718',\
    '57722','57729','57731','57741','57803','57805','57806','57816','57825','57827','57832','57839',\
    '57840','57902','57906','57907','57910','57912','57916','57922','57926','57932','59007'
    ]

def main(O):
    file_list = cycle_file(file_dirs[O])
    # Circular files
    for filename in file_list:
        print(filename[-13:])
        df = read_file(filename)
        # print(df)
        # Circular stations
        for stat in range(len(station)):
            # print(stat)
            # print(station[stat])
            pre = 0.0
            # print(df.shape[0])
            # For Monthly precipitation(sum)
            for i in range(df.shape[0]):
                # print(df.iloc[i,0],station[stat])
                if(df.iloc[i,0] == int(station[stat])):
                    pre = df.iloc[i,9] + pre
                    lat = df.iloc[i,1]/100
                    lon = df.iloc[i,2]/100
            # print(station[stat],lat,lon,pre)   
            
            with open(outfile_dir+'/'+filename[-13:],'a') as f:
                f.write(str(station[stat])+','+str(lat)+','+str(lon)+','+str(pre)+'\n')  
                f.close()
    return

# Reading files
def read_file(filename):
    df = pd.read_csv(filename,header=None,sep=r',')
    return df

# Get File List
def cycle_file(file_dir):
    file_list = []
    for filedir in glob.glob(os.path.join(file_dir,"*.txt")):
        file_list.append(filedir)
    return file_list


if __name__ == '__main__':
    # file_dir = r'G:/meteorologyData/output/pre_summer/6'   
    # main(file_dir,outfile_dir,station)
    pool = Pool(5)   
    for O in range(len(file_dirs)):
        pool.apply_async(main,(O,))

    pool.close()
    pool.join()
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表