[R] 실시간 기상 관측 자료 (매 10분)의 날짜 정보를 이용하여 일별 파일 개수 열지도 (heatmap) 가시화

 정보

  • 업무명     : 실시간 기상 관측 자료 (매 10분)의 날짜 정보를 이용하여 일별 파일 개수 열지도 (heatmap) 가시화

  • 작성자     : 이상호

  • 작성일     : 2020-03-04

  • 설   명      :

  • 수정이력 :

 

 내용

[개요]

  • 안녕하세요? 기상 연구 및 웹 개발을 담당하고 있는 해솔입니다.

  • 오늘은 실시간 기상 관측 자료의 날짜 정보를 이용하여 일별 파일 개수 열지도 가시화를 소개해 드리고자 합니다.

 

dog-647528_1280.jpg

 

[특징]

  • 실시간 10분 기상 자료를 이해하기 위해서 효과적인 시각화가 요구되며 이 프로그램은 이러한 목적을 달성하기 위한 소프트웨어

 

[기능]

  • 문자형 변수를 날짜형으로 변환

  • 숫자형 변수를 날짜형으로 변환

  • calendarHeat를 이용한 가시화

  • ggplot2를 이용한 가시화

 

[활용 자료]

  • 자료명 : rsr_v001.dat, file_L1B_SW.check, file_CLD.check

  • 자료 설명 : 히마와리위성 8호 (Himawari-8/AHI) 자료를 이용하여 대기상단에서의 상향단파복사를 실행하기 위해서 각각 이미 산출된 상향단파복사 (날짜 정보), 6개 가시채널 정보 (디렉터리 및 파일명, 날짜 정보), 구름 탐지 정보 (디렉터리 및 파일명, 날짜 정보)를 의미

 

[자료 처리 방안 및 활용 분석 기법]

  • 없음

 

[사용법]

  • 입력 자료를 동일 디렉터리 위치

  • 소스 코드를 실행
    (Rscript Heatmap_Visualize_Daily_File_Numbers_Using_Date_Information_from_Weather_Observations.R)

  • 가시화 결과를 확인  

 

[사용 OS]

  • Windows 10

 

[사용 언어]

  • R v3.6.2

  • R Studio v1.2.5033

 

 소스 코드

[명세]

  • 전역 설정

    • 최대 10 자리 설정

    • 메모리 해제

    • 영어 설정

# Set Option 
options(digits = 10)
memory.limit(size = 9999999999999)
Sys.setlocale("LC_TIME", "english")

 

  • 라이브러리 읽기

# Library Load
library(data.table)
library(tidyverse)
library(lubridate)
library(extrafont)
library(colorRamps)

 

히마와리위성 8호를 이용하여 이미 산출된 대기 상단에서의 상향단파복사 (날짜 정보) 열지도 가시화

 

  • 파일 읽기

# File Read
dfData = data.table::fread("Data_check/rsr_v001.dat", header = FALSE)

dplyr::tbl_df(dfData)

 

etc-image-1

 

  • Data Frame를 이용한 L1 전처리

    • 날짜 정보 (V1)는 int64형이기 때문에 as.character를 통해 문자형 변환

    • 문자형은 readr::parse_datetime를 통해 날짜형 변환

    • lubridate::date를 통해 년월일 (dtDate)로 변환

# L1 Processing Using Data Frame
dfDataL1 = dfData %>%
  dplyr::mutate(
    dtDateTime = readr::parse_datetime(as.character(V1), format = "%Y%m%d%H%M") 
    , dtDate = lubridate::date(dtDateTime) 
  )
      
dplyr::tbl_df(dfDataL1)

 

etc-image-2

 

  • Data Frame L1를 이용한 L2 전처리

    • 년월일 (dtDate)를 기준으로 자료 개수 처리 (즉 일별 개수)

# L2 Processing Using Data Frame L1
dfDataL2 = dfDataL1 %>%
  dplyr::group_by(dtDate) %>%
  dplyr::summarise(nNumber = n()) 

dplyr::tbl_df(dfDataL2)

 

etc-image-3

 

  • calendarHead을 이용한 가시화

    • 가시화를 위한 초기 설정

# Set Value for Visualization 
font = "Palatino Linotype"
     
