Skip to content

Commit

Permalink
Merge pull request #20 from rigoudyg/gr_V2.0
Browse files Browse the repository at this point in the history
V2.0
  • Loading branch information
rigoudyg authored Jun 3, 2019
2 parents 936ff4e + c88f995 commit c967091
Show file tree
Hide file tree
Showing 31 changed files with 1,960 additions and 1,464 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ test/dr.xml
/test/run.out
*.pyc
*.pyc
pycallgraph_tree/
dr2xml_*.xml
pycallgraph_use.py
tests/
2 changes: 1 addition & 1 deletion DR2xml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
},
"outputs": [],
"source": [
" simulation_settings={ \n",
"simulation_settings={ \n",
" #--- Dictionnary describing the necessary attributes for a given simulation\n",
" #--- Warning : some lines are commented out in this example but should be \n",
" #--- un-commented in some cases. See comments\n",
Expand Down
129 changes: 83 additions & 46 deletions Xparse.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
#!/usr/bin/python
# coding: utf-8

# Whats' necessary for reading XIOS xml file and process attribute's inheritance for
# being able to request the grid_ref for any valid XIOS 'field' object
"""
Whats' necessary for reading XIOS xml file and process attribute's inheritance for
being able to request the grid_ref for any valid XIOS 'field' object
# Main useful functions :
# <opaque> context = init_context(context_name,printout=False)
# <ET object> grid = id2grid(field_id,context,printout=False)
Main useful functions :
<opaque> context = init_context(context_name,printout=False)
<ET object> grid = id2grid(field_id,context,printout=False)
"""

from __future__ import print_function, division, absolute_import, unicode_literals

from collections import OrderedDict

import os
import os.path
import re
import sys

from xml_interface import get_root_of_xml_file, create_string_from_xml_element
# Interface to xml tools
from xml_interface import get_root_of_xml_file


# Define for each object kind those attributes useful for grid inheritance
attributes = dict()
attributes = OrderedDict()
attributes['field'] = ['grid_ref', 'field_ref']
attributes['field_definition'] = attributes['field']
attributes['field_group'] = attributes['field']
Expand Down Expand Up @@ -52,21 +59,28 @@ def read_src(elt, path_parse, printout=False, level=0, dont_read=[]):
if skip:
continue
if printout:
print level * "\t" + "Reading %s" % filen
print(level * "\t" + "Reading %s" % filen)
et = get_root_of_xml_file(filen)
if printout:
print level * "\t" + "Reading %s, %s=%s" % (filen, et.tag, gattrib(et, 'id', 'no_id'))
print(level * "\t" + "Reading %s, %s=%s" % (filen, et.tag, gattrib(et, 'id', 'no_id')))
for el in et:
if printout:
print (level + 1) * "\t" + "Storing %s in %s id=%s" % (
el.tag, child.tag, gattrib(child, 'id', 'no_id'))
print((level + 1) * "\t" + "Storing %s in %s id=%s" % (el.tag, child.tag,
gattrib(child, 'id', 'no_id')))
child.append(el)
for child in elt:
# print level*"\t"+"Recursing on %s %s"%(child.tag,gattrib(child,'id','no_id'))
read_src(child, path_parse, printout, level + 1, dont_read)


def gattrib(e, attrib_name, default=None):
"""
Get the value of an attribute of an element.
:param e: xml element
:param attrib_name: name of the attribute
:param default: default value if attribute is missing
:return: the value of the attribute or default
"""
if attrib_name in e.attrib:
return e.attrib[attrib_name]
else:
Expand All @@ -80,7 +94,7 @@ def merge_sons(elt, printout=False, level=0):
"""
toremove = []
# Using a dict with first instance of an elt for each tag (or tag+id)
bytag = dict()
bytag = OrderedDict()
tags_to_merge = ['context', 'file_definition', 'field_definition',
'axis_definition', 'grid_definition', 'calendar', 'field',
'field_group', 'file_group']
Expand All @@ -106,7 +120,7 @@ def merge_sons(elt, printout=False, level=0):
else:
name = 'no_id'
if printout:
print level * "\t" + "Moving %s %s content to %s" % (child.tag, name, tag)
print(level * "\t" + "Moving %s %s content to %s" % (child.tag, name, tag))
#
# Move childs from secondary entry to first entry (brother)
for sub in child:
Expand All @@ -117,12 +131,12 @@ def merge_sons(elt, printout=False, level=0):
toremove.append(child)
for child in toremove:
if printout:
print "removing one %s child : %s" % (`elt`, `child`)
print("removing one %s child : %s" % (repr(elt), repr(child)))
elt.remove(child)
# Recursion
for child in elt:
if printout:
print level * "\t" + "%s %s" % (child.tag, child.attrib.get('id', 'no_id'))
print(level * "\t" + "%s %s" % (child.tag, child.attrib.get('id', 'no_id')))
merge_sons(child, printout, level + 1)


Expand All @@ -134,23 +148,23 @@ def solve_downward(attrib, elt, value=None, printout=False, level=0):
for child in elt:
value_down = value
if printout:
print level * "\t" + " solving on " + `child`,
print(level * "\t" + " solving on " + repr(child),)
if attrib in attributes.get(child.tag, []):
if attrib not in child.attrib:
if value is not None:
child.attrib[attrib] = value
if printout:
print " set :" + value
print(" set :" + value)
else:
if printout:
print " pass"
print(" pass")
else:
value_down = child.attrib[attrib]
if printout:
print " get :" + value_down
print(" get :" + value_down)
else:
if printout:
print
print()
solve_downward(attrib, child, value_down, printout, level + 1)


Expand All @@ -161,15 +175,15 @@ def make_index(elt, index=None, printout=False, level=0):
crossing their id multiple times
"""
if index is None:
index = dict()
index = OrderedDict()
for child in elt:
if 'id' in child.attrib:
the_id = child.attrib['id']
if printout:
print level * "\t" + " indexing " + the_id,
print(level * "\t" + " indexing " + the_id,)
if the_id in index:
if printout:
print " (merging)"
print(" (merging)")
# Update indexed object with current attributes
for a in child.attrib:
index[the_id].attrib[a] = child.attrib[a]
Expand All @@ -178,7 +192,7 @@ def make_index(elt, index=None, printout=False, level=0):
index[the_id].append(sub)
else:
if printout:
print " init index"
print(" init index")
index[the_id] = child
# else:
# if printout : print
Expand All @@ -196,21 +210,21 @@ def attrib_by_ref(elt, attrib, index, printout, level):
if '_ref' in a:
refid = elt.attrib[a]
if printout:
print "\n" + (level + 1) * "\t" + a + " -> " + refid,
print("\n" + (level + 1) * "\t" + a + " -> " + refid,)
try:
ref = index[refid]
if attrib in ref.attrib:
rep = ref.attrib[attrib]
if printout:
print " ---> !! GOT : " + rep + " !!!"
print(" ---> !! GOT : " + rep + " !!!")
return rep
else:
rep = attrib_by_ref(ref, attrib, index, printout, level + 1)
if rep:
return rep
except:
if not refid.startswith("dummy_"):
print "Error : reference '%s' is invalid" % refid
print("Error : reference '%s' is invalid" % refid)
sys.exit(1)


Expand All @@ -220,13 +234,13 @@ def solve_by_ref(attrib, index, elt, printout=False, level=0):
"""
got_one = 0
for child in elt:
if type(child) != type('') and child.tag != 'variable':
if not isinstance(child, str) and child.tag != 'variable':
if 'id' in child.attrib:
name = child.attrib['id']
else:
name = `child`
name = repr(child)
if printout:
print level * "\t" + attrib + " by_ref on " + name,
print(level * "\t" + attrib + " by_ref on " + name,)
#
if child.tag in attributes and attrib in attributes[child.tag]:
if attrib not in child.attrib:
Expand All @@ -237,31 +251,44 @@ def solve_by_ref(attrib, index, elt, printout=False, level=0):
got_one = got_one + 1
else:
if printout:
print
print()
else:
if printout:
print ", already set : %s" % child.attrib[attrib]
print(", already set : %s" % child.attrib[attrib])
got = solve_by_ref(attrib, index, child, printout, level + 1)
got_one = got_one + got
else:
if printout:
print " : N/A"
print(" : N/A")
return got_one


def select_context(rootel, context_id):
"""
Find the context corresponding to context_id
:param rootel: root of xml element
:param context_id: id of the context to find
:return: context corresponding to context_id in rootel
"""
for context in rootel:
if 'id' in context.attrib and context.attrib['id'] == context_id:
return context


def init_context(context_id, path_parse="./", printout=False):
"""
Create the index for xml elements
:param context_id: id of the context of the index
:param path_parse: directory of the xml iodef
:param printout: boolean to active verbose log
:return: the index of the context
"""
xmldef = path_parse + "iodef.xml"
if printout:
print "Parsing %s ..." % xmldef,
print("Parsing %s ..." % xmldef,)
rootel = get_root_of_xml_file(xmldef)
if printout:
print "sourcing files ...",
print("sourcing files ...",)
read_src(rootel, path_parse, printout=printout, dont_read=["dr2xml_"])
merge_sons(rootel, printout)
rootel = select_context(rootel, context_id)
Expand All @@ -275,16 +302,23 @@ def init_context(context_id, path_parse="./", printout=False):
while True:
n = solve_by_ref(ref, index, rootel, printout)
if printout:
print "%d refs solved" % n
print("%d refs solved" % n)
if n == 0:
break
# ET.dump(rootel)
return index
else:
print "Xparse::init_context : context %s not found in %s" % (context_id, xmldef)
print("Xparse::init_context : context %s not found in %s" % (context_id, xmldef))


def id2gridid(field_id, index, printout=False):
"""
Call to id2grid and get "id" parameter
:param field_id: id of the field
:param index: index of the xml elements
:param printout: boolean to active verbose log
:return: the id of the grid corresponding to the entry parameters.
"""
grid = id2grid(field_id, index, printout=printout)
return grid.attrib['id']

Expand All @@ -299,7 +333,7 @@ def id2grid(field_id, index, printout=False):
grid_ref_field_id = attrib['grid_ref']
if grid_ref_field_id in index:
if printout:
print "grid_ref value for %s is %s" % (grid_ref_field_id, `index[grid_ref_field_id]`)
print("grid_ref value for %s is %s" % (grid_ref_field_id, repr(index[grid_ref_field_id])))
return index[grid_ref_field_id]
else:
# if printout: print("field %s grid reference is %s
Expand All @@ -323,7 +357,7 @@ def idHasExprWithAt(field_id, index, printout=False):
attrib = index[field_id].attrib
if 'expr' in attrib:
if printout:
print "In withAt, for %s, expr=%s" % (field_id, attrib['expr'])
print("In withAt, for %s, expr=%s" % (field_id, attrib['expr']))
return '@' in attrib['expr']
else:
# if printout : print "In withAt, for %s, no expr"%(field_id)
Expand All @@ -336,26 +370,29 @@ def idHasExprWithAt(field_id, index, printout=False):
if False:

nemo = init_context('nemo', "./", False)
# print nemo.keys()
# print list(nemo)
grid = id2grid("CMIP6_O18sw", nemo, True)
print grid.attrib['id']
print
print(grid.attrib['id'])
print()

arpsfx = init_context('arpsfx', "./", False)
grid = id2grid("CMIP6_cdnc", arpsfx, True)
# grid=None
if grid is not None:
# print "Grid id is :"+grid.attrib['id']
print create_string_from_xml_element(grid)
print(create_string_from_xml_element(grid))
grid_string = create_string_from_xml_element(grid)
new_grid_string = re.sub('axis_ref= *.([\w_])*.', 'axis_ref="axis_autre"', grid_string)
print new_grid_string
print(new_grid_string)


class Xparse_error(Exception):
"""
Xparse exceptions class.
"""

def __init__(self, valeur):
self.valeur = valeur

def __str__(self):
return `self.valeur`
return repr(self.valeur)
Loading

0 comments on commit c967091

Please sign in to comment.