Example #1
0
def point(datetime, point_name, point_lonlat, number_grid=1):
    if point_name:
        if "cyclone.hpc.uib.no" in platform.node():
            print("detected cyclone")
            sites = pd.read_csv(
                "../githubclones/islas/weathervis/weathervis/data/sites.csv",
                sep=";",
                header=0,
                index_col=0)
        else:
            sites = pd.read_csv("../../weathervis/weathervis/data/sites.csv",
                                sep=";",
                                header=0,
                                index_col=0)
        point_lonlat = [sites.loc[point_name].lon, sites.loc[point_name].lat]
        print(point_lonlat)

    #test nearest lonlat
    check_all = check_data(date=datetime,
                           model="AromeArctic",
                           param=["longitude"],
                           step=1)
    file_all = check_all.file
    data_domain = domain_input_handler(dt=datetime,
                                       model="AromeArctic",
                                       domain_name="AromeArctic",
                                       file=file_all,
                                       domain_lonlat=None)
    dmap_meps = get_data(model="AromeArctic",
                         data_domain=data_domain,
                         param=["longitude"],
                         file=file_all,
                         step=1,
                         date=datetime)
    dmap_meps.retrieve()
    print("NUMBER GRID")
    print(number_grid)
    closest_idx = nearest_neighbour_idx(point_lonlat[0],
                                        point_lonlat[1],
                                        dmap_meps.longitude,
                                        dmap_meps.latitude,
                                        nmin=number_grid)

    x_llc = np.min(closest_idx[0])
    y_llc = np.min(closest_idx[1])
    x_urc = np.max(closest_idx[0])
    y_urc = np.max(closest_idx[1])
    #print(x_llc*2500)
    #print(y_llc * 2500)
    #print(x_urc * 2500)
    #print(y_urc * 2500)
    #return [x_llc*2500,y_llc * 2500, x_urc * 2500, y_urc * 2500]
    return [y_llc * 2500, x_llc * 2500, y_urc * 2500, x_urc * 2500]
Example #2
0
def area(datetime, domain_name, domain_lonlat):
    check_all = check_data(date=datetime, model="AromeArctic", step=0)
    file_all = check_all.file
    data_domain = domain_input_handler(dt=datetime,
                                       model="AromeArctic",
                                       domain_name=domain_name,
                                       domain_lonlat=domain_lonlat,
                                       file=file_all)
    iii = data_domain.idx
    xllc = min(iii[0])
    xurc = np.max(iii[0])
    yllc = min(*iii[1])
    yurc = max(iii[1])

    return [yllc * 2500, xllc * 2500, yurc * 2500, xurc * 2500]
Example #3
0
def Z500_VEL(datetime, steps=0, model= "MEPS", domain_name = None, domain_lonlat = None, legend=False, info = False,grid=True):

  for dt in datetime: #modelrun at time..
    date = dt[0:-2]
    hour = int(dt[-2:])
    param_sfc = ["air_pressure_at_sea_level", "surface_geopotential"]
    param_pl = ["air_temperature_pl", "geopotential_pl"]  #"air_temperature_2m",
    param_sfx = ["SST","SIC"] #add later
    p_levels = [850,1000]
    param = param_sfc + param_pl
    split = False
    print("\n######## Checking if your request is possible ############")
    try:
      check_all = check_data(date=dt, model=model, param=param, levtype="pl", p_level=p_levels, step=steps)
      check_sfx = check_data(date=dt, model=model, param=param_sfx, step=steps)

    except ValueError:
      split = True
      try:
        print("--------> Splitting up your request to find match ############")
        check_sfc = check_data(date=dt, model=model, param=param_sfc)
        check_pl = check_data(date=dt, model=model, param=param_pl, levtype="pl", p_level=p_levels)
        check_sfx = check_data(date=dt, model=model, param=param_sfx)

        print(check_pl.file)
      except ValueError:
        print("!!!!! Sorry this plot is not availbale for this date. Try with another datetime !!!!!")
        break
    print("--------> Found match for your request ############")


    if not split:
      file_all = check_all.file.loc[0]

      data_domain = domain_input_handler(dt, model,domain_name, domain_lonlat, file_all)

      #lonlat = np.array(data_domain.lonlat)
      dmap_meps = get_data(model=model, data_domain=data_domain, param=param, file=file_all, step=steps,
                           date=dt, p_level=p_levels)
      dmap_mepsdfx = get_data(model=model, data_domain=data_domain, param=param_sfx, file=check_sfx.file.loc[0], step=steps,date=dt)
      print("\n######## Retrieving data ############")
      print(f"--------> from: {dmap_meps.url} ")
      dmap_meps.retrieve()
      tmap_meps = dmap_meps # two names for same value, no copying done.
      dmap_mepsdfx.retrieve()
    else:
      # get sfc level data
      file_sfc = check_sfc.file.loc[0]
      data_domain = domain_input_handler(dt, model,domain_name, domain_lonlat, file_sfc)
      #lonlat = np.array(data_domain.lonlat)
      dmap_meps = get_data(model=model, param=param_sfc, file=file_sfc, step=steps, date=dt, data_domain=data_domain)
      print("\n######## Retrieving data ############")
      print(f"--------> from: {dmap_meps.url} ")
      dmap_meps.retrieve()

      # get pressure level data
      file_pl = check_pl.file.loc[0]
      tmap_meps = get_data(model=model, data_domain=data_domain, param=param_pl, file=file_pl, step=steps, date=dt, p_level=p_levels)
      print("\n######## Retrieving data ############")
      print(f"--------> from: {tmap_meps.url} ")
      tmap_meps.retrieve()

      dmap_mepsdfx = get_data(model=model, data_domain=data_domain, param=param_sfx, file=check_sfx.file.loc[0],
                              step=steps,
                              date=dt)
      dmap_mepsdfx.retrieve()

    #CALCULATE

    pt = potential_temperatur(dmap_meps.air_temperature_pl, dmap_meps.pressure*100.)
    pt_sst = potential_temperatur(dmap_mepsdfx.SST, dmap_meps.air_pressure_at_sea_level[:,0,:,:])

    dpt = pt[:,np.where(dmap_meps.pressure==1000)[0],:,:]-pt[:,np.where(dmap_meps.pressure==850)[0],:,:]
    dpt_sst =pt_sst[:,:,:] - pt[:,np.where(dmap_meps.pressure==850)[0],:,:].squeeze()
    #dpt_sst =abs(pt_sst[:,:,:] - pt[:,np.where(dmap_meps.pressure==850)[0],:,:].squeeze())

    # convert fields
    dmap_meps.air_pressure_at_sea_level/=100
    tmap_meps.geopotential_pl/=10.0


    lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
    lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
    parallels = dmap_meps.standard_parallel_projection_lambert

    # setting up projection
    # setting up projection
    globe = ccrs.Globe(ellipse='sphere', semimajor_axis=6371000., semiminor_axis=6371000.)
    crs = ccrs.LambertConformal(central_longitude=lon0, central_latitude=lat0, standard_parallels=parallels,
                                 globe=globe)

    for tim in np.arange(np.min(steps), np.max(steps)+1, 1):
      fig1, ax1 = plt.subplots(1, 1, figsize=(7, 9),
                               subplot_kw={'projection': crs})
      ttt = tim #+ np.min(steps)
      tidx = tim - np.min(steps)
      print('Plotting {0} + {1:02d} UTC'.format(dt, ttt))
      plev2 = 0
      embr = 0
      ZS = dmap_meps.surface_geopotential[tidx, 0, :, :]
      MSLP = np.where(ZS < 3000, dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :], np.NaN).squeeze()
      Z = (tmap_meps.geopotential_pl[tidx, plev2, :, :]).squeeze()
      DELTAPT=dpt[tidx, 0, :, :]
      DELTAPT = dpt_sst[tidx,:,:]
      ICE = dmap_mepsdfx.SIC[tidx, :, :]
      DELTAPT = np.where( ICE <= 0.99,DELTAPT,0)
      lvl = range(-1,13)
      C = [[255,255,255	],  # grey #[255,255,255],#gre
           [204,191,189	],  # grey
           [155,132,127	],  # grey
           [118,86,80],  # lillac, 39	64	197	149,53,229
           [138,109,81],  # blue dark,7,67,194 [218,81,14],
           [181,165,102],  #
           [229,226,124],  ##
           [213,250,128],
           [125,231,111],
           [55,212,95],
           [25,184,111],
           [17,138,234],
           [21,82,198],
           [37,34,137]]
      C = np.array(C)
      C = np.divide(C, 255.)  # RGB has to be between 0 and 1 in python
      CF_prec = plt.contourf(dmap_meps.x, dmap_meps.y, DELTAPT, zorder=0,
                            antialiased=True,extend = "max", levels=lvl, colors=C, vmin=0, vmax=12)#

      CF_ice = plt.contour(dmap_meps.x, dmap_meps.y, ICE, zorder=1, linewidths=2.5, colors="black", levels=[0.1, 0.5])  #
      # MSLP with contour labels every 10 hPa
      C_P = ax1.contour(dmap_meps.x, dmap_meps.y, MSLP, zorder=1, alpha=1.0,
                      levels=np.arange(round(np.nanmin(MSLP), -1) - 10, round(np.nanmax(MSLP), -1) + 10, 1),
                      colors='grey', linewidths=0.5)
      C_P = ax1.contour(dmap_meps.x, dmap_meps.y, MSLP, zorder=2, alpha=1.0,
                        levels=np.arange(round(np.nanmin(MSLP), -1) - 10, round(np.nanmax(MSLP), -1) + 10, 10),
                        colors='grey', linewidths=1.0)
      ax1.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)

      #CS = ax1.contour(dmap_meps.x, dmap_meps.y, Z, zorder=3, alpha=1.0,
      #                  levels=np.arange(4600, 5800, 20), colors="blue", linewidths=0.7)
      #ax1.clabel(CS, CS.levels, inline=True, fmt="%4.0f", fontsize=10)

      ax1.add_feature(cfeature.GSHHSFeature(scale='intermediate'))  # ‘auto’, ‘coarse’, ‘low’, ‘intermediate’, ‘high, or ‘full’ (default is ‘auto’).
      ax1.text(0, 1, "{0}_CAOi_{1}+{2:02d}".format(model, dt, ttt), ha='left', va='bottom', transform=ax1.transAxes, color='black')

      ##########################################################
      legend=True
      if legend:
        proxy = [plt.axhline(y=0, xmin=1, xmax=1, color="grey"),
                plt.axhline(y=0, xmin=1, xmax=1, color="black",linewidth=4)]
        try:
          ax_cb = adjustable_colorbar_cax(fig1, ax1)

          plt.colorbar(CF_prec,cax = ax_cb, fraction=0.046, pad=0.01, aspect=25, label=r"$\theta_{SST}-\theta_{850}$", extend="both")

        except:
          pass

        lg = ax1.legend(proxy, [f"MSLP [hPa]",
                               f"Sea ice at 10%, 80%, 99%"])
        frame = lg.get_frame()
        frame.set_facecolor('white')
        frame.set_alpha(1)
      make_modelrun_folder = setup_directory(OUTPUTPATH, "{0}".format(dt))
      if grid:
        nicegrid(ax=ax1)

      if domain_name != model and data_domain != None:  # weird bug.. cuts off when sees no data value
        ax1.set_extent(data_domain.lonlat)
      print("filename: "+make_modelrun_folder + "/{0}_{1}_CAOi_{2}+{3:02d}.png".format(model, domain_name, dt, ttt))
      fig1.savefig(make_modelrun_folder + "/{0}_{1}_CAOi_{2}+{3:02d}.png".format(model, domain_name, dt, ttt), bbox_inches="tight", dpi=200)
      ax1.cla()
      plt.clf()
      plt.close(fig1)
  plt.close("all")
Example #4
0
def checkget_data_handler(all_param,
                          date,
                          model,
                          step,
                          p_level=None,
                          m_level=None,
                          mbrs=None,
                          domain_name=None,
                          domain_lonlat=None,
                          point_name=None):
    fileobj = check_data(model, date=date, step=step).file
    print(fileobj)
    print(all_param)
    print("start finding choices")
    all_choices, bad_param = find_best_combinationoffiles(all_param=all_param,
                                                          fileobj=fileobj,
                                                          m_level=m_level,
                                                          p_level=p_level)
    bad_param_sfx = []
    if bad_param:
        new_bad = ["SFX_" + x for x in bad_param]
        all_param = all_param + new_bad
        all_choices, bad_param_sfx = find_best_combinationoffiles(
            all_param=all_param,
            fileobj=fileobj,
            m_level=m_level,
            p_level=p_level)
        print("bad_param")
        print(bad_param)
        print("bad_param_sfx")
        print(bad_param_sfx)
        #Ass SFX_param to it and try again.

    print(all_choices)
    print("stopped finding choices")

    # RETRIEVE FROM THE BEST COMBINATIONS AND TOWARDS WORSE COMBINATION IF ANY ERROR
    for i in range(0, len(all_choices)):

        try:
            print(
                "getting data"
            )  #our_choice,model,step, date,fileobj,m_level, domain_name=None, domain_lonlat=None
            dmet, data_domain, bad_param = retrievenow(
                our_choice=all_choices.loc[i],
                model=model,
                step=step,
                date=date,
                fileobj=fileobj,
                m_level=m_level,
                domain_name=domain_name,
                domain_lonlat=domain_lonlat,
                bad_param=bad_param,
                bad_param_sfx=bad_param_sfx,
                point_name=point_name)
            break
        except:
            #del (dmet)
            print("Oops!", sys.exc_info()[0], "occurred.")
            print("Next entry.")
            print(" ")
    #for i in range(0,bad_param):

    return dmet, data_domain, bad_param
Example #5
0
    param_ml = ["air_temperature_ml", "specific_humidity_ml"]
    param_sfc = [
        "surface_air_pressure", "air_pressure_at_sea_level",
        "air_temperature_0m", "air_temperature_2m", "relative_humidity_2m",
        "x_wind_gust_10m", "y_wind_gust_10m", "x_wind_10m", "y_wind_10m",
        "specific_humidity_2m", "precipitation_amount_acc",
        "convective_cloud_area_fraction", "cloud_area_fraction",
        "high_type_cloud_area_fraction", "medium_type_cloud_area_fraction",
        "low_type_cloud_area_fraction", "rainfall_amount", "snowfall_amount",
        "graupelfall_amount", "land_area_fraction"
    ]
    param_sfc = ["specific_humidity_2m"]
    all_param = param_sfc + param_ml + param_pl

    fileobj = check_data(args.model,
                         date=str(args.datetime[0]),
                         step=args.steps).file
    all_choices, bad_param = find_best_combinationoffiles(all_param, fileobj)

    #RETRIEVE FROM THE BEST COMBINATIONS AND TOWARDS WORSE COMBINATION IF ANY ERROR
    for i in range(0, len(all_choices)):
        try:
            dmet = retrievenow(all_choices.loc[i], args.model, args.steps,
                               str(args.datetime[0]))
            break
        except:
            #del(dmet)
            print("Oops!", sys.exc_info()[0], "occurred.")
            print("Next entry.")
            print(" ")
