示例#1
0
def fbp(config, service : grid_capnp.Grid):
    conman = common.ConnectionManager()
    inp = conman.try_connect(config["in_sr"], cast_as=common_capnp.Channel.Reader, retry_secs=1)
    outp = conman.try_connect(config["out_sr"], cast_as=common_capnp.Channel.Writer, retry_secs=1)

    try:
        if inp and outp and service:
            while True:
                in_msg = inp.read().wait()
                if in_msg.which() == "done":
                    break
                
                in_ip = in_msg.value.as_struct(common_capnp.IP)
                attr = common.get_fbp_attr(in_ip, config["from_attr"])
                if attr:
                    coord = attr.as_struct(geo_capnp.LatLonCoord)
                else:
                    coord = in_ip.content.as_struct(geo_capnp.LatLonCoord)

                val = service.closestValueAt(coord).wait().val

                out_ip = common_capnp.IP.new_message()
                if not config["to_attr"]:
                    out_ip.content = val
                common.copy_fbp_attr(in_ip, out_ip, config["to_attr"], val)
                outp.write(value=out_ip).wait()
            
            outp.write(done=None).wait()

    except Exception as e:
        print("ascii_grid.py ex:", e)

    print("ascii_grid.py: exiting FBP component")
def fbp(config, service: soil_capnp.Service):
    conman = common.ConnectionManager()
    inp = conman.try_connect(config["in_sr"],
                             cast_as=common_capnp.Channel.Reader,
                             retry_secs=1)
    outp = conman.try_connect(config["out_sr"],
                              cast_as=common_capnp.Channel.Writer,
                              retry_secs=1)
    mandatory = json.loads(config["mandatory"])

    try:
        if inp and outp and service:
            while True:
                in_msg = inp.read().wait()
                if in_msg.which() == "done":
                    break

                in_ip = in_msg.value.as_struct(common_capnp.IP)
                attr = common.get_fbp_attr(in_ip, config["from_attr"])
                if attr:
                    coord = attr.as_struct(geo_capnp.LatLonCoord)
                else:
                    coord = in_ip.content.as_struct(geo_capnp.LatLonCoord)

                profiles = service.profilesAt(coord, {
                    "mandatory": mandatory,
                    "onlyRawData": False
                }).wait().profiles
                if len(profiles) > 0:
                    profile = profiles[0]

                    out_ip = common_capnp.IP.new_message()
                    if not config["to_attr"]:
                        out_ip.content = profile
                    common.copy_fbp_attr(in_ip, out_ip, config["to_attr"],
                                         profile)
                    outp.write(value=out_ip).wait()

            outp.write(done=None).wait()

    except Exception as e:
        print("sqlite_soil_data_service.py ex:", e)

    print("dwd_germany_service.py: exiting FBP component")
示例#3
0
conman = common.ConnectionManager()
inp = conman.try_connect(config["in_sr"], cast_as=common_capnp.Channel.Reader, retry_secs=1)
outp = conman.try_connect(config["out_sr"], cast_as=common_capnp.Channel.Writer, retry_secs=1)

from_type = geo.name_to_struct_type(config["from_name"])

try:
    if inp and outp:
        while True:
            msg = inp.read().wait()
            # check for end of data from in port
            if msg.which() == "done":
                break
            
            in_ip = msg.value.as_struct(common_capnp.IP)
            attr = common.get_fbp_attr(in_ip, config["from_attr"])
            if attr:
                from_coord = attr.as_struct(from_type)
            else:
                from_coord = in_ip.content.as_struct(from_type)
            to_coord = geo.transform_from_to_geo_coord(from_coord, config["to_name"])
            out_ip = common_capnp.IP.new_message()
            if not config["to_attr"]:
                out_ip.content = to_coord
            common.copy_fbp_attr(in_ip, out_ip, config["to_attr"], to_coord)
            outp.write(value=out_ip).wait()

        # close out port
        outp.write(done=None).wait()

except Exception as e:
                          retry_secs=1)

lift_from_type = common.load_capnp_module(config["lift_from_type"])
lft_fieldnames = lift_from_type.schema.fieldnames
lift_from_attr_name = config["lift_from_attr"]

try:
    if inp and outp:
        while True:
            msg = inp.read().wait()
            # check for end of data from in port
            if msg.which() == "done":
                break

            in_ip = msg.value.as_struct(common_capnp.IP)
            lift_from_attr = common.get_fbp_attr(
                in_ip, config["lift_from_attr"]).as_struct(lift_from_type)

            out_ip = common_capnp.IP.new_message(content=in_ip.content)
            lifted_attrs = config["lifted_attrs"].split(",")
            attrs = []
            for attr in in_ip.attributes:
                attrs.append(attr)
            if lift_from_attr:
                for lattr_name in lifted_attrs:
                    if lattr_name in lft_fieldnames:
                        attrs.append({
                            "key":
                            lattr_name,
                            "value":
                            lift_from_attr.__getattr__(lattr_name)
                        })
}
common.update_config(config, sys.argv, print_config=True, allow_new_keys=False)

