Beispiel #1
0
    def __init__(self, an_time, filename, label=""):

        qc_obs = surfex.dataset_from_file(an_time, filename)
        observations = []
        for i in range(0, len(qc_obs)):
            observations.append(
                Observation(qc_obs.obstimes[i], qc_obs.dataset.lons[i],
                            qc_obs.dataset.lats[i], qc_obs.dataset.elevs[i],
                            qc_obs.dataset.value[i]))

        ObservationSet.__init__(self, observations, label=label)
Beispiel #2
0
def write_obsmon_sqlite_file(**kwargs):
    modes = ["total", "land", "sea"]
    stat_cols = ["nobs", "fg_bias", "fg_abs_bias", "fg_rms", "fg_dep", "fg_uncorr", "bc", "an_bias", "an_abs_bias",
                 "an_rms", "an_dep"]

    an_time = kwargs["dtg"]
    if isinstance(an_time, str):
        an_time = datetime.strptime(an_time, "%Y%m%d%H")
    dtg = an_time.strftime("%Y%m%d%H")
    varname = kwargs["varname"]
    dbname = kwargs["output"]

    operator = "bilinear"
    if "operator" in kwargs:
        operator = kwargs["operator"]

    qc = kwargs["qc"]
    obs_titan = surfex.dataset_from_file(an_time, qc, skip_flags=[150])

    conn = open_db(dbname)
    create_db(conn, modes, stat_cols)
    fg_file = kwargs["fg_file"]
    fg_var = kwargs["file_var"]
    an_file = kwargs["an_file"]
    an_var = kwargs["file_var"]

    # Only first guess file implemented at the moment
    geo_in, validtime, an_field, glafs, gelevs = surfex.read_first_guess_netcdf_file(an_file, an_var)
    geo_in, validtime, fg_field, glafs, gelevs = surfex.read_first_guess_netcdf_file(fg_file, fg_var)

    fg_dep = surfex.Departure(operator, geo_in, obs_titan, fg_field, "first_guess").get_departure()
    an_dep = surfex.Departure(operator, geo_in, obs_titan, an_field, "analysis").get_departure()

    obs_titan = surfex.dataset_from_file(an_time, qc, skip_flags=[150, 199], fg_dep=fg_dep, an_dep=an_dep)

    populate_usage_db(conn, dtg, varname, obs_titan)
    populate_obsmon_db(conn, dtg, calculate_statistics(obs_titan, modes, stat_cols),
                       modes, stat_cols, varname)
    close_db(conn)
Beispiel #3
0
def write_obsmon_sqlite_file(args):
    modes = ["total", "land", "sea"]
    stat_cols = [
        "nobs", "fg_bias", "fg_abs_bias", "fg_rms", "fg_dep", "fg_uncorr",
        "bc", "an_bias", "an_abs_bias", "an_rms", "an_dep"
    ]

    dtg = args.DTG
    an_time = datetime.strptime(dtg, "%Y%m%d%H")
    varname = args.varname
    dbname = args.output

    obs_titan = surfex.dataset_from_file(an_time, args.qc, skip_flags=[150])

    conn = open_db(dbname)
    create_db(conn, modes, stat_cols)
    cache = surfex.Cache(False, 3600)
    fg_file = args.fg_file
    fg_var = args.file_var
    an_file = args.an_file
    an_var = args.file_var

    # Only first guess file implemented at the moment
    geo_in, validtime, an_field, glafs, gelevs = surfex.read_first_guess_netcdf_file(
        an_file, an_var)
    geo_in, validtime, fg_field, glafs, gelevs = surfex.read_first_guess_netcdf_file(
        fg_file, fg_var)
    dx = []
    dy = []
    for i in range(0, len(obs_titan.flags)):
        dx.append(0.5)
        dy.append(0.5)

    settings = {
        "nam_lonlatval": {
            "xx": obs_titan.lons[:],
            "xy": obs_titan.lats[:],
            "xdx": dx,
            "xdy": dy
        }
    }
    geo_out = surfex.LonLatVal(settings)

    an_interpolated_field = surfex.NearestNeighbour(geo_in, geo_out, distance_check=False, cache=cache). \
        interpolate(an_field)
    fg_interpolated_field = surfex.NearestNeighbour(geo_in, geo_out, distance_check=False, cache=cache). \
        interpolate(fg_field)
    fg_dep = []
    an_dep = []
    for o in range(0, len(obs_titan.flags)):
        fg_dep.append(obs_titan.values[o] - fg_interpolated_field[o])
        an_dep.append(obs_titan.values[o] - an_interpolated_field[o])

    obs_titan = surfex.dataset_from_file(an_time,
                                         args.qc,
                                         skip_flags=[150, 199],
                                         fg_dep=fg_dep,
                                         an_dep=an_dep)

    populate_usage_db(conn, dtg, varname, obs_titan)
    populate_obsmon_db(conn, dtg,
                       calculate_statistics(obs_titan, modes, stat_cols),
                       modes, stat_cols, varname)
    close_db(conn)