Example #6
0
def T850_RH(datetime,
            steps=0,
            model="MEPS",
            domain_name=None,
            domain_lonlat=None,
            legend=False,
            info=False,
            save=True):
    for dt in datetime:  #modelrun at time..
        date = dt[0:-2]
        hour = int(dt[-2:])
        param_sfc = [
            "air_pressure_at_sea_level", "air_temperature_2m",
            "precipitation_amount_acc", "surface_geopotential"
        ]
        param_pl = ["air_temperature_pl", "relative_humidity_pl"]
        param = param_sfc + param_pl
        #print(type(steps))
        split = False
        print("\n######## Checking if your request is possibel ############")
        try:
            check_all = check_data(date=dt,
                                   model=model,
                                   param=param,
                                   p_level=850)
        except ValueError:
            split = True
            try:
                print(
                    "--------> Splitting up your request to find match ############"
                )
                check_sfc = check_data(date=dt, model=model, param=param_sfc)
                check_pl = check_data(date=dt,
                                      model=model,
                                      param=param_pl,
                                      p_level=850)
            except ValueError:
                print(
                    "!!!!! Sorry this plot is not availbale for this date. Try with another datetime !!!!!"
                )
                break
        print("--------> Found match for your request ############")

        if not split:
            file_all = check_all.file.loc[0]

            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_all)

            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param,
                                 file=file_all,
                                 step=steps,
                                 date=dt,
                                 p_level=850)
            print("\n######## Retriving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()
            tmap_meps = dmap_meps  # two names for same value, no copying done.
        else:
            # get sfc level data
            file_sfc = check_sfc.file.loc[0]
            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_sfc)
            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 param=param_sfc,
                                 file=file_sfc,
                                 step=steps,
                                 date=dt,
                                 data_domain=data_domain)
            print("\n######## Retriving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()

            # get pressure level data
            file_pl = check_pl.file
            tmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param_pl,
                                 file=file_pl,
                                 step=steps,
                                 date=dt,
                                 p_level=850)
            print("\n######## Retriving data ############")
            print(f"--------> from: {tmap_meps.url} ")
            tmap_meps.retrieve()

        # convert fields
        dmap_meps.air_pressure_at_sea_level /= 100
        dmap_meps.air_temperature_2m -= 273.15
        tmap_meps.air_temperature_pl -= 273.15
        tmap_meps.relative_humidity_pl *= 100.0

        # plot map
        fig1, ax1 = plt.subplots(figsize=(7, 9))
        lonlat = [
            dmap_meps.longitude[0, 0], dmap_meps.longitude[-1, -1],
            dmap_meps.latitude[0, 0], dmap_meps.latitude[-1, -1]
        ]
        print(lonlat)

        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        map = Basemap(llcrnrlon=lonlat[0],
                      llcrnrlat=lonlat[2],
                      urcrnrlon=lonlat[1],
                      urcrnrlat=lonlat[3],
                      resolution='i',
                      projection="lcc",
                      lon_0=lon0,
                      lat_0=lat0,
                      lat_1=lat0,
                      area_thresh=0.0001)
        x, y = map(dmap_meps.longitude,
                   dmap_meps.latitude)  #longitude_of_central_meridian

        for tim in np.arange(np.min(steps), np.max(steps) + 1, 1):
            tidx = tim - np.min(steps)
            print('Plotting {0} + {1:02d} UTC'.format(dt, tim))
            # gather, filter and squeeze variables for plotting
            plev = 0
            #reduces noise over mountains by removing values over a certain height.
            Z = dmap_meps.surface_geopotential[tidx, 0, :, :]
            TA = np.where(Z < 3000, tmap_meps.air_temperature_pl[tidx,
                                                                 plev, :, :],
                          np.NaN).squeeze()
            MSLP = np.where(Z < 3000,
                            dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :],
                            np.NaN).squeeze()
            RH = (tmap_meps.relative_humidity_pl[tidx, plev, :, :]).squeeze()

            # clear subplot
            plt.cla()
            # MSLP with contour labels every 10 hPa
            C_P = plt.contour(x,
                              y,
                              MSLP,
                              zorder=1,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 1),
                              colors='grey',
                              linewidths=0.5)
            C_P = plt.contour(x,
                              y,
                              MSLP,
                              zorder=2,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 10),
                              colors='grey',
                              linewidths=1.0,
                              label="MSLP [hPa]")
            ax1.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)
            # air temperature (C)
            C_T = plt.contour(x,
                              y,
                              TA,
                              zorder=3,
                              alpha=1.0,
                              levels=np.arange(-50, 30, 0.5),
                              colors="red",
                              linewidths=0.7,
                              label="Temp [C]")
            # relative humidity above 80%
            CF_RH = plt.contourf(x,
                                 y,
                                 RH,
                                 zorder=1,
                                 alpha=0.1,
                                 levels=np.arange(80, 120, 20),
                                 colors="blue",
                                 linewidths=0.7,
                                 label="RH >80% [%]")
            map.drawcoastlines(linewidth=0.5, color='black', ax=ax1, zorder=1)

            proxy = [
                plt.Rectangle(
                    (0, 0),
                    1,
                    1,
                    fc=pc.get_facecolor()[0],
                ) for pc in CF_RH.collections
            ]
            proxy1 = [
                plt.axhline(y=0, xmin=1, xmax=1, color="red"),
                plt.axhline(y=0,
                            xmin=1,
                            xmax=1,
                            color="red",
                            linestyle="dashed"),
                plt.axhline(y=0, xmin=1, xmax=1, color="gray")
            ]
            proxy.extend(proxy1)

            if legend:
                lg = ax1.legend(proxy, [
                    f"RH > 80% [%] at {dmap_meps.pressure[plev]:.0f} hPa",
                    f"T>0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
                    f"T<0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
                    "MSLP [hPa]", ""
                ])
                frame = lg.get_frame()
                frame.set_facecolor('white')
                frame.set_alpha(1)

            if info:
                plt.text(
                    x=0,
                    y=-1,
                    s=
                    "INFO: Reduced topographic noise by filtering with surface_geopotential bellow 3000",
                    fontsize=7)  #, bbox=dict(facecolor='white', alpha=0.5))

            if save:
                fig2 = plt.figure(figsize=(2, 1.25))
                fig2.legend(proxy, [
                    f"RH > 80% [%] at {dmap_meps.pressure[plev]:.0f} hPa",
                    f"T>0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
                    f"T<0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
                    "MSLP [hPa]", ""
                ])
                fig2.savefig(
                    "../../../output/{0}_T850_RH_LEGEND.png".format(model),
                    bbox_inches="tight",
                    dpi=200)
                ##########################################################

                #plt.show()
                fig1.savefig(
                    "../../../output/{0}_T850_RH_{1}+{2:02d}.png".format(
                        model, dt, tim),
                    bbox_inches="tight",
                    dpi=200)
            else:
                return fig1
Example #7
0
def T850_RH(datetime,
            steps=0,
            model="MEPS",
            domain_name=None,
            domain_lonlat=None,
            legend=False,
            info=False,
            save=True,
            grid=True):
    for dt in datetime:  #modelrun at time..
        print(dt)
        date = dt[0:-2]
        hour = int(dt[-2:])
        param_sfc = [
            "air_pressure_at_sea_level", "air_temperature_2m",
            "surface_geopotential"
        ]
        param_pl = ["air_temperature_pl", "relative_humidity_pl"]
        param = param_sfc + param_pl
        #print(type(steps))
        split = False
        print("\n######## Checking if your request is possible ############")
        try:
            check_all = check_data(date=dt,
                                   model=model,
                                   param=param,
                                   p_level=850,
                                   step=steps)
        except ValueError:
            split = True
            try:
                print(
                    "--------> Splitting up your request to find match ############"
                )
                check_sfc = check_data(date=dt,
                                       model=model,
                                       param=param_sfc,
                                       step=steps)
                check_pl = check_data(date=dt,
                                      model=model,
                                      param=param_pl,
                                      p_level=850,
                                      step=steps)
            except ValueError:
                print(
                    "!!!!! Sorry this plot is not availbale for this date. Try with another datetime !!!!!"
                )
                break
        print("--------> Found match for your request ############")

        if not split:
            file_all = check_all.file.loc[0]

            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_all)

            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param,
                                 file=file_all,
                                 step=steps,
                                 date=dt,
                                 p_level=[850])
            print("\n######## Retrieving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()
            tmap_meps = dmap_meps  # two names for same value, no copying done.
        else:
            # get sfc level data
            file_sfc = check_sfc.file.loc[0]
            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_sfc)
            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 param=param_sfc,
                                 file=file_sfc,
                                 step=steps,
                                 date=dt,
                                 data_domain=data_domain)
            print("\n######## Retrieving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()

            # get pressure level data
            file_pl = check_pl.file
            tmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param_pl,
                                 file=file_pl,
                                 step=steps,
                                 date=dt,
                                 p_level=850)
            print("\n######## Retrieving data ############")
            print(f"--------> from: {tmap_meps.url} ")
            tmap_meps.retrieve()

        # convert fields
        dmap_meps.air_pressure_at_sea_level /= 100
        dmap_meps.air_temperature_2m -= 273.15
        tmap_meps.air_temperature_pl -= 273.15
        tmap_meps.relative_humidity_pl *= 100.0

        # plot map

        lonlat = [
            dmap_meps.longitude[0, 0], dmap_meps.longitude[-1, -1],
            dmap_meps.latitude[0, 0], dmap_meps.latitude[-1, -1]
        ]
        print(lonlat)

        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        parallels = dmap_meps.standard_parallel_projection_lambert

        # setting up projection
        globe = ccrs.Globe(ellipse='sphere',
                           semimajor_axis=6371000.,
                           semiminor_axis=6371000.)
        crs = ccrs.LambertConformal(central_longitude=lon0,
                                    central_latitude=lat0,
                                    standard_parallels=parallels,
                                    globe=globe)
        #fig1 = plt.figure(figsize=(7, 9))
        for tim in np.arange(np.min(steps), np.max(steps) + 1, 1):
            #ax1 = plt.subplot(projection=crs)

            fig1, ax1 = plt.subplots(1,
                                     1,
                                     figsize=(7, 9),
                                     subplot_kw={'projection': crs})
            ttt = tim
            tidx = tim - np.min(steps)

            print('Plotting {0} + {1:02d} UTC'.format(dt, tim))
            # gather, filter and squeeze variables for plotting
            plev = 0
            #reduces noise over mountains by removing values over a certain height.

            Z = dmap_meps.surface_geopotential[tidx, 0, :, :]
            TA = np.where(Z < 3000, tmap_meps.air_temperature_pl[tidx,
                                                                 plev, :, :],
                          np.NaN).squeeze()
            MSLP = np.where(Z < 3000,
                            dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :],
                            np.NaN).squeeze()
            RH = (tmap_meps.relative_humidity_pl[tidx, plev, :, :]).squeeze()
            # MSLP with contour labels every 10 hPa
            C_P = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              MSLP,
                              zorder=1,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 1),
                              colors='grey',
                              linewidths=0.5)
            C_P = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              MSLP,
                              zorder=2,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 10),
                              colors='grey',
                              linewidths=1.0,
                              label="MSLP [hPa]")
            ax1.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)
            # air temperature (C)
            C_T = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              TA,
                              zorder=3,
                              alpha=1.0,
                              levels=np.arange(-50, 30, 0.5),
                              colors="red",
                              linewidths=0.7)
            ax1.clabel(C_T,
                       C_T.levels[::3],
                       inline=True,
                       fmt="%3.0f",
                       fontsize=10)

            # relative humidity above 80%
            CF_RH = ax1.contourf(dmap_meps.x,
                                 dmap_meps.y,
                                 RH,
                                 zorder=1,
                                 alpha=0.1,
                                 levels=np.arange(80, 120, 20),
                                 colors="blue",
                                 linewidths=0.7,
                                 label="RH >80% [%]")

            #lat_p = 60.2
            #lon_p = 5.4167
            #mainpoint = ax1.scatter(lon_p, lat_p, s=9.0 ** 2, transform=ccrs.PlateCarree(),
            #                        color='lime', zorder=6, linestyle='None', edgecolors="k", linewidths=3)

            ax1.add_feature(
                cfeature.GSHHSFeature(scale='intermediate')
            )  #‘auto’, ‘coarse’, ‘low’, ‘intermediate’, ‘high, or ‘full’ (default is ‘auto’).
            ax1.text(0,
                     1,
                     "{0}_T850_RH_{1}+{2:02d}".format(model, dt, ttt),
                     ha='left',
                     va='bottom',
                     transform=ax1.transAxes,
                     color='black')

            legend = True
            if legend:
                proxy = [
                    plt.Rectangle(
                        (0, 0),
                        1,
                        1,
                        fc=pc.get_facecolor()[0],
                    ) for pc in CF_RH.collections
                ]
                proxy1 = [
                    plt.axhline(y=0, xmin=1, xmax=1, color="red"),
                    plt.axhline(y=0,
                                xmin=1,
                                xmax=1,
                                color="red",
                                linestyle="dashed"),
                    plt.axhline(y=0, xmin=1, xmax=1, color="gray")
                ]
                proxy.extend(proxy1)
                lg = ax1.legend(proxy, [
                    f"RH > 80% [%] at {dmap_meps.pressure[plev]:.0f} hPa",
                    f"T>0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
                    f"T<0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
                    "MSLP [hPa]", ""
                ])
                frame = lg.get_frame()
                frame.set_facecolor('white')
                frame.set_alpha(1)

            #if info:
            #  plt.text(x=0, y=-1, s="INFO: Reduced topographic noise by filtering with surface_geopotential bellow 3000",
            #           fontsize=7)#, bbox=dict(facecolor='white', alpha=0.5))

            ##########################################################

            #plt.show()

            #lonlat = [dmap_meps.longitude[0, 0], dmap_meps.longitude[-1, -1], dmap_meps.latitude[0, 0],
            #          dmap_meps.latitude[-1, -1]]
            # ax.set_extent((lonlat[0]-5, lonlat[1], lonlat[2], lonlat[3]))  # (x0, x1, y0, y1)
            # ax.set_extent((dmap_meps.x[0], dmap_meps.x[-1], dmap_meps.y[0], dmap_meps.y[-1]))  # (x0, x1, y0, y1)
            #ax1.set_extent((lonlat[0], lonlat[1], lonlat[2], lonlat[3]))
            #fig1.savefig("../../../../output/{0}_T850_RH_{1}_{2:02d}.png".format(model,dt, tim), bbox_inches="tight", dpi=200)
            make_modelrun_folder = setup_directory(OUTPUTPATH,
                                                   "{0}".format(dt))

            if grid:
                nicegrid(ax=ax1)

            if domain_name != model and data_domain != None:  # weird bug.. cuts off when sees no data value
                ax1.set_extent(data_domain.lonlat)

            fig1.savefig(make_modelrun_folder +
                         "/{0}_{1}_T850_RH_{2}+{3:02d}.png".format(
                             model, domain_name, dt, tim),
                         bbox_inches="tight",
                         dpi=200)
            ax1.cla()
            plt.clf()
            plt.close(fig1)

        #proxy = [plt.Rectangle((0, 0), 1, 1, fc=pc.get_facecolor()[0], )
        #        for pc in CF_RH.collections]
        #proxy1 = [plt.axhline(y=0, xmin=1, xmax=1, color="red"),eee
        #         plt.axhline(y=0, xmin=1, xmax=1, color="red", linestyle="dashed"),
        #         plt.axhline(y=0, xmin=1, xmax=1, color="gray")]
        #proxy.extend(proxy1)
        #fig2 = plt.figure(figsize=(2, 1.25))
        #fig2.legend(proxy, [f"RH > 80% [%] at {dmap_meps.pressure[plev]:.0f} hPa",
        #                 f"T>0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
        #                  f"T<0 [C] at {dmap_meps.pressure[plev]:.0f} hPa", "MSLP [hPa]", ""])
        #fig2.savefig("../../../output/{0}_T850_RH_LEGEND.png".format(model), bbox_inches="tight", dpi=200)

        ax1.cla()
        plt.clf()

    plt.close("all")
