예제 #1
0
def test():
    sim_tz = timezone.FixedTimeZone(-8, 'PST')
    init_date = datetime.datetime(1969, 12, 31, 16, tzinfo=sim_tz)
    print('{:} {:}'.format(
        timezone.datetime_to_epoch(
            datetime.datetime(1970, 1, 1, tzinfo=timezone.utc_tz)),
        timezone.datetime_to_epoch(init_date)))

    print('{:} {:}'.format(
        timezone.datetime_to_epoch(
            datetime.datetime(2016, 5, 1, 8, tzinfo=timezone.utc_tz)),
        timezone.datetime_to_epoch(datetime.datetime(2016, 5, 1,
                                                     tzinfo=sim_tz))))

    init_date = datetime.datetime(2016, 5, 1, tzinfo=sim_tz)
    river_flux_interp = interpolation.NetCDFTimeSeriesInterpolator(
        'forcings/stations/bvao3/bvao3.0.A.FLUX/*.nc', ['flux'],
        init_date,
        scalars=[-1.0],
        allow_gaps=True)
    print('{:} {:}'.format(timezone.datetime_to_epoch(init_date), init_date,
                           river_flux_interp(0.)))

    ncd = netCDF4.Dataset('forcings/stations/bvao3/bvao3.0.A.FLUX/201605.nc')
    t = ncd['time'][:]
    v = ncd['flux'][:]
    print('{:} {:} {:}'.format(
        t[0], datetime.datetime.fromtimestamp(t[0], tz=timezone.utc_tz), v[0]))
예제 #2
0
def test():
    import datetime
    mesh2d = Mesh('mesh_cre-plume002.msh')
    p1 = FunctionSpace(mesh2d, 'CG', 1)
    m2_field = Function(p1, name='elevation')

    sim_tz = timezone.FixedTimeZone(-8, 'PST')
    init_date = datetime.datetime(2016, 5, 1, tzinfo=sim_tz)

    tbnd = TidalBoundaryForcing(m2_field,
                                init_date,
                                constituents=['M2'],
                                boundary_ids=[1, 2, 3])
    tbnd.set_tidal_field(0.0)

    out = File('tmp/tidal_elev.pvd')
    for t in np.linspace(0, 12 * 3600., 60):
        tbnd.set_tidal_field(t)
        n = norm(m2_field)
        if m2_field.function_space().mesh().comm.rank == 0:
            print(n)
        out.write(m2_field)
예제 #3
0
def test():
    sim_tz = timezone.FixedTimeZone(-8, 'PST')
    zero_date = datetime.datetime(1969, 12, 31, 16, tzinfo=sim_tz)
    print('Epoch zero: {:} {:}'.format(
        timezone.datetime_to_epoch(
            datetime.datetime(1970, 1, 1, tzinfo=timezone.pytz.utc)),
        timezone.datetime_to_epoch(zero_date)))

    init_date = datetime.datetime(2006, 5, 15, tzinfo=sim_tz)
    print('sim. start: {:} {:}'.format(
        timezone.datetime_to_epoch(
            datetime.datetime(2006, 5, 15, 8, tzinfo=timezone.pytz.utc)),
        timezone.datetime_to_epoch(init_date)))

    river_flux_interp = interpolation.NetCDFTimeSeriesInterpolator(
        'forcings/stations/beaverarmy/flux_*.nc', ['flux'],
        init_date,
        scalars=[-1.0],
        allow_gaps=True)
    print('interpolated: {:} {:} {:}'.format(
        timezone.datetime_to_epoch(init_date), init_date,
        river_flux_interp(0.)[0]))

    ncd = netCDF4.Dataset('forcings/stations/beaverarmy/flux_2006.nc')
    t = ncd['time'][:]
    v = ncd['flux'][:]
    print('original:     {:} {:} {:}'.format(
        t[0], datetime.datetime.fromtimestamp(t[0], tz=timezone.pytz.utc),
        -v[0]))

    dt = 900
    for i in range(96):
        d = init_date + datetime.timedelta(seconds=i * dt)
        print('Time step {:3d}, {:}, flux: {:8.1f}'.format(
            i, d,
            river_flux_interp(i * dt)[0]))
