From e7b96c5dab05fba082e37941eb7193d020f73986 Mon Sep 17 00:00:00 2001
From: MattB <29564171+GeoMattB@users.noreply.github.com>
Date: Sun, 24 Mar 2024 10:40:37 +1100
Subject: [PATCH] bug fixes and minor updates

---
 badlands_doe_toolset/csv_to_badlands.py     | 8 ++++----
 badlands_doe_toolset/postproc_utils.py      | 5 ++++-
 badlands_doe_toolset/postproc_xmf_update.py | 8 ++++----
 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/badlands_doe_toolset/csv_to_badlands.py b/badlands_doe_toolset/csv_to_badlands.py
index e76c949..8cec2c0 100644
--- a/badlands_doe_toolset/csv_to_badlands.py
+++ b/badlands_doe_toolset/csv_to_badlands.py
@@ -96,7 +96,7 @@ def badlands_encode(row, *args):
     if 'Tectonic' in row.keys():
         try:
             tectonic_structure = tectonic_dict[row['Tectonic']]
-        except(KeyError): # this is the error returned if the specified xml file is not found.
+        except(UnboundLocalError): # this is the error returned if the specified xml file is not found.
             print('No Tectonic structure files found')
     elif 'dstart' in row.keys():
         tectonic_structure=f"""    <tectonic>
@@ -119,7 +119,7 @@ def badlands_encode(row, *args):
     if '.xml' in str(row['Precipitation']):
         try:
             precipitation_structure = precipitation_dict[row['Precipitation']]
-        except(KeyError): # this is the error returned if the specified xml file is not found.
+        except(UnboundLocalError): # this is the error returned if the specified xml file is not found.
             print('No precipitation structure files found using single event climate values')
     else:
         precipitation_structure = f"""<!-- Precipitation structure -->
@@ -281,7 +281,7 @@ def badlands_encode(row, *args):
     if '.xml' in str(row['Erodibility']):
         try:
             erodibility_structure = erodability_dict[row['Erodibility']]
-        except(KeyError): # this is the error returned if the specified xml file is not found.
+        except(UnboundLocalError): # this is the error returned if the specified xml file is not found.
             print('No Erodibility structure files found using single Erodibility values')
     else:
         erodability_structure = f"""<erodibility>{row['Erodibility']}</erodibility>"""         
@@ -365,7 +365,7 @@ def badlands_encode(row, *args):
     if 'Carbonate' in row.keys():
         try:
             carbonate_structure = carbonate_dict[row['Carbonate']]
-        except(KeyError): # this is the error returned if the specified xml file is not found.
+        except(UnboundLocalError): # this is the error returned if the specified xml file is not found.
             print('No carbonate structure files found')
     elif 'tcarb' in row.keys():
         carbonate_structure=f"""<carb>
diff --git a/badlands_doe_toolset/postproc_utils.py b/badlands_doe_toolset/postproc_utils.py
index b709dfd..aa74192 100644
--- a/badlands_doe_toolset/postproc_utils.py
+++ b/badlands_doe_toolset/postproc_utils.py
@@ -178,7 +178,10 @@ def tin_write_instdiff(modelfile):
         tin_N1=TINfile()
         tin_N.loadTIN(modelh5dir+'/tin.time'+str(i)+'.hdf5')
         #return(tin_N)
-        tin_N1.loadTIN(modelh5dir+'/tin.time'+str(i+1)+'.hdf5')
+        if i != maxSteps:
+            tin_N1.loadTIN(modelh5dir+'/tin.time'+str(i+1)+'.hdf5')
+        elif i == maxSteps:
+            tin_N1.loadTIN(modelh5dir+'/tin.time'+str(i)+'.hdf5')
         
         #this is a bit not-that-useful, it just adds an attribute to the first (zero) layer to keep outputs consistent / tidy.
         if i==0:
diff --git a/badlands_doe_toolset/postproc_xmf_update.py b/badlands_doe_toolset/postproc_xmf_update.py
index 3b9d8c2..dec58ff 100644
--- a/badlands_doe_toolset/postproc_xmf_update.py
+++ b/badlands_doe_toolset/postproc_xmf_update.py
@@ -27,7 +27,7 @@ def flowfile_xmf(modelfile):
         xmf_file=str(modelXmfdir)+'/flow.time'+str(i)+'.xmf'
         print ('output will be '+xmf_file)
         f=h5py.File(hdf_file, 'r')
-        tmstep=float(i*model.tDisplay)
+        tmstep=float(model.tStart+(i*model.tDisplay))
         conshp=len(f['connect'])
         geom=len(f['coords'])
 #        row=pd.DataFrame()
@@ -88,7 +88,7 @@ def tinfile_xmf(modelfile):
         xmf_file=str(modelXmfdir)+'/tin.time'+str(i)+'.xmf'
         print ('output will be '+xmf_file)
         f=h5py.File(hdf_file, 'r')
-        tmstep=float(i*model.tDisplay)
+        tmstep=float(model.tStart+(i*model.tDisplay))
         cells=len(f['cells'])
         geom=len(f['coords'])
         row=pd.DataFrame()
@@ -145,7 +145,7 @@ def tinfile_xmf(modelfile):
  
 # XMF for sed time strat files
 # single xmf build write the xmf to view in paraview
-def sedfile_xmf(hdf_file,modelinc,tDisplay=100000)  
+def sedfile_xmf(hdf_file,modelinc,tDisplay=100000):  
         print ('load '+hdf_file)
         strat=ppu.Stratadata()
         strat.loadStrat(hdf_file)
@@ -186,7 +186,7 @@ def sedfile_xmf(hdf_file,modelinc,tDisplay=100000)
  </Domain>
 </Xdmf>
 """)
-    texfile.close()
+        texfile.close()
 
 
 #write all of the xmf files and then the XDMF for a time series.