import os
import sys
import numpy as np

def get_volume(filename):
    with open(filename, 'r') as f:
        data = f.readlines()
    v = float(data[5].split()[-4])
    return v

study_id = ['STF_16', 'STF_17', 'STF_18','STF_19', 'STF_20',  'MR_250250', 'MR_293293', 'MR_119119', 'MR_054054', 'MR_104104', 'MR_236236', 'MR_269269','MR_087087','MR_091091', 'MR_124124', 'MR_126126', 'STF_01', 'STF_02', 'STF_08', 'STF_09', 'STF_13','MR_042042', 'STF_10', 'STF_11', 'MR_262262', 'STF_06', 'STF_12', 'MR_160160']
ds = [23, 16, 22, 22, 17, 24, 24, 22, 16, 25, 25, 24, 20, 21, 18, 25, 23, 14, 18, 19, 21, 18, 21, 18, 23, 20, 23, 14]
tot = [30, 25, 30, 30, 20, 30, 30, 30, 20, 30, 30, 30, 30, 25, 25, 25, 30, 20, 25, 25, 30, 25, 30, 25, 30, 30, 30, 20]
curdir = os.getcwd()

for i in range(0, len(ds)):
    v = []
    dir = '/hpc/zwan145/Heart_Failure_Project_STF_Workspace/ParameterEstimation/Studies/'+study_id[i]+'/LVMechanics'+study_id[i]+'/PassiveMechanics/OptimisedCavityVolume/'
    os.chdir(dir)
    v.append(get_volume('../DSCavity.opelem'))
    for j in range(ds[i]+1, tot[i]+1):
        v.append(get_volume('LVCavity_'+str(j)+'.opelem'))
    v.append(get_volume('LVCavity_1.opelem'))
    np.savetxt(curdir +'/'+ study_id[i]+'_volumes.txt', v)

 
