hima8_pv/Download_AOD.py

168 lines
5.7 KiB
Python
Raw Permalink Normal View History

2022-11-09 10:16:06 +08:00
import os
import ftplib
import time
# 这个函数用于将日期从整型转为FTP路径所需的字符串
def getDateStr(yearNum, monNum, dayNum):
# 四位数年份
yearStr = str(yearNum)
# 两位数月份
if monNum < 10:
monStr = "0" + str(monNum)
else:
monStr = str(monNum)
# 两位数天
if dayNum < 10:
dayStr = "0" + str(dayNum)
else:
dayStr = str(dayNum)
return yearStr, monStr, dayStr
# 这个函数用于获取前一天的日期号
def getYesterday(year, month, day):
if day == 1:
if month == 1:
year -= 1
month = 12
day = 31
elif month == 2 or month == 4 or month == 6 or month == 8 or month == 9 or month == 11:
month -= 1
day = 31
elif month == 5 or month == 7 or month == 10 or month == 12:
month -= 1
day = 30
elif month == 3:
# 闰年
if year % 4 == 0 and year % 400 == 0:
day = 29
month -= 1
# 闰年
elif year % 4 == 0 and year % 100 != 0:
day = 29
month -= 1
else:
day = 28
month -= 1
else:
day -= 1
return year, month, day
# 获取文件后缀名
def suffix(file, *suffixName):
array = map(file.endswith, suffixName)
if True in array:
return True
else:
return False
2022-11-24 08:54:08 +08:00
# 删除目录下扩展名为.temp的文件temp的文件
def deleteFile(fileDir, suf='.temp'):
2022-11-09 10:16:06 +08:00
targetDir = fileDir
for file in os.listdir(targetDir):
targetFile = os.path.join(targetDir, file)
2022-11-24 08:54:08 +08:00
if suffix(file, suf):
2022-11-09 10:16:06 +08:00
os.remove(targetFile)
class myFTP:
ftp = ftplib.FTP()
# 连接FTPhost是IP地址port是端口默认21
def __init__(self, host, port=21, YesdayNum=1):
self.ftp.connect(host, port)
self._dayNum = YesdayNum
# 登录FTP连接user是用户名password是密码
def Login(self, user, password):
self.ftp.login(user, password)
print(self.ftp.welcome) # 显示登录信息
# 下载单个文件LocalFile表示本地存储路径和文件名RemoteFile是FTP路径和文件名
def DownLoadFile(self, LocalFile, RemoteFile):
bufSize = 102400
file_handler = open(LocalFile, 'wb')
print(file_handler)
# 接收服务器上文件并写入本地文件
self.ftp.retrbinary('RETR ' + RemoteFile, file_handler.write, bufSize)
self.ftp.set_debuglevel(0)
file_handler.close()
return True
# 下载整个目录下的文件LocalDir表示本地存储路径 emoteDir表示FTP路径
def DownLoadFileTree(self, LocalDir, RemoteDir, choice):
# print("remoteDir:", RemoteDir)
# 如果本地不存在该路径,则创建
if not os.path.exists(LocalDir):
os.makedirs(LocalDir)
# 获取FTP路径下的全部文件名以列表存储
# 好像是乱序
self.ftp.cwd(RemoteDir)
RemoteNames = self.ftp.nlst()
RemoteNames.reverse()
# print("RemoteNames", RemoteNames)
for file in RemoteNames:
# 先下载为临时文件Local,下载完成后再改名为nc4格式的文件
# 这是为了防止上一次下载中断后,最后一个下载的文件未下载完整,而再开始下载时,程序会识别为已经下载完成
Local = os.path.join(LocalDir, file[0:-3] + ".temp")
LocalNew = os.path.join(LocalDir, file)
'''
下载小时文件只下载UTC时间1时至10时北京时间9时至18时的文件
下载的文件必须是nc格式
若已经存在则跳过下载
'''
# 小时数据命名格式示例H08_20200819_0700_1HARP030_FLDK.02401_02401.nc
if choice == 1:
if (int(file[13:15]) >= 0 and int(file[13:15]) <= 12) or (int(file[13:15]) >= 21):
if not os.path.exists(LocalNew):
print("Downloading the file of %s" % file)
self.DownLoadFile(Local, file)
os.rename(Local, LocalNew)
print("The download of the file of %s has finished\n" % file)
elif os.path.exists(LocalNew):
print("The file of %s has already existed!\n" % file)
else:
pass
# 天数据命名格式示例H08_20200802_0000_1DARP030_FLDK.02401_02401.nc
elif choice == 2:
if int(file[10:12]) == self._dayNum and not os.path.exists(LocalNew):
print("Downloading the file of %s" % file)
self.DownLoadFile(Local, file)
os.rename(Local, LocalNew)
print("The download of the file of %s has finished\n" % file)
elif int(file[10:12]) == self._dayNum and os.path.exists(LocalNew):
print("The file of %s has already existed!" % file)
elif choice == 3:
# 分钟级别的数据已经明确了小时关系,所以不需要根据小时筛选
if not os.path.exists(LocalNew) and '02401_02401' in file:
print("Downloading the file of %s" % file)
self.DownLoadFile(Local, file)
os.rename(Local, LocalNew)
print("The download of the file of %s has finished\n" % file)
elif os.path.exists(LocalNew):
print("The file of %s has already existed!\n" % file)
else:
pass
self.ftp.cwd("..")
return
def close(self):
self.ftp.quit()