Skip to content

Instantly share code, notes, and snippets.

@practice
Created November 2, 2018 07:21
Show Gist options
  • Save practice/b590633b53bce67d30bac6943f172bfa to your computer and use it in GitHub Desktop.
Save practice/b590633b53bce67d30bac6943f172bfa to your computer and use it in GitHub Desktop.
Reading hdf5, hdf4, netcdf with R
@practice
Copy link
Author

practice commented Nov 2, 2018

Run Rstudio with docker

docker run --name rstudio -e PASSWORD=r -p 8787:8787 rocker/verse

Install hdf5 inside Rstudio container with

apt install libhdf5-dev

Install hdf5r R package

install.packages("hdf5r")

@practice
Copy link
Author

practice commented Nov 5, 2018

library("hdf5r")

file.h5 <- H5File$new("./aod/GEM_TEST_L2__AERAOD_20050302T040700_20110120T041712_03348_00_000001_20180829T023337.h5", mode = "r+")
# str(file.h5)
# tempfile(tmpdir = getwd(), fileext = ".h5")
file.h5$ls(recursive = TRUE)
aodDs <- file.h5[["HDF5/SWATHS/Aerosol NearUV Swath/Data Fields/FinalAerosolOpticalDepth"]]
str(aodDs)
h5attr_names(aodDs)
h5attr(aodDs, "MissingValue")

aodDs$print()
aodDs$get_type()
aodDs$get_type()$get_class()

h5attr_names(aodDs)
file.h5$ls()
aod <- aodDs[3, 1:60, 1:1644]
aod[33:37, 887:890]
summary(aod)

write.table(aod, file = "./aod/GEM_TEST_L2__AERAOD_20050302T040700_20110120T041712_03348_00_000001_20180829T023337.csv",
            sep = ",", row.names = FALSE)

file.h5$close_all()

@practice
Copy link
Author

practice commented Nov 6, 2018

Reading NetCDF4

netcdf 유틸리티 설치

$ apt-get install libnetcdf-dev netcdf-bin

R에서

install.packages("ncdf4")
library(ncdf4)
ncfile <- nc_open("S5P_RPRO_L2__NO2____20180301T021947_20180301T040315_01968_01_010001_20180522T003757.nc")

? nc_open

str(ncfile)
no2 <- ncvar_get(ncfile, "QA_STATISTICS/nitrogendioxide_total_column_pdf_axis")
no2

aero <- read.csv("19930101_20181027_XiangHe.lev20", skip = 6, header = TRUE)
head(aero[c(1,2,19)])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment