# Utility script to:
# 1) Delete alt directories and .dss output files from the /runs/ directory.
# 2) delete simulations that are not in the "wy ResSimOnly" or "FRA_*" analysis periods
# This is a good thing to do before packaging up your watershed to send to someone else.  This does not
# descriminate between alternatives, so use it carefully.  It is also a useful tool for cleaning up
# watersheds before recreating simulations with the ~WATSimulationCreator script.
# To use, double click and let run.  If you want to use the output, rename the $WATERSHED.sty.fraOnly 
# study file to $WATERSHED.sty.  All this does is modify the .sty file and create the .fraOnly version.
# This script DOES NOT delete the .simulation and .container files in  /wat/sims/ associated, so you 
# may need to manually delete them.
#
# - EAH 3/8/2016 (with help from KAR and KBD)
#
#Adapted for Willamette in August 2022
#

import os
import sys
import shutil
from glob import glob

'''
alt_directories = []
standaloneComputes = []
# remove alt directories from runs directory.
# FRA directory
fraDirs = glob(".\\runs\\*\\FRA_50year\\")
# Deterministic historical events
histEvents = glob(".\\runs\\*\\wy_*\\")
# Deterministic synthetic events
synthEvents = glob(".\\runs\\*\\*\\*.dss")
alt_directories = fraDirs + histEvents + synthEvents + standaloneComputes
print("Removing runs .dss files.")
for alt in set([r.split("\\")[2] for r in alt_directories]):
    print("\tDeleting %s results" % alt)
    shutil.rmtree(".\\runs\\%s\\" % alt)
'''
# ResSim standalone computes
standaloneComputes = glob(".\\rss\\*\\simulation.dss")
for f in standaloneComputes:
     print("\tDeleting %s" % f)
     os.remove(f)
# hotStart files
rssHotstarts = glob(".\\rss\\hotstarts\\*.dss") + \
               glob(".\\rss\\hotstarts\\*.nhs") + \
               glob(".\\rss\\hotstarts\\*.nhs.bak")
runHotstarts = glob(".\\runs\\*\\*\\hotstarts\\*.*")
for f in rssHotstarts + runHotstarts:
    print("\tDeleting %s" % f)
    os.remove(f)
    
'''
#print("Creating new .sty file.")
# Remove unused simulations
styFile = glob("./*.sty")
if len(styFile) == 0:
  sys.exit()
styFile = styFile[0]
newStyFile = "%s.baseSimsOnly" % styFile
with open(styFile, 'r') as styFile:
  with open(newStyFile, 'w') as newStyFile:
    textBlock = []
    writeBlock = True
    for L in styFile.readlines():
      textBlock.append(L)
      if ((".container" in L) or (".simulation" in L)) and not (("FRA" in L) or ("ResSimOnly" in L)):
        writeBlock = False
      if L.strip() == "ManagerEnd":
        # end of block
        if writeBlock:
          newStyFile.write("".join(textBlock))
        # reset tracking variables
        writeBlock = True
        textBlock = []
    # Flush last bit of textBlock to file if no more managerEnds encountered.
    newStyFile.write("".join(textBlock))  

# rename base only file
i = raw_input("Do you want to remove all WY specific runs from your .sty file?")
baseOnlyFile = glob(".\\*.sty.baseSimsOnly")
if i.upper()[0] == "Y":
    if len(baseOnlyFile) == 0:
        print("unable to find .sty.baseSimsOnly file.")
    else:
        # rename base sims only file
        shutil.move(baseOnlyFile[0], baseOnlyFile[0][:-len(".baseSimsOnly")])
        print("moved .sty.baseSimsOnly file")
else:
    print("Did not rename new .sty file.")
    os.remove(baseOnlyFile[0])
'''
wait = raw_input("done...")
