source: 1dwg/trunk/examples/af1410/cs_af1410.py @ 7

Last change on this file since 7 was 7, checked in by prjemian, 13 years ago

use trunk,tags,branches

File size: 6.4 KB
Line 
1#!/usr/bin/env python
2
3# file:    cs_af1410.py
4# author:  Pete R. Jemian
5# date:    2008-02-25
6#
7# Case Study:
8#
9# read the AF1410 SANS data file archives and
10# build a single canSAS v1.0 reduced 1D SANS data file
11#
12
13import os
14import string
15import time
16import types
17
18reqDir = "/home/prjemian/af1410/"
19reqDir = "historical/"
20#cansasXMLFile = "./cs_af1410.xml"
21
22aging = {}
23aging["qu"] ="0.25" 
24aging["hf"] ="0.5" 
25aging["1h"] ="1" 
26aging["2h"] ="2" 
27aging["5h"] ="5" 
28aging["8h"] ="8" 
29aging["10"] ="10" 
30aging["20"] ="20"    # a20 dataset not found!
31aging["50"] ="50"
32aging["cc"] ="100" 
33
34sector = {
35  "a": "nuclear sector", 
36  "b": "nuclear+magnetic sector"
37}
38
39
40#-------------------------------------------------------------
41
42def readVMHfile(dirName, fileName):
43  '''return SANS data from file
44   file contains three columns: Q, I, esd'''
45  # prepare to return some information
46  mylist = []
47  #------------------
48  # read the file into a buffer: guts
49  cwd = os.getcwd()
50  os.chdir(dirName)
51  if (os.path.exists(fileName) != 1):
52    os.chdir(cwd)
53    return(mylist)
54  file = open(fileName, 'r')
55  guts = file.read()
56  file.close()
57  os.chdir(cwd)
58  #------------------
59  # interpret the file data
60  lines = guts.split("\r")
61  numLines = len(lines)
62  #print numLines, " lines in the file: ", fileName
63  index = 0
64  while (index < numLines):
65    line = lines[index].split()
66    if (len(line)):
67      # print line
68      mylist.append(line)
69    index = index + 1
70  return(mylist)
71
72#-------------------------------------------------------------
73
74def makeXMLelement(fullTag, content='', indent=1):
75  '''This is the new version of how to make XML elements'''
76  #print "[%s] [%s] %d\n" % (fullTag, content, indent)
77  #print type(content)
78  if (type(content) == types.IntType):
79    content = "%d" % (content)
80  elif (type(content) == type(123.4)):
81    content = "%g" % (content)
82  parts = fullTag.split()
83  shortTag = parts[0]
84  tag = "<" + fullTag
85  if (len(content)):
86    tag = tag + ">"
87    if (indent != 0):
88      tag = tag + "\n"
89      indentation = "  "
90      for line in content.split("\n"):
91        if (len(line) > 0):
92          # indent each line of the content
93          tag = tag + indentation + line + "\n"
94    else:
95      tag =  tag + content
96    tag = tag + "</" + shortTag + ">"
97  else:
98    tag = tag + " />"
99  return(tag + "\n")
100
101
102#-------------------------------------------------------------
103
104def makeSASentry(dirName, index, aging, sectors):
105   title = "AF1410-" + index + " (AF1410 steel aged " + aging[index] + " h)"
106   SASentry = ""
107   SASentry = SASentry + makeXMLelement('Title', title, 0)
108   # associate Run and SASdata using common "name" attribute based on sector&aging codes
109   for alpha in sectors:
110      SASentry = SASentry + makeXMLelement('Run name="AF1410-' + alpha + index + '"', sectors[alpha], 0)
111   for alpha in sectors:
112      SASentry = SASentry + makeSASdata(dirName, alpha + index + ".vmh", "AF1410-" + alpha + index)
113   SASentry = SASentry + makeSASsample(title)
114   SASentry = SASentry + makeSASinstrument()
115   SASentry = SASentry + makeSASnote()
116   return(makeXMLelement('SASentry name="' + "AF1410:" + index + '"', SASentry, 1))
117
118def makeSASdata(dirName, fileName, name):
119   Idata = ""
120   Q_I_esd = readVMHfile(dirName, fileName)
121   for Q, I, esd in Q_I_esd:
122     str = ""
123     str = str + makeXMLelement('Q unit="1/A"', Q, 0).strip() 
124     str = str + makeXMLelement('I unit="1/cm"', I, 0).strip() 
125     str = str + makeXMLelement('Qdev unit="1/A"', 0.001, 0).strip() 
126     str = str + makeXMLelement('Idev unit="1/cm"', esd, 0).strip() 
127     Idata = Idata + makeXMLelement('Idata', str, 0)
128   SASdata = makeXMLelement('SASdata name="' + name + '"', Idata, 1)
129   return( SASdata )
130
131def makeSASsample(title):
132  SASsample = ""
133  SASsample = SASsample + makeXMLelement('ID', title, 0)
134  details = "transverse saturation magnetic field (1.6 T) applied in horizontal direction to clear magnetic domain scattering"
135  SASsample = SASsample + makeXMLelement('details', details, 1)
136  makeXMLelement('SASsample', SASsample, 1)
137  return( makeXMLelement('SASsample', SASsample, 1) )
138
139def makeSASinstrument():
140   SASinstrument = ""
141   SASinstrument = SASinstrument + makeXMLelement('name', "NIST/CNRF SANS", 0)
142   SASsource = ""
143   SASsource = SASsource + makeXMLelement('radiation', "neutron", 0)
144   SASsource = SASsource + makeXMLelement('wavelength unit="nm"', "0.85", 0)
145   SASsource = SASsource + makeXMLelement('wavelength_spread unit="percent"', "25", 0)
146   SASinstrument = SASinstrument + makeXMLelement('SASsource', SASsource, 1)
147   SASinstrument = SASinstrument + makeXMLelement('SAScollimation',"", 0)
148   SASinstrument = SASinstrument + makeXMLelement('SASdetector', makeXMLelement('name', "area", 0), 1)
149   return( makeXMLelement('SASinstrument', SASinstrument, 1) )
150
151def makeSASnote():
152   authors = ""
153   authors = authors + makeXMLelement('author', "A.J. Allen", 0)
154   authors = authors + makeXMLelement('author', "D. Gavillet", 0)
155   authors = authors + makeXMLelement('author', "J.R. Weertman", 0)
156   atitle = makeXMLelement('title', "Small-Angle Neutron Scattering Studies of Carbide Precipitation in Ultrahigh-Strength Steels", 1)
157   journal = makeXMLelement('journal', "Acta Metall", 0)
158   volume = makeXMLelement('volume', "41", 0)
159   year = makeXMLelement('year', "1993", 0)
160   pages = makeXMLelement('pages', "1869-1884", 0)
161   article = makeXMLelement('authors', authors, 1)
162   SASnote = makeXMLelement('citation', article + atitle + journal + volume  + year + pages, 1)
163   return( makeXMLelement('SASnote', SASnote, 1) )
164
165# -------------------------------------------------------------
166
167def makeSASroot(reqDir, aging, sector):
168   xml = ""
169   SASroot_tag = 'SASroot version="1.0"'
170   SASroot_tag = SASroot_tag + '\n xmlns="http://www.smallangles.net/cansas1d"'
171   SASroot_tag = SASroot_tag + '\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
172   SASroot_tag = SASroot_tag + '\n xsi:schemaLocation="http://www.smallangles.net/cansas1d/ '
173   SASroot_tag = SASroot_tag + '\n                     http://www.smallangles.net/cansas1d/1.0/cansas1d.xsd"'
174
175   for index in aging:
176       entry = makeSASentry(reqDir, index, aging, sector)
177       #print len(entry)
178       xml = xml + entry
179
180   complete_xml = ""
181   complete_xml = complete_xml + '<?xml version="1.0"?>\n'
182   complete_xml = complete_xml + makeXMLelement(SASroot_tag, xml, 1)
183   return( complete_xml )
184
185# -------------------------------------------------------------
186
187print makeSASroot(reqDir, aging, sector)
Note: See TracBrowser for help on using the repository browser.