Skip to content

Commit 82a93e0

Browse files
committed
Change json file structure for organic fertilizer events
1 parent d1cb6ae commit 82a93e0

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

json_file_helpers.R

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,41 @@ create_file_folder <- function(site, block) {
4848
}
4949

5050
# write a given event list to a json file, overwriting everything in it
51-
write_json_file <- function(site, block, new_list) {
51+
write_json_file <- function(site, block, event_list) {
5252

5353
# this ensures that the folder to store this file exists
5454
create_file_folder(site, block)
5555

5656
file_path <- file.path(json_file_base_folder, site, block, "events.json")
5757

58-
# create appropriate structure
59-
experiment <- list()
60-
experiment$management <- list()
61-
experiment$management$events <- new_list
6258

63-
# erase block information in each event if there are any events in the list
64-
if (length(experiment$management$events) > 0) {
65-
for (i in 1:length(experiment$management$events)) {
66-
experiment$management$events[[i]]$block <- NULL
59+
# if there are events in the list, do the following:
60+
# - erase block information in each event
61+
# - apply exceptions
62+
if (length(event_list) > 0) {
63+
for (i in 1:length(event_list)) {
64+
event_list[[i]]$block <- NULL
65+
66+
##### EXCEPTIONS
67+
event <- event_list[[i]]
68+
69+
# if the event type is fertilizer application and the fertilizer
70+
# type is organic, change mgmt_operations_event to organic_material
71+
# to conform to the ICASA standard
72+
if (identical(event$mgmt_operations_event, "fertilizer") &&
73+
identical(event$fertilizer_type, "fertilizer_type_organic")) {
74+
event_list[[i]]$mgmt_operations_event <- "organic_material"
75+
}
76+
77+
#####
6778
}
6879
}
6980

81+
# create appropriate structure
82+
experiment <- list()
83+
experiment$management <- list()
84+
experiment$management$events <- event_list
85+
7086
# create file
7187
jsonlite::write_json(experiment, path = file_path, pretty = TRUE,
7288
null = "list", auto_unbox = TRUE)
@@ -75,8 +91,6 @@ write_json_file <- function(site, block, new_list) {
7591
# retrieve the events of a specific site and block and return as a NESTED LIST.
7692
# this retrieves the events in the same "format" as they will be saved back
7793
# later, i.e. with code names, "-99.0" for missing values etc.
78-
# the only difference is the block value, which will be removed when saving
79-
# back to a json file.
8094
retrieve_json_info <- function(site, block) {
8195
file_path <- file.path(json_file_base_folder, site, block, "events.json")
8296

@@ -93,9 +107,18 @@ retrieve_json_info <- function(site, block) {
93107
return(list())
94108
}
95109

96-
# add block information to each event
110+
# add block information and apply exceptions to each event
97111
for (i in 1:length(events)) {
98112
events[[i]]$block <- block
113+
114+
##### EXCEPTIONS
115+
116+
# if mgmt_operations_event is organic_material, change it to fertilizer
117+
if (identical(events[[i]]$mgmt_operations_event, "organic_material")) {
118+
events[[i]]$mgmt_operations_event <- "fertilizer"
119+
}
120+
121+
#####
99122
}
100123

101124
return(events)

0 commit comments

Comments
 (0)