def evaluate_impl(expression, params=None):
    '''Implementation of CalculatorImpl::evaluate(), also shared by
    FunctionImpl::call().  In the latter case, `params` are the parameter
    values passed to the function; in the former case, `params` is just an
    empty list.'''

    which = expression.which()

    if which == 'literal':
        return capnp.Promise(expression.literal)
    elif which == 'previousResult':
        return read_value(expression.previousResult)
    elif which == 'parameter':
        assert expression.parameter < len(params)
        return capnp.Promise(params[expression.parameter])
    elif which == 'call':
        call = expression.call
        func = call.function

        # Evaluate each parameter.
        paramPromises = [evaluate_impl(param, params) for param in call.params]

        joinedParams = capnp.join_promises(paramPromises)
        # When the parameters are complete, call the function.
        ret = (joinedParams
               .then(lambda vals: func.call(vals))
               .then(lambda result: result.value))

        return ret
    else:
        raise ValueError("Unknown expression type: " + which)
def evaluate_impl(expression, params=None):
    '''Implementation of CalculatorImpl::evaluate(), also shared by
    FunctionImpl::call().  In the latter case, `params` are the parameter
    values passed to the function; in the former case, `params` is just an
    empty list.'''

    which = expression.which()

    if which == 'literal':
        return capnp.Promise(expression.literal)
    elif which == 'previousResult':
        return read_value(expression.previousResult)
    elif which == 'parameter':
        assert expression.parameter < len(params)
        return capnp.Promise(params[expression.parameter])
    elif which == 'call':
        call = expression.call
        func = call.function

        # Evaluate each parameter.
        paramPromises = [evaluate_impl(param, params) for param in call.params]

        joinedParams = capnp.join_promises(paramPromises)
        # When the parameters are complete, call the function.
        ret = (joinedParams.then(lambda vals: func.call(vals)).then(
            lambda result: result.value))

        return ret
    else:
        raise ValueError("Unknown expression type: " + which)
Example #3
0
def offer_cap(cap_id):
    print("should offer", cap_id)
    cap_id = base64.urlsafe_b64decode(cap_id)

    sys.stdout.flush()
    # Restore the sturdyref into a liveref
    bridge_cap = get_bridge_cap()
    liveref_promise = bridge_cap.getSandstormApi().then(
        lambda res: res.api.cast_as(grain.SandstormApi).restore(cap_id)
    )

    # Then, offer that liveref to the requesting user's session context
    session_id = request.headers["X-Sandstorm-Session-Id"]
    session_ctx_promise = bridge_cap.getSessionContext(session_id)

    def offerCap(res):
        session_ctx_resp, liveref_resp = res
        session_ctx = session_ctx_resp.context.cast_as(hack_session.HackSessionContext)
        liveref = liveref_resp.cap
        uiViewTag = powerbox.PowerboxDescriptor.Tag.new_message(id=grain.UiView.schema.node.id)
        descriptor = powerbox.PowerboxDescriptor.new_message(tags=[uiViewTag])
        displayInfo = powerbox.PowerboxDisplayInfo.new_message(
            title=util.LocalizedText.new_message(defaultText="some title"),
            verbPhrase=util.LocalizedText.new_message(defaultText="some verbPhrase"),
            description=util.LocalizedText.new_message(defaultText="some description"),
        )
        # PowerboxDescriptor is:
        # tags: List(Tag)
        #   tag is:
        #     id uint64 (capnproto type id of an interface or capnproto type id of a struct type)
        #     value AnyPointer (optional, if struct type id above, an instance of that struct)

        # PowerboxDisplayInfo is
        # title
        # verbPhrase
        # description
        # all three are Util.LocalizedText
        return session_ctx.offer(cap=liveref, descriptor=descriptor, displayInfo=displayInfo)

    capnp.join_promises([session_ctx_promise, liveref_promise]).then(
        offerCap
    ).wait()

    return make_response("", 200)