Example #8
0
def BLH(datetime,
        steps=0,
        model="MEPS",
        domain_name=None,
        domain_lonlat=None,
        legend=False,
        info=False,
        save=True,
        grid=True):
    for dt in datetime:  #modelrun at time..
        print(dt)
        date = dt[0:-2]
        hour = int(dt[-2:])
        param_sfc = [
            "air_pressure_at_sea_level", "surface_geopotential",
            "atmosphere_boundary_layer_thickness"
        ]
        param_pl = ["upward_air_velocity_pl"]
        param = param_sfc + param_pl
        #print(type(steps))
        split = False
        print("\n######## Checking if your request is possibel ############")
        try:
            check_all = check_data(date=dt,
                                   model=model,
                                   param=param,
                                   p_level=850,
                                   step=steps)
        except ValueError:
            split = True
            try:
                print(
                    "--------> Splitting up your request to find match ############"
                )
                check_sfc = check_data(date=dt,
                                       model=model,
                                       param=param_sfc,
                                       step=steps)
                check_pl = check_data(date=dt,
                                      model=model,
                                      param=param_pl,
                                      p_level=850,
                                      step=steps)
            except ValueError:
                print(
                    "!!!!! Sorry this plot is not availbale for this date. Try with another datetime !!!!!"
                )
                break
        print("--------> Found match for your request ############")

        if not split:
            file_all = check_all.file.loc[0]

            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_all)

            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param,
                                 file=file_all,
                                 step=steps,
                                 date=dt,
                                 p_level=[850])
            print("\n######## Retriving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()
            tmap_meps = dmap_meps  # two names for same value, no copying done.
        else:
            # get sfc level data
            file_sfc = check_sfc.file.loc[0]
            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_sfc)
            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 param=param_sfc,
                                 file=file_sfc,
                                 step=steps,
                                 date=dt,
                                 data_domain=data_domain)
            print("\n######## Retriving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()
            print("\n2########")

            # get pressure level data
            print(check_pl)
            file_pl = check_pl.file
            tmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param_pl,
                                 file=file_pl,
                                 step=steps,
                                 date=dt,
                                 p_level=850)
            print("\n######## Retriving data ############")
            print(f"--------> from: {tmap_meps.url} ")
            tmap_meps.retrieve()
            print("\n3########")

        # convert fields
        dmap_meps.air_pressure_at_sea_level /= 100
        #dmap_meps.air_temperature_2m -= 273.15
        #tmap_meps.air_temperature_pl -= 273.15
        #tmap_meps.relative_humidity_pl *= 100.0

        lonlat = [
            dmap_meps.longitude[0, 0], dmap_meps.longitude[-1, -1],
            dmap_meps.latitude[0, 0], dmap_meps.latitude[-1, -1]
        ]
        print(lonlat)

        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        parallels = dmap_meps.standard_parallel_projection_lambert

        # setting up projection
        globe = ccrs.Globe(ellipse='sphere',
                           semimajor_axis=6371000.,
                           semiminor_axis=6371000.)
        crs = ccrs.LambertConformal(central_longitude=lon0,
                                    central_latitude=lat0,
                                    standard_parallels=parallels,
                                    globe=globe)
        # plot map
        print("\nplotting")
        #fig1 = plt.figure(figsize=(7, 9), projection=crs)
        print("\nplotting")
        for tim in np.arange(np.min(steps), np.max(steps) + 1, 1):
            #ax1 = plt.subplot(projection=crs)
            fig1, ax1 = plt.subplots(1,
                                     1,
                                     figsize=(7, 9),
                                     subplot_kw={'projection': crs})
            ttt = tim  # + np.min(steps)

            tidx = tim - np.min(steps)

            print('Plotting {0} + {1:02d} UTC'.format(dt, tim))
            # gather, filter and squeeze variables for plotting
            plev = 0
            #reduces noise over mountains by removing values over a certain height.

            Z = dmap_meps.surface_geopotential[tidx, 0, :, :]
            W = np.where(Z < 3000,
                         tmap_meps.upward_air_velocity_pl[tidx, plev, :, :],
                         np.NaN).squeeze()
            MSLP = np.where(Z < 3000,
                            dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :],
                            np.NaN).squeeze()
            BLH = (tmap_meps.atmosphere_boundary_layer_thickness[tidx, :, :]
                   ).squeeze()
            # MSLP with contour labels every 10 hPa
            C_P = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              MSLP,
                              zorder=1,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 1),
                              colors='grey',
                              linewidths=0.5)
            C_P = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              MSLP,
                              zorder=2,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 10),
                              colors='grey',
                              linewidths=1.0,
                              label="MSLP [hPa]")
            ax1.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)
            # vertical velocity
            C_W = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              W,
                              zorder=3,
                              alpha=1.0,
                              levels=np.linspace(0.07, 2.0, 4),
                              colors="red",
                              linewidths=0.7)

            C_W = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              W,
                              zorder=3,
                              alpha=1.0,
                              levels=np.linspace(-2.0, -0.07, 4),
                              colors="blue",
                              linewidths=0.7)
            # boundary layer thickness
            CF_BLH = ax1.contourf(dmap_meps.x,
                                  dmap_meps.y,
                                  BLH,
                                  zorder=1,
                                  alpha=0.5,
                                  levels=np.arange(500, 5000, 500),
                                  linewidths=0.7,
                                  label="BLH",
                                  cmap="summer",
                                  extend="both")
            ax1.add_feature(cfeature.GSHHSFeature(scale='intermediate'))
            #ax1.set_extent(data_domain.lonlat)

            #divider = make_axes_locatable(ax1) ##__N
            #ax_cb = divider.new_horizontal(size="5%", pad=0.1, axes_class=plt.Axes) ##__N
            #fig1.add_axes(ax_cb) ##__N
            ax_cb = adjustable_colorbar_cax(fig1, ax1)
            cb = fig1.colorbar(CF_BLH,
                               fraction=0.046,
                               pad=0.01,
                               aspect=25,
                               cax=ax_cb,
                               label="Boundary layer thickness [m]",
                               extend="both")  ##__N
            #cb,fig1,ax1 = adjustable_colorbar(fig1,ax1,data= CF_BLH, fraction=0.046, pad=0.01,aspect=25, label="Boundary layer thickness [m]", extend="both")
            ax1.text(0, 1, "{0}_BLH_{1}+{2:02d}".format(model, dt, ttt), ha='left', va='bottom', \
                     transform=ax1.transAxes, color='dimgrey')
            legend = True

            if legend:
                #proxy = [plt.Rectangle((0, 0), 1, 1, fc=pc.get_facecolor()[0], )
                #        for pc in CF_RH.collections]
                proxy = [
                    plt.axhline(y=0, xmin=1, xmax=1, color="red"),
                    plt.axhline(y=0,
                                xmin=1,
                                xmax=1,
                                color="blue",
                                linestyle="dashed"),
                    plt.axhline(y=0, xmin=1, xmax=1, color="gray")
                ]
                #proxy.extend(proxy1)
                lg = ax1.legend(proxy, [
                    f"W [m s-1]>0.07 m/s at {dmap_meps.pressure[plev]:.0f} hPa",
                    f"W [m s-1]<0.07 m/s at {dmap_meps.pressure[plev]:.0f} hPa",
                    "MSLP [hPa]", ""
                ])
                frame = lg.get_frame()
                frame.set_facecolor('white')
                frame.set_alpha(1)

            #if info:
            #  plt.text(x=0, y=-1, s="INFO: Reduced topographic noise by filtering with surface_geopotential bellow 3000",
            #           fontsize=7)#, bbox=dict(facecolor='white', alpha=0.5))

            ##########################################################

            #plt.show()
            if grid:
                nicegrid(ax=ax1)
            if domain_name != model and data_domain != None:  # weird bug.. cuts off when sees no data value
                ax1.set_extent(data_domain.lonlat)
            #  print(data_domain.lonlat)
            make_modelrun_folder = setup_directory(OUTPUTPATH,
                                                   "{0}".format(dt))
            print("filename: " + make_modelrun_folder +
                  "/{0}_{1}_{2}_{3}+{4:02d}.png".format(
                      model, domain_name, "BLH", dt, ttt))
            fig1.savefig(make_modelrun_folder +
                         "/{0}_{1}_{2}_{3}+{4:02d}.png".format(
                             model, domain_name, "BLH", dt, ttt),
                         bbox_inches="tight",
                         dpi=200)
            ax1.cla()
            plt.clf()
            plt.close(fig1)

            #proxy = [plt.Rectangle((0, 0), 1, 1, fc=pc.get_facecolor()[0], )
            #       for pc in CF_RH.collections]
            #proxy1 = [plt.axhline(y=0, xmin=1, xmax=1, color="red"),
            #        plt.axhline(y=0, xmin=1, xmax=1, color="red", linestyle="dashed"),
            #        plt.axhline(y=0, xmin=1, xmax=1, color="gray")]
            #proxy.extend(proxy1)
            #fig2 = plt.figure(figsize=(2, 1.25))
            #fig2.legend(proxy, [f"RH > 80% [%] at {dmap_meps.pressure[plev]:.0f} hPa",
            #                 f"T>0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
            #                  f"T<0 [C] at {dmap_meps.pressure[plev]:.0f} hPa", "MSLP [hPa]", ""])
            #fig2.savefig("../../../output/{0}_T850_RH_LEGEND.png".format(model), bbox_inches="tight", dpi=200)

        ax1.cla()
        plt.clf()
    plt.close("all")
Example #9
0
def surf(datetime, steps=0, model=model, domain_name=None, domain_lonlat=None, legend=False, info=False,grid=True):
    for dt in datetime:  # modelrun at time..
        date = dt[0:-2]
        hour = int(dt[-2:])
        # param_sfc = ["relative_humidity_2m", "air_temperature_2m", "specific_humidity_2m", "air_pressure_at_sea_level"]
        param_sfc = ["air_temperature_2m", "specific_humidity_2m", "air_pressure_at_sea_level", "surface_geopotential"]
        param_sfx = ["SST", "SIC"]
        param_pl = []
        param = param_sfc + param_pl
        split = False
        print("\n######## Checking if your request is possibel ############")
        try:
            check_all = check_data(date=dt, model=model, param=param, step=steps)
            check_sfx = check_data(date=dt, model=model, param=param_sfx, step=steps)

            print(check_all.file)

        except ValueError:
            print("!!!!! Sorry this plot is not availbale for this date. Try with another datetime !!!!!")
            break
        print("--------> Found match for your request ############")

        if not split:
            file_all = check_all.file.loc[0]
            file_sfx = check_sfx.file.loc[0]

            data_domain = domain_input_handler(dt, model, domain_name, domain_lonlat, file_all)

            # lonlat = np.array(data_domain.lonlat)
            print(file_all)
            dmap_meps = get_data(model=model, data_domain=data_domain, param=param, file=file_all, step=steps,
                                 date=dt)
            dmap_meps_sfx = get_data(model=model, data_domain=data_domain, param=param_sfx, file=file_sfx, step=steps,
                                     date=dt)
            print("\n######## Retriving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()
            tmap_meps = dmap_meps  # two names for same value, no copying done.
            dmap_meps_sfx.retrieve()

        # convert fields
        # dmap_meps.air_pressure_at_sea_level/=100
        # u,v = xwind2uwind(tmap_meps.x_wind_10m,tmap_meps.y_wind_10m, tmap_meps.alpha)
        # vel = wind_speed(tmap_meps.x_wind_10m,tmap_meps.y_wind_10m)

        # plot map
        fig1 = plt.figure(figsize=(7, 9))

        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        parallels = dmap_meps.standard_parallel_projection_lambert

        # setting up projection
        globe = ccrs.Globe(ellipse='sphere', semimajor_axis=6371000., semiminor_axis=6371000.)
        data = ccrs.LambertConformal(central_longitude=lon0, central_latitude=lat0, standard_parallels=parallels,
                                     globe=globe)
        crs = data
        crs_lon = ccrs.PlateCarree()
        # crs = ccrs.PlateCarree()
        for tim in np.arange(np.min(steps), np.max(steps) + 1, 1):
            fig1, ax1 = plt.subplots(1, 1, figsize=(7, 9),
                                     subplot_kw={'projection': crs})

            ttt = tim  # + np.min(steps)
            tidx = tim - np.min(steps)
            print('Plotting {0} + {1:02d} UTC'.format(dt, ttt))
            SI = dmap_meps_sfx.SIC[tidx, :, :].squeeze()
            SImask = np.where(SI >= 0.1, dmap_meps_sfx.SIC[tidx, :, :], np.NaN).squeeze()

            ZS = dmap_meps.surface_geopotential[tidx, 0, :, :]
            MSLP = np.where(ZS < 3000, dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :], np.NaN).squeeze() / 100
            # print("SIC: {}, {}".format(min(MSLP[0][:]), max(MSLP[0][:])))
            # SST = np.where(SI == 0, dmap_meps_sfx.SST[tidx, :, :], np.NaN).squeeze()
            # TP = precip_acc(dmap_meps.precipitation_amount_acc, acc=1)[tidx, 0, :,:].squeeze()
            # L = dmap_meps_sfx.LE[tidx,:,:].squeeze()
            # L = np.where(ZS < 3000, L, np.NaN).squeeze()
            # SH = dmap_meps_sfx.H[tidx,:,:].squeeze()
            # SH = np.where(ZS < 3000, SH, np.NaN).squeeze()
            SST = dmap_meps_sfx.SST[tidx, :, :].squeeze() - 273.15
            es = 6.1094 * np.exp(17.625 * SST / (SST + 243.04))
            mslp = dmap_meps.air_pressure_at_sea_level[tidx, :, :].squeeze() / 100
            if model == 'AromeArctic':
                Q = dmap_meps.specific_humidity_2m[tidx, :, :].squeeze()
            else:
                Q = 1
                # AT = dmap_meps.air_temperature_2m[tidx,:,:].squeeze()
                # w =
                # Q = w / (w+1)
            Qs = 0.622 * es / (mslp - 0.37 * es)
            # RH_2m = dmap_meps.relative_humidity_2m[tidx,:,:].squeeze()*100
            RH = Q / Qs * 100
            # print("RH: {}".format(RH_2m[0][0]))
            d = 48.2 - 0.54 * RH
            # Ux = dmap_meps.x_wind_10m[tidx, 0, :, :].squeeze()
            # Vx = dmap_meps.y_wind_10m[tidx, 0, :, :].squeeze()
            # xm,ym = np.meshgrid(dmap_meps.x, dmap_meps.y)

            # VELOCITY
            # new_x, new_y, new_u, new_v, = vector_scalar_to_grid(src_crs= data, target_proj= crs_lon,regrid_shape = np.shape(Ux), x= dmap_meps.x, y= dmap_meps.y, u= Ux, v= Vx)
            # magnitude = (new_u ** 2 + new_v ** 2) ** 0.5
            # cmap = plt.get_cmap("viridis") #cividis copper
            # wii = Axes.streamplot(ax1, new_x, new_y, new_u, new_v, density=4,zorder=4,transform=crs_lon, linewidth=0.7, color=magnitude, cmap=cmap)
            # LATENT
            # levelspos=np.arange(80, round(np.nanmax(L), -1) + 10, 40)
            # levelsneg = np.arange(-300, -9, 10)
            # levels = np.append(levelsneg, levelspos)
            # CL = ax1.contour(dmap_meps.x, dmap_meps.y, L, zorder=3, alpha=1.0, colors="red", linewidths=0.7, levels=levels, transform=data)
            # ax1.clabel(CL, CL.levels[::2], inline=True, fmt="%3.0f", fontsize=10)
            # xx = np.where(L < -10, xm, np.NaN).squeeze()
            # yy = np.where(L < -10, ym, np.NaN).squeeze()
            # skip = (slice(None, None, 4), slice(None, None, 4))
            # ax1.scatter(xx[skip][skip], yy[skip][skip], s=20, zorder=2, marker='x', linewidths=0.9,
            #                         c="white", alpha=0.7, transform=data)
            # xx = np.where(L > 80, xm, np.NaN).squeeze()
            # yy = np.where(L > 80, ym, np.NaN).squeeze()
            # skip = (slice(None, None, 4), slice(None, None, 4))
            # ax1.scatter(xx[skip][skip], yy[skip][skip], s=20, zorder=2, marker='.', linewidths=0.9,
            #             c="black", alpha=0.7, transform=data)

            # SENSIBLE
            # levelspos = np.arange(80, round(np.nanmax(SH), -1) + 10, 40)
            # levelsneg = np.arange(-300, -9, 10)
            # levels = np.append(levelsneg, levelspos)
            # CSH = ax1.contour(dmap_meps.x, dmap_meps.y, SH, zorder=3, alpha=1.0, colors="blue", linewidths=0.7, levels=levels, transform=data)
            # ax1.clabel(CSH, CSH.levels[1::2], inline=True, fmt="%3.0f", fontsize=10)
            # xx = np.where(SH < -10, xm, np.NaN).squeeze()
            # yy = np.where(SH < -10, ym, np.NaN).squeeze()
            # skip = (slice(None, None, 4), slice(None, None, 4))
            # ax1.scatter(xx[skip][skip],yy[skip][skip],s=20, zorder=2, marker='x',linewidths=0.9, c= "white", alpha=0.7, transform=data)

            # xx = np.where(SH >80, xm, np.NaN).squeeze()
            # yy = np.where(SH >80, ym, np.NaN).squeeze()
            # skip = (slice(None, None, 4), slice(None, None, 4))
            # ax1.scatter(xx[skip][skip], yy[skip][skip], s=20, zorder=2, marker='.', linewidths=0.9, c="black", alpha=0.7,
            #             transform=data)

            # SST
            levels = np.arange(-10, 45, 5)
            cmap = plt.get_cmap("cividis_r")
            Cd = ax1.contourf(dmap_meps.x, dmap_meps.y, d, zorder=1, alpha=0.7, cmap=cmap, levels=levels, extend="both",
                              transform=data)
            Cd10 = ax1.contour(dmap_meps.x, dmap_meps.y, d, zorder=1, alpha=0.7, colors='tab:blue', levels=[10],
                               transform=data)
            SI = ax1.contourf(dmap_meps.x, dmap_meps.y, SImask, zorder=2, alpha=1, colors='azure', transform=data)
            ax1.contour(dmap_meps.x, dmap_meps.y, SImask, zorder=2, alpha=1, colors='black', levels=[0.15],
                        transform=data, linestyles='--')

            C_P = ax1.contour(dmap_meps.x, dmap_meps.y, MSLP, zorder=4, alpha=1.0,
                              levels=np.arange(round(np.nanmin(MSLP), -1) - 10, round(np.nanmax(MSLP), -1) + 10, 2),
                              colors='dimgrey', linewidths=0.5)
            C_P = ax1.contour(dmap_meps.x, dmap_meps.y, MSLP, zorder=4, alpha=1.0,
                              levels=np.arange(round(np.nanmin(MSLP), -1) - 10, round(np.nanmax(MSLP), -1) + 10, 10),
                              colors='dimgrey', linewidths=1.0)
            ax1.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)

            ax1.add_feature(cfeature.GSHHSFeature(scale='intermediate'), zorder=3,
                            facecolor="whitesmoke")  # ‘auto’, ‘coarse’, ‘low’, ‘intermediate’, ‘high, or ‘full’ (default is ‘auto’).
            ax1.text(0, 1, "{0}_dxs_{1}+{2:02d}".format(model, dt, ttt), ha='left', va='bottom', \
                     transform=ax1.transAxes, color='black')
            ##########################################################
            # handles, labels = ax1.get_legend_handles_labels()
            legend = True
            if legend:
                proxy = [plt.axhline(y=0, xmin=1, xmax=1, color="tab:blue"), \
                         plt.axhline(y=0, xmin=1, xmax=1, color="dimgrey", linewidth=0.5), \
                         plt.axhline(y=0, xmin=1, xmax=1, color="black", linestyle='--'), \
                         ]
                lg = plt.legend(proxy, ["d-xs=10", "MSLP [hPa]", "Sea Ice conc. >10%"], loc=1)


                # cb = plt.colorbar(CSST, fraction=0.046, pad=0.01, ax=ax1, aspect=25, label ="RH [%]", extend = "both")
                ax_cb = adjustable_colorbar_cax(fig1, ax1)
                cb = plt.colorbar(Cd, fraction=0.046, pad=0.01, ax=ax1, aspect=25, cax= ax_cb, label="d-excess [$\perthousand$]",
                                  extend="both")

                frame = lg.get_frame()
                frame.set_facecolor('lightgray')
                frame.set_alpha(1)
            make_modelrun_folder = setup_directory(OUTPUTPATH, "{0}".format(dt))
            print("filename: " + make_modelrun_folder + "/{0}_{1}_dxs_{2}+{3:02d}.png".format(model, domain_name, dt,
                                                                                             ttt))
            if grid:
                nicegrid(ax=ax1)
            if domain_name != model and data_domain != None:  # weird bug.. cuts off when sees no data value
                ax1.set_extent(data_domain.lonlat)
            fig1.savefig(make_modelrun_folder + "/{0}_{1}_dxs_{2}+{3:02d}.png".format(model, domain_name, dt, ttt),
                         bbox_inches="tight", dpi=200)
            ax1.cla()
            plt.clf()
            plt.close(fig1)
    plt.close("all")