예제 #4
0
def test():
    mesh2d = Mesh('mesh_cre-plume002.msh')
    comm = mesh2d.comm
    p1 = FunctionSpace(mesh2d, 'CG', 1)
    p1v = VectorFunctionSpace(mesh2d, 'CG', 1)
    windstress_2d = Function(p1v, name='wind stress')
    atmpressure_2d = Function(p1, name='atm pressure')

    sim_tz = timezone.FixedTimeZone(-8, 'PST')
    init_date = datetime.datetime(2015, 5, 16, tzinfo=sim_tz)
    pattern = 'forcings/atm/wrf/wrf_air.2015_*_*.nc'

    wrf = WRFInterpolator(p1, windstress_2d, atmpressure_2d, pattern,
                          init_date)

    # create a naive interpolation for first file
    xy = SpatialCoordinate(p1.mesh())
    fsx = Function(p1).interpolate(xy[0]).dat.data_with_halos
    fsy = Function(p1).interpolate(xy[1]).dat.data_with_halos

    mesh_lonlat = []
    for node in range(len(fsx)):
        lat, lon = to_latlon(fsx[node], fsy[node])
        mesh_lonlat.append((lon, lat))
    mesh_lonlat = np.array(mesh_lonlat)

    ncfile = netCDF4.Dataset('forcings/atm/wrf/wrf_air.2015_05_16.nc')
    itime = 10
    grid_lat = ncfile['lat'][:].ravel()
    grid_lon = ncfile['lon'][:].ravel()
    grid_lonlat = np.array((grid_lon, grid_lat)).T
    grid_pres = ncfile['prmsl'][itime, :, :].ravel()
    pres = scipy.interpolate.griddata(grid_lonlat,
                                      grid_pres,
                                      mesh_lonlat,
                                      method='linear')
    grid_uwind = ncfile['uwind'][itime, :, :].ravel()
    uwind = scipy.interpolate.griddata(grid_lonlat,
                                       grid_uwind,
                                       mesh_lonlat,
                                       method='linear')
    grid_vwind = ncfile['vwind'][itime, :, :].ravel()
    vwind = scipy.interpolate.griddata(grid_lonlat,
                                       grid_vwind,
                                       mesh_lonlat,
                                       method='linear')
    vrot = coordsys.VectorCoordSysRotation(coordsys.LL_WGS84,
                                           coordsys.SPCS_N_OR,
                                           mesh_lonlat[:, 0], mesh_lonlat[:,
                                                                          1])
    uwind, vwind = vrot(uwind, vwind)
    u_stress, v_stress = compute_wind_stress(uwind, vwind)

    # compare
    wrf.set_fields((itime - 8) * 3600.)  # NOTE timezone offset
    assert np.allclose(pres, atmpressure_2d.dat.data_with_halos)
    assert np.allclose(u_stress, windstress_2d.dat.data_with_halos[:, 0])

    # write fields to disk for visualization
    out_pres = File('tmp/atm_pressure.pvd')
    out_wind = File('tmp/wind_stress.pvd')
    hours = 24 * 3
    granule = 4
    simtime = np.arange(granule * hours) * 3600. / granule
    i = 0
    for t in simtime:
        wrf.set_fields(t)
        norm_atm = norm(atmpressure_2d)
        norm_wind = norm(windstress_2d)
        if comm.rank == 0:
            print('{:} {:} {:} {:}'.format(i, t, norm_atm, norm_wind))
        out_pres.write(atmpressure_2d)
        out_wind.write(windstress_2d)
        i += 1
