Example #1
0
def test_frontogenesis_asym():
    """Test vorticity and convergence calculation with a complicated field."""
    u = np.array([[2, 4, 8], [0, 2, 2], [4, 6, 8]]) * units('m/s')
    v = np.array([[6, 4, 8], [2, 6, 0], [2, 2, 6]]) * units('m/s')
    theta = np.array([[303, 295, 305], [308, 310, 312], [299, 293, 289]
                      ]) * units('K')
    fronto = frontogenesis(theta,
                           u,
                           v,
                           1 * units.meters,
                           2 * units.meters,
                           dim_order='yx')
    true_fronto = np.array([[-20.93890452, -7.83070042, -36.43293256],
                            [0.89442719, -2.12218672, -8.94427191],
                            [-16.8, -7.65600391, -61.65921479]
                            ]) * units.K / units.meter / units.sec
    assert_almost_equal(fronto, true_fronto)

    # Now try for xy ordered
    fronto = frontogenesis(theta.T,
                           u.T,
                           v.T,
                           1 * units.meters,
                           2 * units.meters,
                           dim_order='xy')
    assert_almost_equal(fronto, true_fronto.T)
Example #2
0
def test_frontogenesis_asym():
    """Test frontogensis calculation with a complicated field."""
    u = np.array([[2, 4, 8], [0, 2, 2], [4, 6, 8]]) * units('m/s')
    v = np.array([[6, 4, 8], [2, 6, 0], [2, 2, 6]]) * units('m/s')
    theta = np.array([[303, 295, 305], [308, 310, 312], [299, 293, 289]
                      ]) * units('K')
    fronto = frontogenesis(theta,
                           u,
                           v,
                           1 * units.meters,
                           2 * units.meters,
                           dim_order='yx')
    true_fronto = np.array([[-52.4746386, -37.3658646, -50.3996939],
                            [3.5777088, -2.1221867, -16.9941166],
                            [-23.1417334, 26.0499143, -158.4839684]
                            ]) * units.K / units.meter / units.sec
    assert_almost_equal(fronto, true_fronto)

    # Now try for xy ordered
    fronto = frontogenesis(theta.T,
                           u.T,
                           v.T,
                           1 * units.meters,
                           2 * units.meters,
                           dim_order='xy')
    assert_almost_equal(fronto, true_fronto.T)
Example #3
0
QVEC700 = mpcalc.q_vector(U700, V700, T700, 700 * units.mbar, DX, DY)
QC700 = mpcalc.divergence(QVEC700[0], QVEC700[1], DX, DY,
                          dim_order='yx') * (10**18)
QVECX = QVEC700[0]
QVECY = QVEC700[1]
# =============================================================================
# FIG #4: 850: GEOPOTENTIAL HEIGHT, TEMP, WINDS, TEMP-ADVECTION, FRONTOGENESIS
# =============================================================================
H850 = HGT_DATA.variables['hgt'][TIME_INDEX, 2, :, :]
T850 = AIR_DATA.variables['air'][TIME_INDEX, 2, :, :] * units('kelvin')
U850 = UWND_DATA.variables['uwnd'][TIME_INDEX, 2, :, :] * units('m/s')
V850 = VWND_DATA.variables['vwnd'][TIME_INDEX, 2, :, :] * units('m/s')
T_ADV850 = mpcalc.advection(T850 * units.kelvin, [U850, V850], (DX, DY),
                            dim_order='yx') * units('K/sec')
