Python实现葵花8号卫星数据自动下载实例
作者:Comtech 发布时间:2021-09-26 13:40:25
标签:Python,自动下载,葵花8号卫星,数据
一:数据源介绍
本篇文章介绍的是使用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


猜你喜欢
- 核心导出作业的 代码 和 作业备份是相似的 代码如下:alter PROC DumpJob (@job VARCHAR(100)
- import httplibimport osimport timedef check_http(i):
- 问题背景用户反馈说当与外部客户端进行 FTP 传输时,可以成功登录,但无法传输任何数据。总之 FTP 传输失败,需要来弄清楚到底发生了什么。
- 最近,就“尊重”一词,个人小有感概。也许跟我说“尊重”一词的同事并不是这么想的,但我反思了一下自己,作为一名设计师,确实存在这些疑问(不足之
- 场景在 Go 语言中,常量分为有类型常量和无类型常量。// 有类型常量const VERSION string = "v1.0.0
- 1、filter_uniquefrom collections import Counterdef filter_unique(lst):
- 在list中嵌套元组,在进行sort排序的时候,产生的是原数组的副本,排序过程中,先根据第一个字段进行从小到大排序,如果第一个字段相同的话,
- <script> window.onload=function(){ upfile('file.php'); }
- 一、ConfigParser简介ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为sectio
- 一、概述OpenCV在V4.5.3版本的contrib包中提供了一个barcode::BarcodeDetector类,用于条形码的识别。二
- 前言:本文为 HITwh 网络空间安全专业网络空间安全设计与实践 I 的选题之一,主要实现了远程监控局域网内的主机桌面与网络情况、简单键鼠控
- 这里使用TensorFlow实现一个简单的卷积神经网络,使用的是MNIST数据集。网络结构为:数据输入层–卷积层1–池化层1–卷积层2–池化
- 用类和对象实现一个银行账户的资金交易管理, 包括存款、取款和打印交易详情, 交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余
- 前言超参调优是“模型调优”(Model Tuning)阶段最主要的工作,是直接影响模型最终效果的关键
- Pytorch中retain_graph的坑在查看SRGAN源码时有如下损失函数,其中设置了retain_graph=True,其作用就是在
- 本文实例为大家分享了微信小程序实现星星评价效果的具体代码,供大家参考,具体内容如下代码实现wxml文件<!--pages/evalua
- 浏览器的具体功能都储存在服务器端的Browscap.ini中:<% SET
- 简介:本文主要介绍在linux系统下,如何配置mysql支持IPV6的连接。环境要求:1、debian7.5操作系统虚拟机2、mysql5.
- pypdf2是一个Python模块,可以用来读取、写入和操作PDF文件。要安装pypdf2模块,请按照以下步骤操作:确保你已经安装了Pyth
- MySQL安全性指南(2) 作 者: 晏子2.1.3 数据库和表权限下列权限运用于数据库和表上的操作。ALTER允许你使用ALTER TAB