Beispiel #4
0
    def execute(self, **kwargs):

        if self.var_name in self.translation:
            var = self.translation[self.var_name]
        else:
            raise Exception

        hlength = 30000
        vlength = 100000
        wlength = 0.5
        land_only = True
        max_locations = 20
        elev_gradient = 0
        epsilon = 0.25

        hlength = self.config.get_setting("OBSERVATIONS#OI#" + var +
                                          "#HLENGTH",
                                          default=hlength)
        vlength = self.config.get_setting("OBSERVATIONS#OI#" + var +
                                          "#VLENGTH",
                                          default=vlength)
        wlength = self.config.get_setting("OBSERVATIONS#OI#" + var +
                                          "#WLENGTH",
                                          default=wlength)
        elev_gradient = self.config.get_setting("OBSERVATIONS#OI#" + var +
                                                "#GRADIENT",
                                                default=elev_gradient)
        land_only = self.config.get_setting("OBSERVATIONS#OI#" + var +
                                            "#LAND_ONLY",
                                            default=land_only)
        max_locations = self.config.get_setting("OBSERVATIONS#OI#" + var +
                                                "#MAX_LOCATIONS",
                                                default=max_locations)
        epsilon = self.config.get_setting("OBSERVATIONS#OI#" + var +
                                          "#EPISLON",
                                          default=epsilon)
        minvalue = self.config.get_setting("OBSERVATIONS#OI#" + var +
                                           "#MINVALUE",
                                           default=None,
                                           abort=False)
        maxvalue = self.config.get_setting("OBSERVATIONS#OI#" + var +
                                           "#MAXVALUE",
                                           default=None,
                                           abort=False)
        input_file = self.archive + "/raw_" + var + ".nc"
        output_file = self.archive + "/an_" + var + ".nc"

        # Get input fields
        geo, validtime, background, glafs, gelevs = surfex.read_first_guess_netcdf_file(
            input_file, var)

        an_time = validtime
        # Read OK observations
        obs_file = self.system_file_paths.get_system_file("obs_dir",
                                                          "qc_" + var +
                                                          ".json",
                                                          basedtg=self.dtg)
        observations = surfex.dataset_from_file(an_time, obs_file, qc_flag=0)

        field = surfex.horizontal_oi(geo,
                                     background,
                                     observations,
                                     gelevs=gelevs,
                                     glafs=glafs,
                                     hlength=hlength,
                                     vlength=vlength,
                                     wlength=wlength,
                                     land_only=land_only,
                                     max_locations=max_locations,
                                     elev_gradient=elev_gradient,
                                     epsilon=epsilon,
                                     minvalue=minvalue,
                                     maxvalue=maxvalue)

        if os.path.exists(output_file):
            os.unlink(output_file)
        surfex.write_analysis_netcdf_file(output_file,
                                          field,
                                          var,
                                          validtime,
                                          gelevs,
                                          glafs,
                                          new_file=True,
                                          geo=geo)