def test_substitute_wo_including(self):
     """ Test a simple substitution without include
     """
     fname = os.path.join(self.dir_testdata, 'substitute_wo_including.yaml')
     with open(fname, 'r') as f:
         data = load(f)
         self.assertEqual(data['mesh']['work_dir'], 'dir1')
 def test_multiple_substitution_in_one_item(self):
     """ Test multiple substitution in one item
     """
     fname = os.path.join(self.dir_testdata,
                          'substitute_multiple_times_in_one_item.yaml')
     with open(fname, 'r') as f:
         data = load(f)
         self.assertEqual(data['mesh']['work_dir'], 'dir1/file1')
 def read(self, fpath, **kwargs):
     with open(fpath, 'r') as f:
         data = schism_yaml.load(f)['linestrings']
         linestrings = []
         for row in data:
             linestrings.append(LineString(coordinates=row['coordinates'],
                                           prop=dict([(k, row[k]) for k in row if k != 'coordinates'])))
         return linestrings
 def test_substitute_multiple_including(self):
     """ Test substitution in multiple including
     """
     fname = os.path.join(self.dir_testdata, 'substitute_w_including.yaml')
     with open(fname, 'r') as f:
         data = load(f)
         self.assertEqual(data['mesh']['work_dir'], 'dir1')
     fname = os.path.join(self.dir_testdata,
                          'substitute_w_including_typo.yaml')
     with open(fname, 'r') as f:
         self.assertRaises(ValueError, load, f)
Example #5
0
def grid_opt_with_args(args):
    """ Optimize grid with arguments parsed by argparse
    """
    with open(args.optparam) as f:
        opt_param = schism_yaml.load(f)
    mesh = read_mesh(args.filename, nodestring_option='land')
    with open(args.demfile, 'r') as f:
        demfiles = schism_yaml.load(f)
        # dir = os.path.dirname(args.demfile)
        # demfiles_full = [os.path.join(dir, fname) for fname in demfiles]
    logger = init_logger()
    kwargs = {
        'mesh': mesh,
        'demfiles': demfiles,  # demfiles_full,
        'na_fill': args.na_fill,
        'logger': logger
    }
    optimizer = GridOptimizer(**kwargs)
    optimized = optimizer.optimize(opt_param)
    fpath_output = args.optfile
    write_mesh(mesh, fpath_output, node_attr=-optimized)
Example #6
0
def prepare_schism(args, use_logging=True):
    if use_logging is True:
        setup_logger()
        logger = logging.getLogger('SCHISM')
    else:
        logger = logging.getLogger('')
    logger.info("Start pre-processing SCHISM inputs...")
    in_fname = args.main_inputfile

    if not os.path.exists(in_fname):
        logger.error("The main input file, %s, is not found", in_fname)
        raise ValueError("Main input file not found")
    with open(in_fname, 'r') as f:
        inputs = schism_yaml.load(f)
    keys_top_level = ["mesh", "gr3",
                      "prop", "hydraulics",
                      "sources_sinks", "flow_outputs"] \
        + schism_yaml.include_keywords
    logger.info("Processing the top level...")
    check_and_suggest(list(inputs.keys()), keys_top_level, logger)

    out_fname = os.path.splitext(in_fname)[0] \
        + '_echo' + os.path.splitext(in_fname)[1]
    with open(out_fname, 'w') as f:
        f.write(schism_yaml.safe_dump(inputs))

    # Mesh section
    if item_exist(inputs, 'mesh'):
        logger.info("Processing mesh section...")
        mesh_items = inputs['mesh']
        keys_mesh_section = ["mesh_inputfile", "dem_list",
                             "open_boundaries",
                             "depth_optimization",
                             "gr3_outputfile", "ll_outputfile"] \
            + schism_yaml.include_keywords
        check_and_suggest(list(mesh_items.keys()), keys_mesh_section)
        if item_exist(inputs['mesh'], 'mesh_inputfile'):
            # Read the grid file to be processed
            mesh_input_fpath = \
                os.path.expanduser(mesh_items['mesh_inputfile'])
            s = create_schism_setup(mesh_input_fpath, logger)
            update_spatial_inputs(s, inputs, logger)
        else:
            raise ValueError("No mesh input file in the mesh section.")
    else:
        raise ValueError("No mesh section in the main input.")
    logger.info("Done.")