Jia-Shun Wang's Ocean Library分享 http://blog.sciencenet.cn/u/muskchina 记录研究生成长过程

博文

MATLAB自动化下载ERSSTv5数据

已有 1829 次阅读 2022-9-21 20:52 |个人分类:技术类|系统分类:科研笔记

此代码可实现自动化下载ERSSTv5数据,这是一款空间分辨率2°,时间跨度可追溯到1854年的SST数据,是计算nino指数的常用数据。

该数据似乎不支持ftp下载,故以此数据为例尝试了一下如何用matlab批量下载nc数据。如果再遇到类似的仅提供网页下载的数据,可尝试以此脚本为参考。

% 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




https://m.sciencenet.cn/blog-3427446-1356331.html

上一篇:普通物理学笔记

1 杨正瓴

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-24 11:17

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部