Example #4
0
def offer_cap(cap_id):
    debug("should offer", cap_id)
    cap_id = base64.urlsafe_b64decode(cap_id)

    # Restore the sturdyref into a liveref
    bridge_cap = get_bridge_cap()
    liveref_promise = bridge_cap.getSandstormApi().then(
        lambda res: res.api.cast_as(grain.SandstormApi).restore(cap_id)
    )

    # Then, offer that liveref to the requesting user's session context
    session_id = request.headers["X-Sandstorm-Session-Id"]
    session_ctx_promise = bridge_cap.getSessionContext(session_id)

    def offerCap(res):
        session_ctx_resp, liveref_resp = res
        session_ctx = session_ctx_resp.context.cast_as(hack_session.HackSessionContext)
        liveref = liveref_resp.cap
        uiViewTag = powerbox.PowerboxDescriptor.Tag.new_message(id=grain.UiView.schema.node.id)
        descriptor = powerbox.PowerboxDescriptor.new_message(tags=[uiViewTag])
        displayInfo = powerbox.PowerboxDisplayInfo.new_message(
            title=util.LocalizedText.new_message(defaultText="some title"),
            verbPhrase=util.LocalizedText.new_message(defaultText="some verbPhrase"),
            description=util.LocalizedText.new_message(defaultText="some description"),
        )
        # PowerboxDescriptor is:
        # tags: List(Tag)
        #   tag is:
        #     id uint64 (capnproto type id of an interface or capnproto type id of a struct type)
        #     value AnyPointer (optional, if struct type id above, an instance of that struct)

        # PowerboxDisplayInfo is
        # title
        # verbPhrase
        # description
        # all three are Util.LocalizedText
        return session_ctx.offer(cap=liveref, descriptor=descriptor, displayInfo=displayInfo)

    capnp.join_promises([session_ctx_promise, liveref_promise]).then(
        offerCap
    ).wait()

    return make_response("", 200)
Example #5
0
    def send(self, text, _context, **kwargs):
        message = Capnp_Message.new_message(
            author=self.client.name,
            content=text,
        )
        chatroom = self.chatroom
        chatroom.messages.append(message)
        self.server.save_room(chatroom.name)

        promises = []
        for client in chatroom.users:
            if client.name == self.client.name:
                continue
            promises.append(client.send(message))

        return capnp.join_promises(promises)
Example #6
0
def test_timer():
    global test_timer_var
    test_timer_var = False

    def set_timer_var():
        global test_timer_var
        test_timer_var = True
    capnp.getTimer().after_delay(1).then(set_timer_var).wait()

    assert test_timer_var is True

    test_timer_var = False
    promise = capnp.Promise(0).then(lambda x: time.sleep(.1)).then(lambda x: time.sleep(.1)).then(lambda x: set_timer_var())

    canceller = capnp.getTimer().after_delay(1).then(lambda: promise.cancel())

    joined = capnp.join_promises([canceller, promise])
    joined.wait()
Example #7
0
def test_timer():
    global test_timer_var
    test_timer_var = False

    def set_timer_var():
        global test_timer_var
        test_timer_var = True

    capnp.getTimer().after_delay(1).then(set_timer_var).wait()

    assert test_timer_var is True

    test_timer_var = False
    promise = capnp.Promise(0).then(lambda x: time.sleep(.1)).then(
        lambda x: time.sleep(.1)).then(lambda x: set_timer_var())

    canceller = capnp.getTimer().after_delay(1).then(lambda: promise.cancel())

    joined = capnp.join_promises([canceller, promise])
    joined.wait()
