Ejemplo n.º 1
1
    def test(self):
        from netCDF4 import Dataset as NetCDF

        timeidx = 0
        in_wrfnc = NetCDF(wrf_in)

        if (varname == "interplevel"):
            ref_ht_850 = _get_refvals(referent, "interplevel", repeat, multi)
            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)
            hts_850 = interplevel(hts, p, 850)

            nt.assert_allclose(to_np(hts_850), ref_ht_850)

        elif (varname == "vertcross"):
            ref_ht_cross = _get_refvals(referent, "vertcross", repeat, multi)

            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)

            pivot_point = CoordPair(hts.shape[-1] // 2, hts.shape[-2] // 2)
            ht_cross = vertcross(hts, p, pivot_point=pivot_point, angle=90.)

            nt.assert_allclose(to_np(ht_cross), ref_ht_cross, rtol=.01)

        elif (varname == "interpline"):

            ref_t2_line = _get_refvals(referent, "interpline", repeat, multi)

            t2 = getvar(in_wrfnc, "T2", timeidx=timeidx)
            pivot_point = CoordPair(t2.shape[-1] // 2, t2.shape[-2] // 2)

            t2_line1 = interpline(t2, pivot_point=pivot_point, angle=90.0)

            nt.assert_allclose(to_np(t2_line1), ref_t2_line)

        elif (varname == "vinterp"):
            # Tk to theta
            fld_tk_theta = _get_refvals(referent, "vinterp", repeat, multi)
            fld_tk_theta = np.squeeze(fld_tk_theta)

            tk = getvar(in_wrfnc, "temp", timeidx=timeidx, units="k")

            interp_levels = [200, 300, 500, 1000]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 5 / 100.
            atol = 0.0001

            field = np.squeeze(field)

            nt.assert_allclose(to_np(field), fld_tk_theta, tol, atol)
Ejemplo n.º 2
0
def variables(ff, t):
    """ air density & qc for lwp"""
    z = getvar(ff, "z", timeidx=t, units="m")
    p = getvar(ff, "pressure", timeidx=t)  #[hPa]
    tk = getvar(ff, "tk", timeidx=t)  #[K]
    rho = p * 100.0 / (tk * 287.0)
    del tk, p  #[kg m-3]
    qc = getvar(ff, "QCLOUD", timeidx=t)
    qc *= 1.e3  #[g kg-1]
    rhoxqc = rho * qc
    del rho, qc
    """ for CTT, Max.dBZ, 500 m W, 500 m Wind vel. """
    dbz = getvar(ff, "REFL_10CM", timeidx=t)  #radar reflectivity
    ctt = getvar(ff, "ctt", timeidx=t, units="degC")  #Cloud top T
    rh = getvar(ff, "rh", timeidx=t)  #relative humidity %
    wa = getvar(ff, "wa", timeidx=t, units="m s-1")
    ua = getvar(ff, "ua", timeidx=t, units="m s-1")
    va = getvar(ff, "va", timeidx=t, units="m s-1")
    w500 = interplevel(wa, z, 500.0)
    del wa  #500 m height
    u500 = interplevel(ua, z, 500.0)
    del ua
    v500 = interplevel(va, z, 500.0)
    del va
    vel_500 = (u500**2 + v500**2)**0.5
    del u500, v500
    """ 0:MCAPE, 1:MCIN, 2:LCL, 3:LFC """
    thermo = g_cape.get_2dcape(ff, timeidx=t)
    lcl = thermo[2, :, :]
    del thermo  #m
    #td = getvar(ff, "td", timeidx=t, units="K") #Dew point T
    #print("Get all variables!")
    return rhoxqc, z, dbz, ctt, rh, lcl, w500, vel_500
Ejemplo n.º 3
0
def plot_interact(tindex, level):
    ua_interp = interplevel(ua, z, level)
    va_interp = interplevel(va, z, level)
    wa_interp = interplevel(wa, z, level)

    fig1, ax1 = plt.subplots(figsize=(12, 10))
    
    cb = ax1.contourf(z['west_east'].values,
                   z['south_north'].values,
               wa_interp.isel(Time=tindex).values,levels=np.arange(-30,30,0.5),cmap='Spectral_r')

    Q = ax1.quiver(z['west_east'].values,
                   z['south_north'].values,
                   ua_interp.isel(Time=tindex).values,
                   va_interp.isel(Time=tindex).values,pivot='middle',color='black',
                   units='width',width=0.0007,headwidth=10)
    
    qk = ax1.quiverkey(Q, 0.92, .95, 5, r'$5 \frac{m}{s}$', labelpos='E',
                       coordinates='figure')

    cb = plt.colorbar(cb, shrink=0.5, title='Vertical wind (m/s)')

    ax1.set_title('Vertical motion (m/s) and winds (m/s) at time='+str(tindex)+' and level='+str(level))
    
    plt.tight_layout()
    plt.show()
Ejemplo n.º 4
0
def chi_from_wrf(file, time_index):
    ncfile = Dataset(file, mode='r')
    td = getvar(ncfile, "td", timeidx=time_index)
    tc = getvar(ncfile, "tc", timeidx=time_index)
    p = getvar(ncfile, "pressure", timeidx=time_index)
    t850, t700 = [interplevel(tc, p, level).data for level in [850, 700]]
    td850 = interplevel(td, p, 850).data
    chaines = chi(t850, td850, t700)
    return chaines
Ejemplo n.º 5
0
def calculate_icing_wrf(ncfile, lwc, lwc_threshold):

    # levels(hPa): 0=100 1=150 2=200 3=250 4=300 5=350 6=400 7=450 8=500 9=550
    #              10=600 11=650 12=700 13=750 14=800 15=850 16=900 17=950 18=1000
    levels_list = [
        100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750,
        800, 850, 900, 950, 1000
    ]
    total_levels = np.size(levels_list, 0)

    ################################################
    # get temperature and relative humidity
    ################################################
    temperature_var = getvar(ncfile, "tc")
    rh_var = getvar(ncfile, "rh")

    # Get the pressure for interpolating the variables to the correct pressure level
    p = getvar(ncfile, "pressure")

    ################################################
    # calculate icing probabilities values
    ################################################
    grid_lats = rh_var.shape[1]
    grid_lons = rh_var.shape[2]
    probability_grid = np.ones([total_levels, grid_lats, grid_lons])

    for current_level in range(total_levels):
        # Interpolate the variables for the current pressure level
        current_pressure = levels_list[current_level]
        temperature_level = np.array(
            interplevel(temperature_var, p, current_pressure))
        rh_level = np.array(interplevel(rh_var, p, current_pressure))

        # Make the lwc values binary: 0 for less than the threshold and 1 for above it
        lwc_level = np.squeeze(lwc[current_level, :, :])
        lwc_level = np.ceil(lwc_level - lwc_threshold)

        for lats_idx in range(grid_lats):
            for lons_idx in range(grid_lons):
                RH_index = (np.abs(RH_probabilities[:, 0] -
                                   rh_level[lats_idx, lons_idx])).argmin()
                RH_value = RH_probabilities[RH_index, 1]
                temperature_index = (
                    np.abs(temperature_probabilities[:, 0] -
                           temperature_level[lats_idx, lons_idx])).argmin()
                temperature_value = temperature_probabilities[
                    temperature_index, 1]
                probability_grid[current_level, lats_idx,
                                 lons_idx] = lwc_level[
                                     lats_idx,
                                     lons_idx] * temperature_value * RH_value

    return probability_grid
Ejemplo n.º 6
0
def calculate_lwc_wrf(ncfile):

    # levels(hPa): 0=100 1=150 2=200 3=250 4=300 5=350 6=400 7=450 8=500 9=550
    #              10=600 11=650 12=700 13=750 14=800 15=850 16=900 17=950 18=1000
    levels_list = [
        100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750,
        800, 850, 900, 950, 1000
    ]
    total_levels = np.size(levels_list, 0)

    ################################################
    # get Virtual temperature, and CLWC.
    # In the ecmwf version we needed specific humidity and temperature to
    # calculate the virtual temperature. in WRF it is already calculated!
    ################################################
    virtual_temperature_var = getvar(ncfile, "tv")
    clwc_var = getvar(ncfile, "QCLOUD")

    # Get the pressure for interpolating the variables to the correct pressure level
    p = getvar(ncfile, "pressure")

    ################################################
    # calculate liquid water content in g/m3 values
    ################################################
    lwc_grid = np.ones([total_levels, clwc_var.shape[1], clwc_var.shape[2]])

    Rd = 2.87 / 1000  # m3*mb/Kg*K ->  m3*mb/g*K
    for current_level in range(total_levels):
        current_pressure = levels_list[current_level]

        # Interpolate the variables for the current pressure level
        virtual_temperature_grid = np.array(
            interplevel(virtual_temperature_var, p, current_pressure))
        clwc_grid = np.array(interplevel(clwc_var, p, current_pressure))

        # Calculate LWC
        air_density_grid = current_pressure / (Rd * virtual_temperature_grid)
        lwc_grid[current_level] = clwc_grid * air_density_grid

        ################################################
        # Debug messages
        # print("Current Level: " + str(current_level))
        # print("Max Air Density: " + str(air_density_grid[current_level].max()))
        # print("Max CLWC: " + str(clwc_grid.max()))
        # print("Max LWC: " + str(lwc_grid[current_level].max()))
        # print"========================================="
        ################################################

    return lwc_grid
Ejemplo n.º 7
0
def interp_z(ff, time, varname, speedup=1, cores=7):
    '''
    speedup 1: thread, It's may be faster. 
    '''
    # global variables
    zz = getvar(ff, "z", timeidx=time)
    var = getvar(ff, str(varname), timeidx=time)

    def wrf_interp(level):
        result = interplevel(var, zz, level)
        return result

    (lev, nk) = delta_z()  # dz for interpolation
    ny = np.int32(len(var[0, :, 0]))
    nx = np.int32(len(var[0, 0, :]))
    var_m = np.zeros(shape=(nk, ny, nx), dtype=np.float32)
    print("Time index: {}, Variable: {}".format(time, str(varname)))
    print("After Interpolation (nk,nj,ni): \033[93m{}\033[0m".format(
        var_m.shape))
    if speedup == 1:
        with ThreadPoolExecutor(max_workers=cores) as executor:
            for k in range(nk):
                #var_m[k,:,:] = executor.submit(interplevel,var,zz,lev[k]).result()
                var_m[k, :, :] = executor.submit(wrf_interp, lev[k]).result()
    else:
        for k in range(nk):
            var_m[k, :, :] = interplevel(var, zz, lev[k])
    var_m = np.where(np.isnan(var_m), fill_value, var_m)
    return var_m
def interp_data(fieldname, field, Z_AGL):

    interp_levels = [
        0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500,
        6000, 6500, 7000
    ]
    #interp_levels = [2000]

    vars = {}
    for level in interp_levels:

        varname = fieldname + "_" + str(level) + "m_AGL"

        if level == 0:
            vars[varname] = field[:, 0, :, :]
        else:
            #
            # interpolate to the requested level
            #
            vars[varname] = wrf.interplevel(field[:, :, :, :],
                                            Z_AGL[:, :, :, :],
                                            level,
                                            meta=False)

    return vars
Ejemplo n.º 9
0
def interp_data_concat(field, Z_AGL):

    interp_levels = [
        0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500,
        6000, 6500, 7000
    ]

    count = 0
    for level in interp_levels:

        count = count + 1

        if level == 0:
            interp_field = field[:, 0, :, :]
            interp_field = interp_field[:, np.newaxis, :, :]
        else:
            #
            # interpolate to the requested level
            #
            interp_field = wrf.interplevel(field[:, :, :, :],
                                           Z_AGL[:, :, :, :],
                                           level,
                                           meta=False)
            interp_field = interp_field[:, np.newaxis, :, :]

        if count == 1:
            interp_3d = interp_field
            print(interp_3d.shape)
        else:
            interp_3d = np.concatenate((interp_3d, interp_field), axis=1)
            print(interp_3d.shape)

    return interp_3d
Ejemplo n.º 10
0
def regrid_and_sum(flx_ds, _boo=True):
    ds_rl = flx_ds['CONC'].where(_boo).sum(co.RL).load()
    # %%
    import wrf
    topo_ = (ds_rl[co.ZM] - ds_rl[co.TOPO])
    ds_g = (topo_ / 500).round() * 500
    # %%
    res = wrf.interplevel(ds_rl.reset_coords(drop=True), ds_g, ds_g[co.ZM])
    res = res.rename({'level': co.ZM})
    res.name = 'CONC'
    # %%
    res_sum = res.sum([co.R_CENTER, co.TH_CENTER])
    return res_sum
Ejemplo n.º 11
0
def WRF3D(WindArray, Pos_LLH):
    # Interpolates spatially

    # Assign the lat/lon/hei and find height variation in the model
    [[lat], [lon],
     [hei]] = [np.rad2deg(Pos_LLH[0]),
               np.rad2deg(Pos_LLH[1]), Pos_LLH[2]]

    # Find xy positions of the lat/lon
    ang_dist2 = (WindArray[0, 0] - lat)**2 + (WindArray[1, 0] - lon)**2
    min_index = np.argmin(ang_dist2)
    ncol = WindArray.shape[3]
    xid = min_index % ncol
    yid = min_index // ncol

    lat_var = WindArray[0, 0, yid - 1:yid + 2, xid - 1:xid + 2]  #[3,3]
    lon_var = WindArray[1, 0, yid - 1:yid + 2, xid - 1:xid + 2]  #[3,3]
    hei_var = WindArray[2, :, yid - 1:yid + 2, xid - 1:xid + 2]  #[z,3,3]

    # Find the variable at a certain altitude as a 2D array [3,3] #linear interpolation!
    interp_horiz = lambda entry_no: interplevel(
        field3d=WindArray[entry_no, :, yid - 1:yid + 2, xid - 1:xid + 2],
        vert=hei_var,
        desiredlev=hei,
        missing=np.nan).data  #<---RuntimeWarning originates from here

    # 2D interpolate [1,]
    latlon = np.vstack((lat_var.flatten(), lon_var.flatten())).T
    interp2pt = lambda entry_no: griddata(latlon,
                                          interp_horiz(entry_no).flatten(),
                                          np.array([lat, lon]))

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        we = interp2pt(3)  # Wind east [m/s]
        wn = interp2pt(4)  # Wind north [m/s]
        wu = interp2pt(5)  # Wind up [m/s]
        tk = interp2pt(6)  # Temperature [K]
        pr = interp2pt(7)  # Pressure [Pa]
        rh = interp2pt(8)  # Relative humidity []

    # Compare:
    rho_a = density_from_pressure(tk, pr, rh)
    if np.isnan(rho_a): [hei, we, wn, wu, rho_a, tk] = WRF_history[-1]
    Wind_ENU = np.vstack((we, wn, wu))

    # Save the variables
    WRF_history.append(np.vstack((hei, Wind_ENU, rho_a, tk)))

    return Wind_ENU, rho_a, tk
def interpolate_vert(ncfile, start_lat, end_lat, start_lon, end_lon, var, time,
                     start_p, end_p, interval_p):
    startpoint = CoordPair(lat=start_lat, lon=start_lon)
    endpoint = CoordPair(lat=end_lat, lon=end_lon)
    height = getvar(ncfile, 'height')
    hgt = getvar(ncfile, 'HGT')
    p = getvar(ncfile, 'pressure', timeidx=time)
    f = getvar(ncfile, var, timeidx=time)
    p_height = p[:, 0, 0]
    bool, start_layer, end_layer = get_pressure_layer(p_height, start_p, end_p)
    p_level = np.mgrid[p_height[start_layer].values:p_height[end_layer].
                       values:complex(str(end_layer - start_layer + 1) + 'j')]
    f_vert = vertcross(f,
                       p,
                       wrfin=ncfile,
                       levels=p_level,
                       start_point=startpoint,
                       end_point=endpoint,
                       latlon=True)
    if var == 'wa':
        f_vert = f_vert * 200
        print(f_vert)
    #处理插值数据的维度
    p_vert_list = f_vert.coords['vertical'].values
    print("插值的压力为:", end='')
    print(p_vert_list)
    lon_vert_list, lat_vert_list = [], []
    for i in range(len(f_vert.coords['xy_loc'])):
        s = str(f_vert.coords['xy_loc'][i].values)
        lon_vert_list.append(float(s[s.find('lon=') + 4:s.find('lon=') + 16]))
        lat_vert_list.append(float(s[s.find('lat=') + 4:s.find('lat=') + 16]))
    lon_vert_list = np.array(lon_vert_list)
    lat_vert_list = np.array(lat_vert_list)
    lon_vert_list = np.around(lon_vert_list, decimals=2)
    lat_vert_list = np.around(lat_vert_list, decimals=2)
    h_vert_list = []
    h2h = height - hgt
    for i in range(end_p, start_p - interval_p, -interval_p):
        h_vert_list.append(float(np.max(interplevel(h2h, p, i)).values))
    print("对应的高度为:", end='')
    print(h_vert_list)
    lat_vert_list = np.mgrid[lat_vert_list[0]:lat_vert_list[-1]:complex(
        str(len(lat_vert_list)) + 'j')]
    lon_vert_list = np.mgrid[lon_vert_list[0]:lon_vert_list[-1]:complex(
        str(len(lon_vert_list)) + 'j')]
    return f_vert, lat_vert_list, lon_vert_list, p_vert_list, h_vert_list
Ejemplo n.º 13
0
def interp_4d(ipvar, wrf_hgts, out_hgts):
    """ Interpolates vertically to heights specified by
    out_hgts using the interplevel function from wrf-python
    -----------------------------------------------------
    In: 4D field of variable to interpolate (ipvar)
        Heights of the WRF simulation (wrf_hgts)
        Heights to interpolate to (out_hgts)
    -----------------------------------------------------
    Out: Interpolated 4D field (out)
    --------------------------------------
    """
    shape = list(ipvar.shape)
    shape[1] = len(out_hgts)
    out = np.empty(shape, ipvar.dtype)
    for k, out_hgt in enumerate(out_hgts):
        out[:, k, :, :] = wrf.interplevel(ipvar, wrf_hgts, out_hgt)
    return (out)
Ejemplo n.º 14
0
def wrf_interp(data_AGL, data_var):
    """Function to compute interpolation using wrf-python.
    
    Args:
        data_AGL (Xarray dask array): Data of heights above ground level.
        data_var (Xarray dask array): Data of variable for interpolation.
        
    Returns:
        Data interpolated onto four fixed heights (1, 3, 5, and 7 km).
    
    """
    import os
    os.environ[
        "PROJ_LIB"] = "/glade/work/molina/miniconda3/envs/python-tutorial/share/proj/"
    import wrf
    return (wrf.interplevel(data_var.squeeze(), data_AGL.squeeze(),
                            [1000, 3000, 5000, 7000]).expand_dims("Time"))
Ejemplo n.º 15
0
def from_agl_to_asl(
        ds,
        ds_var='conc_norm',
        delta_z=500,
        z_top=15000,
        ds_var_name_out=None
):
    log.ger.warning(
        f'this will only work if ds z levels are constant')
    import wrf
    t_list = [co.ZM, co.R_CENTER, co.TH_CENTER]
    d3d = ds[ds_var]  # .sum( [ co.RL ] )
    d3d_attrs = d3d.attrs
    d3d = d3d.transpose(
        co.RL, *t_list, transpose_coords=True
    )
    dz = d3d[co.TOPO] + np.round(d3d[co.ZM] / delta_z) * delta_z
    d3d = d3d.reset_coords(drop=True)
    dz = dz.transpose(*t_list, transpose_coords=True)
    dz = dz.reset_coords(drop=True)
    # %%
    # print( d3d.shape )
    # print( dz.shape )
    # %%
    z_lev = np.arange(delta_z / 2, z_top, delta_z)
    da_interp = wrf.interplevel(d3d, dz, z_lev)
    da_reinterp = da_interp.rename(level=co.ZM)

    # %%
    ds_chop = ds.isel({co.ZM: slice(0, len(da_reinterp[co.ZM]))})
    for coord in list(ds.coords):
        da_reinterp = da_reinterp.assign_coords(
            **{coord: ds_chop[coord]})
    if ds_var_name_out is not None:
        da_reinterp.name = ds_var_name_out

    # we do this in order to avoid the problem of setting attributes
    # to none that cannot be saved using to netcdf.
    da_reinterp.attrs = d3d_attrs

    ds_reinterp = da_reinterp.to_dataset()
    # todo: check that concentrations are the same after resampling
    return ds_reinterp
Ejemplo n.º 16
0
def interp_to_isosurface_worker(ds: xr.Dataset, *, isovalues, space_dims,
                                **kwargs) -> xr.DataArray:
    '''
    Return an xr.DataArray with a block of data interpolated to an isosurface.
    Input must be xr.Dataset object that is not lazy and not chunked.
    Input dataset will have two variables with names "variable" and "level_variable"
    the two arrays needed are passed as a dataset as xr.map_blocks cannot take a datarray as kwarg
    Lazy/Dasky xr.DataArray objects do not support data assignment with .loc
    like xr.DataArrays with numpy data.
    '''

    # make final data structure to populate with data
    final = interp_to_isosurface_meta_template(ds, isovalues)

    # record initial axis order; for reshaping back to initial before return
    final_dims = final.variable.dims
    ordered_dims = list(final_dims)
    z = final.camps.z.name
    ordered_dims.remove(z)
    ordered_dims.insert(
        0, z
    )  # ordered dims now has z as left-most axis for use with wrf interplevel

    # go to working space shape
    ds = ds.transpose(*ordered_dims)  # input
    final = final.transpose(*ordered_dims)  # output

    # order dims for work with interplevel
    other_dims = [dim for dim in ds.dims if dim not in space_dims]
    other_dim_arrays = [ds[dim].data for dim in other_dims]
    # iterate over all the space sections applying interplevel
    for dim_values in product(*other_dim_arrays):
        loc = {k: v for k, v in zip(other_dims, dim_values)}
        final.loc[loc] = interplevel(ds.variable.loc[loc],
                                     ds.level_variable.loc[loc],
                                     isovalues,
                                     meta=False,
                                     **kwargs)

    # go back to initial shape and axis order
    final = final.transpose(*final_dims)
    return final
Ejemplo n.º 17
0
def main(folder, wrffolder, append, sourceid, plotmap, plotcurves, plotcontour,
         plotcontourf, plotbarbs, resol):
    #z_unit = 'km'
    z_unit = 'dm'
    z_unitStr = 'dam'
    #u_unit = 'm s-1'
    u_unit = 'kt'
    w_unit = u_unit
    w_unitStr = 'm/s'
    w_unitStr = 'kt'
    p_unit = 'hPa'
    df_obs = pd.read_csv(folder + sourceid + '_json.txt', delimiter='\t')
    df_obs['time'] = pd.to_datetime(df_obs['CurrentTime'], unit='s')
    df_obs['air_temperature'] = df_obs['Temperature']
    df_obs['pressure'] = df_obs['Barometric Pressure']
    df_obs['wind_speed'] = df_obs[
        'Wind Speed'] * 1.852 / 3.6  # Convert from knots to m/s
    if z_unit == "km":
        feetScale = 0.3048 / 1000
    elif z_unit == "m":
        feetScale = 0.3048
    elif z_unit == "dm":
        feetScale = 0.3048 / 10

    df_obs['z'] = df_obs['Alt'].to_numpy(
    ) * feetScale  # Convert from feet to decameters (dam)
    # Get data from WRF file
    i_domain = 10
    isOutside = True
    while isOutside:
        if i_domain < 0:
            print('Parts of the flightpath for ' + str(sourceid) +
                  ' is not inside the solution domain')
            break

        i_domain -= 1
        try:
            ncfile = Dataset(wrffolder + '/wrfout_d0' + str(i_domain) + '.nc')
        except:
            continue

        fields = ['T', 'wind_speed', 'wind_from_direction']
        lat_e = df_obs['Latitude'].to_numpy()
        lon_e = df_obs['Longitude'].to_numpy()
        z_e = df_obs['z'].to_numpy()
        time_e = df_obs['time'].to_numpy()
        dummy = pd.DataFrame(
            {'time': wrf.getvar(ncfile, 'Times', wrf.ALL_TIMES)})
        time = dummy['time'].to_numpy()
        indices = (time[0] <= time_e) & (time_e <= time[-1])
        if not np.any(indices):
            raise OutOfRange('The mode-S data is out of range')

        lat_e = lat_e[indices]
        lon_e = lon_e[indices]
        z_e = z_e[indices]
        time_e = time_e[indices]

        x = np.zeros((lat_e.shape[0], 4))
        xy = wrf.ll_to_xy(ncfile, lat_e, lon_e, as_int=False, meta=False)
        if ncfile.MAP_PROJ_CHAR == 'Cylindrical Equidistant' and i_domain == 1:
            xy[0] += 360 / 0.25

        x[:, 0] = xy[0]
        x[:, 1] = xy[1]
        e_we = ncfile.dimensions['west_east'].size
        e_sn = ncfile.dimensions['south_north'].size
        if np.any(xy < 0) or np.any(xy[0] > e_we - 1) or np.any(
                xy[1] > e_sn - 1):
            print('Flight path is outside domain d0' + str(i_domain))
            continue
        else:
            print('Extracting WRF-data from domain d0' + str(i_domain))

        if plotcurves:
            xy2 = np.zeros(xy.T.shape)
            xy2[:, :] = xy[:, :].T
            #zgrid = wrf.interp2dxy(wrf.getvar(ncfile,'z',units=z_unit),xy2,meta=False)
            zgrid = wrf.interp2dxy(wrf.g_geoht.get_height(ncfile,
                                                          units=z_unit),
                                   xy2,
                                   meta=False)  # Get geopotential height
            for i in range(0, len(z_e)):
                f_time = interp1d(zgrid[:, i],
                                  range(0, zgrid.shape[0]),
                                  kind='linear')
                x[i, 2] = f_time(z_e[i])

            f_time = interp1d(time.astype('int'),
                              range(0, len(time)),
                              kind='linear')
            x[:, 3] = f_time(time_e.astype('int'))
            df_wrf = pd.DataFrame()
            df_wrf['time'] = time_e
            df_wrf['air_temperature'] = linInterp(
                wrf.getvar(ncfile, 'tc', wrf.ALL_TIMES, meta=False), x)
            df_wrf['pressure'] = linInterp(
                wrf.g_pressure.get_pressure(ncfile,
                                            wrf.ALL_TIMES,
                                            meta=False,
                                            units=p_unit), x)
            df_wrf['wind_speed'] = linInterp(
                wrf.g_wind.get_destag_wspd(ncfile, wrf.ALL_TIMES, meta=False),
                x)
            df_wrf['wind_from_direction'] = linInterp(
                wrf.g_wind.get_destag_wdir(ncfile, wrf.ALL_TIMES, meta=False),
                x)

            if df_wrf.isnull().values.any():
                print('Some points are outside the domain ' + str(i_domain))
                continue
            else:
                isOutside = False
        else:
            isOutside = False

    if plotcurves:
        # Plot data
        sharex = True
        #fields = np.array([['air_temperature','wind_speed','pressure'],
        #                       ['windDirX', 'windDirY','z']])
        #ylabels = np.array([['Temperature [°C]', 'Wind speed [m/s]', 'Pressure ['+p_unit+']'],
        #                        ['Wind direction - X','Wind direction - Y', 'Altitude ['+z_unitStr+']']])
        fields = np.array([['air_temperature', 'wind_speed'],
                           ['windDirX', 'windDirY']])
        ylabels = np.array([['Temperature [°C]', 'Wind speed [m/s]'],
                            ['Wind direction - X', 'Wind direction - Y']])
        df_obs['windDirX'] = np.cos(np.radians(df_obs['Wind Direction']))
        df_obs['windDirY'] = np.sin(np.radians(df_obs['Wind Direction']))
        df_wrf['windDirX'] = np.cos(np.radians(df_wrf['wind_from_direction']))
        df_wrf['windDirY'] = np.sin(np.radians(df_wrf['wind_from_direction']))
        df_wrf['z'] = z_e

        fig, axs = plt.subplots(fields.shape[0],
                                fields.shape[1],
                                sharex=sharex)
        mng = plt.get_current_fig_manager()
        #mng.resize(*mng.window.maxsize())
        #mng.frame.Maximize(True)
        mng.window.showMaximized()
        title = 'Comparison between Mode-S data and WRF simulations for flight ' + sourceid
        if plotcurves:
            title += '. WRF data from domain d0' + str(
                i_domain) + ' with grid resolution {:.1f} km'.format(
                    max(ncfile.DX, ncfile.DY) / 1000)
        fig.suptitle(title)
        for i in range(0, fields.shape[0]):
            for j in range(0, fields.shape[1]):
                axs[i, j].plot(df_wrf.time.to_numpy(),
                               df_wrf[fields[i, j]].to_numpy(),
                               'b',
                               label='WRF forecast')
                axs[i, j].plot(df_obs.time.to_numpy(),
                               df_obs[fields[i, j]].to_numpy(),
                               'r',
                               label='Observation data')

                axs[i, j].legend()
                axs[i, j].set(xlabel='Time', ylabel=ylabels[i, j])
                axs[i, j].set_xlim(time_e[0], time_e[-1])
        axs[1, 0].set_ylim(-1, 1)
        axs[1, 1].set_ylim(-1, 1)
        plt.show()
        fig.savefig(folder + '/' + sourceid + '_curves.png', dpi=400)

    if plotmap:
        # Extract the pressure, geopotential height, and wind variables
        ncfile = Dataset(wrffolder + '/wrfout_d01.nc')
        p = wrf.g_pressure.get_pressure(ncfile, units=p_unit)
        #p = getvar(ncfile, "pressure",units=p_unit)
        z = getvar(ncfile, "z", units=z_unit)
        ua = getvar(ncfile, "ua", units=u_unit)
        va = getvar(ncfile, "va", units=u_unit)
        wspd = getvar(ncfile, "wspd_wdir", units=w_unit)[0, :]

        # Interpolate geopotential height, u, and v winds to 500 hPa
        ht_500 = interplevel(z, p, 500)
        u_500 = interplevel(ua, p, 500)
        v_500 = interplevel(va, p, 500)
        wspd_500 = interplevel(wspd, p, 500)

        # Get the lat/lon coordinates
        lats, lons = latlon_coords(ht_500)

        # Get the map projection information
        cart_proj = get_cartopy(ht_500)

        # Create the figure
        fig = plt.figure(figsize=(12, 9))
        ax = plt.axes(projection=cart_proj)

        # Download and add the states and coastlines
        name = 'admin_0_boundary_lines_land'
        #name = 'admin_1_states_provinces_lines'
        #name = "admin_1_states_provinces_shp"
        bodr = cfeature.NaturalEarthFeature(category='cultural',
                                            name=name,
                                            scale=resol,
                                            facecolor='none')
        land = cfeature.NaturalEarthFeature('physical', 'land', \
            scale=resol, edgecolor='k', facecolor=cfeature.COLORS['land'])
        ocean = cfeature.NaturalEarthFeature('physical', 'ocean', \
            scale=resol, edgecolor='none', facecolor=cfeature.COLORS['water'])
        lakes = cfeature.NaturalEarthFeature('physical', 'lakes', \
            scale=resol, edgecolor='b', facecolor=cfeature.COLORS['water'])
        rivers = cfeature.NaturalEarthFeature('physical', 'rivers_lake_centerlines', \
            scale=resol, edgecolor='b', facecolor='none')

        ax.add_feature(land, facecolor='beige', zorder=0)
        ax.add_feature(ocean, linewidth=0.2, zorder=0)
        ax.add_feature(lakes, linewidth=0.2, zorder=1)
        ax.add_feature(bodr, edgecolor='k', zorder=1, linewidth=0.5)
        #ax.add_feature(rivers, linewidth=0.5,zorder=1)
        if plotcontour:
            # Add the 500 hPa geopotential height contour
            levels = np.arange(100., 2000., 10.)
            contours = plt.contour(to_np(lons),
                                   to_np(lats),
                                   to_np(ht_500),
                                   levels=levels,
                                   colors="forestgreen",
                                   linewidths=1,
                                   transform=ccrs.PlateCarree(),
                                   zorder=3)
            plt.clabel(contours, inline=1, fontsize=10, fmt="%i")

        if plotcontourf:
            try:
                with open(home + '/kode/colormaps/SINTEF1.json') as f:
                    json_data = json.load(f)
                SINTEF1 = np.reshape(json_data[0]['RGBPoints'], (-1, 4))
                cmap = ListedColormap(fill_colormap(SINTEF1))
            except:
                print(
                    'SINTEF1 colormap not found (can be found at https://github.com/Zetison/colormaps.git)'
                )
                cmap = get_cmap("rainbow")

            # Add the wind speed contours
            levels = np.linspace(20, 120, 101)
            wspd_contours = plt.contourf(to_np(lons),
                                         to_np(lats),
                                         to_np(wspd_500),
                                         levels=levels,
                                         cmap=cmap,
                                         alpha=0.7,
                                         antialiased=True,
                                         transform=ccrs.PlateCarree(),
                                         zorder=2)
            cbar_wspd = plt.colorbar(wspd_contours,
                                     ax=ax,
                                     orientation="horizontal",
                                     pad=.05,
                                     shrink=0.5,
                                     aspect=30,
                                     ticks=range(10, 110, 10))
            cbar_wspd.ax.set_xlabel('Wind speeds [' + w_unitStr +
                                    '] at 500 mbar height')
        # Add the 500 hPa wind barbs, only plotting every nthb data point.
        nthb = 10
        if plotbarbs:
            plt.barbs(to_np(lons[::nthb, ::nthb]),
                      to_np(lats[::nthb, ::nthb]),
                      to_np(u_500[::nthb, ::nthb]),
                      to_np(v_500[::nthb, ::nthb]),
                      transform=ccrs.PlateCarree(),
                      length=6,
                      zorder=3)

        track = sgeom.LineString(
            zip(df_obs['Longitude'].to_numpy(), df_obs['Latitude'].to_numpy()))
        fullFlightPath = ax.add_geometries([track],
                                           ccrs.PlateCarree(),
                                           facecolor='none',
                                           zorder=4,
                                           edgecolor='red',
                                           linewidth=2,
                                           label='Flight path')
        track = sgeom.LineString(zip(lon_e, lat_e))
        flightPath = ax.add_geometries([track],
                                       ccrs.PlateCarree(),
                                       facecolor='none',
                                       zorder=4,
                                       linestyle=':',
                                       edgecolor='green',
                                       linewidth=2,
                                       label='Extracted flight path')
        #plt.legend(handles=[fullFlightPath])
        #blue_line = mlines.Line2D([], [], color='red',linestyle='--', label='Flight path',linewidth=2)
        #plt.legend(handles=[blue_line])
        # Set the map bounds
        ax.set_xlim(cartopy_xlim(ht_500))
        ax.set_ylim(cartopy_ylim(ht_500))

        #ax.gridlines(draw_labels=True)
        ax.gridlines()
        startdate = pd.to_datetime(str(
            time_e[0])).strftime("%Y-%m-%d %H:%M:%S")
        plt.title('Flight ' + sourceid +
                  ', with visualization of WRF simulation (' + startdate +
                  ') at 500 mbar Height (' + z_unitStr + '), Wind Speed (' +
                  w_unitStr + '), Barbs (' + w_unitStr + ')')

        # Plot domain boundaries
        infile_d01 = wrffolder + '/wrfout_d01.nc'
        cart_proj, xlim_d01, ylim_d01 = get_plot_element(infile_d01)

        infile_d02 = wrffolder + '/wrfout_d02.nc'
        _, xlim_d02, ylim_d02 = get_plot_element(infile_d02)

        infile_d03 = wrffolder + '/wrfout_d03.nc'
        _, xlim_d03, ylim_d03 = get_plot_element(infile_d03)

        infile_d04 = wrffolder + '/wrfout_d04.nc'
        _, xlim_d04, ylim_d04 = get_plot_element(infile_d04)

        # d01
        ax.set_xlim([
            xlim_d01[0] - (xlim_d01[1] - xlim_d01[0]) / 15,
            xlim_d01[1] + (xlim_d01[1] - xlim_d01[0]) / 15
        ])
        ax.set_ylim([
            ylim_d01[0] - (ylim_d01[1] - ylim_d01[0]) / 15,
            ylim_d01[1] + (ylim_d01[1] - ylim_d01[0]) / 15
        ])

        # d01 box
        textSize = 10
        colors = ['blue', 'orange', 'brown', 'deepskyblue']
        linewidth = 1
        txtscale = 0.003
        ax.add_patch(
            mpl.patches.Rectangle((xlim_d01[0], ylim_d01[0]),
                                  xlim_d01[1] - xlim_d01[0],
                                  ylim_d01[1] - ylim_d01[0],
                                  fill=None,
                                  lw=linewidth,
                                  edgecolor=colors[0],
                                  zorder=10))
        ax.text(xlim_d01[0],
                ylim_d01[0] + (ylim_d01[1] - ylim_d01[0]) * (1 + txtscale),
                'd01',
                size=textSize,
                color=colors[0],
                zorder=10)

        # d02 box
        ax.add_patch(
            mpl.patches.Rectangle((xlim_d02[0], ylim_d02[0]),
                                  xlim_d02[1] - xlim_d02[0],
                                  ylim_d02[1] - ylim_d02[0],
                                  fill=None,
                                  lw=linewidth,
                                  edgecolor=colors[1],
                                  zorder=10))
        ax.text(xlim_d02[0],
                ylim_d02[0] + (ylim_d02[1] - ylim_d02[0]) * (1 + txtscale * 3),
                'd02',
                size=textSize,
                color=colors[1],
                zorder=10)

        # d03 box
        ax.add_patch(
            mpl.patches.Rectangle((xlim_d03[0], ylim_d03[0]),
                                  xlim_d03[1] - xlim_d03[0],
                                  ylim_d03[1] - ylim_d03[0],
                                  fill=None,
                                  lw=linewidth,
                                  edgecolor=colors[2],
                                  zorder=10))
        ax.text(xlim_d03[0],
                ylim_d03[0] + (ylim_d03[1] - ylim_d03[0]) *
                (1 + txtscale * 3**2),
                'd03',
                size=textSize,
                color=colors[2],
                zorder=10)

        # d04 box
        ax.add_patch(
            mpl.patches.Rectangle((xlim_d04[0], ylim_d04[0]),
                                  xlim_d04[1] - xlim_d04[0],
                                  ylim_d04[1] - ylim_d04[0],
                                  fill=None,
                                  lw=linewidth,
                                  edgecolor=colors[3],
                                  zorder=10))
        ax.text(xlim_d04[0],
                ylim_d04[0] + (ylim_d04[1] - ylim_d04[0]) *
                (1 + txtscale * 3**3),
                'd04',
                size=textSize,
                color=colors[3],
                zorder=10)

        plt.show()
        fig.savefig(folder + '/' + sourceid + '_map.png', dpi=400)
Ejemplo n.º 18
0
def process_wrfout_manual(DIR_WRFOUT, wrfout_file, start=None, end=None, save_file=True):
    """
    Processes the wrfout file -- calculates GHI and wind power denity (WPD) and writes these variables
    to wrfout_processed_d01.nc data file to be used by the regridding script (wrf2era_error.ncl) in
    wrf_era5_diff().

    This method makes use of two different packages that are not endogeneous to optwrf. The first is
    pvlib.wrfcast, which is a module for processing WRF output data that I have customized based on the
    pvlib.forecast model. The purpose of this is to eventually be able to use this method to calculate
    PV output from systems installed at any arbitrary location within your WRF model domain (this
    is not yet implemented). I have use this wrfcast module to calculate the GHI from WRF output data
    The second package is the wrf module maintained by NCAR, which reproduces some of the funcionality
    of NCL in Python. I use this to interpolate the wind speed to 100m.

    With the help of these two packages, the remaineder of the methods claculates the WPD, formats the
    data to be easily compatible with other methods, and writes the data to a NetCDF file.

    """

    # Absolute path to wrfout data file
    datapath = DIR_WRFOUT + wrfout_file

    # Read in the wrfout file using the netCDF4.Dataset method (I think you can also do this with an xarray method)
    netcdf_data = netCDF4.Dataset(datapath)

    # Create an xarray.Dataset from the wrf qurery_variables.
    query_variables = [
        'T2',
        'CLDFRA',
        'COSZEN',
        'SWDDNI',
        'SWDDIF',
        'height_agl',
        'wspd',
        'wdir',
    ]

    met_data = _wrf2xarray(netcdf_data, query_variables)

    variables = {
        'XLAT': 'XLAT',
        'XLONG': 'XLONG',
        'T2': 'temp_air',
        'CLDFRA': 'cloud_fraction',
        'COSZEN': 'cos_zenith',
        'SWDDNI': 'dni',
        'SWDDIF': 'dhi',
        'height_agl': 'height_agl',
        'wspd': 'wspd',
        'wdir': 'wdir',
    }

    # Rename the variables
    met_data = xr.Dataset.rename(met_data, variables)

    # Convert air temp from kelvin to celsius and calculate global horizontal irradiance
    # using the WRF forecast model methods from pvlib package
    fm = WRF()
    met_data['temp_air'] = fm.kelvin_to_celsius(met_data['temp_air'])
    met_data['ghi'] = fm.dni_and_dhi_to_ghi(met_data['dni'], met_data['dhi'], met_data['cos_zenith'])

    #  Interpolate wind speeds to 100m height
    met_data['wind_speed100'] = wrf.interplevel(met_data['wspd'], met_data['height_agl'], 100)

    # Calculate the wind power density
    met_data['wpd'] = calc_wpd(met_data['wind_speed100'])

    # Drop unnecessary coordinates
    met_data = xr.Dataset.reset_coords(met_data, ['XTIME', 'level'], drop=True)

    # Slice the wrfout data if start and end times ares specified
    if start and end is not None:
        met_data = met_data.sel(Time=slice(start, end))

    # Save the output file if specified.
    if save_file:
        # Write the processed data to a wrfout NetCDF file
        new_filename = DIR_WRFOUT + 'processed_' + wrfout_file
        met_data.to_netcdf(path=new_filename)
    else:
        return met_data
Ejemplo n.º 19
0
 def wrf_interp(level):
     result = interplevel(var, zz, level)
     return result
Ejemplo n.º 20
0
def get_data_vslice(data_path,
                    slice_width=1,
                    slice_index=0,
                    slice_z=None,
                    zres=None,
                    slice_type='vy',
                    t_start=0,
                    t_end=0,
                    force=False):

    if slice_type == 'vy' or slice_type == 'vx':
        file_name = data_path + '/wrfout_' + slice_type + '_slice_index_' + str(
            slice_index) + '_t_start_' + str(t_start) + '_t_end_' + str(
                t_end) + '.pkl'
    if slice_type == 'h':
        file_name = data_path + '/wrfout_' + slice_type + '_slice_z_' + str(
            slice_z) + '_t_start_' + str(t_start) + '_t_end_' + str(
                t_end) + '.pkl'

    if slice_type == 'h' and (slice_z is None or zres is None):
        print(
            'Error: Si se hace un corte horizontal hay que especificar slice_z y zres'
        )
        return

    #Vamos a guardar una cross section a x o y constantes. Si fijamos un x o un y, tomamos width puntos alrededor de ese x o y para poder calcular
    #derivadas espaciales centradas en el corte. La idea es tener todos los datos necesarios para poder hacer calculos sobre ese corte en particular (derivadas temporales y espaciales).

    if os.path.exists(file_name) & (not force):
        with open(file_name, 'rb') as handle:
            my_data = pickle.load(handle)

    else:

        my_data = dict()
        my_data['file_name'] = file_name
        my_data['times'] = []

        my_data['file_list'] = glob.glob(
            data_path +
            '/wrfout_d01_*')  #Busco todos los wrfout en la carpeta indicada.
        my_data['file_list'].sort()

        #Leo el primer archivo de la lista de donde voy a tomar el perfil de referencia.
        ncfile = Dataset(my_data['file_list'][0])
        tmp = to_np(getvar(ncfile, "P"))
        nx = tmp.shape[2]  #Order is Z=0 , Y=1 , X=0
        ny = tmp.shape[1]
        nz = tmp.shape[0]
        nt = len(my_data['file_list'])

        if (t_start != 0 or t_end != 0) and (t_end > t_start):
            ts = t_start
            te = t_end
        else:
            ts = 0
            te = nt

        if slice_type == 'vy' or slice_type == 'vx':  #Vertical slice
            if slice_type == 'vy':
                ys = slice_index - slice_width
                ye = slice_index + slice_width + 1
                xs = 0
                xe = nx
            if slice_type == 'vx':
                xs = slice_index - slice_width
                xe = slice_index + slice_width + 1
                ys = 0
                ye = ny

            zs = 0
            ze = nz
            snz = ze - zs

        if slice_type == 'h':  #Horizontal slice
            ys = 0
            ye = ny
            xs = 0
            xe = nx
            snz = 2 * slice_width + 1

        snx = xe - xs
        sny = ye - ys
        snt = te - ts

        my_data['nx'] = snx
        my_data['ny'] = sny
        my_data['nz'] = snz
        my_data['nt'] = snt

        #Allocate memory
        my_data['p'] = np.zeros((snz, sny, snx, snt))
        my_data['u'] = np.zeros((snz, sny, snx, snt))
        my_data['v'] = np.zeros((snz, sny, snx, snt))
        my_data['w'] = np.zeros((snz, sny, snx, snt))
        my_data['ref'] = np.zeros((snz, sny, snx, snt))
        my_data['t'] = np.zeros((snz, sny, snx, snt))
        my_data['qv'] = np.zeros((snz, sny, snx, snt))
        my_data['qc'] = np.zeros((snz, sny, snx, snt))
        my_data['qr'] = np.zeros((snz, sny, snx, snt))
        my_data['qi'] = np.zeros((snz, sny, snx, snt))
        my_data['qs'] = np.zeros((snz, sny, snx, snt))
        my_data['qg'] = np.zeros((snz, sny, snx, snt))
        my_data['z'] = np.zeros((snz, sny, snx, snt))
        my_data['thetae'] = np.zeros((snz, sny, snx, snt))
        my_data['theta'] = np.zeros((snz, sny, snx, snt))
        my_data['rh'] = np.zeros((snz, sny, snx, snt))
        my_data['h_diabatic'] = np.zeros((snz, sny, snx, snt))
        my_data['qv_diabatic'] = np.zeros((snz, sny, snx, snt))

        my_data['file_list'] = my_data['file_list'][ts:te]
        #Leo los datos y los guardo en el diccionario del corte.
        for itime in range(ts, te):
            ctime = itime - ts
            print('Reading file ' + my_data['file_list'][ctime])
            ncfile = Dataset(my_data['file_list'][ctime])
            if slice_type == 'vy' or slice_type == 'vx':  #Vertical slice
                my_data['p'][:, :, :,
                             ctime] = to_np(getvar(ncfile, "pres",
                                                   units='Pa'))[zs:ze, ys:ye,
                                                                xs:xe]
                my_data['u'][:, :, :,
                             ctime] = to_np(getvar(ncfile, "ua",
                                                   units='ms-1'))[zs:ze, ys:ye,
                                                                  xs:xe]
                my_data['v'][:, :, :,
                             ctime] = to_np(getvar(ncfile, "va",
                                                   units='ms-1'))[zs:ze, ys:ye,
                                                                  xs:xe]
                my_data['w'][:, :, :,
                             ctime] = to_np(getvar(ncfile, "wa",
                                                   units='ms-1'))[zs:ze, ys:ye,
                                                                  xs:xe]
                my_data['ref'][:, :, :,
                               ctime] = to_np(getvar(ncfile,
                                                     "dbz"))[zs:ze, ys:ye,
                                                             xs:xe]
                my_data['t'][:, :, :,
                             ctime] = to_np(getvar(ncfile, "temp",
                                                   units='K'))[zs:ze, ys:ye,
                                                               xs:xe]
                my_data['qv'][:, :, :,
                              ctime] = to_np(getvar(ncfile,
                                                    "QVAPOR"))[zs:ze, ys:ye,
                                                               xs:xe]
                my_data['qc'][:, :, :,
                              ctime] = to_np(getvar(ncfile,
                                                    "QCLOUD"))[zs:ze, ys:ye,
                                                               xs:xe]
                my_data['qr'][:, :, :,
                              ctime] = to_np(getvar(ncfile,
                                                    "QRAIN"))[zs:ze, ys:ye,
                                                              xs:xe]
                my_data['qi'][:, :, :,
                              ctime] = to_np(getvar(ncfile,
                                                    "QICE"))[zs:ze, ys:ye,
                                                             xs:xe]
                my_data['qs'][:, :, :,
                              ctime] = to_np(getvar(ncfile,
                                                    "QSNOW"))[zs:ze, ys:ye,
                                                              xs:xe]
                my_data['qg'][:, :, :,
                              ctime] = to_np(getvar(ncfile,
                                                    "QGRAUP"))[zs:ze, ys:ye,
                                                               xs:xe]
                my_data['z'][:, :, :,
                             ctime] = to_np(getvar(ncfile, "height",
                                                   units='m'))[zs:ze, ys:ye,
                                                               xs:xe]
                my_data['thetae'][:, :, :, ctime] = to_np(
                    getvar(ncfile, "theta_e", units='K'))[zs:ze, ys:ye, xs:xe]
                my_data['theta'][:, :, :, ctime] = to_np(
                    getvar(ncfile, "theta", units='K'))[zs:ze, ys:ye, xs:xe]
                my_data['rh'][:, :, :,
                              ctime] = to_np(getvar(ncfile,
                                                    "rh"))[zs:ze, ys:ye, xs:xe]
                try:
                    my_data['h_diabatic'][:, :, :, ctime] = to_np(
                        getvar(ncfile, "H_DIABATIC"))[zs:ze, ys:ye, xs:xe]
                    my_data['qv_diabatic'][:, :, :, ctime] = to_np(
                        getvar(ncfile, "QV_DIABATIC"))[zs:ze, ys:ye, xs:xe]
                except:
                    print('Warning no diabatic data found')
                my_data['times'].append(
                    dt.datetime.strptime(
                        os.path.basename(my_data['file_list'][ctime])[11:],
                        '%Y-%m-%d_%H:%M:%S'))
            if slice_type == 'h':  #Horizontal slice at constant height
                z_levels = np.arange(slice_z - slice_width * zres,
                                     slice_z + 2.0 * slice_width * zres, zres)

                z_ = getvar(ncfile, "height", units='m')
                p_ = getvar(ncfile, "pres", units='Pa')
                u_ = getvar(ncfile, "ua", units='ms-1')
                v_ = getvar(ncfile, "va", units='ms-1')
                w_ = getvar(ncfile, "wa", units='ms-1')
                ref_ = getvar(ncfile, "dbz")
                t_ = getvar(ncfile, "temp", units='K')
                qv_ = getvar(ncfile, "QVAPOR")
                qc_ = getvar(ncfile, "QCLOUD")
                qr_ = getvar(ncfile, "QRAIN")
                qi_ = getvar(ncfile, "QICE")
                qs_ = getvar(ncfile, "QSNOW")
                qg_ = getvar(ncfile, "QGRAUP")
                thetae_ = getvar(ncfile, "theta_e", units='K')
                theta_ = getvar(ncfile, "theta", units='K')
                rh_ = getvar(ncfile, "rh")
                try:
                    h_diabatic_ = getvar(ncfile, "H_DIABATIC")
                    qv_diabatic_ = getvar(ncfile, "QV_DIABATIC")
                    my_data['h_diabatic'][:, :, :, ctime] = interplevel(
                        h_diabatic_, z_, z_levels)
                    my_data['qv_diabatic'][:, :, :, ctime] = interplevel(
                        qv_diabatic_, z_, z_levels)
                except:
                    print('Warning no diabatic data found')
                my_data['p'][:, :, :, ctime] = interplevel(p_, z_, z_levels)
                my_data['u'][:, :, :, ctime] = interplevel(u_, z_, z_levels)
                my_data['v'][:, :, :, ctime] = interplevel(v_, z_, z_levels)
                my_data['w'][:, :, :, ctime] = interplevel(w_, z_, z_levels)
                my_data['ref'][:, :, :,
                               ctime] = interplevel(ref_, z_, z_levels)
                my_data['t'][:, :, :, ctime] = interplevel(t_, z_, z_levels)
                my_data['qv'][:, :, :, ctime] = interplevel(qv_, z_, z_levels)
                my_data['qc'][:, :, :, ctime] = interplevel(qc_, z_, z_levels)
                my_data['qr'][:, :, :, ctime] = interplevel(qr_, z_, z_levels)
                my_data['qi'][:, :, :, ctime] = interplevel(qi_, z_, z_levels)
                my_data['qs'][:, :, :, ctime] = interplevel(qs_, z_, z_levels)
                my_data['qg'][:, :, :, ctime] = interplevel(qg_, z_, z_levels)
                my_data['thetae'][:, :, :,
                                  ctime] = interplevel(thetae_, z_, z_levels)
                my_data['theta'][:, :, :,
                                 ctime] = interplevel(theta_, z_, z_levels)
                my_data['rh'][:, :, :, ctime] = interplevel(rh_, z_, z_levels)

                my_data['times'].append(
                    dt.datetime.strptime(
                        os.path.basename(my_data['file_list'][ctime])[11:],
                        '%Y-%m-%d_%H:%M:%S'))

        if slice_type == 'h':
            for iz, my_z in enumerate(z_levels):
                my_data['z'][iz, :, :, :] = my_z

        if nt == 1:
            my_data['dt'] = 1.0
        else:
            my_data['dt'] = (my_data['times'][1] - my_data['times'][0]).seconds
        my_data['dx'] = ncfile.DX
        my_data['dy'] = ncfile.DY
        my_data['file_name'] = file_name
        my_data['slice_type'] = slice_type
        my_data['slice_index'] = slice_index
        my_data['slice_width'] = slice_width
        my_data['slice_z'] = slice_z
        my_data['zres'] = zres
        my_data['ts'] = ts
        my_data['te'] = te

        my_data['p'] = np.float32(my_data['p'])
        my_data['u'] = np.float32(my_data['u'])
        my_data['v'] = np.float32(my_data['v'])
        my_data['w'] = np.float32(my_data['w'])
        my_data['ref'] = np.float32(my_data['ref'])
        my_data['t'] = np.float32(my_data['t'])
        my_data['qv'] = np.float32(my_data['qv'])
        my_data['qc'] = np.float32(my_data['qc'])
        my_data['qr'] = np.float32(my_data['qr'])
        my_data['qi'] = np.float32(my_data['qi'])
        my_data['qs'] = np.float32(my_data['qs'])
        my_data['qg'] = np.float32(my_data['qg'])
        my_data['z'] = np.float32(my_data['z'])
        my_data['thetae'] = np.float32(my_data['thetae'])
        my_data['theta'] = np.float32(my_data['theta'])
        my_data['rh'] = np.float32(my_data['rh'])
        my_data['h_diabatic'] = np.float32(my_data['h_diabatic'])
        my_data['qv_diabatic'] = np.float32(my_data['qv_diabatic'])

        with open(file_name, 'wb') as handle:
            pickle.dump(my_data, handle, protocol=pickle.HIGHEST_PROTOCOL)

    return my_data
Ejemplo n.º 21
0
############# ESPECIFICAR EL CAMPO DE PRESION QUE SE QUIERA #############

nivel = 700 

# SE EXTRAEN LAS VARIABLES A UTILIZAR
# SE HACE UN CICLO PARA TODOS LOS TIEMPOS DEL WRF_FILE

for tidx in range(0, tpos) :       # len(tpos)  
        p = getvar(wrf_file, "pressure", tidx)  # presion
        z = getvar(wrf_file, "z", tidx, units = 'm')     # geopotencial
        w = getvar(wrf_file, "wa", tidx, units="m s-1")   # vel vertical
        ter = getvar(wrf_file, "ter", tidx)     # terreno alturas

        # Se interpolan las variables al campo de presion correspondiente para podee graficarlas 
        w = interplevel(w, p, nivel)

        # Se extraen lat-lon
        lats, lons = latlon_coords(p)

        # Se extrae info de la proyeccion 
        cart_proj = get_cartopy(p)
        
############################################## GRAFICADO ############################################## 
        
        fig = plt.figure(figsize=(12,9))
        ax = plt.axes(projection=cart_proj)

        # Aniadiendo los sombreados
        #levels = [25, 30, 35, 40, 50, 60, 70, 80, 90, 100, 110, 120]
        levels = np.arange(-1.5, 1.5, 0.25)
Ejemplo n.º 22
0
    def set_height(self, h=1000, update=True):
        """
            h (float) :: height in metres.
        """

        if np.any(self.Xsmooth == None):
            print(
                "Field has not been smoothed. Turbulence cannot be calculated."
            )

        if h == None:
            h = 1000

        if self.isvertical == False:
            print(
                "Variable not defined on vertical layers. Not interpolating.")
            self.Xinterp = self.X
            if not np.any(self.Xsmooth == None):
                self.Xsmoothinterp = self.Xsmooth
        else:
            zmin = np.array(wrf.to_np(np.min(np.max(self.z, axis=(2, 3)))))
            zmax = np.array(wrf.to_np(np.max(np.min(self.z, axis=(2, 3)))))

            if (h < zmin) or (h > zmax):
                print("Height = " + str(h) + \
                        "\nMay produce missing values if not between " + str(zmin) + " and " + str(zmax))

            print("Interpolating " + self.str_id + " onto height " + str(h) +
                  "...")

            self.Xinterp = wrf.interplevel(self.X, self.z, h)

            if not np.any(self.Xsmooth == None):
                self.Xsmoothinterp = wrf.interplevel(self.Xsmooth, self.z, h)

            print("Done.")

            nans = np.sum(np.isnan(np.array(wrf.to_np(self.Xinterp))))
            if nans > 0:
                print("Warning: Field contains " + str(nans) + " NaNs.")
            elif nans == np.size(self.Xinterp):
                print("Warning: Field only contains NaNs.")
            else:
                print("Field contains no NaNs.")
            self.h = h

        if not (np.any(self.Xsmoothinterp == None)
                or np.any(self.Xinterp == None)):
            self.dX = np.array(wrf.to_np(self.Xinterp)) - np.array(
                wrf.to_np(self.Xsmoothinterp))
            self.turb_ratiointerp = self.dX / self.Xsmoothinterp

        if self.derived:
            self.U.set_height(h, update=False)
            self.V.set_height(h, update=False)

        if update:
            self.update(2)
            self.update(3)

        if self.autoplot:
            plot_field(self)

        return
Ejemplo n.º 23
0
# Open the NetCDF file
filename = files_path + '/' + req_year + req_month + req_day + dir_hour\
                + '/wrfout_d01_' + req_year + '-' + req_month + '-' + req_day + '_' + req_hour + '_00_00'
ncfile = Dataset(filename)

######################################################################################################
# pressure levels interpolation
######################################################################################################

# Extract the pressure, geopotential height, and wind variables
# p = getvar(ncfile, "pressure")
p = getvar(ncfile, "pressure")
LWC = getvar(ncfile, "QCLOUD")  #, meta=False)

# Interpolate geopotential height
LWC_750 = interplevel(LWC, p, 750)

# Get the lat/lon coordinates
lats, lons = latlon_coords(LWC_750)

# Get the basemap object
bm = get_basemap(LWC_750)

# Create the figure
fig = plt.figure(figsize=(12, 9))
ax = plt.axes()

# Convert the lat/lon coordinates to x/y coordinates in the projection space
x, y = bm(to_np(lons), to_np(lats))

# Add the 500 hPa geopotential height contours
Ejemplo n.º 24
0
    def test(self):
        try:
            from netCDF4 import Dataset as NetCDF
        except:
            pass

        try:
            from PyNIO import Nio
        except:
            pass

        if not multi:
            timeidx = 0
            if not pynio:
                in_wrfnc = NetCDF(wrf_in)
            else:
                # Note: Python doesn't like it if you reassign an outer scoped
                # variable (wrf_in in this case)
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                in_wrfnc = Nio.open_file(wrf_file)
        else:
            timeidx = None
            if not pynio:
                nc = NetCDF(wrf_in)
                in_wrfnc = [nc for i in xrange(repeat)]
            else:
                if not wrf_in.endswith(".nc"):
                    wrf_file = wrf_in + ".nc"
                else:
                    wrf_file = wrf_in
                nc = Nio.open_file(wrf_file)
                in_wrfnc = [nc for i in xrange(repeat)]

        if (varname == "interplevel"):
            ref_ht_500 = _get_refvals(referent, "z_500", repeat, multi)
            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)
            hts_500 = interplevel(hts, p, 500)

            nt.assert_allclose(to_np(hts_500), ref_ht_500)

        elif (varname == "vertcross"):
            ref_ht_cross = _get_refvals(referent, "ht_cross", repeat, multi)
            ref_p_cross = _get_refvals(referent, "p_cross", repeat, multi)

            hts = getvar(in_wrfnc, "z", timeidx=timeidx)
            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)

            pivot_point = CoordPair(hts.shape[-1] / 2, hts.shape[-2] / 2)
            ht_cross = vertcross(hts, p, pivot_point=pivot_point, angle=90.)

            nt.assert_allclose(to_np(ht_cross), ref_ht_cross, rtol=.01)

            # Test opposite
            p_cross1 = vertcross(p, hts, pivot_point=pivot_point, angle=90.0)

            nt.assert_allclose(to_np(p_cross1), ref_p_cross, rtol=.01)
            # Test point to point
            start_point = CoordPair(0, hts.shape[-2] / 2)
            end_point = CoordPair(-1, hts.shape[-2] / 2)

            p_cross2 = vertcross(p,
                                 hts,
                                 start_point=start_point,
                                 end_point=end_point)

            nt.assert_allclose(to_np(p_cross1), to_np(p_cross2))

        elif (varname == "interpline"):

            ref_t2_line = _get_refvals(referent, "t2_line", repeat, multi)

            t2 = getvar(in_wrfnc, "T2", timeidx=timeidx)
            pivot_point = CoordPair(t2.shape[-1] / 2, t2.shape[-2] / 2)

            t2_line1 = interpline(t2, pivot_point=pivot_point, angle=90.0)

            nt.assert_allclose(to_np(t2_line1), ref_t2_line)

            # Test point to point
            start_point = CoordPair(0, t2.shape[-2] / 2)
            end_point = CoordPair(-1, t2.shape[-2] / 2)

            t2_line2 = interpline(t2,
                                  start_point=start_point,
                                  end_point=end_point)

            nt.assert_allclose(to_np(t2_line1), to_np(t2_line2))
        elif (varname == "vinterp"):
            # Tk to theta
            fld_tk_theta = _get_refvals(referent, "fld_tk_theta", repeat,
                                        multi)
            fld_tk_theta = np.squeeze(fld_tk_theta)

            tk = getvar(in_wrfnc, "temp", timeidx=timeidx, units="k")

            interp_levels = [200, 300, 500, 1000]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 5 / 100.
            atol = 0.0001

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_theta)))
            nt.assert_allclose(to_np(field), fld_tk_theta, tol, atol)

            # Tk to theta-e
            fld_tk_theta_e = _get_refvals(referent, "fld_tk_theta_e", repeat,
                                          multi)
            fld_tk_theta_e = np.squeeze(fld_tk_theta_e)

            interp_levels = [200, 300, 500, 1000]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="theta-e",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 3 / 100.
            atol = 50.0001

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_theta_e)/fld_tk_theta_e)*100)
            nt.assert_allclose(to_np(field), fld_tk_theta_e, tol, atol)

            # Tk to pressure
            fld_tk_pres = _get_refvals(referent, "fld_tk_pres", repeat, multi)
            fld_tk_pres = np.squeeze(fld_tk_pres)

            interp_levels = [850, 500]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            #print (np.amax(np.abs(to_np(field) - fld_tk_pres)))
            nt.assert_allclose(to_np(field), fld_tk_pres, tol, atol)

            # Tk to geoht_msl
            fld_tk_ght_msl = _get_refvals(referent, "fld_tk_ght_msl", repeat,
                                          multi)
            fld_tk_ght_msl = np.squeeze(fld_tk_ght_msl)
            interp_levels = [1, 2]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="ght_msl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_ght_msl)))
            nt.assert_allclose(to_np(field), fld_tk_ght_msl, tol, atol)

            # Tk to geoht_agl
            fld_tk_ght_agl = _get_refvals(referent, "fld_tk_ght_agl", repeat,
                                          multi)
            fld_tk_ght_agl = np.squeeze(fld_tk_ght_agl)
            interp_levels = [1, 2]

            field = vinterp(in_wrfnc,
                            field=tk,
                            vert_coord="ght_agl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_tk_ght_agl)))
            nt.assert_allclose(to_np(field), fld_tk_ght_agl, tol, atol)

            # Hgt to pressure
            fld_ht_pres = _get_refvals(referent, "fld_ht_pres", repeat, multi)
            fld_ht_pres = np.squeeze(fld_ht_pres)

            z = getvar(in_wrfnc, "height", timeidx=timeidx, units="m")
            interp_levels = [500, 50]
            field = vinterp(in_wrfnc,
                            field=z,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="ght",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_ht_pres)))
            nt.assert_allclose(to_np(field), fld_ht_pres, tol, atol)

            # Pressure to theta
            fld_pres_theta = _get_refvals(referent, "fld_pres_theta", repeat,
                                          multi)
            fld_pres_theta = np.squeeze(fld_pres_theta)

            p = getvar(in_wrfnc, "pressure", timeidx=timeidx)
            interp_levels = [200, 300, 500, 1000]
            field = vinterp(in_wrfnc,
                            field=p,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="pressure",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_pres_theta)))
            nt.assert_allclose(to_np(field), fld_pres_theta, tol, atol)

            # Theta-e to pres
            fld_thetae_pres = _get_refvals(referent, "fld_thetae_pres", repeat,
                                           multi)
            fld_thetae_pres = np.squeeze(fld_thetae_pres)

            eth = getvar(in_wrfnc, "eth", timeidx=timeidx)
            interp_levels = [850, 500, 5]
            field = vinterp(in_wrfnc,
                            field=eth,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="theta-e",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            #print (np.amax(np.abs(to_np(field) - fld_thetae_pres)))
            nt.assert_allclose(to_np(field), fld_thetae_pres, tol, atol)
Ejemplo n.º 25
0
def main(args):
    fname = args.file
    save_file = args.save_file

    # List of variables that are already included in the WRF output and that we want to compute using the wrf-python
    variables = dict(primary=[
        'XLAT', 'XLONG', 'T2', 'SWDOWN', 'LWUPB', 'GLW', 'PSFC', 'RAINC',
        'RAINNC', 'RAINSH', 'SNOWNC', 'SST', 'DIFFUSE_FRAC', 'LANDMASK',
        'LAKEMASK', 'PBLH', 'TSK', 'UST'
    ],
                     computed=['rh2', 'slp', 'mdbz'])

    # Generate height table for interpolation of U and V components
    gen_heights = [
        20, 320, 20
    ]  # minimum height, maximum height, distance between heights

    # Output time units
    time_units = 'seconds since 1970-01-01 00:00:00'

    os.makedirs(os.path.dirname(save_file), exist_ok=True)
    # create_dir(os.path.dirname(save_file))

    # Create list of heights between min and max height separated by a stride value defined above
    heights = list(np.arange(gen_heights[0], gen_heights[1], gen_heights[2]))
    heights.append(gen_heights[1])

    # Open using netCDF toolbox
    ncfile = xr.open_dataset(fname)
    original_global_attributes = ncfile.attrs
    ncfile = ncfile._file_obj.ds

    # Load primary variables and append to list
    primary_vars = {}
    for var in variables['primary']:
        primary_vars[var] = cf.delete_attr(getvar(ncfile, var))

    # Calculate diagnostic variables defined above and add to dictionary
    diagnostic_vars = {}
    for dvar in variables['computed']:
        diagnostic_vars[dvar.upper()] = cf.delete_attr(getvar(ncfile, dvar))

    # Subtract terrain height from height above sea level
    new_z = getvar(ncfile, 'z') - getvar(ncfile, 'ter')

    # Calculate u and v components of wind rotated to Earth coordinates
    uvm = getvar(ncfile, 'uvmet')

    # interpolate u and v components of wind to 0-200m by 10m
    uvtemp = interplevel(uvm, new_z, heights, default_fill(np.float32))
    uvtemp = uvtemp.rename({'level': 'height'})
    utemp, vtemp = cf.split_uvm(uvtemp)

    # Calculate 10m u and v components of wind rotated to Earth coordinates and split into separate variables
    primary_vars['U10'], primary_vars['V10'] = cf.split_uvm(
        getvar(ncfile, 'uvmet10'))

    # Concatenate the list of calculated u and v values into data array. Append to diagnostic_vars list
    diagnostic_vars['U'] = xr.concat(utemp, dim='height')
    diagnostic_vars['V'] = xr.concat(vtemp, dim='height')

    # Interpolate u and v components of wind to Boundary Layer Heights for wind gust calculation
    uvpblh = interplevel(uvm, new_z, primary_vars['PBLH'])
    uvpblh = cf.delete_attr(uvpblh).drop(['u_v'
                                          ])  # drop unnecessary attributes
    upblh = uvpblh[0].rename('upblh')
    vpblh = uvpblh[1].rename('vpblh')

    # Calculate wind gust
    sfcwind = np.sqrt(primary_vars['U10']**2 + primary_vars['V10']**2)
    pblwind = np.sqrt(upblh**2 + vpblh**2)
    delwind = pblwind - sfcwind
    pblval = primary_vars['PBLH'] / 2000
    pblval = pblval.where(
        pblval < 0.5, other=0.5
    )  # if the value is less than 0.5, keep it. otherwise, change to 0.5
    delwind = delwind * (1 - pblval)
    gust = sfcwind + delwind
    gust = gust.drop('level')  # drop the 'level' coordinate
    gust = gust.astype(np.float32)

    # Create xarray dataset of primary and diagnostic variables
    ds = xr.Dataset({**primary_vars, **diagnostic_vars})
    ds['U'] = ds.U.astype(np.float32)
    ds['V'] = ds.V.astype(np.float32)
    ds['height'] = ds.height.astype(np.int32)

    try:
        del ds.U.attrs['vert_units']
        del ds.V.attrs['vert_units']
    except KeyError:
        pass

    ds['Times'] = np.array([
        pd.Timestamp(ds.Time.data).strftime('%Y-%m-%d_%H:%M:%S')
    ]).astype('<S19')
    ds = ds.expand_dims('Time', axis=0)

    # Add description and units for lon, lat dimensions for georeferencing
    ds['XLAT'].attrs['description'] = 'latitude'
    ds['XLAT'].attrs['units'] = 'degree_north'
    ds['XLONG'].attrs['description'] = 'longitude'
    ds['XLONG'].attrs['units'] = 'degree_east'

    # Set XTIME attribute
    ds['XTIME'].attrs['units'] = 'minutes'

    # Set lon attributes
    ds['XLONG'].attrs['long_name'] = 'Longitude'
    ds['XLONG'].attrs['standard_name'] = 'longitude'
    ds['XLONG'].attrs['short_name'] = 'lon'
    ds['XLONG'].attrs['units'] = 'degrees_east'
    ds['XLONG'].attrs['axis'] = 'X'
    ds['XLONG'].attrs['valid_min'] = np.float32(-180.0)
    ds['XLONG'].attrs['valid_max'] = np.float32(180.0)

    # Set lat attributes
    ds['XLAT'].attrs['long_name'] = 'Latitude'
    ds['XLAT'].attrs['standard_name'] = 'latitude'
    ds['XLAT'].attrs['short_name'] = 'lat'
    ds['XLAT'].attrs['units'] = 'degrees_north'
    ds['XLAT'].attrs['axis'] = 'Y'
    ds['XLAT'].attrs['valid_min'] = np.float32(-90.0)
    ds['XLAT'].attrs['valid_max'] = np.float32(90.0)

    # Set depth attributes
    ds['height'].attrs['long_name'] = 'Height Above Ground Level'
    ds['height'].attrs['standard_name'] = 'height'
    ds['height'].attrs[
        'comment'] = 'Derived from subtracting terrain height from height above sea level'
    ds['height'].attrs['units'] = 'm'
    ds['height'].attrs['axis'] = 'Z'
    ds['height'].attrs['positive'] = 'up'

    # Set u attributes
    ds['U'].attrs['long_name'] = 'Eastward Wind Component'
    ds['U'].attrs['standard_name'] = 'eastward_wind'
    ds['U'].attrs['short_name'] = 'u'
    ds['U'].attrs['units'] = 'm s-1'
    ds['U'].attrs['description'] = 'earth rotated u'
    ds['U'].attrs['valid_min'] = np.float32(-300)
    ds['U'].attrs['valid_max'] = np.float32(300)

    # Set v attributes
    ds['V'].attrs['long_name'] = 'Northward Wind Component'
    ds['V'].attrs['standard_name'] = 'northward_wind'
    ds['V'].attrs['short_name'] = 'v'
    ds['V'].attrs['units'] = 'm s-1'
    ds['V'].attrs['description'] = 'earth rotated v'
    ds['V'].attrs['valid_min'] = np.float32(-300)
    ds['V'].attrs['valid_max'] = np.float32(300)

    # Set u10 attributes
    ds['U10'].attrs['long_name'] = 'Eastward Wind Component - 10m'
    ds['U10'].attrs['standard_name'] = 'eastward_wind'
    ds['U10'].attrs['short_name'] = 'u'
    ds['U10'].attrs['units'] = 'm s-1'
    ds['U10'].attrs['description'] = '10m earth rotated u'
    ds['U10'].attrs['valid_min'] = np.float32(-300)
    ds['U10'].attrs['valid_max'] = np.float32(300)

    # Set v10 attributes
    ds['V10'].attrs['long_name'] = 'Northward Wind Component - 10m'
    ds['V10'].attrs['standard_name'] = 'northward_wind'
    ds['V10'].attrs['short_name'] = 'v'
    ds['V10'].attrs['units'] = 'm s-1'
    ds['V10'].attrs['description'] = '10m earth rotated v'
    ds['V10'].attrs['valid_min'] = np.float32(-300)
    ds['V10'].attrs['valid_max'] = np.float32(300)

    # set primary attributes
    ds['GLW'].attrs[
        'standard_name'] = 'surface_downwelling_longwave_flux_in_air'
    ds['GLW'].attrs['long_name'] = 'Surface Downwelling Longwave Flux'

    ds['LWUPB'].attrs['standard_name'] = 'surface_upwelling_longwave_flux'
    ds['LWUPB'].attrs['long_name'] = 'Surface Upwelling Longwave Flux'

    ds['PSFC'].attrs['standard_name'] = 'surface_air_pressure'
    ds['PSFC'].attrs['long_name'] = 'Air Pressure at Surface'

    ds['RH2'].attrs['standard_name'] = 'relative_humidity'
    ds['RH2'].attrs['long_name'] = 'Relative Humidity'

    ds['SLP'].attrs['standard_name'] = 'air_pressure_at_sea_level'
    ds['SLP'].attrs['long_name'] = 'Air Pressure at Sea Level'

    ds['SWDOWN'].attrs[
        'standard_name'] = 'surface_downwelling_shortwave_flux_in_air'
    ds['SWDOWN'].attrs['long_name'] = 'Surface Downwelling Shortwave Flux'

    ds['T2'].attrs['standard_name'] = 'air_temperature'
    ds['T2'].attrs['long_name'] = 'Air Temperature at 2m'

    ds['RAINC'].attrs['long_name'] = 'Accumulated Total Cumulus Precipitation'
    ds['RAINNC'].attrs[
        'long_name'] = 'Accumulated Total Grid Scale Precipitation'
    ds['RAINSH'].attrs[
        'long_name'] = 'Accumulated Shallow Cumulus Precipitation'

    ds['SNOWNC'].attrs['standard_name'] = 'surface_snow_thickness'
    ds['SNOWNC'].attrs[
        'long_name'] = 'Accumulated Total Grid Scale Snow and Ice'
    ds['SNOWNC'].attrs['description'] = '{}; water equivalent'.format(
        ds['SNOWNC'].description)

    ds['SST'].attrs['standard_name'] = 'sea_surface_temperature'
    ds['SST'].attrs['long_name'] = 'Sea Surface Temperature'

    ds['DIFFUSE_FRAC'].attrs[
        'long_name'] = 'Diffuse Fraction of Surface Shortwave Irradiance'

    ds['MDBZ'].attrs['long_name'] = 'Maximum Radar Reflectivity'

    ds['TSK'].attrs['long_name'] = 'Surface Skin Temperature'

    ds['LANDMASK'].attrs['standard_name'] = 'land_binary_mask'
    ds['LANDMASK'].attrs['long_name'] = 'Land Mask'

    ds['LAKEMASK'].attrs['long_name'] = 'Lake Mask'

    ds['PBLH'].attrs[
        'long_name'] = 'Height of the Top of the Planetary Boundary Layer (PBL)'

    ds['UST'].attrs['long_name'] = 'Friction Velocity'

    ds['XTIME'].attrs['long_name'] = 'minutes since simulation start'

    # add the calculated wind gust to the dataset
    windgust_attrs = dict(
        long_name='Near Surface Wind Gust',
        description=
        'Calculated wind gust, computed by mixing down momentum from the level at the '
        'top of the planetary boundary layer',
        units='m s-1')
    ds['WINDGUST'] = xr.Variable(gust.dims, gust.values, attrs=windgust_attrs)
    ds['WINDGUST'] = ds['WINDGUST'].expand_dims('Time', axis=0)

    # Set time attribute
    ds['Time'].attrs['standard_name'] = 'time'

    datetime_format = '%Y%m%dT%H%M%SZ'
    created = pd.Timestamp(pd.datetime.utcnow()).strftime(
        datetime_format)  # creation time Timestamp
    time_start = pd.Timestamp(pd.Timestamp(
        ds.Time.data[0])).strftime(datetime_format)
    time_end = pd.Timestamp(pd.Timestamp(
        ds.Time.data[0])).strftime(datetime_format)
    global_attributes = OrderedDict([
        ('title', 'Rutgers Weather Research and Forecasting Model'),
        ('summary', 'Processed netCDF containing subset of RUWRF output'),
        ('keywords', 'Weather Advisories > Marine Weather/Forecast'),
        ('Conventions', 'CF-1.7'),
        ('naming_authority', 'edu.rutgers.marine.rucool'),
        ('history',
         'Hourly WRF raw output processed into new hourly file with selected variables.'
         ), ('processing_level', 'Level 2'),
        ('comment', 'WRF Model operated by RUCOOL'),
        ('acknowledgement',
         'This data is provided by the Rutgers Center for Ocean Observing Leadership. Funding is provided by the New Jersey Board of Public Utilities).'
         ), ('standard_name_vocabulary', 'CF Standard Name Table v41'),
        ('date_created', created), ('creator_name', 'Joseph Brodie'),
        ('creator_email', '*****@*****.**'),
        ('creator_url', 'rucool.marine.rutgers.edu'),
        ('institution',
         'Center for Ocean Observing and Leadership, Department of Marine & Coastal Sciences, Rutgers University'
         ),
        ('project',
         'New Jersey Board of Public Utilities - Offshore Wind Energy - RUWRF Model'
         ), ('geospatial_lat_min', -90), ('geospatial_lat_max', 90),
        ('geospatial_lon_min', -180), ('geospatial_lon_max', 180),
        ('geospatial_vertical_min', 0.0), ('geospatial_vertical_max', 0.0),
        ('geospatial_vertical_positive', 'down'),
        ('time_coverage_start', time_start), ('time_coverage_end', time_end),
        ('creator_type', 'person'),
        ('creator_institution', 'Rutgers University'),
        ('contributor_name', 'Joseph Brodie'),
        ('contributor_role', 'Director of Atmospheric Research'),
        ('geospatial_lat_units', 'degrees_north'),
        ('geospatial_lon_units', 'degrees_east'), ('date_modified', created),
        ('date_issued', created), ('date_metadata_modified', created),
        ('keywords_vocabulary', 'GCMD Science Keywords'),
        ('platform', 'WRF Model Run'), ('cdm_data_type', 'Grid'),
        ('references',
         'http://maracoos.org/node/146 https://rucool.marine.rutgers.edu/facilities https://rucool.marine.rutgers.edu/data'
         )
    ])

    global_attributes.update(original_global_attributes)
    ds = ds.assign_attrs(global_attributes)

    # Add compression to all variables
    encoding = {}
    for k in ds.data_vars:
        encoding[k] = {'zlib': True, 'complevel': 1}

    # add the encoding for time so xarray exports the proper time.
    # Also remove compression from dimensions. They should never have fill values
    encoding['Time'] = dict(units=time_units,
                            calendar='gregorian',
                            zlib=False,
                            _FillValue=False,
                            dtype=np.double)
    encoding['XLONG'] = dict(zlib=False, _FillValue=False)
    encoding['XLAT'] = dict(zlib=False, _FillValue=False)
    encoding['height'] = dict(zlib=False, _FillValue=False, dtype=np.int32)

    ds.to_netcdf(save_file,
                 encoding=encoding,
                 format='netCDF4',
                 engine='netcdf4',
                 unlimited_dims='Time')
Ejemplo n.º 26
0
    def test(self):
        try:
            from netCDF4 import Dataset as NetCDF
        except ImportError:
            pass

        try:
            import Nio
        except ImportError:
            pass

        timeidx = 0 if not multi else None
        pat = os.path.join(dir, pattern)
        wrf_files = glob.glob(pat)
        wrf_files.sort()

        wrfin = []
        for fname in wrf_files:
            if not pynio:
                f = NetCDF(fname)
                try:
                    f.set_always_mask(False)
                except AttributeError:
                    pass
                wrfin.append(f)
            else:
                if not fname.endswith(".nc"):
                    _fname = fname + ".nc"
                else:
                    _fname = fname
                f = Nio.open_file(_fname)
                wrfin.append(f)

        if (varname == "interplevel"):
            ref_ht_500 = _get_refvals(referent, "z_500", multi)
            ref_p_5000 = _get_refvals(referent, "p_5000", multi)
            ref_ht_multi = _get_refvals(referent, "z_multi", multi)
            ref_p_multi = _get_refvals(referent, "p_multi", multi)

            ref_ht2_500 = _get_refvals(referent, "z2_500", multi)
            ref_p2_5000 = _get_refvals(referent, "p2_5000", multi)
            ref_ht2_multi = _get_refvals(referent, "z2_multi", multi)
            ref_p2_multi = _get_refvals(referent, "p2_multi", multi)

            ref_p_lev2d = _get_refvals(referent, "p_lev2d", multi)

            hts = getvar(wrfin, "z", timeidx=timeidx)
            p = getvar(wrfin, "pressure", timeidx=timeidx)
            wspd_wdir = getvar(wrfin, "wspd_wdir", timeidx=timeidx)

            # Make sure the numpy versions work first
            hts_500 = interplevel(to_np(hts), to_np(p), 500)
            hts_500 = interplevel(hts, p, 500)

            # Note: the '*2*' versions in the reference are testing
            # against the new version of interplevel in NCL
            nt.assert_allclose(to_np(hts_500), ref_ht_500)
            nt.assert_allclose(to_np(hts_500), ref_ht2_500)

            # Make sure the numpy versions work first
            p_5000 = interplevel(to_np(p), to_np(hts), 5000)
            p_5000 = interplevel(p, hts, 5000)

            nt.assert_allclose(to_np(p_5000), ref_p_5000)
            nt.assert_allclose(to_np(p_5000), ref_p2_5000)

            hts_multi = interplevel(to_np(hts), to_np(p),
                                    [1000., 850., 500., 250.])
            hts_multi = interplevel(hts, p, [1000., 850., 500., 250.])

            nt.assert_allclose(to_np(hts_multi), ref_ht_multi)
            nt.assert_allclose(to_np(hts_multi), ref_ht2_multi)

            p_multi = interplevel(to_np(p), to_np(hts),
                                  [500., 2500., 5000., 10000.])
            p_multi = interplevel(p, hts, [500., 2500., 5000., 10000.])

            nt.assert_allclose(to_np(p_multi), ref_p_multi)
            nt.assert_allclose(to_np(p_multi), ref_p2_multi)

            pblh = getvar(wrfin, "PBLH", timeidx=timeidx)
            p_lev2d = interplevel(to_np(p), to_np(hts), to_np(pblh))
            p_lev2d = interplevel(p, hts, pblh)
            nt.assert_allclose(to_np(p_lev2d), ref_p_lev2d)

            # Just make sure these run below
            wspd_wdir_500 = interplevel(to_np(wspd_wdir), to_np(p), 500)
            wspd_wdir_500 = interplevel(wspd_wdir, p, 500)

            wspd_wdir_multi = interplevel(to_np(wspd_wdir), to_np(p),
                                          [1000, 500, 250])
            wdpd_wdir_multi = interplevel(wspd_wdir, p, [1000, 500, 250])

            wspd_wdir_pblh = interplevel(to_np(wspd_wdir), to_np(hts), pblh)
            wspd_wdir_pblh = interplevel(wspd_wdir, hts, pblh)

            if multi:
                wspd_wdir_pblh_2 = interplevel(to_np(wspd_wdir), to_np(hts),
                                               pblh[0, :])
                wspd_wdir_pblh_2 = interplevel(wspd_wdir, hts, pblh[0, :])

                # Since PBLH doesn't change in this case, it should match
                # the 0 time from previous computation. Note that this
                # only works when the data has 1 time step that is repeated.
                # If you use a different case with multiple times,
                # this will probably fail.
                nt.assert_allclose(to_np(wspd_wdir_pblh_2[:, 0, :]),
                                   to_np(wspd_wdir_pblh[:, 0, :]))

                nt.assert_allclose(to_np(wspd_wdir_pblh_2[:, -1, :]),
                                   to_np(wspd_wdir_pblh[:, 0, :]))

        elif (varname == "vertcross"):
            ref_ht_cross = _get_refvals(referent, "ht_cross", multi)
            ref_p_cross = _get_refvals(referent, "p_cross", multi)
            ref_ht_vertcross1 = _get_refvals(referent, "ht_vertcross1", multi)
            ref_ht_vertcross2 = _get_refvals(referent, "ht_vertcross2", multi)
            ref_ht_vertcross3 = _get_refvals(referent, "ht_vertcross3", multi)

            hts = getvar(wrfin, "z", timeidx=timeidx)
            p = getvar(wrfin, "pressure", timeidx=timeidx)

            pivot_point = CoordPair(hts.shape[-1] / 2, hts.shape[-2] / 2)

            # Beginning in wrf-python 1.3, users can select number of levels.
            # Originally, for pressure, dz was 10, so let's back calculate
            # the number of levels.
            p_max = np.floor(np.amax(p) / 10) * 10  # bottom value
            p_min = np.ceil(np.amin(p) / 10) * 10  # top value

            p_autolevels = int((p_max - p_min) / 10)

            # Make sure the numpy versions work first

            ht_cross = vertcross(to_np(hts),
                                 to_np(p),
                                 pivot_point=pivot_point,
                                 angle=90.,
                                 autolevels=p_autolevels)

            ht_cross = vertcross(hts,
                                 p,
                                 pivot_point=pivot_point,
                                 angle=90.,
                                 autolevels=p_autolevels)

            try:
                nt.assert_allclose(to_np(ht_cross), ref_ht_cross, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(ht_cross) - ref_ht_cross)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            lats = hts.coords["XLAT"]
            lons = hts.coords["XLONG"]

            # Test the manual projection method with lat/lon
            # Only do this for the non-multi case, since the domain
            # might be moving
            if not multi:
                if lats.ndim > 2:  # moving nest
                    lats = lats[0, :]
                    lons = lons[0, :]

                ll_point = ll_points(lats, lons)

                pivot = CoordPair(lat=lats[int(lats.shape[-2] / 2),
                                           int(lats.shape[-1] / 2)],
                                  lon=lons[int(lons.shape[-2] / 2),
                                           int(lons.shape[-1] / 2)])

                v1 = vertcross(hts,
                               p,
                               wrfin=wrfin,
                               pivot_point=pivot_point,
                               angle=90.0)
                v2 = vertcross(hts,
                               p,
                               projection=hts.attrs["projection"],
                               ll_point=ll_point,
                               pivot_point=pivot_point,
                               angle=90.)

                try:
                    nt.assert_allclose(to_np(v1), to_np(v2), atol=.0001)
                except AssertionError:
                    absdiff = np.abs(to_np(v1) - to_np(v2))
                    maxdiff = np.amax(absdiff)
                    print(maxdiff)
                    print(np.argwhere(absdiff == maxdiff))
                    raise

            # Test opposite

            p_cross1 = vertcross(p, hts, pivot_point=pivot_point, angle=90.0)

            try:
                nt.assert_allclose(to_np(p_cross1), ref_p_cross, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(p_cross1) - ref_p_cross)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Test point to point
            start_point = CoordPair(0, hts.shape[-2] / 2)
            end_point = CoordPair(-1, hts.shape[-2] / 2)

            p_cross2 = vertcross(p,
                                 hts,
                                 start_point=start_point,
                                 end_point=end_point)

            try:
                nt.assert_allclose(to_np(p_cross1),
                                   to_np(p_cross2),
                                   atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(p_cross1) - to_np(p_cross2))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Check the new vertcross routine
            pivot_point = CoordPair(hts.shape[-1] / 2, hts.shape[-2] / 2)
            ht_cross = vertcross(hts,
                                 p,
                                 pivot_point=pivot_point,
                                 angle=90.,
                                 latlon=True)

            try:
                nt.assert_allclose(to_np(ht_cross),
                                   to_np(ref_ht_vertcross1),
                                   atol=.005)
            except AssertionError:
                absdiff = np.abs(to_np(ht_cross) - to_np(ref_ht_vertcross1))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            levels = [1000., 850., 700., 500., 250.]
            ht_cross = vertcross(hts,
                                 p,
                                 pivot_point=pivot_point,
                                 angle=90.,
                                 levels=levels,
                                 latlon=True)

            try:
                nt.assert_allclose(to_np(ht_cross),
                                   to_np(ref_ht_vertcross2),
                                   atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(ht_cross) - to_np(ref_ht_vertcross2))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            idxs = (0, slice(None)) if lats.ndim > 2 else (slice(None), )

            start_lat = np.amin(
                lats[idxs]) + .25 * (np.amax(lats[idxs]) - np.amin(lats[idxs]))
            end_lat = np.amin(
                lats[idxs]) + .65 * (np.amax(lats[idxs]) - np.amin(lats[idxs]))

            start_lon = np.amin(
                lons[idxs]) + .25 * (np.amax(lons[idxs]) - np.amin(lons[idxs]))
            end_lon = np.amin(
                lons[idxs]) + .65 * (np.amax(lons[idxs]) - np.amin(lons[idxs]))

            start_point = CoordPair(lat=start_lat, lon=start_lon)
            end_point = CoordPair(lat=end_lat, lon=end_lon)

            ll_point = ll_points(lats[idxs], lons[idxs])

            ht_cross = vertcross(hts,
                                 p,
                                 start_point=start_point,
                                 end_point=end_point,
                                 projection=hts.attrs["projection"],
                                 ll_point=ll_point,
                                 latlon=True,
                                 autolevels=1000)

            try:
                nt.assert_allclose(to_np(ht_cross),
                                   to_np(ref_ht_vertcross3),
                                   atol=.01)
            except AssertionError:
                absdiff = np.abs(to_np(ht_cross) - to_np(ref_ht_vertcross3))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            if multi:
                ntimes = hts.shape[0]

                for t in range(ntimes):
                    hts = getvar(wrfin, "z", timeidx=t)
                    p = getvar(wrfin, "pressure", timeidx=t)

                    ht_cross = vertcross(hts,
                                         p,
                                         start_point=start_point,
                                         end_point=end_point,
                                         wrfin=wrfin,
                                         timeidx=t,
                                         latlon=True,
                                         autolevels=1000)

                    refname = "ht_vertcross_t{}".format(t)
                    ref_ht_vertcross = _get_refvals(referent, refname, False)

                    try:
                        nt.assert_allclose(to_np(ht_cross),
                                           to_np(ref_ht_vertcross),
                                           atol=.01)
                    except AssertionError:
                        absdiff = np.abs(
                            to_np(ht_cross) - to_np(ref_ht_vertcross))
                        maxdiff = np.amax(absdiff)
                        print(maxdiff)
                        print(np.argwhere(absdiff == maxdiff))
                        raise

        elif (varname == "interpline"):

            ref_t2_line = _get_refvals(referent, "t2_line", multi)
            ref_t2_line2 = _get_refvals(referent, "t2_line2", multi)
            ref_t2_line3 = _get_refvals(referent, "t2_line3", multi)

            t2 = getvar(wrfin, "T2", timeidx=timeidx)
            pivot_point = CoordPair(t2.shape[-1] / 2, t2.shape[-2] / 2)

            # Make sure the numpy version works
            t2_line1 = interpline(to_np(t2),
                                  pivot_point=pivot_point,
                                  angle=90.0)
            t2_line1 = interpline(t2, pivot_point=pivot_point, angle=90.0)

            nt.assert_allclose(to_np(t2_line1), ref_t2_line)

            # Test the new NCL wrf_user_interplevel result
            try:
                nt.assert_allclose(to_np(t2_line1), ref_t2_line2)
            except AssertionError:
                absdiff = np.abs(to_np(t2_line1) - ref_t2_line2)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Test the manual projection method with lat/lon
            lats = t2.coords["XLAT"]
            lons = t2.coords["XLONG"]
            if multi:
                if lats.ndim > 2:  # moving nest
                    lats = lats[0, :]
                    lons = lons[0, :]

            ll_point = ll_points(lats, lons)

            pivot = CoordPair(lat=lats[int(lats.shape[-2] / 2),
                                       int(lats.shape[-1] / 2)],
                              lon=lons[int(lons.shape[-2] / 2),
                                       int(lons.shape[-1] / 2)])

            l1 = interpline(t2,
                            wrfin=wrfin,
                            pivot_point=pivot_point,
                            angle=90.0)

            l2 = interpline(t2,
                            projection=t2.attrs["projection"],
                            ll_point=ll_point,
                            pivot_point=pivot_point,
                            angle=90.)
            try:
                nt.assert_allclose(to_np(l1), to_np(l2))
            except AssertionError:
                absdiff = np.abs(to_np(l1) - to_np(l2))
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Test point to point
            start_point = CoordPair(0, t2.shape[-2] / 2)
            end_point = CoordPair(-1, t2.shape[-2] / 2)

            t2_line2 = interpline(t2,
                                  start_point=start_point,
                                  end_point=end_point)

            try:
                nt.assert_allclose(to_np(t2_line1), to_np(t2_line2))
            except AssertionError:
                absdiff = np.abs(to_np(t2_line1) - t2_line2)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Now test the start/end with lat/lon points

            start_lat = float(
                np.amin(lats) + .25 * (np.amax(lats) - np.amin(lats)))
            end_lat = float(
                np.amin(lats) + .65 * (np.amax(lats) - np.amin(lats)))

            start_lon = float(
                np.amin(lons) + .25 * (np.amax(lons) - np.amin(lons)))
            end_lon = float(
                np.amin(lons) + .65 * (np.amax(lons) - np.amin(lons)))

            start_point = CoordPair(lat=start_lat, lon=start_lon)
            end_point = CoordPair(lat=end_lat, lon=end_lon)

            t2_line3 = interpline(t2,
                                  wrfin=wrfin,
                                  timeidx=0,
                                  start_point=start_point,
                                  end_point=end_point,
                                  latlon=True)

            try:
                nt.assert_allclose(to_np(t2_line3), ref_t2_line3, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(t2_line3) - ref_t2_line3)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Test all time steps
            if multi:
                refnc = NetCDF(referent)
                ntimes = t2.shape[0]

                for t in range(ntimes):
                    t2 = getvar(wrfin, "T2", timeidx=t)

                    line = interpline(t2,
                                      wrfin=wrfin,
                                      timeidx=t,
                                      start_point=start_point,
                                      end_point=end_point,
                                      latlon=True)

                    refname = "t2_line_t{}".format(t)
                    refline = refnc.variables[refname][:]

                    try:
                        nt.assert_allclose(to_np(line),
                                           to_np(refline),
                                           atol=.0001)
                    except AssertionError:
                        absdiff = np.abs(to_np(line) - refline)
                        maxdiff = np.amax(absdiff)
                        print(maxdiff)
                        print(np.argwhere(absdiff == maxdiff))
                        raise

                refnc.close()

            # Test NCLs single time case
            if not multi:
                refnc = NetCDF(referent)
                ref_t2_line4 = refnc.variables["t2_line4"][:]

                t2 = getvar(wrfin, "T2", timeidx=0)
                line = interpline(t2,
                                  wrfin=wrfin,
                                  timeidx=0,
                                  start_point=start_point,
                                  end_point=end_point,
                                  latlon=True)

                try:
                    nt.assert_allclose(to_np(line),
                                       to_np(ref_t2_line4),
                                       atol=.0001)
                except AssertionError:
                    absdiff = np.abs(to_np(line) - ref_t2_line4)
                    maxdiff = np.amax(absdiff)
                    print(maxdiff)
                    print(np.argwhere(absdiff == maxdiff))
                    raise
                finally:
                    refnc.close()

        elif (varname == "vinterp"):
            # Tk to theta
            fld_tk_theta = _get_refvals(referent, "fld_tk_theta", multi)
            fld_tk_theta = np.squeeze(fld_tk_theta)

            tk = getvar(wrfin, "temp", timeidx=timeidx, units="k")

            interp_levels = [200, 300, 500, 1000]

            # Make sure the numpy version works
            field = vinterp(wrfin,
                            field=to_np(tk),
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            tol = 0 / 100.
            atol = 0.0001

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_tk_theta)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_theta)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Tk to theta-e
            fld_tk_theta_e = _get_refvals(referent, "fld_tk_theta_e", multi)
            fld_tk_theta_e = np.squeeze(fld_tk_theta_e)

            interp_levels = [200, 300, 500, 1000]

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="theta-e",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            try:
                nt.assert_allclose(to_np(field), fld_tk_theta_e, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_theta_e)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Tk to pressure
            fld_tk_pres = _get_refvals(referent, "fld_tk_pres", multi)
            fld_tk_pres = np.squeeze(fld_tk_pres)

            interp_levels = [850, 500]

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_tk_pres, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_pres)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Tk to geoht_msl
            fld_tk_ght_msl = _get_refvals(referent, "fld_tk_ght_msl", multi)
            fld_tk_ght_msl = np.squeeze(fld_tk_ght_msl)
            interp_levels = [1, 2]

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="ght_msl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)
            try:
                nt.assert_allclose(to_np(field), fld_tk_ght_msl, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_ght_msl)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Tk to geoht_agl
            fld_tk_ght_agl = _get_refvals(referent, "fld_tk_ght_agl", multi)
            fld_tk_ght_agl = np.squeeze(fld_tk_ght_agl)
            interp_levels = [1, 2]

            field = vinterp(wrfin,
                            field=tk,
                            vert_coord="ght_agl",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="tk",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_tk_ght_agl, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_tk_ght_agl)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Hgt to pressure
            fld_ht_pres = _get_refvals(referent, "fld_ht_pres", multi)
            fld_ht_pres = np.squeeze(fld_ht_pres)

            z = getvar(wrfin, "height", timeidx=timeidx, units="m")
            interp_levels = [500, 50]
            field = vinterp(wrfin,
                            field=z,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="ght",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_ht_pres, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_ht_pres)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Pressure to theta
            fld_pres_theta = _get_refvals(referent, "fld_pres_theta", multi)
            fld_pres_theta = np.squeeze(fld_pres_theta)

            p = getvar(wrfin, "pressure", timeidx=timeidx)
            interp_levels = [200, 300, 500, 1000]
            field = vinterp(wrfin,
                            field=p,
                            vert_coord="theta",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="pressure",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_pres_theta, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_pres_theta)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise

            # Theta-e to pres
            fld_thetae_pres = _get_refvals(referent, "fld_thetae_pres", multi)
            fld_thetae_pres = np.squeeze(fld_thetae_pres)

            eth = getvar(wrfin, "eth", timeidx=timeidx)
            interp_levels = [850, 500, 5]
            field = vinterp(wrfin,
                            field=eth,
                            vert_coord="pressure",
                            interp_levels=interp_levels,
                            extrapolate=True,
                            field_type="theta-e",
                            timeidx=timeidx,
                            log_p=True)

            field = np.squeeze(field)

            try:
                nt.assert_allclose(to_np(field), fld_thetae_pres, atol=.0001)
            except AssertionError:
                absdiff = np.abs(to_np(field) - fld_thetae_pres)
                maxdiff = np.amax(absdiff)
                print(maxdiff)
                print(np.argwhere(absdiff == maxdiff))
                raise
