1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2016-01-22 19:05:12 +0000 (Fri, 22 Jan 2016) $ |
---|
4 | # $Author: toby $ |
---|
5 | # $Revision: 2133 $ |
---|
6 | # $URL: trunk/imports/G2pwd_csv.py $ |
---|
7 | # $Id: G2pwd_csv.py 2133 2016-01-22 19:05:12Z toby $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | ''' |
---|
10 | *Module G2pwd_csv: Read Excel .csv data* |
---|
11 | ------------------------------------------ |
---|
12 | |
---|
13 | Routine to read in powder data from Excel type comma separated variable |
---|
14 | column-oriented variable |
---|
15 | |
---|
16 | ''' |
---|
17 | |
---|
18 | import sys |
---|
19 | import os.path as ospath |
---|
20 | import numpy as np |
---|
21 | import GSASIIIO as G2IO |
---|
22 | import GSASIIpath |
---|
23 | GSASIIpath.SetVersionNumber("$Revision: 2133 $") |
---|
24 | class csv_ReaderClass(G2IO.ImportPowderData): |
---|
25 | 'Routines to import powder data from a .xye file' |
---|
26 | def __init__(self): |
---|
27 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
28 | extensionlist=('.csv',), |
---|
29 | strictExtension=True, |
---|
30 | formatName = 'comma/tab separated (.csv)', |
---|
31 | longFormatName = 'Worksheet-type .csv powder data file' |
---|
32 | ) |
---|
33 | |
---|
34 | # Validate the contents -- make sure we only have valid lines |
---|
35 | def ContentsValidator(self, filepointer): |
---|
36 | return True # no errors encountered |
---|
37 | |
---|
38 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
39 | 'Read a csv file' |
---|
40 | x = [] |
---|
41 | y = [] |
---|
42 | w = [] |
---|
43 | try: |
---|
44 | begin = True |
---|
45 | for i,S in enumerate(filepointer): |
---|
46 | vals = S.replace(',',' ').split() |
---|
47 | if len(vals) < 2 and i > 0: |
---|
48 | print 'Line '+str(i+1)+' cannot be read:\n\t'+S |
---|
49 | continue |
---|
50 | try: |
---|
51 | x.append(float(vals[0])) |
---|
52 | f = float(vals[1]) |
---|
53 | if f <= 0.0: |
---|
54 | y.append(0.0) |
---|
55 | w.append(0.0) |
---|
56 | elif len(vals) == 3: |
---|
57 | y.append(float(vals[1])) |
---|
58 | w.append(1.0/float(vals[2])**2) |
---|
59 | else: |
---|
60 | y.append(float(vals[1])) |
---|
61 | w.append(1.0/float(vals[1])) |
---|
62 | err = False |
---|
63 | except ValueError: |
---|
64 | err = True |
---|
65 | msg = 'Error parsing number in line '+str(i+1) |
---|
66 | except: |
---|
67 | err = True |
---|
68 | msg = 'Error in line '+str(i+1) |
---|
69 | if err and i > 0: |
---|
70 | print msg |
---|
71 | print S |
---|
72 | break |
---|
73 | N = len(x) |
---|
74 | self.powderdata = [ |
---|
75 | np.array(x), # x-axis values |
---|
76 | np.array(y), # powder pattern intensities |
---|
77 | np.array(w), # 1/sig(intensity)^2 values (weights) |
---|
78 | np.zeros(N), # calc. intensities (zero) |
---|
79 | np.zeros(N), # calc. background (zero) |
---|
80 | np.zeros(N), # obs-calc profiles |
---|
81 | ] |
---|
82 | self.powderentry[0] = filename |
---|
83 | #self.powderentry[1] = pos # bank offset (N/A here) |
---|
84 | self.powderentry[2] = 1 # xye file only has one bank |
---|
85 | self.idstring = ospath.basename(filename) |
---|
86 | # scan comments for temperature |
---|
87 | Temperature = 300 |
---|
88 | for S in self.comments: |
---|
89 | if 'Temp' in S.split('=')[0]: |
---|
90 | try: |
---|
91 | Temperature = float(S.split('=')[1]) |
---|
92 | except: |
---|
93 | pass |
---|
94 | self.Sample['Temperature'] = Temperature |
---|
95 | |
---|
96 | return True |
---|
97 | except Exception as detail: |
---|
98 | self.errors += '\n '+str(detail) |
---|
99 | print self.formatName+' read error:'+str(detail) # for testing |
---|
100 | import traceback |
---|
101 | traceback.print_exc(file=sys.stdout) |
---|
102 | return False |
---|