Commit 5a967574 authored by jclamber's avatar jclamber

add unsio sqlite3 full support

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@154 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent e93593e2
......@@ -210,3 +210,14 @@ INSTALL(FILES ${UNSIO_BINARY_DIR}/../man/man3/unsiof.3 DESTINATION man/man3)
#set (CMAKE_INSTALL_PREFIX $ENV{NEMOBIN})
INSTALL(PROGRAMS ${UNSIO_BINARY_DIR}/bin/uns_info DESTINATION bin)
# install sqlite3 database files
INSTALL(PROGRAMS ${UNSIO_BINARY_DIR}/../scripts/perl/mains/unsio_sql3_get_info.pl DESTINATION bin)
INSTALL(PROGRAMS ${UNSIO_BINARY_DIR}/../scripts/perl/mains/unsio_sql3_update_info.pl DESTINATION bin)
INSTALL(PROGRAMS ${UNSIO_BINARY_DIR}/../scripts/perl/mains/unsio_sql3_update_nemorange.pl DESTINATION bin)
INSTALL(PROGRAMS ${UNSIO_BINARY_DIR}/../scripts/perl/mains/unsio_sql3_create_db.pl DESTINATION bin)
INSTALL(FILES ${UNSIO_BINARY_DIR}/../scripts/perl/lib/Tools/Tools.pm DESTINATION scripts/perl/lib/Tools)
INSTALL(FILES ${UNSIO_BINARY_DIR}/../scripts/perl/lib/Tools/Sqlite3.pm DESTINATION scripts/perl/lib/Tools)
INSTALL(FILES ${UNSIO_BINARY_DIR}/../scripts/sql/create_unsio_db.sql DESTINATION scripts/sql)
INSTALL(FILES ${UNSIO_BINARY_DIR}/../scripts/sql/README DESTINATION scripts/sql)
# -*- Mode: Perl -*-
#
# Perl package to manage sqlite3 operations
#
# ============================================================================
# Copyright Jean-Charles LAMBERT - -2013
# e-mail: Jean-Charles.Lambert@oamp.fr
# address: Dynamique des galaxies
# Laboratoire d'Astrophysique de Marseille
# Pole de l'Etoile, site de Chateau-Gombert
# 38, rue Frederic Joliot-Curie
# 13388 Marseille cedex 13 France
# CNRS U.M.R 6110
# ============================================================================
#
package Sqlite3::Sqlite;
use strict;
use IO::File;
use Tools::Tools;
use DBI;
my $db="/pil/programs/DB/simulation.dbl"; # Marseille database
my $paramfile="$ENV{HOME}/.unsio";
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# create unsio sqlite3 empty table
sub Sqlite::createUnsioDb()
{
my $class = shift;
my $db = shift;
# open database
my %attr =( PrintWarn=>0,PrintError=>0,RaiseError=>0,Taint=>0);
my $dbh = DBI->connect( "dbi:SQLite:${db}",,,\%attr ) || die "Cannot connect: $DBI::errstr";
if ( $dbh->err()) {
printf STDERR "\nUnable to open DATABASE ==> [$db]\n\n";
exit();
}
# EPS table
my $stmt = qq(CREATE TABLE eps (name text unique, gas real, halo real, disk real, bulge real, stars real););
Sqlite->runSqlCommand($dbh,$stmt);
# INFO table
my $stmt = qq(CREATE TABLE info (name text unique, type text, dir text, base text););
Sqlite->runSqlCommand($dbh,$stmt);
# NEMORANGE table
my $stmt = qq(CREATE TABLE nemorange (name text unique, total text, disk text,
bulge text, halo text, halo2 text, gas text, bndry text, stars text););
Sqlite->runSqlCommand($dbh,$stmt);
$dbh->disconnect();
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# create unsio sqlite3 empty table
sub Sqlite::runSqlCommand() {
my $class = shift;
my $dbh = shift; # DB file handler
my $cmd = shift;
my $rv = $dbh->do($cmd);
if($rv < 0){
print $DBI::errstr;
} else {
print "Sql command successfully commited\n";
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Return valid unsio sqlite3 database
sub Sqlite::getValidDb
{
my $class = shift;
my $dbparam = shift;
if (defined($dbparam)) {
$db = $dbparam;
} else {
# parse parameter file
my (%param,$status)=File->parseParameters($paramfile);
#Tools->printKeyValueHash(\%param); # display parameters
if (defined ($param{dbname}) ) {
$db = $param{dbname};
}
}
return $db;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Return Sim information
#
sub Sqlite::getAllRef {
# parameters
my $class = shift;
my $simname = shift;
my $dbparam = shift;
my $db = Sqlite->getValidDb($dbparam);
die "Unable to open sqlite3 database [$db] ...." if ( ! -f $db );
# open database
my %attr =( PrintWarn=>0,PrintError=>0,RaiseError=>0,Taint=>0);
my $dbh = DBI->connect( "dbi:SQLite:${db}",,,\%attr ) || die "Cannot connect: $DBI::errstr";
if ( $dbh->err()) {
printf STDERR "\nUnable to open DATABASE ==> [$db]\n\n";
exit();
}
# try Select request
#my $sql="select * from info where name=='$simname'";
my $sql="select * from info where name like '$simname' ";
my $status=$dbh->selectall_hashref($sql,'name');
if (0) {
printf "-------------\n";
foreach my $sim ( $status ) {
print "sim dir ==> $sim->{dir}\n";
}
printf "-------------\n";
}
#if ( defined(%$status ) ) {
if ( %$status ) {
return %$status;
} else {
return;
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Return Sim information
#
sub Sqlite::getSimInfo {
# parameters
my $class = shift;
my $simname = shift;
my $dbparam = shift;
my $db = Sqlite->getValidDb($dbparam);
die "Unable to open sqlite3 database [$db] ...." if ( ! -f $db );
# open database
my %attr =( PrintWarn=>0,PrintError=>0,RaiseError=>0,Taint=>0);
my $dbh = DBI->connect( "dbi:SQLite:${db}",,,\%attr ) || die "Cannot connect: $DBI::errstr";
if ( $dbh->err()) {
printf STDERR "\nUnable to open DATABASE ==> [$db]\n\n";
exit();
}
# try Select request
my $sql="select * from info where name=='$simname'";
#my $sql="select * from info";
my $status=$dbh->selectrow_hashref($sql);
if (0) {
printf "-------------\n";
foreach my $sim ( $status ) {
print "sim dir ==> $sim->{dir}\n";
}
printf "-------------\n";
}
#if ( defined(%$status ) ) {
if ( %$status ) {
return %$status;
} else {
return;
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Return Sim Eps
#
sub Sqlite::getSimEps {
# parameters
my $class = shift;
my $simname = shift;
my $dbparam = shift;
my $db = Sqlite->getValidDb($dbparam);
die "Unable to open sqlite3 database [$db] ...." if ( ! -f $db );
# open database
my %attr =( PrintWarn=>0,PrintError=>0,RaiseError=>0,Taint=>0);
my $dbh = DBI->connect( "dbi:SQLite:${db}",,,\%attr ) || die "Cannot connect: $DBI::errstr";
if ( $dbh->err()) {
printf STDERR "\nUnable to open DATABASE ==> [$db]\n\n";
exit();
}
# try Select request
my $sql="select * from eps where name=='$simname'";
my $status=$dbh->selectrow_hashref($sql);
if (0) {
printf "-------------\n";
foreach my $sim ( $status ) {
print "sim dir ==> $sim->{dir}\n";
}
printf "-------------\n";
}
#if ( defined(%$status ) ) {
if ( $status ) {
return %$status;
} else {
return;
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Return Sim Nemo Range
#
sub Sqlite::getSimNemoRange {
# parameters
my $class = shift;
my $simname = shift;
my $dbparam = shift;
my $db = Sqlite->getValidDb($dbparam);
die "Unable to open sqlite3 database [$db] ...." if ( ! -f $db );
# open database
my %attr =( PrintWarn=>0,PrintError=>0,RaiseError=>0,Taint=>0);
my $dbh = DBI->connect( "dbi:SQLite:${db}",,,\%attr ) || die "Cannot connect: $DBI::errstr";
if ( $dbh->err()) {
printf STDERR "\nUnable to open DATABASE ==> [$db]\n\n";
exit();
}
# try Select request
my $sql="select * from nemorange where name=='$simname'";
my $status=$dbh->selectrow_hashref($sql);
if (0) {
printf "-------------\n";
foreach my $sim ( $status ) {
print "sim dir ==> $sim->{dir}\n";
}
printf "-------------\n";
}
#if ( defined(%$status ) ) {
if ( $status ) {
return %$status;
} else {
return;
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Return Snasphots list
#
sub Sqlite::getSnapshotsList {
# parameters
my $class = shift;
my $simname = shift;
my $dbparam = shift;
my $nolog = shift;
my $db = Sqlite->getValidDb($dbparam);
my %res=Sqlite->getSimInfo($simname);
#if (defined(%res)) {
if (%res) {
my $simtype=$res{type};
#printf STDERR "NOLOG[$nolog]\n";
# call the right method
$simtype->getSnapshotsList(\%res,$nolog);
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# check if simulation exist and abort if requested
# return simulation's info structure
sub Sqlite::checkSimAbort {
# parameters
my $class = shift;
my $name = shift;
my $abort = shift;
# collect information from database
my %res=Sqlite->getSimInfo($name);
my $status=1;
#if ( !defined(%res)) {
if ( !(%res)) {
$status=0;
printf STDERR <<EOF;
Sorry but the simulation $name does not exist in
[$db] database.....
EOF
if ($abort) {
exit();
}
}
return ($status,%res);
}
# -*- Mode: Perl -*-
#
# Perl package with useful functions
#
#
# ============================================================================
# Copyright Jean-Charles LAMBERT - -2013
# e-mail: Jean-Charles.Lambert@oamp.fr
# address: Dynamique des galaxies
# Laboratoire d'Astrophysique de Marseille
# Pole de l'Etoile, site de Chateau-Gombert
# 38, rue Frederic Joliot-Curie
# 13388 Marseille cedex 13 France
# CNRS U.M.R 6110
# ============================================================================
#
package Tools::Tools;
use strict;
use IO::File;
# - - - - - - - - - - - - - - - - - - - - - - - - - -
#
sub Tools::printUnsioInfo
{
my $class = shift;
printf STDERR <<EOF;
To get information about UNSIO, please visit :
-> http://projets.lam.fr/projects/unsio
To get information about Unsio Sqlite3 database, please visit :
-> http://projets.lam.fr/projects/unsio/wiki/Sqlite3Db
EOF
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
#
sub File::readFile
{
my $class = shift;
my $HANDLE = shift;
my $line;
#printf STDERR "In Tools2 [$HANDLE]\n";
while ( <$HANDLE> ) {
chomp;
#printf STDERR ">>> %s\n", $_;
return (1,$_);
}
return 0;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# parse a file with input parameters based on pair "key = value"
# and fill up the result into an hash table
sub File::parseParameters
{
my $class = shift;
my $file = shift; # input file
my %param; # hash structure to store parameter file
my $FD = IO::File->new();
if (! $FD->open("<$file")) {
warn "Unable to open for reading [$file]";
return (%param,0);
}
my $ret=1;
while ($ret) {
my $line;
($ret,$line) = File->readFile($FD);
if ($ret) {
my $line2 = (split(/#/,$line))[0]; # skip commented line
if (defined($line2)) {
my ($key, $value) = split(/=/,$line2);
if ((defined $key) && (defined $value)) {
$key =~ s/ +//g; # remove all blank characters
$value =~ s/^\s+//; # remove leading blank characters
$value =~ s/\s+$//; # remove trailing blank characters
#printf STDERR "[$key]/[$value]\n";
$param{$key} = $value;
}
}
#my $simname="$tab[0]"; # simulation name
}
}
return (%param,1);
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
#
sub Tools::printKeyValueHash
{
my $class = shift;
#my %hash = %{(shift)}; #% hash table given in parameter (copy in local variabl)
my $hash = shift; #% hash table given in parameter (pass by reference)
#foreach my $kk ( keys((%hash)) ) { #
foreach my $kk ( keys(($hash)) ) { #
printf STDERR "Key/Value = [$kk/$hash->{$kk}]\n";
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
#
sub Tools::run
{
my $class = shift;
my $cmd = shift;
my $notverbose = shift;
if (! defined($notverbose)) {
print STDERR "Running:\n[$cmd]\n";
}
system($cmd);
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
# Guess CPU number
# and create hostmpich
sub guess_cpu_number
{
#system("echo `hostname|cut -d. -f1`:$ncpu > hostmpich");
my $host=Tools->getMyShortHostname();
my $nproc=Tools->getCoresNumber();
my $hmp="hostmpich";
my $HMP = IO::File->new("> $hmp ") ||
die "Unable to open for writing [$hmp]";
for (my $i=0; $i<${nproc}; ${i}++) {
printf $HMP "${host}\n";
}
$HMP->close();
printf "#CPU detected = $nproc\n";
return $nproc;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
#
sub Tools::getCoresNumber {
# parameters
my $class = shift;
my $cpu=0;
my $FD;
if ( open($FD,"cat /proc/cpuinfo | grep processor|wc -l|")) {
chomp(my $x=<$FD>);
if ( defined $x ) {
$cpu=$x;
}
close ($FD);
} else {
printf STDERR "In Tools::getCoresNumber, unable to detect #cores..\n";
}
return $cpu;
}
# - - - - - - - - - - - - - - - - - - - - - - - - - -
1; # Allways return 1
#
#!/usr/bin/perl
#
# This script create an empty unsio sqlite3 database
#
#
# ============================================================================
# Copyright Jean-Charles LAMBERT - -2013
# e-mail: Jean-Charles.Lambert@oamp.fr
# address: Dynamique des galaxies
# Laboratoire d'Astrophysique de Marseille
# Pole de l'Etoile, site de Chateau-Gombert
# 38, rue Frederic Joliot-Curie
# 13388 Marseille cedex 13 France
# CNRS U.M.R 6110
# ============================================================================
#
#
#
BEGIN
{
my $exe=(split(/\//,$0))[-1]; # basename program name
my $path=$0; # absolute path name
$path =~ s/${exe}//g; # dirname
push @INC, "$path/../scripts/perl/lib", "$path/../lib"; # add path to locate modules
}
# load package
use strict; # must define everything
use Getopt::Long;
use Tools::Sqlite3;
# -------------------------------------------------------------
# Main program
# get exe basename
my $exe=(split(/\//,$0))[-1];
# setup default input parameter
my $db; # = "/pil/programs/DB/simulation.dbl"; # Marseille database
my $help = 0;
GetOptions( 'db=s' => \$db,
'help!' => \$help,
) or die "Incorrect usage!\n";
# check for help
if( $help ) {
usage();
exit();
}
# get input parameters not named
if ( @ARGV == 1 ) {
chomp ($db =$ARGV[0]);
shift;
} else {
# check mandatory parameters
die "Option --db not specified.\n" unless defined($db);
}
Sqlite->createUnsioDb($db);
printf STDERR "\nUnsio sqlite3 database [$db] has been created ! \n\n";
# ------------------------------------------------------------
sub usage {
printf STDERR <<EOF;
========================================================
Create an empty unsio sqlite3 database
========================================================
Usage : $exe databasename
Example : $exe --db=mydb.sql3
EOF
Tools->printUnsioInfo();
}
#!/usr/bin/perl
#
# This script update INFO table from an unsio sqlite3 database
#
#
# ============================================================================
# Copyright Jean-Charles LAMBERT - -2013
# e-mail: Jean-Charles.Lambert@oamp.fr
# address: Dynamique des galaxies
# Laboratoire d'Astrophysique de Marseille
# Pole de l'Etoile, site de Chateau-Gombert
# 38, rue Frederic Joliot-Curie
# 13388 Marseille cedex 13 France
# CNRS U.M.R 6110
# ============================================================================
#
#
#
BEGIN
{
my $exe=(split(/\//,$0))[-1]; # basename program name
my $path=$0; # absolute path name
$path =~ s/${exe}//g; # dirname
push @INC, "$path/../scripts/perl/lib", "$path/../lib"; # add path to locate modules
}
# load package
use strict; # must define everything
use Getopt::Long;
use Tools::Sqlite3;
# -------------------------------------------------------------
# Main program
# get exe basename
my $exe=(split(/\//,$0))[-1];
# setup default input parameter
my $name;
my $db; # = "/pil/programs/DB/simulation.dbl"; # Marseille database
my $help = 0;
GetOptions( 'simname=s' => \$name,
'db=s' => \$db,
'help!' => \$help,
) or die "Incorrect usage!\n";
# check for help
if( $help ) {
usage();
exit();
}
# get input parameters not named
if ( @ARGV >= 1) {
chomp($name=$ARGV[0]); shift;
if ( @ARGV == 1 ) {
chomp ($db =$ARGV[0]);
shift;
}
} else {
# check mandatory parameters
die "Option --simname not specified.\n" unless defined($name);
}
if ($db eq "") {
undef $db;
}
my $db = Sqlite->getValidDb($db);
printf STDERR "simname=[$name] db=[$db]\n";
my %res=Sqlite->getSimInfo($name,$db);
if ((%res)) {
printf "dir = $res{dir}\n";
if (1) {
for my $info ( keys %res ) {
printf " $info = $res{$info}\n";
}
}
}
my %res=Sqlite->getSimEps($name,$db);
if ((%res)) {
if (1) {
for my $info ( keys %res ) {
printf " $info = $res{$info}\n";
}
}
}
my %res=Sqlite->getSimNemoRange($name,$db);
if ((%res)) {
if (1) {
for my $info ( keys %res ) {
printf " $info = $res{$info}\n";
}
}
}
# ------------------------------------------------------------
sub usage {
my $db = Sqlite->getValidDb($db);
printf STDERR <<EOF;
========================================================
Return from an unsio sqlite3 database simulation file:
[$db]
all the informations belonging to the simulation\'s name
given in parameter.
========================================================
Usage : $exe simname [db]
Example : $exe --simname=sgs019
EOF
Tools->printUnsioInfo();
}
#!/usr/bin/perl
#
# This script return simulation information from an unsio sqlite3 database
#
#
# ============================================================================
# Copyright Jean-Charles LAMBERT 2013
# e-mail: Jean-Charles.Lambert@oamp.fr
# address: Dynamique des galaxies
# Laboratoire d'Astrophysique de Marseille
# Pole de l'Etoile, site de Chateau-Gombert
# 38, rue Frederic Joliot-Curie
# 13388 Marseille cedex 13 France
# CNRS U.M.R 6110
# ============================================================================
#
#
#
BEGIN
{
my $exe=(split(/\//,$0))[-1]; # basename program name
my $path=$0; # absolute path name
$path =~ s/${exe}//g; # dirname
push @INC, "$path/../scripts/perl/lib", "$path/../lib"; # add path to locate modules
}
# load package
use strict; # must define everything
use Getopt::Long;
use Tools::Tools;
# -------------------------------------------------------------
# Main program
# get exe basename
my $exe=(split(/\//,$0))[-1];
# setup default input parameter
my $name;
my $type;
my $dir;
my $base;