From 0f5d528ef6c265136ffdee854c530e2730324b60 Mon Sep 17 00:00:00 2001 From: caitlyn-wilhelm Date: Mon, 9 Aug 2021 14:20:46 -0700 Subject: [PATCH 1/4] added module import for multiplanet --- multiplanet/__init__.py | 2 ++ multiplanet/multiplanet.py | 23 ++++++++++++++--------- multiplanet/multiplanet_module.py | 19 +++++++++++++++++++ setup.py | 2 +- 4 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 multiplanet/multiplanet_module.py diff --git a/multiplanet/__init__.py b/multiplanet/__init__.py index 73c8520..f958d16 100644 --- a/multiplanet/__init__.py +++ b/multiplanet/__init__.py @@ -14,3 +14,5 @@ __copyright__ = "Copyright 2020 Caitlyn Wilhelm" from .multiplanet import * + +from .multiplanet_module import * diff --git a/multiplanet/multiplanet.py b/multiplanet/multiplanet.py index 1b87d3a..bc32524 100644 --- a/multiplanet/multiplanet.py +++ b/multiplanet/multiplanet.py @@ -117,20 +117,25 @@ def CreateCP(checkpoint_file, input_file, quiet, sims): def ReCreateCP(checkpoint_file, input_file, quiet, sims): if quiet == False: print("WARNING: multi-planet checkpoint file already exists!") - print("Checking if checkpoint file is corrupt...") datalist = [] - - with open(checkpoint_file, "r") as f: - for newline in f: + with open(checkpoint_file, "r") as re: + for newline in re: datalist.append(newline.strip().split()) - for l in datalist: - if l[1] == "0": - l[1] = "-1" - with open(checkpoint_file, "w") as f: + for l in datalist: + if l[1] == "0": + l[1] = "-1" + if datalist[-1] != ["THE","END"]: + lest = datalist[-2][0] + idx = sims.index(lest) + for f in range(idx+2,len(sims)): + datalist.append([sims[f],'-1']) + datalist.append(["THE","END"]) + + with open(checkpoint_file, "w") as wr: for newline in datalist: - f.writelines(" ".join(newline) + "\n") + wr.writelines(" ".join(newline) + "\n") ## parallel worker to run vplanet ## diff --git a/multiplanet/multiplanet_module.py b/multiplanet/multiplanet_module.py new file mode 100644 index 0000000..159664a --- /dev/null +++ b/multiplanet/multiplanet_module.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +import os +import multiprocessing as mp +import sys +import subprocess as sub +import h5py +import numpy as np +import csv +from scipy import stats +from multiplanet import parallel_run_planet + +""" +Code for Multi-planet Module +""" + + +def RunMultiplanet(InputFile,cores, quiet=False, bigplanet=False,email=None): + parallel_run_planet(InputFile,cores,bigplanet,email) diff --git a/setup.py b/setup.py index c2fc9b3..d6db862 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ ], entry_points={ "console_scripts": [ - "multi-planet = multiplanet.multiplanet:Arguments", + "multiplanet = multiplanet.multiplanet:Arguments", "mpstatus = multiplanet.mpstatus:Arguments", ], }, From 61a24d1d949f4123e2338d636cb8411474b7e68e Mon Sep 17 00:00:00 2001 From: caitlyn-wilhelm Date: Tue, 14 Sep 2021 12:37:49 -0700 Subject: [PATCH 2/4] fixed bigplanet issue --- multiplanet/multiplanet.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/multiplanet/multiplanet.py b/multiplanet/multiplanet.py index bc32524..f53941c 100644 --- a/multiplanet/multiplanet.py +++ b/multiplanet/multiplanet.py @@ -62,7 +62,8 @@ def parallel_run_planet(input_file, cores, quiet, bigplanet, email): w.join() if bigplanet == False: - sub.run(["rm", master_hdf5_file]) + if os.isfile(master_hdf5_file) == True: + sub.run(["rm", master_hdf5_file]) if email is not None: SendMail(email, folder_name) From 3f170f5b42b401a0daf17ab8f39f505f89071faa Mon Sep 17 00:00:00 2001 From: Caitlyn Wilhelm Date: Tue, 14 Sep 2021 14:25:25 -0700 Subject: [PATCH 3/4] helps to use the os package correctly --- multiplanet/multiplanet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multiplanet/multiplanet.py b/multiplanet/multiplanet.py index f53941c..4fb54af 100644 --- a/multiplanet/multiplanet.py +++ b/multiplanet/multiplanet.py @@ -62,7 +62,7 @@ def parallel_run_planet(input_file, cores, quiet, bigplanet, email): w.join() if bigplanet == False: - if os.isfile(master_hdf5_file) == True: + if os.path.isfile(master_hdf5_file) == True: sub.run(["rm", master_hdf5_file]) if email is not None: SendMail(email, folder_name) From 434d23be7b9b60290a8712eb03eedeaa198e7e1b Mon Sep 17 00:00:00 2001 From: caitlyn-wilhelm Date: Tue, 14 Sep 2021 16:52:18 -0700 Subject: [PATCH 4/4] fixed tests and added file with proper functions --- multiplanet/bp_get.py | 180 ++++++++++++++++++ multiplanet/multiplanet.py | 30 +-- .../test_multi-planet_checkpoint.py | 2 +- tests/MpStatus/test_mpstatus.py | 2 +- tests/Parallel/test_multi-planet_parallel.py | 2 +- tests/Serial/test_multi-planet_serial.py | 2 +- 6 files changed, 199 insertions(+), 19 deletions(-) create mode 100644 multiplanet/bp_get.py diff --git a/multiplanet/bp_get.py b/multiplanet/bp_get.py new file mode 100644 index 0000000..dda08bb --- /dev/null +++ b/multiplanet/bp_get.py @@ -0,0 +1,180 @@ +#!/usr/bin/env python + +import os +import subprocess as sub +import sys + + +def ReadFile(bplSplitFile,verbose = False): + + includelist = [] + excludelist = [] + bodylist = [] + bpl_file = None + + with open(bplSplitFile, 'r') as input: + #first thing we check is if both include and exclude are in the file bc that is bad + if 'saKeyInclude' and 'saKeyExclude' in input.read(): + print("ERROR: saKeyInclude and saKeyExclude are mutually exclusive") + exit() + + with open(bplSplitFile, 'r') as input: + #now we loop over the file and get the various inputs + content = [line.strip().split() for line in input.readlines()] + for line in content: + if line: + #we get the folder where the raw data is stored and have the default output file name set + if line[0] == 'sDestFolder': + folder_name = line[1] + outputFile = folder_name.split('/')[-1] + "_filtered.bpl" + if line[0] == 'sBigplanetFile': + bpl_file = line[1] + if line[0] == 'sOutputName': + outputFile = line[1] + if line[0] == 'sPrimaryFile': + primaryFile = line[1] + if line[0] == 'bUlysses': + thing = line[1] + if line[0] == "saBodyFiles": + bodylist = line[1:] + for i, value in enumerate(bodylist): + bodylist[i] = value.strip("[]") + if line[0] == 'saKeyInclude': + includelist = line[1:] + for i, value in enumerate(includelist): + includelist[i] = value.strip("[]") + if line[0] == 'saKeyExclude': + excludelist = line[1:] + for i, value in enumerate(excludelist): + excludelist[i] = value.strip("[]") + + if bpl_file == None and includelist == [] and excludelist == []: + print("No BPL Archive file or Include/Exclude List detected. This will create the BPL Archive File.") + print("WARNING: This may take some time...") + + + if verbose: + print("Folder Name:",folder_name) + if bpl_file != None and includelist != [] and excludelist != []: + print("BPL Archive File:",bpl_file) + print("Include List:",includelist) + print("Exclude List:",excludelist) + print("Output File:",outputFile) + print("Body Files:",bodylist) + print("Primary File:",primaryFile) + + + return folder_name,bpl_file,outputFile,bodylist,primaryFile,includelist,excludelist + + +def GetDir(vspace_file): + """ Give it input file and returns name of folder where simulations are located. """ + + infiles = [] + # gets the folder name with all the sims + with open(vspace_file, 'r') as vpl: + content = [line.strip().split() for line in vpl.readlines()] + for line in content: + if line: + if line[0] == 'destfolder': + folder_name = line[1] + + if line[0] == 'bodyfile' or line[0] == 'primaryfile': + infiles.append(line[1]) + if folder_name is None: + raise IOError("Name of destination folder not provided in file '%s'." + "Use syntax 'destfolder '"% vspace_file) + + + if os.path.isdir(folder_name) == False: + print("ERROR: Folder", folder_name, "does not exist in the current directory.") + exit() + + return folder_name, infiles + +def GetSims(folder_name): + """ Pass it folder name where simulations are and returns list of simulation folders. """ + #gets the list of sims + sims = sorted([f.path for f in os.scandir(os.path.abspath(folder_name)) if f.is_dir()]) + return sims + +def GetSNames(in_files,sims): + #get system and the body names + body_names = [] + + for file in in_files: + #gets path to infile + full_path = os.path.join(sims[0],file) + #if the infile is the vpl.in, then get the system name + if "vpl.in" in file: + with open(full_path, 'r') as vpl: + content = [line.strip().split() for line in vpl.readlines()] + for line in content: + if line: + if line[0] == 'sSystemName': + system_name = line[1] + else: + with open(full_path, 'r') as infile: + content = [line.strip().split() for line in infile.readlines()] + for line in content: + if line: + if line[0] == 'sName': + body_names.append(line[1]) + + return system_name,body_names + +def GetVplanetHelp(): + command = "vplanet -H | egrep -v '^$|^\+' | cut -f 2,4 -d '|' | egrep '^ \*\*|^ Cust|^ Type|^ Dim|^ Defa|^Output Parameters'" + py_ver = sys.version.split()[0] + if '3.6' in py_ver: + proc = sub.run(command, shell = True, universal_newlines = True, stdout=sub.PIPE,stderr=sub.PIPE) + else: + proc = sub.run(command, shell = True, text = True, stdout=sub.PIPE,stderr=sub.PIPE) + output = proc.stdout.splitlines() + + vplanet_dict = {} + + for count, line in enumerate(output): + if ((line.startswith(' **b') or line.startswith(' **d') or line.startswith(' **i') or line.startswith(' **s')) or line.startswith(' **sa') and len(line.split()) == 1): + + option = line.strip("** ") + #print("Option:",option) + vplanet_dict[option] = {} + num = count + 1 + while num != count: + if "Type" in output[num]: + tp = output[num].rpartition('|')[-1].strip() + vplanet_dict[option]['Type'] = tp + #print("Type:",tp) + num += 1 + + elif "Custom unit" in output[num]: + custom_unit = output[num].rpartition('|')[-1].strip() + vplanet_dict[option]['Custom Unit'] = custom_unit + #print("Custom Unit:",custom_unit) + num += 1 + + elif "Dimension(s)" in output[num]: + dim = output[num].rpartition('|')[-1].strip() + vplanet_dict[option]['Dimension'] = dim + #print("Dimension:",dim) + num += 1 + + elif "Default value" in output[num]: + default = output[num].rpartition('|')[-1].strip() + vplanet_dict[option]['Default Value'] = default + #print("Default Value",default) + #print() + + num += 1 + + elif "**" in output[num]: + num = count + + else: + num += 1 + + if "Output Parameters" in line: + break + + return vplanet_dict diff --git a/multiplanet/multiplanet.py b/multiplanet/multiplanet.py index 4fb54af..b007f5a 100644 --- a/multiplanet/multiplanet.py +++ b/multiplanet/multiplanet.py @@ -6,7 +6,7 @@ import argparse import h5py import numpy as np -from bigplanet import CreateHDF5Group, GetSNames, GetSims +from .bp_get import * # -------------------------------------------------------------------- @@ -213,19 +213,19 @@ def par_worker( break if quiet == False: print(folder, "completed") - if bigplanet == True: - with h5py.File(h5_file, "w") as Master: - group_name = folder.split("/")[-1] - if group_name not in Master: - CreateHDF5Group( - data, - system_name, - body_list, - log_file, - group_name, - in_files, - h5_file, - ) + # if bigplanet == True: + # with h5py.File(h5_file, "w") as Master: + # group_name = folder.split("/")[-1] + # if group_name not in Master: + # CreateHDF5Group( + # data, + # system_name, + # body_list, + # log_file, + # group_name, + # in_files, + # h5_file, + # ) else: for l in datalist: if l[0] == folder: @@ -265,7 +265,7 @@ def Arguments(): "-bp", "--bigplanet", action="store_true", - help="Runs bigplanet and creates the HDF5 files alongside running mutlt-planet", + help="Runs bigplanet and creates the Bigplanet Archive file alongside running multiplanet", ) parser.add_argument( "-m", diff --git a/tests/Checkpoint/test_multi-planet_checkpoint.py b/tests/Checkpoint/test_multi-planet_checkpoint.py index d15a84f..34195b4 100644 --- a/tests/Checkpoint/test_multi-planet_checkpoint.py +++ b/tests/Checkpoint/test_multi-planet_checkpoint.py @@ -23,7 +23,7 @@ def test_mp_checkpoint(): # Run multi-planet if not (path / ".MP_Checkpoint").exists(): - subprocess.check_output(["multi-planet", "vspace.in"], cwd=path) + subprocess.check_output(["multiplanet", "vspace.in"], cwd=path) #gets list of folders folders = sorted([f.path for f in os.scandir(path / "MP_Checkpoint") if f.is_dir()]) diff --git a/tests/MpStatus/test_mpstatus.py b/tests/MpStatus/test_mpstatus.py index 086c4f1..5b617ad 100644 --- a/tests/MpStatus/test_mpstatus.py +++ b/tests/MpStatus/test_mpstatus.py @@ -22,7 +22,7 @@ def test_mpstatus(): # Run multi-planet if not (path / ".MP_Status").exists(): - subprocess.check_output(["multi-planet", "vspace.in"], cwd=path) + subprocess.check_output(["multiplanet", "vspace.in"], cwd=path) subprocess.check_output(["mpstatus", "vspace.in"], cwd=path) #gets list of folders diff --git a/tests/Parallel/test_multi-planet_parallel.py b/tests/Parallel/test_multi-planet_parallel.py index 37b6350..4c247fd 100644 --- a/tests/Parallel/test_multi-planet_parallel.py +++ b/tests/Parallel/test_multi-planet_parallel.py @@ -22,7 +22,7 @@ def test_mp_parallel(): # Run multi-planet if not (path / ".MP_Parallel").exists(): - subprocess.check_output(["multi-planet", "vspace.in"], cwd=path) + subprocess.check_output(["multiplanet", "vspace.in"], cwd=path) folders = sorted([f.path for f in os.scandir(path / "MP_Parallel") if f.is_dir()]) diff --git a/tests/Serial/test_multi-planet_serial.py b/tests/Serial/test_multi-planet_serial.py index 506c99a..6498316 100644 --- a/tests/Serial/test_multi-planet_serial.py +++ b/tests/Serial/test_multi-planet_serial.py @@ -15,7 +15,7 @@ def test_mp_serial(): # Run multi-planet if not (path / ".MP_Serial").exists(): - subprocess.check_output(["multi-planet", "vspace.in", "-c", "1"], cwd=path) + subprocess.check_output(["multiplanet", "vspace.in", "-c", "1"], cwd=path) folders = sorted([f.path for f in os.scandir(path / "MP_Serial") if f.is_dir()])