generated from IMMM-SFA/metarepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill_na_in_epw.R
55 lines (46 loc) · 1.63 KB
/
fill_na_in_epw.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
library("dplyr")
library("readr")
source("read_epw.R")
climate.folder = "input_data/annual_WRF/M02_EnergyPlus_Forcing_Historical_LowRes_ann_2018"
files = list.files(sprintf("%s/wrf_epw", climate.folder), pattern="*.epw")
df.all.epw <- lapply(files, function(f){
print(f)
df = read.epw(sprintf("%s/wrf_epw/%s", climate.folder, f)) %>%
dplyr::mutate(filename = f) %>%
{.}
df
}) %>%
dplyr::bind_rows() %>%
dplyr::mutate(cell.id = as.numeric(gsub(".epw", "", filename))) %>%
{.}
df.all.epw %>%
summary()
## [1] "119.epw" "78.epw" "91.epw" "97.epw"
epw.need.fill.na <- df.all.epw %>%
dplyr::filter(if_any(everything(), ~ is.na(.))) %>%
distinct(filename) %>%
.$filename %>%
{.}
filled.dfs <- df.all.epw %>%
dplyr::filter(filename %in% epw.need.fill.na) %>%
tidyr::fill(everything(), .direction = "downup") %>%
dplyr::group_by(filename) %>%
dplyr::group_split()
for (df in filled.dfs) {
filename = df$filename[[1]]
df %>%
dplyr::select(-filename, -cell.id) %>%
readr::write_csv(sprintf("%s/wrf_epw/%s", climate.folder, gsub("epw", "csv", filename)))
}
for (f in epw.need.fill.na) {
print(f)
err.epw = readLines(sprintf("%s/wrf_epw/%s", climate.folder, f))
filled.csv = readLines(sprintf("%s/wrf_epw/%s", climate.folder, gsub("epw", "csv", f)))
newlines = c(err.epw[1:8], filled.csv[2:length(filled.csv)])
writeLines(newlines, sprintf("%s/wrf_epw/%s", climate.folder, f))
}
## remove temp csv files
for (f in epw.need.fill.na) {
filled.csv = sprintf("%s/wrf_epw/%s", climate.folder, gsub("epw", "csv", f))
file.remove(filled.csv)
}