Ejemplo n.º 27
0
def make_result_file(opts):
    infilename = os.path.expanduser(
        os.path.join(opts.outdir, "ci_test_file.nc"))
    outfilename = os.path.expanduser(
        os.path.join(opts.outdir, "ci_result_file.nc"))

    with Dataset(infilename) as infile, Dataset(outfilename, "w") as outfile:

        for varname in WRF_DIAGS:
            var = getvar(infile, varname)
            add_to_ncfile(outfile, var, varname)

        for interptype in INTERP_METHS:
            if interptype == "interpline":
                hts = getvar(infile, "z")
                p = getvar(infile, "pressure")
                hts_850 = interplevel(hts, p, 850.)

                add_to_ncfile(outfile, hts_850, "interplevel")

            if interptype == "vertcross":

                hts = getvar(infile, "z")
                p = getvar(infile, "pressure")

                pivot_point = CoordPair(hts.shape[-1] // 2, hts.shape[-2] // 2)
                ht_cross = vertcross(hts,
                                     p,
                                     pivot_point=pivot_point,
                                     angle=90.)

                add_to_ncfile(outfile, ht_cross, "vertcross")

            if interptype == "interpline":

                t2 = getvar(infile, "T2")
                pivot_point = CoordPair(t2.shape[-1] // 2, t2.shape[-2] // 2)

                t2_line = interpline(t2, pivot_point=pivot_point, angle=90.0)

                add_to_ncfile(outfile, t2_line, "interpline")

            if interptype == "vinterp":

                tk = getvar(infile, "temp", units="k")

                interp_levels = [200, 300, 500, 1000]

                field = vinterp(infile,
                                field=tk,
                                vert_coord="theta",
                                interp_levels=interp_levels,
                                extrapolate=True,
                                field_type="tk",
                                log_p=True)

                add_to_ncfile(outfile, field, "vinterp")

        for latlonmeth in LATLON_METHS:
            if latlonmeth == "xy":
                # Hardcoded values from other unit tests
                lats = [-55, -60, -65]
                lons = [25, 30, 35]

                xy = ll_to_xy(infile, lats[0], lons[0])
                add_to_ncfile(outfile, xy, "xy")
            else:
                # Hardcoded from other unit tests
                i_s = np.asarray([10, 100, 150], int) - 1
                j_s = np.asarray([10, 100, 150], int) - 1

                ll = xy_to_ll(infile, i_s[0], j_s[0])
                add_to_ncfile(outfile, ll, "ll")
Ejemplo n.º 28
0
v_tmp = np.empty((times.shape[0], z.shape[0], v_int.shape[2], v_int.shape[3]))
w_tmp = np.empty((times.shape[0], zw.shape[0], y.shape[0], x.shape[0]))
qv_tmp = np.empty(
    (times.shape[0], z.shape[0], qv_int.shape[2], qv_int.shape[3]))
pt_tmp = np.empty(
    (times.shape[0], z.shape[0], pt_int.shape[2], pt_int.shape[3]))
pres_tmp = np.empty(
    (times.shape[0], z.shape[0], pres_int.shape[2], pres_int.shape[3]))
tk_tmp = np.empty(
    (times.shape[0], z.shape[0], tk_int.shape[2], tk_int.shape[3]))

for l_idx, l in tqdm(enumerate(z),
                     desc="Interpolating unstaggered vertical levels"):
    for t in range(0, times.shape[0]):
        qv_tmp[t,
               int(l_idx), :, :] = interplevel(qv_int[t, :, :, :],
                                               z_wrf_int[t, :, :, :], l).data
        pt_tmp[t,
               int(l_idx), :, :] = interplevel(pt_int[t, :, :, :],
                                               z_wrf_int[t, :, :, :], l).data
        u_tmp[t,
              int(l_idx), :, :] = interplevel(u_int[t, :, :, :],
                                              z_wrf_int_u[t, :, :, :], l).data
        v_tmp[t,
              int(l_idx), :, :] = interplevel(v_int[t, :, :, :],
                                              z_wrf_int_v[t, :, :, :], l).data
        pres_tmp[t,
                 int(l_idx), :, :] = interplevel(pres_int[t, :, :, :],
                                                 z_wrf_int[t, :, :, :], l).data
        tk_tmp[t,
               int(l_idx), :, :] = interplevel(tk_int[t, :, :, :],
                                               z_wrf_int[t, :, :, :], l).data
    timeneed = datelist(timeperiod1[tp], timeperiod2[tp])

    os.chdir("/scratch/zhaohuiw/PWRF_non_staticseaice_ib/WRFV3/test/em_real")
    x = np.zeros((264, 699, 699))

    i = 0
    for date in timeneed:
        filename = 'wrfout_d01_' + date + '_00:00:00'
        ncfile = Dataset(filename)
        # Extract the Geopotential Height and Pressure (hPa) fields
        z = getvar(ncfile, "z", units='dm',
                   timeidx=ALL_TIMES)  #change the variable to what we need
        p = getvar(ncfile, "pressure",
                   timeidx=ALL_TIMES)  #change the variable to what we need
        # Compute the 500 MB Geopotential Height
        ht_500mb = interplevel(z, p, 500.)
        x[24 * i:24 * (i + 1), :, :] = ht_500mb
        i = i + 1
    del i
    os.chdir("/scratch/zhaohuiw/wrf_variable")
    sio.savemat('ht_500mb_non_staticseaice_' + filename[11:21] + '.mat',
                {'ht_500mb': x})  # give x the variable name

    ###################################
    os.chdir("/scratch/zhaohuiw/PWRF_staticseaice_ib/WRFV3/test/em_real")
    x = np.zeros((264, 699, 699))

    i = 0
    for date in timeneed:
        filename = 'wrfout_d01_' + date + '_00:00:00'
        ncfile = Dataset(filename)
Ejemplo n.º 30
0
                    start_point=startpoint,
                    end_point=endpoint,
                    latlon=True)

lonlist, latlist = [], []
plist = ua_vert.coords['vertical']
print(plist)
for i in range(len(ua_vert.coords['xy_loc'])):
    s = str(ua_vert.coords['xy_loc'][i].values)
    lonlist.append(float(s[s.find('lon=') + 4:s.find('lon=') + 12]))
    latlist.append(float(s[s.find('lat=') + 4:s.find('lat=') + 12]))
print(lonlist)
print(latlist)
hlist = []
for i in range(1000, 700, -30):
    hlist.append(float(np.max(interplevel(height2earth, p, i)).values))
hlist = np.array([int(i) for i in hlist])
print(hlist)
plist = to_np(plist)

fig = plt.figure(figsize=(12, 8), dpi=150)
axe_1 = plt.subplot(1, 1, 1)  #这里可以设置多个子图,第一个参数表示多少行,第二个表示多少列,第三个表示第几个子图
axe_1.set_title(str(Readtime.get_ncfile_time(ncfile, timezone=8)[time]),
                fontsize=12)
start_x, end_x = start_lon, end_lon
small_p, big_p = 700, 1000
big_interval_x, small_interval_x, big_interval_p, small_interval_p = 0.1, 0.1, 30, 30
#axe_1.set_xlim(start_x, end_x)
axe_1.set_ylim(65, 100)  # 设置图的范围

axe_1.grid(color='gray', linestyle=':', linewidth=0.7)