JGCRI / gcamreader

Python package for reading GCAM output databases
Other
4 stars 5 forks source link

"CalledProcessError" when query "LDV energy by primary fuel" #21

Open panday1995 opened 3 years ago

panday1995 commented 3 years ago

when querying "LDV energy by primary fuel" with

conn.runQuery([i for i in queries if i.title == "LDV energy by primary fuel"][0])

The following error occured: ERROR_log.txt

Other queries work well (Besides, the "LDV" query works fine in the interactive mode).

The query for "LDV energy by primary fuel" is this (which is copied from the main_query.xml file)

  <aQuery>
       <region name="China" />
        <supplyDemandQuery title="LDV energy by primary fuel">
               <axis1 name="Primary Fuel">input[@name]</axis1>
               <axis2 name="Year">demand-physical[@vintage]</axis2>
               <xPath buildList="true" dataName="input" group="true" sumAll="false"><![CDATA[
               declare function local:append-heirarchy($parent as node(), $append as node()*) as node() {
                     let $scn := $parent/ancestor::scenario,
                          $rgn := $parent (: /ancestor::region :)
                       return
                          document { element scenario {
                                            $scn/@*,
                                            element region {
                                                $rgn/@*,
                                                $append
                                            }
                                        }
                        }
                     (: I can get by with just the scenario and region
                     let $new_node := element {local-name($parent)} {$parent/@*, $append}   
                     return
                     if(local-name($parent) != 'scenario')
                     then local:append-heirarchy($parent/parent::*, $new_node)
                     else document { $new_node } :)
                 };  
                 declare function local:trace-inputs($outputName as xs:string, $currTree as node(), $outputs as node()*, $is_usa as xs:boolean) as node()* {
                    if( exists(index-of(('biomass',
'traded unconventional oil', 'regional corn for ethanol', 'regional biomassOil', 'regional sugar for ethanol', 'regional sugarbeet for ethanol'),
$outputName)) or not($currTree/*[@type='sector' and @name=$outputName]) or sum($outputs//text()) < 0.001)
then 
if(not($is_usa) and string-length($currTree/@name) = 2) then
local:trace-inputs($outputName, $currTree/parent::*/*[@type='region' and @name='USA'], $outputs, true())
else
element input {
                                attribute name {$outputName},
                                attribute type {'input'},
                                $outputs
                            }
                        else
                    let $useOutputs := $currTree//output-primary[@type='output' and @name=$outputName]/physical-output,
                        $sectorSums := for $out in $useOutputs
                           let $currInputs := $out/../following-sibling::input-energy/demand-physical[@vintage=$out/@vintage]
                           return 
                            for $currInput in $currInputs
                                return
                                element sectorSum {
                                    attribute name { $currInput/../@name},
                                attribute year { $currInput/@vintage},
                            element output-sum {
                                text { $out }
                            },
                            element input-sum {
                                text { $currInput }
                            }
                               }
                   return for $sector in distinct-values($sectorSums/@name)
                           return local:trace-inputs($sector, $currTree, for $out in $outputs
                                              let $totalOutputSum := sum($useOutputs[@vintage=$out/@vintage]),
                                              $inputSum := sum($sectorSums[@name=$sector and @year=$out/@vintage]/input-sum)
                                              where $inputSum > 0
                                              return element { local-name($out) } {
                                                $out/@*,
                                                text{ $out * ($inputSum
                                                         div $totalOutputSum) }
                                              }, $is_usa)
        };
        declare function local:run-input-by-primary($scenarios as xs:string*, $regions as xs:string*, $collection as xs:string) as node()* {    
                 unordered {    
                 let $regionsG := if(not($regions[1] = 'Global'))
                          then $regions
                          else distinct-values(collection($collection)/scenario/world/*[@type='region']/@name)
                 return
                 for $scenario in $scenarios,       
                 $region in $regionsG   
                 let $scenario_split := tokenize($scenario, ' '),       
                 $currTree := collection($collection)/scenario[@name = $scenario_split[1] and @date = $scenario_split[2]]/world/*[@type='region' and @name=$region],
                 $currInputs := $currTree/*[@type='sector' and @name='trn_pass_road']/*[@type='subsector' and @name='LDV']//*[@type='input']
                 return 
                    for $inputName in distinct-values($currInputs/@name)
                    return local:append-heirarchy($currTree, local:trace-inputs($inputName, $currTree, $currInputs[@name=$inputName]/demand-physical, false()))//text()
                 } 
         };
         local:run-input-by-primary((:scenarios:), (:regions:), (:collection:))
               ]]> 
               </xPath>
               <comments/>
        </supplyDemandQuery>
   </aQuery>