root/trunk/matml/webselector/parser/parse.pl

Revision 48, 4.0 kB (checked in by vieyra, 8 years ago)

Inital check in
Kind of works . . . in some places

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl
2
3use DBI;
4use XML::XPath;
5use XML::XPath::XMLParser;
6
7#Specify database, user and password
8
9my $dbh = DBI->connect ("dbi:Pg:dbname=matdl","el_oso", "hyfeguu")
10    || die "Can't connect: $DBI::errstr";
11
12
13# Check the path of the xmlfile in the future, this will not work
14# in a batch environment so far I hope
15
16my $xmlfile = shift @ARGV;
17my $xpath = XML::XPath->new( filename => $xmlfile );
18
19sub xpath{
20
21    my ($query) = @_;
22    my $nodeset = $xpath->find( $query );
23    my $results;
24
25# print each node in the list
26    foreach my $node ( $nodeset->get_nodelist ) {
27        $results .= XML::XPath::XMLParser::as_string( $node );
28    }
29   
30    return $results;
31}
32
33sub formunit{
34
35    my ($propid) = @_;
36    my ($units, $powers);
37
38#    $units  = &xpath("//PropertyDetails[\@id='$propid']/Units/Unit/Name/text()");
39#    $powers = &xpath("//PropertyDetails[\@id='$propid']/Units/Unit/\@power");
40   
41#    @unit  = split/ /,$units;
42#    @power = split/ /,$powers;
43 
44
45    $units = &xpath("//PropertyDetails[\@id='$propid']/Units/\@name");
46    chop $units;
47    ($trash, $units) = split/\"/,$units;
48    $formedunit = $units;
49
50    return $formedunit;
51}
52
53
54
55my $matname = &xpath( "//BulkDetails/Name/text()");
56
57
58#Check for duplicates in the future
59#inserting the Material name and location of xmlfile
60
61$str = $dbh->prepare("insert into Materials values (DEFAULT,'$xmlfile','$matname')");
62$str->execute;
63
64#getting the assigned matid for this material for future use with properties insertion
65
66$str = $dbh->prepare("select matid from Materials where matname='$matname'");
67$str->execute;
68$matid = $str->fetchrow_array;
69
70my $properties = &xpath("//PropertyDetails/\@id");
71chop $properties;
72@props = split / id=\"/,$properties;
73@props = split /\" /,"@props";
74
75
76$propname_last = "";
77
78foreach (@props){
79
80    $propid = $_;
81    $propid =~ s/ //g; #Remove blank spaces
82   
83               
84    $propname = &xpath("//PropertyDetails[\@id='$propid']/Name/text()");
85
86    #trying to fix duplicates is broken
87    if($propname eq $propname_last){
88        break;}
89
90    $value =    &xpath("//PropertyData[\@property='$propid'][1]/Data/text()");
91    $format =   &xpath("//PropertyData[\@property='$propid'][1]/Data/\@format");
92    chop $format;
93    ($trash,$format) = split /\"/,$format;
94    $units = &formunit($propid);   
95
96    $propname =~ s/\'/\\\'/g; #Escape apostrophes
97
98 print "insert into Properties values('$matid','$propid','$propname','$value','$format','$units')\n";
99    $str = $dbh->prepare("insert into Properties values('$matid','$propid','$propname','$value','$format','$units')");
100   $str->execute;
101
102    $propname_last = $propname;
103
104}
105
106
107#Fix units
108# I don't care about english units, it is an old and obsolete system that should not be used at all
109# And we should teach that.
110
111# If you have any doubts check the NIST webpage and you will see that the official system in US is the metrical system
112
113# Anyway, so far I converting everything to International Units and then I will give the options for unit conversion
114
115# psi -> MPa
116$str = $dbh->prepare("update properties set value=cast(cast(value as numeric)/145.03774 as text),units='MPa' where units='psi'");
117$str->execute;
118# ksi -> MPa
119$str = $dbh->prepare("update properties set value=cast(cast(value as numeric)*6.8947573 as text),units='MPa' where units='ksi'");
120$str->execute;
121#  GPa -> MPa  (Well, just to have everything in the same units)
122$str = $dbh->prepare("update properties set value=cast(cast(value as numeric)*1000 as text),units='MPa' where units='GPa'");
123$str->execute;
124# lb/cu in. -> g/cc
125$str = $dbh->prepare("update properties set value=cast(cast(value as numeric)*27.6799047102 as text),units='g/cc' where units='lb/cu in.';
126");
127$str->execute;
128# Btu/lb/ F -> J/g-°C
129$str = $dbh->prepare("update properties set value=cast(cast(value as numeric)*4.1868 as text),units='' where units='Btu/lb./°F'");
130$str->execute;
131# Btu/ft squared/hr/F -> W/m-K
132$str = $dbh->prepare("update properties set value=cast(cast(value as numeric)*.144227888864 as text),units='W/m-K' where units='Btu-in/ft squared/hr/°F';
133");
134$str->execute;
Note: See TracBrowser for help on using the browser.