Python實現(xiàn)葵花8號衛(wèi)星數(shù)據(jù)自動下載實例
一:數(shù)據(jù)源介紹
本篇文章介紹的是使用python實現(xiàn)對葵花8號衛(wèi)星數(shù)據(jù)進行自動下載。
葵花8號衛(wèi)星是日本的一顆靜止軌道氣象衛(wèi)星,覆蓋范圍為60S-60N, 80E-160W,除了提供十分鐘一幅的原始衛(wèi)星影像外,還提供了如氣溶膠光學厚度(AOT,也叫AOD)、葉綠素A、海表溫度、云層厚度以及火點等多種產(chǎn)品,這些數(shù)據(jù)都可以進行下載。
二:FTP服務器描述
首先需要在葵花8官網(wǎng)申請帳號。
可以通過FTP(ftp.ptree.jaxa.jp)使用申請的帳號密碼訪問文件服務器,可以看到jma文件夾、pub文件夾和兩個文本文件,其中,jma文件夾和pub文件夾中存放的都是葵花系列衛(wèi)星的影像產(chǎn)品,文本文件的內(nèi)容是每種影像產(chǎn)品的存放位置和數(shù)據(jù)介紹。
三: 程序描述
- 本代碼下載Ftp服務器如下地址下的文件,如需使用可根據(jù)自己的需要進行修改,也可以參考官方txt數(shù)據(jù)介紹文檔尋找自己需要的數(shù)據(jù)的存儲路徑。
- 使用/031目錄是因為數(shù)據(jù)最全。
/pub/himawari/L3/ARP/031/
- 本程序有兩個全局調(diào)試變量。
| 全局變量 | True | False | 配置變量 |
|---|---|---|---|
| debugLocalDownload | 下載到本地目錄 | 下載到服務器指定目錄 | self._save_path |
| debugDownloadDaily | 下載當前日期前1天的文件 | 下載指定時間段的文件 | - |
本程序有兩個版本在debugDownloadDaily=False時略有區(qū)別
- HimawariDownloadBulitIn的時間變量寫在程序內(nèi)部,運行前需手動修改,適用于超算節(jié)點。
- HimawariDownloadCmdLine的時間變量通過命令行輸入,適用于登陸節(jié)點。
四:注意事項
- 代碼無法直接運行 需要將以下行中的帳號和密碼替換成你申請的賬號密碼
五:代碼
# -*- 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):
# 如果本地不存在該路徑,則創(chuàng)建
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格式的文件
# 這是為了防止上一次下載中斷后,最后一個下載的文件未下載完整,而再開始下載時,程序會識別為已經(jīng)下載完成
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格式
若已經(jīng)存在,則跳過下載
'''
# 小時數(shù)據(jù)命名格式示例: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以上就是Python實現(xiàn)葵花8號衛(wèi)星數(shù)據(jù)自動下載實例的詳細內(nèi)容,更多關于Python數(shù)據(jù)自動下載的資料請關注腳本之家其它相關文章!
相關文章
TensorFlow命名空間和TensorBoard圖節(jié)點實例
今天小編就為大家分享一篇TensorFlow命名空間和TensorBoard圖節(jié)點實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