trellis.par.set(
  list(
    par.main.text = list(font = 2, col = "black")
    , fontsize = list(text = 30)
    , axis.text=list(font = 1)
    , grid.pars = list(fontfamily = font) 
    )
  )

# Visualization Using calendarHeat
png(file = paste0("FIG3/Heatmap1_Visualization_Using_calendarHeat.png"), 1200, 600) 

calendarHeat(
  dates = dfDataL2$dtDate
  , values = dfDataL2$nNumber
  , color = "r2b"
  , varname = "Himawari-8/AHI L1B Data Exist Number"
  )

dev.off()  

 

blob
그림. calendar를 이용한 열지도 가시화 (1).

 

대기 상단에서의 상향단파복사에 필요한 6개 가시채널 정보 (디렉터리 및 파일명, 날짜 정보) 열지도 가시화

 

  • 파일 읽기

# File Read
dfData = data.table::fread("Data_check/file_L1B_SW.check", header = FALSE) 
      
colnames(dfData) = c("sDirName", "iYear", "iMonth", "iDay", "iHour", "iMinute")

dplyr::tbl_df(dfData)

 

etc-image-5

 

  • Data Frame를 이용한 L1 전처리

    • 날짜 정보 (iYear, iMonth, iDay, iHour, iMinute)는 int형이기 때문에 ISOdatetime를 통해 날짜형 변환

    • lubridate::date를 통해 년월일 (dtDate)로 변환

# L1 Processing Using Data Frame
dfDataL1 = dfData %>%
  dplyr::mutate(
    dtDateTime = ISOdatetime(iYear, iMonth, iDay, iHour, iMinute, sec = 00, tz = "UTC")
    , dtDate = lubridate::date(dtDateTime)
  )

dplyr::tbl_df(dfDataL1)

 

etc-image-6

 

  • Data Frame L1를 이용한 L2 전처리

    • 년월일 (dtDate)를 기준으로 자료 개수 처리 (즉 일별 개수)

# L2 Processing Using Data Frame L1
dfDataL2 = dfDataL1 %>%
  dplyr::group_by(dtDate) %>%
  dplyr::summarise(nNumber = n()) 

dplyr::tbl_df(dfDataL2)

 

etc-image-7

 

  • calendarHead을 이용한 가시화

    • 가시화를 위한 초기 설정

# Set Value for Visualization 
font = "Palatino Linotype"

trellis.par.set(
  list(
    par.main.text = list(font = 2, col = "black")
    , fontsize = list(text = 30)
    , axis.text=list(font = 1)
    , grid.pars = list(fontfamily = font) 
  )
)

# Visualization Using calendarHeat
png(file = paste0("FIG3/Heatmap2_Visualization_Using_calendarHeat.png"), 1200, 600) 

calendarHeat(
  dates = dfDataL2$dtDate
  , values=dfDataL2$nNumber
  , ncolors = 144
  , color = "g2r"
  , varname = "Himawari-8/AHI L1B Shortwave Radiation Data Exist Number"
  )

dev.off()

 

Heatmap2_Visualization_Using_calendarHeat.png
그림. calendar를 이용한 열지도 가시화 (2).

 

대기 상단에서의 상향단파복사에 필요한 구름 탐지 정보 (디렉터리 및 파일명, 날짜 정보) 열지도 가시화

 

  • 파일 읽기

# File Read
dfData = data.table::fread("Data_check/file_CLD.check", header = FALSE) 

colnames(dfData) = c("sDirName", "iYear", "iMonth", "iDay", "iHour", "iMinute")

dplyr::tbl_df(dfData)

 

etc-image-9

 

  • Data Frame를 이용한 L1 전처리

    • 날짜 정보 (iYear, iMonth, iDay, iHour, iMinute)는 int형이기 때문에 ISOdatetime를 통해 날짜형 변환

    • lubridate::date를 통해 년월일 (dtDate)로 변환

# L1 Processing Using Data Frame
dfDataL1 = dfData %>%
  dplyr::mutate(
    dtDateTime = ISOdatetime(iYear, iMonth, iDay, iHour, iMinute, sec = 00, tz = "UTC")
    , dtDate = lubridate::date(dtDateTime)
  )