Example #10
0
def flexpart_EC(datetime,
                steps=0,
                model="MEPS",
                domain_name=None,
                domain_lonlat=None,
                legend=False,
                info=False,
                save=True,
                grid=True):
    for dt in datetime:  #modelrun at time..
        print(dt)
        date = dt[0:-2]
        hour = int(dt[-2:])
        param_sfc = ["air_pressure_at_sea_level", "surface_geopotential"]
        param = param_sfc
        #print(type(steps))
        split = False
        print("\n######## Checking if your request is possible ############")
        try:
            check_all = check_data(date=dt,
                                   model=model,
                                   param=param,
                                   p_level=850,
                                   step=steps)
        except ValueError:
            split = True
            try:
                print(
                    "--------> Splitting up your request to find match ############"
                )
                check_sfc = check_data(date=dt,
                                       model=model,
                                       param=param_sfc,
                                       step=steps)
                #check_pl = check_data(date=dt, model=model, param=param_pl, p_level=850,step=steps)
            except ValueError:
                print(
                    "!!!!! Sorry this plot is not availbale for this date. Try with another datetime !!!!!"
                )
                break
        print("--------> Found match for your request ############")

        if not split:
            file_all = check_all.file.loc[0]

            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_all)

            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param,
                                 file=file_all,
                                 step=steps,
                                 date=dt,
                                 p_level=[850])
            print("\n######## Retrieving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()
            tmap_meps = dmap_meps  # two names for same value, no copying done.
        else:
            # get sfc level data
            file_sfc = check_sfc.file.loc[0]
            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_sfc)
            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 param=param_sfc,
                                 file=file_sfc,
                                 step=steps,
                                 date=dt,
                                 data_domain=data_domain)
            print("\n######## Retrieving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()

            # get pressure level data
            #file_pl = check_pl.file
            #tmap_meps = get_data(model=model, data_domain=data_domain, param=param_pl, file=file_pl, step=steps, date=dt, p_level = 850)
            #print("\n######## Retrieving data ############")
            #print(f"--------> from: {tmap_meps.url} ")
            #tmap_meps.retrieve()

        # convert fields
        dmap_meps.air_pressure_at_sea_level /= 100

        # read netcdf files with flexpart output
        release_name = 'NYAlesund_S1'
        path = "/home/centos/flexpart-arome/{0}/{1}*/flexpart_run_d01_combined.nc".format(
            dt, release_name)
        findpath = glob.glob(path)
        print(findpath)
        cdf = nc.Dataset(
            findpath[0], "r"
        )  #"/home/centos/flexpart/{0}/grid_conc_{1}0000.nc".format(release_name,dt), "r")
        lats = cdf.variables["XLAT"][:]
        lons = cdf.variables["XLONG"][:]
        #lons, lats = np.meshgrid(lons, lats)
        tim = cdf.variables["time"][:]
        levs = cdf.variables["ZTOP"][:]
        spec1a = cdf.variables["CONC"][:]

        release_name = 'Tromso_S1'
        path = "/home/centos/flexpart-arome/{0}/{1}*/flexpart_run_d01_combined.nc".format(
            dt, release_name)
        findpath = glob.glob(path)
        cdf = nc.Dataset(
            findpath[0], "r"
        )  #"/home/centos/flexpart/{0}/grid_conc_{1}0000.nc".format(release_name,dt), "r")
        spec1b = cdf.variables["CONC"][:]

        # plot map
        lonlat = [
            dmap_meps.longitude[0, 0], dmap_meps.longitude[-1, -1],
            dmap_meps.latitude[0, 0], dmap_meps.latitude[-1, -1]
        ]
        print(lonlat)

        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        parallels = dmap_meps.standard_parallel_projection_lambert

        # setting up projection
        globe = ccrs.Globe(ellipse='sphere',
                           semimajor_axis=6371000.,
                           semiminor_axis=6371000.)
        crs = ccrs.LambertConformal(central_longitude=lon0,
                                    central_latitude=lat0,
                                    standard_parallels=parallels,
                                    globe=globe)
        #fig1 = plt.figure(figsize=(7, 9))
        make_modelrun_folder = setup_directory(OUTPUTPATH, "{0}".format(dt))

        for tim in np.arange(np.min(steps), np.max(steps) + 1, 1):
            #ax1 = plt.subplot(projection=crs)

            # determine if image should be created for this time step
            stepok = False
            if tim < 25:
                stepok = True
            elif (tim <= 36) and ((tim % 3) == 0):
                stepok = True
            elif (tim <= 66) and ((tim % 6) == 0):
                stepok = True
            if stepok == True:

                l = 0
                for lev in levs[0:8]:

                    fig1, ax1 = plt.subplots(1,
                                             1,
                                             figsize=(7, 9),
                                             subplot_kw={'projection': crs})
                    ttt = tim
                    tidx = tim - np.min(steps)

                    if lev >= 5000:  # TOC for last levels
                        spec2a = np.sum(spec1a[tim, :, :, :], 0).squeeze()
                        spec2b = np.sum(spec1b[tim, :, :, :], 0).squeeze()
                        lev = 0
                    else:
                        spec2a = (spec1a[tim, l, :, :]).squeeze()
                        spec2b = (spec1b[tim, l, :, :]).squeeze()
                        l = l + 1
                    print(np.shape(spec2a))
                    print(np.shape(lons))
                    print(np.shape(lats))
                    #print(tidx)
                    #print(lev)
                    #print(np.min(spec2))
                    print(np.max(spec2a))
                    #spec2[:,:]=0.01
                    spec2a = np.where(spec2a > 1e-10, spec2a, np.NaN)
                    spec2b = np.where(spec2b > 1e-10, spec2b, np.NaN)

                    print('Plotting FLEXPART-EC {0} + {1:02d} UTC, level {2}'.
                          format(dt, tim, lev))
                    # gather, filter and squeeze variables for plotting
                    plev = 0
                    #reduces noise over mountains by removing values over a certain height.

                    Z = dmap_meps.surface_geopotential[tidx, 0, :, :]
                    MSLP = np.where(
                        Z < 50000,
                        dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :],
                        np.NaN).squeeze()
                    F_P = ax1.pcolormesh(lons,
                                         lats,
                                         spec2a,
                                         norm=colors.LogNorm(vmin=1e-10,
                                                             vmax=0.2),
                                         cmap=plt.cm.Reds,
                                         zorder=1,
                                         alpha=0.9,
                                         transform=ccrs.PlateCarree())
                    F_P = ax1.pcolormesh(lons,
                                         lats,
                                         spec2b,
                                         norm=colors.LogNorm(vmin=1e-10,
                                                             vmax=0.2),
                                         cmap=plt.cm.Blues,
                                         zorder=1,
                                         alpha=0.9,
                                         transform=ccrs.PlateCarree())
                    del spec2a
                    # MSLP with contour labels every 10 hPa
                    C_P = ax1.contour(dmap_meps.x,
                                      dmap_meps.y,
                                      MSLP,
                                      zorder=3,
                                      alpha=1.0,
                                      levels=np.arange(960, 1050, 1),
                                      colors='grey',
                                      linewidths=0.5,
                                      transform=crs)
                    C_P = ax1.contour(dmap_meps.x,
                                      dmap_meps.y,
                                      MSLP,
                                      zorder=4,
                                      alpha=1.0,
                                      levels=np.arange(960, 1050, 10),
                                      colors='grey',
                                      linewidths=1.0,
                                      label="MSLP [hPa]",
                                      transform=crs)
                    ax1.clabel(C_P,
                               C_P.levels,
                               inline=True,
                               fmt="%3.0f",
                               fontsize=10)

                    ax1.add_feature(
                        cfeature.GSHHSFeature(scale='intermediate'))
                    ax1.text(0,
                             1,
                             "{0}_FP_{1}+{2:02d}".format(model, dt, ttt),
                             ha='left',
                             va='bottom',
                             transform=ax1.transAxes,
                             color='black')

                    legend = False
                    if legend:
                        proxy = [
                            plt.Rectangle(
                                (0, 0),
                                1,
                                1,
                                fc=pc.get_facecolor()[0],
                            ) for pc in CF_T.collections
                        ]
                        proxy1 = [
                            plt.axhline(y=0, xmin=1, xmax=1, color="red"),
                            plt.axhline(y=0,
                                        xmin=1,
                                        xmax=1,
                                        color="red",
                                        linestyle="dashed"),
                            plt.axhline(y=0, xmin=1, xmax=1, color="gray")
                        ]
                        proxy.extend(proxy1)
                        lg = ax1.legend(proxy, [
                            f"RH > 80% [%] at {dmap_meps.pressure[plev]:.0f} hPa",
                            f"T>0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
                            f"T<0 [C] at {dmap_meps.pressure[plev]:.0f} hPa",
                            "MSLP [hPa]", ""
                        ])
                        frame = lg.get_frame()
                        frame.set_facecolor('white')
                        frame.set_alpha(1)

                    #if info:
                    #  plt.text(x=0, y=-1, s="INFO: Reduced topographic noise by filtering with surface_geopotential bellow 3000",
                    #           fontsize=7)#, bbox=dict(facecolor='white', alpha=0.5))

                    ##########################################################

                    #plt.show()

                    #lonlat = [dmap_meps.longitude[0, 0], dmap_meps.longitude[-1, -1], dmap_meps.latitude[0, 0],
                    #          dmap_meps.latitude[-1, -1]]
                    # ax.set_extent((lonlat[0]-5, lonlat[1], lonlat[2], lonlat[3]))  # (x0, x1, y0, y1)
                    # ax.set_extent((dmap_meps.x[0], dmap_meps.x[-1], dmap_meps.y[0], dmap_meps.y[-1]))  # (x0, x1, y0, y1)
                    #ax1.set_extent((lonlat[0], lonlat[1], lonlat[2], lonlat[3]))
                    #fig1.savefig("../../../../output/{0}_T2M_{1}_{2:02d}.png".format(model,dt, tim), bbox_inches="tight", dpi=200)

                    if grid:
                        nicegrid(ax=ax1)

                    #if domain_name != model and data_domain != None:  # weird bug.. cuts off when sees no data value
                    ax1.set_extent(lonlat)

                    model = 'FLEXPART_AA'
                    print(make_modelrun_folder +
                          "/{0}_{1}_L{2:05.0f}_{3}+{4:02d}.png".format(
                              model, domain_name, lev, dt, tim))
                    fig1.savefig(make_modelrun_folder +
                                 "/{0}_{1}_L{2:05.0f}_{3}+{4:02d}.png".format(
                                     model, domain_name, lev, dt, tim),
                                 bbox_inches="tight",
                                 dpi=200)
                    ax1.cla()
                    plt.clf()
                    plt.close(fig1)

    plt.close("all")
