-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
380 lines (343 loc) · 14.3 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package hermes
import (
"log"
"os"
"reflect"
"strconv"
yaml "gopkg.in/yaml.v3"
)
type Config struct {
//***** Formats *****
// DateDEshort ddmmyy <- default format(old)
// DateDElong ddmmyyyy
// DateENshort mmddyy
// DateENlong mmddyyyy
// short format "ddmmyy", e.g. 24.01.95 -> "ddmmyy" requires input as 240195, you need to set the century devision year "DivideCentury", e.g. 1950 -> 50
// long format "ddmmyyyy", e.g. 24.01.2066 requires input as 24012066
Dateformat DateFormat `yaml:"Dateformat"`
DivideCentury int `yaml:"DivideCentury,omitempty"` // (depends on Date format) Year to divide 20. and 21. Century (YY)
GroundWaterFrom GroundWaterFrom `yaml:"GroundWaterFrom"` // ground water from 'soilfile'=static, 'polygonfile'=dynamic High/Low, 'gwTimeSeries'= measured time series
ResultFileFormat int `yaml:"ResultFileFormat,omitempty"` // result file format (0= hermes default, 1 = csv)
ResultFileExt string `yaml:"ResultFileExt,omitempty"` // result file extensions (default RES, csv)
OutputIntervall int `yaml:"OutputIntervall"` // Output intervall (days) (0=no time serie)
ManagementEvents int `yaml:"ManagementEvents"` // Management events (0=no management events)
InitSelection int `yaml:"InitSelection"` // Init.values all(1),Field_ID(2), Polyg(3) SoilID(4)-> POLY_XXX.txt, Uses: 1= all (if the word ALLE is written in the file), 2= Field_ID, 3= Polyg 4= SoilID
SoilFile string `yaml:"SoilFile"` // soil profile file name (without projectname)
SoilFileExtension string `yaml:"SoilFileExtension"` // soil file extension (txt = hermes soil, csv = csv table format)
CropFileFormat string `yaml:"CropFileFormat"` // crop file format (txt = hermes crop, csv = csv table format)
CropParameterFormat string `yaml:"CropParameterFormat"` // crop parameter file format (txt = hermes crop, yaml = yaml format)
MeasurementFileFormat string `yaml:"MeasurementFileFormat"` // Measurement file (endit_*.) format (txt = old Hermes, csv = csv table format)
PolygonGridFileName string `yaml:"PolygonGridFileName"` // Name of Polygon resp. grid file
//***** Weather *****
WeatherFile string `yaml:"WeatherFile"` // weather file name template (without projectname)
WeatherFileFormat int `yaml:"WeatherFileFormat"` // Weather file format (0=separator(, ; \t), 1 year per file ) (1=separator(, ; \t), multiple years per file, (1=separator(, ; \t ' '), cz format, multiple years per file)
WeatherFolder string `yaml:"WeatherFolder"` // Weather scenario folder
WeatherRootFolder string `yaml:"WeatherRootFolder"` // weather root directory without scenario folder or filename
WeatherNoneValue float64 `yaml:"WeatherNoneValue"` // weather none value, default -99.9
WeatherNumHeader int `yaml:"WeatherNumHeader"` // number of header lines (min = 1, with column names)
CorrectionPrecipitation FeatureSwitch `yaml:"CorrectionPrecipitation"` // correction precipitation (0= no, 1 = yes)
AnnualAverageTemperature float64 `yaml:"AnnualAverageTemperature"` // annual average temperature (Celsius)
//***** Atmosphere *****
ETpot int `yaml:"ETpot"` // ETpot method(1=Haude,2=Turc-Wendling,3 Penman-Monteith)
CO2method int `yaml:"CO2method"` // CO2method(1=Nonhebel,2=Hoffmann,3=Mitchell)
CO2concentration float64 `yaml:"CO2concentration"` // CO2 concentration (ppm)
CO2StomataInfluence FeatureSwitch `yaml:"CO2StomataInfluence"` // CO2 Stomata influence (1=on/0= off)
NDeposition float64 `yaml:"NDeposition"` // N-Deposition (annual kg/ha)
//***** Time *****
StartYear int `yaml:"StartYear"` // Starting year of simulation (YYYY)
EndDate string `yaml:"EndDate"` // End date of simulation (DDMMYYYY)
AnnualOutputDate string `yaml:"AnnualOutputDate"` // Date for annual output
VirtualDateFertilizerPrediction string `yaml:"VirtualDateFertilizerPrediction"` // Virtual date for fertilizer prediction; '------' for no prediction
//***** Geoography *****
Latitude float64 `yaml:"Latitude"` // Latitude
Altitude float64 `yaml:"Altitude"` // Altitude - height (can be overwritten weather file)
CoastDistance float64 `yaml:"CoastDistance"` // Distance to coast (km)
//***** Soil *****
PTF int `yaml:"PTF"` // PTF pedo transfer function (0 = none (from file), 1 = Toth 2015, 2 = Batjes for pF 2.5, 3 = Batjes for pF 1.7, 4 = Rawls et al. 2003 for pF 2.5 )
LeachingDepth int `yaml:"LeachingDepth"` // Depth for leaching/seepage calculation (dm)
OrganicMatterMineralProportion float64 `yaml:"OrganicMatterMineralProportion"` // Mineralisable proportion of organic matter
KcFactorBareSoil float64 `yaml:"KcFactorBareSoil"` // kc factor for bare soil
PotMineralisation int `yaml:"PotMineralisation,omitempty"` // Potential mineralisation method (0= standard, 1 = considers bulk density, 2 = considers bulk density)
GroundWaterPhase int `yaml:"GroundWaterPhase,omitempty"` // Ground water phase in days (80= standard)
//***** Management *****
Fertilization float64 `yaml:"Fertilization"` // fertilization scenario (fertilization in %)
AutoSowingHarvest FeatureSwitch `yaml:"AutoSowingHarvest"` // automatic sowing/harvest (0=no, 1 = yes)
AutoFertilization FeatureSwitch `yaml:"AutoFertilization"` // automatic fertilization (0=no, 1=on demand)
AutoIrrigation FeatureSwitch `yaml:"AutoIrrigation"` // automatic irrigation (0=no, 1= on demand)
AutoHarvest FeatureSwitch `yaml:"AutoHarvest"` // automatic harvest (0=no, 1= on demand)
}
func readConfig(g *GlobalVarsMain, argValues map[string]string, hp *HFilePath) Config {
hconfig := NewDefaultConfig()
// if config files exists, read it into hconfig
if _, err := os.Stat(hp.config); err == nil {
byteData := HermesFilePool.Get(&FileDescriptior{FilePath: hp.config, ContinueOnError: true, UseFilePool: true})
err := yaml.Unmarshal(byteData, &hconfig)
if err != nil {
log.Fatalf("error: %v", err)
}
} else {
// no config exist, generate default config (if project is not fitting default setup, execution will fail)
hconfig = NewDefaultConfig()
}
err := commandlineOverride(argValues, &hconfig)
if err != nil {
log.Fatalf("error while parsing commandline: %v", err)
}
g.GROUNDWATERFROM = hconfig.GroundWaterFrom
g.DATEFORMAT = hconfig.Dateformat
g.ANJAHR = hconfig.StartYear
g.INIWAHL = hconfig.InitSelection
g.PRECO = bool(hconfig.CorrectionPrecipitation)
g.CO2METH = hconfig.CO2method
g.CO2KONZ = hconfig.CO2concentration
g.CTRANS = bool(hconfig.CO2StomataInfluence)
g.ETMETH = hconfig.ETpot
g.LAT = hconfig.Latitude
g.ALTI = hconfig.Altitude
g.OUTN = hconfig.LeachingDepth
g.NAKT = hconfig.OrganicMatterMineralProportion
g.DEPOS = hconfig.NDeposition
g.DUNGSZEN = hconfig.Fertilization / 100
g.Datum = DateConverter(hconfig.DivideCentury, g.DATEFORMAT)
g.Kalender = KalenderConverter(g.DATEFORMAT, ".")
g.LangTag = LangTagConverter(hconfig.DivideCentury, g.DATEFORMAT)
_, g.ENDE = g.Datum(hconfig.EndDate)
g.FKB = hconfig.KcFactorBareSoil
g.TBASE = hconfig.AnnualAverageTemperature
g.AUTOMAN = bool(hconfig.AutoSowingHarvest)
g.AUTOFERT = bool(hconfig.AutoFertilization)
g.AUTOIRRI = bool(hconfig.AutoIrrigation)
g.AUTOHAR = bool(hconfig.AutoHarvest)
g.PTF = hconfig.PTF
g.GWPhase = hconfig.GroundWaterPhase
if len(hconfig.WeatherFolder) == 0 {
hconfig.WeatherFolder = "Weather"
}
if len(hconfig.WeatherRootFolder) == 0 {
hconfig.WeatherRootFolder = hp.path
}
if len(hconfig.ResultFileExt) == 0 {
if OutputFileFormat(hconfig.ResultFileFormat) == csvOut {
hconfig.ResultFileExt = "csv"
} else {
hconfig.ResultFileExt = "RES"
}
}
g.PotMineralisationMethod = hconfig.PotMineralisation
return hconfig
}
// commandlineOverride will parse through the commandline and try to parse them into config
func commandlineOverride(argValues map[string]string, hconfig *Config) error {
if argValues != nil {
v := reflect.ValueOf(hconfig)
if v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
for argKey, argVal := range argValues {
f := v.FieldByName(argKey)
if f.IsValid() {
if f.CanSet() {
if f.Kind() == reflect.Float64 {
newRefVal, err := strconv.ParseFloat(argVal, 64)
if err != nil {
return err
}
if !f.OverflowFloat(newRefVal) {
f.SetFloat(newRefVal)
}
}
if f.Kind() == reflect.Int {
newRefVal, err := strconv.ParseInt(argVal, 10, 64)
if err != nil {
return err
}
if !f.OverflowInt(newRefVal) {
f.SetInt(newRefVal)
}
}
if f.Kind() == reflect.String {
f.SetString(argVal)
}
if f.Kind() == reflect.Bool {
if fs, ok := featureSwitchStrToID[argVal]; ok {
f.SetBool(bool(fs))
}
}
}
}
}
}
return nil
}
// WriteYamlConfig write a default config file
func WriteYamlConfig(filename string, structIn interface{}) {
file := OpenResultFile(filename, false)
defer file.Close()
data, err := yaml.Marshal(structIn)
if err != nil {
log.Fatalf("error: %v", err)
}
if _, err := file.WriteBytes(data); err != nil {
log.Fatal(err)
}
}
// NewDefaultConfig creates a config file with default setup
func NewDefaultConfig() Config {
return Config{
Dateformat: DateDElong,
DivideCentury: 0,
GroundWaterFrom: Soilfile,
ResultFileFormat: 0,
ResultFileExt: "",
OutputIntervall: 0,
ManagementEvents: 0,
InitSelection: 3,
SoilFile: "soil",
SoilFileExtension: "txt",
CropFileFormat: "txt",
CropParameterFormat: "txt",
MeasurementFileFormat: "txt",
PolygonGridFileName: "poly",
WeatherFile: "%s.csv",
WeatherFileFormat: 1,
WeatherFolder: "Weather",
WeatherRootFolder: "",
WeatherNoneValue: -99.9,
WeatherNumHeader: 2,
CorrectionPrecipitation: false,
AnnualAverageTemperature: 8.7,
ETpot: 3,
CO2method: 2,
CO2concentration: 360,
CO2StomataInfluence: true,
NDeposition: 20,
StartYear: 1980,
EndDate: "31122010",
AnnualOutputDate: "3009",
VirtualDateFertilizerPrediction: "--------",
Latitude: 52.52,
Altitude: 0,
CoastDistance: 300,
PTF: 0,
LeachingDepth: 15,
OrganicMatterMineralProportion: 0.13,
KcFactorBareSoil: 0.4,
PotMineralisation: 0,
GroundWaterPhase: 80,
Fertilization: 100,
AutoSowingHarvest: true,
AutoFertilization: true,
AutoIrrigation: true,
AutoHarvest: true,
}
}
// GroundWaterFrom enum from where to load ground water data
type GroundWaterFrom int
const (
// Polygonfile load from polygon file
Polygonfile GroundWaterFrom = iota
// Soilfile load from soil file
Soilfile
// Ground water time series file
GWTimeSeries
)
func (s GroundWaterFrom) String() string {
return toString[s]
}
var toString = map[GroundWaterFrom]string{
Polygonfile: "polygonfile",
Soilfile: "soilfile",
GWTimeSeries: "gwTimeSeries",
}
var toID = map[string]GroundWaterFrom{
"polygonfile": Polygonfile,
"soilfile": Soilfile,
"gwTimeSeries": GWTimeSeries,
}
// MarshalYAML implement YAML Marshaler
func (s GroundWaterFrom) MarshalYAML() (interface{}, error) {
return toString[s], nil
}
// UnmarshalYAML implement YAML Unmarshaler interface
func (s *GroundWaterFrom) UnmarshalYAML(unmarshal func(interface{}) error) error {
var j string
err := unmarshal(&j)
if err != nil {
return err
}
// if the string cannot be found, it will be set to 'soilfile'
*s = toID[j]
return nil
}
// FeatureSwitch to convert 1/0 on/off to true/false
type FeatureSwitch bool
var featureSwitchToString = map[FeatureSwitch]int{
true: 1,
false: 0,
}
var featureSwitchStrToID = map[string]FeatureSwitch{
"1": true,
"0": false,
"on": true,
"off": false,
"yes": true,
"no": false,
"true": true,
"false": false,
}
// MarshalYAML implement YAML Marshaler
func (s FeatureSwitch) MarshalYAML() (interface{}, error) {
return featureSwitchToString[s], nil
}
// UnmarshalYAML implement YAML Unmarshaler interface
func (s *FeatureSwitch) UnmarshalYAML(unmarshal func(interface{}) error) error {
var j string
err := unmarshal(&j)
if err != nil {
return err
}
*s = featureSwitchStrToID[j]
return nil
}
// DateFormat - default date format for all hermes input
type DateFormat int
const (
// DateDEshort ddmmyy
DateDEshort DateFormat = iota
// DateDElong ddmmyyyy
DateDElong
// DateENshort mmddyy
DateENshort
// DateENlong mmddyyyy
DateENlong
)
func (s DateFormat) String() string {
return dateFToString[s]
}
var dateFToString = map[DateFormat]string{
DateDEshort: "DateDEshort",
DateDElong: "DateDElong",
DateENshort: "DateENshort",
DateENlong: "DateENlong",
}
var dateStrToID = map[string]DateFormat{
"DateDEshort": DateDEshort,
"DateDElong": DateDElong,
"DateENshort": DateENshort,
"DateENlong": DateENlong,
}
// MarshalYAML implement YAML Marshaler
func (s DateFormat) MarshalYAML() (interface{}, error) {
return dateFToString[s], nil
}
// UnmarshalYAML implement YAML Unmarshaler interface
func (s *DateFormat) UnmarshalYAML(unmarshal func(interface{}) error) error {
var j string
err := unmarshal(&j)
if err != nil {
return err
}
// if the string cannot be found, it will be set to 'soilfile'
*s = dateStrToID[j]
return nil
}