Python实现葵花8号卫星数据自动下载实例
作者:Comtech 时间:2021-09-26 13:40:25
一:数据源介绍
本篇文章介绍的是使用python实现对葵花8号卫星数据进行自动下载。
葵花8号卫星是日本的一颗静止轨道气象卫星,覆盖范围为60S-60N, 80E-160W,除了提供十分钟一幅的原始卫星影像外,还提供了如气溶胶光学厚度(AOT,也叫AOD)、叶绿素A、海表温度、云层厚度以及火点等多种产品,这些数据都可以进行下载。
二:FTP服务器描述
首先需要在葵花8官网申请帐号。
可以通过FTP(ftp.ptree.jaxa.jp)使用申请的帐号密码访问文件服务器,可以看到jma文件夹、pub文件夹和两个文本文件,其中,jma文件夹和pub文件夹中存放的都是葵花系列卫星的影像产品,文本文件的内容是每种影像产品的存放位置和数据介绍。
三: 程序描述
本代码下载Ftp服务器如下地址下的文件,如需使用可根据自己的需要进行修改,也可以参考官方txt数据介绍文档寻找自己需要的数据的存储路径。
使用/031目录是因为数据最全。
/pub/himawari/L3/ARP/031/
本程序有两个全局调试变量。
全局变量 | True | False | 配置变量 |
---|---|---|---|
debugLocalDownload | 下载到本地目录 | 下载到服务器指定目录 | self._save_path |
debugDownloadDaily | 下载当前日期前1天的文件 | 下载指定时间段的文件 | - |
本程序有两个版本在debugDownloadDaily=False时略有区别
HimawariDownloadBulitIn的时间变量写在程序内部,运行前需手动修改,适用于超算节点。
HimawariDownloadCmdLine的时间变量通过命令行输入,适用于登陆节点。
四:注意事项
代码无法直接运行 需要将以下行中的帐号和密码替换成你申请的账号密码
五:代码
# -*- codeing = utf-8 -*-
# 可以部署在日本的服务器上,下载速度很快
import ftplib
import json
import os
import time
import numpy as np
debugLocalDownload = True
debugDownloadDaily = False
globPersonalTime = [2022, 9, 7]
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
np.int16, np.int32, np.int64, np.uint8,
np.uint16, np.uint32, np.uint64)):
return int(obj)
elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
return float(obj)
elif isinstance(obj, (np.ndarray,)):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
class himawari:
ftp = ftplib.FTP()
def __init__(self):
self._url = '/pub/himawari/L3/ARP/031/'
self._save_path = './Your_save_path'
if debugLocalDownload:
self._save_path = './Download/'
self.ftp.connect('ftp.ptree.jaxa.jp', 21)
self.ftp.login('YourFTPAccount', 'YourFTPPassWord')
self._yearNum, self._monNum, self._dayNum = self.dayInit()
self._nginx_path = ''
print(self.ftp.welcome) # 显示登录信息
def run(self):
self._nginx_path = ''
try:
if debugDownloadDaily:
self._yearNum, self._monNum, self._dayNum = self.getYesterday(self._yearNum, self._monNum, self._dayNum)
else:
self._yearNum = globPersonalTime[0]
self._monNum = globPersonalTime[1]
self._dayNum = globPersonalTime[2]
self._yearStr, self._monStr, self._dayStr = self.getDateStr(self._yearNum, self._monNum, self._dayNum)
ftp_filePath = self._url + self._yearStr + self._monStr + "/" + self._dayStr + "/"
# 从目标路径ftp_filePath将文件下载至本地路径dst_filePath
dst_filePath = self._nginx_path + self._save_path + self._yearStr + "/" + self._monStr + "/" + self._dayStr + "/" + "hour" + "/"
self.deleteFile(dst_filePath) # 先删除未下载完成的临时文件
print("Local:" + dst_filePath)
print("Remote:" + ftp_filePath)
self.DownLoadFileTree(dst_filePath, ftp_filePath)
if debugDownloadDaily:
self.ftp.quit()
except Exception as err:
print(err)
def getYesterday(self, yy, mm, dd):
dt = (yy, mm, dd, 9, 0, 0, 0, 0, 0)
dt = time.mktime(dt) - 86400
yesterdayList = time.strftime("%Y-%m-%d", time.localtime(dt)).split('-')
return int(yesterdayList[0]), int(yesterdayList[1]), int(yesterdayList[2])
def dayInit(self, ):
yesterdayList = time.strftime("%Y-%m-%d", time.localtime(time.time())).split('-')
return int(yesterdayList[0]), int(yesterdayList[1]), int(yesterdayList[2])
def getDateStr(self, yy, mm, dd):
syy = str(yy)
smm = str(mm)
sdd = str(dd)
if mm < 10:
smm = '0' + smm
if dd < 10:
sdd = '0' + sdd
return syy, smm, sdd
# 删除目录下扩展名为.temp的文件
def deleteFile(self, fileDir):
if os.path.isdir(fileDir):
targetDir = fileDir
for file in os.listdir(targetDir):
targetFile = os.path.join(targetDir, file)
if targetFile.endswith('.temp'):
os.remove(targetFile)
# 下载单个文件,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):
# 如果本地不存在该路径,则创建
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")
files = file[0:-3] + ".nc"
LocalNew = os.path.join(LocalDir, files)
'''
下载小时文件,只下载UTC时间0时至24时(北京时间0时至24时)的文件
下载的文件必须是nc格式
若已经存在,则跳过下载
'''
# 小时数据命名格式示例:H08_20200819_0700_1HARP030_FLDK.02401_02401.nc
if int(file[13:15]) >= 0 and int(file[13:15]) <= 24:
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)
#print("png of the file of %s has finished\n" % png_name)
elif os.path.exists(LocalNew):
print("The file of %s has already existed!\n" % file)
self.ftp.cwd("..")
return
# 主程序
myftp = himawari()
if debugDownloadDaily:
myftp.run()
else:
yyStart, mmStart, ddStart = input("Start(yy mm dd):").split()
yyStart, mmStart, ddStart = int(yyStart), int(mmStart), int(ddStart)
yyEnd, mmEnd, ddEnd = input("End(yy mm dd):").split()
yyEnd, mmEnd, ddEnd = int(yyEnd), int(mmEnd), int(ddEnd)
dtStart = (yyStart, mmStart, ddStart, 9, 0, 0, 0, 0, 0)
dtEnd = (yyEnd, mmEnd, ddEnd, 10, 0, 0, 0, 0, 0)
timeIndex = time.mktime(dtStart)
timeIndexEnd = time.mktime(dtEnd)
while timeIndex < timeIndexEnd:
indexDayList = time.strftime("%Y-%m-%d", time.localtime(timeIndex)).split('-')
globPersonalTime[0] = int(indexDayList[0])
globPersonalTime[1] = int(indexDayList[1])
globPersonalTime[2] = int(indexDayList[2])
print(globPersonalTime)
myftp.run()
timeIndex = int(timeIndex) + 3600 * 24
来源:https://juejin.cn/post/7153456118591651876
标签:Python,自动下载,葵花8号卫星,数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python + Requests + Unittest接口自动化测试实例分析
2021-08-08 05:41:47
![](https://img.aspxhome.com/file/2023/2/65122_0s.png)
PHP使用redis实现分布式锁的示例详解
2023-06-01 16:32:19
Ajax+PHP边学边练 之五 图片处理
2023-11-18 07:21:29
![](https://img.aspxhome.com/file/2023/7/102087_0s.png)
mysql查询一天,查询一周,查询一个月的数据
2011-01-29 16:22:00
微信小程序地图(map)组件点击(tap)获取经纬度的方法
2023-09-03 21:33:37
![](https://img.aspxhome.com/file/2023/6/56196_0s.gif)
图标设计常犯的10种错误
2008-03-06 13:40:00
![](https://img.aspxhome.com/file/UploadPic/20083/6/200836134649762s.jpg)
用Dreamweaver实现飘浮光球特效
2008-03-03 12:28:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113720922s.jpg)
用mysql做站点时怎样记录未知错误的发生
2009-01-14 13:16:00
JS逆序遍历实现代码
2023-10-11 07:31:49
详解Python中for循环的使用
2023-06-13 13:55:40
.NET中书写XML的一种简单方法
2007-08-23 12:55:00
XMLHTTP自动判断远程网页字符编码
2007-12-28 13:41:00
JavaScript缓动动画函数的封装方法
2023-08-07 10:48:26
xmlhttp中运行getResponseHeader出错,提示:The requested header was not found
2010-03-27 21:47:00
ASP分页显示Recordset数据
2008-11-07 15:43:00
asp被杀毒软件误删的解决方法
2011-04-11 11:16:00
在函数间不能传递32个以上参数的疑难问题
2008-12-31 13:31:00
Go标准容器之Ring的使用说明
2023-09-21 02:18:14
一个asp伪静态的程序实现方法
2010-06-28 18:56:00
设计的俗化特征
2010-04-08 16:07:00
![](https://img.aspxhome.com/file/UploadPic/20104/8/01-49s.jpg)