Esempio n. 1
0
def CombineWriteGridFiles(rootPath,numSection,fileTag='*.asc',compress=True,delete=True):
    """
    Combine and write a series of MultiGPU ouput asc files as gz file (default) or asc file
    fileTag is a string end with '.asc' or a list of asc file names
    example:
        CombineWriteGridFiles(rootPath,numSec,'*.asc') combine and write all asc files in gz format
        CombineWriteGridFiles(rootPath,numSec,['h_0.asc','h_3600_max.asc']) combine and write some asc files
        CombineWriteGridFiles(rootPath,numSec,'*.asc',compress=False) combine and write all asc files in asc format
    """
    if rootPath[-1]!='/':
        rootPath = rootPath+'/'
    
    os.makedirs(rootPath+'output',exist_ok=True)
    
    if isinstance(fileTag,list):
        filesToCombine = fileTag
    else:
        os.chdir(rootPath+str(numSection-1)+'/output')
        filesToCombine = glob.glob(fileTag)
        os.chdir(rootPath)
        
    for ascFile in filesToCombine:
        if ascFile.endswith('.asc'):
            grid,head,_ = CombineGridFile(rootPath,numSection,ascFile,delete=delete)
            writeFileName = rootPath+'output/MG_'+ascFile
            if compress:
                ArcgridwriteGZip(writeFileName,grid,head)
                writeFileName=writeFileName+'.gz'
            else:
                arcgridwrite(writeFileName,grid,head)
            print(writeFileName+' is created')
    return writeFileName
Esempio n. 2
0
def GenerateOneGridFile(fileName,
                        rootPath,
                        numSection,
                        compress=True,
                        delete=True):
    """
    Combine and write a series of MultiGPU ouput asc files as gz file (default) or asc file
    filaName: a string end with '.asc', no file path
    example:
        CombineWriteGridFiles(rootPath,numSec,'h_129600.asc') combine and write h_129600.asc in gz format
        CombineWriteGridFiles(rootPath,numSec,'*.asc',compress=False) combine and write all asc files in asc format
    """
    if rootPath[-1] != '/':
        rootPath = rootPath + '/'

    os.makedirs(rootPath + 'output', exist_ok=True)

    ascFile = fileName
    if ascFile.endswith('.asc'):
        grid, head, _ = CombineGridFile(rootPath,
                                        numSection,
                                        ascFile,
                                        delete=delete)
        writeFileName = rootPath + 'output/MG_' + ascFile
        if compress:
            ArcgridwriteGZip(writeFileName, grid, head)
            writeFileName = writeFileName + '.gz'
        else:
            arcgridwrite(writeFileName, grid, head)
        print(writeFileName + ' is created')
    return None
def WriteSecDEM(sectionPathList, demMat, demHeaderList, sectionRowInd):
    # write DEM for each section
    start = time.perf_counter()
    numSection = sectionRowInd.shape[0]
    fileTail = 'DEM.txt'
    for i in range(numSection):
        n = numSection - (i + 1)
        Z_sec = demMat[sectionRowInd[i, 0]:sectionRowInd[i, 1] + 1, :]  #DEM
        subDemHead = demHeaderList[i]
        fileName = sectionPathList[n]['mesh'] + fileTail
        arcgridwrite(fileName, Z_sec, subDemHead)
    #print(fileTail+' is created')
    end = time.perf_counter()
    timeElapse = end - start
    return timeElapse
