Esempio n. 1
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.")
Esempio n. 2
0
def main():
    parser = create_arg_parser()
    args = parser.parse_args()
    monterey_fpath = args.monterey
    pt_reyes_fpath = args.pt_reyes
    hgrid_fpath = args.hgrid
    fpath_out = args.outfile
    #todo: hardwire 
    nnode = 83
    
    tbuf = days(16)              
    # convert start time string input to datetime       
    sdate = datetime(*map(int, re.split('[^\d]', args.stime)))

    if args.etime:
        # convert start time string input to datetime
        edate = datetime(*map(int, re.split('[^\d]', args.etime)))
        bufend = edate + tbuf
    else:
        edate = None
        bufend = None

   

    # UTM positions of Point Reyes, Monterey, SF
    pos_pr = np.array([502195.03, 4205445.47])
    pos_mt = np.array([599422.84, 4051630.37])
    pos_sf = np.array([547094.79, 4184499.42])

    var_subtidal = np.array([0.938, 0.905, 0.969])  # pr, mt, sf
    var_semi = np.array([0.554, 0.493, 0.580])

    # Assume 45 degree from north-west to south-east
    tangent = np.array([1, -1])
    tangent = tangent / np.linalg.norm(tangent)  # Normalize
    # Rotate 90 cw to get normal vec
    normal = np.array([tangent[1], -tangent[0]])
    print "tangent:", tangent
    print "normal:", normal

    mt_rel = pos_mt - pos_pr
    x_mt = np.dot(tangent, mt_rel)  # In pr-mt direction
    y_mt = np.dot(normal, mt_rel)  # Normal to x-direction to the 
    
    # Grid
    #todo: what is the difference between this and m = read_grid()??
    s = create_schism_setup(hgrid_fpath)
    
    ocean_boundary = s.mesh.boundaries[0]  # First one is ocean
 
    # Data
    print "Reading Point Reyes..."
    pt_reyes = read_ts(pt_reyes_fpath, start=sdate - tbuf, end=bufend, force_regular=True)
    pt_reyes = interpolate_ts_nan(pt_reyes)
    if np.any(np.isnan(pt_reyes.data)):
        print pt_reyes.times[np.isnan(pt_reyes.data)]
    ts_pr_subtidal, ts_pr_diurnal, ts_pr_semi, noise = separate_species(
        pt_reyes)
    del noise

    print "Reading Monterey..."
    monterey = read_ts(monterey_fpath, start=sdate - tbuf, end=bufend, force_regular=True)
    monterey = interpolate_ts_nan(monterey)
    if pt_reyes.interval != monterey.interval:
        raise ValueError(
            "Point Reyes and Monterey time step must be the same in gen_elev2D.py")

    ts_mt_subtidal, ts_mt_diurnal, ts_mt_semi, noise = separate_species(
        monterey)
    del noise

    dt = monterey.interval.seconds

    print "Done Reading"

    print "Interpolating Point Reyes"
    # interpolate_ts(ts_pr_subtidal.window(sdate,edate),step)
    ts_pr_subtidal = ts_pr_subtidal.window(sdate, edate)
    ts_pr_diurnal = ts_pr_diurnal.window(sdate, edate)  # interpolate_ts(,step)
    # interpolate_ts(ts_pr_semi.window(sdate,edate),step)
    ts_pr_semi = ts_pr_semi.window(sdate, edate)

    print "Interpolating Monterey"
    # interpolate_ts(ts_mt_subtidal.window(sdate,edate),step)
    ts_mt_subtidal = ts_mt_subtidal.window(sdate, edate)
    # interpolate_ts(ts_mt_diurnal.window(sdate,edate),step)
    ts_mt_diurnal = ts_mt_diurnal.window(sdate, edate)
    # interpolate_ts(ts_mt_semi.window(sdate,edate),step)
    ts_mt_semi = ts_mt_semi.window(sdate, edate)

    print "Creating writer"  # requires dt be known for netcdf
    if fpath_out.endswith("th"):
        thwriter = BinaryTHWriter(fpath_out,nnode,None)
    elif fpath_out.endswith("nc"):
        thwriter = NetCDFTHWriter(fpath_out,nnode,sdate,dt)
    else:
        raise ValueError("File extension for output not recognized in file: {}".format(fpath_out))    
    
    
    
    # Grid
    boundaries = s.mesh.nodes[ocean_boundary.nodes]
    pos_rel = boundaries[:, :2] - pos_pr

    # x, y in a new principal axes
    x = np.dot(pos_rel, tangent.reshape((2, -1)))
    y = np.dot(pos_rel, normal.reshape((2, -1)))
    theta_x = x / x_mt
    theta_x_comp = 1. - theta_x
    theta_y = y / y_mt
    theta_y_comp = 1. - theta_y

    var_y = (theta_y_comp * var_semi[0] + theta_y * var_semi[1])

    # adj_subtidal_mt = 0.08  # Adjustment in Monterey subtidal signal
    # scaling_diurnal_mt = 0.95 # Scaling of Monterey diurnal signal (for K1/Q1)
    # Used this up to v75
    adj_subtidal_mt = 0.  # Adjustment in Monterey subtidal signal
    scaling_diurnal_mt = 1.  # Scaling of Monterey diurnal signal (for K1/Q1)
    # New trial for LSC2 with v75
    adj_subtidal_mt = -0.07  # Adjustment in Monterey subtidal signal
    scaling_diurnal_mt = 0.95  # Scaling of Monterey diurnal signal (for K1/Q1)
    scaling_semidiurnal_mt = 1.03

    adj_subtidal_mt = -0.14  # Adjustment in Monterey subtidal signal
    scaling_diurnal_mt = 0.90  # Scaling of Monterey diurnal signal (for K1/Q1)
    scaling_semidiurnal_mt = 1.07

    adj_subtidal_mt = 0.10  # Adjustment in Monterey subtidal signal
    scaling_diurnal_mt = 0.90  # Scaling of Monterey diurnal signal (for K1/Q1)
    scaling_semidiurnal_mt = 1.03

    adj_subtidal_mt = 0.10  # Adjustment in Monterey subtidal signal
    scaling_diurnal_mt = 0.97  # Scaling of Monterey diurnal signal (for K1/Q1)
    # Scaling of Point Reyes diurnal signal (for K1/Q1)
    scaling_diurnal_pr = 0.97
    scaling_semidiurnal_mt = 1.025  # Scaling at Monterey semi-diurnal signal

    adj_subtidal_mt = 0.09  # Adjustment in Monterey subtidal signal
    scaling_diurnal_mt = 0.94  # Scaling of Monterey diurnal signal (for K1/Q1)
    # Scaling of Point Reyes diurnal signal (for K1/Q1)
    scaling_diurnal_pr = 0.94
    scaling_semidiurnal_mt = 1.0  # Scaling at Monterey semi-diurnal signal

    slr = 0.0  # Sea level rise

    if np.any(np.isnan(ts_pr_semi.data)):
        print ts_pr_semi.times[np.isnan(ts_pr_semi.data)]
        raise ValueError('')

