Skip to content

Commit 99be16c

Browse files
committed
mv subregion postprocessing to seperate script
1 parent 42a5cc5 commit 99be16c

File tree

2 files changed

+105
-90
lines changed

2 files changed

+105
-90
lines changed

data/data_postprocessing.Rmd

+88-88
Original file line numberDiff line numberDiff line change
@@ -134,94 +134,94 @@ write.csv(df_dsa_legal, 'data/dsa_legal/dsa_legal.csv', row.names = FALSE)
134134

135135
```{r subregions}
136136
137-
####################################################################
138-
# data loading functions for processsing EE geojson for subregions
139-
# note this data does not include any crop information
140-
# All non-ag area should be masked out in EE
141-
142-
# parse model and year from filename. Should be similiar to disalexi-DSAsubregions-2015.geojson
143-
get_filename_info <- function(file){
144-
# get the basename from the file path
145-
b <- basename(file)
146-
147-
# split filename into parts
148-
b_ex <- strsplit(b, "\\.")
149-
n <- b_ex[[1]][1] # name without extension
150-
151-
n_split <- strsplit(n, "-")
152-
m <- n_split[[1]][1]
153-
yr <- n_split[[1]][3]
154-
return(c(m, yr))
155-
}
156-
157-
# calculate acre-feet per month
158-
acre_feet_per_month <- function(df, wy, month, reducer_size){
159-
#num_days_in_month
160-
numdays <- num_days_in_month_csv(wy, month)
161-
162-
# name of the field with the month's mean
163-
mean_field <- paste(month, '_', 'mean', sep='')
164-
165-
# name of the field with the month's count
166-
count_field <- paste(month, '_', 'count', sep='')
167-
168-
# output name for the field with the month's acre-feet total
169-
output_fieldname <- paste(month, '_', 'ACREFT', sep='')
170-
171-
# add field that calculates acre-feet for month using mean, count, number of days in month and reducer
172-
d <- df %>% mutate_(xyz = interp(~acre_feet(m, c, numdays, reducer_size), m=as.name(mean_field), c=as.name(count_field)))
173-
names(d)[names(d) == "xyz"] <- output_fieldname
174-
return(d)
175-
}
176-
177-
# read in geojson export and add fields parsed from filename and calculate monthly and wy acre-feet
178-
subregions_add_fields <- function(geojson, reducer_size){
179-
model_year <- get_filename_info(geojson)
180-
d <- st_read(geojson) # read geojson file to dataframe
181-
d$model <- model_year[1] # add name of model parsed from filename
182-
d$wateryear <- model_year[2] # add water year parsed from filename
183-
d$source <- geojson # add the filepath as the source
184-
185-
# calculate island/region area from geojson (units in square meters)
186-
d$AREA_m <- st_area(d)
187-
188-
#add fields for each month's acre-feet
189-
for(m in month.abb){
190-
d<-acre_feet_per_month(d, model_year[2], toupper(m), reducer_size)
191-
}
192-
193-
# calculate water year total acre-feet by adding all the monthly acre-feet columns
194-
d$WY_ACREFT <- d$OCT_ACREFT + d$NOV_ACREFT + d$DEC_ACREFT + d$JAN_ACREFT +
195-
d$FEB_ACREFT + d$MAR_ACREFT + d$APR_ACREFT + d$MAY_ACREFT + d$JUN_ACREFT +
196-
d$JUL_ACREFT + d$AUG_ACREFT + d$SEP_ACREFT
197-
return(d)
198-
}
199-
200-
num_days_in_month_csv <- function(wy, month){
201-
# load the months
202-
month <- toupper(month)
203-
months <- read.csv('lookups/months.csv', stringsAsFactors=FALSE)
204-
wateryr <- mutate(months, cmb=paste(months$WaterYear, months$Month))
205-
days <- wateryr$NumberDays[match(paste(wy, month), wateryr$cmb)]
206-
return(days)
207-
}
208-
209-
####################################################################################
210-
####################################################################################
211-
212-
list_of_files <- list.files(path="data/subregions", pattern=".geojson", full.names=TRUE)
213-
listofdfs <- list() #Create a list in which you intend to save your df's.
214-
215-
# for loop to load in all the raw json files to single data frame
216-
for (i in 1:length(list_of_files)) {
217-
print(list_of_files[i])
218-
g <- subregions_add_fields(list_of_files[i], 30)
219-
listofdfs[[i]]<-g
220-
}
221-
df_subregions <- ldply(listofdfs, data.frame) # make into one dataframe
222-
223-
saveRDS(df_subregions, file="data/subregions/subregions.rds")
224-
write.csv(df_subregions, file="data/subregions/subregions.csv", row.names = FALSE)
137+
# ####################################################################
138+
# # data loading functions for processsing EE geojson for subregions
139+
# # note this data does not include any crop information
140+
# # All non-ag area should be masked out in EE
141+
#
142+
# # parse model and year from filename. Should be similiar to disalexi-DSAsubregions-2015.geojson
143+
# get_filename_info <- function(file){
144+
# # get the basename from the file path
145+
# b <- basename(file)
146+
#
147+
# # split filename into parts
148+
# b_ex <- strsplit(b, "\\.")
149+
# n <- b_ex[[1]][1] # name without extension
150+
#
151+
# n_split <- strsplit(n, "-")
152+
# m <- n_split[[1]][1]
153+
# yr <- n_split[[1]][3]
154+
# return(c(m, yr))
155+
# }
156+
#
157+
# # calculate acre-feet per month
158+
# acre_feet_per_month <- function(df, wy, month, reducer_size){
159+
# #num_days_in_month
160+
# numdays <- num_days_in_month_csv(wy, month)
161+
#
162+
# # name of the field with the month's mean
163+
# mean_field <- paste(month, '_', 'mean', sep='')
164+
#
165+
# # name of the field with the month's count
166+
# count_field <- paste(month, '_', 'count', sep='')
167+
#
168+
# # output name for the field with the month's acre-feet total
169+
# output_fieldname <- paste(month, '_', 'ACREFT', sep='')
170+
#
171+
# # add field that calculates acre-feet for month using mean, count, number of days in month and reducer
172+
# d <- df %>% mutate_(xyz = interp(~acre_feet(m, c, numdays, reducer_size), m=as.name(mean_field), c=as.name(count_field)))
173+
# names(d)[names(d) == "xyz"] <- output_fieldname
174+
# return(d)
175+
# }
176+
#
177+
# # read in geojson export and add fields parsed from filename and calculate monthly and wy acre-feet
178+
# subregions_add_fields <- function(geojson, reducer_size){
179+
# model_year <- get_filename_info(geojson)
180+
# d <- st_read(geojson) # read geojson file to dataframe
181+
# d$model <- model_year[1] # add name of model parsed from filename
182+
# d$wateryear <- model_year[2] # add water year parsed from filename
183+
# d$source <- geojson # add the filepath as the source
184+
#
185+
# # calculate island/region area from geojson (units in square meters)
186+
# d$AREA_m <- st_area(d)
187+
#
188+
# #add fields for each month's acre-feet
189+
# for(m in month.abb){
190+
# d<-acre_feet_per_month(d, model_year[2], toupper(m), reducer_size)
191+
# }
192+
#
193+
# # calculate water year total acre-feet by adding all the monthly acre-feet columns
194+
# d$WY_ACREFT <- d$OCT_ACREFT + d$NOV_ACREFT + d$DEC_ACREFT + d$JAN_ACREFT +
195+
# d$FEB_ACREFT + d$MAR_ACREFT + d$APR_ACREFT + d$MAY_ACREFT + d$JUN_ACREFT +
196+
# d$JUL_ACREFT + d$AUG_ACREFT + d$SEP_ACREFT
197+
# return(d)
198+
# }
199+
#
200+
# num_days_in_month_csv <- function(wy, month){
201+
# # load the months
202+
# month <- toupper(month)
203+
# months <- read.csv('lookups/months.csv', stringsAsFactors=FALSE)
204+
# wateryr <- mutate(months, cmb=paste(months$WaterYear, months$Month))
205+
# days <- wateryr$NumberDays[match(paste(wy, month), wateryr$cmb)]
206+
# return(days)
207+
# }
208+
#
209+
# ####################################################################################
210+
# ####################################################################################
211+
#
212+
# list_of_files <- list.files(path="data/subregions", pattern=".geojson", full.names=TRUE)
213+
# listofdfs <- list() #Create a list in which you intend to save your df's.
214+
#
215+
# # for loop to load in all the raw json files to single data frame
216+
# for (i in 1:length(list_of_files)) {
217+
# print(list_of_files[i])
218+
# g <- subregions_add_fields(list_of_files[i], 30)
219+
# listofdfs[[i]]<-g
220+
# }
221+
# df_subregions <- ldply(listofdfs, data.frame) # make into one dataframe
222+
#
223+
# saveRDS(df_subregions, file="data/subregions/subregions.rds")
224+
# write.csv(df_subregions, file="data/subregions/subregions.csv", row.names = FALSE)
225225
226226
```
227227

