예제 #1
0
def getzMatFromNimrodTar(fileName, clipExtent=[]):
    #  clipExtent = (left,right,bottom,top)
    zMatList = []
    dtStrList = []
    tar = tarfile.open(fileName)
    tar.getmembers()
    for member in tar.getmembers():
        fgz = tar.extractfile(member)
        #print(member.name)
        datetimeStr = member.name[31:31 + 12]
        f = gzip.open(fgz, 'rb')
        # using nimrod package
        gridObj = nimrod.Nimrod(f)
        f.close()
        #del f,fgz,member
        if len(clipExtent) == 4:
            gridObj = gridObj.apply_bbox(clipExtent[0], clipExtent[1],
                                         clipExtent[2], clipExtent[3])
        zMat, head, zExtent = gridObj.extract_asc()
        zMat[zMat == 0] = np.nan  #unit: mm/h
        zMatList.append(zMat)
        #del zMat
        dtStrList.append(datetimeStr)
    tar.close()
    #del tar
    #import gc
    #gc.collect()
    zMatList = [x for _, x in sorted(zip(dtStrList, zMatList))]
    dtStrList.sort()
    return dtStrList, zMatList, head, zExtent
예제 #2
0
def read_gridded_data_from_tar(tarfileName, datetimeStr=None):
    """
    input:
        tarfileName: NIMROD tar file name
        datetimeStr: yyyyMMddHHmm('201012310830') the date and time string
            if datetimeStr is not given, output data of all records
    output:
        a Raster object
    """

    if datetimeStr is None:
        datetimeStr = get_datetime_str_from_tar(tarfileName)
    else:
        if type(datetimeStr) is not list:
            datetimeStr = [datetimeStr]
    tar = tarfile.open(tarfileName)
    members = tar.getmembers()
    output_grids = []
    output_datetimes = []
    for member in members:
        name = member.name.split('_')
        name = name[2]
        if name in datetimeStr:
            output_datetimes.append(name)
            fgz = tar.extractfile(member)
            f = gzip.open(fgz, 'rb')
            gridObj = nimrod.Nimrod(f)
            array, header, _ = gridObj.extract_asc()
            obj_raster = Raster(array=array, header=header)
            output_grids.append(obj_raster)
            f.close()
    tar.close()
    return output_grids, output_datetimes
예제 #3
0
def extractOneGridFromNimrodTar(tarfileName, datetimeStr):
    """
    input:
        tarfileName: NIMROD tar file name
        datetimeStr: yyyyMMddHHmm('201012310830') the date and time string
    output:
        a nimrod object
    """
    gridObj = []
    tar = tarfile.open(tarfileName)
    for member in tar.getmembers():
        fgz = tar.extractfile(member)
        #print(member.name)
        if datetimeStr in member.name:
            print(member.name)
            f = gzip.open(fgz, 'rb')
            # using nimrod package
            gridObj = nimrod.Nimrod(f)
            f.close()
    tar.close()
    return gridObj