Example of POST vpz/inout in R language

For the simulator ‘wwdm.vpz’ whose Id is 266, enter the R code/instructions in a R 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

    # R code :
    
    library('rjson')
    
    # content of simulation input information in 'tree' style of presentation
    # (vpzinput)
    content_simulation_inputs_tree <- function(vpzinput){
    
        # duration
        vleduration = vpzinput$vleduration
        #vleduration
        duration_value = vleduration$value
        duration_verbose_name = vleduration$verbose_name
        duration_id = vleduration$id
        cat("duration value :", duration_value, "\n")
    
        # begin
        vlebegin = vpzinput$vlebegin
        vlebegin
        begin_value = vlebegin$value
        begin_verbose_name = vlebegin$verbose_name
        begin_id = vlebegin$id
        cat("begin value :", begin_value, "\n")
    
        # conditions and parameters
        conds = vpzinput$vlecond_list
        for (cond in conds){
            #print(cond)
            cond_verbose_name = cond$verbose_name
            cond_id = cond$id
            cond_name = cond$name
            cat("\nCondition (id, name, verbose_name) : ", cond_id, cond_name, cond_verbose_name, "\n")
            cat("List of its parameters (id, cname, pname, type, selected, verbose_name, value) :", "\n")
            pars = cond$vlepar_list
            for (par in pars){
                #print(par)
                par_verbose_name = par$verbose_name
                par_id = par$id
                par_cname = par$cname
                par_pname = par$pname
                par_type = par$type
                par_value = fromJSON(par$value)
                par_selected = par$selected
                cat("- ", par_id, par_cname, par_pname, par_type, par_selected, par_verbose_name, "\n")
                print(par_value)
            }
        }
    
        # views and output datas identity
        views = vpzinput$vleview_list
        for (view in views){
            #print(view)
            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
            cat("\nView (id, name, type, timestep, output_format, output_location, output_name, output_plugin, verbose_name) : ", view_id, view_name, view_type, view_timestep, view_output_format, view_output_location, view_output_name, view_output_plugin, view_verbose_name, "\n")
            cat("List of its output datas (id, vname, oname, shortname, selected, verbose_name) :", "\n")
            outs = view$vleout_list
            for (out in outs){
                #print(out)
                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
                cat("- ", out_id, out_vname, out_oname, out_shortname, out_selected, out_verbose_name, "\n")
            }
        }
    }
    
    
    # R code :
    
    library('rjson')
    
    # content of simulation results (res) in 'tree' style of presentation,
    # according to plan values ('single', 'linear') and
    # restype values ('dataframe' or 'matrix')
    content_simulation_results_tree <- function(res, plan, restype){
    
        res = fromJSON(res)
        cat( "plan, restype :", plan, restype, "\n")
        #cat( "res :", "\n")
        #print(res)
        cat("\nDetailing the results :", "\n")
        if (restype=='dataframe' && plan=='single'){
            cat("(view name, output data name, value)", "\n")
            for (viewname in names(res)){ 
                outputs = res[[viewname]]
                for (outputname in names(outputs)){ 
                    val = outputs[[outputname]]
                    cat("\n- ", viewname, outputname, "\n")
                    print(val)
                }
            }
        } else if (restype=='dataframe' && plan=='linear'){
            for (res_a in res){
                cat("\n*** simulation :", "\n")
                cat("(view name, output data name, value)", "\n")
                for (viewname in names(res_a)){ 
                    outputs = res_a[[viewname]]
                    for (outputname in names(outputs)){ 
                        val = outputs[[outputname]]
                        cat("\n- ", viewname, outputname, "\n")
                        print(val)
                    }
                }
            }
        } else if (restype=='matrix' && plan=='single'){
            cat("(view name, value)", "\n")
            for (viewname in names(res)){ 
                v = res[[viewname]]
                cat("\n- ", viewname, "\n")
                print(v)
            }
        } else if (restype=='matrix' && plan=='linear'){
            for (res_a in res){
                cat("\n*** simulation :", "\n")
                cat("(view name, value)", "\n")
                for (viewname in names(res_a)){ 
                    v = res_a[[viewname]]
                    cat("\n- ", viewname, "\n")
                    print(v)
                }
            }
        } else {
            cat("error (unexpected)", "\n")
        }
    }
    
    
    # R code :
    
    library('RCurl')
    library('rjson')
    
    #######################
    # request and response
    #######################
    
    header = c('Content-Type'='application/json', Accept='application/json')
    
    options(RCurlOptions=list(followlocation=TRUE))
    
    postfields = toJSON(list(
        vpz=266,
        duration=6,
        begin=2453982.0,
        cond_wwdm.A=0.0064,
        cond_wwdm.Eb=1.86,
        mode=c("tree", "single", "dataframe"),
        parselect=c("cond_wwdm.A","cond_wwdm.B","cond_wwdm.Eb","cond_wwdm.TI"),
        outselect=c("view.top:wwdm.LAI", "view.top:wwdm.ST"),
        format="json"))
    
    res = postForm(uri="http://erecord.toulouse.inra.fr:8000/vpz/inout/",
        .opts=list(postfields=postfields, httpheader=header))
    
    responsedata = fromJSON(res)
    #responsedata
    
    #######################################################
    # responsedata in case of 'tree' style of presentation 
    #######################################################
    
    # id as VpzAct
    id = responsedata$id
    cat("id :", id, "\n")
    
    verbose_name = responsedata$verbose_name
    
    #----------------------
    # input information of simulation
    
    vpzinput = responsedata$vpzinput
    
    # id as VpzInput
    id = vpzinput$id
    
    content_simulation_inputs_tree(vpzinput=vpzinput)
    
    #----------------------
    # output information of simulation
    
    vpzoutput = responsedata$vpzoutput
    
    # id as VpzOutput
    id = vpzoutput$id
    
    content_simulation_results_tree( res=vpzoutput$res,
                                     plan=vpzoutput$plan,
                                     restype=vpzoutput$restype)
    
    
    #----------------------
    # others
    
    vpzorigin = responsedata$vpzorigin
    
    pkgname = responsedata$pkgname
    
    vlepath = responsedata$vlepath
    
    vpzname = responsedata$vpzname
    
    vpzworkspace = responsedata$vpzworkspace