def writeYamlFile(folderName, inputType, unit, approachName, houseLayout, houseNumber):
    '''
    This method used to prepare and output files which could be used in SmartGridToolBox yaml input file.
    The content includes: 1) houseLayout 2) load txt files 3) appliance file
        str, str, int, str, [[int], [int], ...], int, float => file
    '''
    
    # first of all, write the load txt
    print 'START read request type file'
    RequestTypeDict = CsvToLoad.readExperiment(folderName)
    
    for RequestType in RequestTypeDict.values():
        load_list = CsvToLoad.extractApplianceProfile(RequestType, unit)
        CsvToLoad.generateLoadTxt(load_list, RequestType.request_name, folderName, unit)
    
    # then get the total start time list
    print 'START read requests file'
    Result = ReadCSV(folderName, inputType) # read requests information from input csv file
    
    applienceMap = yamlCalcMethods.calcRandStartTime(Result, unit, approachName, houseNumber) # get observed load
    
    file_name = ''.join(('CSVdata/', folderName, '/' 'resident_', folderName, '_', str(houseNumber), '.yaml'))

    writeFile = open(file_name, 'w')
    
    yamlWriteMethods.writeConstParameters(writeFile)
    
    yamlWriteMethods.writeHouseLayout(writeFile, houseLayout) # write house layout
    
    yamlWriteMethods.writeVariableList(writeFile, houseLayout) # write variable list
    
    yamlWriteMethods.writeConstSimulation(writeFile) 
    
    yamlWriteMethods.writeConstNetwork(writeFile) 
    
    yamlWriteMethods.writeConstHeartbeat(writeFile) 
    
    yamlWriteMethods.writeTimeSerialLoop(writeFile, applienceMap, folderName) # write load / time_series content
    
    yamlWriteMethods.writeConstBus(writeFile)
    
    yamlWriteMethods.writeConstGenericgen(writeFile)
    
    yamlWriteMethods.writeConstLoop(writeFile)
    
    applianceCount = 0
        
    yamlWriteMethods.writeAllAppliance(houseLayout, writeFile, applienceMap, applianceCount)
    
    writeFile.close()
def writeYamlFileWithLargeNumOfHouses(folderName, inputType, unit, approachName, houseNumber):
    """
    This method used to create a yaml file with the large number of houses. In order to save code time, several
    methods will import from 'yamlCalcMethods' and 'yamlWriteMethods'. Consider of the tiny different requirement,
    it is better to write a new python to construct yaml file.
        str, str, int, str, int, float => file
    """

    print "START read request type file"
    RequestTypeDict = CsvToLoad.readExperiment(folderName)

    for RequestType in RequestTypeDict.values():
        load_list = CsvToLoad.extractApplianceProfile(RequestType, unit)
        CsvToLoad.generateLoadTxt(load_list, RequestType.request_name, folderName, unit)

    print "START read requests file"
    Result = ReadCSV(folderName, inputType)  # read requests information from input csv file
    requests = list(Result[0])

    houseLayout = yamlCalcMethods.getHouseLayout(houseNumber, requests)

    requestNameMinMap = yamlCalcMethods.getRequestNameMinMap(requests)  # get the requestNameMinMap

    ideal_shiftable_load = modify_ideal_load(Result[1][-1], unit)  # get ideal shift-able load
    signal_load = list(ideal_shiftable_load)  # make ideal shift-able load equal to signal load

    requests = updateProfile(requests, unit)  # update profile
    requests = getProbDistribution(requests, signal_load, unit, approachName)  # update probabilities

    fileCount = 0
    totalRequestNumber = getTotalRequestNumber(requests)
    restRequestNumber = int(totalRequestNumber)

    while restRequestNumber != 0:

        if restRequestNumber < houseNumber:
            repeatNumber = restRequestNumber

        else:
            repeatNumber = houseNumber

        fileName = "".join(("CSVdata/", folderName, "/resident/" "resident_", folderName, "_", str(fileCount), ".yaml"))

        applianceMap = getApplianceMap(repeatNumber, requests, unit)  # get the start time and request name dict

        requests = setNewRequests(requests)  # delete the request which quantity is zero

        writeFile = open(fileName, "w")

        yamlWriteMethods.writeConstParameters(writeFile)

        yamlWriteMethods.writeHouseLayout(writeFile, houseLayout)

        yamlWriteMethods.writeVariableList(writeFile, houseLayout)

        yamlWriteMethods.writeConstSimulation(writeFile)

        yamlWriteMethods.writeConstNetwork(writeFile)

        yamlWriteMethods.writeConstHeartbeat(writeFile)

        yamlWriteMethods.writeTimeSerialLoop(writeFile, applianceMap, folderName)  # write load / time_series content

        yamlWriteMethods.writeConstBus(writeFile)

        yamlWriteMethods.writeConstGenericgen(writeFile)

        yamlWriteMethods.writeConstLoop(writeFile)

        applianceCount = 0

        yamlWriteMethods.writeAllAppliance(houseLayout, writeFile, applianceMap, applianceCount, requestNameMinMap)

        writeFile.close()

        print "No.",
        print fileCount,
        print "yaml file had constructed"

        fileCount = fileCount + 1
        restRequestNumber = restRequestNumber - repeatNumber

    print "CONSTRUCT ",
    print fileCount,
    print "yaml files"
