11# import netCDF4
22import pandas
33import os
4+ import sys
45import LoopProjectFile
56
67
@@ -30,6 +31,7 @@ def GetGroup(node, groupName, verbose=False):
3031
3132
3233def ElementFromDataframe (loopFilename , df , element , loopCompoundType ):
34+ # print('Entered ElementFromDataframe')
3335 """
3436 **ElementFromCsv** - Imports one element of the loop project file
3537 from a csv file into the project file
@@ -51,10 +53,12 @@ def ElementFromDataframe(loopFilename, df, element, loopCompoundType):
5153 """
5254 if isinstance (df , pandas .DataFrame ) is False :
5355 print ("not a dataframedoes not exist" )
54- return
56+ raise Exception ( "not a dataframedoes not exist" )
5557 if not os .path .isfile (loopFilename ):
5658 print (loopFilename , "does not exist. Try LoopProjectFile.CreateBasic first" )
57- return
59+ raise Exception (
60+ f"{ loopFilename } does not exist. Try LoopProjectFile.CreateBasic first"
61+ )
5862 if len (df .columns ) != len (loopCompoundType ):
5963 print ("In dataframe columns do not match compound type" )
6064 print (
@@ -63,11 +67,15 @@ def ElementFromDataframe(loopFilename, df, element, loopCompoundType):
6367 " does not match\n Compound type:" ,
6468 loopCompoundType .names ,
6569 )
66- return
67- struct = LoopProjectFile .ConvertDataFrame (df , loopCompoundType )
68- resp = LoopProjectFile .Set (loopFilename , element , data = struct )
69- if resp ["errorFlag" ]:
70- print (resp ["errorString" ])
70+ raise Exception (f"In dataframe columns do not match compound type" )
71+ try :
72+ struct = LoopProjectFile .ConvertDataFrame (df , loopCompoundType )
73+ resp = LoopProjectFile .Set (loopFilename , element , data = struct )
74+ if resp ["errorFlag" ]:
75+ print (resp ["errorString" ])
76+ raise Exception (resp ["errorString" ])
77+ except Exception as e :
78+ return f"Error in ElementFromDataframe for { element } : { e } "
7179
7280
7381def ElementFromCsv (loopFilename , importFilename , element , loopCompoundType ):
@@ -96,11 +104,15 @@ def ElementFromCsv(loopFilename, importFilename, element, loopCompoundType):
96104 if not os .path .isfile (loopFilename ):
97105 print (loopFilename , "does not exist. Try LoopProjectFile.CreateBasic first" )
98106 return
99- df = pandas .read_csv (importFilename )
100- ElementFromDataframe (loopFilename , df , element , loopCompoundType )
107+ try :
108+ df = pandas .read_csv (importFilename )
109+ ElementFromDataframe (loopFilename , df , element , loopCompoundType )
110+ except Exception as e :
111+ raise Exception (f"Error processing { importFilename } : { e } " )
101112
102113
103114def FromCsv (loopFilename , importPath , overwrite = False ):
115+ # print('Entered Fromcsv','loopFilename',loopFilename,'importPath',importPath)
104116 """
105117 **FromCsv** - Imports all elements of the loop project file
106118 from csv files into the project file
@@ -124,16 +136,16 @@ def FromCsv(loopFilename, importPath, overwrite=False):
124136 if overwrite :
125137 os .remove (loopFilename )
126138 else :
127- print (loopFilename , "already exists and overwrite not set" )
128- return
139+ print (loopFilename , "already exists and overwrite not set" , file = sys . stderr )
140+ raise Exception ( f"already exists and overwrite not set" )
129141
130142 importPath = importPath .replace ("\\ " , "/" )
131143 if importPath [- 1 ] != "/" and importPath [- 1 ] != "\\ " :
132144 importPath += "/"
133145
134146 if not os .path .isdir (importPath ):
135- print ("Import path" , importPath , "does not exist" )
136- return
147+ print ("Import path" , importPath , "does not exist" , file = sys . stderr )
148+ raise Exception ( f"Import path { importPath } does not exist" )
137149
138150 # Create the basic loop project file
139151 print ("Creating" , loopFilename )
@@ -142,7 +154,9 @@ def FromCsv(loopFilename, importPath, overwrite=False):
142154 print (" Importing from" , str (importPath ) + "extents.csv" , "into project file" )
143155 if not os .path .isfile (importPath + "extents.csv" ):
144156 print (str (importPath ) + "extents.csv" , "does not exist" )
157+ raise Exception ("extents.csv is required" )
145158 else :
159+ print (importPath + "extents.csv" , file = sys .stderr )
146160 df = pandas .read_csv (str (importPath ) + "extents.csv" )
147161 extents = {}
148162 extents ["geodesic" ] = list (df .values [0 ][0 :4 ])
@@ -152,6 +166,7 @@ def FromCsv(loopFilename, importPath, overwrite=False):
152166 LoopProjectFile .Set (loopFilename , "extents" , ** extents )
153167
154168 # Import from various csvs
169+ # try:
155170 print (" Importing from" , str (importPath ) + "contacts.csv" , "into project file" )
156171 ElementFromCsv (
157172 loopFilename ,
@@ -197,6 +212,7 @@ def FromCsv(loopFilename, importPath, overwrite=False):
197212 "foliationLog" ,
198213 LoopProjectFile .foliationEventType ,
199214 )
215+
200216 print (" Importing from" , str (importPath ) + "foliationObs.csv" , "into project file" )
201217 ElementFromCsv (
202218 loopFilename ,
@@ -216,6 +232,7 @@ def FromCsv(loopFilename, importPath, overwrite=False):
216232 "discontinuityLog" ,
217233 LoopProjectFile .discontinuityEventType ,
218234 )
235+
219236 print (
220237 " Importing from" ,
221238 str (importPath ) + "discontinuityObs.csv" ,
@@ -239,6 +256,7 @@ def FromCsv(loopFilename, importPath, overwrite=False):
239256 "stratigraphicLog" ,
240257 LoopProjectFile .stratigraphicLayerType ,
241258 )
259+
242260 print (
243261 " Importing from" ,
244262 str (importPath ) + "stratigraphicObs.csv" ,
@@ -258,6 +276,7 @@ def FromCsv(loopFilename, importPath, overwrite=False):
258276 "eventRelationships" ,
259277 LoopProjectFile .eventRelationshipType ,
260278 )
279+ return "All CSV files processed successfully"
261280
262281
263282def ElementToDataframe (loopFilename , element , loopCompoundType ):
@@ -499,3 +518,122 @@ def ToCsv(loopFilename, outputPath):
499518 "eventRelationships" ,
500519 LoopProjectFile .eventRelationshipType ,
501520 )
521+
522+
523+ def handleLoopProjectFile (file , shared_path = "/shared" ):
524+ if file :
525+ filename = file .filename
526+ if not filename .endswith (".loop3d" ):
527+ filename += ".loop3d"
528+ filepath = os .path .join (shared_path , filename )
529+ if os .path .exists (filepath ):
530+ raise Exception (f"File { filename } already exists in the shared path." )
531+ file .save (filepath )
532+ if not LoopProjectFile .CheckFileValid (filepath ):
533+ os .remove (filepath )
534+ raise Exception ("Uploaded file is not a valid LoopProjectFile." )
535+ return
536+ else :
537+ raise Exception ("No file was provided for upload." )
538+
539+
540+ def handleCSVlist (files , loopFilename , shared_path = "/shared" ):
541+ if not loopFilename :
542+ raise Exception ("loopFilename is required" )
543+ if not files :
544+ raise Exception ("No CSV files provided" )
545+
546+ loop_file_path = os .path .join (shared_path , loopFilename )
547+ saved_files = []
548+
549+ for file_storage in files .getlist ("file" ):
550+ if file_storage and file_storage .filename .endswith (".csv" ):
551+ filepath = os .path .join (shared_path , file_storage .filename )
552+ file_storage .save (filepath )
553+ saved_files .append (filepath )
554+
555+ try :
556+ FromCsv (loop_file_path , shared_path )
557+ except Exception as conversion_error :
558+ for csv_file in saved_files :
559+ try :
560+ os .remove (os .path .join (shared_path , csv_file ))
561+ except Exception as e :
562+ print (f"Failed to delete CSV file { csv_file } : { e } " )
563+ if os .path .exists (loop_file_path ):
564+ try :
565+ os .remove (loop_file_path )
566+ except Exception as e :
567+ print (f"Failed to delete project file { loop_file_path } : { e } " )
568+ raise conversion_error
569+ else :
570+ for csv_file in saved_files :
571+ try :
572+ os .remove (os .path .join (shared_path , csv_file ))
573+ except Exception as e :
574+ print (f"Failed to delete CSV file { csv_file } : { e } " )
575+
576+ return "success" , f"{ loopFilename } is created and saved successfully"
577+
578+
579+ def handleLoopProjectFile (file , shared_path = "/shared" ):
580+ if file :
581+ filename = file .filename
582+
583+ if not filename .endswith (".loop3d" ):
584+ filename += ".loop3d"
585+
586+ filepath = os .path .join (shared_path , filename )
587+
588+ if os .path .exists (filepath ):
589+ raise Exception (f"File { filename } already exists in the shared path." )
590+
591+ file .save (filepath )
592+
593+ if not LoopProjectFile .CheckFileValid (filepath ):
594+ os .remove (filepath )
595+ raise Exception ("Uploaded file is not a valid LoopProjectFile." )
596+
597+ return
598+
599+ else :
600+ raise Exception ("No file was provided for upload." )
601+
602+
603+ def handleCSVlist (files , loopFilename , shared_path = "/shared" ):
604+ if not loopFilename :
605+ raise Exception ("loopFilename is required" )
606+ if not files :
607+ raise Exception ("No CSV files provided" )
608+
609+ loop_file_path = os .path .join (shared_path , loopFilename )
610+ saved_files = []
611+
612+ for file_storage in files .getlist ("file" ):
613+ if file_storage and file_storage .filename .endswith (".csv" ):
614+ filepath = os .path .join (shared_path , file_storage .filename )
615+ file_storage .save (filepath )
616+ saved_files .append (filepath )
617+
618+ try :
619+ FromCsv (loop_file_path , shared_path )
620+ except Exception as conversion_error :
621+ for csv_file in saved_files :
622+ try :
623+ os .remove (os .path .join (shared_path , csv_file ))
624+ except Exception as e :
625+ print (f"Failed to delete CSV file { csv_file } : { e } " )
626+ if os .path .exists (loop_file_path ):
627+ try :
628+ os .remove (loop_file_path )
629+ except Exception as e :
630+ print (f"Failed to delete project file { loop_file_path } : { e } " )
631+ raise conversion_error
632+ else :
633+ for csv_file in saved_files :
634+ try :
635+ os .remove (os .path .join (shared_path , csv_file ))
636+ except Exception as e :
637+ print (f"Failed to delete CSV file { csv_file } : { e } " )
638+
639+ return "success" , f"{ loopFilename } is created and saved successfully"
0 commit comments