Example #8
0
def main():

    config = {
        "port": "6666",
        "server": "localhost",
        "sim.json": os.path.join(os.path.dirname(__file__), '../sim-min.json'),
        "crop.json": os.path.join(os.path.dirname(__file__), '../crop-min.json'),
        "site.json": os.path.join(os.path.dirname(__file__), '../site-min.json'),
        "climate.csv": os.path.join(os.path.dirname(__file__), '../climate-min.csv'),
        "shared_id": None,
        "climate_service_address": "10.10.24.186:11000",
        "admin_master_address": "10.10.24.186:8000"
    }
    # 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:", config)

    with open(config["sim.json"]) as _:
        sim_json = json.load(_)

    with open(config["site.json"]) as _:
        site_json = json.load(_)

    with open(config["crop.json"]) as _:
        crop_json = json.load(_)

    env = monica_io3.create_env_json_from_json_config({
        "crop": crop_json,
        "site": site_json,
        "sim": sim_json,
        "climate": ""  # climate_csv
    })
    # env["csvViaHeaderOptions"] = sim_json["climate.csv-options"]
    env["pathToClimateCSV"] = "blbla"  # config["climate.csv"]

    with open("env.json", "r") as _:
        env = json.load(_)

    # with open("env.json", "w") as _:
    #    _.write(json.dumps(env))

    # rust_client = capnp.TwoPartyClient("localhost:4000")
    # client = capnp.TwoPartyClient("localhost:8000")

    csv_time_series = capnp.TwoPartyClient(config["climate_service_address"]).bootstrap().cast_as(
        climate_data_capnp.Climate.TimeSeries)
    # monica = capnp.TwoPartyClient("localhost:9999").bootstrap().cast_as(model_capnp.Model.EnvInstance)

    master_available = False
    while not master_available:
        try:
            admin_master = capnp.TwoPartyClient(config["admin_master_address"]).bootstrap().cast_as(
                cluster_admin_service_capnp.Cluster.AdminMaster)
            master_available = True
        except:
            # time.sleep(1)
            pass

    model_factories = admin_master.availableModels().wait().factories
    if len(model_factories) > 0:

        model_factory = model_factories[0]

        # get instance id
        print(model_factory.modelId().wait().id)

        if True:
            # get a single instance
            print("requesting single instance ...")
            cap_holder = model_factory.newInstance().instance
            # cap_holder = model_factory.restoreSturdyRef(
            #    "b37e2e43-8a72-4dc0-8cb1-2bdc416d8148").cap
            monica = cap_holder.cap().wait().cap.as_interface(model_capnp.Model.EnvInstance)
            sturdy_ref = cap_holder.save().wait().sturdyRef
            print("single instance sturdy ref:", sturdy_ref)

            proms = []
            print("running jobs on single instance ...")
            for i in range(5):
                env["customId"] = str(i)
                proms.append(monica.run({"rest": {"value": json.dumps(env), "structure": {
                             "json": None}}, "timeSeries": csv_time_series}))

            print("results from single instance ...")
            for res in capnp.join_promises(proms).wait():
                if len(res.result.value) > 0:
                    # .result["customId"])
                    print(json.loads(res.result.value)["customId"])

            # cap_holder.release().wait()

        if True:
            # get multiple instances
            print("requesting multiple instances ...")
            cap_holders = model_factory.newInstances(5).instances
            restore = True
            # cap_holders = model_factory.restoreSturdyRef(
            #    "96495c8f-e241-4289-bee4-f314fef5b16d").cap
            sturdy_ref_p = cap_holders.save()
            entries_p = cap_holders.cap()
            sturdy_ref_p, entries_p = capnp.join_promises(
                [sturdy_ref_p, entries_p]).wait()
            entries = entries_p.cap

            sturdy_ref = sturdy_ref_p.sturdyRef
            print("multi instance sturdy ref:", sturdy_ref)
            monica_proms = []
            for ent in entries:
                monica_proms.append(ent.entry.cap().then(
                    lambda res: res.cap.as_interface(model_capnp.Model.EnvInstance)))

            monicas = capnp.join_promises(monica_proms).wait()

            proms = []
            print("running jobs on multiple instances ...")
            for i in range(len(monicas)):
                env["customId"] = str(i)
                proms.append(monicas[i].run({"rest": {"value": json.dumps(
                    env), "structure": {"json": None}}, "timeSeries": csv_time_series}))

            print("results from multiple instances ...")
            for res in capnp.join_promises(proms).wait():
                if len(res.result.value) > 0:
                    # .result["customId"])
                    print(json.loads(res.result.value)["customId"])

    return

    """
    climate_service = capnp.TwoPartyClient("localhost:8000").bootstrap().cast_as(climate_data_capnp.Climate.DataService)

    sims_prom = climate_service.simulations_request().send()
    sims = sims_prom.wait()
    
    if len(sims.simulations) > 0:
        info_prom = sims.simulations[1].info()
        info = info_prom.wait()
        print("id:", info.info.id)

        sim = sims.simulations[1]
        scens_p = sim.scenarios()
        scens = scens_p.wait()

        if len(scens.scenarios) > 0:
            scen = scens.scenarios[0]
            reals_p = scen.realizations()
            reals = reals_p.wait()

            if len(reals.realizations) > 0:
                real = reals.realizations[0]
                ts_p = real.closestTimeSeriesAt({"latlon": {"lat": 52.5, "lon": 14.1}})
                ts = ts_p.wait().timeSeries

                ts.range().then(lambda r: print(r)).wait()
                ts.realizationInfo().then(lambda r: print(r.realInfo)).wait()
                ts.scenarioInfo().then(lambda r: print(r.scenInfo)).wait()
                ts.simulationInfo().then(lambda r: print(r.simInfo)).wait()
    """

    csv_time_series = capnp.TwoPartyClient("localhost:8000").bootstrap().cast_as(
        climate_data_capnp.Climate.TimeSeries)
    # header = csv_time_series.header().wait().header
    monica_instance = capnp.TwoPartyClient(
        "localhost:9999").bootstrap().cast_as(model_capnp.Model.EnvInstance)
    # monica_instance = capnp.TwoPartyClient("login01.cluster.zalf.de:9999").bootstrap().cast_as(model_capnp.Model.EnvInstance)

    proms = []
    for i in range(10):
        env["customId"] = str(i)
        proms.append(monica_instance.run({"rest": {"value": json.dumps(
            env), "structure": {"json": None}}, "timeSeries": csv_time_series}))

    for i in range(10):
        ps = proms[i * 50:i * 50 + 50]

        for res in capnp.join_promises(ps).wait():
            if len(res.result.value) > 0:
                # .result["customId"])
                print(json.loads(res.result.value)["customId"])

    return

    reslist = capnp.join_promises(proms).wait()
    # reslist.wait()

    for res in reslist:
        print(json.loads(res.result.value)["customId"])  # .result["customId"])

    #        .then(lambda res: setattr(_context.results, "result", \
    #           self.calc_yearly_tavg(res[2].startDate, res[2].endDate, res[0].header, res[1].data)))

    # result_j = monica_instance.runEnv({"jsonEnv": json.dumps(env), "timeSeries": csv_time_series}).wait().result
    # result = json.loads(result_j)

    # print("result:", result)

    """
    # req = model.run_request()
    # req.data = ts
    # result = req.send().wait().result
    tavg_ts = ts.subheader(["tavg"]).wait().timeSeries
    start_time = time.perf_counter()
    result = model.run(tavg_ts).wait().result
    end_time = time.perf_counter()
    print("rust:", result, "time:", (end_time - start_time), "s")
    """

    """
def main():

    config = {
        "port": "6666",
        "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

    print("config:", config)

    """
    climate_service = capnp.TwoPartyClient("localhost:8000").bootstrap().cast_as(climate_data_capnp.DataService)

    sims_prom = climate_service.simulations_request().send()
    sims = sims_prom.wait()
    
    if len(sims.simulations) > 0:
        info_prom = sims.simulations[1].info()
        info = info_prom.wait()
        print("id:", info.info.id)

        sim = sims.simulations[1]
        scens_p = sim.scenarios()
        scens = scens_p.wait()

        if len(scens.scenarios) > 0:
            scen = scens.scenarios[0]
            reals_p = scen.realizations()
            reals = reals_p.wait()

            if len(reals.realizations) > 0:
                real = reals.realizations[0]
                ts_p = real.closestTimeSeriesAt({"latlon": {"lat": 52.5, "lon": 14.1}})
                ts = ts_p.wait().timeSeries

                ts.range().then(lambda r: print(r)).wait()
                ts.realizationInfo().then(lambda r: print(r.realInfo)).wait()
                ts.scenarioInfo().then(lambda r: print(r.scenInfo)).wait()
                ts.simulationInfo().then(lambda r: print(r.simInfo)).wait()
    """

    #cmip_service = capnp.TwoPartyClient("login01.cluster.zalf.de:11001").bootstrap().cast_as(climate_data_capnp.Service)
    cmip_service = capnp.TwoPartyClient("localhost:9000").bootstrap().cast_as(climate_data_capnp.Service)
    #header = csv_time_series.header().wait().header

    print(cmip_service.info().wait())

    all_dss = cmip_service.getAvailableDatasets().wait().datasets
    mpd0 = all_dss[0]
    es = mpd0.meta.entries
    ds = mpd0.data
    ts = ds.closestTimeSeriesAt({"lat": 50.0, "lon": 12.0}).wait().timeSeries
    h = ts.header().wait().header
    data = ts.data().wait().data
    loc = ts.location().wait()

    some_dss = cmip_service.getDatasetsFor({"entries": [{"gcm": "ichecEcEarth"}]}).wait().datasets
    ds2 = some_dss[0]
    md2 = ds2.metadata().wait()
    print(md2)
    info2 = md2.info.forOne({"gcm": 0}).wait()
    print(info2)
    infos2 = md2.info.forAll().wait().all
    print(infos2)
    locs = ds2.locations().wait().locations


    proms = []
    for i in range(10):
        env["customId"] = str(i)
        proms.append(monica_instance.run({"rest": {"value": json.dumps(env), "structure": {"json": None}}, "timeSeries": csv_time_series}))

    
    for i in range (10):
        ps = proms[i*50:i*50+50]

        for res in capnp.join_promises(ps).wait():
            if len(res.result.value) > 0:
                print(json.loads(res.result.value)["customId"]) #.result["customId"])

    return

    reslist = capnp.join_promises(proms).wait()
    #reslist.wait()
    
    for res in reslist:
        print(json.loads(res.result.value)["customId"]) #.result["customId"])

    #        .then(lambda res: setattr(_context.results, "result", \
    #           self.calc_yearly_tavg(res[2].startDate, res[2].endDate, res[0].header, res[1].data))) 

    #result_j = monica_instance.runEnv({"jsonEnv": json.dumps(env), "timeSeries": csv_time_series}).wait().result
    #result = json.loads(result_j)

    #print("result:", result)


    """
    #req = model.run_request()
    #req.data = ts
    #result = req.send().wait().result
    tavg_ts = ts.subheader(["tavg"]).wait().timeSeries
    start_time = time.perf_counter()
    result = model.run(tavg_ts).wait().result
    end_time = time.perf_counter()
    print("rust:", result, "time:", (end_time - start_time), "s")
    """     

    """
