# Download the OISST-V2.1 from NOAA # # # Script version 1.4 # # Script works with: # Python version 3.10.13 # # Package version # # # # Ver1.0.0: Created by Mariko Koseki, 04.04.2024 # Ver1.1.0: Updated by Mariko, 16.04.2024 # Ver1.2.0: Updated by Mariko, 03.06.2024 # Ver1.3.0: Updated by Mariko, 29.04.2025 # ver1.4: updated by Mariko, 08.12.2025 ##--- Import modules ---------------------------## import numpy as np import pandas as pd import datetime from datetime import timedelta #from dateutil.relativedelta import relativedelta import os import glob # Ver1.1.0 import sys import platform import calendar import urllib.request import urllib.error print('--- Python version ---') print(platform.python_version()) #print('--- Package version ---') #print('numpy: ', np.__version__) #print('pandas: ', pd.__version__) print('-------------------------') print('') ##--- Input -----------------------------------------## args = sys.argv if len(args) == 2: date_oisst = args[1] if len(date_oisst) == 6: start_year = int(date_oisst[0:4]) start_month = int(date_oisst[4:6]) end_year = start_year end_month = start_month else: print('---How to use this script---') print('python download_oisst.py ') print('or') print('python download_oisst.py ') print(' = start month; = end month') print('') print('Example: python download_oisst.py 202312') print('Example: python download_oisst.py 199501 202312') print('') sys.exit() elif len(args) == 3: date_oisst_start = args[1] date_oisst_end = args[2] if (len(date_oisst_start) == 6) & (len(date_oisst_end) == 6): start_year = int(date_oisst_start[0:4]) start_month = int(date_oisst_start[4:6]) end_year = int(date_oisst_end[0:4]) end_month = int(date_oisst_end[4:6]) else: print('---How to use this script---') print('python download_oisst.py ') print('or') print('python download_oisst.py ') print(' = start month; = end month') print('') print('Example: python download_oisst.py 202312') print('Example: python download_oisst.py 199501 202312') print('') sys.exit() else: print('---How to use this script---') print('python download_oisst.py ') print('or') print('python download_oisst.py ') print(' = start month; = end month') print('') print('Example: python download_oisst.py 202312') print('Example: python download_oisst.py 199501 202312') print('') sys.exit() ##--- Function -------------------------------------## ### Download file ### def download_file(url1, path_dir1): with urllib.request.urlopen(url1) as web_file, open(path_dir1, 'wb') as local_file: local_file.write(web_file.read()) ### Download file and save it ### def download_file_to_dir(url2, path_dir2): download_file(url2, os.path.join(path_dir2, os.path.basename(url2))) ##--- Set version ---------------------------------------## ver1.2.0 ver = 'v2.1' #ver = 'v2' ##--- Set path ------------------------------------------## ver1.2.0, ver1.4 if ver == 'v2.1': url = 'https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/' + ver + '/access/avhrr/' out_dir = '/nird/datapeak/NS9873K/norcpm/validation/observation/NOAA/OISST/original/' + ver + '/' else: url = 'https://www.ncei.noaa.gov/data/sea-surface-temperature-optimum-interpolation/' + ver + '/access/avhrr-only/' out_dir = '/nird/datapkeak/NS9873K/norcpm/validation/observation/NOAA/OISST/original/' + ver + '/' if not os.path.exists(out_dir): os.makedirs(out_dir) ##--- Set year, month ------------------------------------## strdt = datetime.date(start_year, start_month, 1) enddt = datetime.date(end_year, end_month, calendar.monthrange(end_year, end_month)[1]) days_num = (enddt - strdt).days + 1 ymd_str_list = [] for i in range(days_num): ymd_str_list.append(strdt + timedelta(days=i)) print('start date: ', ymd_str_list[0]) print('end date: ', ymd_str_list[-1]) ##--- Download data ----------------------------------------## print('') print('Start downloading data.....') print('') for ymd in ymd_str_list: YEAR = ymd.strftime('%Y') MONTH = ymd.strftime('%m') DATE = ymd.strftime('%d') if ver == 'v2.1': #ver1.2.0 url_oisst = url + YEAR + MONTH + '/oisst-avhrr-v02r01.' + YEAR + MONTH + DATE + '.nc' url_oisst_pre = url + YEAR + MONTH + '/oisst-avhrr-v02r01.' + YEAR + MONTH + DATE + '_preliminary.nc' file_name = 'oisst-avhrr-v02r01.' + YEAR + MONTH + DATE + '.nc' file_name_pre = 'oisst-avhrr-v02r01.' + YEAR + MONTH + DATE + '_preliminary.nc' else: url_oisst = url + YEAR + MONTH + '/avhrr-only-v2.' + YEAR + MONTH + DATE + '.nc' url_oisst_pre = url + YEAR + MONTH + '/avhrr-only-v2.' + YEAR + MONTH + DATE + '_preliminary.nc' file_name = 'avhrr-only-v2.' + YEAR + MONTH + DATE + '.nc' file_name_pre = 'avhrr-only-v2.' + YEAR + MONTH + DATE + '_preliminary.nc' path_to_file = out_dir + file_name path_to_file_pre = out_dir + file_name_pre ##--- Download OISST ------------------------------------## ''' # 1. Check if the data is already downloaded # 2. If not, call function and download the new data # 3. Delete preliminary file if final file exists #ver1.3.0 ''' #---- deleted --------------------------------------------------------------------------------------# #ver1.3.0 ### Remove any existing file ### # Ver1.1.0 #for files in glob.glob(out_dir + 'oisst-avhrr-v02r01.' + YEAR + MONTH + DATE + '*', recursive=True): #if os.path.isfile(files): #os.remove(files) #---- deleted --------------------------------------------------------------------------------------# if os.path.exists(path_to_file): print('file name: ', file_name, 'already exists on NIRD!!') print('') else: print('file name: ', file_name, 'does not exist on NIRD') print('downloadding data...') print('') try: download_file_to_dir(url_oisst, out_dir) print('Downloaded: ', file_name) print('') except urllib.error.HTTPError as httperror: print(httperror) print(file_name, 'does not exist on remote server!!') print('') if os.path.exists(path_to_file_pre): print('file name: ', file_name_pre, 'already exists on NIRD!!') print('') else: print('file name: ', file_name_pre, 'does not exist on NIRD') print('downloadding data...') print('') try: download_file_to_dir(url_oisst_pre, out_dir) print('Downloaded: ', file_name_pre) print('') except urllib.error.HTTPError as httperror: print(httperror) print(file_name_pre, 'does not exist on remote server!!') print('') ### Delete preliminary file if final file exists ### #ver1.3.0 if os.path.exists(path_to_file) and os.path.exists(path_to_file_pre): print('delete preliminary file') os.remove(path_to_file_pre) print('') print('Download completed!')