data/subregions/DSAsubregions_postprocessing.R

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
1+
library(sf)
2+
library(plyr)
3+
library(tidyverse)
4+
library(lazyeval)
25

36
####################################################################
47
# data loading functions for processsing EE geojson for subregions
@@ -40,6 +43,15 @@ acre_feet_per_month <- function(df, wy, month, reducer_size){
4043
return(d)
4144
}
4245

46+
# calculate acre-feet from monthly avg daily ET
47+
acre_feet <- function(mean_et, cell_count, number_days, reducer_size){
48+
# Crop_acre_feet = (count) * (reducer pixel size) ^2 * (sq m to acres) * (mean daily ET) /10* (mm to feet) * (num days in month)
49+
mm2ft <- 0.00328084
50+
sqm2acres <- 0.000247105
51+
crop_acft <- cell_count * reducer_size^2 * sqm2acres * mean_et / 10 * mm2ft * number_days
52+
return(crop_acft)
53+
}
54+
4355
# read in geojson export and add fields parsed from filename and calculate monthly and wy acre-feet
4456
subregions_add_fields <- function(geojson, reducer_size){
4557
model_year <- get_filename_info(geojson)
@@ -84,7 +96,10 @@ for (i in 1:length(list_of_files)) {
8496
g <- subregions_add_fields(list_of_files[i], 30)
8597
listofdfs[[i]]<-g
8698
}
87-
df_subregions <- ldply(listofdfs, data.frame) # make into one dataframe
99+
subregions <- ldply(listofdfs, data.frame) # make into one dataframe
100+
101+
# drop geo and ID columns
102+
df_subregions <- subregions %>% select(-c(id, geometry))
88103

89104
saveRDS(df_subregions, file="data/subregions/subregions.rds")
90105
write.csv(df_subregions, file="data/subregions/subregions.csv", row.names = FALSE)

0 commit comments

Comments
 (0)