@@ -48,25 +48,41 @@ create_file_folder <- function(site, block) {
48
48
}
49
49
50
50
# 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 ) {
52
52
53
53
# this ensures that the folder to store this file exists
54
54
create_file_folder(site , block )
55
55
56
56
file_path <- file.path(json_file_base_folder , site , block , " events.json" )
57
57
58
- # create appropriate structure
59
- experiment <- list ()
60
- experiment $ management <- list ()
61
- experiment $ management $ events <- new_list
62
58
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
+ # ####
67
78
}
68
79
}
69
80
81
+ # create appropriate structure
82
+ experiment <- list ()
83
+ experiment $ management <- list ()
84
+ experiment $ management $ events <- event_list
85
+
70
86
# create file
71
87
jsonlite :: write_json(experiment , path = file_path , pretty = TRUE ,
72
88
null = " list" , auto_unbox = TRUE )
@@ -75,8 +91,6 @@ write_json_file <- function(site, block, new_list) {
75
91
# retrieve the events of a specific site and block and return as a NESTED LIST.
76
92
# this retrieves the events in the same "format" as they will be saved back
77
93
# 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.
80
94
retrieve_json_info <- function (site , block ) {
81
95
file_path <- file.path(json_file_base_folder , site , block , " events.json" )
82
96
@@ -93,9 +107,18 @@ retrieve_json_info <- function(site, block) {
93
107
return (list ())
94
108
}
95
109
96
- # add block information to each event
110
+ # add block information and apply exceptions to each event
97
111
for (i in 1 : length(events )) {
98
112
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
+ # ####
99
122
}
100
123
101
124
return (events )
0 commit comments