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
|
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use File::Slurp qw(read_file write_file);
use JSON;
use List::Util qw(any);
use XML::LibXML;
my $json_str = read_file('stations.json');
my $stations = JSON->new->utf8->decode($json_str);
@{$stations} = sort { $a->{name} cmp $b->{name} } @{$stations};
my $xml_str = read_file('stations.xml');
my $tree = XML::LibXML->load_xml( string => $xml_str );
my @missing;
if ( -e "missing.txt" ) {
for my $line ( read_file("missing.txt") ) {
chomp $line;
push( @missing, $line );
}
}
for my $station ( $tree->findnodes('//station') ) {
my $name = $station->getAttribute('name');
my $eva = $station->getAttribute('eva');
my $ds100 = $station->getAttribute('ds100');
my $is_db = $station->getAttribute('db') eq 'true';
my $found = 0;
for my $j_station ( @{$stations} ) {
my $j_name = $j_station->{name};
my $j_ds100 = $j_station->{ds100};
my $j_eva = $j_station->{eva};
if ( $name eq $j_name or $eva == $j_eva ) {
$found = 1;
}
if ( $j_ds100 eq $ds100 and $j_name ne $name ) {
printf( "%8s has been renamed: %30s -> %30s\n",
$ds100, $j_name, $name );
#$j_station->{name} = $name;
last;
}
elsif ( $j_eva == $eva and $j_name ne $name ) {
printf( "%d mismatch: (%s -> %s), (%s -> %s)\n",
$eva, $j_name, $name, $j_ds100, $ds100 );
last;
}
elsif ( $j_name eq $name
and $j_ds100 ne $ds100
and $is_db
and $ds100 !~ m{ ^ PQ }x )
{
printf( "%30s has been recoded: %8s -> %8s\n",
$name, $j_ds100, $ds100 );
last;
}
elsif ( $j_name eq $name
and $j_eva != $eva
and $is_db
and $ds100 !~ m{ ^ PQ }x )
{
printf( "%30s has been recoded: %d -> %d\n", $name, $j_eva, $eva );
last;
}
}
if ( not $found
and any { $_ eq $name } @missing )
{
say "missing $eva $ds100 \"$name\"";
push(
@{$stations},
{
name => $name,
ds100 => $ds100,
eva => $eva,
}
);
}
}
my @to_delete;
for my $i ( 0 .. $#{$stations} ) {
my $j_station = $stations->[$i];
my $j_name = $j_station->{name};
my $j_ds100 = $j_station->{ds100};
my $j_eva = $j_station->{eva};
my $found = 0;
for my $station ( $tree->findnodes('//station') ) {
my $name = $station->getAttribute('name');
my $eva = $station->getAttribute('eva');
my $ds100 = $station->getAttribute('ds100');
my $is_db = $station->getAttribute('db') eq 'true';
if ( $name eq $j_name or $eva == $j_eva ) {
$found = 1;
}
}
if ( not $found ) {
say "station no longer exists: $j_eva $j_ds100 \"$j_name\"";
unshift( @to_delete, $i );
}
}
for my $i (@to_delete) {
splice( @{$stations}, $i, 1 );
}
my $json_out = JSON->new->utf8->canonical->pretty->encode($stations);
write_file( 'stations.json', $json_out );
|