#    temp=np.zeros((len(ts_pr_semi),nnode))
#    times[:]=[dt*i for i in xrange(len(ts_pr_semi))]   

    for i in xrange(len(ts_pr_semi)):
        t = float(dt * i)
        # semi-diurnal
        # Scaling
        pr = ts_pr_semi[i].value
        mt = ts_mt_semi[i].value * scaling_semidiurnal_mt

        if np.isnan(pr) or np.isnan(mt):
            print pr
            print mt
            raise ValueError("One of values is numpy.nan.")

        eta_pr_side = var_y / var_semi[0] * pr
        eta_mt_side = var_y / var_semi[1] * mt
        eta = eta_pr_side * theta_x_comp + eta_mt_side * theta_x

        # diurnal
        # Interpolate in x-direction only to get a better phase
        pr = ts_pr_diurnal[i].value * scaling_diurnal_pr
        mt = ts_mt_diurnal[i].value * scaling_diurnal_mt

        if np.isnan(pr) or np.isnan(mt):
            raise ValueError("One of values is numpy.nan.")

        eta += pr * theta_x_comp + mt * theta_x

        # Subtidal
        # No phase change in x-direction. Simply interpolate in
        # y-direction.
        pr = ts_pr_subtidal[i].value + slr
        mt = ts_mt_subtidal[i].value + adj_subtidal_mt + slr

        if np.isnan(pr) or np.isnan(mt):
            raise ValueError("One of values is numpy.nan.")

        eta += pr * theta_y_comp + mt * theta_y
        
        # write data to netCDF file   
        thwriter.write_step(i,t,eta)

    # Delete class
    del thwriter