-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
351 lines (312 loc) · 10.6 KB
/
path.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
package hermes
import (
"bufio"
"fmt"
"log"
"net/rpc"
"os"
"path"
"strconv"
"strings"
"sync"
)
// HermesFilePool file pool for shared files
var HermesFilePool FilePool
var HermesRPCService RPCService
// Modfil default module filename
const Modfil = "modinp.txt"
// HFilePath list of hermes file pathes and path template
type HFilePath struct {
path string
locid string
parameter string
outputfolder string
config string
//enam string // configuration file // daily output for single polygone
vnam string // daily output for single polygone
// tnam string // output PEST
// tnnam string // other output PEST
pfnam string // output ground temperature
pnam string // output yearly
mnam string // output management
bofile string // soil file e.g soil_<project>.txt
polnam string // polygon file e.g poly_<project>.txt
irrigation string // irrigation file
crop string // crop file
obs string // observations file
til string // tillage times file
dun string // fertilization times file
fert string // output fertilization suggestion
auto string // automated processes file
gwtimeseries string // groundwater timeseries file
hypar string
precorr string
cropn string
evapo string
parcap string
dung string
vwdatnrm string
paranamTemplate string
paranamVarietyTemplate string
bofileTemplate string
polnamTemplate string
vwdatTemplate string
vwdatNoExt string
yearlyOutput string
dailyOutput string
cropOutput string
pfOutput string
cnam string
managementOutput string
}
// NewHermesFilePath create an initialized HermesFilePath struct
func NewHermesFilePath(root, locid, uniqueOutputId, parameterOverride, resultOverride string) HFilePath {
pathToProject := path.Join(root, "project", locid)
parameter := path.Join(root, "parameter")
if len(parameterOverride) > 0 {
parameter = path.Join(root, parameterOverride)
}
var out string
if len(resultOverride) > 0 {
out = resultOverride
} else {
out = path.Join(pathToProject, "RESULT")
}
return HFilePath{
locid: locid, // location id, equals project folder name
path: pathToProject, // project folder
parameter: parameter, // parameter folder
outputfolder: out, // output folder
// project input files
irrigation: path.Join(pathToProject, "irr_"+locid+".txt"), // irrigation file
crop: path.Join(pathToProject, "crop_"+locid+".txt"), // crop file
obs: path.Join(pathToProject, "init_"+locid+".txt"), // observations file
til: path.Join(pathToProject, "til_"+locid+".txt"), // tillage times file
dun: path.Join(pathToProject, "fert_"+locid+".txt"), // fertilization times file
auto: path.Join(pathToProject, "automan.txt"), // automated processes file
gwtimeseries: path.Join(pathToProject, "gw_"+locid+".csv"), // groundwater timeseries file
precorr: path.Join(pathToProject, "Weather", "preco.txt"), // weather precorrection file
bofileTemplate: path.Join(pathToProject, "%s_"+locid+".%s"), // soil file e.g soil_<project>.txt
polnamTemplate: path.Join(pathToProject, "%s_"+locid+".txt"), // polygon file e.g poly_<project>.txt
vwdatTemplate: path.Join(pathToProject, "Weather", "%s_"+locid+"."), // weather data file
config: path.Join(pathToProject, "config.yml"), // configuration file
yearlyOutput: path.Join(pathToProject, "yearlyout_conf.yml"), // yearly output configuration
dailyOutput: path.Join(pathToProject, "dailyout_conf.yml"), // daily output configuration
cropOutput: path.Join(pathToProject, "cropout_conf.yml"), // crop output configuration
pfOutput: path.Join(pathToProject, "pfout_conf.yml"), // harvest output configuration
managementOutput: path.Join(pathToProject, "managementout_conf.yml"), // management output configuration
// output files
vnam: path.Join(out, "V"+uniqueOutputId+".%s"), // daily output template for single polygone
pfnam: path.Join(out, "P"+uniqueOutputId+".%s"), // harvest output template
pnam: path.Join(out, "Y"+uniqueOutputId+".%s"), // output yearly template
cnam: path.Join(out, "C"+uniqueOutputId+".%s"), // output crop template
mnam: path.Join(out, "M"+uniqueOutputId+".txt"), // management output
fert: path.Join(out, "D"+uniqueOutputId+".txt"), // output fertilization suggestion
// parameter files
parcap: path.Join(parameter, "PARCAP.TRU"), // capacity parameters
hypar: path.Join(parameter, "HYPAR.TRU"), // hydraulic parameters
evapo: path.Join(parameter, "EVAPO.HAU"), // evaporation parameters
cropn: path.Join(parameter, "CROP_N.TXT"), // crop parameters
dung: path.Join(parameter, "FERTILIZ.TXT"), // fertilization parameters
paranamTemplate: path.Join(parameter, "PARAM.%s"), // parameter file for crop
paranamVarietyTemplate: path.Join(parameter, "PARAM_%s.%s"), // parameter file for crop variety
}
}
// set extension for output files (e.g. res, csv, txt, ...)
func (hp *HFilePath) SetOutputExtension(ext string) {
hp.vnam = fmt.Sprintf(hp.vnam, ext)
hp.pfnam = fmt.Sprintf(hp.pfnam, ext)
hp.cnam = fmt.Sprintf(hp.cnam, ext)
hp.pnam = fmt.Sprintf(hp.pnam, ext)
}
// SetBofile completes bofile filename
func (hp *HFilePath) SetBofile(prefix, extension string) {
hp.bofile = fmt.Sprintf(hp.bofileTemplate, strings.TrimSpace(prefix), strings.TrimSpace(extension))
}
// OverrideBofile overrides the complete bofile filename
func (hp *HFilePath) OverrideBofile(newPath string) {
hp.bofile = newPath
}
// SetPolnam completes polnam filename
func (hp *HFilePath) SetPolnam(ins string) {
hp.polnam = fmt.Sprintf(hp.polnamTemplate, strings.TrimSpace(ins))
}
// SetVwdatNoExt completes weather data filename, without extension
func (hp *HFilePath) SetVwdatNoExt(ins string) {
hp.vwdatNoExt = fmt.Sprintf(hp.vwdatTemplate, strings.TrimSpace(ins))
hp.vwdatnrm = hp.vwdatNoExt + "nrm"
}
func (hp *HFilePath) SetPreCorrFolder(folder string) {
hp.precorr = path.Join(folder, "preco.txt")
}
// GetParanam returns the full filename for the choosen fruit
func (hp *HFilePath) GetParanam(fruit, variety string, yml bool) string {
var filename string
if len(variety) > 0 {
filename = fmt.Sprintf(hp.paranamVarietyTemplate, variety, fruit)
} else {
filename = fmt.Sprintf(hp.paranamTemplate, fruit)
}
if yml {
filename = filename + ".yml"
}
return filename
}
// VWdat returns weather data file with the correct extension for a year
func (hp *HFilePath) VWdat(year int) string {
return hp.vwdatNoExt + yearToExtension(year)
}
func yearToExtension(year int) string {
var EXTENT string
strYear := strconv.Itoa(year)
// IF j >= 100 then
if year >= 100 {
// LET EXTENT$ = "0"&STR$(j)(2:3)
EXTENT = "0" + strYear[1:3]
// ELSE
} else {
// LET extent$ = "9"& STR$(j)
EXTENT = "9" + strYear
// END IF
}
return EXTENT
}
// FilePool to store filepointer by path
type FilePool struct {
list map[string][]byte
mux sync.Mutex
}
// Get file content from the filepool
func (fp *FilePool) Get(fd *FileDescriptior) []byte {
fp.mux.Lock()
if fp.list == nil {
fp.list = make(map[string][]byte)
}
if _, ok := fp.list[fd.FilePath]; !ok {
data, err := os.ReadFile(fd.FilePath)
if err != nil {
if fd.FileDescription != "" {
log.Fatalf("Error occured while reading %s: %s \n", fd.FileDescription, fd.FilePath)
} else {
log.Fatalf("Error occured while reading: %s \n", fd.FilePath)
}
}
// if fd.debugOut != nil {
// fd.debugOut <- fmt.Sprintf("%s %s", fd.logID, fd.filePath)
// }
fp.list[fd.FilePath] = data
}
defer fp.mux.Unlock()
return fp.list[fd.FilePath]
}
// Close clears the filepool
func (fp *FilePool) Close() {
fp.mux.Lock()
fp.list = nil
fp.mux.Unlock()
}
// Fout bufferd file writer
type Fout struct {
file *os.File
fwriter *bufio.Writer
}
// Write string to bufferd file
func (f *Fout) Write(s string) (int, error) {
return f.fwriter.WriteString(s)
}
// WriteBytes writes a bufferd byte array
func (f *Fout) WriteBytes(s []byte) (int, error) {
return f.fwriter.Write(s)
}
// WriteRune writes a bufferd rune
func (f *Fout) WriteRune(s rune) (int, error) {
return f.fwriter.WriteRune(s)
}
// Close file writer
func (f *Fout) Close() {
err := f.fwriter.Flush()
if err != nil {
log.Fatalln(err)
}
err = f.file.Close()
if err != nil {
log.Fatalln(err)
}
}
type RPCService struct {
address string // "localhost:8081"
client *rpc.Client
}
func NewRPCService(address string) (RPCService, error) {
client, err := rpc.Dial("tcp", address)
if err != nil {
return RPCService{}, err
}
return RPCService{address: address, client: client}, nil
}
type TransferEnvGlobal struct {
Global GlobalVarsMain
Zeit int
Wdt float64
Step int
}
type TransferEnvNitro struct {
Nitro NitroSharedVars
Zeit int
Wdt float64
Step int
}
type TransferEnvWdt struct {
Zeit int
WDT float64
N int
WG [3][21]float64
W [21]float64
DZ float64
REGEN float64
}
func (rs *RPCService) SendWdt(g *GlobalVarsMain, zeit int, wdt float64) error {
if rs.client != nil {
wdtData := TransferEnvWdt{
Zeit: zeit,
WDT: wdt,
N: g.N,
WG: g.WG,
W: g.W,
DZ: g.DZ.Num,
REGEN: g.REGEN[g.TAG.Index],
}
if err := rs.client.Call("RPCHandler.DumpWdtCalc", wdtData, nil); err != nil {
return fmt.Errorf("DumpWdtCalc %+v", err)
}
}
return nil
}
func (rs *RPCService) SendGV(g *GlobalVarsMain, zeit int, wdt float64, step int) error {
if rs.client != nil {
glob := TransferEnvGlobal{
Global: *g,
Zeit: zeit,
Wdt: wdt,
Step: step,
}
if err := rs.client.Call("RPCHandler.DumpGlobalVar", glob, nil); err != nil {
return fmt.Errorf("DumpGlobalVar %+v", err)
}
}
return nil
}
func (rs *RPCService) SendNV(n *NitroSharedVars, zeit int, wdt float64, step int) error {
if rs.client != nil {
nitro := TransferEnvNitro{
Nitro: *n,
Zeit: zeit,
Wdt: wdt,
Step: step,
}
if err := rs.client.Call("RPCHandler.DumpNitroVar", nitro, nil); err != nil {
return fmt.Errorf("DumpNitroVar %+v", err)
}
}
return nil
}