Skip to content

Commit 213b6c6

Browse files
authored
Merge pull request #11 from Loop3D/noelle-lpf
refactor OpenProjectFile and Get in LPF.py for error handling
2 parents 7c1a365 + 92f094b commit 213b6c6

File tree

4 files changed

+77
-70
lines changed

4 files changed

+77
-70
lines changed

LoopProjectFile/LoopProjectFile.py

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import numpy
3333
import pandas
3434

35-
# import sys
35+
import sys
3636
import os
3737
import enum
3838

@@ -126,20 +126,29 @@ def OpenProjectFile(filename, readOnly=True, verbose=True):
126126
127127
"""
128128
if verbose:
129-
print("Accessing file named: " + filename)
129+
print(f"Accessing file named: {filename}", file=sys.stderr)
130+
130131
if not os.path.isfile(filename):
131-
errStr = "File " + filename + " does not exist"
132-
print(errStr)
132+
errStr = f"File {filename} does not exist"
133+
print(errStr, file=sys.stderr)
133134
return {"errorFlag": True, "errorString": errStr}
135+
136+
try:
137+
with open(filename, 'rb') as f:
138+
print(f"File {filename} opened successfully.", file=sys.stderr)
139+
except Exception as e:
140+
return {"errorFlag": True, "errorString": str(e)}
141+
134142
readFlag = "r" if readOnly else "a"
135-
rootgrp = netCDF4.Dataset(filename, readFlag, format="NETCDF4")
136-
if not rootgrp:
137-
errStr = "(ERROR) File was not a Loop Project File"
138-
print(errStr)
139-
return {"errorFlag": True, "errorString": errStr}
140-
if verbose:
141-
print("NetCDF data model type: " + rootgrp.data_model)
142-
return {"errorFlag": False, "root": rootgrp}
143+
try:
144+
rootgrp = netCDF4.Dataset(filename, readFlag, format="NETCDF4")
145+
if rootgrp:
146+
if verbose:
147+
print(f"NetCDF data model type: {rootgrp.data_model}", file=sys.stderr)
148+
return {"errorFlag": False, "root": rootgrp}
149+
except Exception as e:
150+
print(f"Error occurred while opening file {filename}: {e}", file=sys.stderr)
151+
return {"errorFlag": True, "errorString": str(e)}
143152

144153

145154
# Accessor Function handling opening and closing of file and calling
@@ -370,57 +379,61 @@ def Get(filename, element, **kwargs):
370379
response = fileResp
371380
else:
372381
root = fileResp["root"]
373-
if element == "version":
374-
response = Version.GetVersion(root)
375-
elif element == "extents":
376-
response = Extents.GetExtents(root)
377-
elif element == "strModel":
378-
response = StructuralModels.GetStructuralModel(root, **kwargs)
379-
elif element == "faultObservations":
380-
response = DataCollection.GetFaultObservations(root, **kwargs)
381-
elif element == "foldObservations":
382-
response = DataCollection.GetFoldObservations(root, **kwargs)
383-
elif element == "foliationObservations":
384-
response = DataCollection.GetFoliationObservations(root, **kwargs)
385-
elif element == "discontinuityObservations":
386-
response = DataCollection.GetDiscontinuityObservations(root, **kwargs)
387-
elif element == "stratigraphicObservations":
388-
response = DataCollection.GetStratigraphicObservations(root, **kwargs)
389-
elif element == "contacts":
390-
response = DataCollection.GetContacts(root, **kwargs)
391-
elif element == "drillholeObservations":
392-
response = DataCollection.GetDrillholeObservations(root, **kwargs)
393-
elif element == "drillholeSurveys":
394-
response = DataCollection.GetDrillholeSurveys(root, **kwargs)
395-
elif element == "drillholeProperties":
396-
response = DataCollection.GetDrillholeProperties(root, **kwargs)
397-
elif element == "stratigraphicLog":
398-
response = ExtractedInformation.GetStratigraphicLog(root, **kwargs)
399-
elif element == "faultLog":
400-
response = ExtractedInformation.GetFaultLog(root, **kwargs)
401-
elif element == "foldLog":
402-
response = ExtractedInformation.GetFoldLog(root, **kwargs)
403-
elif element == "foliationLog":
404-
response = ExtractedInformation.GetFoliationLog(root, **kwargs)
405-
elif element == "discontinuityLog":
406-
response = ExtractedInformation.GetDiscontinuityLog(root, **kwargs)
407-
elif element == "drillholeLog":
408-
response = ExtractedInformation.GetDrillholeLog(root, **kwargs)
409-
elif element == "dataCollectionConfig":
410-
response = DataCollection.GetConfiguration(root, **kwargs)
411-
elif element == "dataCollectionSources":
412-
response = DataCollection.GetSources(root, **kwargs)
413-
elif element == "dataCollectionRawSourceData":
414-
response = DataCollection.GetRawSourceData(root, **kwargs)
415-
elif element == "eventRelationships":
416-
response = ExtractedInformation.GetEventRelationships(root, **kwargs)
417-
elif element == "structuralModelsConfig":
418-
response = StructuralModels.GetConfiguration(root, **kwargs)
419-
else:
420-
errStr = "(ERROR) Unknown element for Get function '" + element + "'"
421-
print(errStr)
422-
response = {"errorFlag": True, "errorString": errStr}
423-
root.close()
382+
try:
383+
if element == "version":
384+
response = Version.GetVersion(root)
385+
elif element == "extents":
386+
response = Extents.GetExtents(root)
387+
elif element == "strModel":
388+
response = StructuralModels.GetStructuralModel(root, **kwargs)
389+
elif element == "faultObservations":
390+
response = DataCollection.GetFaultObservations(root, **kwargs)
391+
elif element == "foldObservations":
392+
response = DataCollection.GetFoldObservations(root, **kwargs)
393+
elif element == "foliationObservations":
394+
response = DataCollection.GetFoliationObservations(root, **kwargs)
395+
elif element == "discontinuityObservations":
396+
response = DataCollection.GetDiscontinuityObservations(root, **kwargs)
397+
elif element == "stratigraphicObservations":
398+
response = DataCollection.GetStratigraphicObservations(root, **kwargs)
399+
elif element == "contacts":
400+
response = DataCollection.GetContacts(root, **kwargs)
401+
elif element == "drillholeObservations":
402+
response = DataCollection.GetDrillholeObservations(root, **kwargs)
403+
elif element == "drillholeSurveys":
404+
response = DataCollection.GetDrillholeSurveys(root, **kwargs)
405+
elif element == "drillholeProperties":
406+
response = DataCollection.GetDrillholeProperties(root, **kwargs)
407+
elif element == "stratigraphicLog":
408+
response = ExtractedInformation.GetStratigraphicLog(root, **kwargs)
409+
elif element == "faultLog":
410+
response = ExtractedInformation.GetFaultLog(root, **kwargs)
411+
elif element == "foldLog":
412+
response = ExtractedInformation.GetFoldLog(root, **kwargs)
413+
elif element == "foliationLog":
414+
response = ExtractedInformation.GetFoliationLog(root, **kwargs)
415+
elif element == "discontinuityLog":
416+
response = ExtractedInformation.GetDiscontinuityLog(root, **kwargs)
417+
elif element == "drillholeLog":
418+
response = ExtractedInformation.GetDrillholeLog(root, **kwargs)
419+
elif element == "dataCollectionConfig":
420+
response = DataCollection.GetConfiguration(root, **kwargs)
421+
elif element == "dataCollectionSources":
422+
response = DataCollection.GetSources(root, **kwargs)
423+
elif element == "dataCollectionRawSourceData":
424+
response = DataCollection.GetRawSourceData(root, **kwargs)
425+
elif element == "eventRelationships":
426+
response = ExtractedInformation.GetEventRelationships(root, **kwargs)
427+
elif element == "structuralModelsConfig":
428+
response = StructuralModels.GetConfiguration(root, **kwargs)
429+
else:
430+
errStr = "(ERROR) Unknown element for Get function '" + element + "'"
431+
print(errStr)
432+
response = {"errorFlag": True, "errorString": errStr}
433+
finally:
434+
print(f"Closing file: {filename}",file=sys.stderr)
435+
root.close()
436+
print(f"{filename} closed successfully",file=sys.stderr)
424437
return response
425438

426439

@@ -799,4 +812,4 @@ def ConvertToDataFrame(data, loopCompoundType):
799812
columns = list(loopCompoundType.names)
800813
df = pandas.DataFrame.from_records(data, columns=columns)
801814
df = df.applymap(lambda x: x.decode() if isinstance(x, bytes) else x)
802-
return df
815+
return df

extents.csv

Lines changed: 0 additions & 2 deletions
This file was deleted.

stratigraphicObs.csv

Lines changed: 0 additions & 4 deletions
This file was deleted.

test.loop3d

-28 KB
Binary file not shown.

0 commit comments

Comments
 (0)