-
Notifications
You must be signed in to change notification settings - Fork 10
/
ReadConfigurationFile.m
132 lines (108 loc) · 3.98 KB
/
ReadConfigurationFile.m
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
% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
% Reads a configuration file.
%
% INPUT:
% fname = Configuration file name.
%
% Gautam Bisht (gbisht@lbl.gov)
% 05-28-2015
% +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function cfg = ReadConfigurationFile(fname)
% Initialization
cfg.site_latlon_filename = '';
cfg.clm_gridded_surfdata_filename = '';
cfg.clm_gridded_domain_filename = '';
cfg.clm_usrdat_name = '';
cfg.dlat = 0;
cfg.dlon = 0;
cfg.lon_min = -999;
cfg.lon_max = -999;
cfg.set_natural_veg_frac_to_one = 0;
cfg.landuse_timeseries_filename = '';
% Read the file
fid = fopen (fname,'r');
if (fid < 0)
error(['Unable to open file: ' fname]);
end
while ~feof(fid)
line = fgetl(fid);
if (~isempty(line))
if (strcmp(line(1),'%') == 0)
tmp_string = strsplit(line,' ');
if (length(tmp_string) ~= 2)
error(['Incorrect enrty: ' line])
end
switch lower(tmp_string{1})
case 'site_latlon_filename'
cfg.site_latlon_filename = tmp_string{2};
case 'clm_gridded_surfdata_filename'
cfg.clm_gridded_surfdata_filename = tmp_string{2};
case 'clm_gridded_domain_filename'
cfg.clm_gridded_domain_filename = tmp_string{2};
case 'clm_usrdat_name'
cfg.clm_usrdat_name = tmp_string{2};
case 'dlat'
cfg.dlat = str2double(tmp_string{2});
case 'dlon'
cfg.dlon = str2double(tmp_string{2});
case 'lon_min'
cfg.lon_min = str2double(tmp_string{2});
case 'lon_max'
cfg.lon_max = str2double(tmp_string{2});
case 'set_natural_veg_frac_to_one'
cfg.set_natural_veg_frac_to_one = str2double(tmp_string{2});
case 'landuse_timeseries_filename'
cfg.landuse_timeseries_filename = tmp_string{2};
otherwise
error(['Unknown variable: ' tmp_string{1}])
end
end
end
end
fclose(fid);
% Do some error checking
if (isempty(cfg.site_latlon_filename))
error(['Stopping because entry for site_latlon_filename not found in ' fname])
end
if (isempty(cfg.clm_gridded_surfdata_filename))
error(['Stopping because entry for clm_gridded_surfdata_filename not found in ' fname])
end
if (isempty(cfg.clm_gridded_domain_filename))
error(['Stopping because entry for clm_gridded_domain_filename not found in ' fname])
end
if (isempty(cfg.clm_usrdat_name))
error(['Stopping because entry for clm_usrdat_name not found in ' fname])
end
if (cfg.dlat == 0)
error(['Stopping because entry for dlat not found in ' fname])
end
if (cfg.dlon == 0)
error(['Stopping because entry for dlon not found in ' fname])
end
if (cfg.lon_min == -999)
error(['Stopping because entry for lon_min not found in ' fname])
end
if (cfg.lon_max == -999)
error(['Stopping because entry for lon_max not found in ' fname])
end
if (cfg.dlat < 0)
error(['Stopping because dlat is negative in ' fname])
end
if (cfg.dlon < 0)
error(['Stopping because dlon is negative in ' fname])
end
if ~(cfg.lon_min == 0 && cfg.lon_max == 360)
error(['Stopping because lon_min and lon_max not equal to 0 and 360.'])
end
loc = find(cfg.site_latlon_filename == '/');
if (~isempty(loc))
cfg.out_netcdf_dir = cfg.site_latlon_filename(1:loc(end)-1);
else
cfg.out_netcdf_dir = './';
end
switch cfg.set_natural_veg_frac_to_one
case {0,1}
otherwise
disp('Valid values for set_natural_veg_frac_to_one are 0 or 1')
error(['Stopping because set_natural_veg_frac_to_one is defined as ' num2str(cfg.set_natural_veg_frac_to_one)])
end