예제 #5
0
def test():
    """
    Tests atmospheric model data interpolation.

    .. note::
        The following files must be present
        forcings/atm/wrf/wrf_air.2015_05_16.nc
        forcings/atm/wrf/wrf_air.2015_05_17.nc

        forcings/atm/nam/nam_air.local.2006_05_01.nc
        forcings/atm/nam/nam_air.local.2006_05_02.nc
    """
    mesh2d = Mesh('mesh_cre-plume_03_normal.msh')
    comm = mesh2d.comm
    p1 = get_functionspace(mesh2d, 'CG', 1)
    p1v = get_functionspace(mesh2d, 'CG', 1, vector=True)
    windstress_2d = Function(p1v, name='wind stress')
    atmpressure_2d = Function(p1, name='atm pressure')

    sim_tz = timezone.FixedTimeZone(-8, 'PST')

    # WRF
    # init_date = datetime.datetime(2015, 5, 16, tzinfo=sim_tz)
    # pattern = 'forcings/atm/wrf/wrf_air.2015_*_*.nc'
    # atm_time_step = 3600.  # for verification only
    # test_atm_file = 'forcings/atm/wrf/wrf_air.2015_05_16.nc'

    # NAM
    init_date = datetime.datetime(2006, 5, 1, tzinfo=sim_tz)
    pattern = 'forcings/atm/nam/nam_air.local.2006_*_*.nc'
    atm_time_step = 3*3600.
    test_atm_file = 'forcings/atm/nam/nam_air.local.2006_05_01.nc'

    atm_interp = ATMInterpolator(p1, windstress_2d, atmpressure_2d,
                                 to_latlon,
                                 pattern, init_date, COORDSYS, verbose=True)

    # create a naive interpolation for first file
    xy = SpatialCoordinate(p1.mesh())
    fsx = Function(p1).interpolate(xy[0]).dat.data_with_halos
    fsy = Function(p1).interpolate(xy[1]).dat.data_with_halos

    mesh_lonlat = []
    for node in range(len(fsx)):
        lat, lon = to_latlon(fsx[node], fsy[node])
        mesh_lonlat.append((lon, lat))
    mesh_lonlat = np.array(mesh_lonlat)

    ncfile = netCDF4.Dataset(test_atm_file)
    itime = 6
    grid_lat = ncfile['lat'][:].ravel()
    grid_lon = ncfile['lon'][:].ravel()
    grid_lonlat = np.array((grid_lon, grid_lat)).T
    grid_pres = ncfile['prmsl'][itime, :, :].ravel()
    pres = scipy.interpolate.griddata(grid_lonlat, grid_pres, mesh_lonlat, method='linear')
    grid_uwind = ncfile['uwind'][itime, :, :].ravel()
    uwind = scipy.interpolate.griddata(grid_lonlat, grid_uwind, mesh_lonlat, method='linear')
    grid_vwind = ncfile['vwind'][itime, :, :].ravel()
    vwind = scipy.interpolate.griddata(grid_lonlat, grid_vwind, mesh_lonlat, method='linear')
    vrot = coordsys.VectorCoordSysRotation(coordsys.LL_WGS84, COORDSYS, mesh_lonlat[:, 0], mesh_lonlat[:, 1])
    uwind, vwind = vrot(uwind, vwind)
    u_stress, v_stress = compute_wind_stress(uwind, vwind)

    # compare
    atm_interp.set_fields(itime*atm_time_step - 8*3600.)  # NOTE timezone offset
    assert np.allclose(pres, atmpressure_2d.dat.data_with_halos)
    assert np.allclose(u_stress, windstress_2d.dat.data_with_halos[:, 0])

    # write fields to disk for visualization
    pres_fn = 'tmp/AtmPressure2d.pvd'
    wind_fn = 'tmp/WindStress2d.pvd'
    print('Saving output to {:} {:}'.format(pres_fn, wind_fn))
    out_pres = File(pres_fn)
    out_wind = File(wind_fn)
    hours = 24*1.5
    granule = 4
    simtime = np.arange(granule*hours)*3600./granule
    i = 0
    for t in simtime:
        atm_interp.set_fields(t)
        norm_atm = norm(atmpressure_2d)
        norm_wind = norm(windstress_2d)
        if comm.rank == 0:
            print('{:} {:} {:} {:}'.format(i, t, norm_atm, norm_wind))
        out_pres.write(atmpressure_2d)
        out_wind.write(windstress_2d)
        i += 1