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 by ‘cname.pname’,

  • compact’ 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 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_results_compact
    
    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_compact(res, plan, restype):
        """Content of simulation results (res) in 'compact' 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("(Output data name (ie selection_name) and value)")
            for compact_outputname,val in res.items() :
                print("- ", compact_outputname, val)
        elif restype=='dataframe' and plan=='linear' :
            for (a,res_a) in enumerate(res) :
                print("*** simulation number ", a, ":")
                print("(Output data name (ie selection_name) and value)")
                for compact_outputname,val in res_a.items() :
                    print("- ", compact_outputname, val)
        elif restype=='matrix' and plan=='single' :
            print("(View name (ie selection_name) and 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 (ie selection_name) and 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 'cname.pname'
    inputdata["cond_wwdm.A"] = 0.0064
    inputdata["cond_wwdm.Eb"] = 1.86
    
    inputdata["mode"]      = ["compact", "single", "dataframe"]
    # or inputdata["mode"] = ["compact", "single", "matrix"]
    
    #----------------------
    # parameters and mode in case of linear plan (multiple simulation)
    
    # some parameters modification with 'cname.pname' (multiple values)
    inputdata["cond_wwdm.A"] = [0.0064,0.0065,0.0066]
    inputdata["cond_wwdm.Eb"] = [1.84,1.85,1.86]
    
    inputdata["mode"] = ["compact", "linear", "dataframe"]
    # or inputdata["mode"] = ["compact", "linear", "matrix"]
    
    #----------------------
    
    inputdata["outselect"] = ["view.top:wwdm.LAI", "view.top:wwdm.ST"]
    
    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 'compact' style of presentation
    ##########################################################
    
    if 'res' in keys and 'plan' in keys and 'restype' in keys :
        content_simulation_results_compact( res=responsedata['res'],
                                            plan=responsedata['plan'],
                                            restype=responsedata['restype'])
    
    pass