Exemplo n.º 1
0
async def async_main(path_to_data,
                     serve_bootstrap=False,
                     host="0.0.0.0",
                     port=None,
                     reg_sturdy_ref=None,
                     id=None,
                     name="ISIMIP AgMIP Phase3",
                     description=None):

    config = {
        "path_to_data": path_to_data,
        "host": host,
        "port": port,
        "id": id,
        "name": name,
        "description": description,
        "reg_sturdy_ref": reg_sturdy_ref,
        "serve_bootstrap": str(serve_bootstrap),
        "reg_category": "climate",
    }
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v
    print("config used:", config)

    conMan = async_helpers.ConnectionManager()

    interpolator, rowcol_to_latlon = ccdi.create_lat_lon_interpolator_from_json_coords_file(
        config["path_to_data"] + "/" + "latlon-to-rowcol.json")
    meta_plus_data = create_meta_plus_datasets(config["path_to_data"],
                                               interpolator, rowcol_to_latlon)
    service = ccdi.Service(meta_plus_data,
                           id=config["id"],
                           name=config["name"],
                           description=config["description"])

    if config["reg_sturdy_ref"]:
        registrator = await conMan.try_connect(config["reg_sturdy_ref"],
                                               cast_as=reg_capnp.Registrator)
        if registrator:
            unreg = await registrator.register(
                ref=service, categoryId=config["reg_category"]).a_wait()
            print("Registered ", config["name"], "climate service.")
            #await unreg.unregister.unregister().a_wait()
        else:
            print("Couldn't connect to registrator at sturdy_ref:",
                  config["reg_sturdy_ref"])

    if config["serve_bootstrap"].upper() == "TRUE":
        await async_helpers.serve_forever(config["host"], config["port"],
                                          service)
    else:
        await conMan.manage_forever()
Exemplo n.º 2
0
async def async_main(path_to_monica_parameters,
                     serve_bootstrap=False,
                     host="0.0.0.0",
                     port=None,
                     reg_sturdy_ref=None,
                     id=None,
                     name="MONICA Parameters Fertilizer Service",
                     description=None):

    config = {
        "path_to_min_ferts_dir":
        path_to_monica_parameters + "/mineral-fertilisers",
        "path_to_org_ferts_dir":
        path_to_monica_parameters + "/organic-fertilisers",
        "host": host,
        "port": str(port),
        "id": id,
        "name": name,
        "description": description,
        "reg_sturdy_ref": reg_sturdy_ref,
        "serve_bootstrap": str(serve_bootstrap),
        "reg_category": "fertilizer",
    }
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v
    print("config used:", config)

    conMan = async_helpers.ConnectionManager()

    service = Service(config["path_to_min_ferts_dir"],
                      config["path_to_org_ferts_dir"],
                      id=config["id"],
                      name=config["name"],
                      description=config["description"])

    if config["reg_sturdy_ref"]:
        registrator = await conMan.try_connect(config["reg_sturdy_ref"],
                                               cast_as=reg_capnp.Registrator)
        if registrator:
            unreg = await registrator.register(
                ref=service, categoryId=config["reg_category"]).a_wait()
            print("Registered ", config["name"], "climate service.")
            #await unreg.unregister.unregister().a_wait()
        else:
            print("Couldn't connect to registrator at sturdy_ref:",
                  config["reg_sturdy_ref"])

    if config["serve_bootstrap"].upper() == "TRUE":
        await async_helpers.serve_forever(config["host"], config["port"],
                                          service)
    else:
        await conMan.manage_forever()
