示例#1
0
def T2m_all_type(initTime=None,
                 fhour=24,
                 day_back=0,
                 model='中央气象台中短期指导',
                 Var_plot='Tmn_2m',
                 map_ratio=19 / 9,
                 zoom_ratio=20,
                 cntr_pnt=[102, 34],
                 data_source='MICAPS',
                 south_China_sea=True,
                 area='全国',
                 city=False,
                 output_dir=None,
                 Global=False):

    # prepare data
    if (data_source == 'MICAPS'):
        try:
            data_dir = [
                utl.Cassandra_dir(data_type='surface',
                                  data_source=model,
                                  var_name=Var_plot)
            ]
        except KeyError:
            raise ValueError('Can not find all directories needed')

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

        T_2m = MICAPS_IO.get_model_grid(data_dir[0], filename=filename)
        if T_2m is None:
            return

    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)
        try:
            # retrieve data from CMISS server
            T_2m = CMISS_IO.cimiss_model_by_time(
                '20' + filename[0:8],
                valid_time=fhour,
                data_code=utl.CMISS_data_code(data_source=model,
                                              var_name='TEF2'),
                levattrs={
                    'long_name': 'Mean_sea_level',
                    'units': 'm',
                    '_CoordinateAxisType': '-'
                },
                fcst_level=0,
                fcst_ele="TEF2",
                units='K')
            if T_2m is None:
                return
            T_2m['data'].values = T_2m['data'].values - 273.15

        except KeyError:
            raise ValueError('Can not find all data needed')


# set map extent
    if (area != '全国'):
        south_China_sea = False

    if (area != None):
        cntr_pnt, zoom_ratio = utl.get_map_area(area_name=area)

    map_extent = [0, 0, 0, 0]
    map_extent[0] = cntr_pnt[0] - zoom_ratio * 1 * map_ratio
    map_extent[1] = cntr_pnt[0] + zoom_ratio * 1 * map_ratio
    map_extent[2] = cntr_pnt[1] - zoom_ratio * 1
    map_extent[3] = cntr_pnt[1] + zoom_ratio * 1

    delt_x = (map_extent[1] - map_extent[0]) * 0.2
    delt_y = (map_extent[3] - map_extent[2]) * 0.1

    #+ to solve the problem of labels on all the contours
    mask1 = (T_2m['lon'] > map_extent[0] - delt_x) & (
        T_2m['lon'] < map_extent[1] + delt_x) & (
            T_2m['lat'] > map_extent[2] - delt_y) & (T_2m['lat'] <
                                                     map_extent[3] + delt_y)
    T_2m = T_2m.where(mask1, drop=True)

    titles = {
        'Tmn_2m': '过去24小时2米最低温度',
        'Tmx_2m': '过去24小时2米最高温度',
        'T2m': '2米温度'
    }
    #- to solve the problem of labels on all the contours
    T_2m.attrs['model'] = model
    T_2m.attrs['title'] = titles[Var_plot]

    elements_graphics.draw_T_2m(T_2m=T_2m,
                                map_extent=map_extent,
                                regrid_shape=20,
                                city=city,
                                south_China_sea=south_China_sea,
                                output_dir=output_dir,
                                Global=Global)
示例#2
0
def T2m_all_type(initTime=None,
                 fhour=24,
                 day_back=0,
                 model='中央台指导',
                 Var_plot='Tmn_2m',
                 map_ratio=19 / 9,
                 zoom_ratio=20,
                 cntr_pnt=[102, 34],
                 south_China_sea=True,
                 area='全国',
                 city=False,
                 output_dir=None,
                 Global=False):

    if (area != '全国'):
        south_China_sea = False

    # micaps data directory
    try:
        data_dir = [
            utl.Cassandra_dir(data_type='surface',
                              data_source=model,
                              var_name=Var_plot)
        ]
    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
    T_2m = get_model_grid(data_dir[0], filename=filename)
    if T_2m is None:
        return
    init_time = T_2m.coords['forecast_reference_time'].values

    # prepare data

    if (area != None):
        cntr_pnt, zoom_ratio = utl.get_map_area(area_name=area)

    map_extent = [0, 0, 0, 0]
    map_extent[0] = cntr_pnt[0] - zoom_ratio * 1 * map_ratio
    map_extent[1] = cntr_pnt[0] + zoom_ratio * 1 * map_ratio
    map_extent[2] = cntr_pnt[1] - zoom_ratio * 1
    map_extent[3] = cntr_pnt[1] + zoom_ratio * 1

    delt_x = (map_extent[1] - map_extent[0]) * 0.2
    delt_y = (map_extent[3] - map_extent[2]) * 0.1

    #+ to solve the problem of labels on all the contours
    idx_x1 = np.where((T_2m.coords['lon'].values > map_extent[0] - delt_x)
                      & (T_2m.coords['lon'].values < map_extent[1] + delt_x))
    idx_y1 = np.where((T_2m.coords['lat'].values > map_extent[2] - delt_y)
                      & (T_2m.coords['lat'].values < map_extent[3] + delt_y))

    titles = {
        'Tmn_2m': '过去24小时2米最低温度',
        'Tmx_2m': '过去24小时2米最高温度',
        'T2m': '2米温度'
    }
    #- to solve the problem of labels on all the contours
    T_2m = {
        'lon':
        T_2m.coords['lon'].values[idx_x1],
        'lat':
        T_2m.coords['lat'].values[idx_y1],
        'data':
        T_2m['data'].values[0, 0, idx_y1[0][0]:(idx_y1[0][-1] + 1),
                            idx_x1[0][0]:(idx_x1[0][-1] + 1)],
        'model':
        model,
        'fhour':
        fhour,
        'title':
        titles[Var_plot],
        'init_time':
        init_time
    }

    elements_graphics.draw_T_2m(T_2m=T_2m,
                                map_extent=map_extent,
                                regrid_shape=20,
                                city=city,
                                south_China_sea=south_China_sea,
                                output_dir=output_dir,
                                Global=Global)
