File: plotGraphLayer.cpp

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (1303 lines) | stat: -rw-r--r-- 44,994 bytes parent folder | download
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
/***********************************************/
/**
* @file plotGraphLayer.cpp
*
* @brief Lines, points and polygons in 2d plots.
*
* @author Andreas Kvas
* @author Torsten Mayer-Guerr
* @date 2016-07-23
*
*/
/***********************************************/

#define DOCSTRING_PlotGraphLayer

#include "base/import.h"
#include "parser/stringParser.h"
#include "parser/dataVariables.h"
#include "config/configRegister.h"
#include "inputOutput/logging.h"
#include "inputOutput/file.h"
#include "inputOutput/system.h"
#include "files/fileMatrix.h"
#include "classes/gravityfield/gravityfield.h"
#include "plot/plotMisc.h"
#include "plotGraphLayer.h"

/***********************************************/

static const char *docstringPlotGraphLayerLinesAndPoints = R"(
\subsection{LinesAndPoints}\label{plotGraphLayerType:linesAndPoints}
Draws a \configClass{line}{plotLineType} and/or points (\configClass{symbol}{plotSymbolType})
of xy data. The standard \reference{dataVariables}{general.parser:dataVariables}
are available to select the data columns of \configFile{inputfileMatrix}{matrix}.
If no \configClass{color}{plotColorType} of the \configClass{symbol}{plotSymbolType}
is given a \configClass{colorbar}{plotColorbarType}
is required and the color is determined by \config{valueZ}.
Additionally a vertical error bar can be plotted at each data point with
size \config{valueErrorBar}.

See \program{Gravityfield2AreaMeanTimeSeries} for an example plot.
)";

class PlotGraphLayerLinesAndPoints : public PlotGraphLayer
{
protected:
  std::pair<std::string, VariableList> description;
  PlotLinePtr   line;
  PlotSymbolPtr symbol;
  Bool          hasZValues, hasErrors;

public:
  PlotGraphLayerLinesAndPoints(Config &config);
  Bool requiresColorBar()   const override {return hasZValues;}
  std::string scriptEntry() const override;
  std::string legendEntry() const override;
};

/***********************************************/

