#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''文件说明'''
__author__ = "LQK"
## DateTime
def GetDateArr_days(timeStart, timeEnd):
TIME_Start = datetime.datetime.strptime(timeStart, "%Y-%m-%d")
TIME_End = datetime.datetime.strptime(timeEnd, "%Y-%m-%d")
dateArr = getDayByDay(TIME_Start, TIME_End)
# print dateArr
return dateArr
def GetDateArr_strdays(timeStart, timeEnd, fmt="%Y-%m-%d"):
days = GetDateArr_days(timeStart, timeEnd)
dateArr_str = []
for d in days:
dateArr_str.append(datetime.datetime.strftime(d, fmt))
return dateArr_str
def getDayByDay(timeStart, timeEnd):
oneday = datetime.timedelta(days=1)
timeArr = [timeStart]
while timeArr[len(timeArr) - 1] < timeEnd:
tempday = timeArr[len(timeArr) - 1] + oneday
timeArr.append(tempday)
return timeArr
# Remove space(' ') and indent('\t') at the begin and end of the string
def StripStr(str):
oldStr = ''
newStr = str
while oldStr != newStr:
oldStr = newStr
newStr = oldStr.strip('\t')
newStr = newStr.strip(' ')
return newStr
# Split string by spliter space(' ') and indent('\t') as default
def SplitStr(str, spliters=None):
# spliters = [' ', '\t']
# spliters = []
# if spliter is not None:
# spliters.append(spliter)
if spliters is None:
spliters = [' ', '\t']
destStrs = []
srcStrs = [str]
while True:
oldDestStrs = srcStrs[:]
for s in spliters:
for srcS in srcStrs:
tempStrs = srcS.split(s)
for tempS in tempStrs:
tempS = StripStr(tempS)
if tempS != '':
destStrs.append(tempS)
srcStrs = destStrs[:]
destStrs = []
if oldDestStrs == srcStrs:
destStrs = srcStrs[:]
break
return destStrs
# Write file
def WriteLog(logfile, contentlist, MODE='replace'):
if os.path.exists(logfile):
if MODE == 'replace':
os.remove(logfile)
logStatus = open(logfile, 'w')
else:
logStatus = open(logfile, 'a')
else:
logStatus = open(logfile, 'w')
if isinstance(contentlist, list) or isinstance(contentlist, tuple):
for content in contentlist:
logStatus.write("%s%s" % (content, '\r\n'))
else:
logStatus.write(contentlist)
logStatus.flush()
logStatus.close()
# Create forld
def createForld(forldPath):
if not os.path.isdir(forldPath):
os.makedirs(forldPath)
# Delete file
def DeleteFile(fp):
if os.path.exists(fp):
os.remove(fp)
import os
import math
import numpy
import time
import datetime
# 创建类
class ClimateData:
'''
读取气象站点数据,格式化输出
(下载的原始逐日气象数据)
'''
def __init__(self, dir, dir_out, sid, fields, period, days):
self.dataDir = dir
self.dataDir_out = dir_out
self.sid = sid
self.period = period
self.days = days
self.fieldName = fields
# code:数据类型,ind:数据索引号,frc:真值拉伸系数,ev:异常值阈值,详见气象数据说明文档
self.fieldInfo = {
"TEM": {"code": "12001", "ind": 7, "frc": 0.1, "ev": 30000},
"TMX": {"code": "12001", "ind": 8, "frc": 0.1, "ev": 30000},
"TMN": {"code": "12001", "ind": 9, "frc": 0.1, "ev": 30000},
"PRE": {"code": "13011", "ind": 9, "frc": 0.1, "ev": 30000}, # 7:8-20 8:20-8 9:20-20
"EVP": {"code": "13240", "ind": 7, "frc": 0.1, "ev": 1000},
"RHU": {"code": "13003", "ind": 7, "frc": 1.0, "ev": 300},
"WIN": {"code": "11002", "ind": 7, "frc": 0.1, "ev": 1000},
"SSD": {"code": "14032", "ind": 7, "frc": 0.1, "ev": 99},
"GST": {"code": "12030-0cm", "ind": 7, "frc": 0.1, "ev": 10000},
"PRS": {"code": "10004", "ind": 7, "frc": 0.1, "ev": 20000}
}
# 全部数据
self.data = {}
# 数据日期
self.data_date = {}
# 存储逐年数据
self.data_y = {}
# 待提取日期数据
self.data_d = {}
# 获得日期数组
self.GetDateArr()
for i in self.fieldName:
self.data[i] = []
self.data_date[i] = []
for t in self.years:
self.data_y[t] = {}
for i in self.fieldName:
self.data_y[t][i] = []
for d in self.days:
self.data_d[d] = {}
for i in self.fieldName:
self.data_d[d][i] = 0
def GetDateArr(self):
'''
根据起始日期,获得逐月日期
:return:
'''
self.date = []
self.years = []
startDT_y = int(self.period[0][0:4])
startDT_m = int(self.period[0][4:6])
endDT_y = int(self.period[1][0:4])
endDT_m = int(self.period[1][4:6])
if startDT_y == endDT_y:
self.years.append(startDT_y)
for j in range(startDT_m, endDT_m + 1):
if j > 9:
self.date.append(str(startDT_y) + str(j))
else:
self.date.append(str(startDT_y) + "0" + str(j))
else:
for i in range(startDT_y, endDT_y + 1):
self.years.append(i)
if i == startDT_y:
for j in range(startDT_m, 13):
if j > 9:
self.date.append(str(i) + str(j))
else:
self.date.append(str(i) + "0" + str(j))
elif i < endDT_y:
for j in range(1, 13):
if j > 9:
self.date.append(str(i) + str(j))
else:
self.date.append(str(i) + "0" + str(j))
else:
for j in range(1, endDT_m + 1):
if j > 9:
self.date.append(str(i) + str(j))
else:
self.date.append(str(i) + "0" + str(j))
def ExtractData(self, sr=0):
'''
Extract data
:param sr: start row numbers, default is 0
:return:
'''
print("Data extracting...")
# Get date arr
s_time = time.clock()
# 遍历每个数据类型
for fn in self.fieldName:
# 遍历逐月日期
for dt in self.date:
print(fn, dt)
yr = int(dt[0:4])
# 拼接字符串,组成数据文件路径
field = fn
if fn == "TMN" or fn == "TMX":
field = "TEM"
fileName = self.dataDir + os.sep + "SURF_CLI_CHN_MUL_DAY-" + \
field + "-" + self.fieldInfo[fn]['code'] + "-" + dt + ".TXT"
if not os.path.isfile(fileName):
raise Exception("Can not find %s" % fileName)
else:
txtFile = open(fileName, 'r')
linesList = txtFile.read().split('\n')
iffind = False
hasdata = True
# 逐行遍历数据
for i in range(sr, len(linesList)):
if len(linesList[i]) > 0:
# 将每行数据拆成数组,按照索引提取数据
lineArr = SplitStr(linesList[i], spliters=' ')
if int(lineArr[0]) == self.sid:
iffind = Tru
评论2