示例#3
0
def T2m_mean24(initTime=None,
               fhour=24,
               day_back=0,
               model='中央气象台中短期指导',
               map_ratio=19 / 9,
               zoom_ratio=20,
               cntr_pnt=[102, 34],
               data_source='MICAPS',
               south_China_sea=True,
               area='全国',
               city=False,
               output_dir=None,
               Global=False):

    # prepare data
    if (data_source == 'MICAPS'):
        data_dir = [
            utl.Cassandra_dir(data_type='surface',
                              data_source=model,
                              var_name='T2m')
        ]
        fhours = np.arange(fhour - 21, fhour + 1, 3)
        if (initTime is None):
            initTime = utl.filename_day_back_model(day_back=day_back,
                                                   fhour=fhour)[0:8]
        filenames = [initTime + '.' + str(fhour).zfill(3) for fhour in fhours]
        T_2m = MICAPS_IO.get_model_grids(data_dir[0], filenames=filenames)

        Tmean_2m = T_2m.isel(time=[-1]).copy()
        Tmean_2m['data'].values[0, :, :] = np.mean(T_2m['data'].values, axis=0)

    if (data_source == 'CIMISS'):
        # get filename
        fhours = np.arange(fhour - 18, fhour + 1, 3)
        if (initTime is None):
            initTime = utl.filename_day_back_model(day_back=day_back,
                                                   fhour=fhour,
                                                   UTC=True)[0:8]
        T_2m = CMISS_IO.cimiss_model_by_times('20' + initTime,
                                              valid_times=fhours,
                                              data_code=utl.CMISS_data_code(
                                                  data_source=model,
                                                  var_name='TEF2'),
                                              fcst_level=0,
                                              fcst_ele='TEF2',
                                              units='K',
                                              allExists=False)
        T_2m['data'].values = T_2m['data'].values - 273.15
        Tmean_2m = T_2m.isel(time=[-1]).copy()
        Tmean_2m['data'].values[0, :, :] = np.min(T_2m['data'].values, axis=0)


# set map extent
    if (area != '全国'):
        south_China_sea = False

    if (area != None):
        cntr_pnt, zoom_ratio = utl.get_map_area(area_name=area)

    map_extent = [0, 0, 0, 0]
    map_extent[0] = cntr_pnt[0] - zoom_ratio * 1 * map_ratio
    map_extent[1] = cntr_pnt[0] + zoom_ratio * 1 * map_ratio
    map_extent[2] = cntr_pnt[1] - zoom_ratio * 1
    map_extent[3] = cntr_pnt[1] + zoom_ratio * 1

    delt_x = (map_extent[1] - map_extent[0]) * 0.2
    delt_y = (map_extent[3] - map_extent[2]) * 0.1

    #+ to solve the problem of labels on all the contours
    mask1 = (Tmean_2m['lon'] > map_extent[0] - delt_x) & (
        Tmean_2m['lon'] < map_extent[1] + delt_x) & (
            Tmean_2m['lat'] > map_extent[2] - delt_y) & (
                Tmean_2m['lat'] < map_extent[3] + delt_y)
    Tmean_2m = Tmean_2m.where(mask1, drop=True)

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

    Tmean_2m.attrs['model'] = model
    Tmean_2m.attrs['title'] = '2米平均温度'

    elements_graphics.draw_T_2m(T_2m=Tmean_2m,
                                T_type='T_mean',
                                map_extent=map_extent,
                                regrid_shape=20,
                                city=city,
                                south_China_sea=south_China_sea,
                                output_dir=output_dir,
                                Global=Global)