Python實(shí)現(xiàn)葵花8號(hào)衛(wèi)星數(shù)據(jù)自動(dòng)下載實(shí)例
一:數(shù)據(jù)源介紹
本篇文章介紹的是使用python實(shí)現(xiàn)對(duì)葵花8號(hào)衛(wèi)星數(shù)據(jù)進(jìn)行自動(dòng)下載。
葵花8號(hào)衛(wèi)星是日本的一顆靜止軌道氣象衛(wèi)星,覆蓋范圍為60S-60N, 80E-160W,除了提供十分鐘一幅的原始衛(wèi)星影像外,還提供了如氣溶膠光學(xué)厚度(AOT,也叫AOD)、葉綠素A、海表溫度、云層厚度以及火點(diǎn)等多種產(chǎn)品,這些數(shù)據(jù)都可以進(jìn)行下載。
二:FTP服務(wù)器描述
首先需要在葵花8官網(wǎng)申請(qǐng)帳號(hào)。
可以通過FTP(ftp.ptree.jaxa.jp)使用申請(qǐng)的帳號(hào)密碼訪問文件服務(wù)器,可以看到j(luò)ma文件夾、pub文件夾和兩個(gè)文本文件,其中,jma文件夾和pub文件夾中存放的都是葵花系列衛(wèi)星的影像產(chǎn)品,文本文件的內(nèi)容是每種影像產(chǎn)品的存放位置和數(shù)據(jù)介紹。
三: 程序描述
- 本代碼下載Ftp服務(wù)器如下地址下的文件,如需使用可根據(jù)自己的需要進(jìn)行修改,也可以參考官方txt數(shù)據(jù)介紹文檔尋找自己需要的數(shù)據(jù)的存儲(chǔ)路徑。
- 使用/031目錄是因?yàn)閿?shù)據(jù)最全。
/pub/himawari/L3/ARP/031/
- 本程序有兩個(gè)全局調(diào)試變量。
全局變量 | True | False | 配置變量 |
---|---|---|---|
debugLocalDownload | 下載到本地目錄 | 下載到服務(wù)器指定目錄 | self._save_path |
debugDownloadDaily | 下載當(dāng)前日期前1天的文件 | 下載指定時(shí)間段的文件 | - |
本程序有兩個(gè)版本在debugDownloadDaily=False時(shí)略有區(qū)別
- HimawariDownloadBulitIn的時(shí)間變量寫在程序內(nèi)部,運(yùn)行前需手動(dòng)修改,適用于超算節(jié)點(diǎn)。
- HimawariDownloadCmdLine的時(shí)間變量通過命令行輸入,適用于登陸節(jié)點(diǎn)。
四:注意事項(xiàng)
- 代碼無法直接運(yùn)行 需要將以下行中的帳號(hào)和密碼替換成你申請(qǐng)的賬號(hào)密碼
五:代碼
# -*- codeing = utf-8 -*- # 可以部署在日本的服務(wù)器上,下載速度很快 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 + "/" # 從目標(biāo)路徑ftp_filePath將文件下載至本地路徑dst_filePath dst_filePath = self._nginx_path + self._save_path + self._yearStr + "/" + self._monStr + "/" + self._dayStr + "/" + "hour" + "/" self.deleteFile(dst_filePath) # 先刪除未下載完成的臨時(shí)文件 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 # 刪除目錄下擴(kuò)展名為.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) # 下載單個(gè)文件,LocalFile表示本地存儲(chǔ)路徑和文件名,RemoteFile是FTP路徑和文件名 def DownLoadFile(self, LocalFile, RemoteFile): bufSize = 102400 file_handler = open(LocalFile, 'wb') print(file_handler) # 接收服務(wù)器上文件并寫入本地文件 self.ftp.retrbinary('RETR ' + RemoteFile, file_handler.write, bufSize) self.ftp.set_debuglevel(0) file_handler.close() return True # 下載整個(gè)目錄下的文件,LocalDir表示本地存儲(chǔ)路徑, emoteDir表示FTP路徑 def DownLoadFileTree(self, LocalDir, RemoteDir): # 如果本地不存在該路徑,則創(chuàng)建 if not os.path.exists(LocalDir): os.makedirs(LocalDir) # 獲取FTP路徑下的全部文件名,以列表存儲(chǔ) self.ftp.cwd(RemoteDir) RemoteNames = self.ftp.nlst() RemoteNames.reverse() # print("RemoteNames:", RemoteNames) for file in RemoteNames: # 先下載為臨時(shí)文件Local,下載完成后再改名為nc4格式的文件 # 這是為了防止上一次下載中斷后,最后一個(gè)下載的文件未下載完整,而再開始下載時(shí),程序會(huì)識(shí)別為已經(jīng)下載完成 Local = os.path.join(LocalDir, file[0:-3] + ".temp") files = file[0:-3] + ".nc" LocalNew = os.path.join(LocalDir, files) ''' 下載小時(shí)文件,只下載UTC時(shí)間0時(shí)至24時(shí)(北京時(shí)間0時(shí)至24時(shí))的文件 下載的文件必須是nc格式 若已經(jīng)存在,則跳過下載 ''' # 小時(shí)數(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實(shí)現(xiàn)葵花8號(hào)衛(wèi)星數(shù)據(jù)自動(dòng)下載實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于Python數(shù)據(jù)自動(dòng)下載的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python中浮點(diǎn)數(shù)比較判斷!為什么不能用==(推薦)
這篇文章主要介紹了python中浮點(diǎn)數(shù)比較判斷!為什么不能用==,本文給大家分享問題解決方法,需要的朋友可以參考下2023-09-09Scrapy中詭異xpath的匹配內(nèi)容失效問題及解決
這篇文章主要介紹了Scrapy中詭異xpath的匹配內(nèi)容失效問題及解決方案,具有很好的價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12python openpyxl提取Excel圖片實(shí)現(xiàn)原理技巧
在這篇文章中,將介紹如何使用openpyxl來提取Excel中的圖片,以及它的原理和技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python切片列表字符串如何實(shí)現(xiàn)切換
這篇文章主要介紹了Python切片列表字符串如何實(shí)現(xiàn)切換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Pytorch中的masked_fill基本知識(shí)詳解
本文介紹了PyTorch中masked_fill函數(shù)的基本使用和原理,該函數(shù)接受一個(gè)輸入張量和一個(gè)布爾掩碼作為參數(shù),掩碼的形狀必須與輸入張量相同,True表示需要填充的位置,False表示保持原值2024-10-10