Example #10
0
def main():

    config = {
        "port":
        "6666",
        "server":
        "localhost",
        "sim.json":
        os.path.join(os.path.dirname(__file__), '../sim-min.json'),
        "crop.json":
        os.path.join(os.path.dirname(__file__), '../crop-min.json'),
        "site.json":
        os.path.join(os.path.dirname(__file__), '../site-min.json'),
        "climate.csv":
        os.path.join(os.path.dirname(__file__), '../climate-min.csv'),
        "shared_id":
        None
    }
    # 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:", config)

    with open(config["sim.json"]) as _:
        sim_json = json.load(_)

    with open(config["site.json"]) as _:
        site_json = json.load(_)

    with open(config["crop.json"]) as _:
        crop_json = json.load(_)

    env = monica_io3.create_env_json_from_json_config({
        "crop": crop_json,
        "site": site_json,
        "sim": sim_json,
        "climate": ""  #climate_csv
    })
    #env["csvViaHeaderOptions"] = sim_json["climate.csv-options"]
    #env["pathToClimateCSV"] = config["climate.csv"]

    #rust_client = capnp.TwoPartyClient("localhost:4000")
    #client = capnp.TwoPartyClient("localhost:8000")
    """
    climate_service = capnp.TwoPartyClient("localhost:8000").bootstrap().cast_as(climate_data_capnp.Climate.DataService)

    sims_prom = climate_service.simulations_request().send()
    sims = sims_prom.wait()
    
    if len(sims.simulations) > 0:
        info_prom = sims.simulations[1].info()
        info = info_prom.wait()
        print("id:", info.info.id)

        sim = sims.simulations[1]
        scens_p = sim.scenarios()
        scens = scens_p.wait()

        if len(scens.scenarios) > 0:
            scen = scens.scenarios[0]
            reals_p = scen.realizations()
            reals = reals_p.wait()

            if len(reals.realizations) > 0:
                real = reals.realizations[0]
                ts_p = real.closestTimeSeriesAt({"latlon": {"lat": 52.5, "lon": 14.1}})
                ts = ts_p.wait().timeSeries

                ts.range().then(lambda r: print(r)).wait()
                ts.realizationInfo().then(lambda r: print(r.realInfo)).wait()
                ts.scenarioInfo().then(lambda r: print(r.scenInfo)).wait()
                ts.simulationInfo().then(lambda r: print(r.simInfo)).wait()
    """

    csv_time_series = capnp.TwoPartyClient(
        "localhost:8000").bootstrap().cast_as(
            climate_data_capnp.Climate.TimeSeries)
    #header = csv_time_series.header().wait().header
    monica_instance = capnp.TwoPartyClient(
        "localhost:9999").bootstrap().cast_as(model_capnp.Model.EnvInstance)
    #monica_instance = capnp.TwoPartyClient("login01.cluster.zalf.de:9999").bootstrap().cast_as(model_capnp.Model.EnvInstance)

    proms = []
    for i in range(10):
        env["customId"] = str(i)
        proms.append(
            monica_instance.run({
                "rest": {
                    "value": json.dumps(env),
                    "structure": {
                        "json": None
                    }
                },
                "timeSeries": csv_time_series
            }))

    for i in range(10):
        ps = proms[i * 50:i * 50 + 50]

        for res in capnp.join_promises(ps).wait():
            if len(res.result.value) > 0:
                print(json.loads(
                    res.result.value)["customId"])  #.result["customId"])

    return

    reslist = capnp.join_promises(proms).wait()
    #reslist.wait()

    for res in reslist:
        print(json.loads(res.result.value)["customId"])  #.result["customId"])

    #        .then(lambda res: setattr(_context.results, "result", \
    #           self.calc_yearly_tavg(res[2].startDate, res[2].endDate, res[0].header, res[1].data)))

    #result_j = monica_instance.runEnv({"jsonEnv": json.dumps(env), "timeSeries": csv_time_series}).wait().result
    #result = json.loads(result_j)

    #print("result:", result)
    """
    #req = model.run_request()
    #req.data = ts
    #result = req.send().wait().result
    tavg_ts = ts.subheader(["tavg"]).wait().timeSeries
    start_time = time.perf_counter()
    result = model.run(tavg_ts).wait().result
    end_time = time.perf_counter()
    print("rust:", result, "time:", (end_time - start_time), "s")
    """
    """
 def run(self, timeSeries, _context,
         **kwargs):  # (timeSeries :TimeSeries) -> (result :XYResult);
     #return timeSeries.header().then(lambda res: setattr(_context.results, "result", {"xs": [1,2,3], "ys": [2,3,4]}))
     return capnp.join_promises([timeSeries.header(), timeSeries.data(), timeSeries.range()]) \
         .then(lambda res: setattr(_context.results, "result", \
             self.calc_yearly_tavg(res[2].startDate, res[2].endDate, res[0].header, res[1].data)))
Example #12
0
def main():

    config = {"port": "6666", "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

    print("config:", config)

    #cmip_service = capnp.TwoPartyClient("login01.cluster.zalf.de:11001").bootstrap().cast_as(cd.Climate.Service)
    factory = capnp.TwoPartyClient("localhost:9000").bootstrap().cast_as(
        vr.RT.Factory)

    vr_ = factory.createVR(resolution=1000,
                           regionParams={
                               "bounds": {
                                   "tl": {
                                       "x": 0,
                                       "y": 0
                                   },
                                   "br": {
                                       "x": 100,
                                       "y": 100
                                   },
                               }
                           }).wait().vr

    params = vr_.params().wait()

    print(cmip_service.info().wait())

    all_dss = cmip_service.getAvailableDatasets().wait().datasets
    mpd0 = all_dss[0]
    es = mpd0.meta.entries
    ds = mpd0.data
    ts = ds.closestTimeSeriesAt({
        "latlon": {
            "lat": 50.0,
            "lon": 12.0
        }
    }).wait().timeSeries
    h = ts.header().wait().header
    data = ts.data().wait().data
    loc = ts.location().wait()

    some_dss = cmip_service.getDatasetsFor({
        "entries": [{
            "gcm": "ichecEcEarth"
        }]
    }).wait().datasets
    ds2 = some_dss[0]
    md2 = ds2.metadata().wait()
    print(md2)
    info2 = md2.info.forOne({"gcm": 0}).wait()
    print(info2)
    infos2 = md2.info.forAll().wait().all
    print(infos2)
    locs = ds2.locations().wait().locations

    proms = []
    for i in range(10):
        env["customId"] = str(i)
        proms.append(
            monica_instance.run({
                "rest": {
                    "value": json.dumps(env),
                    "structure": {
                        "json": None
                    }
                },
                "timeSeries": csv_time_series
            }))

    for i in range(10):
        ps = proms[i * 50:i * 50 + 50]

        for res in capnp.join_promises(ps).wait():
            if len(res.result.value) > 0:
                print(json.loads(
                    res.result.value)["customId"])  #.result["customId"])

    return

    reslist = capnp.join_promises(proms).wait()
    #reslist.wait()

    for res in reslist:
        print(json.loads(res.result.value)["customId"])  #.result["customId"])

    #        .then(lambda res: setattr(_context.results, "result", \
    #           self.calc_yearly_tavg(res[2].startDate, res[2].endDate, res[0].header, res[1].data)))

    #result_j = monica_instance.runEnv({"jsonEnv": json.dumps(env), "timeSeries": csv_time_series}).wait().result
    #result = json.loads(result_j)

    #print("result:", result)
    """
    #req = model.run_request()
    #req.data = ts
    #result = req.send().wait().result
    tavg_ts = ts.subheader(["tavg"]).wait().timeSeries
    start_time = time.perf_counter()
    result = model.run(tavg_ts).wait().result
    end_time = time.perf_counter()
    print("rust:", result, "time:", (end_time - start_time), "s")
    """
    """