conman = common.ConnectionManager()
inp = conman.try_connect(config["in_sr"], cast_as=common_capnp.Channel.Reader, retry_secs=1)
count = 0

try:
    if inp:
        while True:
            msg = inp.read().wait()
            if msg.which() == "done":
                break

            in_ip = msg.value.as_struct(common_capnp.IP)
            id_attr = common.get_fbp_attr(in_ip, config["id_attr"])
            id = id_attr.as_text() if id_attr else str(count)
            out_path_attr = common.get_fbp_attr(in_ip, config["out_path_attr"])
            out_path = out_path_attr.as_text() if out_path_attr else config["path_to_out_dir"]

            dir = out_path
            if os.path.isdir(dir) and os.path.exists(dir):
                pass
            else:
                try:
                    os.makedirs(dir)
                except OSError:
                    print("c: Couldn't create dir:", dir, "! Exiting.")
                    exit(1)

            filepath = dir + "/" + config["file_pattern"].format(id=id)
示例#6
0
def fbp(config : dict, service : ccdi.Service):
    conman = common.ConnectionManager()
    inp = conman.try_connect(config["in_sr"], cast_as=common_capnp.Channel.Reader, retry_secs=1)
    outp = conman.try_connect(config["out_sr"], cast_as=common_capnp.Channel.Writer, retry_secs=1)
    mode = config["mode"]

    def iso_to_cdate(iso_date_str):
        ds = iso_date_str.split("-")
        return {"year": int(ds[0]), "month": int(ds[1]), "day": int(ds[2])} 

    try:
        if inp and outp and service:
            dataset : csv_based.Dataset = service.getAvailableDatasets().wait().datasets[0].data
            while True:
                in_msg = inp.read().wait()
                if in_msg.which() == "done":
                    break

                in_ip = in_msg.value.as_struct(common_capnp.IP)
                attr = common.get_fbp_attr(in_ip, config["latlon_attr"])
                if attr:
                    coord = attr.as_struct(geo_capnp.LatLonCoord)
                else:
                    coord = in_ip.content.as_struct(geo_capnp.LatLonCoord)
                start_date = common.get_fbp_attr(in_ip, config["start_date_attr"]).as_text()
                end_date = common.get_fbp_attr(in_ip, config["end_date_attr"]).as_text()

                timeseries_p : csv_based.TimeSeries = dataset.closestTimeSeriesAt(coord).timeSeries
                timeseries = timeseries_p.subrange(iso_to_cdate(start_date), iso_to_cdate(end_date)).wait().timeSeries

                res = timeseries
                if mode == "sturdyref":
                    res = timeseries.save().wait()
                elif mode == "capability":
                    res = timeseries
                elif mode == "data":
                    res = climate_capnp.TimeSeriesData.new_message()
                    res.isTransposed = False
                    header = timeseries.header().header
                    se_date = timeseries.range()
                    resolution = timeseries.resolution().resolution
                    res.data = timeseries.data().wait().data
                    res.header = header
                    se_date = se_date
                    res.startDate = se_date.startDate
                    res.endDate = se_date.endDate
                    res.resolution = resolution

                #print(res.data().wait())
                out_ip = common_capnp.IP.new_message()
                if not config["to_attr"]:
                    out_ip.content = res
                common.copy_fbp_attr(in_ip, out_ip, config["to_attr"], res)
                outp.write(value=out_ip).wait()


            outp.write(done=None).wait()

    except Exception as e:
        print("dwd_germany_service.py ex:", e)

    print("dwd_germany_service.py: exiting FBP component")
示例#7
0
              path_harvest)
        create_seed_harvest_geoGrid_interpolator_and_read_data(
            path_harvest, wgs84_crs, utm32n_crs, ilr_seed_harvest_data)
except IOError:
    print("Couldn't read file:", path_harvest)
    exit(1)

try:
    if inp and outp:
        while True:
            in_msg = inp.read().wait()
            if in_msg.which() == "done":
                break

            in_ip = in_msg.value.as_struct(common_capnp.IP)
            latlon = common.get_fbp_attr(
                in_ip, config["latlon_attr"]).as_struct(geo_capnp.LatLonCoord)
            sowing_time = common.get_fbp_attr(
                in_ip, config["sowing_time_attr"]).as_text()
            harvest_time = common.get_fbp_attr(
                in_ip, config["harvest_time_attr"]).as_text()
            crop_id = common.get_fbp_attr(in_ip,
                                          config["crop_id_attr"]).as_text()

            utm = geo.transform_from_to_geo_coord(latlon, "utm32n")
            ilr_interpolate = ilr_seed_harvest_data[crop_id]["interpolate"]
            seed_harvest_cs = ilr_interpolate(
                utm.r, utm.h) if ilr_interpolate else None

            out_ip = common_capnp.IP.new_message()
            if ilr_interpolate is None or seed_harvest_cs is None:
                common.copy_fbp_attr(in_ip, out_ip)