Example of POST vpz/output in Python language

For the simulator ‘wwdm.vpz’ whose Id is 266, enter the Python code/instructions in a Python interpreter.

Example illustrating :

  • modifying begin,

  • modifying duration,

  • modifying some parameters with ‘pars’,

  • tree’ as style of presentation,

  • json’ as format,

  • value ‘single’ as plan, or value ‘linear’ in case of multiple simulation

  • value ‘dataframe’ as restype (or ‘matrix’)

  • value ‘all’ as ‘outselect’,

  • with ‘application/json’ as ‘Content-Type’

    Memo

    # -*- coding: utf-8 -*-
    """erecord_cmn.utils.using.send_post_and_receive
    
    Methods that may be used by a user calling the erecord web services from python
    
    """
    
    import pycurl
    import io
    import json
    
    def send_post_and_receive(url, inputdata):
        """Send POST request and return response datas"""
    #
        buffer = io.BytesIO()
        c = pycurl.Curl()
        c.setopt(c.POST, 1)
        c.setopt(c.HTTPHEADER, ['Content-Type: application/json'])
        c.setopt(c.URL, url)
        #c.setopt(c.FOLLOWLOCATION, True)
        json_inputdata = json.dumps( inputdata)
        c.setopt(c.POSTFIELDS, json_inputdata)
        c.setopt(c.WRITEFUNCTION, buffer.write)
        c.perform()
        buffer_str = buffer.getvalue()
        buffer.close()
        buffer_str = buffer_str.decode("utf8")
        responsedata = json.loads(buffer_str)
        return responsedata
    #
    
    
    # -*- coding: utf-8 -*-
    """erecord_cmn.utils.using.content_simulation_results_tree
    
    Methods that may be used by a user calling the erecord web services from python
    
    """
    
    from __future__ import print_function # python 2.7 version case
    
    def content_simulation_results_tree(res, plan, restype):
        """Content of simulation results (res) in 'tree' style of presentation,
           according to plan values ('single', 'linear') and
           restype values ('dataframe' or 'matrix')
        """
    #
        res = json.loads(res)
        print("plan, restype :", plan, restype)
        print("res :", res)
        print("\nDetailing the results :")
        if restype=='dataframe' and plan=='single' :
            print("(view name, output data name, value)")
            for viewname,outputs in res.items() :
                for outputname,val in outputs.items() :
                    print("- ", viewname, outputname, val)
        elif restype=='dataframe' and plan=='linear' :
            for (a,res_a) in enumerate(res) :
                print("*** simulation number ", a, ":")
                print("(view name, output data name, value)")
                for viewname,outputs in res_a.items() :
                    for outputname,val in outputs.items() :
                        print("- ", viewname, outputname, val)
        elif restype=='matrix' and plan=='single' :
            print("(view name, value)")
            for viewname,v in res.items() :
                print("- ", viewname, v)
        elif restype=='matrix' and plan=='linear' :
            for (a,res_a) in enumerate(res) :
                print("*** simulation number ", a, ":")
                print("(view name, value)")
                for viewname,v in res_a.items() :
                    print("- ", viewname, v)
        else : # error (unexpected)
            pass
    #
    
    
    # Python code :
    
    #######################
    # request and response
    #######################
    
    inputdata = {"vpz":266, "format":"json"}
    inputdata["duration"] = 6
    inputdata["begin"] = 2453982.0
    
    #----------------------
    # parameters and mode in case of single plan (single simulation)
    
    # some parameters modification with 'pars'
    parameter_A = {"selection_name":"cond_wwdm.A",
                   "cname":"cond_wwdm","pname":"A",
                   "value":0.0064}
    parameter_Eb = {"selection_name":"cond_wwdm.Eb",
                    "cname":"cond_wwdm","pname":"Eb",
                    "value":1.86}
    pars = list()
    pars.append(parameter_A)
    pars.append(parameter_Eb)
    inputdata["pars"] = pars
    
    inputdata["mode"]      = ["tree", "single", "dataframe"]
    # or inputdata["mode"] = ["tree", "single", "matrix"]
    
    #----------------------
    # parameters and mode in case of linear plan (multiple simulation)
    
    # some parameters modification with 'pars' (multiple values)
    parameter_A = {"selection_name":"cond_wwdm.A",
                   "cname":"cond_wwdm","pname":"A",
                   "value":"[0.0064,0.0065,0.0066]"}
    parameter_Eb = {"selection_name":"cond_wwdm.Eb",
                    "cname":"cond_wwdm","pname":"Eb",
                    "value":"[1.84,1.85,1.86]"}
    pars = list()
    pars.append(parameter_A)
    pars.append(parameter_Eb)
    inputdata["pars"] = pars
    
    inputdata["mode"] = ["tree", "linear", "dataframe"]
    # or inputdata["mode"] = ["tree", "linear", "matrix"]
    
    #----------------------
    
    inputdata["outselect"] = "all"
    
    responsedata = send_post_and_receive(
        url="http://erecord.toulouse.inra.fr:8000/vpz/output/",
        inputdata=inputdata)
    
    responsedata
    keys = responsedata.keys()
    keys
    
    #######################################################
    # responsedata in case of 'tree' style of presentation
    #######################################################
    
    # id as VpzOutput
    if "id" in keys :
        id = responsedata['id']
    
    if 'res' in keys and 'plan' in keys and 'restype' in keys : 
        content_simulation_results_tree( res=responsedata['res'],
                                         plan=responsedata['plan'],
                                         restype=responsedata['restype'])
    
    pass