Example of POST vpz/inout 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 by ‘cname.pname’,

  • tree’ as style of presentation,

  • json’ as format,

  • values of type cname.pname as ‘parselect’ :

    value ‘cond_wwdm.A’ to select the parameter named ‘A’ of the condition named ‘cond_wwdm’.

    value ‘cond_wwdm.B’ to select the parameter named ‘B’ of the condition named ‘cond_wwdm’.

    value ‘cond_wwdm.Eb’ to select the parameter named ‘Eb’ of the condition named ‘cond_wwdm’.

    value ‘cond_wwdm.TI’ to select the parameter named ‘TI’ of the condition named ‘cond_wwdm’.

  • value ‘single’ as plan

  • value ‘dataframe’ as restype

  • value of type vname.oname as ‘outselect’ :

    value ‘view.top:wwdm.LAI’ to select the ‘LAI’ output data of the view named ‘view’.

    value ‘view.top:wwdm.ST’ to select the ‘ST’ output data of the view named ‘view’.

  • 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_inputs_tree
    
    Methods that may be used by a user calling the erecord web services from python
    
    """
    
    import json
    from __future__ import print_function # python 2.7 version case
    
    def content_simulation_inputs_tree(vpzinput):
        """Content of simulation input information in 'tree' style of presentation
           (vpzinput)
        """
    #
        keys = vpzinput.keys()
        if "vleduration" in keys : # duration
            vleduration = vpzinput['vleduration']
            duration_value = vleduration['value']
            #duration_verbose_name = vleduration['verbose_name']
            #duration_id = vleduration['id']
            #vleduration
            print("duration value : ", duration_value)
        if "vlebegin" in keys : # begin
            vlebegin = vpzinput['vlebegin']
            begin_value = vlebegin['value']
            #begin_verbose_name = vlebegin['verbose_name']
            #begin_id = vlebegin['id']
            #vlebegin
            print("begin value : ", begin_value)
        if "vlecond_list" in keys : # conditions and parameters
            conds = vpzinput['vlecond_list']
            for cond in conds :
                #cond.keys()
                cond_verbose_name = cond['verbose_name']
                cond_id = cond['id']
                cond_name = cond['name']
                print("\nCondition name ", cond_name)
                #print(cond_id, cond_name, cond_verbose_name)
                print("List of its parameters (id, cname, pname, type, value, selected, verbose_name) :")
                pars = cond['vlepar_list']
                for par in pars :
                    #par.keys()
                    par_verbose_name = par['verbose_name']
                    par_id = par['id']
                    par_cname = par['cname']
                    par_pname = par['pname']
                    par_type = par['type']
                    par_value = json.loads(par['value'])
                    par_selected = par['selected']
                    print("- ", par_id, par_cname, par_pname, par_type, par_value, par_selected, par_verbose_name)
                    #type(par_value)
        if "vleview_list" in keys : # views and output datas identity
            views = vpzinput['vleview_list']
            for view in views :
                #view.keys()
                view_verbose_name = view['verbose_name']
                view_id = view['id']
                view_name = view['name']
                view_type = view['type']
                view_timestep = view['timestep']
                view_output_format = view['output_format']
                view_output_location = view['output_location']
                view_output_name = view['output_name']
                view_output_plugin = view['output_plugin']
                print("\nView name ", view_name)
                #print(view_id, view_name, view_type, view_timestep, view_output_format, view_output_location, view_output_name, view_output_plugin, view_verbose_name)
                print("List of its output datas (id, vname, oname, shortname, selected, verbose_name) :")
                outs = view['vleout_list']
                for out in outs :
                    #out.keys()
                    out_verbose_name = out['verbose_name']
                    out_id = out['id']
                    out_vname = out['vname']
                    out_oname = out['oname']
                    out_shortname = out['shortname']
                    out_selected = out['selected']
                    print("- ", out_id, out_vname, out_oname, out_shortname, out_selected, out_verbose_name)
    #
    
    
    # -*- 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
    
    inputdata["mode"] = ["tree", "single", "dataframe"]
    
    # some parameters modification with 'cname.pname'
    inputdata["cond_wwdm.A"] = 0.0064
    inputdata["cond_wwdm.Eb"] = 1.86
    
    inputdata["parselect"] = ["cond_wwdm.A", "cond_wwdm.B", "cond_wwdm.Eb",
                              "cond_wwdm.TI"]
    
    inputdata["outselect"] = ["view.top:wwdm.LAI", "view.top:wwdm.ST"]
    
    responsedata = send_post_and_receive(
        url="http://erecord.toulouse.inra.fr:8000/vpz/inout/",
        inputdata=inputdata)
    
    responsedata
    keys = responsedata.keys()
    keys
    
    #######################################################
    # responsedata in case of 'tree' style of presentation 
    #######################################################
    
    # id as VpzAct
    if "id" in keys :
        id = responsedata['id']
    
    if "verbose_name" in keys :
        verbose_name = responsedata['verbose_name']
    
    #----------------------
    # input information of simulation
    
    if "vpzinput" in keys :
        vpzinput = responsedata['vpzinput']
        vpzinput_keys = vpzinput.keys()
        #
        # id as VpzInput
        if "id" in vpzinput_keys :
            id = vpzinput['id']
        #
        content_simulation_inputs_tree(vpzinput=vpzinput)
    
    pass
    
    #----------------------
    # output information of simulation
    
    if "vpzoutput" in keys :
        vpzoutput = responsedata['vpzoutput']
        vpzoutput_keys = vpzoutput.keys()
        #
        # id as VpzOutput
        if "id" in vpzoutput_keys :
            id = vpzoutput['id']
        #
        if 'res' in vpzoutput_keys and 'plan' in vpzoutput_keys and 'restype' in vpzoutput_keys : 
            content_simulation_results_tree(res=vpzoutput['res'],
                                            plan=vpzoutput['plan'],
                                            restype=vpzoutput['restype'])
    
    pass
    
    #----------------------
    # others
    
    if "vpzorigin" in keys :
        vpzorigin = responsedata['vpzorigin']
    
    if "pkgname" in keys :
        pkgname = responsedata['pkgname']
    
    if "vlepath" in keys :
        vlepath = responsedata['vlepath']
    
    if "vpzname" in keys :
        vpzname = responsedata['vpzname']
    
    if "vpzworkspace" in keys :
        vpzworkspace = responsedata['vpzworkspace']