def __is_sim_dir__(path='.'): """ Checks if a path is pointing at a pencil code simulation.""" if __isdir__(__join__(path, 'data')) and __exists__(__join__(path, 'run.in')) \ and __exists__(__join__(path, 'start.in')) and __exists__(__join__(path, 'src/cparam.local')) \ and __exists__(__join__(path, 'src/Makefile.local')): return True return False
def is_sim_dir(path='.'): """Checks if a path is pointing at a pencil code simulation directory. Therefor, it checks the existance of start.in, run.in, src/cparam.local and src/Makefile.local""" from os.path import isdir as __isdir__ from os.path import join as __join__ from os.path import exists as __exists__ if __exists__(__join__(path, 'run.in')) and __exists__(__join__(path, 'start.in')) and __exists__(__join__(path, 'src/cparam.local')) and __exists__(__join__(path, 'src/Makefile.local')): return True return False
def pkl_load(name, folder=False, sim=False, ascii=True): """This scripts loads an pkl-file. It automatically checks known folders if no folder is specified. Args: name: Name of pkl-file (<name>.pkl) folder: Folder containing pkl file sim: Simulation for checking automatically folders for ascii: Switch to False if pkl file was NOT written in ascii Example: to read ".pc/sim.pkl" use: pkl_load('sim', '.pc') or simply pkl_load('sim'), since pkl_load checks for following folders automatically: '.pc', 'data/.pc' """ import pickle from os.path import join as __join__ from os.path import exists as __exists__ if (not name.endswith('.pkl')): name = name + '.pkl' # add .pkl to name if not already ending with # if folder is not defined try to find the pkl-file at typical places sim_path = '.' if sim: sim_path = sim.path if not folder: if __exists__(__join__(sim_path, '.pc', name)): folder = __join__(sim_path, '.pc') print '~ Found ' + name + ' in ' + folder elif __exists__(__join__(sim_path, 'data/.pc', name)): folder = __join__(sim_path, 'data/.pc') print '~ Found ' + name + ' in ' + folder elif __exists__(__join__(sim_path, '.', name)): folder = __join__(sim_path, '.') print '~ Found ' + name + ' in ' + folder else: print '~ Couldnt find file ' + name return False # open file file = __join__(folder, name) try: # check on existance if not __exists__(file) or not __exists__(__join__(sim_path, file)): print '!! ERROR: pkl_load couldnt load ' + file return False try: # open file and return it with open(file, 'r') as f: return pickle.load(f) except: with open(__join__(sim_path, file), 'r') as f: return pickle.load(f) except: # if anything goes wrong print '!! ERROR: Something went wrong while importing pkl-file!' return False
def dill_load(name, folder=False, sim=False): """This scripts loads an dill-file. It automatically checks known folders if no folder is specified. Args: name: Name of dill-file (<name>.dill) folder: Folder containing dill file sim: Simulation for checking automatically folders for Example: to read ".pc/sim.dill" use: dill_load('sim', '.pc') or simply dill_load('sim'), since dill_load checks for following folders automatically: '.pc', 'data/.pc' """ import pencilnew.backpack.dill as dill from os.path import join as __join__ from os.path import exists as __exists__ if (not name.endswith('.dill')): name = name + '.dill' # add .dill to name if not already ending with # if folder is not defined try to find the dill-file at typical places sim_path = '.' if sim: sim_path = sim.path if not folder: if __exists__(__join__(sim_path, '.pc', name)): folder = __join__(sim_path, '.pc') print('~ Found ' + name + ' in ' + folder) elif __exists__(__join__(sim_path, 'data/.pc', name)): folder = __join__(sim_path, 'data/.pc') print('~ Found ' + name + ' in ' + folder) elif __exists__(__join__(sim_path, '.', name)): folder = __join__(sim_path, '.') print('~ Found ' + name + ' in ' + folder) else: print('~ Couldnt find file ' + name) return False # open file file = __join__(folder, name) try: # check on existance if not __exists__(file) or not __exists__(__join__(sim_path, file)): print('!! ERROR: dill_load couldnt load ' + file) return False try: # open file and return it with open(file, 'r') as f: return dill.load(f) except: with open(__join__(sim_path, file), 'r') as f: return dill.load(f) except: # if anything goes wrong print('!! ERROR: Something went wrong while importing dill-file!') return False
def get_sim(path='.'): """Returns simulation object from 'path/.pc/' if already existing.""" if __exists__(__join__(path, '.pc/sim.pkl')): return io.load('sim', folder='.pc') else: from pencilnew import __is_sim_dir__ if __is_sim_dir__(path): return sim.Simulation(path) else: print('?? WARNING: No simulation found in ' + path + '>. Try get_sims maybe?') return False
def __init__(self, path='.', hidden=False, quiet=False): import os from os.path import join as __join__ from os.path import exists as __exists__ from os.path import split as __split__ #from pen.intern.hash_sim import hash_sim import pencilnew # find out name and store it self.name = __split__(path)[-1] if self.name == '.' or self.name == '': self.name = __split__(os.getcwd())[-1] # store paths self.path = os.path.abspath(path) self.dir = self.path if (not quiet): print '# Creating Simulation object for ' + self.path self.data_dir = __join__(self.path, 'data') self.pc_dir = __join__(self.path, '.pc') self.pc_data_dir = __join__(self.path, 'data', '.pc') # generate status hash identification #self.status_hash = hash_sim(path) # hidden is default False self.hidden = hidden # read params into SIM object self.param = {} if __exists__(__join__(self.data_dir, 'param.nml')): param = pencilnew.read.Param().read(quiet=True, data_dir=self.data_dir) for key in dir(param): if key.startswith('__'): continue self.param[key] = getattr(param, key) else: print '?? WARNING: Couldnt find param.nml in simulation ' + self.name + '! Simulation is now hidden from calculations!' self.param['UNSTARTED'] = True self.hidden = True try: self.grid = pencilnew.read.grid(data_dir=self.data_dir, trim=True, quiet=True) self.ghost_grid = pencilnew.read.grid(data_dir=self.data_dir, trim=False, quiet=True) except: self.grid = None self.ghost_grid = None