def writeYamlFileAndReturnRandomProbabilities(Result, houseNumber, requests, unit, folderName, approachName, houseLayout, requestNameMinMap, S_ZipList, lengthMvList):
    '''
    This method used to get the random RLC result list, and request start time.
        Result, int, [request, ...], int, str, str, [int, int,..], {str:int,...} => files, [[int, int,...]]
    '''
    
    # deal with the random output, and start time at same time
    fileCount = 0
    totalRequestNumber = yamlLargeHouseNumber.getTotalRequestNumber(requests)
    restRequestNumber = int(totalRequestNumber)
    
    # tempList = folderName.split('_');
    # subFolder = ''.join((tempList[0][-1], '_', tempList[1][0]));
    
    while restRequestNumber != 0:
        
        if restRequestNumber < houseNumber:
            repeatNumber = restRequestNumber
            
        else:
            repeatNumber = houseNumber
        
        # fileName = ''.join(('~/project/SmartGridToolbox/examples/ResidentialA/resident/', subFolder, '/', folderName, '_', str(fileCount), '.yaml'))
        fileName = ''.join(('CSVdata/', folderName, '/resident/' 'resident_', folderName, '_', str(fileCount), '.yaml'))
        
        applianceMap = getApplianceMap(repeatNumber, requests, unit) # get the start time and request name dict
        
        
        # tempNumber = 0
        # for element in applianceMap.keys():
        #    tempNumber = tempNumber + len(applianceMap[element])
        
        # if tempNumber > houseNumber:
            # for key in applianceMap.keys():
                # print key,
                # print len(applianceMap[key])
        

        # tempCount = 0
        
        # for element in applianceMap.keys():    
            # if len(applianceMap[element]) != 0:
                # tempCount = tempCount + 1
                
        
        # if tempCount > 1:
            # print 'FILE NAME IS',
            # print fileCount
            # for element in applianceMap.keys():
            
                # if len(applianceMap[element]) != 0:
    
                    # print element,
                    # print len(applianceMap[element])
        
        deleteRequests = setNewRequests(requests) # delete the request which quantity is zero
        
        writeFile = open(fileName, 'w')
        
        yamlWriteMethods.writeConstParameters(writeFile)
        
        yamlWriteMethods.writeHouseLayout(writeFile, houseLayout)
        
        yamlWriteMethods.writeVariableList(writeFile, houseLayout, S_ZipList, lengthMvList)
        
        yamlWriteMethods.writeConstSimulation(writeFile) 
        
        yamlWriteMethods.writeConstNetwork(writeFile) 
    
        yamlWriteMethods.writeConstHeartbeat(writeFile) 
    
        yamlWriteMethods.writeTimeSerialLoop(writeFile, applianceMap, folderName) # write load / time_series content
    
        yamlWriteMethods.writeConstBus(writeFile)
    
        yamlWriteMethods.writeConstGenericgen(writeFile)
    
        yamlWriteMethods.writeConstLoop(writeFile)
    
        applianceCount = 0
        
        yamlWriteMethods.writeAllAppliance(houseLayout, writeFile, applianceMap, applianceCount, requestNameMinMap)
    
        writeFile.close()
        
        print 'No.',
        print fileCount,
        print 'yaml file had constructed'
        
        fileCount = fileCount + 1
        
        # after constructed request number, reduce the rest request number
        restRequestNumber = restRequestNumber - repeatNumber
    
    deleteRequests = setTempProbabilitiesToProbabilities(deleteRequests)

    ## get the RLC random result
    random_observed_load = calculateExpectedLoad(requests, unit) # get observed load
    
    ## get output relative parameters
    random_observed_loads = [random_observed_load] 
    
    return random_observed_loads
