-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec_extractor.pl
316 lines (257 loc) · 11.6 KB
/
rec_extractor.pl
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
use strict;
# Author: [email protected]
my $VERSION = '20211102'; # Set operator => 'SKIP' for $vr to suppress time series output
#my $VERSION = '20210622'; # Now using .tm and .info files
#my $VERSION = '20210615'; # LST conversion of UTC is optional - set extract_utc = 0/1 in json input
#my $VERSION = '20210526'; # Added calculation of statistics
#my $VERSION = '20210518'; # Fix to T indexing, manage dates in filenames
#my $VERSION = '20210416'; # Manage UTC data too (e.g. meteo)
#my $VERSION = '20210407';
if ($^O eq 'MSWin32') {
print "This code must be run under Linux or macOS\n";
exit();
}
use FindBin;
use lib $FindBin::Bin;
use Library qw(
init_input
check_input
compute_statistics
extract_values_from_grid
set_info
load_sq_json_file
load_receptors
load_statistics
node
);
use Getopt::Long;
use Data::Dumper;
use File::Basename;
use File::Path;
use IO::Uncompress::Bunzip2 qw(bunzip2 $Bunzip2Error) ;
use Log::Log4perl qw(:easy);
use POSIX qw (strftime floor);
use IPC::Run qw( run );
use Date::Calc qw( Add_Delta_DHMS );
Log::Log4perl->easy_init($INFO);
my $logger = Log::Log4perl->get_logger();
# Read user input from JSON file and load defaults
my $show_help_info = '';
my $ret = init_input();
my @input_list = @{$ret->{input_list}};
my $hinput = $ret->{input};
GetOptions(@input_list,'help'=>\$show_help_info);
my $ret = check_input(Input=>$hinput,Logger=>$logger);
$hinput = $ret->{input};
my %input = %{$hinput};
my $sq = $input{sq};
my $cs = $input{cs};
my $rl = $input{rl};
my $vr = $input{vr};
# Load sequence info
my %sq_json = load_sq_json_file(File=>"$input{home_dir}{json}/$sq.json");
my $date = $sq_json{cases}{"c$cs"}{first_output};
my @start_date = unpack("a4a2a2a2a2",$sq_json{cases}{"c$cs"}{start});
my $missing = $sq_json{cases}{"c$cs"}{releases}{"r$rl"}{variables}{"v$vr"}{missing_value};
my $src_file = "$input{home_dir}{src}/$sq-$cs.src";
# Load statistics
my %statistics = load_statistics(Input=>$hinput);
my $skip_vr_printout = 0;
if (exists($statistics{$vr}{operator}) && ($statistics{$vr}{operator} eq 'SKIP')) {
$skip_vr_printout = 1;
}
# Loop on models
my @models = @{$input{models}};
foreach my $mo (@models) {
my ($base,$vbase,$bz2_file,$ens_file,$dat_file,$out_folder,$dat_log_file);
my $is_datetime_correct = 0;
# Test for correct datetime used in file name (first output datetime)
my $base = "$mo-$sq-$cs-$rl-$vr-$date";
my $vbase = "v$vr-$mo-$sq-$cs-$rl-$date";
my $bz2_file = "$input{home_dir}{bz2}/$mo/$sq/$cs/$base.ens.bz2";
if ($input{go}{bunzip2}) {
# Check date in in bz2 file
if (-e $bz2_file) {
$is_datetime_correct = 1;
} else {
my $other_date = $date;
my $z = substr($other_date,8,2,'00');
$base = "$mo-$sq-$cs-$rl-$vr-$other_date";
$vbase = "v$vr-$mo-$sq-$cs-$rl-$other_date";
my $bz2_file_other = "$input{home_dir}{bz2}/$mo/$sq/$cs/$base.ens.bz2";
if (-e $bz2_file_other) {
$is_datetime_correct = 0;
} else {
die "Please check if $bz2_file or $bz2_file_other exists";
}
$bz2_file = $bz2_file_other;
}
}
my $ens_file = "$input{home_dir}{ens}/s$sq/c$cs/$base.ens";
my $dat_file = "$input{home_dir}{dat}/s$sq/c$cs/r$rl/v$vr/$vbase.dat";
# check ens_file and dat_file if check was not made on bz2_file (in such
# case we have already corrected the date, if necessary).
unless ($input{go}{bunzip2}) {
if (-e $ens_file) {
$is_datetime_correct = 1;
} else {
my $other_date = $date;
my $z = substr($other_date,8,2,'00');
$base = "$mo-$sq-$cs-$rl-$vr-$other_date";
$vbase = "v$vr-$mo-$sq-$cs-$rl-$other_date";
my $ens_file_other = "$input{home_dir}{ens}/s$sq/c$cs/$base.ens";
if (-e $ens_file_other) {
$is_datetime_correct = 0;
} else {
die "Please check if $ens_file or $ens_file_other exists";
}
$ens_file = $ens_file_other;
}
}
my $out_folder = "$input{home_dir}{out}/$sq/$cs/$rl/$vr/$mo";
my $dat_log_file = "$input{home_dir}{dat}/s$sq/c$cs/$base.log";
# Uncompress ens.bz2 grid file (optional)
if ($input{go}{bunzip2}) {
die "$bz2_file: $!" unless (-e $bz2_file);
my ($basename,$path,$suffix) = fileparse($ens_file,'.ens');
if ($input{create_dirs}) {
mkpath $path unless (-e $path);
}
my $status = bunzip2 $bz2_file => $ens_file or die "bunzip2 failed: $Bunzip2Error\n";
}
# Decode ens grid file (optional)
if ($input{go}{deform}) {
# 1,INPUT_PATH
# 2,INPUT_FILE
# 3,SOURCE_FILE
# 4,MODEL_NAME
# 5,UPL_DATE
# 6,DELTA_M
# 7,DELTA_U
# 8,UPL_USER
# 9,UPL_IP
# 10,OUTPUT_BASE_PATH
my $now = strftime("%Y%m%d%H%M", localtime(time));
my $status_c_folder = "$input{home_dir}{dat}/s$sq/c$cs/status_c";
mkpath $status_c_folder unless (-e $status_c_folder);
my $status_r_folder = "$input{home_dir}{dat}/s$sq/c$cs/r$rl/status_r";
mkpath $status_r_folder unless (-e $status_r_folder);
for my $iv (1..$input{vrmax}) {
my $vro = sprintf "%2.2d", $iv;
my $dat_folder = "$input{home_dir}{dat}/s$sq/c$cs/r$rl/v$vro";
mkpath $dat_folder unless (-e $dat_folder);
}
my @command = (
$input{executables}{deform},
"$input{home_dir}{ens}/s$sq/c$cs/",
$base.'.ens',
$src_file,'DUMMY',$now,'+0m','+0m','dummy','127.0.0.1',
"$input{home_dir}{dat}/"
);
my $cmd = join (" ",@command);
run "$cmd > $dat_log_file 2>&1";
}
# Extract UTC from receptors
if ($input{go}{extract}) {
mkpath $out_folder unless (-e $out_folder);
my $precision = $sq_json{cases}{"c$cs"}{releases}{"r$rl"}{variables}{"v$vr"}{precision};
my $decimals = - log($precision) / log(10);
# Generate output_dates
# my @datetimes = generate_datetimes(Start=$sq_json{first_output_utc});
# Load pool of receptors
my %receptors = load_receptors(File=>$input{pool_file},Info=>\%sq_json);
my $nt = $sq_json{nt};
my @ts = (1..$nt);
# Loop on receptors
foreach my $lcode (sort keys %receptors) {
my @xs = (node(Value=>$receptors{$lcode}{lon},GridSize=>$sq_json{dx}));
my @ys = (node(Value=>$receptors{$lcode}{lat},GridSize=>$sq_json{dy}));
my $dutc = $receptors{$lcode}{dutc} || 0;
die "DUTC not integer ($dutc) for $lcode" if (floor($dutc) < $dutc);
# time shift to LST can be overridden in input with extract_utc set to 1
$dutc = 0 if ($input{extract_utc} == 1);
my $fdate_lst = sprintf "%.4d-%.2d-%.2d".'T'."%.2d:%.2d:00", @start_date[0..4];
my @values = extract_values_from_grid(
DatFile =>$dat_file,
Xs => \@xs,
Ys => \@ys,
Ts => \@ts,
Domain => \%sq_json);
my $out_file = "$out_folder/$lcode-$mo-$sq-$cs-$rl-$vr.csv";
if ($skip_vr_printout == 0) {
open(OUT,">$out_file") or die "$out_file:$!";
}
my %varrays;
# Loop on indexes from 1 to nt
foreach my $it_lst (@ts) {
# Apply time shift to time series
# Compute date array by adding to start date the increment in minutes
my @t_date_lst = Add_Delta_DHMS(@start_date,0,
0,0,$it_lst*$sq_json{dt_out},0);
# Convert array to string, for output
my $tdate_lst = sprintf "%.4d-%.2d-%.2d".'T'."%.2d:%.2d:00", @t_date_lst[0..4];
# Create a datetime string for statistics hashes
my $tdate_mask_lst = sprintf "%.4d%.2d%.2d%.2d", @t_date_lst[0..4];
# Shift the loop index by $dutc.
## my $idx_utc = $it_lst - $dutc + 1;
my $idx_utc = $it_lst - $dutc;
# Test if the shifted loop index is positive and not larger than $nt
# If this is true, use it as a pointer to get the value in the time series,
# otherwise set it to missing.
## my $valp = ($idx_utc < 0 or $idx_utc > $#ts) ? $missing : $values[$idx_utc];
my $valp = ($idx_utc < 1 or $idx_utc > $nt) ? $missing : $values[$idx_utc-1];
# Format value with precision
my $valpout = sprintf "%.${decimals}f", $valp;
if ($skip_vr_printout == 0) {
print OUT "$valpout,$fdate_lst,$tdate_lst\n";
}
# Store values for statistics
foreach my $id_statistics (keys %statistics) {
die "Cannot compute statistics unless vr=01 in input\n" if ($vr ne '01');
# This allows to compute a statistics in a subperiod of data
# by defining the proper intervals in the time mask.
# For example, only extract AOT in summer
if (exists($statistics{$id_statistics}{any}{time_mask_to}{$tdate_mask_lst})) {
my $mask_to = $statistics{$id_statistics}{any}{time_mask_to}{$tdate_mask_lst};
push @{$varrays{$id_statistics}{$mask_to}},$valpout;
} elsif (exists($statistics{$id_statistics}{each}{$lcode}{time_mask_to}{$tdate_mask_lst})) {
my $mask_to = $statistics{$id_statistics}{each}{$lcode}{time_mask_to}{$tdate_mask_lst};
push @{$varrays{$id_statistics}{$mask_to}},$valpout;
} else {
next;
}
}
$fdate_lst = $tdate_lst;
}
# Save time series
if ($skip_vr_printout == 0) {
close (OUT);
}
# Apply statistics
foreach my $id_statistics (keys %statistics) {
next unless (exists $varrays{$id_statistics});
my $stat = $statistics{$id_statistics}{operator};
next unless ($stat); # This will skip 01 when it is an empty hash
next if ($stat eq 'SKIP'); #
my $out_folder = "$input{home_dir}{out}/$sq/$cs/$rl/$id_statistics/$mo";
mkpath $out_folder unless (-e $out_folder);
my $out_file = "$out_folder/$lcode-$mo-$sq-$cs-$rl-$id_statistics.csv";
open(CSV,">$out_file") or die "$out_file: $!";
# Process arrays of model values stored for current statistics
my %arrays = %{$varrays{$id_statistics}};
my @to_datetimes = sort keys %arrays;
foreach my $to (@to_datetimes) {
my @array = @{$arrays{$to}};
my $val;
my $from = ($statistics{$id_statistics}{is_any}) ?
$statistics{$id_statistics}{any}{time_mask_from}{$to} :
$statistics{$id_statistics}{each}{$lcode}{time_mask_from}{$to};
$val = compute_statistics(Operator=>$stat,Values=>\@array,Missing=>$missing);
my $valout = sprintf "%.5f", $val;
print CSV "$valout,$from,$to\n";
}
close(CSV);
}
}
}
}