데이터 처리

[open api] 1년치 조위 그래프 자동 생성

humming_stereo 2023. 3. 13. 10:26

참고글

https://hummingstereo.tistory.com/34

 

[open api] 바다누리 해양정보 서비스

1. 바다누리 해양정보 서비스 회원가입/로그인 http://www.khoa.go.kr/oceangrid/khoa/intro.do 바다누리 해양정보 서비스 조위관측소, 해양관측부이, 해양과학기지 등에서 관측된 해양 정보를 다운로드하여

hummingstereo.tistory.com


발단

1. 조위관측소의 데이터 가시화를 하고 싶다.

2. 1년치 데이터가 필요하지만 일일이 다운받기 귀찮다.

3. open api로 데이터를 받아 자동으로 그래프 생성 프로그램을 만들어보자.


입력 값

1. 개인 인증 키

2. 기간

3. 관측소


처리 과정

1. open api를 통해 데이터 요청

2. 결과 값을 시간, 조위값 matrix 형식으로 가공

3. 각 데이터를 월별로 분리

4. 그래프 가시화 or 저장

 


clc; clear; close all;
 
%% input
inp.key = 'personal key';
inp.dtype = 'tideObs';
inp.code = 'DT_0001';
inp.duration = {'20220101' '20221231'};
inp.rtype = 'json';
inp.out = '02_figure';
output = fullfile(pwd,inp.out);
if isfolder(output) == 0
mkdir(output)
end
 
%% Pretreatment
tp.duration.s = datenum(inp.duration{1},'yyyymmdd');
tp.duration.e = datenum(inp.duration{end},'yyyymmdd');
tp.duration.dnum = (tp.duration.s(1):tp.duration.e(end))';
tp.duration.dvec = datevec(tp.duration.dnum);
tp.duration.str = strcat(num2str(tp.duration.dvec(:,1)),num2str(tp.duration.dvec(:,2:3),'%02d'));
dat.m_list = unique(str2num(tp.duration.str(:,1:6)));
dat.m_list = num2str(dat.m_list);
 
 
%% download api & process data
tp.pcs.d3=[];
for tt = 1:height(tp.duration.str)
tp.url = ['http://www.khoa.go.kr/api/oceangrid/tideObs/search.do?' ...
'ServiceKey=',inp.key,'&' ...
'ObsCode=',inp.code,'&' ...
'Date=',tp.duration.str(tt,:),'&' ...
'ResultType=',inp.rtype];
tp.o = webread(tp.url,'options','json');
% process
tp.pcs.d1 = strsplit(tp.o(21:end-136),'},{')';
for ii = 1:length(tp.pcs.d1)
tp.pcs.d2(ii,:) = strsplit(tp.pcs.d1{ii}(),'"');
end
tp.pcs.d3 = vertcat(tp.pcs.d3,tp.pcs.d2);
end
tp.pcs.d4(:,1) = tp.pcs.d3(:,8);
tp.pcs.d4(:,2) = tp.pcs.d3(:,4);
% all data
dat.dnum = datenum(tp.pcs.d4(:,1));
dat.lev = cellfun(@str2num,tp.pcs.d4(:,2))/100-mean(cellfun(@str2num,tp.pcs.d4(:,2))/100);
 
 
%% figure
for ii = str2double(dat.m_list(1,5:6)):str2double(dat.m_list(end,5:6))
% split month
fig.dnum = dat.dnum(month(dat.dnum) == ii);
fig.lev = dat.lev(month(dat.dnum) == ii);
fig.month = datevec(fig.dnum(1));
% set
figure('Visible','on')
set(gca,'fontname','Arial','fontweight','bold','fontsize',15,'Box','on','Layer','top');
set(gcf,'color','w','Units','Normalized','OuterPosition',[0.1 0.1 .9 .5]);
hold on; grid on;
% plot
plot(fig.dnum,fig.lev,'k','LineWidth',1.5)
yline(0,'r','LineWidth',2)
% deco
xlim([fig.dnum(1) fig.dnum(end)])
xticks([fig.dnum(1:3600:end)])
datetick('x','mm/dd','keepticks','keeplimits')
ylim([-6 6])
yticks([-6:2:6])
xlabel('Time (mm/dd)','FontSize',25)
ylabel('Tide Level (m)','FontSize',25)
title([num2str(fig.month(1)),'\_',num2str(fig.month(2),'%02d')],'FontWeight','bold','FontSize',25)
end