source: 1dwg/tags/v1.1/python/cansas1d.py

Last change on this file was 313, checked in by prjemian, 7 years ago
  • refs #23
  • applied license text
  • cleaned up the Python binding documentation
  • added list of authors and contributors
  • for each binding, show how to get I(Q)
  • retrieve CanSas1dType.java from repository (example in the Java binding documentation calls for it)
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision URL
File size: 2.7 KB
Line 
1#!/usr/bin/env python
2
3'''
4read canSAS 1-D XML data files (either v1.0 or v1.1)
5
6:requires:
7    gnosis.xml.objectify     # easy_install -U gnosis
8
9basic use in a program::
10
11    import cansas1d
12    try:
13        sasxml = cansas1d.readCanSasFile(xmlFile)
14    except cansas1d.Exception_canSAS_namespace, answer:
15        print "wrong XML namespace:", answer
16        return
17    except cansas1d.Exception_canSAS_version, answer:
18        print "wrong version string:", answer
19        return
20
21Copyright (c) 2013, UChicago Argonne, LLC
22This file is distributed subject to a Software License Agreement found
23in the file LICENSE that is included with this distribution.
24'''
25
26
27########### SVN repository information ###################
28# $Date$
29# $Author$
30# $Revision$
31# $URL$
32# $Id$
33########### SVN repository information ###################
34
35
36import os
37import sys
38import gnosis.xml.objectify     # easy_install -U gnosis
39
40
41    # support reading v1.0 and v1.1 data files
42    # v1.1 schema is backwards compatible, mostly
43CANSAS_NAMESPACES = {
44    '1.0': 'cansas1d/1.0',
45    '1.1': 'urn:cansas1d:1.1',
46}
47
48
49def readCanSasFile(xmlFile):
50    '''
51    open a canSAS XML data file as a gnosis file object
52   
53    :param str xmlFile: name of canSAS 1D XML data file
54    :returns: gnosis object with XML data structure
55    :raises Exception_canSAS_namespace: if namespace does not match
56    :raises Exception_canSAS_version: if version does not match
57    '''
58    # read in the XML file
59    sasxml = gnosis.xml.objectify.XML_Objectify(xmlFile).make_instance()
60   
61    # namespace check to accept file as canSAS XML
62    if sasxml.xmlns not in CANSAS_NAMESPACES.values():
63        raise Exception_canSAS_namespace, str(sasxml.xmlns)
64   
65    # version check
66    if sasxml.version not in CANSAS_NAMESPACES.keys():
67        raise Exception_canSAS_version, str(sasxml.version)
68   
69    return sasxml
70
71class Exception_canSAS_namespace(Exception):
72    '''canSAS XML file namespace'''
73    pass
74
75class Exception_canSAS_version(Exception):
76    '''version string of the canSAS standard'''
77    pass
78
79if __name__ == "__main__":
80    '''just for demonstration using example data files'''
81    for xmlFile in ('bimodal-test1', 's81-polyurea'):
82        path = os.path.abspath(os.path.join('..', 'examples', xmlFile+'.xml'))
83        if not os.path.exists(path):
84            continue
85        try:
86            sasxml = readCanSasFile(path)
87        except Exception_canSAS_namespace, answer:
88            print xmlFile, "wrong XML namespace:", answer
89            continue
90        except Exception_canSAS_version, answer:
91            print xmlFile, "wrong version string:", answer
92            continue
93        print '%s has %d data points' % (xmlFile, len(sasxml.SASentry.SASdata.Idata))
94       
Note: See TracBrowser for help on using the repository browser.