| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use XML::LibXML; |
|---|
| 5 |
use DBI; |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
my ($env) = @ARGV; |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
my $xp_env = XML::LibXML->new->parse_file("../environment_xenia_$env.xml"); |
|---|
| 15 |
|
|---|
| 16 |
my $db_host = $xp_env->findvalue('//db/host'); |
|---|
| 17 |
my $db_name = $xp_env->findvalue('//db/name'); |
|---|
| 18 |
my $db_user = $xp_env->findvalue('//db/user'); |
|---|
| 19 |
my $db_passwd = $xp_env->findvalue('//db/passwd'); |
|---|
| 20 |
|
|---|
| 21 |
my $path_psql = $xp_env->findvalue('//path/psql'); |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
my ($temp_sec,$temp_min,$temp_hour,$temp_mday,$temp_mon,$temp_year,$temp_wday,$temp_yday,$isdst) = localtime(time); |
|---|
| 30 |
my $time_add; |
|---|
| 31 |
if ($isdst) {$time_add = 4; } else {$time_add = 5; } |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
my ($dbh,$sth,$sql); |
|---|
| 35 |
if ($db_host eq '') { $dbh = DBI->connect ("dbi:Pg:dbname=$db_name", "$db_user", "$db_passwd"); } |
|---|
| 36 |
else { $dbh = DBI->connect ("dbi:Pg:dbname=$db_name;host=$db_host", "$db_user", "$db_passwd"); } |
|---|
| 37 |
if ( !defined $dbh ) {die "Cannot connect to database!\n";} |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
my @platform = qw(1 niwolmet met |
|---|
| 43 |
2 niwolwq water); |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
while (@platform) { |
|---|
| 48 |
|
|---|
| 49 |
my $platform_id = shift(@platform); |
|---|
| 50 |
my $cdmo_id = shift(@platform); |
|---|
| 51 |
my $file_type = shift(@platform); |
|---|
| 52 |
|
|---|
| 53 |
$sql = qq{ select platform_handle,fixed_longitude,fixed_latitude from platform where row_id = $platform_id }; |
|---|
| 54 |
|
|---|
| 55 |
$sth = $dbh->prepare( $sql ); |
|---|
| 56 |
$sth->execute(); |
|---|
| 57 |
if ($sth->rows == 0) { next; } |
|---|
| 58 |
my ($platform_handle,$m_lon,$m_lat) = $sth->fetchrow_array; |
|---|
| 59 |
|
|---|
| 60 |
my $filename = './tmp/cdmo_'.$cdmo_id; |
|---|
| 61 |
my $sql_out = $filename.'.sql'; |
|---|
| 62 |
open (SQL_OUT,">$sql_out"); |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
open (FILE, $filename.'.txt'); |
|---|
| 68 |
|
|---|
| 69 |
my $line_feed = 0; |
|---|
| 70 |
my $content = ''; |
|---|
| 71 |
foreach my $line (<FILE>) { |
|---|
| 72 |
if (!($line =~ /nds/) && ($line_feed == 0)) { next; } else { $line_feed = 1; } |
|---|
| 73 |
$content .= $line; |
|---|
| 74 |
if ($line =~ /\/nds/) { last; } |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
close (FILE); |
|---|
| 78 |
|
|---|
| 79 |
open (FILE_OUT,">$filename".'.xml'); |
|---|
| 80 |
print FILE_OUT $content; |
|---|
| 81 |
close(FILE_OUT); |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
my $xp = XML::LibXML->new->parse_file($filename.'.xml'); |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
my $line_count = 0; |
|---|
| 92 |
foreach my $row ($xp->findnodes('//data/r')) { |
|---|
| 93 |
$line_count++; |
|---|
| 94 |
if ($line_count < 2) { next; } |
|---|
| 95 |
|
|---|
| 96 |
print "###################\n"; |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
my @obs = (); |
|---|
| 100 |
my $m_date; |
|---|
| 101 |
my ($row_id,$historical); |
|---|
| 102 |
my ($air_temp,$air_temp_qc,$rh,$rh_qc,$air_pressure,$air_pressure_qc,$wind_speed,$wind_speed_qc,$wind_from_direction,$wind_from_direction_qc,$precipitation,$precipitation_qc,$solar,$solar_qc); |
|---|
| 103 |
my ($water_temp,$water_temp_qc,$water_conductivity,$water_conductivity_qc,$salinity,$salinity_qc,$do_percent,$do_percent_qc,$do_mgl,$do_mgl_qc,$water_level,$water_level_qc,$ph,$ph_qc,$turbidity,$turbidity_qc); |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
my @values = (); |
|---|
| 107 |
foreach my $element ($row->findnodes('c/@v')) { push (@values, $element->string_value()); } |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
if ($file_type eq 'met') { |
|---|
| 111 |
($row_id,$historical,$m_date,$air_temp,$air_temp_qc,$rh,$rh_qc,$air_pressure,$air_pressure_qc,$wind_speed,$wind_speed_qc,$wind_from_direction,$wind_from_direction_qc,$precipitation,$precipitation_qc,$solar,$solar_qc) = @values; |
|---|
| 112 |
print "$air_temp\n"; |
|---|
| 113 |
@obs = (1,$air_temp,$air_temp_qc,3,$rh,$rh_qc,4,$air_pressure,$air_pressure_qc,5,$wind_speed,$wind_speed_qc,6,$wind_from_direction,$wind_from_direction_qc,7,$precipitation,$precipitation_qc,8,$solar,$solar_qc); |
|---|
| 114 |
} |
|---|
| 115 |
if ($file_type eq 'water') { |
|---|
| 116 |
($row_id,$historical,$m_date,$water_temp,$water_temp_qc,$water_conductivity,$water_conductivity_qc,$salinity,$salinity_qc,$do_percent,$do_percent_qc,$do_mgl,$do_mgl_qc,$water_level,$water_level_qc,$ph,$ph_qc,$turbidity,$turbidity_qc) = @values; |
|---|
| 117 |
print "$water_temp\n"; |
|---|
| 118 |
@obs = (2,$water_temp,$water_temp_qc,9,$water_conductivity,$water_conductivity_qc,10,$salinity,$salinity_qc,11,$do_percent,$do_percent_qc,12,$do_mgl,$do_mgl_qc,13,$water_level,$water_level_qc,14,$ph,$ph_qc,15,$turbidity,$turbidity_qc); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
$m_date = '20'.substr($m_date,6,2).'-'.substr($m_date,0,2).'-'.substr($m_date,3,2).' '.substr($m_date,9,5).':00'; |
|---|
| 122 |
print "$m_date\n"; |
|---|
| 123 |
|
|---|
| 124 |
while (@obs) { |
|---|
| 125 |
|
|---|
| 126 |
my $sensor_id = shift(@obs); |
|---|
| 127 |
my $m_value = shift(@obs); |
|---|
| 128 |
my $qc_level = shift(@obs); |
|---|
| 129 |
|
|---|
| 130 |
$sql = qq{ select m_type_id,fixed_z from sensor where row_id = $sensor_id }; |
|---|
| 131 |
|
|---|
| 132 |
$sth = $dbh->prepare( $sql ); |
|---|
| 133 |
$sth->execute(); |
|---|
| 134 |
if ($sth->rows == 0) { next; } |
|---|
| 135 |
my ($m_type_id,$m_z) = $sth->fetchrow_array; |
|---|
| 136 |
|
|---|
| 137 |
print SQL_OUT "INSERT INTO multi_obs (row_id,row_entry_date,row_update_date,platform_handle,sensor_id,m_type_id,m_date,m_lon,m_lat,m_z,m_value,qc_level) VALUES (nextval('multi_obs_row_id_seq'),now(),now(),'$platform_handle',$sensor_id,$m_type_id,timestamp '$m_date' + interval '$time_add hours',$m_lon,$m_lat,$m_z,$m_value,$qc_level);\n"; |
|---|
| 138 |
|
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
close (SQL_OUT); |
|---|
| 144 |
|
|---|
| 145 |
`$path_psql -U $db_user -d $db_name -h $db_host -f $sql_out`; |
|---|
| 146 |
|
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
$sth->finish; |
|---|
| 150 |
$dbh->disconnect(); |
|---|
| 151 |
|
|---|
| 152 |
exit 0; |
|---|
| 153 |
|
|---|