Example #13
0
def main():

    config = {
        "port":
        "6666",
        "server":
        "localhost",
        "sim.json":
        os.path.join(os.path.dirname(__file__), '../sim-min.json'),
        "crop.json":
        os.path.join(os.path.dirname(__file__), '../crop-min.json'),
        "site.json":
        os.path.join(os.path.dirname(__file__), '../site-min.json'),
        "climate.csv":
        os.path.join(os.path.dirname(__file__), '../climate-min.csv'),
        "shared_id":
        None,
        "climate_service_address":
        "10.10.24.186:11000",
        #"climate_data_service_address": "localhost:11001",
        "climate_data_service_address":
        "login01.cluster.zalf.de:11001",
        "admin_master_address":
        "10.10.24.186:8000"
    }
    # 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:", config)

    # rust_client = capnp.TwoPartyClient("localhost:4000")
    # client = capnp.TwoPartyClient("localhost:8000")

    climate_data_service = capnp.TwoPartyClient(
        config["climate_data_service_address"]).bootstrap().cast_as(
            climate_data_capnp.ClimateData.Service)
    sims = climate_data_service.getAvailableSimulations().wait(
    ).availableSimulations
    sim1 = sims[0]
    scens = sim1.scenarios().wait().scenarios
    scen1 = scens[0]
    reals = scen1.realizations().wait().realizations
    real1 = reals[0]
    coord = geo_coord_capnp.Geo.Coord.new_message()
    coord.init("latlon")
    coord.latlon.lat = 46.51412
    coord.latlon.lon = 12.81895
    ctss = real1.closestTimeSeriesAt(coord).wait().timeSeries
    cts1 = ctss[0]
    header = cts1.header().wait().header
    datat = cts1.dataT().wait().data
    print(scen1.simulationInfo().wait().simulationInfo)
    stations = sim1.stations().wait().stations
    station1 = stations[0]
    print(station1.geoCoord().wait().geoCoord)
    allts = station1.allTimeSeries().wait().allTimeSeries
    allts1 = allts[0]
    print(allts1.simulationInfo().wait().simulationInfo)
    print(allts1.scenarioInfo().wait().scenarioInfo)
    print(allts1.realizationInfo().wait().realizationInfo)
    ds = allts1.data().wait().data
    dst = allts1.dataT().wait().data

    sim2 = sims[1]
    scen2 = sim2.scenarios().wait().scenarios[0]
    real2 = scen2.realizations().wait().realizations[0]
    cts2 = real2.closestTimeSeriesAt(coord).wait().timeSeries[0]
    cts2.simulationInfo().wait().simulationInfo
    data2 = cts2.data().wait().data

    station2 = sim2.stations().wait().stations[0]
    allts2 = station2.allTimeSeries().wait().allTimeSeries[0]
    data3 = allts2.data().wait().data

    coord.latlon.lat = 51.18323
    coord.latlon.lon = 2.84376
    ctss3 = real1.closestTimeSeriesAt(coord).wait().timeSeries[0]
    ds3 = ctss3.data().wait().data
    ctss4 = real2.closestTimeSeriesAt(coord).wait().timeSeries[0]
    ds4 = ctss4.data().wait().data

    coord.latlon.lat = 44.10437
    coord.latlon.lon = 26.55972
    ctss5 = real1.closestTimeSeriesAt(coord).wait().timeSeries[0]
    ds5 = ctss5.data().wait().data
    ctss6 = real2.closestTimeSeriesAt(coord).wait().timeSeries[0]
    ds6 = ctss6.data().wait().data

    #46.51412,12.81895
    #csv_time_series = capnp.TwoPartyClient(config["climate_service_address"]).bootstrap().cast_as(
    #    climate_data_capnp.Climate.TimeSeries)
    # monica = capnp.TwoPartyClient("localhost:9999").bootstrap().cast_as(model_capnp.Model.EnvInstance)

    master_available = False
    while not master_available:
        try:
            admin_master = capnp.TwoPartyClient(
                config["admin_master_address"]).bootstrap().cast_as(
                    cluster_admin_service_capnp.Cluster.AdminMaster)
            master_available = True
        except:
            # time.sleep(1)
            pass

    model_factories = admin_master.availableModels().wait().factories
    if len(model_factories) > 0:

        model_factory = model_factories[0]

        # get instance id
        print(model_factory.modelId().wait().id)

        if True:
            # get a single instance
            print("requesting single instance ...")
            cap_holder = model_factory.newInstance().instance
            # cap_holder = model_factory.restoreSturdyRef(
            #    "b37e2e43-8a72-4dc0-8cb1-2bdc416d8148").cap
            monica = cap_holder.cap().wait().cap.as_interface(
                model_capnp.Model.EnvInstance)
            sturdy_ref = cap_holder.save().wait().sturdyRef
            print("single instance sturdy ref:", sturdy_ref)

            proms = []
            print("running jobs on single instance ...")
            for i in range(5):
                env["customId"] = str(i)
                proms.append(
                    monica.run({
                        "rest": {
                            "value": json.dumps(env),
                            "structure": {
                                "json": None
                            }
                        },
                        "timeSeries": csv_time_series
                    }))

            print("results from single instance ...")
            for res in capnp.join_promises(proms).wait():
                if len(res.result.value) > 0:
                    # .result["customId"])
                    print(json.loads(res.result.value)["customId"])

            # cap_holder.release().wait()

        if True:
            # get multiple instances
            print("requesting multiple instances ...")
            cap_holders = model_factory.newInstances(5).instances
            restore = True
            # cap_holders = model_factory.restoreSturdyRef(
            #    "96495c8f-e241-4289-bee4-f314fef5b16d").cap
            sturdy_ref_p = cap_holders.save()
            entries_p = cap_holders.cap()
            sturdy_ref_p, entries_p = capnp.join_promises(
                [sturdy_ref_p, entries_p]).wait()
            entries = entries_p.cap

            sturdy_ref = sturdy_ref_p.sturdyRef
            print("multi instance sturdy ref:", sturdy_ref)
            monica_proms = []
            for ent in entries:
                monica_proms.append(
                    ent.entry.cap().then(lambda res: res.cap.as_interface(
                        model_capnp.Model.EnvInstance)))

            monicas = capnp.join_promises(monica_proms).wait()

            proms = []
            print("running jobs on multiple instances ...")
            for i in range(len(monicas)):
                env["customId"] = str(i)
                proms.append(monicas[i].run({
                    "rest": {
                        "value": json.dumps(env),
                        "structure": {
                            "json": None
                        }
                    },
                    "timeSeries": csv_time_series
                }))

            print("results from multiple instances ...")
            for res in capnp.join_promises(proms).wait():
                if len(res.result.value) > 0:
                    # .result["customId"])
                    print(json.loads(res.result.value)["customId"])

    return
    """
    climate_service = capnp.TwoPartyClient("localhost:8000").bootstrap().cast_as(climate_data_capnp.Climate.DataService)

    sims_prom = climate_service.simulations_request().send()
    sims = sims_prom.wait()
    
    if len(sims.simulations) > 0:
        info_prom = sims.simulations[1].info()
        info = info_prom.wait()
        print("id:", info.info.id)

        sim = sims.simulations[1]
        scens_p = sim.scenarios()
        scens = scens_p.wait()

        if len(scens.scenarios) > 0:
            scen = scens.scenarios[0]
            reals_p = scen.realizations()
            reals = reals_p.wait()

            if len(reals.realizations) > 0:
                real = reals.realizations[0]
                ts_p = real.closestTimeSeriesAt({"latlon": {"lat": 52.5, "lon": 14.1}})
                ts = ts_p.wait().timeSeries

                ts.range().then(lambda r: print(r)).wait()
                ts.realizationInfo().then(lambda r: print(r.realInfo)).wait()
                ts.scenarioInfo().then(lambda r: print(r.scenInfo)).wait()
                ts.simulationInfo().then(lambda r: print(r.simInfo)).wait()
    """

    csv_time_series = capnp.TwoPartyClient(
        "localhost:8000").bootstrap().cast_as(
            climate_data_capnp.Climate.TimeSeries)
    # header = csv_time_series.header().wait().header
    monica_instance = capnp.TwoPartyClient(
        "localhost:9999").bootstrap().cast_as(model_capnp.Model.EnvInstance)
    # monica_instance = capnp.TwoPartyClient("login01.cluster.zalf.de:9999").bootstrap().cast_as(model_capnp.Model.EnvInstance)

    proms = []
    for i in range(10):
        env["customId"] = str(i)
        proms.append(
            monica_instance.run({
                "rest": {
                    "value": json.dumps(env),
                    "structure": {
                        "json": None
                    }
                },
                "timeSeries": csv_time_series
            }))

    for i in range(10):
        ps = proms[i * 50:i * 50 + 50]

        for res in capnp.join_promises(ps).wait():
            if len(res.result.value) > 0:
                # .result["customId"])
                print(json.loads(res.result.value)["customId"])

    return

    reslist = capnp.join_promises(proms).wait()
    # reslist.wait()

    for res in reslist:
        print(json.loads(res.result.value)["customId"])  # .result["customId"])

    #        .then(lambda res: setattr(_context.results, "result", \
    #           self.calc_yearly_tavg(res[2].startDate, res[2].endDate, res[0].header, res[1].data)))

    # result_j = monica_instance.runEnv({"jsonEnv": json.dumps(env), "timeSeries": csv_time_series}).wait().result
    # result = json.loads(result_j)

    # print("result:", result)
    """
    # req = model.run_request()
    # req.data = ts
    # result = req.send().wait().result
    tavg_ts = ts.subheader(["tavg"]).wait().timeSeries
    start_time = time.perf_counter()
    result = model.run(tavg_ts).wait().result
    end_time = time.perf_counter()
    print("rust:", result, "time:", (end_time - start_time), "s")
    """
    """