-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlpjio.cpp
More file actions
executable file
·4192 lines (3647 loc) · 150 KB
/
lpjio.cpp
File metadata and controls
executable file
·4192 lines (3647 loc) · 150 KB
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
//----------------------------------------------------------------------
//
// FILE: lpjio.cpp
// AUTHOR: I. Ross
// DATE: 26-MAY-2005
// Modifed by Sarah 10-April-06 for updated version of LPJ-SPITFIRE
// New version contains fire model in spin up & lightening input file
//
// Modifed again by Doug, April/MAy-09 for including agriculture inputs
// (crops and pasture) for SPITFIRE.
//----------------------------------------------------------------------
//
// I/O file for standalone LPJ build using monthly climatology of
// temperature, pressure and cloudiness from MOTIF format netCDF
// files.
//
//----------------------------------------------------------------------
//======================================================================
//
// HEADER FILES
//
//======================================================================
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <set>
#include <vector>
#include <cmath>
#include <limits>
using namespace std;
#include <netcdf.hh>
#include "Array.hh"
//======================================================================
//
// CONSTANT DEFINITIONS
//
//======================================================================
// Array sizes etc.
const int NMON = 12, NDAYS = 365, NTRACE = 6, NTRANSIENT = 1;
const int NPFT = 13;
const int CLIMBUF = 20;
const int NCO2 = 3; // Number of C variables : C02, C13, C14
const int co2_dim_values[NCO2] = { 1, 2, 3 }; // Values for the C dimension
// Missing values to use for output files.
const float FMISSING = 1.0E35;
const int IMISSING = -999;
//======================================================================
//
// TYPE DEFINITIONS
//
//======================================================================
//----------------------------------------------------------------------
//
// Run parameters: read from configuration file.
//
struct RunParameters {
RunParameters() :
temp_var("tas"), prec_var("pr"), sun_var("clt"), wetdays_var("wetdays"),
tmin_var("tasmin"), tmax_var("tasmax"), windsp_var("uvas"),
mask_var("mask"), sun_is_cloud(true), mask_type(BOOLEAN),
wetdays_fixed(false),
soil_type_fixed(true), fixed_soil_type(2), soil_type_var("soiltype"),
ann_popdens_var("popdens"),
crop_var("crop"), crop_fixed(false), //Doug 04/09
pas_var("pas"), pas_fixed(false), //Doug 04/09
lightn_var("lightn"),
a_nd_fixed(false), fixed_a_nd(0.0), a_nd_var("a_nd"),
spinup_years(1000), spinup_data_years(50),
spinup_file("data_after_step1a.txt"), rampup_file("data_after_step1b.txt"), //Doug 07/09
rampup_years(0), rampup_data_years(0), //Doug 07/09
run_years(30),
spinup_out_freq(0),
rampup_out_freq(0), //Doug 07/09
run_out_freq(1),
run_out_years(0) //Doug 06/09: Parameter describing the year from when outputs start.
,co2_var("CO2"),c13_var("C13"),c14_var("C14")
{ }
enum MASK_TYPE { BOOLEAN, PERCENT_LAND };
// Driver data filenames and variable names (variable names default
// to PMIP2 values). The sun_is_cloud flag is used to indicate that
// the input sun values are actually cloud.
string temp_file, prec_file, sun_file, wetdays_file, mask_file;
string tmin_file, tmax_file, windsp_file;
string temp_var, prec_var, sun_var, wetdays_var, mask_var;
string tmin_var, tmax_var, windsp_var;
bool sun_is_cloud;
MASK_TYPE mask_type;
bool wetdays_fixed;
float fixed_wetdays;
// Soil type data - either a single fixed value or a filename for a
// file on the same grid as the climate data.
bool soil_type_fixed;
int fixed_soil_type;
string soil_type_file;
string soil_type_var;
// Population density and human fire ignition data.
string ann_popdens_file;
string ann_popdens_var;
string crop_file,crop_var; //Doug 04/09
bool crop_fixed; //Doug 04/09
float fixed_crop; //Doug 04/09
string pas_file,pas_var; //Doug 04/09
bool pas_fixed; //Doug 04/09
float fixed_pas; //Doug 04/09
string lightn_file;
string lightn_var;
bool a_nd_fixed;
float fixed_a_nd;
string a_nd_file;
string a_nd_var;
// Number of years of spin-up to do (default to 1000), the number of
// years of the climate data to use for the spinup (zero means use
// all years), and the number of years of transient run to do (zero
// means use all the available data once).
int spinup_years, spinup_data_years, run_years;
// Doug 07/09: Number of yearts of "ramping up" from end of the (often
// detrended) spin up, to the point where you want to start doing
// experiments (i.e, spin up in pre-industrial, ramp up to present day,
// then main run for future experiments after present day). Zero ramp_up
// litrally means no ramp up (i.e, for experiments that start at teh end
// of spin up). zero rampup_data_years means use all available years in
// input (provided rampup_years is not equal to zero). Zero
// rampup_out_freq means no ouput during ramp_up.
int rampup_years, rampup_data_years, rampup_out_freq;
// The interval in years between output records written during
// spin-up (default is no output during spin-up, indicated by a zero
// value) and the interval in years between output records written
// during the main part of the run (output values are averaged over
// these intervals, and the default, indicated by a zero value, is
// to write a single averaged record at the end of the run).
// Doug 06/09: run_out_years added. Year after-which ouputs occur.
// initalized at 1 to allow all possible ouputs.
int spinup_out_freq, run_out_freq, run_out_years;
// Doug 07/09: Files and file location where spinup and rampup is I/Oed
string spinup_file, rampup_file;
// CO2 concentration.
float co2_conc[NCO2];
string co2_file, c13_file, c14_north_file, c14_equator_file, c14_south_file;
string co2_var, c13_var, c14_var;
// Output filename.
string output_file;
// Output directory.
string output_dir;
// Output variables.
struct OutVariable {
OutVariable() :
name(""), mean(false), sdev(false), max(false), min(false) { }
OutVariable(string n) :
name(n), mean(true), sdev(false), max(false), min(false) { }
string name;
bool mean, sdev, max, min;
};
vector<OutVariable> output_vars;
};
//----------------------------------------------------------------------
//
// Variable grid definition: derived from driver files.
//
struct Grid {
int nlat, nlon;
vector<float> lats, lons;
Grid() : nlat(0), nlon(0) { }
Grid(string file);
Grid(const Grid &other);
Grid &operator=(const Grid &other);
bool operator==(const Grid &other);
bool operator!=(const Grid &other) { return !(*this == other); }
};
//----------------------------------------------------------------------
//
// Types for handling LPJ output variables.
//
enum LPJ_VAR_TYPE {
SIMPLE,
SIMPLE_MONTHLY,
SIMPLE_DAILY,
PER_PFT,
PER_PFT_MONTHLY,
PER_PFT_DAILY,
TRACE,
TRACE_MONTHLY,
PER_CO2,
PER_CO2_MONTHLY,
//PER_CO2_DAILY,
PER_CO2_PFT,
};
struct LPJVariable {
const char *name;
LPJ_VAR_TYPE type;
};
struct LPJOutputVariable {
LPJOutputVariable(LPJVariable *in_def, RunParameters::OutVariable &collect);
~LPJOutputVariable();
void set_nc(int idx, NcFile &nc);
void reset(void) { clear_buffs(); out_index = 0; }
void clear_buffs(void);
bool average_and_output(void);
void transpose_buffers(int time_axis_len, int other_axis_len);
LPJVariable *def;
NcVar **annual_nc, **mean_nc, **sdev_nc, **min_nc, **max_nc;
int buff_size;
float *annual_buff, *avg_buff, *sdev_buff, *min_buff, *max_buff;
bool annual,mean, sdev, min, max;
int accum_years;
int out_index;
};
//======================================================================
//
// LPJ VARIABLE DEFINITIONS
//
//======================================================================
// All the variable names we recognise from LPJ.
LPJVariable LPJ_VARIABLES[] = {
{ "dhuman_ign", SIMPLE_DAILY }, // Yan: 24/10/07
{ "dlightn", SIMPLE_DAILY }, // Yan: 24/10/07
{ "nind", PER_PFT },
{ "lm_ind", PER_CO2_PFT },
{ "lm_inc", PER_PFT },
{ "rm_ind", PER_CO2_PFT },
{ "sm_ind", PER_CO2_PFT },
{ "hm_ind", PER_CO2_PFT },
{ "fpc_grid", PER_PFT },
{ "anpp_sum", PER_CO2 },
{ "anpp", PER_CO2_PFT }, // Yan: 14/10/07
{ "anpp_pft_sum", PER_CO2 }, // Yan: 14/10/07
{ "acflux_estab", PER_CO2 },
{ "carbon_grid", PER_CO2 },
{ "litter_ag", PER_CO2_PFT },
{ "litter_ag_leaf",PER_CO2_PFT },
{ "litter_ag_wood",PER_CO2_PFT },
{ "litter_bg", PER_CO2_PFT },
{ "cpool_fast", PER_CO2 },
{ "cpool_slow", PER_CO2 },
{ "arh", PER_CO2 },
{ "afire_frac", SIMPLE },
{ "afire_frac_human", SIMPLE }, // Yan: 22/11/07
{ "afire_frac_lightn", SIMPLE }, // Yan: 22/11/07
{ "acflux_fire", PER_CO2 },
{ "mcflux_fire", PER_CO2_MONTHLY },
{ "mcflux_fire_grid",SIMPLE_MONTHLY},
{ "arunoff", SIMPLE },
{ "sla", PER_PFT },
{ "mpar", SIMPLE_MONTHLY },
{ "mapar", SIMPLE_MONTHLY },
{ "mphen", PER_PFT_MONTHLY },
{ "anpp_add", PER_CO2 },
{ "mnpp", PER_PFT_MONTHLY },
{ "mnpp_grid", SIMPLE_MONTHLY }, //Doug 06/09
{ "mrunoff", SIMPLE_MONTHLY },
{ "aaet", SIMPLE },
{ "mrh", PER_CO2_MONTHLY },
{ "mrh_grid", SIMPLE_MONTHLY },
{ "mnpp_add", PER_CO2_MONTHLY },
{ "maet", SIMPLE_MONTHLY },
{ "mgpp", PER_PFT_MONTHLY },
{ "mtemp_soil", SIMPLE_MONTHLY },
{ "mcica", PER_PFT_MONTHLY },
{ "lresp", PER_PFT_MONTHLY },
{ "sresp", PER_PFT_MONTHLY },
{ "rresp", PER_PFT_MONTHLY },
{ "gresp", PER_PFT_MONTHLY },
{ "aresp", PER_PFT_MONTHLY },
{ "mw1", SIMPLE_MONTHLY },
{ "mw2", SIMPLE_MONTHLY },
{ "maep", SIMPLE_MONTHLY },
{ "aaep", SIMPLE },
{ "mpet_grid", SIMPLE_MONTHLY },
{ "apet_grid", SIMPLE },
{ "mintc", SIMPLE_MONTHLY },
{ "aintc", SIMPLE },
{ "meangc", PER_PFT_MONTHLY },
{ "mgp", PER_PFT_MONTHLY },
{ "num_fire", SIMPLE_MONTHLY },
{ "num_fire_human", SIMPLE_MONTHLY }, // Yan 22/11/07
{ "num_fire_lightn", SIMPLE_MONTHLY }, // Yan 22/11/07
{ "annum_fire", SIMPLE },
{ "annum_fire_human", SIMPLE }, // Yan 22/11/07
{ "annum_fire_lightn", SIMPLE }, // Yan 22/11/07
{ "area_burnt", SIMPLE_MONTHLY },
{ "area_burnt_human", SIMPLE_MONTHLY }, // Yan 22/11/07
{ "area_burnt_lightn", SIMPLE_MONTHLY }, // Yan 22/11/07
{ "an_areafires", SIMPLE },
{ "an_areafires_human", SIMPLE }, // Yan 22/11/07
{ "an_areafires_lightn", SIMPLE }, // Yan 22/11/07
{ "char_net_fuel_0", SIMPLE },
{ "livegrass_0", SIMPLE },
{ "dead_fuel_0", SIMPLE },
{ "dead_fuel_all_0", SIMPLE },
{ "fuel_all_0", SIMPLE },
{ "fuel_1hr_total_0", SIMPLE },
{ "fuel_10hr_total_0", SIMPLE },
{ "fuel_100hr_total_0", SIMPLE },
{ "fuel_1000hr_total_0", SIMPLE },
{ "mfuel_1hr_total", SIMPLE_MONTHLY }, //Doug 03/09
{ "mfuel_1hr_leaf_total", SIMPLE_MONTHLY }, //Doug 03/09
{ "mfuel_1hr_wood_total", SIMPLE_MONTHLY }, //Doug 03/09
{ "mfuel_10hr_total", SIMPLE_MONTHLY }, //Doug 03/09
{ "mfuel_100hr_total", SIMPLE_MONTHLY }, //Doug 03/09
{ "mfuel_1000hr_total", SIMPLE_MONTHLY }, //Doug 03/09
{ "mlivegrass", SIMPLE_MONTHLY }, //Doug 05/09
// { "fuel_1000hr_total_0", SIMPLE },
{ "deltaa", SIMPLE },
{ "deltaa_fpc", SIMPLE },
{ "delt_c13_fpc", SIMPLE },
{ "fbdep", SIMPLE },
{ "mfdi", SIMPLE_MONTHLY },
{ "mfire_frac", SIMPLE_MONTHLY },
{ "an_fdi", SIMPLE },
{ "litter_decom_ave", SIMPLE },
{ "turnover_ind", SIMPLE },
{ "an_fseason", SIMPLE },
{ "acflux_trace", TRACE },
{ "mcflux_trace", TRACE_MONTHLY },
{ "m_fc_crown", SIMPLE_MONTHLY },
{ "an_fc_crown", SIMPLE },
{ "m_i_surface", SIMPLE_MONTHLY },
{ "an_i_surface", SIMPLE },
{ "gdd", PER_PFT },
{ "height", PER_PFT },
{ "height_grid", SIMPLE },
// { "mgpp", PER_PFT_MONTHLY },
{ "wu", SIMPLE },
{ "wl", SIMPLE },
{ "dphen", PER_PFT_DAILY },
{ "dphen_change", PER_PFT_MONTHLY }, //Doug 05/09
{ "fbare", SIMPLE },
//sarah
// { "soilc", PER_CO2 },
{ "crop", SIMPLE }, //Doug 05/09: proportion of cell with crops
{ "pas", SIMPLE }, //Doug 05/09: proportion of cell with pasture
{ "anpp_grid", PER_PFT }, //Doug 07/09: cheat annual npp without the deltaC's
{ "arh_grid", SIMPLE }, //Doug 07/09: cheat again, for hetro. resp.
{ "acflux_fire_grid", SIMPLE }, //Doug 07/09: another cherat for carbon burnt
{ "gdd_grid", SIMPLE }, //Doug 07/09: bioclimatic varible growing degree days base 5
{ "alpha_ws", SIMPLE }, //Doug 07/09: Bioclimatic varible for water stress
{ "pfuel_limit", SIMPLE }, //Doug 12/12: proportion of days where fire is limited by fuel
{ "dprec_out", SIMPLE_DAILY }, //Doug 12/12: proportion of days where fire is limited by fuel
{ "BTparam1", PER_CO2_PFT }, //Doug 12/12: proportion of days where fire is limited by fuel
{ "BTparam2", PER_CO2_PFT }, //Doug 12/12: proportion of days where fire is limited by fuel
{ "cgf", SIMPLE_MONTHLY }, //Doug 12/10: cload-to-ground fraction
{ "fdry", SIMPLE_MONTHLY }, //Doug 12/10: cload-to-ground fraction
{ "lt_days", SIMPLE_MONTHLY }, //Doug 12/10: cload-to-ground fraction
{ "mlightn_eff", SIMPLE_MONTHLY }, //Doug 12/10: cload-to-ground fraction
{ "dlm_1hr_old", SIMPLE },
{ "mlm", SIMPLE_MONTHLY }
};
//======================================================================
//
// GLOBAL VARIABLE DEFINITIONS
//
//======================================================================
RunParameters params; // Run parameters.
Grid grid; // Common variable grid definition.
NcFile *clim_temp_nc; // Climate data files.
NcFile *clim_precip_nc;
NcFile *clim_sun_nc;
NcFile *clim_wetdays_nc;
NcFile *clim_tmin_nc;
NcFile *clim_tmax_nc;
NcFile *clim_windsp_nc;
NcFile *clim_lightn_nc;
NcFile *clim_ann_popdens_nc;
NcFile *clim_crop_nc; // Doug 04/09
NcFile *clim_pas_nc; // Doug 04/09
NcVar *clim_temp_var; // Climate data variables.
NcVar *clim_precip_var;
NcVar *clim_sun_var;
NcVar *clim_wetdays_var;
NcVar *clim_tmin_var;
NcVar *clim_tmax_var;
NcVar *clim_windsp_var;
NcVar *clim_lightn_var;
NcVar *clim_ann_popdens_var;
NcVar *clim_crop_var; // Doug 04/09
NcVar *clim_pas_var; // Doug 04/09
BoolArray *land_mask; // Land mask.
IntArray *soil_type; // Soil type data.
Array *a_nd_vals; // Human fire ignition data.
int no_of_months; // Climate data size (in months).
float *clim_temp; // Climate data for current point.
float *clim_precip;
float *clim_sun;
float *clim_wetdays;
float *clim_tmin;
float *clim_tmax;
float *clim_windsp;
float *clim_lightn;
float *clim_ann_popdens;
float *clim_crop; // Doug 04/09
float *clim_pas; // Doug 04/09
int land_points, points_done; // Point counters.
int latidx, lonidx;
NcFile **out_nc; // Output files.
int out_year_count;
int out_idx;
int *out_years;
NcDim *lat_dim; // Output dimensions.
NcDim *lon_dim;
NcDim *pft_dim;
NcDim *month_dim; // Month index.
bool month_dim_needed = false;
NcDim *day_dim; // Day-of-year index.
bool day_dim_needed = false;
NcDim *trace_dim; // Trace gas index.
bool trace_dim_needed = false;
NcDim *co2_dim;
bool co2_dim_needed = false;
LPJOutputVariable **out_vars = 0; // Output variables.
int out_var_count = 0;
float * co2_vals;
float * c13_vals;
float * c14_north_vals;
float * c14_equator_vals;
float * c14_south_vals;
//======================================================================
//
// LOCAL FUNCTION PROTOTYPES
//
//======================================================================
static void read_config(RunParameters ¶ms);
static bool boolean_string(string value, int line, bool &error_flag);
static void read_mask(BoolArray *mask, string mask_file, Grid &grid,
string var, RunParameters::MASK_TYPE mask_type);
static void read_soil_type(IntArray *soil_type, string soil_type_file,
Grid &grid, string soil_type_var);
static void read_a_nd(Array *a_nd, string a_nd_file,
Grid &grid, string a_nd_var);
static void check_var_grid(NcVar *var, NcFile *nc,
string var_name, string file_name);
static void open_output_files(string file_name, string directory_name);
static void handle_output_record(string name, float *data);
static int read_co2(string var_name, string file_name, float **data);
//======================================================================
//
// LPJ INTERFACE FUNCTION DEFINITIONS
//
//======================================================================
//--------------------------------------------------------------------
//
// initio
//
// Reads input climate and land mask files, initialises for looping
// over the grid cells and time.
//
// In fortran : real dummy_array(1:2,1:3,1:4)
typedef float (*DummyFortranArray)[3][2];
extern "C" void initio_(DummyFortranArray dummy_array)
{
// Check array order
float dummy = 1.0;
for( int i = 0; i < 2; ++i ) {
for( int j = 0; j < 3; ++j ) {
for( int k = 0; k < 4; ++k ) {
if( dummy_array[k][j][i] != dummy ) {
cerr << "FATAL ERROR : layout of fortran arrays in C++ is not what was expected\n";
exit( -1 );
}
// As a float[]
float * d = (float *)dummy_array;
if( d[(( k * 3 ) + j ) * 2 + i] != dummy ) {
cerr << "FATAL ERROR : layout of fortran arrays in C++ is not what was expected\n";
exit( -1 );
}
dummy += 1;
}
}
}
// Read the configuration file.
read_config(params);
// Extract grid information from input files.
Grid mask_grid(params.mask_file);
Grid temp_grid(params.temp_file);
Grid prec_grid(params.prec_file);
Grid sun_grid(params.sun_file);
Grid tmin_grid(params.tmin_file);
Grid tmax_grid(params.tmax_file);
Grid windsp_grid(params.windsp_file);
Grid popdens_grid(params.ann_popdens_file);
Grid lightn_grid(params.lightn_file);
if (mask_grid != temp_grid || prec_grid != sun_grid ||
mask_grid != prec_grid || mask_grid != tmin_grid ||
mask_grid != tmax_grid || mask_grid != windsp_grid ||
mask_grid != popdens_grid || mask_grid != lightn_grid )
{
cerr << "ERROR:c Incompatible grid definitions in driver files" << endl;
exit(1);
}
grid = mask_grid;
if (!params.wetdays_fixed) {
Grid wetdays_grid(params.wetdays_file);
if (grid != wetdays_grid) {
cerr << "ERROR:wd Incompatible grid definitions in driver file wetdays" << endl;
exit(1);
}
}
//Doug 04/09: crops and pasture annual fraction
if (!params.crop_fixed) {
Grid crop_grid(params.crop_file);
if (grid != crop_grid) {
cerr << "ERROR:wd Incompatible grid definitions in driver file crops" << endl;
exit(1);
}
}
if (!params.pas_fixed) {
Grid pas_grid(params.pas_file);
if (grid != pas_grid) {
cerr << "ERROR:wd Incompatible grid definitions in driver file pasture" << endl;
exit(1);
}
}
if (!params.soil_type_fixed) {
Grid soil_type_grid(params.soil_type_file);
if (soil_type_grid != grid) {
cerr << "ERROR:st Incompatible grid definitions in driver file soil type" << endl;
exit(1);
}
}
if (!params.a_nd_fixed) {
Grid a_nd_grid(params.a_nd_file);
if (a_nd_grid != grid) {
cerr << "ERROR: Incompatible grid definitions in driver files a(Nd)" << endl;
exit(1);
}
}
// Read the land/sea mask and convert to a boolean array.
land_mask = new BoolArray(grid.nlon, grid.nlat);
read_mask(land_mask, params.mask_file, grid,
params.mask_var, params.mask_type);
// Read the soil type data, if necessary.
if (!params.soil_type_fixed) {
soil_type = new IntArray(grid.nlon, grid.nlat);
read_soil_type(soil_type, params.soil_type_file,
grid, params.soil_type_var);
}
// Read the population density data,human ignition risk and lightening
//
if (!params.a_nd_fixed) {
a_nd_vals = new Array(grid.nlon, grid.nlat);
read_a_nd(a_nd_vals, params.a_nd_file, grid, params.a_nd_var);
}
// Open all climate input data files.
clim_temp_nc = new NcFile(params.temp_file.c_str());
if (!clim_temp_nc->is_valid()) {
cerr << "ERROR: could not open climate file " << params.temp_file << endl;
exit(1);
}
if (params.prec_file == params.temp_file)
clim_precip_nc = clim_temp_nc;
else {
clim_precip_nc = new NcFile(params.prec_file.c_str());
if (!clim_precip_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.prec_file << endl;
exit(1);
}
}
if (params.sun_file == params.temp_file)
clim_sun_nc = clim_temp_nc;
else if (params.sun_file == params.prec_file)
clim_sun_nc = clim_precip_nc;
else {
clim_sun_nc = new NcFile(params.sun_file.c_str());
if (!clim_sun_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.sun_file << endl;
exit(1);
}
}
if (!params.wetdays_fixed) {
if (params.wetdays_file == params.temp_file)
clim_wetdays_nc = clim_temp_nc;
else if (params.wetdays_file == params.prec_file)
clim_wetdays_nc = clim_precip_nc;
else if (params.wetdays_file == params.sun_file)
clim_wetdays_nc = clim_sun_nc;
else {
clim_wetdays_nc = new NcFile(params.wetdays_file.c_str());
if (!clim_wetdays_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.wetdays_file << endl;
exit(1);
}
}
}
if (params.tmin_file == params.temp_file)
clim_tmin_nc = clim_temp_nc;
else if (params.tmin_file == params.prec_file)
clim_tmin_nc = clim_precip_nc;
else if (params.tmin_file == params.sun_file)
clim_tmin_nc = clim_sun_nc;
else if (!params.wetdays_fixed && params.tmin_file == params.wetdays_file)
clim_tmin_nc = clim_wetdays_nc;
else {
clim_tmin_nc = new NcFile(params.tmin_file.c_str());
if (!clim_tmin_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.tmin_file << endl;
exit(1);
}
}
if (params.tmax_file == params.temp_file)
clim_tmax_nc = clim_temp_nc;
else if (params.tmax_file == params.prec_file)
clim_tmax_nc = clim_precip_nc;
else if (params.tmax_file == params.sun_file)
clim_tmax_nc = clim_sun_nc;
else if (!params.wetdays_fixed && params.tmax_file == params.wetdays_file)
clim_tmax_nc = clim_wetdays_nc;
else if (params.tmax_file == params.tmin_file)
clim_tmax_nc = clim_tmin_nc;
else {
clim_tmax_nc = new NcFile(params.tmax_file.c_str());
if (!clim_tmax_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.tmax_file << endl;
exit(1);
}
}
if (params.windsp_file == params.temp_file)
clim_windsp_nc = clim_temp_nc;
else if (params.windsp_file == params.prec_file)
clim_windsp_nc = clim_precip_nc;
else if (params.windsp_file == params.sun_file)
clim_windsp_nc = clim_sun_nc;
else if (!params.wetdays_fixed && params.windsp_file == params.wetdays_file)
clim_windsp_nc = clim_wetdays_nc;
else if (params.windsp_file == params.tmin_file)
clim_windsp_nc = clim_tmin_nc;
else if (params.windsp_file == params.tmax_file)
clim_windsp_nc = clim_tmax_nc;
else {
clim_windsp_nc = new NcFile(params.windsp_file.c_str());
if (!clim_windsp_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.windsp_file << endl;
exit(1);
}
}
//Kirsten: 12-month lightning climatology
if (params.lightn_file == params.temp_file)
clim_lightn_nc = clim_temp_nc;
else if (params.lightn_file == params.prec_file)
clim_lightn_nc = clim_precip_nc;
else if (params.lightn_file == params.sun_file)
clim_lightn_nc = clim_sun_nc;
else if (!params.wetdays_fixed && params.lightn_file == params.wetdays_file)
clim_lightn_nc = clim_wetdays_nc;
else if (params.lightn_file == params.tmin_file)
clim_lightn_nc = clim_tmin_nc;
else if (params.lightn_file == params.tmax_file)
clim_lightn_nc = clim_tmax_nc;
else if (params.lightn_file == params.windsp_file)
clim_lightn_nc = clim_windsp_nc;
else {
clim_lightn_nc = new NcFile(params.lightn_file.c_str());
if (!clim_lightn_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.lightn_file << endl;
exit(1);
}
}
//Kirsten: annual population density
if (params.ann_popdens_file == params.temp_file)
clim_ann_popdens_nc = clim_temp_nc;
else if (params.ann_popdens_file == params.prec_file)
clim_ann_popdens_nc = clim_precip_nc;
else if (params.ann_popdens_file == params.sun_file)
clim_ann_popdens_nc = clim_sun_nc;
else if (!params.wetdays_fixed && params.ann_popdens_file == params.wetdays_file)
clim_ann_popdens_nc = clim_wetdays_nc;
else if (params.ann_popdens_file == params.tmin_file)
clim_ann_popdens_nc = clim_tmin_nc;
else if (params.ann_popdens_file == params.tmax_file)
clim_ann_popdens_nc = clim_tmax_nc;
else if (params.ann_popdens_file == params.windsp_file)
clim_ann_popdens_nc = clim_windsp_nc;
else {
clim_ann_popdens_nc = new NcFile(params.ann_popdens_file.c_str());
if (!clim_ann_popdens_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.ann_popdens_file << endl;
exit(1);
}
}
//Doug 04/09: crops and pasture fraction
if (!params.crop_fixed) {
if (params.crop_file == params.temp_file)
clim_crop_nc = clim_temp_nc;
else if (params.crop_file == params.prec_file)
clim_crop_nc = clim_precip_nc;
else if (params.crop_file == params.sun_file)
clim_crop_nc = clim_sun_nc;
else if (!params.wetdays_fixed && params.crop_file == params.wetdays_file)
clim_crop_nc = clim_wetdays_nc;
else if (params.crop_file == params.tmin_file)
clim_crop_nc = clim_tmin_nc;
else if (params.crop_file == params.tmax_file)
clim_crop_nc = clim_tmax_nc;
else if (params.crop_file == params.windsp_file)
clim_crop_nc = clim_windsp_nc;
else if (params.crop_file == params.ann_popdens_file)
clim_crop_nc = clim_ann_popdens_nc;
else {
clim_crop_nc = new NcFile(params.crop_file.c_str());
if (!clim_crop_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.crop_file << endl;
exit(1);
}
}
}
if (!params.pas_fixed) {
if (params.pas_file == params.temp_file)
clim_pas_nc = clim_temp_nc;
else if (params.pas_file == params.prec_file)
clim_pas_nc = clim_precip_nc;
else if (params.pas_file == params.sun_file)
clim_pas_nc = clim_sun_nc;
else if (!params.wetdays_fixed && params.pas_file == params.wetdays_file)
clim_pas_nc = clim_wetdays_nc;
else if (params.pas_file == params.tmin_file)
clim_pas_nc = clim_tmin_nc;
else if (params.pas_file == params.tmax_file)
clim_pas_nc = clim_tmax_nc;
else if (params.pas_file == params.windsp_file)
clim_pas_nc = clim_windsp_nc;
else if (params.pas_file == params.ann_popdens_file)
clim_pas_nc = clim_ann_popdens_nc;
else if (!params.crop_fixed && params.pas_file == params.crop_file)
clim_pas_nc = clim_wetdays_nc;
else {
clim_pas_nc = new NcFile(params.pas_file.c_str());
if (!clim_pas_nc->is_valid()) {
cerr << "ERROR: could not open climate file "
<< params.pas_file << endl;
exit(1);
}
}
}
// Access all climate variables.
clim_temp_var = clim_temp_nc->get_var(params.temp_var.c_str());
if (!clim_temp_var) {
cerr << "ERROR: could not access climate variable '"
<< params.temp_var << "'" << endl;
exit(1);
}
check_var_grid(clim_temp_var, clim_temp_nc,
params.temp_var, params.temp_file);
clim_precip_var = clim_precip_nc->get_var(params.prec_var.c_str());
if (!clim_precip_var) {
cerr << "ERROR: could not access climate variable '"
<< params.prec_var << "'" << endl;
exit(1);
}
check_var_grid(clim_precip_var, clim_precip_nc,
params.prec_var, params.prec_file);
clim_sun_var = clim_sun_nc->get_var(params.sun_var.c_str());
if (!clim_sun_var) {
cerr << "ERROR: could not access climate variable '"
<< params.sun_var << "'" << endl;
exit(1);
}
check_var_grid(clim_sun_var, clim_sun_nc,
params.sun_var, params.sun_file);
if (!params.wetdays_fixed) {
clim_wetdays_var = clim_wetdays_nc->get_var(params.wetdays_var.c_str());
if (!clim_wetdays_var) {
cerr << "ERROR: could not access climate variable '"
<< params.wetdays_var << "'" << endl;
exit(1);
}
check_var_grid(clim_wetdays_var, clim_wetdays_nc,
params.wetdays_var, params.wetdays_file);
}
clim_tmin_var = clim_tmin_nc->get_var(params.tmin_var.c_str());
if (!clim_tmin_var) {
cerr << "ERROR: could not access climate variable '"
<< params.tmin_var << "'" << endl;
exit(1);
}
check_var_grid(clim_tmin_var, clim_tmin_nc,
params.tmin_var, params.tmin_file);
clim_tmax_var = clim_tmax_nc->get_var(params.tmax_var.c_str());
if (!clim_tmax_var) {
cerr << "ERROR: could not access climate variable '"
<< params.tmax_var << "'" << endl;
exit(1);
}
check_var_grid(clim_tmax_var, clim_tmax_nc,
params.tmax_var, params.tmax_file);
clim_windsp_var = clim_windsp_nc->get_var(params.windsp_var.c_str());
if (!clim_windsp_var) {
cerr << "ERROR: could not access climate variable '"
<< params.windsp_var << "'" << endl;
exit(1);
}
check_var_grid(clim_windsp_var, clim_windsp_nc,
params.windsp_var, params.windsp_file);
clim_lightn_var = clim_lightn_nc->get_var(params.lightn_var.c_str());
if (!clim_lightn_var) {
cerr << "ERROR: could not access climate variable '"
<< params.lightn_var << "'" << endl;
exit(1);
}
check_var_grid(clim_lightn_var, clim_lightn_nc,
params.lightn_var, params.lightn_file);
clim_ann_popdens_var = clim_ann_popdens_nc->get_var(params.ann_popdens_var.c_str());
if (!clim_ann_popdens_var) {
cerr << "ERROR: could not access climate variable '"
<< params.ann_popdens_var << "'" << endl;
exit(1);
}
check_var_grid(clim_ann_popdens_var, clim_ann_popdens_nc,
params.ann_popdens_var, params.ann_popdens_file);
//Doug 04/09: Annual crops and pasture
if (!params.crop_fixed) {
clim_crop_var = clim_crop_nc->get_var(params.crop_var.c_str());
if (!clim_crop_var) {
cerr << "ERROR: could not access climate variable '"
<< params.crop_var << "'" << endl;
exit(1);
}
}
check_var_grid(clim_crop_var, clim_crop_nc,
params.crop_var, params.crop_file);
if (!params.pas_fixed) {
clim_pas_var = clim_pas_nc->get_var(params.pas_var.c_str());
if (!clim_pas_var) {
cerr << "ERROR: could not access climate variable '"
<< params.pas_var << "'" << endl;
exit(1);
}
}
check_var_grid(clim_pas_var, clim_pas_nc,
params.pas_var, params.pas_file);
// Check the time bounds on the climate variables. There should be
// a whole number of years of data, and there should be the same
// amount of data for each variable.
NcDim *temp_time_dim = clim_temp_var->get_dim(0);
NcDim *precip_time_dim = clim_precip_var->get_dim(0);
NcDim *sun_time_dim = clim_sun_var->get_dim(0);
NcDim *tmin_time_dim = clim_tmin_var->get_dim(0);
NcDim *tmax_time_dim = clim_tmax_var->get_dim(0);
NcDim *windsp_time_dim = clim_windsp_var->get_dim(0);
NcDim *wetdays_time_dim = clim_wetdays_var->get_dim(0);
// Yan: put windsp the same as other climate varaibles
// if (windsp_time_dim->size() != NMON) {
// cerr << "ERROR: bad time dimension in windspeed file" << endl;
// exit(1);
// }
NcDim *lightn_time_dim = clim_lightn_var->get_dim(0);
if (lightn_time_dim->size() != NMON) {
cerr << "ERROR: bad time dimension in lightn file" << endl;
exit(1);
}
NcDim *ann_popdens_time_dim = clim_ann_popdens_var->get_dim(0);
NcDim *crop_time_dim = clim_crop_var->get_dim(0); //Doug 04/09
NcDim *pas_time_dim = clim_pas_var->get_dim(0); //Doug 04/09
if (temp_time_dim->size() != precip_time_dim->size() ||
temp_time_dim->size() != sun_time_dim->size() ||
temp_time_dim->size() != windsp_time_dim->size() ||
temp_time_dim->size() != wetdays_time_dim->size() ||
temp_time_dim->size() != tmin_time_dim->size() ||
temp_time_dim->size() != tmax_time_dim->size()
|| temp_time_dim->size() != ann_popdens_time_dim ->size() * NMON
|| temp_time_dim->size() != crop_time_dim->size()*NMON //Doug 04/09
|| temp_time_dim->size() != pas_time_dim->size()*NMON //Doug 04/09
) {
cerr << "ERROR: incompatible time dimensions in popdens climate files" << endl;
exit(1);
}