clear;close all;clc;clear memory;warning('off');tic; % MATLAB16b
%=================================================================================================================================================%
addpath(genpath('/home/mwpark/99__Utill/matFVCOM')); % add function path
%=================================================================================================================================================%
% inp.par = parcluster; % parallel information (disabled: hostname parse error on Linux)
inp.char_set = 'UTF8';feature('DefaultCharacterSet',inp.char_set);slCharacterEncoding(inp.char_set);slCharacterEncoding
%=================================================================================================================================================%
set(groot,'defaultFigureVisible','off'); % figure pop-up control
%=================================================================================================================================================%
inp.time0 = datestr(clock,'yyyy/mm/dd HH:MM:SS'); % current time (clock)
disp(['- Start time: ',inp.time0]); % Start time (yyyy/mm/dd HH:MM:SS)
%=================================================================================================================================================%
%% KMA_LDAPS_download__v6_linux.m (made by khkim)
% - updated in 2026/04/21 MWPark
% - v5: typ06/url/nwp_vars_down.php (GRIB2 direct download) --> 2026-03-31 service ended
% - v6: typ02/openApi/NwpModelInfoService/getLdapsUnisAll (new OpenAPI)
% - dataType=GRIB option retained to keep .gb2 output format
%% user control
% data file url format --> LDAPS new OpenAPI
% 1. pagination (new API parameter)
inp.pageNo = '1';
inp.numOfRows = '999999'; % set large enough to retrieve full grid in one request
% 2. reference date (yyyymmddHHMM - UTC)
%inp.tmfc = datenum(2026,04,01,00,00,00):datenum(0000,00,00,-03,00,00):datenum(2026,03,31,00,00,00);
inp.tmfc = datenum(2024,10,01,00,00,00):datenum(0000,00,00,03,00,00):datenum(2024,11,01,00,00,00);
% remake to string
for ii = 1 : length(inp.tmfc)
inp.tmfc_str{ii} = datestr(inp.tmfc(ii),'yyyymmddHH'); %datestr(inp.tmfc(ii),'yyyymmddHHMM');
end
% 3. authKey (인증키)
%
%
%
%
inp.authkey{1} = 'DCjgj-qLTUuo4I_qi81LTw'; % mwpark O
inp.authkey{2} = '719TYCAQQpSfU2AgEDKUig'; % yjsong O
inp.authkey{3} = 'mF5JvEuyS6SeSbxLstukbg'; % kmlee O
inp.authkey{4} = 'VtEjY0WWRsWRI2NFlhbFwQ'; % ysjung O
inp.authkey{5} = 'Ff3JsBVyTV29ybAVcp1dcA'; % mgchoi O
inp.authkey{6} = 'uZGj1QSKTDyRo9UEijw88g'; % hwdo O
inp.authkey{7} = 'BLkL6glMT6K5C-oJTM-icg'; % hmlee O
inp.authkey{8} = 'Z2Y62u6NR_-mOtrujcf_Lw'; % khkim O
inp.authkey{9} = 'XXZ9ixDbT522fYsQ2y-dfA'; % mhkim O
% 4. leadHour (예측시간) - 기준 시각에서부터의 예측시간 (구 ef)
inp.leadHour = { '0' '1' '2' };
%inp.leadHour = { '0' };
% 5. vars (변수 선택) - new API dataTypeCd codes
%inp.vars = {'ugrd','vgrd','prms',... % 10m u, 10m v, prms
% 'tdsw','ndnl','hfsf','lhtf'... % about heat flux
% 'tmpr',... % 2m temperature
% 'rhwt',... % relative humidity
% 'tcar',... % total cloud amount - random overlap
% 'ncpc'}; % large-scale precipitation
inp.vars = {'ugrd','vgrd','prms'};
% output directory
%dat.out_dir = '/dataS/LDAPS/2026';
dat.out_dir = '/home/fvcom_sed/OBS/01__GGB/02__Model_data/LDAPS/2024/tp';
if exist(fullfile(dat.out_dir),'dir') == 0
mkdir(fullfile(dat.out_dir));
end
dat.out_fname = 'ldaps_unis';
%% make download url
hh = 1;
kk = 1; % input auth key index
% loop: for length of inp.tmfc
for ii = 1 : length(inp.tmfc_str)
% loop: for length of vars
for qq = 1 : length(inp.vars)
% loop: for length of leadHour
jj = 1;
while jj <= length(inp.leadHour)
try
if hh > 2000
kk = kk + 1;
hh = 1;
end
% URL (new OpenAPI / dataType=GRIB to retain .gb2 format)
%tp.url{ii,jj} = [inp.def, ...
% 'pageNo=',inp.pageNo, ...
% '&numOfRows=',inp.numOfRows, ...
% '&dataType=GRIB', ...
% '&baseTime=',inp.tmfc_str{ii}, ...
% '&leadHour=',inp.leadHour{jj}, ...
% '&dataTypeCd=',inp.vars{qq}, ...
% '&authKey=',inp.authkey{kk}];
tp.url{ii,jj} = [inp.def, ...
'nwp=','l015', ...
'&sub=','unis', ...
'&vars=',inp.vars{qq}, ...
'&pres=','1000', ...
'&tmfc=',inp.tmfc_str{ii}, ...
'&ef=',inp.leadHour{jj}, ...
'&dataType=','GRIB', ...
'&authKey=',inp.authkey{kk}];
% output filename (.gb2)
tp.out_fname{ii,jj} = [dat.out_fname,'_lh',inp.leadHour{jj},'.',inp.tmfc_str{ii},'_',inp.vars{qq},'.gb2'];
% download option (linux)
tp.options = weboptions('Timeout',20,'ArrayFormat','php','CertificateFilename','');
% if file does not exist
if exist(fullfile(dat.out_dir,tp.out_fname{ii,jj})) ~= 2
disp(['(',num2str(hh),')...downloading....',tp.out_fname{ii,jj}]);
websave([dat.out_dir,'/',tp.out_fname{ii,jj}],tp.url{ii,jj},tp.options);
hh = hh + 1;
elseif exist(fullfile(dat.out_dir,tp.out_fname{ii,jj})) == 2
tp.dir = dir(fullfile(dat.out_dir,tp.out_fname{ii,jj}));
if tp.dir.bytes < 100
delete(fullfile(dat.out_dir,tp.out_fname{ii,jj}));
disp(['(',num2str(hh),')...re-downloading....',tp.out_fname{ii,jj}]);
websave([dat.out_dir,'/',tp.out_fname{ii,jj}],tp.url{ii,jj},tp.options);
hh = hh + 1;
end
end
jj = jj + 1;
catch
disp(['(',num2str(hh),')...retrying...',tp.out_fname{ii,jj}]);
hh = hh + 1;
end
end
end
end
%=================================================================================================================================================%
inp.elapsed_time = toc; % elaped time (sec)
inp.time1 = datestr(clock,'yyyy/mm/dd HH:MM:SS'); % finished time (clock)
disp(['- End time: ',inp.time1]); % end time (yyyy/mm/dd HH:MM:SS)
disp(['- Elapsed time: ',num2str(round(inp.elapsed_time/60)),' min']); % elaped time (min)
%=================================================================================================================================================%