1
- # #This is a temporary main control file with lots of TODOs to move
1
+ # #This is a temporary main control file with lots of TODOs to move
2
2
# #this whole effort to a package with project specific config and run files
3
3
4
4
# Rprof()
@@ -9,16 +9,26 @@ options(error = function() traceback(2))
9
9
options(warning = function () traceback(2 ))
10
10
11
11
# -----------------------
12
- # Load packages
12
+ # Load packages
13
13
# -----------------------
14
- # #TODO consider local R installation, with predownloaded packages in that library
15
- library(foreign )
16
- library(reshape )
17
- library(XLConnect )
18
- library(descr )
19
- library(Hmisc )
20
- library(data.table )
21
- library(plyr )
14
+
15
+ safeLoadPackage <- function (package_name ){
16
+ tryCatch({
17
+ library(package_name , character.only = TRUE )
18
+ }, error = function (err ) {
19
+ print(paste(" Installing" , package_name ))
20
+ install.packages(package_name , repos = " https://ftp.osuosl.org/pub/cran" )
21
+ library(package_name , character.only = TRUE )
22
+ })
23
+ }
24
+
25
+ safeLoadPackage(" foreign" )
26
+ safeLoadPackage(" reshape" )
27
+ safeLoadPackage(" XLConnect" )
28
+ safeLoadPackage(" descr" )
29
+ safeLoadPackage(" Hmisc" )
30
+ safeLoadPackage(" data.table" )
31
+ safeLoadPackage(" plyr" )
22
32
23
33
getScriptDirectory <- function () {
24
34
argv <- commandArgs(trailingOnly = FALSE )
@@ -42,29 +52,6 @@ setScriptDirectory <- function() {
42
52
}
43
53
}
44
54
45
- # this script uses relative paths for sourcing so requires that the current working directory be script directory
46
- setScriptDirectory()
47
-
48
- # Tried to use ArgParse but it screwed up the long filename of the config file
49
- args <- commandArgs(trailingOnly = TRUE )
50
- print(paste(' # of args=' ,length(args ), ' Args:' ))
51
- print(args )
52
-
53
- if (length(args ) == 0 ) {
54
- print(paste(' no arguments passed so using default configuration file.' ))
55
- configuration_file = ' daysim_output_config.R'
56
- } else if (length(args ) == 1 ) {
57
- configuration_file = args [1 ]
58
- print(paste(" configuration_file:" , configuration_file ))
59
- # remove quotes if needed
60
- configuration_file <- gsub(' [\' "]' , ' ' , configuration_file )
61
- print(paste(" configuration_file:" , configuration_file ))
62
- } else {
63
- print(paste(' Unexpected arguments. Expect only path to configuration file but found # of args=' ,length(args ), ' Args:' ))
64
- print(args )
65
- stop()
66
- }
67
-
68
55
sourceAFileInTryCatch <- function (filename ){
69
56
if (! file.exists(filename )) {
70
57
stop(paste(' Expected source file does not exist:' , filename ))
@@ -80,24 +67,105 @@ sourceAFileInTryCatch <- function(filename){
80
67
})
81
68
}
82
69
70
+ getNamedArg <- function (args , arg_name , default ) {
71
+ named_arg = substring(args [grep(arg_name , args )], nchar(arg_name ) + 1 )
72
+ if ((! is.character(named_arg )) | (length(named_arg ) == 0 )) {
73
+ print(paste(" No" , arg_name , " specified. Using default value." ))
74
+ return (default )
75
+ } else {
76
+ return (named_arg )
77
+ }
78
+ }
79
+
83
80
84
81
# ------------------------------------
85
- # Source functions and config settings
82
+ # Source functions and config settings
86
83
# ------------------------------------
87
- # TODO function in package to create template config file in a specified location
88
- sourceAFileInTryCatch(configuration_file )
84
+ # this script uses relative paths for sourcing so requires that the current working directory be script directory
85
+ setScriptDirectory()
86
+
87
+ args <- commandArgs(trailingOnly = TRUE )
88
+ print(paste(' # of args=' ,length(args ), ' Args:' ))
89
+ print(args )
90
+
91
+ # Override defaults if specified in commandArgs
92
+ DAYSIM_REFERENCE_OUTPUTS = getNamedArg(args , " --reference_dir=" , " reference/regress_outputs" )
93
+ DAYSIM_NEW_OUTPUTS = getNamedArg(args , " --outputs_dir=" , " new/regress_outputs" )
94
+ DAYSIM_REPORT_DIRECTORY = getNamedArg(args , " --reports_dir=" , " excel_report_files" )
95
+
96
+ print(paste(" DAYSIM_REFERENCE_OUTPUTS:" , DAYSIM_REFERENCE_OUTPUTS ))
97
+ print(paste(" DAYSIM_NEW_OUTPUTS:" , DAYSIM_NEW_OUTPUTS ))
98
+ print(paste(" DAYSIM_REPORT_DIRECTORY:" , DAYSIM_REPORT_DIRECTORY ))
99
+
100
+ # DaySim Version - DelPhi or C#
101
+ dsVersion = " C#"
102
+
103
+ # daysim outputs
104
+ dshhfile = paste(DAYSIM_NEW_OUTPUTS , " /_household.tsv" , sep = " " )
105
+ dsperfile = paste(DAYSIM_NEW_OUTPUTS , " /_person.tsv" , sep = " " )
106
+ dspdayfile = paste(DAYSIM_NEW_OUTPUTS , " /_person_day.tsv" , sep = " " )
107
+ dstourfile = paste(DAYSIM_NEW_OUTPUTS , " /_tour.tsv" , sep = " " )
108
+ dstripfile = paste(DAYSIM_NEW_OUTPUTS , " /_trip.tsv" , sep = " " )
109
+
110
+ # reference/survey
111
+ surveyhhfile = paste(DAYSIM_REFERENCE_OUTPUTS , " /_household.tsv" , sep = " " )
112
+ surveyperfile = paste(DAYSIM_REFERENCE_OUTPUTS , " /_person.tsv" , sep = " " )
113
+ surveypdayfile = paste(DAYSIM_REFERENCE_OUTPUTS , " /_person_day.tsv" , sep = " " )
114
+ surveytourfile = paste(DAYSIM_REFERENCE_OUTPUTS , " /_tour.tsv" , sep = " " )
115
+ surveytripfile = paste(DAYSIM_REFERENCE_OUTPUTS , " /_trip.tsv" , sep = " " )
116
+
117
+ wrklocmodelfile = " ./model_csv_files/WrkLocation.csv"
118
+ schlocmodelfile = " ./model_csv_files/SchLocation.csv"
119
+ vehavmodelfile = " ./model_csv_files/VehAvailability.csv"
120
+ daypatmodelfile1 = " ./model_csv_files/DayPattern_pday.csv"
121
+ daypatmodelfile2 = " ./model_csv_files/DayPattern_tour.csv"
122
+ daypatmodelfile3 = " ./model_csv_files/DayPattern_trip.csv"
123
+ tourdestmodelfile = " ./model_csv_files/TourDestination.csv"
124
+ tourdestwkbmodelfile = " ./model_csv_files/TourDestination_wkbased.csv"
125
+ tripdestmodelfile = " ./model_csv_files/TripDestination.csv"
126
+ tourmodemodelfile = " ./model_csv_files/TourMode.csv"
127
+ tourtodmodelfile = " ./model_csv_files/TourTOD.csv"
128
+ tripmodemodelfile = " ./model_csv_files/TripMode.csv"
129
+ triptodmodelfile = " ./model_csv_files/TripTOD.csv"
130
+
131
+ wrklocmodelout = " WrkLocation.xlsm"
132
+ schlocmodelout = " SchLocation.xlsm"
133
+ vehavmodelout = " VehAvailability.xlsm"
134
+ daypatmodelout = " DayPattern.xlsm"
135
+ tourdestmodelout = c(" TourDestination_Escort.xlsm" ," TourDestination_PerBus.xlsm" ," TourDestination_Shop.xlsm" ,
136
+ " TourDestination_Meal.xlsm" ," TourDestination_SocRec.xlsm" )
137
+ tourdestwkbmodelout = " TourDestination_WrkBased.xlsm"
138
+ tourmodemodelout = " TourMode.xlsm"
139
+ tourtodmodelout = " TourTOD.xlsm"
140
+ tripmodemodelout = " TripMode.xlsm"
141
+ triptodmodelout = " TripTOD.xlsm"
142
+
143
+ outputsDir = paste(DAYSIM_REPORT_DIRECTORY , " " , sep = " " )
144
+ validationDir = " "
145
+
146
+ prepSurvey = TRUE
147
+ prepDaySim = TRUE
148
+
149
+ runWrkSchLocationChoice = TRUE
150
+ runVehAvailability = TRUE
151
+ runDayPattern = TRUE
152
+ runTourDestination = TRUE
153
+ runTourMode = TRUE
154
+ runTourTOD = TRUE
155
+ runTripMode = TRUE
156
+ runTripTOD = TRUE
89
157
90
- # stop("Finished test")
158
+ excludeChildren5 = TRUE
91
159
92
160
sourceAFileInTryCatch(" utilfunc.R" )
93
161
94
162
progressStart(" run DaySim summaries" ,14 )
95
163
96
164
# -----------------------
97
- # Load data
165
+ # Load data
98
166
# -----------------------
99
167
100
- # Load DaySim outputs into Rdata files
168
+ # Load DaySim outputs into Rdata files
101
169
if (runWrkSchLocationChoice | runVehAvailability | runDayPattern | runTourDestination | runTourMode )
102
170
{
103
171
progressNextStep(" reading hh data" )
@@ -147,7 +215,7 @@ if(runDayPattern | runTripMode | runTripTOD)
147
215
gc()
148
216
149
217
# -----------------------
150
- # Run tabulations
218
+ # Run tabulations
151
219
# -----------------------
152
220
# #TODO split between preparing tables in an R object and then putting them somewhere
153
221
# #TODO e.g. in a spreadsheet, in a pdf report, etc.
@@ -181,17 +249,17 @@ if(runTourDestination)
181
249
}
182
250
if (runTourMode )
183
251
{
184
- progressNextStep(" summarizing Tour Mode Choice" )
252
+ progressNextStep(" summarizing Tour Mode Choice" )
185
253
sourceAFileInTryCatch(" tourmode.R" )
186
254
}
187
255
if (runTourTOD )
188
256
{
189
- progressNextStep(" summarizing Tour Time of Day Choice" )
257
+ progressNextStep(" summarizing Tour Time of Day Choice" )
190
258
sourceAFileInTryCatch(" tourtod.R" )
191
259
}
192
260
if (runTripMode )
193
261
{
194
- progressNextStep(" summarizing Trip Mode Choice" )
262
+ progressNextStep(" summarizing Trip Mode Choice" )
195
263
sourceAFileInTryCatch(" tripmode.R" )
196
264
}
197
265
if (runTripTOD )
@@ -203,4 +271,4 @@ if(runTripTOD)
203
271
progressEnd(outputsDir )
204
272
205
273
# Rprof(NULL)
206
- # memprof <- summaryRprof()
274
+ # memprof <- summaryRprof()
0 commit comments