def test_plot_WC_layers(self):

        csvFileName = "depth0_ref35_moving_15_day.csv"
        configHolder = ConfigHolder()
        config = configHolder.get()
        prefixOutput = config['path_prefix']['output']

        path = str(Path(prefixOutput + csvFileName).resolve())
        myplot.plot_WC_layers(path)
示例#2
0
from os import listdir
from os.path import isfile, join, basename
from pathlib import Path
from Configuration import ConfigHolder
import linecache

if __name__ == "__main__":
    config = ConfigHolder().get()

    targetFolder = "ref_moving"
    # list target folder to get all file  then  generate a list  is .csv and
    # log file
    targetPathStr = config['path_prefix']['output'] + "/" + targetFolder
    outputPath = Path(targetPathStr).resolve()
    logCSVList = [
        f for f in listdir(outputPath)
        if isfile(join(outputPath, f)) and f.split(".")[1] == "csv"
        and f.split(".")[0].split("_")[-1] == "log"
    ]
    # print(logCSVList)

    resultList = {}
    #  loop iterate all list to read yield and water
    for f in logCSVList:

        filePath = Path(targetPathStr + '/' + f).resolve()
        with open(filePath, "r") as fp:
            lines = fp.readlines()
            nameSeg = f.split("_")
            ref = nameSeg[0]
            mp = nameSeg[1]
示例#3
0
from Algorithm.AlgorithmLog import AlgorithmLog
from Simulator.EnvSimulator import EnvSimulator
from Configuration import ConfigHolder
import helper as helper
import numpy as np
import random
import unittest
import os
from pathlib import Path
from Controller.Controller import SimpleController

cfgHolder = ConfigHolder()
config = cfgHolder.get()

prefixOutput = config['path_prefix']['output']


class AlgorithmLogTest(unittest.TestCase):

    def intiSimulation(self):

        COMMON_VARIABLES = {
            'PROJECT': 'TOMATO2'
        }
        envSimu = EnvSimulator(COMMON_VARIABLES['PROJECT'])
        envSimu.initRun()
        return envSimu

    def initController(self):
        depth = 0
        ref = 20
示例#4
0
def run_simulation(dynamicRef, mp, simulationName):

    project = 'TOMATO2'
    envSimu = EnvSimulator(project)
    envSimu.initRun()

    cfgHolder = ConfigHolder()
    config = cfgHolder.get()

    #
    # Initialize Controller
    #
    simpleCtl = initilzieController(config, dynamicRef)
    refsArray = simpleCtl.get_ref()
    sensor0 = simpleCtl.get_controDepth()
    dynamicRef = refsArray[sensor0]
    #
    # initilize algorithm log
    #
    al = AlgorithmLog()

    #
    # simulation main loop the whole season
    #
    while not envSimu.isFinish():

        print("\nDay Count: {}".format(envSimu.currentDayCount))

        envSimu.loadResult()

        # print("WCs:")
        # print(envSimu.getCurrentDayWCs())
        WCs = envSimu.getCurrentDayWCs()

        ##
        #    Methodology Part:
        #    If moisture condition triggers irrigation event, controller to generate irrigation amount
        ###

        ##
        # Find floating EWC( Efficient Wetting Zone )
        ##
        halfRootDepth = envSimu.getCurrentRootDepth() * mp
        compartment_boundary_list = config['compartment_boundary_list']
        closestCompartment = helper.calEWZbyCompartment(
            compartment_boundary_list, halfRootDepth)
        sensor0 = int(closestCompartment * 10 - 1)

        # print("moving sensor0: {}".format(sensor0))
        simpleCtl.set_controlDepth(sensor0)

        ##
        # *self-adapt with sensor0 and sensor1
        ##
        sensor1 = 0
        #
        # update ref0
        #
        # if WCs[sensor1] > 20 and refsArray[sensor0] > 28:
        #     dynamicRef = dynamicRef - 1
        refsArray[sensor0] = dynamicRef
        simpleCtl.set_ref(refsArray)
        # Display adaptive dynamicRef of sensor0
        print("sensor0 Ref: {}".format(dynamicRef))

        # # notate current sensor0 and sensor1 in soil layer
        # print("WCs:")
        # strWCs = list(map(str, envSimu.getCurrentDayWCs()))
        # strWCs[sensor0] = "*" + strWCs[sensor0]
        # # strWCs[sensor1] = "$" + strWCs[sensor1]
        # print(strWCs)

        # print("Refs:")
        # strRefsArray = list(map(str, simpleCtl.get_ref()))
        # strRefsArray[sensor0] = "*" + strRefsArray[sensor0]
        # # strRefsArray[sensor1] = "$" + strRefsArray[sensor1]
        # print(strRefsArray)

        # print("Error:")
        # strErrors = list(map(str, simpleCtl.get_error(WCs)))
        # strErrors[sensor0] = "*" + strErrors[sensor0]
        # # strErrors[sensor1] = "$" + strErrors[sensor1]
        # print(strErrors)

        ##
        # -- ET-base Irrigation --
        ##
        # ET_k = 1.15
        # predictedIrri = helper.ETbasedIrrigation(ET, ET_k)

        ##
        # -- MI-controller --: shallow point = 0.05m, 0.15m, 0.25m(wc1, wc2, wc3)
        ##
        # print("error:")
        # print(simpleCtl.get_error(WCs))
        predictedIrri = simpleCtl.get_output(WCs)
        controllerStatus = simpleCtl.get_status(WCs)

        logIrri = 0
        if predictedIrri > 0:
            logIrri = predictedIrri
            # irri Day is  currentDatCount add one due to we use
            # today's condition to decide tomorrow's irrigation amount
            irriDay = envSimu.currentDayCount + 1
            # print("IrriDay: {}".format(irriDay))
            # print("To Irrigate: {}".format(predictedIrri))
            helper.appendDotIRR('Example.Irr', irriDay, predictedIrri)
            envSimu.runOnce()

        logDict = {"dayCount": envSimu.currentDayCount + 1,
                   "WCs": envSimu.getCurrentDayWCs(),
                   "refs": simpleCtl.get_ref(),
                   "error": simpleCtl.get_error(WCs),
                   "pidk": simpleCtl.get_k(),
                   "sensor0_depth": sensor0,
                   "sensor1_depth": sensor1,
                   "irri": logIrri,
                   "CC": envSimu.getCurrentDayData()[29],
                   "Biomass": envSimu.getCurrentDayData()[36]}

        al.log(logDict)
        envSimu.increateTimeStep()

    # End the while loop

    # write out the day and season .csv file
    fileName = simulationName
    envSimu.writeOutResult(project, fileName)

    # Write out algorithm log file to csv
    seasonDataPath = r"D:\yk_research\AquaCrop-Irrigation-Design\output\{}_season.csv".format(
        fileName)
    seasonData = helper.loadSeasonCSV(seasonDataPath)

    algInfo = {'sensor0': 'sensor0', 'sensor1': 'sensor1',
               'ref0': 'ref0', 'ref1': 'ref1'}
    summary = {"yield": seasonData['Yield']
               [-1], "water": seasonData['Irri'][-1]}
    al.writeLogToCSV(algInfo, summary, fileName)