async def async_main(serve_bootstrap=True,
                     host=None,
                     port=10000,
                     reg_sturdy_ref=None):
    config = {
        "host": host,
        "port": port,
        "reg_sturdy_ref": reg_sturdy_ref,
        "serve_bootstrap": str(serve_bootstrap),
        "reg_category": "climate",
    }
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v
    print("config used:", config)

    conMan = async_helpers.ConnectionManager()

    service = Factory()
    admin = serv.Admin(service)
    service.admin = admin
    restorer = common.Restorer()

    if config["reg_sturdy_ref"]:
        registrator = await conMan.try_connect(config["reg_sturdy_ref"],
                                               cast_as=reg_capnp.Registrator)
        if registrator:
            unreg = await registrator.register(
                ref=service, categoryId=config["reg_category"]).a_wait()
            print("Registered ", config["name"], "climate service.")
            #await unreg.unregister.unregister().a_wait()
        else:
            print("Couldn't connect to registrator at sturdy_ref:",
                  config["reg_sturdy_ref"])

    if config["serve_bootstrap"].lower() == "true":
        server = await async_helpers.serve(config["host"], config["port"],
                                           restorer)

        admin_sr = restorer.save(admin)
        service_sr = restorer.save(service)
        print("admin_sr:", admin_sr)
        print("service_sr:", service_sr)
        print("restorer_sr:", restorer.sturdy_ref())

        async with server:
            await server.serve_forever()
    else:
        await conMan.manage_forever()
Exemplo n.º 4
0
async def async_main():
    config = {"port": "6003", "server": "localhost"}
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v

    conMan = async_helpers.ConnectionManager()
    registry = await conMan.connect(
        "capnp://insecure@localhost:9999/a8b8ff83-0af4-42c9-95c8-b6ec19a35945",
        registry_capnp.Registry)
    print(await registry.info().a_wait())

    yieldstat = await conMan.connect("capnp://localhost:15000",
                                     model_capnp.EnvInstance)
    info = await yieldstat.info().a_wait()
    print(info)

    time_series = await conMan.connect("capnp://localhost:11002",
                                       climate_data_capnp.TimeSeries)
    ts_header = (await time_series.header().a_wait()).header
    print(ts_header)

    run_req = yieldstat.run_request()
    env = run_req.env
    env.timeSeries = time_series
    env.rest = yieldstat_capnp.RestInput.new_message(
        useDevTrend=True,
        useCO2Increase=True,
        dgm=100.5,
        hft=53,
        nft=1,
        sft=36,
        slope=0,
        steino=1,
        az=14,
        klz=8,
        stt=152,
        germanFederalStates=5,  #-1
        getDryYearWaterNeed=True  #false;
    )
    cr = env.init("cropRotation", 3)
    cr[0].type = "sowing"
    cr[0].params = mgmt_capnp.Params.Sowing.new_message(cultivar="wheatWinter")
    cr[1].type = "irrigation"
    cr[2].type = "harvest"

    ys_res = (await
              run_req.send().a_wait()).result.as_struct(yieldstat_capnp.Output)
    print(ys_res)
Exemplo n.º 5
0
async def async_init_and_run_fbp_component(name_to_in_ports={},
                                           name_to_out_ports={},
                                           host=None,
                                           port=0,
                                           serve_bootstrap=True,
                                           restorer=None,
                                           conman=None,
                                           run_before_enter_eventloop=None,
                                           eventloop_wait_forever=True):

    port = port if port else 0

    if not conman:
        conman = async_helpers.ConnectionManager()
    if not restorer:
        restorer = common.Restorer()

    async def connect_out_ports():
        for name, data in name_to_out_ports.items():
            try:
                print("trying to connect out port:", name, " via sr:",
                      data["sr"])
                data["port"] = await conman.try_connect(
                    data["sr"], cast_as=fbp_capnp.Input, retry_secs=1)
            except Exception as e:
                print("Error connecting to out port:", name, ". Exception:", e)

    if serve_bootstrap:
        server = await async_helpers.serve(host, port, restorer)

        # make input ports available via restorer
        for name, data in name_to_in_ports.items():
            if data["port"] and data["sr_token"]:
                port_sr, _ = restorer.save(data["port"], data["sr_token"])
            print("in_port:", name, "sr:", port_sr)
        print("restorer_sr:", restorer.sturdy_ref())

        await connect_out_ports()

        if run_before_enter_eventloop:
            run_before_enter_eventloop()
        if eventloop_wait_forever:
            async with server:
                await server.serve_forever()
    else:
        await connect_out_ports()
        if run_before_enter_eventloop:
            run_before_enter_eventloop()
        if eventloop_wait_forever:
            await conman.manage_forever()
