forked from NIVANorge/Mobius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobius_io.cpp
792 lines (696 loc) · 22.7 KB
/
mobius_io.cpp
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
static void
DlmWriteResultSeriesToFile(mobius_data_set *DataSet, const char *Filename, std::vector<const char *> ResultNames, const std::vector<std::vector<const char *>> &Indexes, char Delimiter)
{
size_t WriteSize = DataSet->TimestepsLastRun;
size_t NumSeries = ResultNames.size();
double **ResultSeries = AllocClearedArray(double *, NumSeries);
for(size_t Idx = 0; Idx < NumSeries; ++Idx)
{
ResultSeries[Idx] = AllocClearedArray(double, WriteSize);
GetResultSeries(DataSet, ResultNames[Idx], Indexes[Idx], ResultSeries[Idx], WriteSize);
}
FILE *file = fopen(Filename, "w");
if(!file)
{
MOBIUS_FATAL_ERROR("Tried to open file " << Filename << ", but were not able to." << std::endl);
}
else
{
for(size_t Timestep = 0; Timestep < WriteSize; ++Timestep)
{
for(size_t Idx = 0; Idx < NumSeries; ++Idx)
{
fprintf(file, "%f", ResultSeries[Idx][Timestep]);
if(Idx < NumSeries-1) fprintf(file, "%c", Delimiter);
}
fprintf(file, "\n");
}
fclose(file);
}
for(size_t Idx = 0; Idx < NumSeries; ++Idx) free(ResultSeries[Idx]);
free(ResultSeries);
}
static void
WriteParameterValue(FILE *File, parameter_value Value, parameter_type Type)
{
switch(Type)
{
case ParameterType_Double:
fprintf(File, "%.15g", Value.ValDouble);
break;
case ParameterType_Bool:
fprintf(File, "%s", Value.ValBool ? "true" : "false");
break;
case ParameterType_UInt:
fprintf(File, "%llu", (unsigned long long)(Value.ValUInt)); //TODO: check correctness. May depend on sizeof(long long unsigned int)==8
break;
case ParameterType_Time:
fprintf(File, "\"%s\"", Value.ValTime.ToString());
break;
}
}
static void
WriteParameterValues(FILE *File, entity_handle ParameterHandle, parameter_type Type, mobius_data_set *DataSet, index_set_h *IndexSets, index_t *Indexes, size_t IndexSetCount, size_t Level)
{
if(IndexSetCount == 0)
{
size_t Offset = OffsetForHandle(DataSet->ParameterStorageStructure, ParameterHandle);
parameter_value Value = DataSet->ParameterData[Offset];
WriteParameterValue(File, Value, Type);
return;
}
index_set_h IndexSet = IndexSets[Level];
size_t IndexCount = DataSet->IndexCounts[IndexSet.Handle];
for(index_t Index = {IndexSet, 0}; Index < IndexCount; ++Index)
{
Indexes[Level] = Index;
if(Level + 1 == IndexSetCount)
{
size_t Offset = OffsetForHandle(DataSet->ParameterStorageStructure, Indexes, IndexSetCount, DataSet->IndexCounts, ParameterHandle);
parameter_value Value = DataSet->ParameterData[Offset];
WriteParameterValue(File, Value, Type);
if(Index + 1 != IndexCount) fprintf(File, " ");
}
else
{
WriteParameterValues(File, ParameterHandle, Type, DataSet, IndexSets, Indexes, IndexSetCount, Level + 1);
if(Index + 1 != IndexCount)
{
if(Level + 2 == IndexSetCount) fprintf(File, "\n");
else fprintf(File, "\n\n");
}
}
}
}
static void
WriteParametersToFile(mobius_data_set *DataSet, const char *Filename)
{
if(!DataSet->ParameterData)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to write parameters to a file before parameter data was allocated." << std::endl);
}
FILE *File = fopen(Filename, "w");
if(!File)
{
MOBIUS_PARTIAL_ERROR("Tried to open file " << Filename << ", but were not able to." << std::endl);
return;
}
const mobius_model *Model = DataSet->Model;
fprintf(File, "# Parameter file generated for %s V%s", Model->Name, Model->Version);
//NOTE: put_time is not implemented before gcc version 5
#if defined(GCC_VERSION) && GCC_VERSION >= 50000
{
auto T = std::time(nullptr);
auto TM = *std::localtime(&T);
std::stringstream Oss;
Oss << std::put_time(&TM, "%Y-%m-%d %H:%M:%S");
fprintf(File, " at %s", Oss.str().data());
}
#endif
fprintf(File, "\n\n");
fprintf(File, "index_sets:\n");
for(entity_handle IndexSetHandle = 1; IndexSetHandle < Model->FirstUnusedIndexSetHandle; ++IndexSetHandle)
{
const index_set_spec &Spec = Model->IndexSetSpecs[IndexSetHandle];
fprintf(File, "\"%s\" : {", Spec.Name);
for(index_t IndexIndex = {IndexSetHandle, 0}; IndexIndex < DataSet->IndexCounts[IndexSetHandle]; ++IndexIndex)
{
if(Spec.Type == IndexSetType_Basic)
{
if(IndexIndex != 0) fprintf(File, " ");
fprintf(File, "\"%s\"", DataSet->IndexNames[IndexSetHandle][IndexIndex]);
}
else if(Spec.Type == IndexSetType_Branched)
{
if(IndexIndex != 0) fprintf(File, " ");
size_t InputCount = DataSet->BranchInputs[IndexSetHandle][IndexIndex].Count;
if(InputCount > 0) fprintf(File, "{");
fprintf(File, "\"%s\"", DataSet->IndexNames[IndexSetHandle][IndexIndex]);
for(size_t InputIdx = 0; InputIdx < InputCount; ++InputIdx)
{
index_t InputIndexIndex = DataSet->BranchInputs[IndexSetHandle][IndexIndex].Inputs[InputIdx];
fprintf(File, " \"%s\"", DataSet->IndexNames[IndexSetHandle][InputIndexIndex]);
}
if(InputCount > 0) fprintf(File, "}");
}
else
{
assert(0);
}
}
fprintf(File, "}\n");
}
fprintf(File, "\nparameters:\n");
for(size_t UnitIndex = 0; UnitIndex < DataSet->ParameterStorageStructure.Units.size(); ++UnitIndex)
{
std::vector<index_set_h> &IndexSets = DataSet->ParameterStorageStructure.Units[UnitIndex].IndexSets;
fprintf(File, "######################");
if(IndexSets.empty()) fprintf(File, " (no index sets)");
for(index_set_h IndexSet : IndexSets)
{
fprintf(File, " \"%s\"", GetName(Model, IndexSet));
}
fprintf(File, " ######################\n");
for(entity_handle ParameterHandle: DataSet->ParameterStorageStructure.Units[UnitIndex].Handles)
{
const parameter_spec &Spec = Model->ParameterSpecs[ParameterHandle];
if(Spec.ShouldNotBeExposed) continue;
fprintf(File, "\"%s\" :", Spec.Name);
bool PrintedPnd = false;
if(IsValid(Spec.Unit))
{
fprintf(File, " #(%s)", GetName(Model, Spec.Unit));
PrintedPnd = true;
}
if(Spec.Type != ParameterType_Bool)
{
if(!PrintedPnd) fprintf(File, " #");
PrintedPnd = true;
fprintf(File, " [");
WriteParameterValue(File, Spec.Min, Spec.Type);
fprintf(File, ", ");
WriteParameterValue(File, Spec.Max, Spec.Type);
fprintf(File, "]");
}
if(Spec.Description)
{
if(!PrintedPnd) fprintf(File, " #");
fprintf(File, " %s", Spec.Description);
}
fprintf(File, "\n");
size_t IndexSetCount = IndexSets.size();
index_t *CurrentIndexes = AllocClearedArray(index_t, IndexSetCount);
WriteParameterValues(File, ParameterHandle, Spec.Type, DataSet, IndexSets.data(), CurrentIndexes, IndexSetCount, 0);
fprintf(File, "\n\n");
free(CurrentIndexes);
}
}
fclose(File);
}
static void
ReadParametersFromFile(mobius_data_set *DataSet, const char *Filename)
{
token_stream Stream(Filename);
const mobius_model *Model = DataSet->Model;
while(true)
{
token Token = Stream.PeekToken();
if(Token.Type == TokenType_EOF)
break;
token_string Section = Stream.ExpectUnquotedString();
Stream.ExpectToken(TokenType_Colon);
int Mode = -1;
if(Section.Equals("index_sets"))
{
Mode = 0;
}
else if(Section.Equals("parameters"))
{
Mode = 1;
}
else
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Parameter file parser does not recognize section type: " << Section << std::endl);
}
if(Mode == 0)
{
while(true)
{
token Token = Stream.PeekToken();
if(Token.Type != TokenType_QuotedString) break;
token_string IndexSetName = Stream.ExpectQuotedString();
index_set_h IndexSet = GetIndexSetHandle(Model, IndexSetName);
Stream.ExpectToken(TokenType_Colon);
if(Model->IndexSetSpecs[IndexSet.Handle].Type == IndexSetType_Basic)
{
std::vector<token_string> Indexes;
Stream.ReadQuotedStringList(Indexes);
SetIndexes(DataSet, IndexSetName, Indexes);
}
else if(Model->IndexSetSpecs[IndexSet.Handle].Type == IndexSetType_Branched)
{
//TODO: Make a helper function for this too!
std::vector<std::pair<token_string, std::vector<token_string>>> Indexes;
Stream.ExpectToken(TokenType_OpenBrace);
while(true)
{
Token = Stream.ReadToken();
if(Token.Type == TokenType_CloseBrace)
{
if(Indexes.empty())
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Expected one or more indexes for index set " << IndexSetName << std::endl);
}
else
{
SetBranchIndexes(DataSet, IndexSetName, Indexes);
}
break;
}
else if(Token.Type == TokenType_QuotedString)
{
Indexes.push_back({Token.StringValue, {}});
}
else if(Token.Type == TokenType_OpenBrace)
{
token_string IndexName;
std::vector<token_string> Inputs;
while(true)
{
Token = Stream.ReadToken();
if(Token.Type == TokenType_CloseBrace)
{
if(!IndexName.Data || Inputs.empty())
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("No inputs in the braced list for one of the indexes of index set " << IndexSetName << std::endl);
}
break;
}
else if(Token.Type == TokenType_QuotedString)
{
if(!IndexName.Data) IndexName = Token.StringValue;
else Inputs.push_back(Token.StringValue);
}
else
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Expected either the quoted name of an index or a }" << std::endl);
}
}
Indexes.push_back({IndexName, Inputs});
}
else
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Expected either the quoted name of an index or a }" << std::endl);
}
}
}
}
}
else if(Mode == 1)
{
while(true)
{
Token = Stream.PeekToken();
if(Token.Type != TokenType_QuotedString) break;
if(!DataSet->ParameterData)
{
AllocateParameterStorage(DataSet);
}
token_string ParameterName = Stream.ExpectQuotedString();
Stream.ExpectToken(TokenType_Colon);
entity_handle ParameterHandle = GetParameterHandle(Model, ParameterName);
const parameter_spec &Spec = Model->ParameterSpecs[ParameterHandle];
if(Spec.ShouldNotBeExposed)
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("The parameter " << ParameterName << " is computed by the model, and should not be provided in a parameter file." << std::endl);
}
parameter_type Type = Spec.Type;
size_t ExpectedCount = 1;
size_t UnitIndex = DataSet->ParameterStorageStructure.UnitForHandle[ParameterHandle];
for(index_set_h IndexSet : DataSet->ParameterStorageStructure.Units[UnitIndex].IndexSets)
{
ExpectedCount *= DataSet->IndexCounts[IndexSet.Handle];
}
//TODO: Check that the values are in the Min-Max range? (issue warning only)
if(Type == ParameterType_Time)
{
//NOTE: Since we can't distinguish date identifiers from quoted string identifiers, we have to handle them separately. Perhaps we should have a separate format for dates so that the parsing could be handled entirely by the lexer??
std::vector<parameter_value> Values;
Values.resize(ExpectedCount);
for(size_t ValIdx = 0; ValIdx < ExpectedCount; ++ValIdx)
{
Values[ValIdx].ValTime = Stream.ExpectDate();
}
SetMultipleValuesForParameter(DataSet, ParameterHandle, Values.data(), Values.size());
}
else
{
std::vector<parameter_value> Values;
Values.reserve(ExpectedCount);
Stream.ReadParameterSeries(Values, Type);
if(Values.size() != ExpectedCount)
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Did not get the expected number of values for " << ParameterName << std::endl);
}
SetMultipleValuesForParameter(DataSet, ParameterHandle, Values.data(), Values.size());
}
}
}
}
}
static void
ReadInputSeries(mobius_data_set *DataSet, token_stream &Stream)
{
const mobius_model *Model = DataSet->Model;
while(true)
{
token Token = Stream.ReadToken();
if(Token.Type == TokenType_EOF)
{
return;
}
if(Token.Type == TokenType_UnquotedString)
{
if(Token.StringValue.Equals("include_file"))
{
token_string Filename = Stream.ExpectQuotedString();
//NOTE: The file path is given relatively to the current file, so we have to add any path of the current one in front.
const char *ParentPath = Stream.Filename;
int LastSlash;
bool AnySlashAtAll = false;
for(LastSlash = strlen(ParentPath) - 1; LastSlash >= 0; --LastSlash)
{
char C = ParentPath[LastSlash];
if(C == '\\' || C == '/')
{
AnySlashAtAll = true;
break;
}
}
if(!AnySlashAtAll) LastSlash = -1;
char NewPath[512]; //Umm, hope this is plenty???
sprintf(NewPath, "%.*s%.*s", LastSlash+1, ParentPath, (int)Filename.Length, Filename.Data);
token_stream SubStream(NewPath);
ReadInputSeries(DataSet, SubStream);
continue;
}
else
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Unexpected command word " << Token.StringValue);
}
}
else if(Token.Type != TokenType_QuotedString)
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Expected the quoted name of an input or an include_file directive" << std::endl);
}
token_string InputName = Token.StringValue;
input_h Input = GetInputHandle(Model, InputName);
std::vector<size_t> Offsets;
while(true)
{
Token = Stream.PeekToken();
if(Token.Type == TokenType_OpenBrace)
{
std::vector<token_string> IndexNames;
Stream.ReadQuotedStringList(IndexNames);
const std::vector<index_set_h> &IndexSets = Model->InputSpecs[Input.Handle].IndexSetDependencies;
if(IndexNames.size() != IndexSets.size())
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Did not get the right amount of indexes for input " << InputName << std::endl);
}
index_t Indexes[256]; //This could cause a buffer overflow, but will not do so in practice.
for(size_t IdxIdx = 0; IdxIdx < IndexNames.size(); ++IdxIdx)
{
Indexes[IdxIdx] = GetIndex(DataSet, IndexSets[IdxIdx], IndexNames[IdxIdx]);
}
size_t Offset = OffsetForHandle(DataSet->InputStorageStructure, Indexes, IndexSets.size(), DataSet->IndexCounts, Input.Handle);
Offsets.push_back(Offset);
}
else
{
break;
}
}
if(Offsets.empty())
{
size_t Offset = OffsetForHandle(DataSet->InputStorageStructure, Input.Handle);
Offsets.push_back(Offset);
}
Stream.ExpectToken(TokenType_Colon);
for(size_t Offset : Offsets)
{
DataSet->InputTimeseriesWasProvided[Offset] = true;
}
//NOTE: This reads the actual data after the header.
//NOTE: For the first timestep, try to figure out what format the data was provided in.
int FormatType = -1;
Token = Stream.PeekToken();
u64 Timesteps = DataSet->InputDataTimesteps;
if(Token.Type == TokenType_Numeric)
{
FormatType = 0;
}
else if(Token.Type == TokenType_QuotedString)
{
FormatType = 1;
//NOTE: For this format, the default value is NaN (signifying a missing value).
for(size_t Offset : Offsets)
{
double *WriteTo = DataSet->InputData + Offset;
for(u64 Timestep = 0; Timestep < Timesteps; ++ Timestep)
{
*WriteTo = std::numeric_limits<double>::quiet_NaN();
WriteTo += DataSet->InputStorageStructure.TotalCount;
}
}
}
else
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Inputs are to be provided either as a series of numbers or a series of dates together with numbers" << std::endl);
}
if(FormatType == 0)
{
for(u64 Timestep = 0; Timestep < Timesteps; ++Timestep)
{
double Value = Stream.ExpectDouble();
for(size_t Offset : Offsets)
{
double *WriteTo = DataSet->InputData + Offset + Timestep*DataSet->InputStorageStructure.TotalCount;
*WriteTo = Value;
}
}
}
else //FormatType == 1
{
datetime StartDate = DataSet->InputDataStartDate;
while(true)
{
s64 CurTimestep;
token Token = Stream.PeekToken();
if(Token.Type == TokenType_QuotedString)
{
datetime Date = Stream.ExpectDate();
CurTimestep = StartDate.DaysUntil(Date); //NOTE: Only one-day timesteps currently supported.
}
else if(Token.Type == TokenType_UnquotedString)
{
if(Token.StringValue.Equals("end_timeseries"))
{
Stream.ReadToken(); //NOTE: Consume it.
break;
}
else
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Unexpected command word: " << Token.StringValue << std::endl);
}
}
else
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Expected either a date (as a quoted string) or the command word end_timeseries." << std::endl);
}
Token = Stream.PeekToken();
if(Token.Type == TokenType_UnquotedString)
{
Stream.ReadToken();
datetime EndDateRange = Stream.ExpectDate();
s64 EndTimestepRange = StartDate.DaysUntil(EndDateRange); //NOTE: Only one-day timesteps currently supported.
if(EndTimestepRange < CurTimestep)
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("The end of the date range is earlier than the beginning.");
}
double Value = Stream.ExpectDouble();
for(s64 Timestep = CurTimestep; Timestep <= EndTimestepRange; ++Timestep)
{
if(Timestep >= 0 && Timestep < (s64)Timesteps)
{
for(size_t Offset : Offsets)
{
double *WriteTo = DataSet->InputData + Offset + Timestep*DataSet->InputStorageStructure.TotalCount;
*WriteTo = Value;
}
}
else
{
//NOTE: Should we log if this happens and print a warning?
}
}
}
else if(Token.Type == TokenType_Numeric)
{
double Value = Stream.ExpectDouble();
if(CurTimestep >= 0 && CurTimestep < (s64)Timesteps)
{
for(size_t Offset : Offsets)
{
double *WriteTo = DataSet->InputData + Offset + CurTimestep*DataSet->InputStorageStructure.TotalCount;
*WriteTo = Value;
}
}
else
{
//NOTE: Should we log if this happens and print a warning?
}
}
else
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Expected either a 'to' or a number.");
}
}
}
}
}
static void
ReadInputsFromFile(mobius_data_set *DataSet, const char *Filename)
{
const mobius_model *Model = DataSet->Model;
token_stream Stream(Filename);
u64 Timesteps = 0;
while(true)
{
token Token = Stream.PeekToken();
if(Token.Type == TokenType_EOF)
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Expected one of the code words timesteps, start_date, inputs, additional_timeseries or index_set_dependencies" << std::endl);
}
token_string Section = Stream.ExpectUnquotedString();
if(Section.Equals("timesteps"))
{
Stream.ExpectToken(TokenType_Colon);
Timesteps = Stream.ExpectUInt();
}
else if(Section.Equals("start_date"))
{
Stream.ExpectToken(TokenType_Colon);
DataSet->InputDataStartDate = Stream.ExpectDate();
DataSet->InputDataHasSeparateStartDate = true;
}
else if(Section.Equals("inputs"))
{
Stream.ExpectToken(TokenType_Colon);
break;
}
else if(Section.Equals("index_set_dependencies") || Section.Equals("additional_timeseries"))
{
//NOTE: These are handled in a separate call, so we have to skip through them here.
while(true)
{
Token = Stream.PeekToken();
if(Token.Type == TokenType_UnquotedString && !Token.StringValue.Equals("unit")) break; //We hit a new section;
Stream.ReadToken(); //Otherwise consume the token and ignore it.
}
}
else
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Input file parser does not recognize section type: " << Section << "." << std::endl);
}
}
if(Timesteps == 0)
{
MOBIUS_FATAL_ERROR("ERROR: Timesteps in the input file " << Filename << " is set to 0." << std::endl);
}
AllocateInputStorage(DataSet, Timesteps);
if(!DataSet->InputDataHasSeparateStartDate)
{
DataSet->InputDataStartDate = GetStartDate(DataSet); //NOTE: This reads the "Start date" parameter.
}
ReadInputSeries(DataSet, Stream);
}
static void
ReadInputDependenciesFromFile(mobius_model *Model, const char *Filename)
{
token_stream Stream(Filename);
while(true)
{
token Token = Stream.PeekToken();
if(Token.Type == TokenType_EOF)
break;
token_string Section = Stream.ExpectUnquotedString();
Stream.ExpectToken(TokenType_Colon);
if(Section.Equals("index_set_dependencies"))
{
while(true)
{
token Token = Stream.PeekToken();
if(Token.Type == TokenType_QuotedString)
{
Token = Stream.ReadToken();
token_string InputName = Token.StringValue;
input_h Input = GetInputHandle(Model, InputName);
std::vector<index_set_h> &IndexSets = Model->InputSpecs[Input.Handle].IndexSetDependencies;
if(!IndexSets.empty()) //TODO: OR we could just clear it and give a warning..
{
Stream.PrintErrorHeader();
MOBIUS_FATAL_ERROR("Tried to set index set dependencies for the input " << InputName << " for a second time." << std::endl);
}
Stream.ExpectToken(TokenType_Colon);
std::vector<token_string> IndexSetNames;
Stream.ReadQuotedStringList(IndexSetNames);
for(token_string IndexSetName : IndexSetNames)
{
IndexSets.push_back(GetIndexSetHandle(Model, IndexSetName));
}
}
else break;
}
}
else if(Section.Equals("additional_timeseries"))
{
while(true)
{
token Token = Stream.PeekToken();
if(Token.Type == TokenType_QuotedString)
{
Token = Stream.ReadToken();
token_string InputName = Token.StringValue.Copy(); //TODO: Leaks.
unit_h Unit = {0};
Token = Stream.PeekToken();
if(Token.Type == TokenType_UnquotedString)
{
if(Token.StringValue.Equals("unit"))
{
Stream.ReadToken();
token_string UnitName = Stream.ExpectQuotedString().Copy(); //TODO: Leaks.
Unit = RegisterUnit(Model, UnitName.Data);
}
}
RegisterInput(Model, InputName.Data, Unit, true);
}
else break;
}
}
else if(Section.Equals("inputs"))
{
//NOTE: "index_set_dependencies" and "additional_timeseries" are assumed to come before "inputs" in the file.
// This is so that we don't have to skip through the entire inputs section on this read since it can be quite long.
break;
}
else
{
//NOTE: We have to skip through other sections that are not relevant for this reading
while(true)
{
Token = Stream.PeekToken();
if(Token.Type == TokenType_EOF) return;
if(Token.Type == TokenType_UnquotedString) break; //We hit a new section;
Stream.ReadToken(); //Otherwise consume the token and ignore it.
}
}
}
}