PT850 = mpcalc.potential_temperature(850 * units.mbar, T850)
FRONT_850 = mpcalc.frontogenesis(PT850, U850, V850, DX, DY, dim_order='YX')
# =============================================================================
# FIG #5: 850: GEOPOTENTIAL HEIGHT, EQUIV. POT. TEMP, WINDS, LAPSE RATES
# =============================================================================
H500 = HGT_DATA.variables['hgt'][TIME_INDEX, 5, :, :]
H700 = HGT_DATA.variables['hgt'][TIME_INDEX, 3, :, :]
T500 = AIR_DATA.variables['air'][TIME_INDEX, 5, :, :] * units('degC')
T700 = AIR_DATA.variables['air'][TIME_INDEX, 3, :, :] * units('degC')
LR = -1000 * (T500 - T700) / (H500 - H700)
H850 = HGT_DATA.variables['hgt'][TIME_INDEX, 2, :, :]
T850 = AIR_DATA.variables['air'][TIME_INDEX, 2, :, :] * units('kelvin')
SH850 = SHUM_DATA.variables['shum'][TIME_INDEX, 2, :, :]
DP850 = mpcalc.dewpoint_from_specific_humidity(SH850, T850, 850 * units.mbar)
EPT850 = mpcalc.equivalent_potential_temperature(850 * units.mbar, T850, DP850)
# =============================================================================
# FIG #6: 925: MOISTURE FLUX, MOISTURE FLUX CONVERGENCE,
Example #4
0
def fg_uv_tmp(initTime=None, fhour=6, day_back=0,model='ECMWF',
    fg_lev=500,
    map_ratio=16/9,zoom_ratio=20,cntr_pnt=[102,34],
    south_China_sea=True,area = None,city=False,output_dir=None,data_source='MICAPS',
    **kwargs):

    if(area != None):
        south_China_sea=False

    # micaps data directory
    if(data_source=='MICAPS'):
        try:
            data_dir = [utl.Cassandra_dir(data_type='high',data_source=model,var_name='HGT',lvl=fg_lev),
                        utl.Cassandra_dir(data_type='high',data_source=model,var_name='UGRD',lvl=fg_lev),
                        utl.Cassandra_dir(data_type='high',data_source=model,var_name='VGRD',lvl=fg_lev),
                        utl.Cassandra_dir(data_type='high',data_source=model,var_name='TMP',lvl=fg_lev),
                        utl.Cassandra_dir(data_type='surface',data_source=model,var_name='PSFC')]
        except KeyError:
            raise ValueError('Can not find all directories needed')

        # get filename
        if(initTime != None):
            filename = utl.model_filename(initTime, fhour)
        else:
            filename=utl.filename_day_back_model(day_back=day_back,fhour=fhour)

        # retrieve data from micaps server
        gh = MICAPS_IO.get_model_grid(data_dir[0], filename=filename)
        if gh is None:
            return
        
        u = MICAPS_IO.get_model_grid(data_dir[1], filename=filename)
        if u is None:
            return
            
        v = MICAPS_IO.get_model_grid(data_dir[2], filename=filename)
        if v is None:
            return
        tmp = MICAPS_IO.get_model_grid(data_dir[3], filename=filename)
        
        psfc = MICAPS_IO.get_model_grid(data_dir[4], filename=filename)
        
    if(data_source =='CIMISS'):
        
        # get filename
        if(initTime != None):
            filename = utl.model_filename(initTime, fhour,UTC=True)
        else:
            filename=utl.filename_day_back_model(day_back=day_back,fhour=fhour,UTC=True)
        
        # retrieve data from CIMISS server 
        try:       
            gh=CMISS_IO.cimiss_model_by_time('20'+filename[0:8],valid_time=fhour,
                        data_code=utl.CMISS_data_code(data_source=model,var_name='GPH'),
                        levattrs={'long_name':'pressure_level', 'units':'hPa', '_CoordinateAxisType':'-'},
                        fcst_level=fg_lev, fcst_ele="GPH", units='gpm')
            if gh is None:
                return
            gh['data'].values=gh['data'].values/10.

            u=CMISS_IO.cimiss_model_by_time('20'+filename[0:8],valid_time=fhour,
                        data_code=utl.CMISS_data_code(data_source=model,var_name='WIU'),
                        levattrs={'long_name':'pressure_level', 'units':'hPa', '_CoordinateAxisType':'-'},
                        fcst_level=fg_lev, fcst_ele="WIU", units='m/s')
            if u is None:
                return
                
            v=CMISS_IO.cimiss_model_by_time('20'+filename[0:8],valid_time=fhour,
                        data_code=utl.CMISS_data_code(data_source=model,var_name='WIV'),
                        levattrs={'long_name':'pressure_level', 'units':'hPa', '_CoordinateAxisType':'-'},
                        fcst_level=fg_lev, fcst_ele="WIV", units='m/s')
            if v is None:
                return

            tmp=CMISS_IO.cimiss_model_by_time('20'+filename[0:8],valid_time=fhour,
                        data_code=utl.CMISS_data_code(data_source=model,var_name='TEM'),
                        levattrs={'long_name':'pressure_level', 'units':'hPa', '_CoordinateAxisType':'-'},
                        fcst_level=fg_lev, fcst_ele="TEM", units='K')
            if tmp is None:
                return
            tmp['data'].values=tmp['data'].values-273.15

            psfc=CMISS_IO.cimiss_model_by_time('20'+filename[0:8], valid_time=fhour,
                        data_code=utl.CMISS_data_code(data_source=model,var_name='PRS'),
                        fcst_level=0, fcst_ele="PRS", units='Pa')
            psfc['data']=psfc['data']/100.
        except KeyError:
            raise ValueError('Can not find all data needed')

    #caluculate_fg
    pressure=fg_lev*units('hPa')
    theta = mpcalc.potential_temperature(pressure, tmp['data'].values.squeeze()*units('degC'))
    dx,dy=mpcalc.lat_lon_grid_deltas(tmp['lon'].values.squeeze(),tmp['lat'].values.squeeze())
    fg=mpcalc.frontogenesis(theta,u['data'].values.squeeze()*units('mps'),v['data'].values.squeeze()*units('mps'),dx,dy,dim_order='yx')

    if(area != None):
        cntr_pnt,zoom_ratio=utl.get_map_area(area_name=area)
    map_extent,delt_x,delt_y=utl.get_map_extent(cntr_pnt, zoom_ratio, map_ratio)

#+ to solve the problem of labels on all the contours

    fg_xr=u.copy()
    fg_xr['data'].values=fg.magnitude[np.newaxis,np.newaxis,:,:]
    fg_xr=utl.cut_xrdata(map_extent, fg_xr, delt_x=delt_x, delt_y=delt_y)
    u=utl.cut_xrdata(map_extent, u, delt_x=delt_x, delt_y=delt_y)
    v=utl.cut_xrdata(map_extent, v, delt_x=delt_x, delt_y=delt_y)
    tmp=utl.cut_xrdata(map_extent, tmp, delt_x=delt_x, delt_y=delt_y)

    fg_xr=utl.mask_terrian(fg_lev,psfc,fg_xr)
    u=utl.mask_terrian(fg_lev,psfc,u)
    v=utl.mask_terrian(fg_lev,psfc,v)
    tmp=utl.mask_terrian(fg_lev,psfc,tmp)

    fg_xr.attrs['model']=model
    fg_xr.attrs['units']='K*s${^{-1}}$ m${^{-1}}$'
    uv=xr.merge([u.rename({'data': 'u'}),v.rename({'data': 'v'})])
#- to solve the problem of labels on all the contours
    dynamic_graphics.draw_fg_uv_tmp(
        fg=fg_xr, tmp=tmp, uv=uv,
        map_extent=map_extent, regrid_shape=20,
        city=city,south_China_sea=south_China_sea,
        output_dir=output_dir)