def writeYamlFileAndReturnRandomProbabilitiesOneRequest(Result, houseNumber, requests, unit, folderName, approachName, houseLayout, requestNameMinMap, S_ZipList, lengthMvList):
    '''
    This method used to constructed yaml file and return load information, and each yaml file only had one kind of request.
        
    '''
    
    fileCount = 0
    totalRequestNumber = yamlLargeHouseNumber.getTotalRequestNumber(requests)
    restRequestNumber = int(totalRequestNumber)
    createdRequestNum = 0
    
    while restRequestNumber != 0:
        
        if restRequestNumber < houseNumber:
            repeatNumber = restRequestNumber
        else:
            repeatNumber = houseNumber
            
        fileName = ''.join(('CSVdata/', folderName, '/resident/' 'resident_', folderName, '_', str(fileCount), '.yaml'))
        
        tempResult = getApplianceMapOneRequest(repeatNumber, requests, unit) # get the start time and request name dict
        
        applianceMap = tempResult[0]
        usedNumber = int(tempResult[1])
        
        
        writeFile = open(fileName, 'w')
        
        yamlWriteMethods.writeConstParameters(writeFile)
        
        yamlWriteMethods.writeHouseLayout(writeFile, houseLayout)
        
        yamlWriteMethods.writeVariableList(writeFile, houseLayout, S_ZipList, lengthMvList)
        
        yamlWriteMethods.writeConstSimulation(writeFile) 
        
        yamlWriteMethods.writeConstNetwork(writeFile) 
    
        yamlWriteMethods.writeConstHeartbeat(writeFile) 
    
        yamlWriteMethods.writeTimeSerialLoop(writeFile, applianceMap, folderName) # write load / time_series content
    
        yamlWriteMethods.writeConstBus(writeFile)
    
        yamlWriteMethods.writeConstGenericgen(writeFile)
    
        yamlWriteMethods.writeConstLoop(writeFile)
    
        applianceCount = 0
        
        yamlWriteMethods.writeAllAppliance(houseLayout, writeFile, applianceMap, applianceCount, requestNameMinMap)
    
        writeFile.close()
        
        
        print 'FileCount is:',
        print fileCount
        for key in applianceMap.keys():
            print key,
            print len(applianceMap[key])
        
        deleteRequests = RLCSimulate.setNewRequests(requests) # delete the request which quantity is zero
        
        fileCount = fileCount + 1
        restRequestNumber = restRequestNumber - usedNumber
        createdRequestNum = createdRequestNum + usedNumber
    
    print 'created request number is: ',
    print createdRequestNum
    
    deleteRequests = RLCSimulate.setTempProbabilitiesToProbabilities(deleteRequests)
    
    ## get the RLC random result
    random_observed_load = RLCSimulate.calculateExpectedLoad(requests, unit) # get observed load
    
    ## get output relative parameters
    random_observed_loads = [random_observed_load] 
    
    return random_observed_loads