Changeset 37 for 1dwg


Ignore:
Timestamp:
May 16, 2008 6:21:33 PM (12 years ago)
Author:
prjemian
Message:

improved the python example code a bit
Still fails when SASinstrument has a name attribute and not a name element
maybe other gotchas, as well

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 1dwg/trunk/python/python-test.py

    r35 r37  
    66''' 
    77 
     8# ../bimodal-test1.xml 
     9# ../s81-polyurea.xml 
     10 
    811import sys 
    912import gnosis.xml.objectify 
    1013 
    11 print sys.argv[1] 
    12 sasxml = gnosis.xml.objectify.XML_Objectify(sys.argv[1]).make_instance()   
    13 print 'title:', sasxml.SASentry.Title.PCDATA 
    14 print 'run:', sasxml.SASentry.Run.PCDATA 
    15 print 'instrument:', sasxml.SASentry.SASinstrument.name.PCDATA 
    16 print 'Q (', sasxml.SASentry.SASdata.Idata[0].Q.unit, ')  I (', sasxml.SASentry.SASdata.Idata[0].I.unit, ')' 
    17 print sasxml.SASentry.SASdata.Idata[0].Q.PCDATA,  sasxml.SASentry.SASdata.Idata[0].I.PCDATA,  sasxml.SASentry.SASdata.Idata[0].Idev.PCDATA 
     14def indra_metadata(SASentry): 
     15    '''print metadata from APS/USAXS Indra package''' 
     16    if 'metadata' in SASentry.__dict__: 
     17        for metadata in SASentry.metadata: 
     18            '''loop through the USAXS metadata''' 
     19            print metadata.xmlns 
     20            for index in metadata.usaxs: 
     21                '''multiple invocations of the usaxs element''' 
     22                for item in dir(index): 
     23                    '''discover the element names''' 
     24                    if (item[0] == '_'):    # sift out data structure management terms 
     25                        continue 
     26                    if (item == 'PCDATA'):  # sift this out 
     27                        continue 
     28                    if (item == 'name'):    # sift out the "name" attribute 
     29                        continue 
     30                    if (item == 'xmlns'):   # sift out the "xmlns" attribute 
     31                        continue 
     32                    s = '' 
     33                    s += '('+index.xmlns+') ' 
     34                    s += '('+index.name+') ' 
     35                    s += item + ': ' 
     36                    s += index.__dict__[item].PCDATA 
     37                    print s 
    1838 
     39def print_SASdata(sd): 
     40    '''print the contents of the SASdata element''' 
     41    numPts = len(sd.Idata) 
     42    if 'name' in sd.__dict__: 
     43        print 'SASdata name:', sd.name 
     44    print '# points:', numPts 
     45    s  = 'Q ('+sd.Idata[0].Q.unit+')' 
     46    s += '  I ('+sd.Idata[0].I.unit+')' 
     47    s += '  Idev ('+sd.Idata[0].Idev.unit+')' 
     48    print s 
     49    for Idata in sd.Idata: 
     50        print Idata.Q.PCDATA,  Idata.I.PCDATA,  Idata.Idev.PCDATA 
    1951 
     52def print_optional_item(title, parent, item): 
     53    ''' this item is optional and may not be present''' 
     54    if item in parent.__dict__: 
     55        #print item, item in parent.__dict__ 
     56        obj = parent.__dict__[item] 
     57        s = title+':\t' 
     58        s += obj.PCDATA 
     59        if 'unit' in obj.__dict__: 
     60            s += ' (' + obj.unit + ')' 
     61        print s 
     62 
     63xmlFile = sys.argv[1] 
     64print '#---------------------------------------------------' 
     65print 'XML:', xmlFile 
     66# read in the XML file 
     67sasxml = gnosis.xml.objectify.XML_Objectify(xmlFile).make_instance() 
     68# might check it here to make sure it is a "cansas1d/1.0" file 
     69print 'namespace:', sasxml.xmlns 
     70# might check it here to make sure it is version "1.0" 
     71print 'version:', sasxml.version 
     72SASentry = sasxml.SASentry                  # just the first one 
     73print 'title:', SASentry.Title.PCDATA 
     74print 'run:', SASentry.Run.PCDATA 
     75print_optional_item('instrument', SASentry.SASinstrument, 'name') 
     76indra_metadata(SASentry)        # foreign XML elements from APS/USAXS 
     77print_optional_item('sample ID', SASentry.SASsample, 'ID') 
     78print_optional_item('sample thickness', SASentry.SASsample, 'thickness') 
     79print_optional_item('sample transmission', SASentry.SASsample, 'transmission') 
     80if 'position' in SASentry.SASsample.__dict__: 
     81    print_optional_item('sample X', SASentry.SASsample.position, 'x') 
     82    print_optional_item('sample Y', SASentry.SASsample.position, 'y') 
     83print_SASdata(SASentry.SASdata) 
     84 
Note: See TracChangeset for help on using the changeset viewer.