@@ -134,94 +134,94 @@ write.csv(df_dsa_legal, 'data/dsa_legal/dsa_legal.csv', row.names = FALSE)
134
134
135
135
``` {r subregions}
136
136
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)
225
225
226
226
```
227
227
0 commit comments