def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('input_files', nargs='+', help="""
                        The input data files in NetCDF format. These files can
                        be given in any order. They MUST appear before any other
                        arguments/options.""")
    parser.add_argument('output_file', default='ocean_monthly.nc', help="""
                        The name of the output file.""")
    parser.add_argument('--overall_mean_file', default=None, help="""
                        Calculate the overall mean and save with the given 
                        file name. This is the same as executing ncra
                        <output_file> <overall_mean_file>. This command
                        uses a lot less memory than ncra - it loads one
                        variable into memory at a time.""")
    parser.add_argument('--vars', default=['temp', 'dzt'], nargs='+',
                        help='A list of the variables to average.')

    args = parser.parse_args()

    # Create the output file by copying accross variables from one of
    # the inputs.
    other_vars = ['geolat_t', 'geolat_c', 'geolon_t', 'geolon_c', 'time_bounds']
    create_output_file(args.input_files[0], other_vars + args.vars, args.output_file)
    calc_monthly_mean(args.input_files, args.vars, args.output_file)
    update_file_history(args.output_file, ' '.join(sys.argv))

    if args.overall_mean_file:
        create_output_file(args.input_files[0], other_vars + args.vars,
                           args.overall_mean_file)
        calc_overall_mean([args.output_file], args.vars, args.overall_mean_file) 
        update_file_history(args.overall_mean_file, ' '.join(sys.argv))

    return 0
Exemple #2
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('input_files', nargs='+',
                        help="The input data file in NetCDF format.")
    parser.add_argument('--preproc_dir', default='./',
                        help='Directory where preprocessed files are placed.')
    parser.add_argument('--output_file', default='amoc.png',
                        help="""The name of the AMOC output file.""")
    parser.add_argument('--global_output_file', default=None,
                        help="""
                        The name of the output file for the global moc.
                        The global MOC is only calculated if this is set.
                        """)
    parser.add_argument('--output_atlantic_mask', action='store_true',
                        default=False,
                        help="""
                        Output the mask file used to define the Atlantic basin.
                        """)
    args = parser.parse_args()

    with nc.Dataset(args.input_files[0]) as f:
        lons = f.variables['geolon_t'][:]
        lats = f.variables['geolat_t'][:]
        times = f.variables['time'][:]

    if len(args.input_files) == 1 and len(times) == 1:
        mean_file = args.input_files[0]
    else:
        # Calculate a mean if one doesn't already exist.
        # Figure out output filename based on input files
        files, date_str, runtime = get_last_x_years(args.input_files, -1)
        mean_file = os.path.join(args.preproc_dir,
                                'ocean_mean_{}.nc'.format(date_str))

    if not os.path.exists(mean_file):
        vars = ['ty_trans', 'dzt', 'geolon_t', 'geolat_t']
        create_output_file(args.input_files[0], vars, mean_file + '.tmp')
        calc_overall_mean(args.input_files, ['ty_trans', 'dzt'], mean_file + '.tmp')
        shutil.move(mean_file + '.tmp', mean_file)

    with nc.Dataset(mean_file) as f:
        ty_trans = f.variables['ty_trans'][0, :, :, :]
        dzt = f.variables['dzt'][0, :, :, :]

    plot_atlantic_moc(ty_trans, dzt, lons, lats, args.output_file,
                        args.output_atlantic_mask, date_str)
    if args.global_output_file is not None:
        plot_global_moc(ty_trans, dzt, lons, lats,
                            args.global_output_file, date_str)

    return 0
Exemple #3
0
def mom_monthly_mean(input_files, output_file, vars, overall_mean_file=None, force=False):

    if os.path.exists(output_file) and not force:
        return 0

    # Create the output file by copying accross variables from one of
    # the inputs.
    other_vars = ["geolat_t", "geolat_c", "geolon_t", "geolon_c", "time_bounds"]
    create_output_file(input_files[0], other_vars + vars, output_file)
    calc_monthly_mean(input_files, vars, output_file)

    if overall_mean_file:
        create_output_file(input_files[0], other_vars + vars, overall_mean_file)
        calc_overall_mean([output_file], vars, overall_mean_file)

    return 0
Exemple #4
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('input_files', nargs='+', help="""
                        The input data files in NetCDF format. These files can
                        be given in any order. They MUST appear before any other
                        arguments/options.""")
    parser.add_argument('output_file', help="""The name of the output file.""")
    parser.add_argument('--vars', default=[], nargs='+',
                        help='A list of the variables to average.')
    parser.add_argument('--copy_vars', default=[], nargs='+',
                        help="""A list of the variables to copy across but not
                                included in the averaging.""")

    args = parser.parse_args()

    create_output_file(args.input_files[0], args.vars + args.copy_vars,
                       args.output_file)
    calc_overall_mean(args.input_files, args.vars, args.output_file)

    return 0