Exemplo n.º 6
0
async def async_main(serve_bootstrap=False,
                     host="0.0.0.0",
                     port=None,
                     reg_sturdy_ref=None):
    config = {
        "host": host,
        "port": port,
        "reg_sturdy_ref": reg_sturdy_ref,
        "serve_bootstrap": str(serve_bootstrap),
        "reg_category": "climate",
    }
    # read commandline args only if script is invoked directly from commandline
    if len(sys.argv) > 1 and __name__ == "__main__":
        for arg in sys.argv[1:]:
            k, v = arg.split("=")
            if k in config:
                config[k] = v
    print("config used:", config)

    conMan = async_helpers.ConnectionManager()

    service = A_Impl()

    if config["reg_sturdy_ref"]:
        registrator = await conMan.try_connect(config["reg_sturdy_ref"],
                                               cast_as=reg_capnp.Registrator)
        if registrator:
            unreg = await registrator.register(
                ref=service, categoryId=config["reg_category"]).a_wait()
            print("Registered ", config["name"], "climate service.")
            #await unreg.unregister.unregister().a_wait()
        else:
            print("Couldn't connect to registrator at sturdy_ref:",
                  config["reg_sturdy_ref"])

    if config["serve_bootstrap"].upper() == "TRUE":
        await async_helpers.serve_forever(config["host"], config["port"],
                                          service)
    else:
        await conMan.manage_forever()
async def main(path_to_config,
               serve_bootstrap=True,
               host=None,
               port=None,
               id=None,
               name="DWD Core Ensemble",
               description=None,
               reg_sturdy_ref=None,
               use_async=False):

    config = {
        "path_to_config": path_to_config,
        "config_toml_file": "metadata.toml",
        "port": port,
        "host": host,
        "id": id,
        "name": name,
        "description": description,
        "serve_bootstrap": serve_bootstrap,
        "reg_sturdy_ref": reg_sturdy_ref,
        "reg_category": "climate",
        "use_async": use_async,
    }
    common.update_config(config,
                         sys.argv,
                         print_config=True,
                         allow_new_keys=False)

    path_to_config = Path(config["path_to_config"])
    with open(path_to_config / config["config_toml_file"], "rb") as f:
        datasets_config = tomli.load(f)

    if not datasets_config:
        print("Couldn't load datasets configuration from:",
              str(path_to_config / config["config_toml_file"]))
        exit(1)

    general = datasets_config["general"]

    conman = async_helpers.ConnectionManager()
    restorer = common.Restorer()
    interpolator, rowcol_to_latlon = ccdi.create_lat_lon_interpolator_from_json_coords_file(
        path_to_config / general["latlon_to_rowcol_mapping"])
    meta_plus_data = create_meta_plus_datasets(path_to_config, datasets_config,
                                               interpolator, rowcol_to_latlon,
                                               restorer)
    service = ccdi.Service(meta_plus_data,
                           id=config["id"],
                           name=config["name"],
                           description=config["description"],
                           restorer=restorer)

    if config["reg_sturdy_ref"]:
        registrator = await conman.try_connect(config["reg_sturdy_ref"],
                                               cast_as=reg_capnp.Registrator)
        if registrator:
            unreg = await registrator.register(
                ref=service, categoryId=config["reg_category"]).a_wait()
            print("Registered ", config["name"], "climate service.")
            #await unreg.unregister.unregister().a_wait()
        else:
            print("Couldn't connect to registrator at sturdy_ref:",
                  config["reg_sturdy_ref"])

    if config["use_async"]:
        await serv.async_init_and_run_service(
            {"service": service},
            config["host"],
            config["port"],
            serve_bootstrap=config["serve_bootstrap"],
            restorer=restorer,
            conman=conman)
    else:

        serv.init_and_run_service({"service": service},
                                  config["host"],
                                  config["port"],
                                  serve_bootstrap=config["serve_bootstrap"],
                                  restorer=restorer,
                                  conman=conman)