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
|
#!/usr/local/bin/perl56 -I/usr/local/lib/metaps/perl
use strict;
use File::Path ;
use File::Basename ;
use File::Copy;
use Cwd;
use DBI;
my $basedir = dirname($0);
my $db="param";
my $host="grib-param-db-test.ecmwf.int";
my $user="ecmwf_ro";
my $pass="ecmwf_ro";
my $filename; my $filebase; my $out; my $conceptDir;
my $query; my $q; my $qh;
my $dbh = DBI->connect("dbi:mysql(RaiseError=>1):database=$db;host=$host",$user,$pass) or die $DBI::errstr;
# I have written to it already or not
my $tarfilesflag = 0;
sub create_def {
my $p; my %seen;
my ($key) =@_;
my $field=$key;
if ($key =~ /paramId/) { $field="param.id"; }
if ($key =~ /name/) { $field="param.name"; }
if ($key =~ /units/) { $field="units.name"; }
my $query= <<"EOF";
select $field,force128,edition,
centre.abbreviation,param_id,attribute.name,attribute_value,param.name,param.shortName
from param,grib,attribute,centre,units where param.hide_def=0 and param.id=grib.param_id
and attribute.id=grib.attribute_id and centre.id=grib.centre and units.id=param.units_id
order by edition,centre,param.o,param.id,grib.param_version,attribute.o;
EOF
my $qh=$dbh->prepare($query);
$qh->execute();
# file containing the list of grib api parameters files we want to tar and
# distribute to users for them to download and update their list of parameter
# to the latest
open(TAR,$tarfilesflag ? ">>" : ">","tarfiles.txt") or die "Count not open file tarfiles.txt: $!";
$tarfilesflag=1;
while (my ($keyval,$force128,$edition,$centre,$paramId,$attribute,$value,$name,$shortName)=$qh->fetchrow_array )
{
if ($centre eq "all" ) { $conceptDir=""; }
else { $conceptDir="/localConcepts/$centre"; }
#if ($key =~ /paramId/ && $force128==1 && $keyval >1000) {
# $keyval= $keyval % 1000;
#}
if ($filebase ne "$basedir/grib$edition$conceptDir") {
if ($filebase) {
print $out "}\n";
close $out;
}
$filebase="$basedir/grib$edition$conceptDir";
mkpath($filebase);
#copy("$filebase/$key.def","$filebase/$key.def.bkp")
# or die ("unable to copy $filebase/$key.def");
print TAR "grib$edition$conceptDir/$key.def\n";
system("cp -f $filebase/$key.def $filebase/$key.def.orig");
#system("p4 edit $filebase/$key.def");
open($out,"> $filebase/$key.def")
or die "unable to open $filebase/$key.def";
print $out "# Automatically generated by $0, do not edit\n";
$p=();
}
if ($p ne $paramId || exists($seen{$attribute}) ) {
if ($p) { print $out "\t}\n"; }
print $out "#$name\n" ;
print $out "\'".$keyval."\' = {\n" ;
$p=$paramId;
%seen=();
}
$seen{$attribute}=1;
print "($key=$keyval) $edition,$centre,$shortName,$paramId,$name,$attribute,$value\n";
# we need to allow strings in the attribute_value field
# for the moment we apply a patch here
if ($attribute =~ /stepType/ ) {
$value="\"accum\"";
}
print $out "\t $attribute = $value ;\n" ;
}
if ($filebase) {
print $out "}\n";
close $out;
}
close(TAR);
}
sub create_paramId_def {
my $p; my %seen;
my $query="select edition,centre.abbreviation,param_id,attribute.name,attribute_value,param.name,param.shortName
from param,grib,attribute,centre where param.hide_def=0 and param.id=grib.param_id
and attribute.id=grib.attribute_id and centre.id=grib.centre
order by edition,centre,param.o,param.id,attribute.o";
my $qh=$dbh->prepare($query);
$qh->execute();
while (my ($edition,$centre,$paramId,$attribute,$value,$name,$shortName)=$qh->fetchrow_array )
{
if ($centre eq "all" ) { $conceptDir=""; }
else { $conceptDir="/localConcepts/$centre"; }
if ($filebase ne "$basedir/grib$edition$conceptDir") {
if ($filebase) {
print $out "}\n";
close $out;
}
$filebase="$basedir/grib$edition$conceptDir";
mkpath($filebase);
copy("$filebase/paramId.def","$filebase/paramId.def.bkp")
or die ("unable to copy $filebase/paramId.def");
system("p4 edit $filebase/paramId.def");
open($out,"> $filebase/paramId.def")
or die "unable to open $filebase/paramId.def";
print $out "# Automatically generated by $0, do not edit\n";
$p=();
}
if ($p ne $paramId || exists($seen{$attribute}) ) {
if ($p) { print $out "\t}\n"; }
print $out "#$name\n" ;
print $out "\'".$paramId."\' = {\n" ;
$p=$paramId;
%seen=();
}
$seen{$attribute}=1;
print "$edition,$centre,$shortName,$paramId,$name,$attribute,$value\n";
print $out "\t $attribute = $value ;\n" ;
}
if ($filebase) {
print $out "}\n";
close $out;
}
}
sub create_def_old {
my ($key,$query)=@_;
my $qh=$dbh->prepare($query);
$qh->execute();
while (my ($edition,$centre,$paramId,$value)=$qh->fetchrow_array )
{
if ($centre eq "all" ) { $conceptDir=""; }
else { $conceptDir="/localConcepts/$centre"; }
if ($filebase ne "$basedir/grib$edition$conceptDir") {
if ($filebase) {
close $out;
}
$filebase="$basedir/grib$edition$conceptDir";
mkpath($filebase);
copy("$filebase/$key.def","$filebase/$key.def.bkp")
or die ("unable to copy $filebase/$key.def");
system("p4 edit $filebase/$key.def");
open($out,"> $filebase/$key.def")
or die "unable to open $filebase/$key.def";
print $out "# Automatically generated by $0, do not edit\n";
}
print $out "\'$value\' \t= { paramId=$paramId; }\n";
}
if ($filebase) {
close $out;
}
}
create_def("paramId");
create_def("shortName");
create_def("name");
create_def("units");
#create_paramId_def();
$query="select distinct edition,centre.abbreviation,param_id,param.shortName
from param,grib,centre where param.hide_def=0 and param.id=grib.param_id and
centre.id=grib.centre and shortName!='~'
order by centre,edition,param.o,param_id";
#create_def("shortName",$query);
$query="select distinct edition,centre.abbreviation,param_id,param.name
from param,grib,centre where param.hide_def=0 and param.id=grib.param_id and
centre.id=grib.centre and shortName!='~'
order by centre,edition,param.o,param_id";
#create_def("name",$query);
$query="select distinct edition,centre.abbreviation,param_id,units.name
from param,grib,centre,units where param.hide_def=0 and param.id=grib.param_id and units.id=param.units_id
and centre.id=grib.centre and shortName!='~'
order by centre,edition,param.o,param_id";
#create_def("units",$query);
|