def clean_datadir( exp, run, keep_files=['input.nml', 'diag_table', 'field_table', 'git_hash_used.txt']): """Remove the `run` directory from output data, retaining only small configuration files.""" outdir = exp.get_outputdir(run) for file in keep_files: filepath = P(outdir, 'run', file) if os.path.isfile(filepath): sh.cp(filepath, P(outdir, file)) exp.log.info('Copied %s to %s' % (file, outdir)) sh.rm('-r', P(outdir, 'run')) exp.log.info('Deleted %s directory' % P(outdir, 'run'))
def interpolate_output(infile, outfile, all_fields=True, var_names=[], p_levs="input"): """Interpolate data from sigma to pressure levels. Includes option to remove original file. This is a very thin wrapper around the plevel.sh script found in `postprocessing/plevel_interpolation/scripts/plevel.sh`. Read the documentation in that script for more information. The interpolator must also be compiled before use. See `postprocessing/plevel_interpolation/README` for instructions. infile: The path of a netcdf file to interpolate over. outfile: The path to save the output to. all_fields: if True, interpolate all fields. var_names: a list of fields to interpolate. This is needed in addition to `all_fields=True` if you wish to recalculate e.g. `slp` or `height`. p_levs: The list of pressure values, in pascals, to interpolate onto. Can be: * A list of integer pascal values * "input": Interpolate onto the pfull values in the input file * "even": Interpolate onto evenly spaced in Pa levels. Outputs to outfile. """ interpolator = sh.Command( P(GFDL_BASE, 'postprocessing', 'plevel_interpolation', 'scripts', 'plevel.sh')) # Select from pre-chosen pressure levels, or input new ones in hPa in the format below. if isinstance(p_levs, str): if p_levs.upper() == "INPUT": with xr.open_dataset(infile, decode_times=False) as dat: levels = dat.pfull.data * 100 elif p_levs.upper() == "EVEN": #plev = ' -p "100000 95000 90000 85000 80000 75000 70000 65000 60000 55000 50000 45000 40000 35000 30000 25000 20000 15000 10000 5000" ' levels = [ 100000, 95000, 90000, 85000, 80000, 75000, 70000, 65000, 60000, 55000, 50000, 45000, 40000, 35000, 30000, 25000, 20000, 15000, 10000, 5000 ] else: raise ValueError("Unknown p_levs type '{}'".format(p_levs)) else: levels = p_levs plev = " ".join("{:.0f}".format(x) for x in reversed(sorted(levels))) if all_fields: interpolator = interpolator.bake('-a') var_names = ' '.join(var_names) interpolator('-i', infile, '-o', outfile, '-p', plev, var_names)
def delete_all_restarts(exp, exceptions=None): """Remove the restart files for a given experiment except those given. e.g. remove_restarts(exp, [3,6,9,12])""" if exceptions: exceptions = [exp.restartfmt % i for i in exceptions] all_restarts = os.listdir(exp.restartdir) restarts_to_remove = [ file for file in all_restarts if file not in exceptions ] for file in restarts_to_remove: sh.rm(P(exp.restartdir, file)) exp.log.info('Deleted restart file %s' % file)
def keep_only_certain_restart_files(exp, max_num_files, interval=12): try: # sh.ls(sh.glob(P(exp.workdir,'restarts','res_*.cpio'))) #TODO get max_num_files calculated in line, rather than a variable to pass. #First defines a list of ALL the restart file numbers files_to_remove = range(0, max_num_files) #Then defines a list of the ones we want to KEEP files_to_keep = range(0, max_num_files, interval) #Then we remove the files we want to keep from the list of all files, giving a list of those we wish to remove for x in files_to_keep: files_to_remove.remove(x) #Then we remove them. for entry in files_to_remove: sh.rm(P(exp.workdir, 'restarts', 'res_' + str(entry) + '.cpio')) except sh.ErrorReturnCode_1: log.warning( 'Tried to remove some restart files, but the last one doesnt exist' )