此代码可实现自动化下载ERSSTv5数据,这是一款空间分辨率2°,时间跨度可追溯到1854年的SST数据,是计算nino指数的常用数据。
该数据似乎不支持ftp下载,故以此数据为例尝试了一下如何用matlab批量下载nc数据。如果再遇到类似的仅提供网页下载的数据,可尝试以此脚本为参考。
blibili代码块不支持MATLAB,用的是python的高亮显示。
% automated download ERSSTv5
% Data path: https://www.ncei.noaa.gov/products/extended-reconstructed-sst
%
% Jia-Shun Wang
% wjs@outlook.at
% 2022/09/21
%
% Reference:
% Huang, B., Thorne, P.W., Banzon, V.F., Boyer, T., Chepurin, G.,
% Lawrimore, J.H., Menne, M.J., Smith, T.M., Vose, R.S., Zhang, H.-M.,
% 2017. Extended Reconstructed Sea Surface Temperature,
% Version 5 (ERSSTv5): Upgrades, Validations, and Intercomparisons.
% Journal of Climate 30, 8179–8205.https://doi.org/10.1175/JCLI-D-16-0836.1
clc;clear;close
%% setting
prefix='ersst.v5.';
suffix='.nc';
mkdir ../data ERSSTv5 %Create the output first
output_path='data/ERSSTv5/';
input_path='https://www.ncei.noaa.gov/pub/data/cmb/ersst/v5/netcdf/';
start_year=double(string(datetime(1854,01,01,'Format','yyyy')));%1854.01
end_date=datetime('today');
this_year=double(string(datetime('today','Format','yyyy')));
this_month=double(string(datetime('today','Format','MM')));%ignore this month
h=waitbar(0,'please wait');
end_year=this_year;% test
%% judge
if end_year==this_year
bd=1;
else
bd=0;
end
total=(end_year-start_year+1-bd)*12+bd*this_month-1;%ignore this month
%% implement
tic
for y=start_year:end_year
if y~=this_year
for m=1:12
filename=[prefix,char(datetime(y,m,1,'Format','yyyyMM')),suffix];
url=[input_path,filename];
websave([output_path,filename],url);% save it to the local disk.
str=['Downloading...',num2str(((y-start_year)*12+m)/...
total*100,'%4.2f'),'%'];
waitbar(((y-start_year)*12+m)/total,h,str)
end
else
for m=1:this_month-1 %ignore this month
filename=[prefix,char(datetime(y,m,1,'Format','yyyyMM')),suffix];
url=[input_path,filename];
websave([output_path,filename],url);% save it to the local disk.
str=['Downloading...',num2str(((y-start_year)*12+m)/...
total*100,'%4.2f'),'%'];
waitbar(((y-start_year)*12+m)/total,h,str)
end
end
end
fprintf(2,'Awesome!\n You have download all data.\n')
toc