dplyr::tbl_df(dfDataL1)

 

etc-image-10

 

  • Data Frame L1를 이용한 L2 전처리

    • 년월일 (dtDate)를 기준으로 자료 개수 처리 (즉 일별 개수)

# L2 Processing Using Data Frame L1
dfDataL2 = dfDataL1 %>%
  dplyr::group_by(dtDate) %>%
  dplyr::summarise(nNumber = n())

dplyr::tbl_df(dfDataL2)

 

etc-image-11

 

  • calendarHead을 이용한 가시화

    • 가시화를 위한 초기 설정

# Set Value for calendarHeat 
font = "Palatino Linotype"

trellis.par.set(
  list(
    par.main.text = list(font = 2, col = "black")
    , fontsize = list(text = 30)
    , axis.text=list(font = 1)
    , grid.pars = list(fontfamily = font) 
  )
)

# Set Value for Visualization 
png(file = paste0("FIG3/Heatmap3_Visualization_Using_calendarHeat.png"), 1200, 600) 

calendarHeat(
  dates = dfDataL2$dtDate
  , values=dfDataL2$nNumber
  , ncolors = 144
  , color = "w2b"
  , varname = "Himawari-8/AHI L2 Cloud Data Exist Number"
)

dev.off()

 

blob
그림. calendar를 이용한 열지도 가시화 (3).

 

ggplot2를 이용한 열지도 가시화

 

  • 최근 데이터 분석에서 자주 사용하는 ggplot2를 통해 가시화 해보았습니다.
  • 일반적으로 연구에서는 기본 plot을 주로 이용하나 학술 논문 및 발표에서는 ggplot2를 사용하고 합니다.
  • 이는 효과적인 전달하는 데 도와줍니다.

 

  • Data Frame L1를 이용한 L3 전처리

    • 앞서 dfDataL2와 달리 영문 월 (sMonth), 영문 요일 (sWday), 연도 (nYear), 해당 월에 대한 주말 (nMonthWeek) 변수 추가

# L3 Processing Using Data Frame L1
dfDataL3 = dfDataL1 %>%
  dplyr::group_by(dtDate) %>%
  dplyr::summarise(nNumber = n()) %>%
  dplyr::mutate(
    sMonth = lubridate::month(dtDate, label = TRUE, abbr = TRUE, locale = "English")
    , sWday = lubridate::wday(dtDate, label = TRUE, abbr = TRUE, locale = "English")
    , nYear = lubridate::year(dtDate)
    , nWeek = lubridate::week(dtDate)
    , nMinWeek = lubridate::week(lubridate::floor_date(dtDate, unit = "month"))
    , nMonthWeek = nWeek - nMinWeek + 1
  )

dplyr::tbl_df(dfDataL3)

 

etc-image-13

 

  • ggplot2을 이용한 열지도 가시화

    • 가시화를 위한 초기 설정

# Set Value for Visualization 
cbMatlab = colorRamps::matlab.like(11)
font = "Palatino Linotype"

# Visualization Using ggplot2
ggplot(data = dfDataL3, aes(x = nMonthWeek, y = sWday, fill = nNumber)) + 
  geom_tile(colour = "white") + 
  theme_bw() +
  facet_grid(nYear ~ sMonth) + 
  scale_fill_gradientn(colours = cbMatlab, limits=c(0, 144), breaks = seq(0, 144, 36), na.value = cbMatlab[length(cbMatlab)]) +
  scale_x_continuous(expand = c(0,0), breaks=seq(1, 6, by = 1), minor_breaks = seq(1, 6, by = 2), limits=c(0, 6)) +
  labs(
    x = "Week of Month"
    , y = "Weekend"
    , title = "Himawari-8/AHI L2 CLD Data Exist Number"
    , fill = "Number"
  ) +
  theme(
    plot.title = element_text(face = "bold", size = 18, color = "black")
    , axis.title.x = element_text(size = 18, colour = "black")
    , axis.title.y = element_text(size = 18, colour = "black", angle=90)
    , axis.text.x  = element_text(size = 18, colour = "black")
    , axis.text.y  = element_text(size = 18, colour = "black")
    , legend.title = element_text(face = "bold", size = 14, colour = "black")
    , legend.key = element_blank()
    , legend.text = element_text(size = 14, colour = "black")
    , legend.background = element_blank()
    , text = element_text(family = font)
    , plot.margin = unit(c(0, 8, 0, 0), "mm")
  ) +
  ggsave(filename = paste0("FIG3/Heatmap_Visualization_Using_ggplot2.png"), width = 12, height = 6, dpi = 600)

 