Example #11
0
def IWC_LWC(datetime, steps=0, model= "MEPS", domain_name = None, domain_lonlat = None, legend=False, info = False,grid=True,m_level = [0, 64]):

    for dt in datetime:  # modelrun at time..
        date = dt[0:-2]
        hour = int(dt[-2:])

        param = ['mass_fraction_of_cloud_condensed_water_in_air_ml',
               'mass_fraction_of_cloud_ice_in_air_ml',"air_pressure_at_sea_level","surface_geopotential"]
        check_all = check_data(date=dt, model=model, param=param, levtype="ml", m_level=m_level, step=steps)
        print(check_all.file)
        file_all = check_all.file.loc[0]

        data_domain = domain_input_handler(dt, model, domain_name, domain_lonlat, file_all)

        # lonlat = np.array(data_domain.lonlat)
        print(m_level)
        dmap_meps = get_data(model=model, data_domain=data_domain, param=param, file=file_all, step=steps,
                             date=dt, m_level=m_level)
        print("\n######## Retrieving data ############")
        print(f"--------> from: {dmap_meps.url} ")
        dmap_meps.retrieve()
        dmap_meps.air_pressure_at_sea_level /= 100

        #CALCULATE
        dmap_meps.LWC = np.sum(dmap_meps.mass_fraction_of_cloud_condensed_water_in_air_ml[:,:,:,:],axis=1)
        dmap_meps.LWC =dmap_meps.LWC*1000
        dmap_meps.units.LWC = "g/kg"
        dmap_meps.IWC = np.sum(dmap_meps.mass_fraction_of_cloud_ice_in_air_ml[:,:,:,:],axis=1)
        dmap_meps.IWC = dmap_meps.IWC*1000
        dmap_meps.units.IWC = "g/kg"
        del dmap_meps.mass_fraction_of_cloud_condensed_water_in_air_ml
        del dmap_meps.mass_fraction_of_cloud_ice_in_air_ml
        # for more normal unit of g/m^2 read
        # #https://www.nwpsaf.eu/site/download/documentation/rtm/docs_rttov12/rttov_gas_cloud_aerosol_units.pdf
        # https://www.researchgate.net/post/How-to-convert-the-units-of-specific-cloud-liquid-water-from-ERA5-kg-kg-to-kg-m2
        dmap_meps.LWC[np.where(dmap_meps.LWC < 0.1)] = np.nan
        dmap_meps.IWC[np.where(dmap_meps.IWC < 0.01)] = np.nan

        #It is a bug in pcolormesh. supposedly newest is correct, but not older versions. Invalid corner values set to nan
        #https://github.com/matplotlib/basemap/issues/470
        x,y = np.meshgrid(dmap_meps.x, dmap_meps.y)
        #dlon,dlat=  np.meshgrid(dmap_meps.longitude, dmap_meps.latitude)

        nx, ny = x.shape
        mask = (
          (x[:-1, :-1] > 1e20) |
          (x[1:, :-1] > 1e20) |
          (x[:-1, 1:] > 1e20) |
          (x[1:, 1:] > 1e20) |
          (x[:-1, :-1] > 1e20) |
          (x[1:, :-1] > 1e20) |
          (x[:-1, 1:] > 1e20) |
          (x[1:, 1:] > 1e20)
        )

        # plot map
        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        parallels = dmap_meps.standard_parallel_projection_lambert

        globe = ccrs.Globe(ellipse='sphere', semimajor_axis=6371000., semiminor_axis=6371000.)
        crs = ccrs.LambertConformal(central_longitude=lon0, central_latitude=lat0, standard_parallels=parallels,
                                         globe=globe)
        make_modelrun_folder = setup_directory(OUTPUTPATH, "{0}".format(dt))

        for tim in np.arange(np.min(steps), np.max(steps)+1, 1):
                                      
            #ax1 = plt.subplot(projection=crs)

            # determine if image should be created for this time step
            stepok=False
            if tim<25:
                stepok=True
            elif (tim<=36) and ((tim % 3) == 0):
                stepok=True
            elif (tim<=66) and ((tim % 6) == 0):
                stepok=True
            if stepok==True:

                fig1, ax1 = plt.subplots(1, 1, figsize=(7, 9),subplot_kw={'projection': crs})
                ttt = tim #+ np.min(steps)
                tidx = tim - np.min(steps)

                ZS = dmap_meps.surface_geopotential[tidx, 0, :, :]
                MSLP = np.where(ZS < 3000, dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :], np.NaN).squeeze()

                #pcolor as pcolormesh and  this projection is not happy together. If u want faster, try imshow
                #dmap_meps.LWC[np.where( dmap_meps.LWC <= 0.09)] = np.nan
                data =  dmap_meps.LWC[tidx,:nx - 1, :ny - 1].copy()
                data[mask] = np.nan
                CC=ax1.pcolormesh(x, y,  data[:, :], cmap=plt.cm.Reds, vmin=0.1, vmax=4.0,zorder=2)
                data =  dmap_meps.IWC[tidx,:nx - 1, :ny - 1].copy()
                data[mask] = np.nan
                CI= ax1.pcolormesh(x, y, data[:, :], cmap=plt.cm.Blues,alpha=0.5, vmin=0.01, vmax=0.1,zorder=3)

                # MSLP
                # MSLP with contour labels every 10 hPa
                C_P = ax1.contour(dmap_meps.x, dmap_meps.y, MSLP, zorder=4, alpha=1.0,
                                  levels=np.arange(960, 1050, 1),
                                  colors='grey', linewidths=0.5)
                C_P = ax1.contour(dmap_meps.x, dmap_meps.y, MSLP, zorder=5, alpha=1.0,
                                  levels=np.arange(960, 1050, 10),
                                  colors='grey', linewidths=1.0, label="MSLP [hPa]")
                ax1.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)


                ax1.add_feature(cfeature.GSHHSFeature(scale='intermediate'),zorder=6,facecolor="none",edgecolor="gray")  # ‘auto’, ‘coarse’, ‘low’, ‘intermediate’, ‘high, or ‘full’ (default is ‘auto’).
                if domain_name != model and data_domain !=None: #weird bug.. cuts off when sees no data value
                     ax1.set_extent(data_domain.lonlat)
                ax1.text(0, 1, "{0}_LWC_IWC_{1}_{2}+{3:02d}".format(model,m_level, dt, ttt), ha='left', va='bottom', \
                                       transform=ax1.transAxes, color='black')
                print("filename: "+make_modelrun_folder + "/{0}_{1}_{2}_{3}+{4:02d}.png".format(model, domain_name, "LWP_IWP", dt, ttt))
                if grid:
                     nicegrid(ax=ax1)

                legend = True
                if legend:
                    cbar = nice_vprof_colorbar(CF=CI, ax=ax1, extend="max",label='IWC [g/kg]',x0=0.75,y0=0.95,width=0.26,height=0.05,
                                               format='%.3f', ticks=[0.001, np.nanmax(dmap_meps.IWC[tidx, :, :])*0.8])
                    cbar = nice_vprof_colorbar(CF=CC, ax=ax1, extend="max", label='LWC [g/kg]',x0=0.50,y0=0.95,width=0.26,height=0.05,
                                              format='%.1f',ticks=[0.09, np.nanmax(dmap_meps.LWC[tidx, :, :])*0.8])
                    proxy = [plt.axhline(y=0, xmin=0, xmax=0, color="gray",zorder=7)]
                    # proxy.extend(proxy1)
                    lg = ax1.legend(proxy, ["MSLP [hPa]"])
                    frame = lg.get_frame()
                    frame.set_facecolor('white')
                    frame.set_alpha(0.8)

                fig1.savefig(make_modelrun_folder +"/{0}_{1}_{2}_{3}+{4:02d}.png".format(model, domain_name, "clouds", dt, ttt), bbox_inches="tight", dpi=200)
                ax1.cla()
                plt.clf()
                plt.close(fig1)
    plt.close("all")
Example #12
0
def OLR_sat(datetime,
            steps=0,
            model="MEPS",
            domain_name=None,
            domain_lonlat=None,
            legend=False,
            info=False,
            grid=True):

    for dt in datetime:  #modelrun at time..
        param = [
            "toa_outgoing_longwave_flux", "air_pressure_at_sea_level",
            "surface_geopotential"
        ]
        check_all = check_data(date=dt, model=model, param=param, step=steps)
        file_all = check_all.file
        data_domain = domain_input_handler(dt, model, domain_name,
                                           domain_lonlat, file_all)
        dmap_meps = get_data(model=model,
                             param=param,
                             file=file_all,
                             step=steps,
                             date=dt,
                             data_domain=data_domain)
        dmap_meps.retrieve()

        dmap_meps.air_pressure_at_sea_level /= 100

        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        parallels = dmap_meps.standard_parallel_projection_lambert

        #fig = plt.figure(figsize=(7, 9))
        # setting up projection
        globe = ccrs.Globe(ellipse='sphere',
                           semimajor_axis=6371000.,
                           semiminor_axis=6371000.)
        crs = ccrs.LambertConformal(central_longitude=lon0,
                                    central_latitude=lat0,
                                    standard_parallels=parallels,
                                    globe=globe)

        for tim in np.arange(np.min(steps), np.max(steps) + 1, 1):
            fig, ax = plt.subplots(1,
                                   1,
                                   figsize=(7, 9),
                                   subplot_kw={'projection': crs})

            ttt = tim
            tidx = tim - np.min(steps)
            ZS = dmap_meps.surface_geopotential[tidx, 0, :, :]
            MSLP = np.where(ZS < 3000,
                            dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :],
                            np.NaN).squeeze()

            #ax = plt.subplot(projection=crs)

            print('Plotting {0} + {1:02d} UTC'.format(dt, ttt))
            #ax.coastlines('10m')
            #ax.pcolormesh(dmap_meps.x, dmap_meps.y, dmap_meps.integral_of_toa_outgoing_longwave_flux_wrt_time[0, 0, :, :], vmin=-230,
            #              vmax=-110, cmap=plt.cm.Greys_r)
            #ax.pcolormesh(dmap_meps.x, dmap_meps.y, dmap_meps.toa_outgoing_longwave_flux[tidx, 0, :, :], vmin=-230,vmax=-110, cmap=plt.cm.Greys_r)

            # MSLP
            # MSLP with contour labels every 10 hPa
            C_P = ax.contour(dmap_meps.x,
                             dmap_meps.y,
                             MSLP,
                             zorder=10,
                             alpha=0.6,
                             levels=np.arange(
                                 round(np.nanmin(MSLP), -1) - 10,
                                 round(np.nanmax(MSLP), -1) + 10, 1),
                             colors='cyan',
                             linewidths=0.5)
            C_P = ax.contour(dmap_meps.x,
                             dmap_meps.y,
                             MSLP,
                             zorder=10,
                             alpha=0.6,
                             levels=np.arange(
                                 round(np.nanmin(MSLP), -1) - 10,
                                 round(np.nanmax(MSLP), -1) + 10, 5),
                             colors='cyan',
                             linewidths=1.0,
                             label="MSLP [hPa]")
            ax.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)

            #It is a bug in pcolormesh. supposedly newest is correct, but not older versions. Invalid corner values set to nan
            #https://github.com/matplotlib/basemap/issues/470
            x, y = np.meshgrid(dmap_meps.x, dmap_meps.y)
            #dlon,dlat=  np.meshgrid(dmap_meps.longitude, dmap_meps.latitude)

            nx, ny = x.shape
            mask = ((x[:-1, :-1] > 1e20) | (x[1:, :-1] > 1e20) |
                    (x[:-1, 1:] > 1e20) | (x[1:, 1:] > 1e20) |
                    (x[:-1, :-1] > 1e20) | (x[1:, :-1] > 1e20) |
                    (x[:-1, 1:] > 1e20) | (x[1:, 1:] > 1e20))
            data = dmap_meps.toa_outgoing_longwave_flux[tidx, 0, :nx - 1, :ny -
                                                        1].copy()
            data[mask] = np.nan
            #ax.pcolormesh(x, y, data[ :, :])#, cmap=plt.cm.Greys_r)

            ax.pcolormesh(x,
                          y,
                          data[:, :],
                          vmin=-230,
                          vmax=-110,
                          cmap=plt.cm.Greys_r)
            #ax.pcolormesh(dmap_meps.x, dmap_meps.y, dmap_meps.toa_outgoing_longwave_flux[tidx, 0, :, :], cmap=plt.cm.Greys_r)
            #lat_p = 78.9243
            #lon_p = 11.9312
            #mainpoint = ax.scatter(lon_p, lat_p, s=9.0 ** 2, transform=ccrs.PlateCarree(),
            #                        color='lime', zorder=6, linestyle='None', edgecolors="k", linewidths=3)

            ax.add_feature(
                cfeature.GSHHSFeature(scale='intermediate'),
                edgecolor="brown",
                linewidth=0.5
            )  # ‘auto’, ‘coarse’, ‘low’, ‘intermediate’, ‘high, or ‘full’ (default is ‘auto’).

            #distancerange="../../data/Table_circle_nm_Andenes.csv"
            #dist = pd.read_csv(distancerange)
            #lats = dist["lat_300nm"]
            #lons = dist["lon_300nm"]
            #lons[dmap_meps.longitude]=np.nan

            #lons_mask = ma.masked_outside(lons, np.nanmin(dmap_meps.longitude), np.nanmax(dmap_meps.longitude))
            #lats_mask = ma.masked_outside(lats, np.nanmin(dmap_meps.latitude), np.nanmax(dmap_meps.latitude))
            #C300 = ax.plot(lons_mask,lats_mask, transform = ccrs.PlateCarree())

            #lats = dist["lat_400nm"]
            #lons = dist["lon_400nm"]
            #lons_mask = ma.masked_outside(lons, np.nanmin(dmap_meps.longitude), np.nanmax(dmap_meps.longitude))
            #lats_mask = ma.masked_outside(lats, np.nanmin(dmap_meps.latitude), np.nanmax(dmap_meps.latitude))
            #C300 = ax.plot(lons_mask, lats_mask, transform=ccrs.PlateCarree())

            #lats = dist["lat_500nm"]
            #lons = dist["lon_500nm"]
            #lons_mask = ma.masked_outside(lons, np.nanmin(dmap_meps.longitude), np.nanmax(dmap_meps.longitude))
            #lats_mask = ma.masked_outside(lats, np.nanmin(dmap_meps.latitude), np.nanmax(dmap_meps.latitude))
            #C300 = ax.plot(lons, lats, transform=ccrs.PlateCarree())

            #lonlat = [dmap_meps.longitude[0, 0], dmap_meps.longitude[0, -1], dmap_meps.latitude[0, 0],
            #          dmap_meps.latitude[-1, -1]]
            #lonlat = [np.nanmin(dmap_meps.longitude), np.nanmax(dmap_meps.longitude), np.nanmin(dmap_meps.latitude),
            #         np.nanmax(dmap_meps.latitude)]
            #print(dmap_meps.longitude[-2, -2])
            #print(np.nanmax(dmap_meps.longitude))
            #ax.set_extent((lonlat[0], lonlat[1], lonlat[2], lonlat[3]))  # (x0, x1, y0, y1)
            #ax.set_extent([x[0,0], x[-1,-1], y[0,0], y[-1,-1]], projection=crs)  # (x0, x1, y0, y1)
            #ax.set_extent((lonlat[0], lonlat[1], lonlat[2], lonlat[3]))  # (x0, x1, y0, y1)
            make_modelrun_folder = setup_directory(OUTPUTPATH,
                                                   "{0}".format(dt))
            ax.text(0, 1, "{0}_{1}+{2:02d}".format(model, dt, ttt), ha='left', va='bottom', \
                     transform=ax.transAxes, color='dimgrey')
            #ax.set_extent((lonlat[0], lonlat[1], lonlat[2], lonlat[3]))  # (x0, x1, y0, y1)
            #ax.set_extent([lonlat[0]+10, lonlat[1], lonlat[2]-2, lonlat[3]])  # (x0, x1, y0, y1)

            #ax.set_extent((-18.0,80.0,62.0,88.0))  # (x0, x1, y0, y1)

            #ax.set_extent(data_domain.lonlat)
            if grid:
                nicegrid(ax=ax, color="orange")
            fig.savefig(make_modelrun_folder +
                        "/{0}_{1}_OLR_sat_{2}+{3:02d}.png".format(
                            model, domain_name, dt, ttt),
                        bbox_inches="tight",
                        dpi=200)

            ax.cla()
            fig.clf()
            plt.close(fig)

        ax.cla()
        plt.clf()
    plt.close("all")
