File: create_tables.pl

package info (click to toggle)
eccodes 2.44.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 150,256 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 274; xml: 183; awk: 66
file content (121 lines) | stat: -rwxr-xr-x 3,732 bytes parent folder | download | duplicates (6)
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
#! /usr/local/apps/perl/current/bin/perl
#  /usr/local/bin/perl56 -I/usr/local/lib/metaps/perl
#
# Script to update GRIB2 tables from database
# Usage: $0 [version]
#

use strict;
use File::Path;
use File::Basename;
use File::Copy;
use DBI;

my $basedir=dirname($0);  # the "definitions" dir in grib_api workspace

my $db="fm92_grib2";
my $host="grib-param-db-prod.ecmwf.int";
my $user="ecmwf";
my $pass="";
my $filename; my $filebase; my $out; my $conceptDir;
my $query; my $q; my $qh;
my $tablesVersion=6;          # default GRIB2 version
my %records;

# Check if user has provided arg to set table version
if ( @ARGV) {
	$tablesVersion = $ARGV[0];
	if ($tablesVersion !~ /^\d+$/ ) {
		die "Bad version number: '$tablesVersion'.  Please enter a positive integer.\n";
	}
}
print "GRIB2 tablesVersion set to $tablesVersion\n";

my $dbh  = DBI->connect("dbi:mysql(RaiseError=>1):database=$db;host=$host",$user,$pass) or die $DBI::errstr;

###########################################################################################
sub create_parameter_tables {
	my $tablesDir="$basedir/grib2/tables/$tablesVersion";
	my $tableFile="";

	my $query="select discipline_id,category_id,param_id,name,units ".
				"from parameter_specs order by discipline_id,category_id,param_id";

	my $qh=$dbh->prepare($query);
	$qh->execute();

	while (my ($discipline,$category,$code,$name,$units)=$qh->fetchrow_array )
	{
		my $f="$tablesDir/4.2.$discipline.$category.table";
		if ($f ne $tableFile) {
			print "discipline=$discipline category=$category;\n";
			$tableFile=$f;
			if ($out) {
				# Write out what we stored in 'records' into the PREVIOUS file
				print $out "# Automatically generated by $0 from database $db\@$host, do not edit\n";
				my @keys = sort { $a <=> $b } (keys %records);
				foreach my $key (@keys) { print $out $records{$key}."\n"; }
				close $out;
				%records=();
				@keys=();
			}
			system("p4 edit $tableFile");
         open($out,"> $tableFile") or die "unable to open $tableFile";
      }
      next if ($code !~ /^\s*[0-9]/);
		if (!$units) { $units="-"; }
		my $codeText = "$code $code";
		my $unitsText = "($units)";
		if ($code =~ /\d\-\d/ ) {
			# This is a range like 24-191 e.g. for Reserved entries
			$codeText = "#$code";  # Comment out
			$unitsText = "";       # Units do not make sense
		}
		$records{$code}="$codeText $name $unitsText";
	}
	# Now write the final records set into the last opened file
	if ($out) {
		print $out "# Automatically generated by $0 from database $db\@$host, do not edit\n";
		my @keys = sort { $a <=> $b } (keys %records);
		foreach my $key (@keys) { print $out $records{$key}."\n"; }
	}
	close $out;
}

###########################################################################################
sub create_tables {
	my $tablesDir="$basedir/grib2/tables/$tablesVersion";
	my $tableFile="";

	my $query="select section_id,ctable_id,code,meaning from ctable_specs order by section_id,ctable_id";

	my $qh=$dbh->prepare($query);
	$qh->execute();

	while (my ($section,$table,$code,$meaning)=$qh->fetchrow_array )
	{
		my $f="$tablesDir/$section.$table.table";
		if ($f ne $tableFile) {
			print "section=$section table=$table\n";
			$tableFile=$f;
			if ($out) {
				print $out "# Automatically generated by $0 from database $db\@$host, do not edit\n";
				my @keys = sort { $a <=> $b } (keys %records);
				foreach my $key (@keys) { print $out $records{$key}."\n"; }
				close $out;
				%records=();
				@keys=();
			}
			system("p4 edit $tableFile");
         open($out,"> $tableFile") or die "unable to open $tableFile";
        }
		next if ($code =~ /-/) ; 
		$records{$code}="$code $code $meaning";
	}
	close $out;
}
 
create_parameter_tables();
 
#create_tables();