PlotGraphLayerLinesAndPoints::PlotGraphLayerLinesAndPoints(Config &config)
{
  try
  {
    FileName fileName;
    ExpressionVariablePtr exprX, exprY, exprZ, exprError;

    readConfig(config, "inputfileMatrix",  fileName,     Config::MUSTSET,  "",      "each line contains x,y");
    readConfig(config, "valueX",           exprX,        Config::OPTIONAL, "data0", "expression for x-values (input columns are named data0, data1, ...)");
    readConfig(config, "valueY",           exprY,        Config::MUSTSET,  "data1", "expression for y-values (input columns are named data0, data1, ...)");
    readConfig(config, "valueZ",           exprZ,        Config::OPTIONAL, "",      "expression for the colorbar");
    readConfig(config, "valueErrorBar",    exprError,    Config::OPTIONAL, "",      "expression for error bars (input columns are named data0, data1, ...)");
    readConfig(config, "description",      description,  Config::OPTIONAL, "",      "text of the legend");
    readConfig(config, "line",             line,         Config::OPTIONAL, "solid", "");
    readConfig(config, "symbol",           symbol,       Config::OPTIONAL, "",      "");
    readConfig(config, "plotOnSecondAxis", onSecondAxis, Config::DEFAULT,  "0",     "draw dataset on a second Y-axis (if available).");
    if(isCreateSchema(config)) return;

    hasZValues = symbol && symbol->requiresColorBar() && exprZ;
    hasErrors  = (exprError != nullptr);

    // tests
    if(!line && !symbol)
      throw(Exception("At least one of line and symbol must be set."));
    if(symbol && symbol->requiresColorBar() && !exprZ)
      throw(Exception("valueZ is needed to determine color of line/symbol"));
    if(!hasZValues)
      exprZ = nullptr;

    // check if file exists
    // --------------------
    if(!System::exists(fileName))
    {
      if(description.first.empty())
        description.first = fileName.str();
      description.first += " (file not found)";
      logWarning<<"file <"<<fileName<<"> not found!"<<Log::endl;
      return;
    }

    // read data
    // ---------
    Matrix A;
    readFileMatrix(fileName, A);

    if(!A.size())
    {
      if(description.first.empty())
        description.first = fileName.str();
      description.first += " (empty file)";
      logWarning<<"file <"<<fileName<<"> is empty!"<<Log::endl;
      return;
    }

    // create data variables
    // ---------------------
    VariableList varList;
    addDataVariables(A, varList);
    try {description.second += varList; description.first = StringParser::parse(description.first, description.second);} catch(std::exception &) {}
    for(ExpressionVariablePtr expr : {exprX, exprY, exprZ, exprError})
      if(expr) expr->simplify(varList);

    // evaluate expressions
    // --------------------
    data = Matrix(A.rows(), 2+hasZValues+hasErrors);
    for(UInt i=0; i<A.rows(); i++)
    {
      UInt idx = 0;
      evaluateDataVariables(A, i, varList);
      if(!exprX)
        data(i, idx++) = static_cast<Double>(i); // default: index
      for(auto expression : {exprX, exprY, exprZ, exprError})
        if(expression)
          data(i, idx++) = expression->evaluate(varList);
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerLinesAndPoints::scriptEntry() const
{
  try
  {
    if(!data.size())
      return std::string();

    std::stringstream ss;
    if(hasErrors)
      ss<<"gmt psxy "<<dataFileName<<" -bi"<<data.columns()<<"d -J -R -i0,1,"<<(hasZValues ? 3 : 2)<<" -Sc1p -Ey/1p -O -K >> groopsPlot.ps"<<std::endl;
    if(line)
      ss<<"gmt psxy "<<dataFileName<<" -bi"<<data.columns()<<"d -J -R -W"<<line->str()<<" -O -K >> groopsPlot.ps"<<std::endl;
    if(symbol)
      ss<<"gmt psxy "<<dataFileName<<" -bi"<<data.columns()<<"d -J -R -S"<<symbol->str()<<" -O -K >> groopsPlot.ps"<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerLinesAndPoints::legendEntry() const
{
  try
  {
    std::stringstream ss;
    if(description.first.empty() || (!line && !symbol))
      return ss.str();
    if(line)
      ss<<"S 0.3c - 0.5c - "<<line->str()<<" 0.7c " ;
    if(symbol)
    {
      if(line)
        ss<<std::endl<<"G -1l"<<std::endl;
      ss<<"S 0.3c "<<symbol->legendStr()<<"\t0.7c\t";
    }
    ss<<description.first<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/
/***********************************************/

static const char *docstringPlotGraphLayerErrorEnvelope = R"(
\subsection{ErrorEnvelope}
Draws a symmetrical envelope around \config{valueY} as function of \config{valueX}
using deviations \config{valueErrors}.
The standard \reference{dataVariables}{general.parser:dataVariables}
are available to select the data columns of \configFile{inputfileMatrix}{matrix}.
The data line itself is not plotted but must be added as extra
\configClass{layer:linesAndPoints}{plotGraphLayerType:linesAndPoints}.
)";

class PlotGraphLayerErrorEnvelope : public PlotGraphLayer
{
protected:
  std::pair<std::string, VariableList> description;
  PlotColorPtr fillColor;
  PlotLinePtr  edgeLine;

public:
  PlotGraphLayerErrorEnvelope(Config &config);
  std::string scriptEntry() const override;
  std::string legendEntry() const override;
};

/***********************************************/

PlotGraphLayerErrorEnvelope::PlotGraphLayerErrorEnvelope(Config &config)
{
  try
  {
    FileName fileName;
    ExpressionVariablePtr exprX, exprY, exprErrors;

    readConfig(config, "inputfileMatrix",  fileName,     Config::MUSTSET,  "",      "each line contains x,y");
    readConfig(config, "valueX",           exprX,        Config::OPTIONAL, "data0", "expression for x-values (input columns are named data0, data1, ...)");
    readConfig(config, "valueY",           exprY,        Config::MUSTSET,  "data1", "expression for y-values (input columns are named data0, data1, ...)");
    readConfig(config, "valueErrors",      exprErrors,   Config::MUSTSET,  "data2", "expression for error values");
    readConfig(config, "description",      description,  Config::OPTIONAL, "",      "text of the legend");
    readConfig(config, "fillColor",        fillColor,    Config::OPTIONAL, "gray",  "fill color of the envelope");
    readConfig(config, "edgeLine",         edgeLine,     Config::OPTIONAL, "",      "edge line style of the envelope");
    readConfig(config, "plotOnSecondAxis", onSecondAxis, Config::DEFAULT,  "0",     "draw dataset on a second Y-axis (if available).");
    if(isCreateSchema(config)) return;

    if(!fillColor && !edgeLine)
      throw(Exception("At least one of fillColor and edgeLine must be set."));

    // check if file exists
    // --------------------
    if(!System::exists(fileName))
    {
      if(description.first.empty())
        description.first = fileName.str();
      description.first += " (file not found)";
      logWarning<<"file <"<<fileName<<"> not found!"<<Log::endl;
      return;
    }

    // read data
    // ---------
    Matrix A;
    readFileMatrix(fileName, A);

    if(!A.size())
    {
      if(description.first.empty())
        description.first = fileName.str();
      description.first += " (empty file)";
      logWarning<<"file <"<<fileName<<"> is empty!"<<Log::endl;
      return;
    }

    // create data variables
    // ---------------------
    VariableList varList;
    addDataVariables(A, varList);
    try {description.second += varList; description.first = StringParser::parse(description.first, description.second);} catch(std::exception &) {}

    std::vector<ExpressionVariablePtr> expressions = {exprX, exprY, exprErrors};
    for(ExpressionVariablePtr expr : expressions)
      if(expr) expr->simplify(varList);

    // evaluate expressions
    // --------------------
    data = Matrix(A.rows(), 3);
    for(UInt i=0; i<A.rows(); i++)
    {
      evaluateDataVariables(A, i, varList);
      data(i, 0) = static_cast<Double>(i); // default: index
      for(UInt k=0; k<expressions.size(); k++)
        if(expressions.at(k))
          data(i, k) = expressions.at(k)->evaluate(varList);
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerErrorEnvelope::scriptEntry() const
{
  try
  {
    if(!data.size())
      return std::string();

    std::stringstream ss;
    ss<<"gmt psxy "<<dataFileName<<" -bi"<<data.columns()<<"d -J -R -L+d";
    if(edgeLine)
      ss<<"+p"<<edgeLine->str();
    if(fillColor)
      ss<<" -G"<<fillColor->str();
    ss<<" -O -K >> groopsPlot.ps"<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerErrorEnvelope::legendEntry() const
{
  try
  {
    if(description.first.empty())
      return std::string();

    std::stringstream ss;
    ss<<"S 0.3c s 0.5c "<<(fillColor ? fillColor->str() : "-"s)<<" ";
    if(edgeLine)
      ss<<edgeLine->str();
    else
      ss<<"0p,"<<fillColor->str();
    ss<<"\t0.7c\t"<<description.first<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/
/***********************************************/

static const char *docstringPlotGraphLayerBars = R"(
\subsection{Bars}
Creates a bar plot with vertical or \config{horizontal} bars out of the given
x- and y-values. The standard \reference{dataVariables}{general.parser:dataVariables}
are available to select the data columns of \configFile{inputfileMatrix}{matrix}.
The bars ranges from \config{valueBase} (can be also an expression) to the \config{valueY}.
If no \configClass{color}{plotColorType} is given a \configClass{colorbar}{plotColorbarType}
is required and the color is determined by \config{valueZ}.

See \program{Instrument2Histogram} for an example plot.
)";

class PlotGraphLayerBars : public PlotGraphLayer
{
private:
  Double       barWidth;
  Bool         horizontal;
  PlotColorPtr color;
  PlotLinePtr  edgeLine;
  std::pair<std::string, VariableList> description;

public:
  PlotGraphLayerBars(Config &config);
  std::string scriptEntry() const override;
  std::string legendEntry() const override;
};

/***********************************************/

PlotGraphLayerBars::PlotGraphLayerBars(Config &config)
{
  try
  {
    FileName fileName;
    ExpressionVariablePtr exprX, exprY, exprZ, exprBase, exprWidth;

    horizontal = FALSE;

    readConfig(config, "inputfileMatrix",  fileName,     Config::MUSTSET,  "",      "each line contains x,y");
    readConfig(config, "valueX",           exprX,        Config::OPTIONAL, "data0", "expression for x-values (input columns are named data0, data1, ...)");
    readConfig(config, "valueY",           exprY,        Config::MUSTSET,  "data1", "expression for y-values (input columns are named data0, data1, ...)");
    readConfig(config, "valueZ",           exprZ,        Config::OPTIONAL, "",      "expression for the colorbar");
    readConfig(config, "valueBase",        exprBase,     Config::OPTIONAL, "",      "base value of bars (default: minimum y-value)");
    readConfig(config, "width",            exprWidth,    Config::OPTIONAL, "",      "width of bars (default: minimum x-gap)");
    readConfig(config, "horizontal",       horizontal,   Config::OPTIONAL, "",      "draw horizontal bars instead of vertical");
    readConfig(config, "description",      description,  Config::OPTIONAL, "",      "text of the legend");
    readConfig(config, "color",            color,        Config::OPTIONAL, "black", "");
    readConfig(config, "edgeLine",         edgeLine,     Config::OPTIONAL, "",      "line");
    readConfig(config, "plotOnSecondAxis", onSecondAxis, Config::DEFAULT,  "0",     "draw dataset on a second Y-axis (if available).");
    if(isCreateSchema(config)) return;

    // check if file exists
    // --------------------
    if(!System::exists(fileName))
    {
      if(description.first.empty())
        description.first = fileName.str();
      description.first += " (file not found)";
      logWarning<<"file <"<<fileName<<"> not found!"<<Log::endl;
      return;
    }

    // read data
    // ---------
    Matrix A;
    readFileMatrix(fileName, A);

    if(!A.size())
    {
      if(description.first.empty())
        description.first = fileName.str();
      description.first += " (empty file)";
      logWarning<<"file <"<<fileName<<"> is empty!"<<Log::endl;
      return;
    }

    // create data variables
    // ---------------------
    VariableList varList;
    addDataVariables(A, varList);
    try {description.second += varList; description.first = StringParser::parse(description.first, description.second);} catch(std::exception &) {}
    for(ExpressionVariablePtr expr : {exprX, exprY, exprZ, exprBase, exprBase, exprWidth})
      if(expr) expr->simplify(varList);

    // evaluate expressions
    // --------------------
    data = Matrix(A.rows(), exprZ ? 4 : 3);
    for(UInt i=0; i<A.rows(); i++)
    {
      evaluateDataVariables(A, i, varList);
      data(i, 0) = static_cast<Double>(i); // default: index
      if(exprX)    data(i, 0) = exprX->evaluate(varList);
      if(exprY)    data(i, 1) = exprY->evaluate(varList);
      if(exprZ)    data(i, 2) = exprZ->evaluate(varList);
      if(exprBase) data(i, exprZ ? 3 : 2) = exprBase->evaluate(varList);
    }

    // compute bar width
    // -----------------
    if(!exprWidth)
    {
      barWidth = std::numeric_limits<Double>::infinity();
      for(UInt i=1; i<data.rows(); i++)
        barWidth = std::min(barWidth, std::abs(data(i, horizontal ? 1 : 0) - (data(i-1, horizontal ? 1 : 0))));
    }
    else
      barWidth = exprWidth->evaluate(varList);

    // compute base value
    // ------------------
    if(!exprBase)
      copy(Vector(data.rows(), min(data.column(horizontal ? 0 : 1))), data.column(2 +  (exprZ ? 1 : 0)));

    // test z-values
    // -------------
    if(!color && !exprZ)
      throw(Exception("valueZ is needed to determine color of line/symbol"));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerBars::scriptEntry() const
{
  try
  {
    if(!data.size())
      return std::string();

    std::stringstream ss;
    ss<<"gmt psxy "<<dataFileName<<" -bi"<<data.columns()<<"d -J -R -S"<<(horizontal ? "B" : "b")<<barWidth<<"ub";
    if(edgeLine)
      ss<<" -W"<<edgeLine->str();
    if(color)
      ss<<" -G"<<color->str();
    else
      ss<<" -CgroopsPlot.cpt";
    ss<<" -O -K >> groopsPlot.ps"<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerBars::legendEntry() const
{
  try
  {
    if(description.first.empty())
      return std::string();

    if(!color)
    {
      logWarning<<"No legend entry can be generated for bar layers when using <fromValue> as color."<<Log::endl;
      return std::string();
    }

    std::stringstream ss;
    ss<<"S 0.3c r 5p "<<color->str()<<" ";
    if(edgeLine) ss<<edgeLine->str();
    else ss<<"-";
    ss<<"\t0.7c\t"<<description.first<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/
/***********************************************/

static const char *docstringPlotGraphLayerGridded = R"(
\subsection{Gridded}
Creates a regular grid of yxz values. The standard \reference{dataVariables}{general.parser:dataVariables}
are available to select the data columns of \configFile{inputfileMatrix}{matrix}.
Empty grid cells are not plotted. Cells with more than one value will be set to the mean value.
The grid spacing is determined by the median spacing of the input data or set by \config{incrementX/Y}.

See \program{Orbit2ArgumentOfLatitude} for an example plot.
)";

/***** CLASS ***********************************/

class PlotGraphLayerGridded : public PlotGraphLayer
{
  Double incX, incY;

public:
  PlotGraphLayerGridded(Config &config);
  Bool requiresColorBar() const override {return TRUE;}
  std::string scriptEntry() const override;
  Double bufferX() const override {return 0.5 * incX;}
  Double bufferY() const override {return 0.5 * incY;}
};

/***********************************************/

PlotGraphLayerGridded::PlotGraphLayerGridded(Config &config)
{
  try
  {
    FileName    fileName;
    std::string choice;
    ExpressionVariablePtr exprX, exprY, exprZ;
    incX = incY = NAN_EXPR;

    readConfig(config, "inputfileMatrix",  fileName,     Config::MUSTSET,   "",      "each line contains x,y,z");
    readConfig(config, "valueX",           exprX,        Config::OPTIONAL,  "data0", "expression for x-values (input columns are named data0, data1, ...)");
    readConfig(config, "valueY",           exprY,        Config::MUSTSET,   "data1", "expression for y-values (input columns are named data0, data1, ...)");
    readConfig(config, "valueZ",           exprZ,        Config::MUSTSET,   "data2", "expression for the colorbar");
    readConfig(config, "incrementX",       incX,         Config::OPTIONAL,  "",      "the grid spacing");
    readConfig(config, "incrementY",       incY,         Config::OPTIONAL,  "",      "the grid spacing");
    readConfig(config, "plotOnSecondAxis", onSecondAxis, Config::DEFAULT,   "0",     "draw dataset on a second Y-axis (if available).");
    if(isCreateSchema(config)) return;

    // check if file exists
    // --------------------
    if(!System::exists(fileName))
    {
      logWarning<<"file <"<<fileName<<"> not found!"<<Log::endl;
      return;
    }

    // read data
    // ---------
    Matrix A;
    readFileMatrix(fileName, A);

    if(!A.size())
    {
      logWarning<<"file <"<<fileName<<"> is empty!"<<Log::endl;
      return;
    }

    // create data variables
    // ---------------------
    VariableList varList;
    addDataVariables(A, varList);

    std::vector<ExpressionVariablePtr> expressions = {exprX, exprY, exprZ};
    for(ExpressionVariablePtr expr : expressions)
      if(expr) expr->simplify(varList);

    // evaluate expressions
    // --------------------
    data = Matrix(A.rows(), (exprZ ? 3 : 2));
    for(UInt i=0; i<A.rows(); i++)
    {
      evaluateDataVariables(A, i, varList);
      data(i, 0) = static_cast<Double>(i); // default: index
      for(UInt k=0; k<expressions.size(); k++)
        if(expressions.at(k))
          data(i, k) = expressions.at(k)->evaluate(varList);
    }

    // determine sampling (median)
    // ---------------------------
    auto computeIncrement = [](std::vector<Double> x)
    {
      std::sort(x.begin(), x.end());
      auto it = std::unique(x.begin(), x.end());
      std::vector<Double> dx;
      std::adjacent_difference(x.begin(), it, std::back_inserter(dx));
      std::nth_element(dx.begin(), dx.begin()+dx.size()/2, dx.end());
      return dx[dx.size()/2];
    };

    if(std::isnan(incX))
      incX = computeIncrement(Vector(data.column(0)));
    if(std::isnan(incY))
      incY = computeIncrement(Vector(data.column(1)));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerGridded::scriptEntry() const
{
  try
  {
    if(!data.size())
      return std::string();

    const Double minX = min(data.column(0)) - bufferX();
    const Double maxX = max(data.column(0)) + bufferX();
    const Double minY = min(data.column(1)) - bufferY();
    const Double maxY = max(data.column(1)) + bufferY();

    std::stringstream ss;
    ss<<"gmt xyz2grd "<<dataFileName<<" -bi3d -G"<<dataFileName<<".grd -Vn";
    ss<<" -r -I"<<incX<<"=/"<<incY<<"= -R"<<minX<<"/"<<maxX<<"/"<<minY<<"/"<<maxY<<std::endl;
    ss<<"gmt grdimage "<<dataFileName<<".grd -Q -J -R"<<PlotBasics::scriptVariable("range")<<" -CgroopsPlot.cpt";
    ss<<" -O -K >> groopsPlot.ps"<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/
/***********************************************/

static const char *docstringPlotGraphLayerRectangle = R"(
\subsection{Rectangle}
Plots a rectangle to highlight an area.
)";

/***** CLASS ***********************************/

class PlotGraphLayerRectangle : public PlotGraphLayer
{
  PlotLinePtr  edgeLine;
  PlotColorPtr fillColor;
  std::string  description;

public:
  PlotGraphLayerRectangle(Config &config);
  void        writeDataFile(const FileName &workingDirectory, UInt idxLayer, Double minX, Double maxX, Double minY, Double maxY) override;
  std::string scriptEntry() const override;
  std::string legendEntry() const override;
};

/***********************************************/

PlotGraphLayerRectangle::PlotGraphLayerRectangle(Config &config)
{
  try
  {
    Double x1=NAN_EXPR, y1=NAN_EXPR, x2=NAN_EXPR, y2=NAN_EXPR;

    readConfig(config, "minX",             x1,           Config::OPTIONAL, "",  "empty: left");
    readConfig(config, "maxX",             x2,           Config::OPTIONAL, "",  "empty: right");
    readConfig(config, "minY",             y1,           Config::OPTIONAL, "",  "empty: bottom");
    readConfig(config, "maxY",             y2,           Config::OPTIONAL, "",  "empty: top");
    readConfig(config, "description",      description,  Config::OPTIONAL, "",  "text of the legend");
    readConfig(config, "edgeLine",         edgeLine,     Config::OPTIONAL, "",  "");
    readConfig(config, "fillColor",        fillColor,    Config::OPTIONAL, "",  "");
    readConfig(config, "plotOnSecondAxis", onSecondAxis, Config::DEFAULT,  "0", "draw dataset on a second Y-axis (if available).");
    if(isCreateSchema(config)) return;

    if(!fillColor && !edgeLine)
      throw(Exception("At least one of fillColor and edgeLine must be set."));

    data = Matrix(4, 2);
    copy(Vector({x1, x1, x2, x2}), data.column(0));
    copy(Vector({y1, y2, y2, y1}), data.column(1));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

void PlotGraphLayerRectangle::writeDataFile(const FileName &workingDirectory, UInt idxLayer, Double minX, Double maxX, Double minY, Double maxY)
{
  try
  {
    if(std::isnan(data(0, 0))) data(0, 0) = data(1, 0) = minX;
    if(std::isnan(data(2, 0))) data(2, 0) = data(3, 0) = maxX;
    if(std::isnan(data(0, 1))) data(0, 1) = data(3, 1) = minY;
    if(std::isnan(data(1, 1))) data(1, 1) = data(2, 1) = maxY;
    PlotGraphLayer::writeDataFile(workingDirectory, idxLayer, minX, maxX, minY, maxY);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerRectangle::scriptEntry() const
{
  try
  {
    std::stringstream ss;
    ss<<"gmt psclip "<<dataFileName<<" -bi2d -J -R -O -K >> groopsPlot.ps"<<std::endl;
    ss<<"gmt psxy "<<dataFileName<<" -bi2d -A -L -J -R";
    if(fillColor) ss<<" -G"<<fillColor->str();
    if(edgeLine) ss<<" -W"<<edgeLine->str();
    ss<<" -O -K >> groopsPlot.ps"<<std::endl;
    ss<<"gmt psclip -C -O -K >> groopsPlot.ps"<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerRectangle::legendEntry() const
{
  try
  {
    std::stringstream ss;
    if(!description.empty())
    {
      ss<<"S 0.3c s 0.5c "<<(fillColor ? fillColor->str() : "-")<<" ";
      if(edgeLine)
        ss<<edgeLine->str();
      else
        ss<<"0p,"<<fillColor->str();
      ss<<"\t0.7c\t"<<description<<std::endl;
    }
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/
/***********************************************/

static const char *docstringPlotGraphLayerText = R"(
\subsection{Text}
Writes a \config{text} at \config{originX} and \config{originY} position in the graph.
With \config{clip} the text is cutted at the boundaries of the plotting area.
)";

class PlotGraphLayerText : public PlotGraphLayer
{
  Double       xOffset, yOffset;
  Double       fontSize;
  PlotColorPtr fontColor;
  std::string  text;
  std::string  alignment;
  Bool         clip;

public:
  PlotGraphLayerText(Config &config);
  void writeDataFile(const FileName &, UInt, Double, Double, Double, Double) override;
  std::string scriptEntry() const override;
};

/***********************************************/

PlotGraphLayerText::PlotGraphLayerText(Config &config)
{
  try
  {
    data = Matrix(1, 2);
    readConfig(config, "text",             text,         Config::MUSTSET, "",   "");
    readConfig(config, "originX",          data(0, 0),   Config::MUSTSET, "",   "");
    readConfig(config, "originY",          data(0, 1),   Config::MUSTSET, "",   "");
    readConfig(config, "offsetX",          xOffset,      Config::DEFAULT, "0",  "[cm] x-offset from origin");
    readConfig(config, "offsetY",          yOffset,      Config::DEFAULT, "0",  "[cm] y-offset from origin");
    readConfig(config, "alignment",        alignment,    Config::DEFAULT, "BL", "L, C, R (left, center, right) and T, M, B (top, middle, bottom)");
    readConfig(config, "fontSize",         fontSize,     Config::DEFAULT, "10", "[pt]");
    readConfig(config, "fontColor",        fontColor,    Config::MUSTSET, "",   "");
    readConfig(config, "clip",             clip,         Config::DEFAULT, "1",  "clip at boundaries");
    readConfig(config, "plotOnSecondAxis", onSecondAxis, Config::DEFAULT, "0",  "draw dataset on a second Y-axis (if available).");
    if(isCreateSchema(config)) return;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

void PlotGraphLayerText::writeDataFile(const FileName &workingDirectory, UInt idxLayer, Double /*minX*/, Double /*maxX*/, Double /*minY*/, Double /*maxY*/)
{
  try
  {
    dataFileName = "text."+idxLayer%"%i.txt"s;
    OutFile file(workingDirectory.append(dataFileName));
    file<<data(0, 0)<<" "<<data(0, 1)<<" "<<text;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerText::scriptEntry() const
{
  try
  {
    std::stringstream ss;
    ss<<"gmt pstext "<<dataFileName<<" -F+f"<<fontSize<<"p,,"<<fontColor->str()<<"+j"<<alignment<<" -D"<<xOffset<<"/"<<yOffset<<" -J -R "<<(clip ? "" : "-N")<<" -O -K >> groopsPlot.ps"<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/
/***********************************************/

static const char *docstringPlotGraphLayerDegreeAmplitudes = R"(
\subsection{DegreeAmplitudes}\label{plotGraphLayerType:degreeAmplitudes}
Plot degree amplitudes of potential coefficients computed by \program{Gravityfield2DegreeAmplitudes}
or \program{PotentialCoefficients2DegreeAmplitudes}.
The standard \reference{dataVariables}{general.parser:dataVariables} are available to select
the data columns of \configFile{inputfileMatrix}{matrix}. It plots a solid line for the
\config{valueSignal} and a dotted line for the \config{valueError} per default.
)";

class PlotGraphLayerDegreeAmplitudes : public PlotGraphLayer
{
  std::pair<std::string, VariableList> description;
  PlotLinePtr lineSignal, lineErrors;

public:
  PlotGraphLayerDegreeAmplitudes(Config &config);
  std::string scriptEntry() const override;
  std::string legendEntry() const override;
};

/***********************************************/

PlotGraphLayerDegreeAmplitudes::PlotGraphLayerDegreeAmplitudes(Config &config)
{
  try
  {
    FileName fileName;
    ExpressionVariablePtr exprDegree, exprSignal, exprErrors;

    readConfig(config, "inputfileMatrix",  fileName,     Config::MUSTSET,  "",      "degree amplitudes");
    readConfig(config, "valueDegree",      exprDegree,   Config::OPTIONAL, "data0", "expression for x-values (degrees) (input columns are named data0, data1, ...)");
    readConfig(config, "valueSignal",      exprSignal,   Config::OPTIONAL, "data1", "expression for y-values (signal) (input columns are named data0, data1, ...)");
    readConfig(config, "valueErrors",      exprErrors,   Config::OPTIONAL, "data2", "expression for y-values (formal errors)");
    readConfig(config, "description",      description,  Config::OPTIONAL, "",      "text of the legend");
    readConfig(config, "lineSignal",       lineSignal,   Config::OPTIONAL, "solid", "");
    readConfig(config, "lineErrors",       lineErrors,   Config::OPTIONAL, R"({"custom": {"style":"5_2:0"}})", "");
    readConfig(config, "plotOnSecondAxis", onSecondAxis, Config::DEFAULT,  "0",     "draw dataset on a second Y-axis (if available).");
    if(isCreateSchema(config)) return;

    // check if file exists
    // --------------------
    if(!System::exists(fileName))
    {
      if(description.first.empty())
        description.first = fileName.str();
      description.first += " (file not found)";
      logWarning<<"file <"<<fileName<<"> not found!"<<Log::endl;
      return;
    }

    // read data
    // ---------
    Matrix A;
    readFileMatrix(fileName, A);

    if(!A.size())
    {
      if(description.first.empty())
        description.first = fileName.str();
      description.first += " (empty file)";
      logWarning<<"file <"<<fileName<<"> is empty!"<<Log::endl;
      return;
    }

    // create data variables
    // ---------------------
    VariableList varList;
    addDataVariables(A, varList);
    try {description.second += varList; description.first = StringParser::parse(description.first, description.second);} catch(std::exception &) {}

    std::vector<ExpressionVariablePtr> expressions = {exprDegree, exprSignal, exprErrors};
    for(ExpressionVariablePtr expr : expressions)
      if(expr) expr->simplify(varList);

    // evaluate expressions
    // --------------------
    data = Matrix(A.rows(), 3, NAN_EXPR);
    for(UInt i=0; i<A.rows(); i++)
    {
      evaluateDataVariables(A, i, varList);
      data(i, 0) = static_cast<Double>(i); // default: index
      for(UInt k=0; k<expressions.size(); k++)
        if(expressions.at(k))
          data(i, k) = expressions.at(k)->evaluate(varList);
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerDegreeAmplitudes::scriptEntry() const
{
  try
  {
    if(!data.size())
      return std::string();

    std::stringstream ss;
    if(lineSignal)
      ss<<"gmt psxy "<<dataFileName<<" -bi"<<data.columns()<<"d -J -R -i0,1 -W"<<lineSignal->str()<<" -O -K >> groopsPlot.ps"<<std::endl;
    if(lineErrors)
      ss<<"gmt psxy "<<dataFileName<<" -bi"<<data.columns()<<"d -J -R -i0,2 -W"<<lineErrors->str()<<" -O -K >> groopsPlot.ps"<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerDegreeAmplitudes::legendEntry() const
{
  try
  {
    if(description.first.empty() || (!lineErrors && !lineSignal))
      return std::string();

    std::stringstream ss;
    ss<<"S 0.3c - 0.5c - "<<(lineSignal ? lineSignal->str() : lineErrors->str())<<" 0.7c "<<description.first<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/
/***********************************************/

static const char *docstringPlotGraphLayerDegreeAmplitudesSimple = R"(
\subsection{DegreeAmplitudesSimple}\label{plotGraphLayerType:degreeAmplitudesSimple}
Plot degree amplitudes from a \configClass{gravityfield}{gravityfieldType}.
The coefficients can be converted to different functionals with \configClass{kernel}{kernelType}.
If set the expansion is limited in the range between \config{minDegree}
and \config{maxDegree} inclusivly. It plots a solid line for the degree amplitude (signal)
and a dotted line for the formal errors per default.

This is a simplified version of
\configClass{layer:degreeAmplitudes}{plotGraphLayerType:degreeAmplitudes}.
)";

class PlotGraphLayerDegreeAmplitudesSimple : public PlotGraphLayer
{
  std::string description;
  PlotLinePtr lineSignal, lineErrors;

public:
  PlotGraphLayerDegreeAmplitudesSimple(Config &config);
  std::string scriptEntry() const override;
  std::string legendEntry() const override;
};

/***********************************************/

PlotGraphLayerDegreeAmplitudesSimple::PlotGraphLayerDegreeAmplitudesSimple(Config &config)
{
  try
  {
    enum DegreeType {RMS, CUMMULATE, MEDIAN};
    DegreeType      degreeType = RMS;
    UInt            minDegree, maxDegree = INFINITYDEGREE;
    Time            time;
    GravityfieldPtr gravityfield;
    KernelPtr       kernel;

    readConfig(config, "gravityfield",     gravityfield,  Config::MUSTSET,  "", "");
    readConfig(config, "kernel",           kernel,        Config::MUSTSET,  "", "");
    std::string choice;
    if(readConfigChoice(config, "type", choice, Config::MUSTSET, "", "type of variances"))
    {
      if(readConfigChoiceElement(config, "rms",          choice, "degree amplitudes (square root of degree variances)")) degreeType = RMS;
      if(readConfigChoiceElement(config, "accumulation", choice, "cumulate variances over degrees"))                     degreeType = CUMMULATE;
      if(readConfigChoiceElement(config, "median",       choice, "median of absolute values per degree"))                degreeType = MEDIAN;
      endChoice(config);
    }
    readConfig(config, "time",             time,         Config::OPTIONAL, "", "at this time the gravity field will be evaluated");
    readConfig(config, "minDegree",        minDegree,    Config::DEFAULT,  "0", "");
    readConfig(config, "maxDegree",        maxDegree,    Config::OPTIONAL, "",  "");
    readConfig(config, "description",      description,  Config::OPTIONAL, "",  "text of the legend");
    readConfig(config, "lineSignal",       lineSignal,   Config::OPTIONAL, "solid", "");
    readConfig(config, "lineErrors",       lineErrors,   Config::OPTIONAL, R"({"custom": {"style":"5_2:0"}})", "");
    readConfig(config, "plotOnSecondAxis", onSecondAxis, Config::DEFAULT,  "0",     "draw dataset on a second Y-axis (if available).");
    if(isCreateSchema(config)) return;

    // Create potential coefficients
    // -----------------------------
    SphericalHarmonics harm = gravityfield->sphericalHarmonics(time, maxDegree, minDegree);
    maxDegree = harm.maxDegree();
    const Vector kn     = kernel->inverseCoefficients(Vector3d(0, 0, harm.R()), maxDegree, harm.isInterior());
    const Bool hasSigma = harm.sigma2cnm().size() || harm.sigma2snm().size();

    std::vector<Double> x, y, sigma;
    for(UInt n=0; n<=maxDegree; n++)
    {
      const Double factor = std::pow(harm.GM()/harm.R() * kn(n), 2);
      std::vector<Double> coefficients, formalErrors;
      for(UInt m=0; m<=n; m++)
      {
        coefficients.push_back(factor * std::pow(harm.cnm()(n, m),2));
        coefficients.push_back(factor * std::pow(harm.snm()(n, m),2));
        if(hasSigma) formalErrors.push_back(factor * harm.sigma2cnm()(n, m));
        if(hasSigma) formalErrors.push_back(factor * harm.sigma2snm()(n, m));
      }

      // degree variances
      // ----------------
      x.push_back(n);
      if((degreeType == RMS) || (degreeType == CUMMULATE))
      {
        y.push_back(std::sqrt(std::accumulate(coefficients.begin(), coefficients.end(), 0.)));
        if(hasSigma)
          sigma.push_back(std::sqrt(std::accumulate(formalErrors.begin(), formalErrors.end(), 0.)));
      }
      else if(degreeType == MEDIAN)
      {
        auto vectorMedian = [](std::vector<Double> &data)
        {
          std::partial_sort(data.begin(), data.begin()+data.size()/2+1, data.end());
          return (data.size()%2) ? data.at(data.size()/2) : (0.5*(data.at(data.size()/2-1)+data.at(data.size()/2)));
        };
        y.push_back(std::sqrt((2*n+1)*vectorMedian(coefficients)));
        if(hasSigma)
          sigma.push_back(std::sqrt((2*n+1)*vectorMedian(formalErrors)));
      }
    } // for(n)

    if(degreeType == CUMMULATE)
    {
      auto vectorAccumulate = [](std::vector<Double> &data)
      {
        std::for_each(data.begin(), data.end(), [](Double &v) {v = v*v;});
        std::partial_sum(data.begin(), data.end(), data.begin());
        std::for_each(data.begin(), data.end(), [](Double &v) {v = std::sqrt(v);});
      };
      vectorAccumulate(y);
      if(hasSigma)
        vectorAccumulate(sigma);
    }

    // evaluate expressions
    // --------------------
    data = Matrix(x.size(), 3, NAN_EXPR);
    copy(Vector(x), data.column(0));
    copy(Vector(y), data.column(1));
    if(hasSigma) copy(Vector(sigma), data.column(2));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerDegreeAmplitudesSimple::scriptEntry() const
{
  try
  {
    if(!data.size())
      return std::string();

    std::stringstream ss;
    if(lineSignal)
      ss<<"gmt psxy "<<dataFileName<<" -bi"<<data.columns()<<"d -J -R -i0,1 -W"<<lineSignal->str()<<" -O -K >> groopsPlot.ps"<<std::endl;
    if(lineErrors)
      ss<<"gmt psxy "<<dataFileName<<" -bi"<<data.columns()<<"d -J -R -i0,2 -W"<<lineErrors->str()<<" -O -K >> groopsPlot.ps"<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

std::string PlotGraphLayerDegreeAmplitudesSimple::legendEntry() const
{
  try
  {
    if(description.empty() || (!lineErrors && !lineSignal))
      return std::string();

    std::stringstream ss;
    ss<<"S 0.3c - 0.5c - "<<(lineSignal ? lineSignal->str() : lineErrors->str())<<" 0.7c "<<description<<std::endl;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/
/***********************************************/

GROOPS_REGISTER_CLASS(PlotGraphLayer, "plotGraphLayerType",
                      PlotGraphLayerLinesAndPoints,
                      PlotGraphLayerErrorEnvelope,
                      PlotGraphLayerBars,
                      PlotGraphLayerGridded,
                      PlotGraphLayerRectangle,
                      PlotGraphLayerText,
                      PlotGraphLayerDegreeAmplitudes,
                      PlotGraphLayerDegreeAmplitudesSimple)

GROOPS_READCONFIG_CLASS(PlotGraphLayer, "plotGraphLayerType")

/***********************************************/

PlotGraphLayerPtr PlotGraphLayer::create(Config &config, const std::string &name)
{
  try
  {
    PlotGraphLayerPtr plotGraphLayer;
    std::string  type;

    readConfigChoice(config, name, type, Config::MUSTSET, "", "lines, points and polygons");
    if(readConfigChoiceElement(config, "linesAndPoints",   type, "line/points"))
      plotGraphLayer = PlotGraphLayerPtr(new PlotGraphLayerLinesAndPoints(config));
    if(readConfigChoiceElement(config, "errorEnvelope",    type, "error envelope for line plots"))
      plotGraphLayer = PlotGraphLayerPtr(new PlotGraphLayerErrorEnvelope(config));
    if(readConfigChoiceElement(config, "bars",             type, "bar graph"))
      plotGraphLayer = PlotGraphLayerPtr(new PlotGraphLayerBars(config));
    if(readConfigChoiceElement(config, "gridded",          type, "mesh data"))
      plotGraphLayer = PlotGraphLayerPtr(new PlotGraphLayerGridded(config));
    if(readConfigChoiceElement(config, "rectangle",        type, "draw rectangle"))
      plotGraphLayer = PlotGraphLayerPtr(new PlotGraphLayerRectangle(config));
    if(readConfigChoiceElement(config, "text",             type, "text"))
      plotGraphLayer = PlotGraphLayerPtr(new PlotGraphLayerText(config));
    if(readConfigChoiceElement(config, "degreeAmplitudes", type, "degree amplitudes of a gravity field"))
      plotGraphLayer = PlotGraphLayerPtr(new PlotGraphLayerDegreeAmplitudes(config));
    if(readConfigChoiceElement(config, "degreeAmplitudesSimple", type, "degree amplitudes of a gravity field"))
      plotGraphLayer = PlotGraphLayerPtr(new PlotGraphLayerDegreeAmplitudesSimple(config));
    endChoice(config);

    return plotGraphLayer;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

void PlotGraphLayer::getIntervalX(Bool isLogarithmic, Double &minX, Double &maxX) const
{
  try
  {
    for(UInt i=0; i<data.rows(); i++)
      if(!std::isnan(data(i, 0)) && (!isLogarithmic || (data(i, 0) > 0)))
      {
        minX = std::min(minX, data(i, 0)-bufferX());
        maxX = std::max(maxX, data(i, 0)+bufferX());
      }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

void PlotGraphLayer::getIntervalY(Bool isLogarithmic, Double minX, Double maxX, Double &minY, Double &maxY) const
{
  try
  {
    for(UInt i=0; i<data.rows(); i++)
      if((minX <= data(i, 0)) && (data(i, 0) <= maxX))
        if(!std::isnan(data(i, 1)) && (!isLogarithmic || (data(i, 1) > 0)))
        {
          minY = std::min(minY, data(i, 1)-bufferY());
          maxY = std::max(maxY, data(i, 1)+bufferY());
        }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

void PlotGraphLayer::getIntervalZ(Bool isLogarithmic, Double minX, Double maxX, Double minY, Double maxY, Double &minZ, Double &maxZ) const
{
  try
  {
    if(!requiresColorBar())
      return;

    UInt   count =  0;
    Double avg   =  0.;
    for(UInt i=0; i<data.rows(); i++)
      if((minX <= data(i, 0)) && (data(i, 0) <= maxX) && (minY <= data(i, 1)) && (data(i, 1) <= maxY))
        if(!std::isnan(data(i, 2)) && (!isLogarithmic || (data(i, 2) > 0)))
        {
          minZ  = std::min(minZ, data(i, 2));
          maxZ  = std::max(maxZ, data(i, 2));
          avg  += std::fabs(data(i, 2));
          count++;
        }
    avg /= count;

    if(!isLogarithmic)
    {
      minZ = (minZ > 0) ? 0 : -3*avg;
      maxZ = +3*avg;
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

void PlotGraphLayer::writeDataFile(const FileName &workingDirectory, UInt idxLayer, Double /*minX*/, Double /*maxX*/, Double /*minY*/, Double /*maxY*/)
{
  try
  {
    dataFileName = "data."+idxLayer%"%i.dat"s;
    OutFile file(workingDirectory.append(dataFileName), std::ios::out | std::ios::binary);
    for(UInt i=0; i<data.rows(); i++)
      for(UInt k=0; k<data.columns(); k++)
        file.write(reinterpret_cast<char *>(&data(i, k)), sizeof(Double));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/