Example #13
0
def Z500_VEL(datetime,
             steps=0,
             model="MEPS",
             domain_name=None,
             domain_lonlat=None,
             legend=False,
             info=False,
             grid=True):

    for dt in datetime:  #modelrun at time..
        date = dt[0:-2]
        hour = int(dt[-2:])
        param_sfc = [
            "air_pressure_at_sea_level", "precipitation_amount_acc",
            "surface_geopotential"
        ]
        param_pl = ["x_wind_pl", "y_wind_pl", "geopotential_pl"]
        param = param_sfc + param_pl
        plevel = [500]
        split = False
        print("\n######## Checking if your request is possible ############")
        try:
            check_all = check_data(date=dt,
                                   model=model,
                                   param=param,
                                   levtype="pl",
                                   p_level=plevel,
                                   step=steps)
            print(check_all.file)

        except ValueError:
            split = True
            try:
                print(
                    "--------> Splitting up your request to find match ############"
                )
                check_sfc = check_data(date=dt,
                                       model=model,
                                       param=param_sfc,
                                       step=steps)
                check_pl = check_data(date=dt,
                                      model=model,
                                      param=param_pl,
                                      levtype="pl",
                                      p_level=plevel,
                                      step=steps)
                print(check_pl.file)
            except ValueError:
                print(
                    "!!!!! Sorry this plot is not availbale for this date. Try with another datetime !!!!!"
                )
                break
        print("--------> Found match for your request ############")

        if not split:
            file_all = check_all.file.loc[0]

            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_all)

            #lonlat = np.array(data_domain.lonlat)
            print(file_all)
            dmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param,
                                 file=file_all,
                                 step=steps,
                                 date=dt,
                                 p_level=plevel)
            print("\n######## Retrieving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()
            tmap_meps = dmap_meps  # two names for same value, no copying done.
        else:
            # get sfc level data
            file_sfc = check_sfc.file.loc[0]
            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_sfc)
            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 param=param_sfc,
                                 file=file_sfc,
                                 step=steps,
                                 date=dt,
                                 data_domain=data_domain)
            print("\n######## Retrieving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()

            # get pressure level data
            file_pl = check_pl.file.loc[0]
            tmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param_pl,
                                 file=file_pl,
                                 step=steps,
                                 date=dt,
                                 p_level=plevel)
            print("\n######## Retrieving data ############")
            print(f"--------> from: {tmap_meps.url} ")
            tmap_meps.retrieve()

        # convert fields
        dmap_meps.air_pressure_at_sea_level /= 100
        #dmap_meps.precipitation_amount_acc*=1000.0
        print(dmap_meps.units.precipitation_amount_acc)
        tmap_meps.geopotential_pl /= 10.0
        tmap_meps.units.geopotential_pl = "m"
        u, v = xwind2uwind(tmap_meps.x_wind_pl, tmap_meps.y_wind_pl,
                           tmap_meps.alpha)
        vel = wind_speed(tmap_meps.x_wind_pl, tmap_meps.y_wind_pl)

        # plot map
        lonlat = [
            dmap_meps.longitude[0, 0], dmap_meps.longitude[-1, -1],
            dmap_meps.latitude[0, 0], dmap_meps.latitude[-1, -1]
        ]

        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        parallels = dmap_meps.standard_parallel_projection_lambert

        # setting up projection
        globe = ccrs.Globe(ellipse='sphere',
                           semimajor_axis=6371000.,
                           semiminor_axis=6371000.)
        crs = ccrs.LambertConformal(central_longitude=lon0,
                                    central_latitude=lat0,
                                    standard_parallels=parallels,
                                    globe=globe)

        for tim in np.arange(np.min(steps), np.max(steps) + 1, 1):
            fig1, ax1 = plt.subplots(1,
                                     1,
                                     figsize=(7, 9),
                                     subplot_kw={'projection': crs})

            ttt = tim  #+ np.min(steps)
            tidx = tim - np.min(steps)
            print('Plotting {0} + {1:02d} UTC'.format(dt, ttt))
            plev2 = 0
            embr = 0
            ZS = dmap_meps.surface_geopotential[tidx, 0, :, :]
            MSLP = np.where(ZS < 3000,
                            dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :],
                            np.NaN).squeeze()
            acc = 1
            TP = precip_acc(dmap_meps.precipitation_amount_acc,
                            acc=acc)[tidx, 0, :, :].squeeze()
            VEL = (vel[tidx, plev2, :, :]).squeeze()
            Z = (tmap_meps.geopotential_pl[tidx, plev2, :, :]).squeeze()
            Ux = u[tidx, 0, :, :].squeeze()
            Vx = v[tidx, 0, :, :].squeeze()
            uxx = tmap_meps.x_wind_pl[tidx, 0, :, :].squeeze()
            vxx = tmap_meps.y_wind_pl[tidx, 0, :, :].squeeze()
            cmap = plt.get_cmap("tab20c")
            lvl = [0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10, 15, 20, 25, 30]
            norm = mcolors.BoundaryNorm(lvl, cmap.N)

            try:  #workaround for a stupid matplotlib error not handling when all values are outside of range in lvl or all just nans..
                #https://github.com/SciTools/cartopy/issues/1290
                #cmap =  mcolors.ListedColormap('hsv', 'hsv') #plt.get_cmap("hsv")PuBu
                #TP.filled(np.nan) #fill mask with nan to avoid:  UserWarning: Warning: converting a masked element to nan.
                CF_prec = plt.contourf(dmap_meps.x,
                                       dmap_meps.y,
                                       TP,
                                       zorder=1,
                                       cmap=cmap,
                                       norm=norm,
                                       alpha=0.4,
                                       antialiased=True,
                                       levels=lvl,
                                       extend="max")  #
            except:
                pass
            # MSLP with contour labels every 10 hPa
            C_P = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              MSLP,
                              zorder=1,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 1),
                              colors='grey',
                              linewidths=0.5)
            C_P = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              MSLP,
                              zorder=2,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 10),
                              colors='grey',
                              linewidths=1.0)
            ax1.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)
            ####REMOVE LATER 60.2;5.4166666666667;60;None;N

            skip = 20
            skip = (slice(40, -40, 50), slice(40, -40, 50))  #70
            xm, ym = np.meshgrid(dmap_meps.x, dmap_meps.y)
            CVV = ax1.barbs(xm[skip],
                            ym[skip],
                            uxx[skip] * 1.94384,
                            vxx[skip] * 1.94384,
                            zorder=5)
            #CS = ax1.contour(dmap_meps.x, dmap_meps.y, VEL, zorder=3, alpha=1.0,
            #                   levels=np.arange(-80, 80, 5), colors="green", linewidths=0.7)
            # geopotential
            CS = ax1.contour(dmap_meps.x,
                             dmap_meps.y,
                             Z,
                             zorder=3,
                             alpha=1.0,
                             levels=np.arange(4600, 5800, 20),
                             colors="blue",
                             linewidths=0.7)
            ax1.clabel(CS, CS.levels, inline=True, fmt="%4.0f", fontsize=10)

            ax1.add_feature(
                cfeature.GSHHSFeature(scale='intermediate')
            )  # ‘auto’, ‘coarse’, ‘low’, ‘intermediate’, ‘high, or ‘full’ (default is ‘auto’).
            ax1.text(0,
                     1,
                     "{0}_Z500_{1}+{2:02d}".format(model, dt, ttt),
                     ha='left',
                     va='bottom',
                     transform=ax1.transAxes,
                     color='black')
            if grid:
                nicegrid(ax=ax1)
            ##########################################################
            legend = True
            if legend:
                proxy = [
                    plt.axhline(y=0, xmin=1, xmax=1, color="gray"),
                    plt.axhline(y=0, xmin=1, xmax=1, color="blue")
                ]
                try:
                    ax_cb = adjustable_colorbar_cax(fig1, ax1)

                    cb = plt.colorbar(CF_prec,
                                      cax=ax_cb,
                                      fraction=0.046,
                                      pad=0.01,
                                      aspect=25,
                                      label=f"{acc}h acc. prec. [mm/{acc}h]",
                                      extend="both")

                except:
                    pass
                lg = ax1.legend(proxy, [
                    f"MSLP [hPa]",
                    f"Geopotential height[{tmap_meps.units.geopotential_pl}] at {dmap_meps.pressure[plev2]:.0f} hPa"
                ],
                                loc="upper right")
                frame = lg.get_frame()
                frame.set_facecolor('white')
                frame.set_alpha(1)

            #if info:
            #  plt.text(x=0, y=-1, s="INFO: Reduced topographic noise by filtering with surface_geopotential bellow 3000",
            #           fontsize=7)  # , bbox=dict(facecolor='white', alpha=0.5))

            #plt.show()
            if domain_name != model and data_domain != None:  # weird bug.. cuts off when sees no data value
                ax1.set_extent(data_domain.lonlat)
            make_modelrun_folder = setup_directory(OUTPUTPATH,
                                                   "{0}".format(dt))
            fig1.savefig(make_modelrun_folder +
                         "/{0}_{1}_Z500_VEL_P_{2}+{3:02d}.png".format(
                             model, domain_name, dt, ttt),
                         bbox_inches="tight",
                         dpi=200)
            ax1.cla()
            plt.clf()
            plt.close(fig1)

        #proxy = [plt.axhline(y=0, xmin=1, xmax=1, color="green"),
        #         plt.axhline(y=0, xmin=1, xmax=1, color="blue")]
        #fig2 = plt.figure(figsize=(2, 1.25))
        #fig2.legend(proxy, [f"Wind strength [m/s] at {tmap_meps.pressure[plev2]:.0f} hPa",
        #                          f"Geopotential [{tmap_meps.units.geopotential_pl}]{tmap_meps.pressure[plev2]:.0f} hPa"])
        #fig2.savefig(make_modelrun_folder+"/{0}_Z500_VEL_P_LEGEND.png".format(model), bbox_inches="tight", dpi=200)
        #plt.close(fig2)
        #try:
        #  fig3, ax3 = plt.subplots()
        #  fig3.colorbar(CF_prec, fraction=0.046, pad=0.04)
        #  ax3.remove()
        #  fig3.savefig(make_modelrun_folder+"/{0}_{1}_Z500_VEL_P_COLORBAR.png".format(model, domain_name), bbox_inches="tight", dpi=200)
        #  plt.close(fig3)
        #except:
        #  pass
        ax1.cla()
        plt.clf()
    plt.close("all")