Esempio n. 4
0
def InputSetup(folderName,
               demMat,
               demHead,
               boundList=[],
               fileToCreate='all',
               h0=0,
               hU0=[0, 0],
               manning=0.035,
               rain_mask=0,
               rain_source=np.array([[0, 0], [60, 0]]),
               sewer_sink=0,
               cumulative_depth=0,
               hydraulic_conductivity=0,
               capillary_head=0,
               water_content_diff=0,
               gauges_pos=[]):
    """
    folderName: a path to store input folder
    fileToCreate: 'all'|'z','h','hU','manning',
                        'precipitation_mask','precipitation_source',
                        'boundary_condition','gauges_pos'                        
    """
    start = time.perf_counter()
    #create input and output folders
    _, _, dirMesh, dirField = ISF.CreateIOFolders(folderName)
    #---------- initiate field names and values-------------------
    fieldFiles = {
        'z': demMat,
        'h': h0,
        'hU': hU0,
        'precipitation': 0,  # first ztype file
        'manning': manning,
        'sewer_sink': sewer_sink,
        'cumulative_depth': cumulative_depth,
        'hydraulic_conductivity': hydraulic_conductivity,
        'capillary_head': capillary_head,
        'water_content_diff': water_content_diff,  # last ztype file
        'precipitation_mask': rain_mask,
        'precipitation_source': rain_source,
        'boundary_condition': [],
        'gauges_pos': gauges_pos
    }

    #%------------ generate computing cell ID and define boundary cells-------------
    # outline cells are classified according to user-defined boundList: 1,2,...
    # and boundary code for h and hU are created
    CellIDMat, OutlineBoundMat = ISF.Get_ID_BNoutline_Mat(demMat)
    BoundTypeMat = ISF.BoundaryClassify(OutlineBoundMat, demHead,
                                        boundList)  #oultine cells 0,1,2...

    # create boundary array for writing files
    hCode, hUCode = ISF.Get3ElementBoundCode(boundList)
    # boundary arrays
    id_bCodes = ISF.Create_ID_BoundCode_Array(CellIDMat, BoundTypeMat, hCode,
                                              hUCode)
    h_BC_source, hU_BC_source = ISF.BoundSourceProcessing(
        BoundTypeMat, boundList, demHead)
    #%******************  create files ********************************
    if not isinstance(fileToCreate, list):
        if fileToCreate == 'all':
            fileToCreate = list(fieldFiles.keys())
            fileToCreate.insert(0, 'DEM')
        else:
            fileToCreate = [fileToCreate]
    totalFileNum = len(fileToCreate)
    print('Files to create:')
    print(fileToCreate)
    progress = 1
    fileLeft = totalFileNum
    if fileLeft > 13:
        fileLeft = 13
    end = time.perf_counter()
    timeElapse = end - start  #seconds
    #------------ create DEM.txt in mesh-------------------------------------------
    if 'DEM' in fileToCreate:
        fileNameToRite = dirMesh + 'DEM.txt'
        ProgressBar(totalFileNum, progress, 'DEM', timeElapse * fileLeft * 10)
        start = time.perf_counter()
        arcgridwrite(fileNameToRite, demMat, demHead)
        progress = progress + 1
        #print(fileNameToRite+' is created')
        end = time.perf_counter()
        timeElapse = end - start
        fileLeft = fileLeft - 1
    for key, value in fieldFiles.items():
        if key in fileToCreate:
            start = time.perf_counter()
            if len(key) > 13:
                showTag = key[0:5] + '..' + key[-5:]
            else:
                showTag = key
            ProgressBar(totalFileNum, progress, showTag, timeElapse * fileLeft)
            if key == 'precipitation_mask':  # time 1
                id_zV = ISF.Create_ID_zValue_Array(CellIDMat, value)
                ISF.WriteRainMask(dirField, id_zV)
            elif key == 'precipitation_source':  # time 0.5
                ISF.WriteRainSource(dirField, value)
            elif key == 'boundary_condition':
                ISF.WriteBoundSource(dirField, h_BC_source, hU_BC_source)
            elif key == 'gauges_pos':
                ISF.WriteGaugePos(dirField, value)
            else:
                # create valid cell array for writing files
                id_zV = ISF.Create_ID_zValue_Array(CellIDMat, value)
                if key == 'h':
                    id_bCode = id_bCodes['hArray']
                elif key == 'hU':
                    id_bCode = id_bCodes['hUArray']
                else:
                    id_bCode = id_bCodes['otherArray']
                fileNameToRite = dirField + key + '.dat'
                ISF.Write_ZtypeFile(fileNameToRite,
                                    id_zV,
                                    id_BoundCode=id_bCode)
            fileLeft = fileLeft - 1
            if fileLeft < 0:
                fileLeft = 0
            end = time.perf_counter()
            timeElapse = end - start
            #ProgressBar(totalFileNum, progress, showTag,timeElapse*fileLeft)
            progress = progress + 1
            #print('write_File: '+str(end-start))

    return None