Heatmap_Visualization_Using_ggplot2.png
그림. ggplot2를 이용한 열지도 가시화.

 

[전체]

#===============================================================================================================
# Routine : Main R program
#
# Purpose : Heatmap Visualize Daily File Numbers Using Date Information from Weather Observations
#
# Author : MS. Sang-Ho Lee
#
# Revisions: V1.0 March 04, 2020 First release (MS. Sang-Ho Lee)
#===============================================================================================================
# Set Option
options(digits = 10)
memory.limit(size = 9999999999999)
Sys.setlocale("LC_TIME", "english")
# Library Load
library(data.table)
library(tidyverse)
library(lubridate)
library(extrafont)
library(colorRamps)
#==============================================================
# Heatmap Using calendarHeat
#==============================================================
#==============================================================
# Himawari-8/AHI L1B Data Exist Number
#==============================================================
# File Read
dfData = data.table::fread("Data_check/rsr_v001.dat", header = FALSE)
dplyr::tbl_df(dfData)
# L1 Processing Using Data Frame
dfDataL1 = dfData %>%
dplyr::mutate(
dtDateTime = readr::parse_datetime(as.character(V1), format = "%Y%m%d%H%M")
, dtDate = lubridate::date(dtDateTime)
)
dplyr::tbl_df(dfDataL1)
# L2 Processing Using Data Frame L1
dfDataL2 = dfDataL1 %>%
dplyr::group_by(dtDate) %>%
dplyr::summarise(nNumber = n())
dplyr::tbl_df(dfDataL2)
# Set Value for Visualization
font = "Palatino Linotype"
trellis.par.set(
list(
par.main.text = list(font = 2, col = "black")
, fontsize = list(text = 30)
, axis.text=list(font = 1)
, grid.pars = list(fontfamily = font)
)
)
# Visualization Using calendarHeat
png(file = paste0("FIG3/Heatmap1_Visualization_Using_calendarHeat.png"), 1200, 600)
calendarHeat(
dates = dfDataL2$dtDate
, values = dfDataL2$nNumber
, color = "r2b"
, varname = "Himawari-8/AHI L1B Data Exist Number"
)
dev.off()
#==============================================================
# Himawari-8/AHI L1B Data Exist Number
#==============================================================
# File Read
dfData = data.table::fread("Data_check/file_L1B_SW.check", header = FALSE)
colnames(dfData) = c("sDirName", "iYear", "iMonth", "iDay", "iHour", "iMinute")
dplyr::tbl_df(dfData)
# L1 Processing Using Data Frame
dfDataL1 = dfData %>%
dplyr::mutate(
dtDateTime = ISOdatetime(iYear, iMonth, iDay, iHour, iMinute, sec = 00, tz = "UTC")
, dtDate = lubridate::date(dtDateTime)
)
dplyr::tbl_df(dfDataL1)
# L2 Processing Using Data Frame L1
dfDataL2 = dfDataL1 %>%
dplyr::group_by(dtDate) %>%
dplyr::summarise(nNumber = n())
dplyr::tbl_df(dfDataL2)
# Set Value for Visualization
font = "Palatino Linotype"
trellis.par.set(
list(
par.main.text = list(font = 2, col = "black")
, fontsize = list(text = 30)
, axis.text=list(font = 1)
, grid.pars = list(fontfamily = font)
)
)
# Visualization Using calendarHeat
png(file = paste0("FIG3/Heatmap2_Visualization_Using_calendarHeat.png"), 1200, 600)
calendarHeat(
dates = dfDataL2$dtDate
, values=dfDataL2$nNumber
, ncolors = 144
, color = "g2r"
, varname = "Himawari-8/AHI L1B Data Exist Number"
)
dev.off()
#==============================================================
# Himawari-8/AHI L2 Cloud Data Exist Number
#==============================================================
# File Read
dfData = data.table::fread("Data_check/file_CLD.check", header = FALSE)
colnames(dfData) = c("sDirName", "iYear", "iMonth", "iDay", "iHour", "iMinute")
dplyr::tbl_df(dfData)
# L1 Processing Using Data Frame
dfDataL1 = dfData %>%
dplyr::mutate(
dtDateTime = ISOdatetime(iYear, iMonth, iDay, iHour, iMinute, sec = 00, tz = "UTC")
, dtDate = lubridate::date(dtDateTime)
)
dplyr::tbl_df(dfDataL1)
# L2 Processing Using Data Frame L1
dfDataL2 = dfDataL1 %>%
dplyr::group_by(dtDate) %>%
dplyr::summarise(nNumber = n())
dplyr::tbl_df(dfDataL2)
# Set Value for calendarHeat
font = "Palatino Linotype"
trellis.par.set(
list(
par.main.text = list(font = 2, col = "black")
, fontsize = list(text = 30)
, axis.text=list(font = 1)
, grid.pars = list(fontfamily = font)
)
)
# Set Value for Visualization
png(file = paste0("FIG3/Heatmap3_Visualization_Using_calendarHeat.png"), 1200, 600)
calendarHeat(
dates = dfDataL2$dtDate
, values=dfDataL2$nNumber
, ncolors = 144
, color = "w2b"
, varname = "Himawari-8/AHI L2 Cloud Data Exist Number"
)
dev.off()
#==============================================================
# Heatmap Using ggplot2
#==============================================================
# L3 Processing Using Data Frame L1
dfDataL3 = dfDataL1 %>%
dplyr::group_by(dtDate) %>%
dplyr::summarise(nNumber = n()) %>%
dplyr::mutate(
sMonth = lubridate::month(dtDate, label = TRUE, abbr = TRUE, locale = "English")
, sWday = lubridate::wday(dtDate, label = TRUE, abbr = TRUE, locale = "English")
, nYear = lubridate::year(dtDate)
, nWeek = lubridate::week(dtDate)
, nMinWeek = lubridate::week(lubridate::floor_date(dtDate, unit = "month"))
, nMonthWeek = nWeek - nMinWeek + 1
)
dplyr::tbl_df(dfDataL3)
# Set Value for Visualization
cbMatlab = colorRamps::matlab.like(11)
font = "Palatino Linotype"
# Visualization Using ggplot2
ggplot(data = dfDataL3, aes(x = nMonthWeek, y = sWday, fill = nNumber)) +
geom_tile(colour = "white") +
theme_bw() +
facet_grid(nYear ~ sMonth) +
scale_fill_gradientn(colours = cbMatlab, limits=c(0, 144), breaks = seq(0, 144, 36), na.value = cbMatlab[length(cbMatlab)]) +
scale_x_continuous(expand = c(0,0), breaks=seq(1, 6, by = 1), minor_breaks = seq(1, 6, by = 2), limits=c(0, 6)) +
labs(
x = "Week of Month"
, y = "Weekend"
, title = "Himawari-8/AHI L2 CLD Data Exist Number"
, fill = "Number"
) +
theme(
plot.title = element_text(face = "bold", size = 18, color = "black")
, axis.title.x = element_text(size = 18, colour = "black")
, axis.title.y = element_text(size = 18, colour = "black", angle=90)
, axis.text.x = element_text(size = 18, colour = "black")
, axis.text.y = element_text(size = 18, colour = "black")
, legend.title = element_text(face = "bold", size = 14, colour = "black")
, legend.key = element_blank()
, legend.text = element_text(size = 14, colour = "black")
, legend.background = element_blank()
, text = element_text(family = font)
, plot.margin = unit(c(0, 8, 0, 0), "mm")
) +
ggsave(filename = paste0("FIG3/Heatmap_Visualization_Using_ggplot2.png"), width = 12, height = 6, dpi = 600)

 

 참고 문헌

[논문]

  • 없음

[보고서]

  • 없음

[URL]

  • 없음

 

 문의사항

[기상학/프로그래밍 언어]

  • sangho.lee.1990@gmail.com

[해양학/천문학/빅데이터]

  • saimang0804@gmail.com