Example #14
0
def surf(datetime,
         steps=0,
         model="AromeArctic",
         domain_name=None,
         domain_lonlat=None,
         legend=False,
         info=False,
         grid=True):

    for dt in datetime:  #modelrun at time..
        date = dt[0:-2]
        hour = int(dt[-2:])
        param_sfc = [
            "surface_geopotential", "air_pressure_at_sea_level", "x_wind_10m",
            "y_wind_10m", "precipitation_amount_acc", "wind_speed"
        ]
        param_sfx = ["LE", "H", "SST"]
        param_pl = []
        param = param_sfc + param_pl
        split = False
        sfx = False
        print("\n######## Checking if your request is possible ############")
        try:
            check_all = check_data(date=dt,
                                   model=model,
                                   param=param,
                                   step=steps)
            check_sfx = check_data(date=dt,
                                   model=model,
                                   param=param_sfx,
                                   step=steps)

            print(check_all.file)

        except ValueError:
            try:
                sfx = True
                param_sfx = ["SFX_LE", "SFX_H", "SFX_SST"]
                check_all = check_data(date=dt,
                                       model=model,
                                       param=param,
                                       step=steps)
                check_sfx = check_data(date=dt,
                                       model=model,
                                       param=param_sfx,
                                       step=steps)
            except ValueError:
                print(
                    "!!!!! Sorry this plot is not availbale for this date. Try with another datetime !!!!!"
                )
                break
        print("--------> Found match for your request ############")

        if not split:
            file_all = check_all.file.loc[0]
            file_sfx = check_sfx.file.loc[0]

            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_all)

            #lonlat = np.array(data_domain.lonlat)
            print(file_all)
            dmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param,
                                 file=file_all,
                                 step=steps,
                                 date=dt)
            dmap_meps_sfx = get_data(model=model,
                                     data_domain=data_domain,
                                     param=param_sfx,
                                     file=file_sfx,
                                     step=steps,
                                     date=dt)
            print("\n######## Retrieving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()
            tmap_meps = dmap_meps  # two names for same value, no copying done.
            dmap_meps_sfx.retrieve()
            if sfx:  #["SFX_LE", "SFX_H", "SFX_SST"]
                dmap_meps_sfx.LE = dmap_meps_sfx.SFX_LE
                dmap_meps_sfx.H = dmap_meps_sfx.SFX_H
                dmap_meps_sfx.SST = dmap_meps_sfx.SFX_SST
                dmap_meps_sfx.units.LE = dmap_meps_sfx.units.SFX_LE
                dmap_meps_sfx.units.H = dmap_meps_sfx.units.SFX_H
                dmap_meps_sfx.units.SST = dmap_meps_sfx.units.SFX_SST

        # convert fields
        dmap_meps.air_pressure_at_sea_level /= 100
        u, v = xwind2uwind(tmap_meps.x_wind_10m, tmap_meps.y_wind_10m,
                           tmap_meps.alpha)
        vel = wind_speed(tmap_meps.x_wind_10m, tmap_meps.y_wind_10m)

        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        parallels = dmap_meps.standard_parallel_projection_lambert

        # setting up projection
        globe = ccrs.Globe(ellipse='sphere',
                           semimajor_axis=6371000.,
                           semiminor_axis=6371000.)
        data = ccrs.LambertConformal(central_longitude=lon0,
                                     central_latitude=lat0,
                                     standard_parallels=parallels,
                                     globe=globe)
        crs = data
        crs_lon = ccrs.PlateCarree()
        #crs = ccrs.PlateCarree()
        for tim in np.arange(np.min(steps), np.max(steps) + 1, 1):
            fig1, ax1 = plt.subplots(1,
                                     1,
                                     figsize=(7, 9),
                                     subplot_kw={'projection': crs})
            ttt = tim  #+ np.min(steps)
            tidx = tim - np.min(steps)
            print('Plotting {0} + {1:02d} UTC'.format(dt, ttt))
            ZS = dmap_meps.surface_geopotential[tidx, 0, :, :]
            MSLP = np.where(ZS < 3000,
                            dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :],
                            np.NaN).squeeze()
            #TP = precip_acc(dmap_meps.precipitation_amount_acc, acc=1)[tidx, 0, :,:].squeeze()
            L = dmap_meps_sfx.LE[tidx, :, :].squeeze()
            L = np.where(ZS < 3000, L, np.NaN).squeeze()
            SH = dmap_meps_sfx.H[tidx, :, :].squeeze()
            SH = np.where(ZS < 3000, SH, np.NaN).squeeze()
            SST = dmap_meps_sfx.SST[tidx, :, :].squeeze()
            Ux = dmap_meps.x_wind_10m[tidx, 0, :, :].squeeze()
            Vx = dmap_meps.y_wind_10m[tidx, 0, :, :].squeeze()
            xm, ym = np.meshgrid(dmap_meps.x, dmap_meps.y)
            uxx = dmap_meps.x_wind_10m[tidx, 0, :, :].squeeze()
            vxx = dmap_meps.y_wind_10m[tidx, 0, :, :].squeeze()

            #VELOCITY
            #new_x, new_y, new_u, new_v, = vector_scalar_to_grid(src_crs= data, target_proj= crs_lon,regrid_shape = np.shape(Ux), x= dmap_meps.x, y= dmap_meps.y, u= Ux, v= Vx)
            #magnitude = (new_u ** 2 + new_v ** 2) ** 0.5
            #cmap = plt.get_cmap("viridis") #cividis copper
            #wii = plt.quiver(new_x, new_y, new_u, new_v)
            #wii = ax1.quiver(xm, ym, Ux, Vx)

            #wii = Axes.streamplot(ax1, new_x, new_y, new_u, new_v, density=4,zorder=4,transform=crs_lon, linewidth=0.7, color=magnitude, cmap=cmap)

            #LATENT
            #levelspos=np.arange(80, round(np.nanmax(L), -1) + 10, 40)
            #levelsneg = np.arange(-300, -9, 10)
            #levels = np.append(levelsneg, levelspos)
            #CL = ax1.contour(dmap_meps.x, dmap_meps.y, L, zorder=3, alpha=1.0, colors="red", linewidths=0.7, levels=levels, transform=data)
            #ax1.clabel(CL, CL.levels[::2], inline=True, fmt="%3.0f", fontsize=10)
            #xx = np.where(L < -10, xm, np.NaN).squeeze()
            #yy = np.where(L < -10, ym, np.NaN).squeeze()
            #skip = (slice(None, None, 4), slice(None, None, 4))
            #ax1.scatter(xx[skip][skip], yy[skip][skip], s=20, zorder=2, marker='x', linewidths=0.9,
            #                        c="white", alpha=0.7, transform=data)
            #xx = np.where(L > 80, xm, np.NaN).squeeze()
            #yy = np.where(L > 80, ym, np.NaN).squeeze()
            #skip = (slice(None, None, 4), slice(None, None, 4))
            #ax1.scatter(xx[skip][skip], yy[skip][skip], s=20, zorder=2, marker='.', linewidths=0.9,
            #            c="black", alpha=0.7, transform=data)

            #SENSIBLE
            levelspos = np.arange(20, round(np.nanmax(SH), -10) + 10, 40)
            levelsneg = np.arange(-300, -19, 40)
            levels = np.append(levelsneg, levelspos)
            levels = np.linspace(-300, 300, 15)
            CSH = plt.contour(dmap_meps.x,
                              dmap_meps.y,
                              SH,
                              alpha=1.0,
                              colors="blue",
                              linewidths=0.7,
                              levels=levels)
            #ax1.clabel(CSH, CSH.levels[1::2], inline=True, fmt="%3.0f", fontsize=10)
            #xx = np.where(SH < -10, xm, np.NaN).squeeze()
            #yy = np.where(SH < -10, ym, np.NaN).squeeze()
            #skip = (slice(None, None, 4), slice(None, None, 4))
            #ax1.scatter(xx[skip][skip],yy[skip][skip],s=20, zorder=2, marker='x',linewidths=0.9, c= "white", alpha=0.7, transform=data)

            #xx = np.where(SH >80, xm, np.NaN).squeeze()
            #yy = np.where(SH >80, ym, np.NaN).squeeze()
            #skip = (slice(None, None, 4), slice(None, None, 4))
            #ax1.scatter(xx[skip][skip], yy[skip][skip], s=20, zorder=2, marker='.', linewidths=0.9, c="black", alpha=0.7,
            #            transform=data)

            #SST_new
            #levels=np.arange(270,294,2)
            SST = SST - 273.15
            levels = [np.min(SST), np.max(SST), 3]
            levels = [-2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
            C_SS = ax1.contour(dmap_meps.x,
                               dmap_meps.y,
                               SST,
                               colors="k",
                               linewidths=2,
                               levels=levels,
                               zorder=9)
            ax1.clabel(C_SS,
                       C_SS.levels,
                       inline=True,
                       fmt="%3.0f",
                       fontsize=10)

            #MSLP
            #MSLP with contour labels every 10 hPa
            C_P = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              MSLP,
                              zorder=10,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 1),
                              colors='grey',
                              linewidths=0.5)
            C_P = ax1.contour(dmap_meps.x,
                              dmap_meps.y,
                              MSLP,
                              zorder=10,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 10),
                              colors='grey',
                              linewidths=1.0,
                              label="MSLP [hPa]")
            ax1.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)

            #wind#
            #skip = (slice(50, -50, 50), slice(50, -50, 50))
            skip = (slice(40, -40, 50), slice(40, -40, 50))  #70
            CVV = ax1.barbs(xm[skip],
                            ym[skip],
                            uxx[skip] * 1.94384,
                            vxx[skip] * 1.94384,
                            zorder=10)

            #lat_p = 60.2
            #lon_p = 5.4167
            #mainpoint = ax1.scatter(lon_p, lat_p, s=9.0 ** 2, transform=ccrs.PlateCarree(),
            #                        color='lime', zorder=6, linestyle='None', edgecolors="k", linewidths=3)

            #LATENT_new
            #levels=np.arange(270,294,2)
            cmap = plt.get_cmap("coolwarm")
            levels = np.linspace(-100, 300, 7)

            CSST = ax1.contourf(dmap_meps.x,
                                dmap_meps.y,
                                L,
                                zorder=1,
                                levels=levels,
                                alpha=0.7,
                                cmap=cmap,
                                extend="both",
                                transform=data)
            ax1.add_feature(
                cfeature.GSHHSFeature(scale='intermediate'),
                zorder=3,
                facecolor="whitesmoke",
                edgecolor="gray"
            )  # ‘auto’, ‘coarse’, ‘low’, ‘intermediate’, ‘high, or ‘full’ (default is ‘auto’).
            ax1.text(0,
                     1,
                     "{0}_surf_{1}+{2:02d}".format(model, dt, ttt),
                     ha='left',
                     va='bottom',
                     transform=ax1.transAxes,
                     color='black')
            ##########################################################
            #handles, labels = ax1.get_legend_handles_labels()
            legend = True
            if legend:
                proxy = [
                    plt.axhline(y=0, xmin=1, xmax=1, color="blue"),
                    plt.axhline(y=0, xmin=1, xmax=1, color="black")
                ]
                lg = plt.legend(
                    proxy,
                    [f"Sensible heat [{dmap_meps_sfx.units.H}] ", f"SST [C]"],
                    loc=1)
                ax_cb = adjustable_colorbar_cax(fig1, ax1)
                cb = plt.colorbar(
                    CSST,
                    cax=ax_cb,
                    fraction=0.046,
                    pad=0.01,
                    ax=ax1,
                    aspect=25,
                    label=f"Latent heat [{dmap_meps_sfx.units.LE}]",
                    extend="both")
                frame = lg.get_frame()
                lg.set_zorder(102)
                frame.set_facecolor('lightgray')
                frame.set_alpha(1)
                #plt.title("{0}_surf_{1}+{2:02d}.png".format(model, dt, ttt))
            #print("test")
            #print(OUTPUTPATH)
            #print("{0}".format(dt))
            make_modelrun_folder = setup_directory(OUTPUTPATH,
                                                   "{0}".format(dt))
            if grid:
                nicegrid(ax=ax1)
            if domain_name != model and data_domain != None:  # weird bug.. cuts off when sees no data value
                ax1.set_extent(data_domain.lonlat)
            fig1.savefig(make_modelrun_folder +
                         "/{0}_{1}_surf_{2}+{3:02d}.png".format(
                             model, domain_name, dt, ttt),
                         bbox_inches="tight",
                         dpi=200)

            ax1.cla()
            #cb.remove()
            #lg.remove()
            #plt.draw()

            plt.clf()
            plt.close(fig1)
    plt.close("all")
Example #15
0
def set_variable2d(modelruntime, steps, lvl, xres, yres, model):
    variable2d_arome = {}
    variable3d_arome = {}
    variable2d_sfx = {}
    resol = 7  #?

    variable2d_arome['surface_air_pressure'] = {}
    variable2d_arome['surface_air_pressure']['name'] = 'SP'
    variable2d_arome['surface_air_pressure']['units'] = 'Pa'
    variable2d_arome['surface_air_pressure'][
        'description'] = 'log of surface pressure'
    variable2d_arome['surface_air_pressure']['precision'] = resol
    variable2d_arome['air_temperature_2m'] = {}
    variable2d_arome['air_temperature_2m']['name'] = 'T2m'
    variable2d_arome['air_temperature_2m']['units'] = 'K'
    variable2d_arome['air_temperature_2m']['description'] = 'Temperature at 2m'
    variable2d_arome['air_temperature_2m']['precision'] = resol
    variable2d_arome['surface_geopotential'] = {}
    variable2d_arome['surface_geopotential']['name'] = 'Zg'
    variable2d_arome['surface_geopotential']['units'] = 'm^2/s^2'
    variable2d_arome['surface_geopotential'][
        'description'] = 'surface geopotential'
    variable2d_arome['surface_geopotential']['precision'] = resol
    variable2d_arome['land_area_fraction'] = {}
    variable2d_arome['land_area_fraction']['name'] = 'LS'
    variable2d_arome['land_area_fraction']['units'] = 'none'
    variable2d_arome['land_area_fraction']['description'] = 'land sea mask'
    variable2d_arome['land_area_fraction']['precision'] = resol
    variable2d_arome['x_wind_10m'] = {}
    variable2d_arome['x_wind_10m']['name'] = 'U_lon_10m'
    variable2d_arome['x_wind_10m']['units'] = 'm/s'
    variable2d_arome['x_wind_10m']['description'] = 'zonal wind at 10m'
    variable2d_arome['x_wind_10m']['precision'] = resol
    variable2d_arome['y_wind_10m'] = {}
    variable2d_arome['y_wind_10m']['name'] = 'V_lat_10m'
    variable2d_arome['y_wind_10m']['units'] = 'm/s'
    variable2d_arome['y_wind_10m']['description'] = 'meriodional wind at 10m'
    variable2d_arome['y_wind_10m']['precision'] = resol
    variable2d_arome['specific_humidity_2m'] = {}
    variable2d_arome['specific_humidity_2m']['name'] = 'Q2m'
    variable2d_arome['specific_humidity_2m']['units'] = 'kg/kg'
    variable2d_arome['specific_humidity_2m'][
        'description'] = 'specific humidity at 2m'
    variable2d_arome['specific_humidity_2m']['precision'] = resol
    variable2d_arome[
        'integral_of_surface_downward_sensible_heat_flux_wrt_time'] = {}
    variable2d_arome[
        'integral_of_surface_downward_sensible_heat_flux_wrt_time'][
            'name'] = 'SSHF_CUM'
    variable2d_arome[
        'integral_of_surface_downward_sensible_heat_flux_wrt_time'][
            'units'] = 'J.m-2'
    variable2d_arome[
        'integral_of_surface_downward_sensible_heat_flux_wrt_time'][
            'description'] = 'Cum.Sensible heat flux'
    variable2d_arome[
        'integral_of_surface_downward_sensible_heat_flux_wrt_time'][
            'precision'] = resol

    variable2d_sfx['FMU'] = {}
    variable2d_sfx['FMU']['name'] = 'USTRESS'
    variable2d_sfx['FMU']['units'] = 'Kg.m-1.s-1'
    variable2d_sfx['FMU']['description'] = 'Surface wind stress (u)'
    variable2d_sfx['FMU']['precision'] = resol
    variable2d_sfx['FMV'] = {}
    variable2d_sfx['FMV']['name'] = 'VSTRESS'
    variable2d_sfx['FMV']['units'] = 'Kg.m-1.s-1'
    variable2d_sfx['FMV']['description'] = 'Surface wind stress (v)'
    variable2d_sfx['FMV']['precision'] = resol

    variable3d_arome['air_temperature_ml'] = {}
    variable3d_arome['air_temperature_ml']['name'] = 'T'
    variable3d_arome['air_temperature_ml']['units'] = 'K'
    variable3d_arome['air_temperature_ml'][
        'description'] = 'temperature on pressure sigmal levels'
    variable3d_arome['air_temperature_ml'][
        'precision'] = resol  # digit precision
    variable3d_arome['divergence_vertical'] = {}
    variable3d_arome['divergence_vertical']['name'] = 'NH_dW'
    variable3d_arome['divergence_vertical']['units'] = 'm/s * g'
    variable3d_arome['divergence_vertical'][
        'description'] = 'Non Hydrostatic divergence of vertical velocity: D = -g(w(i) -w(i-1))'
    variable3d_arome['divergence_vertical']['precision'] = resol
    variable3d_arome['x_wind_ml'] = {}
    variable3d_arome['x_wind_ml']['name'] = 'U_X'
    variable3d_arome['x_wind_ml']['units'] = 'm/s'
    variable3d_arome['x_wind_ml'][
        'description'] = 'U wind along x axis on pressure sigmal levels'
    variable3d_arome['x_wind_ml']['precision'] = resol
    variable3d_arome['y_wind_ml'] = {}
    variable3d_arome['y_wind_ml']['name'] = 'V_Y'
    variable3d_arome['y_wind_ml']['units'] = 'm/s'
    variable3d_arome['y_wind_ml'][
        'description'] = 'V wind along y axis on pressure sigmal levels'
    variable3d_arome['y_wind_ml']['precision'] = resol
    # variable3d_arome['PRESS.DEPART']={}
    # variable3d_arome['PRESS.DEPART']['name'] = 'NH_dP'
    # variable3d_arome['PRESS.DEPART']['units'] = 'Pa'
    # variable3d_arome['PRESS.DEPART']['description'] = 'NH departure from pressure'
    # variable3d_arome['PRESS.DEPART']['precision'] = 1
    variable3d_arome['specific_humidity_ml'] = {}
    variable3d_arome['specific_humidity_ml']['name'] = 'Q'
    variable3d_arome['specific_humidity_ml']['units'] = 'kg/kg'
    variable3d_arome['specific_humidity_ml'][
        'description'] = 'specific humidity on pressure sigmal levels'
    variable3d_arome['specific_humidity_ml']['precision'] = resol
    variable3d_arome['turbulent_kinetic_energy_ml'] = {}
    variable3d_arome['turbulent_kinetic_energy_ml']['name'] = 'TKE'
    variable3d_arome['turbulent_kinetic_energy_ml']['units'] = 'm^2/s^2'
    variable3d_arome['turbulent_kinetic_energy_ml'][
        'description'] = 'Turbulent kinetic energy on pressure sigmal levels'
    variable3d_arome['turbulent_kinetic_energy_ml']['precision'] = resol
    variable3d_arome['cloud_area_fraction_ml'] = {}
    variable3d_arome['cloud_area_fraction_ml']['name'] = 'CLDFRA'
    variable3d_arome['cloud_area_fraction_ml']['units'] = 'none'
    variable3d_arome['cloud_area_fraction_ml'][
        'description'] = 'cloud fraction'
    variable3d_arome['cloud_area_fraction_ml']['precision'] = 1

    #2dArome
    print("retrive 2darome")
    param2d_arome = [*variable2d_arome.keys()]
    print(param2d_arome)
    print("check 2d arome")
    arome2d = check_data(date=modelruntime, model=model, param=param2d_arome)
    #
    print(arome2d)
    file_arome2d = arome2d.file
    print(arome2d.file)
    dmap_arome2d = get_data(model=model,
                            file=file_arome2d,
                            param=param2d_arome,
                            step=steps,
                            date=modelruntime)
    print(dmap_arome2d.url)
    dmap_arome2d.retrieve()
    print(dmap_arome2d.__dir__)
    print("retrive 3darome")
    # 3dArome This can be included in 2darome for timeefficency
    param3d_arome = [*variable3d_arome.keys()]
    arome3d = check_data(date=modelruntime,
                         model=model,
                         param=param3d_arome,
                         m_level=lvl)
    file_arome3d = arome3d.file
    print(file_arome3d)
    dmap_arome3d = get_data(model=model,
                            file=file_arome3d,
                            param=param3d_arome,
                            step=steps,
                            date=modelruntime,
                            m_level=lvl)
    print(dmap_arome3d.url)
    dmap_arome3d.retrieve()
    print(dmap_arome3d.__dir__)
    print("retrive sfxarome")
    #2dsfx
    param2d_sfx = [*variable2d_sfx.keys()]
    sfx2d = check_data(date=modelruntime, model=model, param=param2d_sfx)
    file_sfx2d = sfx2d.file
    dmap_sfx2d = get_data(model=model,
                          file=file_sfx2d,
                          param=param2d_sfx,
                          step=steps,
                          date=modelruntime)
    dmap_sfx2d.retrieve()

    #attr
    url = f"https://thredds.met.no/thredds/dodsC/aromearcticarchive/2020/07/01/arome_arctic_full_2_5km_20200701T18Z.nc?projection_lambert,x,y"
    dataset = Dataset(url)
    attr = {}
    proj = dataset.variables["projection_lambert"]
    #proj=dmap_arome2d.projection_lambert

    #for t in range( 0, len(dmap_arome2d.time )):
    for t in np.arange(np.min(steps), np.max(steps) + 1, 1):
        tidx = t - np.min(steps)
        print("Inside for loop")
        output = outputpath + modelruntime + "/"
        print(output)
        if not os.path.exists(output):
            os.makedirs(output)
            print("Directory ", output, " Created ")
        else:
            print("Directory ", output, " exist ")
        print(
            "####################################################################################"
        )
        print(t)
        print(
            "####################################################################################"
        )
        validdate = datetime.datetime(
            int(modelruntime[0:4]), int(modelruntime[4:6]),
            int(modelruntime[6:8]), int(
                modelruntime[8:10])) + datetime.timedelta(hours=int(t))
        date_time = validdate.strftime("%Y%m%d_%H")
        print(
            "####################################################################################"
        )
        print(date_time)
        print(
            "####################################################################################"
        )
        #flexpart dont like 00, want 24
        if validdate.hour == 0:
            dateminus1d = validdate - datetime.timedelta(days=1)
            date_time = dateminus1d.strftime("%Y%m%d") + "_24"
            #d = datetime.today() - timedelta(days=days_to_subtract)

        print(date_time)
        ncid = Dataset(output + 'AR' + date_time + '.nc', 'w')
        attr['reference_lon'] = proj.getncattr("longitude_of_central_meridian")
        attr['ydim'] = np.long(
            len(dmap_arome2d.y[::yres])
        )  #np.long(dataset.variables["y"].getncattr("_ChunkSizes"))  # Use: None
        attr['forecast'] = validdate.strftime("%H")  # 23
        attr['x_resolution'] = np.double(
            "2500.0")  #np.double("2500.0")*xres  # Use: None
        attr['center_lon'] = proj.getncattr("longitude_of_central_meridian")
        attr['rotation_radian'] = 0.0
        attr['xdim'] = np.long(
            len(dmap_arome2d.x[::xres])
        )  #np.long(dataset.variables["x"].getncattr("_ChunkSizes"))  # Use: None
        attr['input_lat'] = proj.getncattr(
            "latitude_of_projection_origin")  # Use: None
        attr['reference_lat'] = proj.getncattr("latitude_of_projection_origin")
        attr['y_resolution'] = np.double(
            "2500.0")  #np.double("2500.0")*yres   # Use: None
        attr['date'] = validdate.strftime("%Y%m%d")  # "20180331"
        attr['input_lon'] = proj.getncattr(
            "longitude_of_central_meridian")  # Use: None
        attr['input_position'] = (794.0, 444.0)  # ??  # Use: None
        attr['geoid'] = proj.getncattr("earth_radius")  #6370000#6371229.0 #
        attr['center_lat'] = proj.getncattr("latitude_of_projection_origin")

        print("Create netcdf")
        ncid.setncatts(attr)
        x = ncid.createDimension('X', len(dmap_arome2d.x[::xres]))
        y = ncid.createDimension('Y', len(dmap_arome2d.y[::yres]))
        level = ncid.createDimension('level', len(dmap_arome3d.hybrid))
        xs = ncid.createVariable('X', 'i4', ('X', ))
        xs.units = 'none'
        xs[:] = range(1, len(dmap_arome2d.x[::xres]) + 1)
        ys = ncid.createVariable('Y', 'i4', ('Y', ))
        ys.units = 'none'
        ys[:] = range(1, len(dmap_arome2d.y[::yres]) + 1)

        levels = ncid.createVariable('level', 'i4', ('level', ))
        levels.units = 'none'
        levels[:] = range(1, len(dmap_arome3d.hybrid) + 1)

        nc_ak = ncid.createVariable('Ak', 'f4', ('level', ))
        nc_ak.units = 'none'
        nc_ak[:] = dmap_arome3d.ap
        nc_bk = ncid.createVariable('Bk', 'f4', ('level', ))
        nc_bk.units = 'none'
        nc_bk[:] = dmap_arome3d.b

        vid = ncid.createVariable('LON', 'f4', ('Y', 'X'), zlib=True)
        vid.description = 'longitude of the center grid'
        vid[:] = dmap_arome2d.longitude[::xres, ::yres]
        vid = ncid.createVariable('LAT', 'f4', ('Y', 'X'), zlib=True)
        vid.description = 'latitude of the center grid'
        vid[:] = dmap_arome2d.latitude[::xres, ::yres]
        print(param3d_arome)
        for param in param3d_arome:
            vid = ncid.createVariable(variable3d_arome[param]['name'],
                                      'f4', ('level', 'Y', 'X'),
                                      zlib=True)
            vid.units = variable3d_arome[param]['units']
            vid.description = variable3d_arome[param]['description']
            expressiondata = f"dmap_arome3d.{param}[{tidx},:,::{xres},::{yres}]"
            data = eval(expressiondata)
            vid[:] = data

        print(param2d_arome)
        for param in param2d_arome:
            vid = ncid.createVariable(variable2d_arome[param]['name'],
                                      'f4', ('Y', 'X'),
                                      zlib=True)
            vid.units = variable2d_arome[param]['units']
            vid.description = variable2d_arome[param]['description']
            expressiondata = f"dmap_arome2d.{param}[{tidx},0,::{xres},::{yres}]"
            data = eval(expressiondata)
            if param == "surface_air_pressure":
                print(param)
                data = np.log(data)
            vid[:] = data
        for param in param2d_sfx:
            vid = ncid.createVariable(variable2d_sfx[param]['name'],
                                      'f4', ('Y', 'X'),
                                      zlib=True)
            vid.units = variable2d_sfx[param]['units']
            vid.description = variable2d_sfx[param]['description']
            expressiondata = f"dmap_sfx2d.{param}[{tidx},::{xres},::{yres}]"
            data = eval(expressiondata)
            vid[:] = data

        ncid.close()
