|
105 | 105 | if ($SANITY_CHECK) {
|
106 | 106 | my %map_sn = (); # map of shortNames
|
107 | 107 | my %map_pid = (); # map of paramIds
|
| 108 | + my $sanity_error_count = 0; |
108 | 109 | print "Checking sanity: uniqueness of paramId and shortName keys ...\n";
|
109 | 110 | while (<>) {
|
110 | 111 | chomp;
|
| 112 | + $lcount++; |
111 | 113 | s/\r//g; # Remove DOS carriage returns
|
112 | 114 | if ($first == 1) {
|
113 | 115 | $first = 0;
|
114 | 116 | next;
|
115 | 117 | }
|
116 |
| - $lcount++; |
| 118 | + |
117 | 119 | ($paramId, $shortName, $name, $units) = split(/\t/);
|
118 | 120 |
|
119 |
| - die "Error: shortName=$shortName is duplicated (line ", $lcount+1, ")\n" if (exists $map_sn{$shortName}); |
| 121 | + die "Error: shortName=$shortName is duplicated (line $lcount)\n" if (exists $map_sn{$shortName}); |
120 | 122 | $map_sn{$shortName}++; # increment count in shortName map
|
121 | 123 |
|
122 |
| - die "Error: paramId=$paramId is duplicated (line ", $lcount+1, ")\n" if (exists $map_pid{$paramId}); |
| 124 | + die "Error: paramId=$paramId is duplicated (line $lcount)\n" if (exists $map_pid{$paramId}); |
123 | 125 | $map_pid{$paramId}++; # increment count in paramId map
|
124 | 126 |
|
125 |
| - die "Error: paramId=$paramId is not an integer (line ", $lcount+1, ")\n" if (!is_integer($paramId)); |
| 127 | + if (!is_integer($paramId)) { |
| 128 | + warn "Error: paramId=$paramId is not an integer (line $lcount)\n"; |
| 129 | + $sanity_error_count++; |
| 130 | + } |
126 | 131 |
|
127 | 132 | my $x = $dbh->selectrow_array("select * from param.param where id = ?",undef,$paramId);
|
128 |
| - die "Error: paramId=$x exists in the database (line ", $lcount+1, ")\n" if (defined $x); |
129 |
| - |
130 |
| - die "Error: Name '$name': ends in space" if ($name =~ / $/); |
131 |
| - die "Error: Name '$name': starts with space" if ($name =~ /^ /); |
| 133 | + if (defined $x) { |
| 134 | + warn "Error: paramId=$x exists in the database (line $lcount)\n"; |
| 135 | + $sanity_error_count++; |
| 136 | + } |
132 | 137 |
|
133 |
| - # Will die if it fails |
134 |
| - get_db_units_code($units); |
| 138 | + if ($name =~ / $/) { |
| 139 | + warn "Error: Name '$name': ends in space" ; |
| 140 | + $sanity_error_count++; |
| 141 | + } |
| 142 | + if ($name =~ /^ /) { |
| 143 | + warn "Error: Name '$name': starts with space" ; |
| 144 | + $sanity_error_count++; |
| 145 | + } |
| 146 | + if ($name !~ /^[A-Z0-9]/) { |
| 147 | + warn "Error: name \"$name\" should have uppercase 1st letter or digit (line $lcount)\n"; |
| 148 | + $sanity_error_count++; |
| 149 | + } |
135 | 150 |
|
136 | 151 | $x = $dbh->selectrow_array("select shortName from param.param where shortName = ?",undef,$shortName);
|
137 |
| - die "Error: shortName=$x exists in the database (line ", $lcount+1, ")\n" if (defined $x); |
| 152 | + if (defined $x) { |
| 153 | + warn "Error: shortName=$x exists in the database (line $lcount)\n"; |
| 154 | + $sanity_error_count++; |
| 155 | + } |
| 156 | + |
| 157 | + if (!check_units($units)) { |
| 158 | + warn "Error: Database does not contain units=$units (line $lcount)\n"; |
| 159 | + $sanity_error_count++; |
| 160 | + } |
| 161 | + } |
| 162 | + if ($sanity_error_count == 0) { |
| 163 | + print "\nSanity checking completed. $lcount rows checked. No errors.\n"; |
| 164 | + } else { |
| 165 | + die "\nSanity checking FAILED. $lcount rows checked. $sanity_error_count error(s).\n"; |
138 | 166 | }
|
139 |
| - print "\nSanity checking completed. $lcount rows checked. No errors.\nExiting.\n"; |
140 | 167 | exit 0;
|
141 | 168 | }
|
142 | 169 |
|
@@ -298,6 +325,13 @@ sub centre_as_str {
|
298 | 325 | return "ECMWF" if ($cc eq $centre_ecmwf);
|
299 | 326 | return "Unknown";
|
300 | 327 | }
|
| 328 | +sub check_units { |
| 329 | + my $u = shift; |
| 330 | + my $unit_id = $dbh->selectrow_array("select id from units where name = ?",undef,$u); |
| 331 | + return 0 if (!$unit_id); |
| 332 | + return 1; |
| 333 | +} |
| 334 | + |
301 | 335 | sub get_db_units_code {
|
302 | 336 | my $u = shift;
|
303 | 337 | my $unit_id = $dbh->selectrow_array("select id from units where name = ?",undef,$u);
|
|
0 commit comments