기상청 API를 통한 asos 데이터 가져오기
library(RMariaDB)
con <- dbConnect(
drv = RMariaDB::MariaDB(),
username = 'ducj',
password = 'whckdwp1!@',
host = NULL,
port = 3306,
db = 'db'
)
dbListTables(con)
key='IQr2d9Wp84iZd8LWOvB2m3yW969idNg/uQh52nZVitgb6zpiM4h9QvOFD0xe3hko'
library(xml2);library(rvest);library(RCurl);library(XML);library(dplyr);library(rjson)
ASOS_read=function(dateCd='HR',startDt,startHh,endDt,endHh,stnlds,schListCnt,pageIndex,key){
if(dateCd=="HR"){
url=paste0('https://data.kma.go.kr/apiData/getData?type=json&dataCd=ASOS&dateCd=',dateCd,'&startDt=',startDt,'&startHh=',startHh,
'&endDt=',endDt,'&endHh=',endHh,'&stnIds=',stnlds,'&schListCnt=',schListCnt,'&pageIndex=',pageIndex,'&apiKey=',key)}
if(dateCd=='DAY'){
url=paste0('https://data.kma.go.kr/apiData/getData?type=json&dataCd=ASOS&dateCd=',dateCd,'&startDt=',startDt,
'&endDt=',endDt,'&stnIds=',stnlds,'&schListCnt=',schListCnt,'&pageIndex=',pageIndex,'&apiKey=',key)
}
bind_rows(fromJSON(paste0(suppressWarnings(readLines(url,encoding='UTF-8')),collapse = ''))[[4]]$info)
}
library(readr)
Info=read_csv('/home/ducj2/lotto/stnInfo_20191127191218.csv',locale = locale(encoding='cp949'))
colnames(Info)[c(1,2,3,4,6,7,8)]=c('STN_ID','startDate','endDate','stationName','lat','lon','alt')
# dbWriteTable(con, 'ASOSInfo',Info,overwrite=T)
dbListTables(con)
i=102
startDate=as.Date('2018-01-01')
endDate=as.Date('2018-12-31')
ls=list()
for(i in as.character(unique(Info$STN_ID))){
message(i)
try(silent = T,{
temp=ASOS_read(dateCd='DAY',startDt=format(startDate,'%Y%m%d'),startHh = '',
endDt =format(endDate,'%Y%m%d'),endHh='',
stnlds=i,schListCnt = 999,pageIndex = 1,key=key)
})
if(nrow(temp)>0)
if(nrow(Info)>1){
temp_Info=Info[Info$STN_ID==i,]
old=temp_Info[!is.na(temp_Info$endDate),'endDate']
new=temp_Info[is.na(temp_Info$endDate),'startDate']
temp2=rbind(left_join(temp[as.Date(temp$TM)<as.Date(old$endDate),],temp_Info[!is.na(temp_Info$endDate),c(1:4,6:8)],by='STN_ID'),
left_join(temp[as.Date(temp$TM)>as.Date(new$startDate),],temp_Info[is.na(temp_Info$endDate),c(1:4,6:8)],by='STN_ID'))
}else temp2=left_join(temp,Info[Info$STN_ID==i,c(1:4,6:8)],by='STN_ID')
ls[[i]]=temp2
}
# dbWriteTable(con, 'ASOS_Dt',unique(bind_rows(ls)[,-grep('ISCS',colnames(bind_rows(ls)))]), overwrite = TRUE)
query='select * from ASOS_Dt'
colnames(dbGetQuery(con,query))
dbWriteTable(con, 'ASOS_Dt',unique(bind_rows(dbGetQuery(con,query),bind_rows(ls)[,-grep('ISCS',colnames(bind_rows(ls)))])), overwrite = TRUE)
dbWriteTable(con, paste0('ASOS_Dt_',format(Sys.Date(),'%Y%m%d')),left_join(bind_rows(ls),Info2[,c(1:4,6:8)],by='STN_ID'), overwrite = TRUE)
dbListTables(con)