Example #16
0
def Z500_VEL(datetime,
             steps=0,
             model="MEPS",
             domain_name=None,
             domain_lonlat=None,
             legend=False,
             info=False):

    for dt in datetime:  #modelrun at time..
        date = dt[0:-2]
        hour = int(dt[-2:])
        param_sfc = [
            "air_pressure_at_sea_level", "precipitation_amount_acc",
            "surface_geopotential"
        ]
        param_pl = ["x_wind_pl", "y_wind_pl", "geopotential_pl"]
        param = param_sfc + param_pl
        split = False
        print("\n######## Checking if your request is possibel ############")
        try:
            check_all = check_data(date=dt,
                                   model=model,
                                   param=param,
                                   p_level=500)
        except ValueError:
            split = True
            try:
                print(
                    "--------> Splitting up your request to find match ############"
                )
                check_sfc = check_data(date=dt, model=model, param=param_sfc)
                check_pl = check_data(date=dt,
                                      model=model,
                                      param=param_pl,
                                      p_level=500)
            except ValueError:
                print(
                    "!!!!! Sorry this plot is not availbale for this date. Try with another datetime !!!!!"
                )
                break
        print("--------> Found match for your request ############")

        if not split:
            file_all = check_all.file.loc[0]

            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_all)

            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param,
                                 file=file_all,
                                 step=steps,
                                 date=dt,
                                 p_level=500)
            print("\n######## Retriving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()
            tmap_meps = dmap_meps  # two names for same value, no copying done.
        else:
            # get sfc level data
            file_sfc = check_sfc.file.loc[0]
            data_domain = domain_input_handler(dt, model, domain_name,
                                               domain_lonlat, file_sfc)
            #lonlat = np.array(data_domain.lonlat)
            dmap_meps = get_data(model=model,
                                 param=param_sfc,
                                 file=file_sfc,
                                 step=steps,
                                 date=dt,
                                 data_domain=data_domain)
            print("\n######## Retriving data ############")
            print(f"--------> from: {dmap_meps.url} ")
            dmap_meps.retrieve()

            # get pressure level data
            file_pl = check_pl.file
            tmap_meps = get_data(model=model,
                                 data_domain=data_domain,
                                 param=param_pl,
                                 file=file_pl,
                                 step=steps,
                                 date=dt,
                                 p_level=500)
            print("\n######## Retriving data ############")
            print(f"--------> from: {tmap_meps.url} ")
            tmap_meps.retrieve()

        print("DONE RETRIEVE")
        # convert fields
        print("cond airpressure")
        dmap_meps.air_pressure_at_sea_level /= 100
        print("cond geopotential_pl")
        tmap_meps.geopotential_pl /= 10.0
        print("cond uv")
        u, v = xwind2uwind(tmap_meps.x_wind_pl, tmap_meps.y_wind_pl,
                           tmap_meps.alpha)
        print("cond vel")
        vel = wind_speed(tmap_meps.x_wind_pl, tmap_meps.y_wind_pl)

        print("map setup")

        # plot map
        fig1, ax1 = plt.subplots(figsize=(7, 9))

        lonlat = [
            dmap_meps.longitude[0, 0], dmap_meps.longitude[-1, -1],
            dmap_meps.latitude[0, 0], dmap_meps.latitude[-1, -1]
        ]

        lon0 = dmap_meps.longitude_of_central_meridian_projection_lambert
        lat0 = dmap_meps.latitude_of_projection_origin_projection_lambert
        print("map =map")
        map = Basemap(llcrnrlon=lonlat[0],
                      llcrnrlat=lonlat[2],
                      urcrnrlon=lonlat[1],
                      urcrnrlat=lonlat[3],
                      resolution='i',
                      projection="lcc",
                      lon_0=lon0,
                      lat_0=lat0,
                      lat_1=lat0,
                      area_thresh=0.0001)
        print("map x y")
        x, y = map(dmap_meps.longitude, dmap_meps.latitude)
        print("start for loop")
        for tim in np.arange(np.min(steps), np.max(steps) + 1, 1):
            tidx = tim - np.min(steps)

            print('Plotting {0} + {1:02d} UTC'.format(dt, tim))

            # gather, filter and squeeze variables for plotting
            #plev1 = 3
            plev2 = 0
            ZS = dmap_meps.surface_geopotential[tidx, 0, :, :]
            MSLP = np.where(ZS < 3000,
                            dmap_meps.air_pressure_at_sea_level[tidx, 0, :, :],
                            np.NaN).squeeze()
            TP = precip_acc(dmap_meps.precipitation_amount_acc,
                            acc=1)[tidx, 0, :, :].squeeze()
            VEL = (vel[tidx, plev2, :, :]).squeeze()
            Z = (tmap_meps.geopotential_pl[tidx, plev2, :, :]).squeeze()
            # velocity bars
            #U = (u[tim, plev2, :, :]).squeeze()
            #V = (v[tim, plev2, :, :]).squeeze()
            # Rotation in basemap: I thought it was not needed when plotting in same proj as model.
            # https://psysmon.mertl-research.at/sourcedoc/autogen/psysmon.packages.geometry.editGeometry.Basemap.rotate_vector.html
            # https://www-k12.atmos.washington.edu/~ovens/wrfwinds.html
            # Ue, Ve = map.rotate_vector(U, V, tmap_meps.longitude, tmap_meps.latitude) #so that shapes in plot is relative correct to shape in wind?
            # clear subplot
            #plt.cla()
            cmap = plt.get_cmap(
                "viridis"
            )  # PuBuGn PuBuGn, nipy_spectral twilight  , plasma, gist_ncar viridis  inferno ,,,rainbow
            # lvl = [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000]
            lvl = [0.01, 0.2, 0.5, 1, 3, 5, 10, 15, 20, 25, 30]
            norm = mcolors.BoundaryNorm(lvl, cmap.N)

            try:  # workaround for a stupid matplotlib error not handling when all values are outside of range in lvl or all just nans..
                # https://github.com/SciTools/cartopy/issues/1290
                CF_prec = ax1.contourf(x,
                                       y,
                                       TP,
                                       zorder=10,
                                       cmap=cmap,
                                       norm=norm,
                                       alpha=0.4,
                                       antialiased=True,
                                       levels=lvl,
                                       extend="max")  #
            except:
                pass
            # MSLP with contour labels every 10 hPa
            C_P = ax1.contour(x,
                              y,
                              MSLP,
                              zorder=1,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 1),
                              colors='grey',
                              linewidths=0.5)
            C_P = ax1.contour(x,
                              y,
                              MSLP,
                              zorder=2,
                              alpha=1.0,
                              levels=np.arange(
                                  round(np.nanmin(MSLP), -1) - 10,
                                  round(np.nanmax(MSLP), -1) + 10, 10),
                              colors='grey',
                              linewidths=1.0,
                              label="MSLP [hPa]")
            ax1.clabel(C_P, C_P.levels, inline=True, fmt="%3.0f", fontsize=10)
            #skip = (slice(None, None, 7), slice(None, None, 7))
            #Cq = plt.barbs(x[skip][skip],y[skip][skip],Ue[skip][skip],Ve[skip][skip], zorder=1000, color = "r")
            CS = ax1.contour(x,
                             y,
                             VEL,
                             zorder=3,
                             alpha=1.0,
                             levels=np.arange(-80, 80, 5),
                             colors="green",
                             linewidths=0.7)
            # geopotential
            CS = ax1.contour(x,
                             y,
                             Z,
                             zorder=3,
                             alpha=1.0,
                             levels=np.arange(4600, 5800, 20),
                             colors="blue",
                             linewidths=0.7)
            ax1.clabel(CS, CS.levels, inline=True, fmt="%4.0f", fontsize=10)

            map.drawcoastlines(linewidth=0.5, color='black', ax=ax1, zorder=5)
            ##########################################################

            if legend:
                proxy = [
                    plt.axhline(y=0, xmin=1, xmax=1, color="green"),
                    plt.axhline(y=0, xmin=1, xmax=1, color="blue")
                ]
                plt.colorbar(CF_prec, fraction=0.046, pad=0.04)
                lg = ax1.legend(proxy, [
                    f"Wind strength [m/s] at {dmap_meps.pressure[plev2]:.0f} hPa",
                    f"Geopotential [{tmap_meps.units_geopotential_pl}] at {dmap_meps.pressure[plev2]:.0f} hPa"
                ])
                frame = lg.get_frame()
                frame.set_facecolor('white')
                frame.set_alpha(1)

            if info:
                plt.text(
                    x=0,
                    y=-1,
                    s=
                    "INFO: Reduced topographic noise by filtering with surface_geopotential bellow 3000",
                    fontsize=7)  # , bbox=dict(facecolor='white', alpha=0.5))

                #plt.show()
            fig1.savefig(
                "../../../output/{0}_Z500_VEL_P_{1}+{2:02d}.png".format(
                    model, dt, tim),
                bbox_inches="tight",
                dpi=200)
            ax1.cla()

        proxy = [
            plt.axhline(y=0, xmin=1, xmax=1, color="green"),
            plt.axhline(y=0, xmin=1, xmax=1, color="blue")
        ]

        fig2 = plt.figure(figsize=(2, 1.25))
        fig2, ax2 = plt.subplots()
        fig2.legend(proxy, [
            f"Wind strength [m/s] at {dmap_meps.pressure[plev2]:.0f} hPa",
            f"Geopotential [{tmap_meps.units_geopotential_pl}]{dmap_meps.pressure[plev2]:.0f} hPa"
        ])
        fig2.savefig("../../../output/{0}_Z500_VEL_P_LEGEND.png".format(model),
                     bbox_inches="tight",
                     dpi=200)

        fig3, ax3 = plt.subplots()
        fig3.colorbar(CF_prec, fraction=0.046, pad=0.04)
        ax3.remove()
        fig3.savefig(
            "../../../output/{0}_Z500_VEL_P_COLORBAR.png".format(model),
            bbox_inches="tight",
            dpi=200)

    plt.clf()