@@ -26,7 +26,7 @@ def GetGroup(node,groupName,verbose=False):
2626 if verbose : print (errStr )
2727 return {"errorFlag" :True ,"errorString" :errStr }
2828
29- def ElementFromCsv (loopFilename ,importFilename ,element ,loopCompoundType ):
29+ def ElementFromDataframe (loopFilename ,df ,element ,loopCompoundType ):
3030 """
3131 **ElementFromCsv** - Imports one element of the loop project file
3232 from a csv file into the project file
@@ -46,22 +46,51 @@ def ElementFromCsv(loopFilename,importFilename,element,loopCompoundType):
4646 -------
4747
4848 """
49- if not os . path . isfile ( importFilename ) :
50- print (importFilename , 'does not exist' )
49+ if isinstance ( df , pandas . DataFrame ) == False :
50+ print ('not a dataframedoes not exist' )
5151 return
5252 if not os .path .isfile (loopFilename ):
5353 print (loopFilename ,'does not exist. Try LoopProjectFile.CreateBasic first' )
5454 return
55- df = pandas .read_csv (importFilename )
5655 if len (df .columns ) != len (loopCompoundType ):
57- print ('In ' , importFilename , ' columns do not match compound type' )
56+ print ('In dataframe columns do not match compound type' )
5857 print (' Dataframe:' ,df .columns ,' does not match\n Compound type:' ,loopCompoundType .names )
5958 return
6059 struct = LoopProjectFile .ConvertDataFrame (df ,loopCompoundType )
6160 resp = LoopProjectFile .Set (loopFilename ,element ,data = struct )
6261 if resp ['errorFlag' ]:
6362 print (resp ['errorString' ])
6463
64+ def ElementFromCsv (loopFilename ,importFilename ,element ,loopCompoundType ):
65+ """
66+ **ElementFromCsv** - Imports one element of the loop project file
67+ from a csv file into the project file
68+
69+ Parameters
70+ ----------
71+ loopFilename: string
72+ The filename of the loop project file
73+ importFilename: string
74+ The filename of the csv file containing the element data
75+ element: string
76+ The name of the element to extract
77+ loopCompoundType: numpy.compoundType
78+ The numpy data structure that the element is stored in
79+
80+ Returns
81+ -------
82+
83+ """
84+ if not os .path .isfile (importFilename ):
85+ print (importFilename ,'does not exist' )
86+ return
87+ if not os .path .isfile (loopFilename ):
88+ print (loopFilename ,'does not exist. Try LoopProjectFile.CreateBasic first' )
89+ return
90+ df = pandas .read_csv (importFilename )
91+ ElementFromDataframe (loopFilename ,df ,element ,loopCompoundType )
92+
93+
6594def FromCsv (loopFilename ,importPath ,overwrite = False ):
6695 """
6796 **FromCsv** - Imports all elements of the loop project file
@@ -146,7 +175,7 @@ def FromCsv(loopFilename,importPath,overwrite=False):
146175 print (' Importing from' ,str (importPath )+ 'eventRel.csv' ,'into project file' )
147176 ElementFromCsv (loopFilename ,importPath + 'eventRel.csv' ,'eventRelationships' ,LoopProjectFile .eventRelationshipType )
148177
149- def ElementToCsv (loopFilename , outputFilename ,element ,loopCompoundType ):
178+ def ElementToDataframe (loopFilename ,element ,loopCompoundType ):
150179 """
151180 **ElementToCsv** - Exports one element of the loop project file
152181 to a csv file outputFilename
@@ -155,8 +184,6 @@ def ElementToCsv(loopFilename,outputFilename,element,loopCompoundType):
155184 ----------
156185 loopFilename: string
157186 The filename of the loop project file
158- outputFilename: string
159- The filename of the csv file which will contain the element data
160187 element: string
161188 The name of the element to extract
162189 loopCompoundType: numpy.compoundType
@@ -169,12 +196,40 @@ def ElementToCsv(loopFilename,outputFilename,element,loopCompoundType):
169196 resp = LoopProjectFile .Get (loopFilename ,element )
170197 if resp ['errorFlag' ]:
171198 print (resp ['errorString' ])
199+ return None
172200 else :
173201 columns = list (loopCompoundType .names )
174202 df = pandas .DataFrame .from_records (resp ['value' ],columns = columns )
203+ for name in columns :
204+ df [name ] = df [name ].astype (loopCompoundType [name ])
175205 df = df .applymap (lambda x :x .decode () if isinstance (x ,bytes ) else x )
176- df .set_index (columns [0 ],inplace = True )
206+ # df.set_index(columns[0],inplace=True)
207+ return df #.to_csv(outputFilename)
208+
209+ def ElementToCsv (loopFilename ,outputFilename ,element ,loopCompoundType ):
210+ """
211+ **ElementToCsv** - Exports one element of the loop project file
212+ to a csv file outputFilename
213+
214+ Parameters
215+ ----------
216+ loopFilename: string
217+ The filename of the loop project file
218+ outputFilename: string
219+ The filename of the csv file which will contain the element data
220+ element: string
221+ The name of the element to extract
222+ loopCompoundType: numpy.compoundType
223+ The numpy data structure that the element is stored in
224+
225+ Returns
226+ -------
227+
228+ """
229+ df = ElementToDataframe (loopFilename ,element ,loopCompoundType )
230+ if df :
177231 df .to_csv (outputFilename )
232+
178233
179234def ToCsv (loopFilename ,outputPath ):
180235 """
0 commit comments