Exemplo n.º 1
0
def temporal_and_area_info_metric(finalMCCList):
    '''
    Purpose:: To provide information regarding the temporal properties of the MCCs found

    Inputs:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC

    Returns:: allMCCtimes: a list of dictionaries {mccTimes, starttime, endtime, duration, area} representing the MCC
       temporal details for each MCC in the period considered

    Assumptions::
        the final time hour --> the event lasted throughout that hr, therefore +1 to endtime
    '''

    # TODO: in real data edit this to use datetime
    mccTimes = []
    allMCCtimes = []
    mcsArea = []

    if finalMCCList:
        for eachMCC in finalMCCList:
            # Get the info from the node
            for eachNode in eachMCC:
                mccTimes.append(
                    mccSearch.this_dict(eachNode)['cloudElementTime'])
                mcsArea.append(
                    mccSearch.this_dict(eachNode)['cloudElementArea'])

            # Sort and remove duplicates
            mccTimes = list(set(mccTimes))
            mccTimes.sort()
            tdelta = mccTimes[1] - mccTimes[0]
            starttime = mccTimes[0]
            endtime = mccTimes[-1]
            duration = (endtime - starttime) + tdelta
            print 'starttime ', starttime, 'endtime ', endtime, 'tdelta ', tdelta, 'duration ', duration, 'MCSAreas ', mcsArea
            allMCCtimes.append({
                'MCCtimes': mccTimes,
                'starttime': starttime,
                'endtime': endtime,
                'duration': duration,
                'MCSArea': mcsArea
            })
            mccTimes = []
            mcsArea = []
    else:
        allMCCtimes = []
        tdelta = 0

    return allMCCtimes, tdelta
Exemplo n.º 2
0
def precip_max_min(finalMCCList):
    '''
    TODO: this doesnt work the np.min/max function seems to be not working with the nonzero option
        ..possibly a problem upstream with cloudElementLatLonTRMM

    Purpose:: Precipitation maximum and min rates associated with each cloud elements in MCS
    
    Inputs:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC

    Returns:: mcsPrecip: a list representing the max and min rate for each cloud elements identified

    '''
    maxCEprecip = 0.0
    minCEprecip = 0.0
    mcsPrecip = []
    allmcsPrecip = []

    if finalMCCList:
        if type(finalMCCList[0]) is str:  # len(finalMCCList) == 1:
            for node in finalMCCList:
                eachNode = mccSearch.this_dict(node)
                #cloudElementTRMM = eachNode['cloudElementLatLonTRMM']
                maxCEprecip = np.max(
                    eachNode['cloudElementLatLonTRMM'][np.nonzero(
                        eachNode['cloudElementLatLonTRMM'])])
                minCEprecip = np.min(
                    eachNode['cloudElementLatLonTRMM'][np.nonzero(
                        eachNode['cloudElementLatLonTRMM'])])
                mcsPrecip.append(
                    (eachNode['uniqueID'], minCEprecip, maxCEprecip))
        else:
            for eachMCC in finalMCCList:
                # Get the info from the node
                for node in eachMCC:
                    eachNode = mccSearch.this_dict(node)
                    # Find min and max precip
                    maxCEprecip = np.max(
                        eachNode['cloudElementLatLonTRMM'][np.nonzero(
                            eachNode['cloudElementLatLonTRMM'])])
                    minCEprecip = np.min(
                        eachNode['cloudElementLatLonTRMM'][np.nonzero(
                            eachNode['cloudElementLatLonTRMM'])])
                    mcsPrecip.append(
                        (eachNode['uniqueID'], minCEprecip, maxCEprecip))
                allmcsPrecip.append(mcsPrecip)
                mcsPrecip = []

    return mcsPrecip
Exemplo n.º 3
0
def average_feature_size(finalMCCList):
    '''
    Purpose:: To determine the average MCC size for the period

    Inputs:: finalMCCList: a list of list of strings representing  a list of list of nodes representing a MCC

    Returns::a floating-point representing the average area of a MCC in the period

    Assumptions::

    '''
    thisMCC = 0.0
    thisMCCAvg = 0.0

    # For each node in the list, get the are information from the dictionary
    # in the graph and calculate the area
    for eachPath in finalMCCList:
        for eachNode in eachPath:
            thisMCC += mccSearch.this_dict(eachNode)['cloudElementArea']

        thisMCCAvg += (thisMCC / len(eachPath))
        thisMCC = 0.0

    # Calculate final average
    return thisMCCAvg / (len(finalMCCList))
Exemplo n.º 4
0
def average_feature_size(finalMCCList):
    '''
    Purpose:: To determine the average MCC size for the period

    Inputs:: finalMCCList: a list of list of strings representing  a list of list of nodes representing a MCC

    Returns::a floating-point representing the average area of a MCC in the period

    Assumptions::

    '''
    thisMCC = 0.0
    thisMCCAvg = 0.0

    # For each node in the list, get the are information from the dictionary
    # in the graph and calculate the area
    for eachPath in finalMCCList:
        for eachNode in eachPath:
            thisMCC += mccSearch.this_dict(eachNode)['cloudElementArea']

        thisMCCAvg += (thisMCC/len(eachPath))
        thisMCC = 0.0

    # Calculate final average
    return thisMCCAvg/(len(finalMCCList))
Exemplo n.º 5
0
def temporal_and_area_info_metric(finalMCCList):
    '''
    Purpose:: To provide information regarding the temporal properties of the MCCs found

    Inputs:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC

    Returns:: allMCCtimes: a list of dictionaries {mccTimes, starttime, endtime, duration, area} representing the MCC
       temporal details for each MCC in the period considered

    Assumptions::
        the final time hour --> the event lasted throughout that hr, therefore +1 to endtime
    '''

    # TODO: in real data edit this to use datetime
    mccTimes = []
    allMCCtimes = []
    mcsArea = []

    if finalMCCList:
        for eachMCC in finalMCCList:
            # Get the info from the node
            for eachNode in eachMCC:
                mccTimes.append(mccSearch.this_dict(eachNode)['cloudElementTime'])
                mcsArea.append(mccSearch.this_dict(eachNode)['cloudElementArea'])

            # Sort and remove duplicates
            mccTimes = list(set(mccTimes))
            mccTimes.sort()
            tdelta = mccTimes[1] - mccTimes[0]
            starttime = mccTimes[0]
            endtime = mccTimes[-1]
            duration = (endtime - starttime) + tdelta
            print 'starttime ', starttime, 'endtime ', endtime, 'tdelta ', tdelta, 'duration ', duration, 'MCSAreas ', mcsArea
            allMCCtimes.append({'MCCtimes': mccTimes, 'starttime': starttime, 'endtime': endtime,
                                'duration': duration, 'MCSArea': mcsArea})
            mccTimes = []
            mcsArea = []
    else:
        allMCCtimes = []
        tdelta = 0

    return allMCCtimes, tdelta
Exemplo n.º 6
0
def precip_totals(finalMCCList):
    '''
    Purpose:: Precipitation totals associated with a cloud element

    Inputs:: finalMCCList: a list of dictionaries representing a list of nodes per a MCC

    Returns:: precipTotal: a floating-point number representing the total amount of precipitation associated
       with the feature

    '''

    precipTotal = 0.0
    cloudElementPrecip = 0.0
    mcsPrecip = []
    allmcsPrecip = []
    count = 0

    if finalMCCList:
        # Print 'len finalMCCList is: ', len(finalMCCList)
        for eachMCC in finalMCCList:
            # Get the info from the node
            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                count += 1
                if count == 1:
                    prevHr = int(
                        str(eachNode['cloudElementTime']).replace(' ',
                                                                  '')[-8:-6])

                currHr = int(
                    str(eachNode['cloudElementTime']).replace(' ', '')[-8:-6])
                if prevHr == currHr:
                    cloudElementPrecip += eachNode['cloudElementPrecipTotal']
                else:
                    mcsPrecip.append((prevHr, cloudElementPrecip))
                    cloudElementPrecip = eachNode['cloudElementPrecipTotal']
                # Last value in for loop
                if count == len(eachMCC):
                    mcsPrecip.append((currHr, cloudElementPrecip))

                precipTotal += eachNode['cloudElementPrecipTotal']
                prevHr = currHr

            mcsPrecip.append(('0', precipTotal))

            allmcsPrecip.append(mcsPrecip)
            precipTotal = 0.0
            cloudElementPrecip = 0.0
            mcsPrecip = []
            count = 0

        print 'allmcsPrecip ', allmcsPrecip

    return allmcsPrecip
Exemplo n.º 7
0
def precip_totals(finalMCCList):
    '''
    Purpose:: Precipitation totals associated with a cloud element

    Inputs:: finalMCCList: a list of dictionaries representing a list of nodes per a MCC

    Returns:: precipTotal: a floating-point number representing the total amount of precipitation associated
       with the feature

    '''

    precipTotal = 0.0
    cloudElementPrecip = 0.0
    mcsPrecip = []
    allmcsPrecip = []
    count = 0

    if finalMCCList:
        # Print 'len finalMCCList is: ', len(finalMCCList)
        for eachMCC in finalMCCList:
            # Get the info from the node
            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                count += 1
                if count == 1:
                    prevHr = int(str(eachNode['cloudElementTime']).replace(' ', '')[-8:-6])

                currHr = int(str(eachNode['cloudElementTime']).replace(' ', '')[-8:-6])
                if prevHr == currHr:
                    cloudElementPrecip += eachNode['cloudElementPrecipTotal']
                else:
                    mcsPrecip.append((prevHr, cloudElementPrecip))
                    cloudElementPrecip = eachNode['cloudElementPrecipTotal']
                # Last value in for loop
                if count == len(eachMCC):
                    mcsPrecip.append((currHr, cloudElementPrecip))

                precipTotal += eachNode['cloudElementPrecipTotal']
                prevHr = currHr

            mcsPrecip.append(('0', precipTotal))

            allmcsPrecip.append(mcsPrecip)
            precipTotal = 0.0
            cloudElementPrecip = 0.0
            mcsPrecip = []
            count = 0

        print 'allmcsPrecip ', allmcsPrecip

    return allmcsPrecip
Exemplo n.º 8
0
def precip_max_min(finalMCCList):
    '''
    TODO: this doesnt work the np.min/max function seems to be not working with the nonzero option
        ..possibly a problem upstream with cloudElementLatLonTRMM

    Purpose:: Precipitation maximum and min rates associated with each cloud elements in MCS
    
    Inputs:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC

    Returns:: mcsPrecip: a list representing the max and min rate for each cloud elements identified

    '''
    maxCEprecip = 0.0
    minCEprecip = 0.0
    mcsPrecip = []
    allmcsPrecip = []

    if finalMCCList:
        if type(finalMCCList[0]) is str:  # len(finalMCCList) == 1:
            for node in finalMCCList:
                eachNode = mccSearch.this_dict(node)
                #cloudElementTRMM = eachNode['cloudElementLatLonTRMM']
                maxCEprecip = np.max(eachNode['cloudElementLatLonTRMM'][np.nonzero(eachNode['cloudElementLatLonTRMM'])])
                minCEprecip = np.min(eachNode['cloudElementLatLonTRMM'][np.nonzero(eachNode['cloudElementLatLonTRMM'])])
                mcsPrecip.append((eachNode['uniqueID'], minCEprecip, maxCEprecip))
        else:
            for eachMCC in finalMCCList:
                # Get the info from the node
                for node in eachMCC:
                    eachNode = mccSearch.this_dict(node)
                    # Find min and max precip
                    maxCEprecip = np.max(eachNode['cloudElementLatLonTRMM'][np.nonzero(eachNode['cloudElementLatLonTRMM'])])
                    minCEprecip = np.min(eachNode['cloudElementLatLonTRMM'][np.nonzero(eachNode['cloudElementLatLonTRMM'])])
                    mcsPrecip.append((eachNode['uniqueID'], minCEprecip, maxCEprecip))
                allmcsPrecip.append(mcsPrecip)
                mcsPrecip = []

    return mcsPrecip
Exemplo n.º 9
0
def find_cloud_element_speed(node, mcsList, theList):
    '''
    Purpose:: To determine the speed of the cloud elements uses vector displacement deltaLat/deltaLon (y/x)

    Inputs:: node: a string representing the cloud element
        mcsList: a list of strings representing the feature

    Returns:: cloudElementSpeed: a floating-point number representing the speed of the cloud element

    '''

    deltaLon = 0.0
    deltaLat = 0.0
    cloudElementSpeed = []
    theSpeed = 0.0

    nodeLatLon = mccSearch.this_dict(node)['cloudElementCenter']

    for aNode in theList:
        if aNode in mcsList:
            # If aNode is part of the mcsList then determine distance
            aNodeLatLon = mccSearch.this_dict(aNode)['cloudElementCenter']
            # Calculate CE speed
            # Checking the lats
            # nodeLatLon[0] += 90.0
            # aNodeLatLon[0] += 90.0
            # deltaLat = (nodeLatLon[0] - aNodeLatLon[0])
            deltaLat = (
                (mccSearch.this_dict(node)['cloudElementCenter'][0] + 90.0) -
                (mccSearch.this_dict(aNode)['cloudElementCenter'][0] + 90.0))
            # nodeLatLon[1] += 360.0
            # aNodeLatLon[1] += 360.0
            # deltaLon = (nodeLatLon[1] - aNodeLatLon[1])
            deltaLon = (
                (mccSearch.this_dict(node)['cloudElementCenter'][1] + 360.0) -
                (mccSearch.this_dict(aNode)['cloudElementCenter'][1] + 360.0))
            # Fail safe for movement only in one dir
            if deltaLat == 0.0:
                deltaLat = 1.0

            if deltaLon == 0.0:
                deltaLon = 1.0

            try:
                theSpeed = abs((((deltaLat / deltaLon) * LAT_DISTANCE * 1000) /
                                (TRES * 3600)))  # Convert to s --> m/s
            except:
                theSpeed = 0.0

            cloudElementSpeed.append(theSpeed)

    if not cloudElementSpeed:
        return 0.0
    else:
        return min(cloudElementSpeed)
Exemplo n.º 10
0
def find_cloud_element_speed(node, mcsList, theList):
    '''
    Purpose:: To determine the speed of the cloud elements uses vector displacement deltaLat/deltaLon (y/x)

    Inputs:: node: a string representing the cloud element
        mcsList: a list of strings representing the feature

    Returns:: cloudElementSpeed: a floating-point number representing the speed of the cloud element

    '''

    deltaLon = 0.0
    deltaLat = 0.0
    cloudElementSpeed = []
    theSpeed = 0.0

    nodeLatLon = mccSearch.this_dict(node)['cloudElementCenter']

    for aNode in theList:
        if aNode in mcsList:
            # If aNode is part of the mcsList then determine distance
            aNodeLatLon = mccSearch.this_dict(aNode)['cloudElementCenter']
            # Calculate CE speed
            # Checking the lats
            # nodeLatLon[0] += 90.0
            # aNodeLatLon[0] += 90.0
            # deltaLat = (nodeLatLon[0] - aNodeLatLon[0])
            deltaLat = ((mccSearch.this_dict(node)['cloudElementCenter'][0] + 90.0) -
                        (mccSearch.this_dict(aNode)['cloudElementCenter'][0]+90.0))
            # nodeLatLon[1] += 360.0
            # aNodeLatLon[1] += 360.0
            # deltaLon = (nodeLatLon[1] - aNodeLatLon[1])
            deltaLon = ((mccSearch.this_dict(node)['cloudElementCenter'][1]+360.0) -
                        (mccSearch.this_dict(aNode)['cloudElementCenter'][1]+360.0))
            # Fail safe for movement only in one dir
            if deltaLat == 0.0:
                deltaLat = 1.0

            if deltaLon == 0.0:
                deltaLon = 1.0

            try:
                theSpeed = abs((((deltaLat/deltaLon)*LAT_DISTANCE*1000)/(TRES*3600)))  # Convert to s --> m/s
            except:
                theSpeed = 0.0

            cloudElementSpeed.append(theSpeed)

    if not cloudElementSpeed:
        return 0.0
    else:
        return min(cloudElementSpeed)
Exemplo n.º 11
0
def display_size(finalMCCList, MAIN_DIRECTORY):
    '''
    Purpose:: To create a figure showing the area verse time for each MCS

    Inputs:: finalMCCList: a list of list of strings representing the list of nodes representing a MCC
            MAIN_DIRECTORY: a string representing the path to the main directory where the data generated is saved

    Returns:: None

    Generates:: A plot with the area contribution of each node in a feature in the list

    '''
    timeList = []
    count = 1
    imgFilename = ''
    minArea = 10000.0
    maxArea = 0.0
    eachNode = {}

    # For each node in the list, get the area information from the dictionary
    # in the graph and calculate the area

    if finalMCCList:
        for eachMCC in finalMCCList:
            # Get the info from the node
            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                timeList.append(eachNode['cloudElementTime'])

                if eachNode['cloudElementArea'] < minArea:
                    minArea = eachNode['cloudElementArea']
                if eachNode['cloudElementArea'] > maxArea:
                    maxArea = eachNode['cloudElementArea']

            # Sort and remove duplicates
            timeList = list(set(timeList))
            timeList.sort()
            tdelta = timeList[1] - timeList[0]
            starttime = timeList[0] - tdelta
            endtime = timeList[-1] + tdelta
            timeList.insert(0, starttime)
            timeList.append(endtime)

            # Plot info
            plt.close('all')
            title = 'Area distribution of the MCC over somewhere'
            fig = plt.figure(facecolor='white', figsize=(18, 10))
            fig, ax = plt.subplots(1, facecolor='white', figsize=(10, 10))

            # The data
            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                if eachNode['cloudElementArea'] < 80000:
                    ax.plot(eachNode['cloudElementTime'],
                            eachNode['cloudElementArea'],
                            'bo',
                            markersize=10)
                elif eachNode['cloudElementArea'] >= 80000.00 and eachNode[
                        'cloudElementArea'] < 160000.00:
                    ax.plot(eachNode['cloudElementTime'],
                            eachNode['cloudElementArea'],
                            'yo',
                            markersize=20)
                else:
                    ax.plot(eachNode['cloudElementTime'],
                            eachNode['cloudElementArea'],
                            'ro',
                            markersize=30)

            # Axes and labels
            maxArea += 1000.00
            ax.set_xlim(starttime, endtime)
            ax.set_ylim(minArea, maxArea)
            ax.set_ylabel('Area in km^2', fontsize=12)
            ax.set_title(title)
            ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d%H:%M:%S')
            fig.autofmt_xdate()

            plt.subplots_adjust(bottom=0.2)

            imgFilename = MAIN_DIRECTORY + '/images/' + str(count) + 'MCS.gif'
            plt.savefig(imgFilename,
                        facecolor=fig.get_facecolor(),
                        transparent=True)

            # If time in not already in the time list, append it
            timeList = []
            count += 1
    return
Exemplo n.º 12
0
def display_size(finalMCCList, MAIN_DIRECTORY):
    '''
    Purpose:: To create a figure showing the area verse time for each MCS

    Inputs:: finalMCCList: a list of list of strings representing the list of nodes representing a MCC
            MAIN_DIRECTORY: a string representing the path to the main directory where the data generated is saved

    Returns:: None

    Generates:: A plot with the area contribution of each node in a feature in the list

    '''
    timeList = []
    count = 1
    imgFilename = ''
    minArea = 10000.0
    maxArea = 0.0
    eachNode = {}

    # For each node in the list, get the area information from the dictionary
    # in the graph and calculate the area

    if finalMCCList:
        for eachMCC in finalMCCList:
            # Get the info from the node
            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                timeList.append(eachNode['cloudElementTime'])

                if eachNode['cloudElementArea'] < minArea:
                    minArea = eachNode['cloudElementArea']
                if eachNode['cloudElementArea'] > maxArea:
                    maxArea = eachNode['cloudElementArea']

            # Sort and remove duplicates
            timeList = list(set(timeList))
            timeList.sort()
            tdelta = timeList[1] - timeList[0]
            starttime = timeList[0] - tdelta
            endtime = timeList[-1] + tdelta
            timeList.insert(0, starttime)
            timeList.append(endtime)

            # Plot info
            plt.close('all')
            title = 'Area distribution of the MCC over somewhere'
            fig = plt.figure(facecolor='white', figsize=(18,10))
            fig, ax = plt.subplots(1, facecolor='white', figsize=(10,10))

            # The data
            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                if eachNode['cloudElementArea'] < 80000:
                    ax.plot(eachNode['cloudElementTime'], eachNode['cloudElementArea'], 'bo', markersize=10)
                elif eachNode['cloudElementArea'] >= 80000.00 and eachNode['cloudElementArea'] < 160000.00:
                    ax.plot(eachNode['cloudElementTime'], eachNode['cloudElementArea'], 'yo', markersize=20)
                else:
                    ax.plot(eachNode['cloudElementTime'], eachNode['cloudElementArea'], 'ro', markersize=30)

            # Axes and labels
            maxArea += 1000.00
            ax.set_xlim(starttime, endtime)
            ax.set_ylim(minArea, maxArea)
            ax.set_ylabel('Area in km^2', fontsize=12)
            ax.set_title(title)
            ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d%H:%M:%S')
            fig.autofmt_xdate()

            plt.subplots_adjust(bottom=0.2)

            imgFilename = MAIN_DIRECTORY+'/images/' + str(count)+'MCS.gif'
            plt.savefig(imgFilename, facecolor=fig.get_facecolor(), transparent=True)

            # If time in not already in the time list, append it
            timeList = []
            count += 1
    return
Exemplo n.º 13
0
def create_text_file(finalMCCList, identifier, userVariables, graphVariables):
    '''
    Purpose::
        Create a text file with information about the MCS
        This function is expected to be especially of use regarding long term record checks

    Inputs::
        finalMCCList: a list of dictionaries representing a list of nodes representing a MCC
        identifier: an integer representing the type of list that has been entered...this is for creating file purposes
            1 - MCCList; 2- mcsList

    Returns:: None

    Generates::
        mcsUserFile: a user readable text file with all information about each MCS
        mcsSummaryFile: a user readable text file with the summary of the MCS
        mcsPostFile: a user readable text file with each MCS identified

    Assumptions::

    TODOs: provide visualizations for the calculations used to create the textFile
    '''

    durations = 0.0
    startTimes = []
    endTimes = []
    averagePropagationSpeed = 0.0
    speedCounter = 0
    maxArea = 0.0
    amax = 0.0
    avgMaxArea = []
    maxAreaCounter = 0.0
    maxAreaTime = ''
    eccentricity = 0.0
    firstTime = True
    matureFlag = True
    timeMCSMatures = ''
    maxCEprecipRate = 0.0
    minCEprecipRate = 0.0
    averageArea = 0.0
    averageAreaCounter = 0
    durationOfMatureMCC = 0
    avgMaxPrecipRate = 0.0
    avgMaxPrecipRateCounter = 0
    avgMinPrecipRate = 0.0
    avgMinPrecipRateCounter = 0
    cloudElementSpeed = 0.0
    mcsSpeed = 0.0
    mcsSpeedCounter = 0
    mcsPrecipTotal = 0.0
    avgmcsPrecipTotalCounter = 0
    bigPtotal = 0.0
    bigPtotalCounter = 0
    allPropagationSpeeds = []
    averageAreas = []
    areaAvg = 0.0
    avgPrecipTotal = 0.0
    avgPrecipTotalCounter = 0
    avgMaxmcsPrecipRate = 0.0
    avgMaxmcsPrecipRateCounter = 0
    avgMinmcsPrecipRate = 0.0
    avgMinmcsPrecipRateCounter = 0
    avgPrecipArea = []
    location = []
    avgPrecipAreaPercent = 0.0
    precipArea = 0.0
    precipAreaPercent = 0.0
    precipPercent = []
    precipCounter = 0
    precipAreaAvg = 0.0
    minSpeed = 0.0
    maxSpeed = 0.0

    if identifier == 1:
        mcsUserFile = open((userVariables.DIRS['mainDirStr'] + '/textFiles/MCCsUserFile.txt'), 'wb')
        mcsSummaryFile = open((userVariables.DIRS['mainDirStr'] + '/textFiles/MCCSummary.txt'), 'wb')
        mcsPostFile = open((userVariables.DIRS['mainDirStr'] + '/textFiles/MCCPostPrecessing.txt'), 'wb')

    if identifier == 2:
        mcsUserFile = open((userVariables.DIRS['mainDirStr'] + '/textFiles/MCSsUserFile.txt'), 'wb')
        mcsSummaryFile = open((userVariables.DIRS['mainDirStr'] + '/textFiles/MCSSummary.txt'), 'wb')
        mcsPostFile = open((userVariables.DIRS['mainDirStr'] + '/textFiles/MCSPostPrecessing.txt'), 'wb')

    for eachPath in finalMCCList:
        eachPath.sort(key=lambda nodeID: (len(nodeID.split('C')[0]), nodeID.split('C')[0], nodeID.split('CE')[1]))
        mcsPostFile.write('\n %s' % eachPath)

        startTime = mccSearch.this_dict(eachPath[0], graphVariables)['cloudElementTime']
        endTime = mccSearch.this_dict(eachPath[-1], graphVariables)['cloudElementTime']
        duration = (endTime - startTime) + timedelta(hours=userVariables.TRES)

        # Convert datatime duration to seconds and add to the total for the average duration of all MCS in finalMCCList
        durations += (duration.total_seconds())

        #durations += duration
        startTimes.append(startTime)
        endTimes.append(endTime)

        # Get the precip info
        for eachNode in eachPath:
            thisNode = mccSearch.this_dict(eachNode, graphVariables)
            # Set first time min 'fake' values
            if firstTime is True:
                minCEprecipRate = thisNode['CETRMMmin']
                avgMinmcsPrecipRate += thisNode['CETRMMmin']
                firstTime = False
            # Calculate the speed
            # if thisNode['cloudElementArea'] >= OUTER_CLOUD_SHIELD_AREA:
            #   averagePropagationSpeed += find_cloud_element_speed(eachNode, eachPath)
            #   speedCounter +=1

            # Amax: find max area
            if thisNode['cloudElementArea'] > maxArea:
                maxArea = thisNode['cloudElementArea']
                maxAreaTime = str(thisNode['cloudElementTime'])
                eccentricity = thisNode['cloudElementEccentricity']
                location = thisNode['cloudElementCenter']

                # Determine the time the feature matures
                if matureFlag is True:
                    timeMCSMatures = str(thisNode['cloudElementTime'])
                    matureFlag = False

            # Find min and max precip rate
            if thisNode['CETRMMmin'] < minCEprecipRate:
                minCEprecipRate = thisNode['CETRMMmin']

            if thisNode['CETRMMmax'] > maxCEprecipRate:
                maxCEprecipRate = thisNode['CETRMMmax']

            # If MCC, then calculate for only mature phase else, calculate for full MCS
            if identifier == 1:
                # Calculations for only the mature stage
                try:
                    if thisNode['nodeMCSIdentifier'] == 'M':
                        # Calculate average area of the maturity feature only
                        averageArea += thisNode['cloudElementArea']
                        averageAreaCounter += 1
                        durationOfMatureMCC += 1
                        avgMaxPrecipRate += thisNode['CETRMMmax']
                        avgMaxPrecipRateCounter += 1
                        avgMinPrecipRate += thisNode['CETRMMmin']
                        avgMinPrecipRateCounter += 1
                        avgMaxmcsPrecipRate += thisNode['CETRMMmax']
                        avgMaxmcsPrecipRateCounter += 1
                        avgMinmcsPrecipRate += thisNode['CETRMMmin']
                        avgMinmcsPrecipRateCounter += 1

                        # The precip percentage (TRMM area/CE area)
                        if thisNode['cloudElementArea'] >= 0.0 and thisNode['TRMMArea'] >= 0.0:
                            precipArea += thisNode['TRMMArea']
                            avgPrecipArea.append(thisNode['TRMMArea'])
                            avgPrecipAreaPercent += (thisNode['TRMMArea']/thisNode['cloudElementArea'])
                            precipPercent.append((thisNode['TRMMArea']/thisNode['cloudElementArea']))
                            precipCounter += 1

                        # System speed for only mature stage
                        # cloudElementSpeed = find_cloud_element_speed(eachNode,eachPath)
                        # if cloudElementSpeed > 0.0 :
                        #   mcsSpeed += cloudElementSpeed
                        #   mcsSpeedCounter += 1
                except:
                    print 'MCS node has no nodeMCSIdentifier ', thisNode['uniqueID']
                    avgMaxmcsPrecipRate += thisNode['CETRMMmax']
                    avgMaxmcsPrecipRateCounter += 1
                    avgMinmcsPrecipRate += thisNode['CETRMMmin']
                    avgMinmcsPrecipRateCounter += 1
            else:
                averageArea += thisNode['cloudElementArea']
                averageAreaCounter += 1
                durationOfMatureMCC += 1
                avgMaxPrecipRate += thisNode['CETRMMmax']
                avgMaxPrecipRateCounter += 1
                avgMinPrecipRate += thisNode['CETRMMmin']
                avgMinPrecipRateCounter += 1
                avgMaxmcsPrecipRate += thisNode['CETRMMmax']
                avgMaxmcsPrecipRateCounter += 1
                avgMinmcsPrecipRate += thisNode['CETRMMmin']
                avgMinmcsPrecipRateCounter += 1

            # The precip percentage (TRMM area/CE area)
            if thisNode['cloudElementArea'] >= 0.0 and thisNode['TRMMArea'] >= 0.0:
                precipArea += thisNode['TRMMArea']
                avgPrecipArea.append(thisNode['TRMMArea'])
                avgPrecipAreaPercent += (thisNode['TRMMArea']/thisNode['cloudElementArea'])
                precipPercent.append((thisNode['TRMMArea']/thisNode['cloudElementArea']))
                precipCounter += 1

            # System speed for only mature stage
            # cloudElementSpeed = find_cloud_element_speed(eachNode,eachPath)
            # if cloudElementSpeed > 0.0 :
            #   mcsSpeed += cloudElementSpeed
            #   mcsSpeedCounter += 1

            # Find accumulated precip
            if thisNode['cloudElementPrecipTotal'] > 0.0:
                mcsPrecipTotal += thisNode['cloudElementPrecipTotal']
                avgmcsPrecipTotalCounter += 1

        # A: calculate the average Area of the (mature) MCS
        if averageAreaCounter > 0:  # and averageAreaCounter > 0:
            averageArea /= averageAreaCounter
            averageAreas.append(averageArea)

        # V: MCS speed
        if mcsSpeedCounter > 0:  # and mcsSpeed > 0.0:
            mcsSpeed /= mcsSpeedCounter

        # smallP_max: calculate the average max precip rate (mm/h)
        if avgMaxmcsPrecipRateCounter > 0:  # and avgMaxPrecipRate > 0.0:
            avgMaxmcsPrecipRate /= avgMaxmcsPrecipRateCounter

        # smallP_min: calculate the average min precip rate (mm/h)
        if avgMinmcsPrecipRateCounter > 0:  # and avgMinPrecipRate > 0.0:
            avgMinmcsPrecipRate /= avgMinmcsPrecipRateCounter

        # smallP_avg: calculate the average precipitation (mm hr-1)
        if mcsPrecipTotal > 0.0:  # and avgmcsPrecipTotalCounter> 0:
            avgmcsPrecipTotal = mcsPrecipTotal/avgmcsPrecipTotalCounter
            avgPrecipTotal += avgmcsPrecipTotal
            avgPrecipTotalCounter += 1

        #smallP_total = mcsPrecipTotal
        # Precip over the MCS lifetime prep for bigP_total
        if mcsPrecipTotal > 0.0:
            bigPtotal += mcsPrecipTotal
            bigPtotalCounter += 1

        if maxArea > 0.0:
            avgMaxArea.append(maxArea)
            maxAreaCounter += 1

        # Average precipate area percentage (TRMM/CE area)
        if precipCounter > 0:
            avgPrecipAreaPercent /= precipCounter
            precipArea /= precipCounter

        # Write stuff to file
        mcsUserFile.write('\n\n\nStarttime is: %s ' % (str(startTime)))
        mcsUserFile.write('\nEndtime is: %s ' % (str(endTime)))
        mcsUserFile.write('\nLife duration is %s hrs' % (str(duration)))
        mcsUserFile.write('\nTime of maturity is %s ' % (timeMCSMatures))
        mcsUserFile.write('\nDuration mature stage is: %s ' % durationOfMatureMCC*userVariables.TRES)
        mcsUserFile.write('\nAverage area is: %.4f km^2 ' % (averageArea))
        mcsUserFile.write('\nMax area is: %.4f km^2 ' % (maxArea))
        mcsUserFile.write('\nMax area time is: %s ' % (maxAreaTime))
        mcsUserFile.write('\nEccentricity at max area is: %.4f ' % (eccentricity))
        mcsUserFile.write('\nCenter (lat,lon) at max area is: %.2f\t%.2f' % (location[0], location[1]))
        mcsUserFile.write('\nPropagation speed is %.4f ' % (mcsSpeed))
        mcsUserFile.write('\nMCS minimum precip rate is %.4f mmh^-1' % (minCEprecipRate))
        mcsUserFile.write('\nMCS maximum precip rate is %.4f mmh^-1' % (maxCEprecipRate))
        mcsUserFile.write('\nTotal precipitation during MCS is %.4f mm/lifetime' % (mcsPrecipTotal))
        mcsUserFile.write('\nAverage MCS precipitation is %.4f mm' % (avgmcsPrecipTotal))
        mcsUserFile.write('\nAverage MCS maximum precipitation is %.4f mmh^-1' % (avgMaxmcsPrecipRate))
        mcsUserFile.write('\nAverage MCS minimum precipitation is %.4f mmh^-1' % (avgMinmcsPrecipRate))
        mcsUserFile.write('\nAverage precipitation area is %.4f km^2 ' % (precipArea))
        mcsUserFile.write('\nPrecipitation area percentage of mature system %.4f percent ' % (avgPrecipAreaPercent*100))


        # Append stuff to lists for the summary file
        if mcsSpeed > 0.0:
            allPropagationSpeeds.append(mcsSpeed)
            averagePropagationSpeed += mcsSpeed
            speedCounter += 1

        # Reset vars for next MCS in list
        averageArea = 0.0
        averageAreaCounter = 0
        durationOfMatureMCC = 0
        mcsSpeed = 0.0
        mcsSpeedCounter = 0
        mcsPrecipTotal = 0.0
        avgMaxmcsPrecipRate = 0.0
        avgMaxmcsPrecipRateCounter = 0
        avgMinmcsPrecipRate = 0.0
        avgMinmcsPrecipRateCounter = 0
        firstTime = True
        matureFlag = True
        avgmcsPrecipTotalCounter = 0
        avgPrecipAreaPercent = 0.0
        precipArea = 0.0
        precipCounter = 0
        maxArea = 0.0
        maxAreaTime = ''
        eccentricity = 0.0
        timeMCSMatures = ''
        maxCEprecipRate = 0.0
        minCEprecipRate = 0.0
        location = []

    # LD: average duration
    if len(finalMCCList) > 1:
        durations /= len(finalMCCList)
        durations /= 3600.0  # Convert to hours

        # A: average area
        areaAvg = sum(averageAreas) / len(finalMCCList)
    # Create histogram plot here
    # if len(averageAreas) > 1:
    #   plotHistogram(averageAreas, 'Average Area [km^2]', 'Area [km^2]')

    # Amax: average maximum area
    if maxAreaCounter > 0.0:  # and avgMaxArea > 0.0 :
        amax = sum(avgMaxArea) / maxAreaCounter
        # Create histogram plot here
        #if len(avgMaxArea) > 1:
        #   plotHistogram(avgMaxArea, 'Maximum Area [km^2]', 'Area [km^2]')

    # v_avg: calculate the average propagation speed
    if speedCounter > 0:  # and averagePropagationSpeed > 0.0
        averagePropagationSpeed /= speedCounter

    # bigP_min: calculate the min rate in mature system
    if avgMinPrecipRate > 0.0:  # and avgMinPrecipRateCounter > 0.0:
        avgMinPrecipRate /= avgMinPrecipRateCounter

    # bigP_max: calculate the max rate in mature system
    if avgMinPrecipRateCounter > 0.0:  # and avgMaxPrecipRate >  0.0:
        avgMaxPrecipRate /= avgMaxPrecipRateCounter

    # bigP_avg: average total preicip rate mm/hr
    if avgPrecipTotalCounter > 0.0:  # and avgPrecipTotal > 0.0:
        avgPrecipTotal /= avgPrecipTotalCounter

    # bigP_total: total precip rate mm/LD
    if bigPtotalCounter > 0.0:  # and bigPtotal > 0.0:
        bigPtotal /= bigPtotalCounter

    # Precipitation area percentage
    if len(precipPercent) > 0:
        precipAreaPercent = (sum(precipPercent)/len(precipPercent))*100.0

    # average precipitation area
    if len(avgPrecipArea) > 0:
        precipAreaAvg = sum(avgPrecipArea)/len(avgPrecipArea)
        #if len(avgPrecipArea) > 1:
        #   plotHistogram(avgPrecipArea, 'Average Rainfall Area [km^2]', 'Area [km^2]')

    sTime = str(average_time(startTimes))
    eTime = str(average_time(endTimes))
    if len(allPropagationSpeeds) > 1:
        maxSpeed = max(allPropagationSpeeds)
        minSpeed = min(allPropagationSpeeds)

    # Write stuff to the summary file
    mcsSummaryFile.write('\nNumber of features is %d ' % (len(finalMCCList)))
    mcsSummaryFile.write('\nAverage duration is %.4f hrs ' % (durations))
    mcsSummaryFile.write('\nAverage startTime is %s ' % (sTime[-8:]))
    mcsSummaryFile.write('\nAverage endTime is %s ' % (eTime[-8:]))
    mcsSummaryFile.write('\nAverage size is %.4f km^2 ' % (areaAvg))
    mcsSummaryFile.write('\nAverage precipitation area is %.4f km^2 ' % (precipAreaAvg))
    mcsSummaryFile.write('\nAverage maximum size is %.4f km^2 ' % (amax))
    mcsSummaryFile.write('\nAverage propagation speed is %.4f ms^-1' % (averagePropagationSpeed))
    mcsSummaryFile.write('\nMaximum propagation speed is %.4f ms^-1 ' % (maxSpeed))
    mcsSummaryFile.write('\nMinimum propagation speed is %.4f ms^-1 ' % (minSpeed))
    mcsSummaryFile.write('\nAverage minimum precipitation rate is %.4f mmh^-1' % (avgMinPrecipRate))
    mcsSummaryFile.write('\nAverage maximum precipitation rate is %.4f mm h^-1' % (avgMaxPrecipRate))
    mcsSummaryFile.write('\nAverage precipitation is %.4f mm ' % (avgPrecipTotal))
    mcsSummaryFile.write('\nAverage total precipitation during MCSs is %.4f mm/LD ' % (bigPtotal))
    mcsSummaryFile.write('\nAverage precipitation area percentage is %.4f percent ' % (precipAreaPercent))

    mcsUserFile.close()
    mcsSummaryFile.close()
    mcsPostFile.close()
    return
Exemplo n.º 14
0
def plot_precip_histograms(finalMCCList, MAIN_DIRECTORY):
    '''
    Purpose:: To create plots (histograms) of the each TRMMnetcdfCEs files

    Input:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC
        MAIN_DIRECTORY: a string representing the path to the main directory where the data generated is saved

    Returns:: None

    Generates:: Histogram plots for each feature in the list passed
        
    '''
    numBins = 5
    precip = []
    imgFilename = ' '
    startTime = ' '
    firstTime = True

    if finalMCCList:

        for eachMCC in finalMCCList:
            firstTime = True

            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                thisTime = eachNode['cloudElementTime']

                thisFileName = MAIN_DIRECTORY+'/TRMMnetcdfCEs/TRMM' + str(thisTime).replace(' ', '_') + \
                    eachNode['uniqueID'] + '.nc'
                TRMMData = Dataset(thisFileName, 'r', format='NETCDF4')
                precipRate = TRMMData.variables['precipitation_Accumulation'][:, :, :]
                cloudElementPrecipRate = precipRate[0, :, :]
                TRMMData.close()

                if firstTime is True:
                    totalPrecip = np.zeros(cloudElementPrecipRate.shape)
                    startTime = str(thisTime) + eachNode['uniqueID']
                    firstTime = False

                totalPrecip = np.add(totalPrecip, precipRate)

                for _, value in np.ndenumerate(cloudElementPrecipRate):
                    if value != 0.0:
                        precip.append(value)

                plt.close('all')
                title = 'TRMM precipitation distribution for ' + str(thisTime)
                imgFilename = MAIN_DIRECTORY+'/images/'+str(thisTime)+eachNode['uniqueID']+'TRMMMCS.gif'
                xlabel = 'Precipitation [mm]'
                ylabel = 'Area [km^2]'
                numBins = 5
                if all(value == 0 for value in precip):
                    print 'NO Precipitation at '+str(thisTime)+eachNode['uniqueID']
                else:
                    plot_histogram(precip, title, imgFilename, xlabel, ylabel, numBins)

                precip = []

            # The image at the end of each MCS
            title = 'TRMM precipitation distribution for '+startTime+' to '+str(thisTime)
            imgFilename = MAIN_DIRECTORY+'/images/'+startTime+'_'+str(thisTime)+eachNode['uniqueID']+'TRMMMCS.gif'
            xlabel = 'Precipitation [mm]'
            ylabel = 'Area [km^2]'
            numBins = 10
            for _, value in np.ndenumerate(totalPrecip):
                if value != 0.0:
                    precip.append(value)

            if all(value == 0 for value in precip):
                print 'No precipitation for MCS starting at '+startTime+' and ending at '+str(thisTime)+eachNode['uniqueID']
            else:
                plot_histogram(precip, title, imgFilename, xlabel, ylabel, numBins)

            precip = []

    return
Exemplo n.º 15
0
def plot_accu_TRMM(finalMCCList, MAIN_DIRECTORY):
    '''
    Purpose:: (1) generate a file with the accumulated precipitation for the MCS (2) generate the appropriate image
            
    Input:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC
            MAIN_DIRECTORY: a string representing the path to the main directory where the data generated is saved

    Returns:: a netcdf file containing the accumulated precip
    
    Generates: A plot (generated in Grads) of the accumulated precipitation

    '''

    os.chdir((MAIN_DIRECTORY+'/TRMMnetcdfCEs'))
    fname = ''
    imgFilename = ''
    firstPartName = ''
    firstTime = True
    replaceExpXDef = ''
    thisNode = ''

    subprocess.call('export DISPLAY=:0.0', shell=True)

    # Generate the file name using MCCTimes
    # if the file name exists, add it to the accTRMM file
    for path in finalMCCList:
        firstTime = True
        for eachNode in path:
            thisNode = mccSearch.this_dict(eachNode)
            fname = 'TRMM' + str(thisNode['cloudElementTime']).replace(' ', '_') + thisNode['uniqueID'] + '.nc'

            if os.path.isfile(fname):
                # Open NetCDF file add info to the accu
                cloudElementTRMMData = Dataset(fname, 'r', format='NETCDF4')
                precipRate = cloudElementTRMMData.variables['precipitation_Accumulation'][:]
                lats = cloudElementTRMMData.variables['latitude'][:]
                lons = cloudElementTRMMData.variables['longitude'][:]
                LONTRMM, LATTRMM = np.meshgrid(lons, lats)
                nygrdTRMM = len(LATTRMM[:, 0])
                nxgrdTRMM = len(LONTRMM[0, :])
                precipRate = ma.masked_array(precipRate, mask=(precipRate < 0.0))
                cloudElementTRMMData.close()

                if firstTime is True:
                    firstPartName = str(thisNode['uniqueID'])+str(thisNode['cloudElementTime']).replace(' ', '_')+'-'
                    accuPrecipRate = ma.zeros(precipRate.shape)
                    firstTime = False

                accuPrecipRate += precipRate

        imgFilename = MAIN_DIRECTORY+'/images/MCS_'+firstPartName+str(thisNode['cloudElementTime']).replace(' ', '_')+'.gif'
        # Create new netCDF file
        accuTRMMFile = MAIN_DIRECTORY+'/TRMMnetcdfCEs/accu'+firstPartName+str(thisNode['cloudElementTime']).replace(' ', '_')+'.nc'
        # Write the file
        accuTRMMData = Dataset(accuTRMMFile, 'w', format='NETCDF4')
        accuTRMMData.description = 'Accumulated precipitation data'
        accuTRMMData.calendar = 'standard'
        accuTRMMData.conventions = 'COARDS'
        # Dimensions
        accuTRMMData.createDimension('time', None)
        accuTRMMData.createDimension('lat', nygrdTRMM)
        accuTRMMData.createDimension('lon', nxgrdTRMM)

        # Variables
        TRMMprecip = ('time', 'lat', 'lon',)
        times = accuTRMMData.createVariable('time', 'f8', ('time',))
        times.units = 'hours since ' + str(thisNode['cloudElementTime']).replace(' ', '_')[:-6]
        latitude = accuTRMMData.createVariable('latitude', 'f8', ('lat',))
        longitude = accuTRMMData.createVariable('longitude', 'f8', ('lon',))
        rainFallacc = accuTRMMData.createVariable('precipitation_Accumulation', 'f8', TRMMprecip)
        rainFallacc.units = 'mm'

        longitude[:] = LONTRMM[0, :]
        longitude.units = 'degrees_east'
        longitude.long_name = 'Longitude'

        latitude[:] = LATTRMM[:, 0]
        latitude.units = 'degrees_north'
        latitude.long_name = 'Latitude'

        rainFallacc[:] = accuPrecipRate[:]

        accuTRMMData.close()

        # Generate the image with GrADS
        subprocess.call('rm acc.ctl', shell=True)
        subprocess.call('touch acc.ctl', shell=True)
        replaceExpDset = 'echo DSET ' + accuTRMMFile + ' >> acc.ctl'
        subprocess.call(replaceExpDset, shell=True)
        subprocess.call('echo "OPTIONS yrev little_endian template" >> acc.ctl', shell=True)
        subprocess.call('echo "DTYPE netcdf" >> acc.ctl', shell=True)
        subprocess.call('echo "UNDEF  0" >> acc.ctl', shell=True)
        subprocess.call('echo "TITLE  TRMM MCS accumulated precipitation" >> acc.ctl', shell=True)
        replaceExpXDef = 'echo XDEF ' + str(nxgrdTRMM) + ' LINEAR ' + str(min(lons)) + ' ' + \
            str((max(lons)-min(lons))/nxgrdTRMM) + ' >> acc.ctl'
        subprocess.call(replaceExpXDef, shell=True)
        replaceExpYDef = 'echo YDEF '+str(nygrdTRMM)+' LINEAR '+str(min(lats)) + ' ' + \
            str((max(lats)-min(lats))/nygrdTRMM)+' >>acc.ctl'
        subprocess.call(replaceExpYDef, shell=True)
        subprocess.call('echo "ZDEF   01 LEVELS 1" >> acc.ctl', shell=True)
        subprocess.call('echo "TDEF 99999 linear 31aug2009 1hr" >> acc.ctl', shell=True)
        subprocess.call('echo "VARS 1" >> acc.ctl', shell=True)
        subprocess.call('echo "precipitation_Accumulation=>precipAcc     1  t,y,x    precipAccu" >> acc.ctl', shell=True)
        subprocess.call('echo "ENDVARS" >> acc.ctl', shell=True)

        # Generate GrADS script
        subprocess.call('rm accuTRMM1.gs', shell=True)
        subprocess.call('touch accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'reinit''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'open acc.ctl ''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'set grads off''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'set mpdset hires''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'set gxout shaded''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'set datawarn off''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'d precipacc''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'draw title TRMM Accumulated Precipitation [mm]''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'run cbarn''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'printim '+imgFilename + ' x1000 y800 white''\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "''\'quit''\'" >> accuTRMM1.gs', shell=True)
        gradscmd = 'grads -blc ' + '\'run accuTRMM1.gs''\''
        subprocess.call(gradscmd, shell=True)

        # Clean up
        subprocess.call('rm accuTRMM1.gs', shell=True)
        subprocess.call('rm acc.ctl', shell=True)

    return
Exemplo n.º 16
0
def display_precip(finalMCCList, MAIN_DIRECTORY):
    '''
    Purpose:: To create a figure showing the precip rate verse time for each MCS

    Inputs:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC
            MAIN_DIRECTORY: a string representing the path to the main directory where the data generated is saved

    Returns:: None

    Generates:: A plot with the precipitation distribution over each feature in the list

    '''
    timeList = []
    oriTimeList = []
    colorBarTime = []
    count = 1
    imgFilename = ''
    percentagePrecipitating = []  #0.0
    cloudElementArea = []
    nodes = []
    xy = []
    x = []
    y = []
    precip = []
    partialArea = []
    totalSize = 0.0
    firstTime = True
    xStart = 0.0
    yStart = 0.0

    # For each node in the list, get the area information from the dictionary
    # in the graph and calculate the area
    if finalMCCList:
        for eachMCC in finalMCCList:
            # Get the info from the node
            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                if firstTime is True:
                    xStart = eachNode['cloudElementCenter'][1]  # lon
                    yStart = eachNode['cloudElementCenter'][0]  # lat
                timeList.append(eachNode['cloudElementTime'])
                percentagePrecipitating.append(
                    (eachNode['TRMMArea'] / eachNode['cloudElementArea']) *
                    100.0)
                cloudElementArea.append(eachNode['cloudElementArea'])
                nodes.append(eachNode['uniqueID'])
                x.append(eachNode['cloudElementCenter'][1])  # -xStart)
                y.append(eachNode['cloudElementCenter'][0])  # -yStart)

                firstTime = False

            # Convert the timeList[] to list of floats
            for i in xrange(len(timeList)):
                colorBarTime.append(time.mktime(timeList[i].timetuple()))

            totalSize = sum(cloudElementArea)
            partialArea = [(a / totalSize) * 30000 for a in cloudElementArea]

            # Plot info
            plt.close('all')

            title = 'Precipitation distribution of the MCS '
            fig, ax = plt.subplots(1, facecolor='white', figsize=(20, 7))

            cmap = cm.jet
            ax.scatter(x,
                       y,
                       s=partialArea,
                       c=colorBarTime,
                       edgecolors='none',
                       marker='o',
                       cmap=cmap)
            colorBarTime = []
            colorBarTime = list(set(timeList))
            colorBarTime.sort()
            cb = colorbar_index(ncolors=len(colorBarTime),
                                nlabels=colorBarTime,
                                cmap=cmap)

            # Axes and labels
            ax.set_xlabel('Degrees Longtude', fontsize=12)
            ax.set_ylabel('Degrees Latitude', fontsize=12)
            ax.set_title(title)
            ax.grid(True)
            plt.subplots_adjust(bottom=0.2)

            for i, txt in enumerate(nodes):
                if cloudElementArea[i] >= 2400.00:
                    ax.annotate('%d' % percentagePrecipitating[i] + '%',
                                (x[i], y[i]))
                precip = []

            imgFilename = MAIN_DIRECTORY + '/images/MCSprecip' + str(
                count) + '.gif'
            plt.savefig(imgFilename,
                        facecolor=fig.get_facecolor(),
                        transparent=True)

            # Reset for next image
            timeList = []
            percentagePrecipitating = []
            cloudElementArea = []
            x = []
            y = []
            colorBarTime = []
            nodes = []
            precip = []
            count += 1
            firstTime = True
    return
Exemplo n.º 17
0
def plot_accu_TRMM(finalMCCList, MAIN_DIRECTORY):
    '''
    Purpose:: (1) generate a file with the accumulated precipitation for the MCS (2) generate the appropriate image
            
    Input:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC
            MAIN_DIRECTORY: a string representing the path to the main directory where the data generated is saved

    Returns:: a netcdf file containing the accumulated precip
    
    Generates: A plot (generated in Grads) of the accumulated precipitation

    '''

    os.chdir((MAIN_DIRECTORY + '/TRMMnetcdfCEs'))
    fname = ''
    imgFilename = ''
    firstPartName = ''
    firstTime = True
    replaceExpXDef = ''
    thisNode = ''

    subprocess.call('export DISPLAY=:0.0', shell=True)

    # Generate the file name using MCCTimes
    # if the file name exists, add it to the accTRMM file
    for path in finalMCCList:
        firstTime = True
        for eachNode in path:
            thisNode = mccSearch.this_dict(eachNode)
            fname = 'TRMM' + str(thisNode['cloudElementTime']).replace(
                ' ', '_') + thisNode['uniqueID'] + '.nc'

            if os.path.isfile(fname):
                # Open NetCDF file add info to the accu
                cloudElementTRMMData = Dataset(fname, 'r', format='NETCDF4')
                precipRate = cloudElementTRMMData.variables[
                    'precipitation_Accumulation'][:]
                lats = cloudElementTRMMData.variables['latitude'][:]
                lons = cloudElementTRMMData.variables['longitude'][:]
                LONTRMM, LATTRMM = np.meshgrid(lons, lats)
                nygrdTRMM = len(LATTRMM[:, 0])
                nxgrdTRMM = len(LONTRMM[0, :])
                precipRate = ma.masked_array(precipRate,
                                             mask=(precipRate < 0.0))
                cloudElementTRMMData.close()

                if firstTime is True:
                    firstPartName = str(thisNode['uniqueID']) + str(
                        thisNode['cloudElementTime']).replace(' ', '_') + '-'
                    accuPrecipRate = ma.zeros(precipRate.shape)
                    firstTime = False

                accuPrecipRate += precipRate

        imgFilename = MAIN_DIRECTORY + '/images/MCS_' + firstPartName + str(
            thisNode['cloudElementTime']).replace(' ', '_') + '.gif'
        # Create new netCDF file
        accuTRMMFile = MAIN_DIRECTORY + '/TRMMnetcdfCEs/accu' + firstPartName + str(
            thisNode['cloudElementTime']).replace(' ', '_') + '.nc'
        # Write the file
        accuTRMMData = Dataset(accuTRMMFile, 'w', format='NETCDF4')
        accuTRMMData.description = 'Accumulated precipitation data'
        accuTRMMData.calendar = 'standard'
        accuTRMMData.conventions = 'COARDS'
        # Dimensions
        accuTRMMData.createDimension('time', None)
        accuTRMMData.createDimension('lat', nygrdTRMM)
        accuTRMMData.createDimension('lon', nxgrdTRMM)

        # Variables
        TRMMprecip = (
            'time',
            'lat',
            'lon',
        )
        times = accuTRMMData.createVariable('time', 'f8', ('time', ))
        times.units = 'hours since ' + str(
            thisNode['cloudElementTime']).replace(' ', '_')[:-6]
        latitude = accuTRMMData.createVariable('latitude', 'f8', ('lat', ))
        longitude = accuTRMMData.createVariable('longitude', 'f8', ('lon', ))
        rainFallacc = accuTRMMData.createVariable('precipitation_Accumulation',
                                                  'f8', TRMMprecip)
        rainFallacc.units = 'mm'

        longitude[:] = LONTRMM[0, :]
        longitude.units = 'degrees_east'
        longitude.long_name = 'Longitude'

        latitude[:] = LATTRMM[:, 0]
        latitude.units = 'degrees_north'
        latitude.long_name = 'Latitude'

        rainFallacc[:] = accuPrecipRate[:]

        accuTRMMData.close()

        # Generate the image with GrADS
        subprocess.call('rm acc.ctl', shell=True)
        subprocess.call('touch acc.ctl', shell=True)
        replaceExpDset = 'echo DSET ' + accuTRMMFile + ' >> acc.ctl'
        subprocess.call(replaceExpDset, shell=True)
        subprocess.call(
            'echo "OPTIONS yrev little_endian template" >> acc.ctl',
            shell=True)
        subprocess.call('echo "DTYPE netcdf" >> acc.ctl', shell=True)
        subprocess.call('echo "UNDEF  0" >> acc.ctl', shell=True)
        subprocess.call(
            'echo "TITLE  TRMM MCS accumulated precipitation" >> acc.ctl',
            shell=True)
        replaceExpXDef = 'echo XDEF ' + str(nxgrdTRMM) + ' LINEAR ' + str(min(lons)) + ' ' + \
            str((max(lons)-min(lons))/nxgrdTRMM) + ' >> acc.ctl'
        subprocess.call(replaceExpXDef, shell=True)
        replaceExpYDef = 'echo YDEF '+str(nygrdTRMM)+' LINEAR '+str(min(lats)) + ' ' + \
            str((max(lats)-min(lats))/nygrdTRMM)+' >>acc.ctl'
        subprocess.call(replaceExpYDef, shell=True)
        subprocess.call('echo "ZDEF   01 LEVELS 1" >> acc.ctl', shell=True)
        subprocess.call('echo "TDEF 99999 linear 31aug2009 1hr" >> acc.ctl',
                        shell=True)
        subprocess.call('echo "VARS 1" >> acc.ctl', shell=True)
        subprocess.call(
            'echo "precipitation_Accumulation=>precipAcc     1  t,y,x    precipAccu" >> acc.ctl',
            shell=True)
        subprocess.call('echo "ENDVARS" >> acc.ctl', shell=True)

        # Generate GrADS script
        subprocess.call('rm accuTRMM1.gs', shell=True)
        subprocess.call('touch accuTRMM1.gs', shell=True)
        subprocess.call('echo "' '\'reinit' '\'" >> accuTRMM1.gs', shell=True)
        subprocess.call('echo "'
                        '\'open acc.ctl '
                        '\'" >> accuTRMM1.gs',
                        shell=True)
        subprocess.call('echo "'
                        '\'set grads off'
                        '\'" >> accuTRMM1.gs',
                        shell=True)
        subprocess.call('echo "'
                        '\'set mpdset hires'
                        '\'" >> accuTRMM1.gs',
                        shell=True)
        subprocess.call('echo "'
                        '\'set gxout shaded'
                        '\'" >> accuTRMM1.gs',
                        shell=True)
        subprocess.call('echo "'
                        '\'set datawarn off'
                        '\'" >> accuTRMM1.gs',
                        shell=True)
        subprocess.call('echo "'
                        '\'d precipacc'
                        '\'" >> accuTRMM1.gs',
                        shell=True)
        subprocess.call(
            'echo "'
            '\'draw title TRMM Accumulated Precipitation [mm]'
            '\'" >> accuTRMM1.gs',
            shell=True)
        subprocess.call('echo "'
                        '\'run cbarn'
                        '\'" >> accuTRMM1.gs',
                        shell=True)
        subprocess.call('echo "'
                        '\'printim ' + imgFilename + ' x1000 y800 white'
                        '\'" >> accuTRMM1.gs',
                        shell=True)
        subprocess.call('echo "' '\'quit' '\'" >> accuTRMM1.gs', shell=True)
        gradscmd = 'grads -blc ' + '\'run accuTRMM1.gs' '\''
        subprocess.call(gradscmd, shell=True)

        # Clean up
        subprocess.call('rm accuTRMM1.gs', shell=True)
        subprocess.call('rm acc.ctl', shell=True)

    return
Exemplo n.º 18
0
def plot_precip_histograms(finalMCCList, MAIN_DIRECTORY):
    '''
    Purpose:: To create plots (histograms) of the each TRMMnetcdfCEs files

    Input:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC
        MAIN_DIRECTORY: a string representing the path to the main directory where the data generated is saved

    Returns:: None

    Generates:: Histogram plots for each feature in the list passed
        
    '''
    numBins = 5
    precip = []
    imgFilename = ' '
    startTime = ' '
    firstTime = True

    if finalMCCList:

        for eachMCC in finalMCCList:
            firstTime = True

            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                thisTime = eachNode['cloudElementTime']

                thisFileName = MAIN_DIRECTORY+'/TRMMnetcdfCEs/TRMM' + str(thisTime).replace(' ', '_') + \
                    eachNode['uniqueID'] + '.nc'
                TRMMData = Dataset(thisFileName, 'r', format='NETCDF4')
                precipRate = TRMMData.variables[
                    'precipitation_Accumulation'][:, :, :]
                cloudElementPrecipRate = precipRate[0, :, :]
                TRMMData.close()

                if firstTime is True:
                    totalPrecip = np.zeros(cloudElementPrecipRate.shape)
                    startTime = str(thisTime) + eachNode['uniqueID']
                    firstTime = False

                totalPrecip = np.add(totalPrecip, precipRate)

                for _, value in np.ndenumerate(cloudElementPrecipRate):
                    if value != 0.0:
                        precip.append(value)

                plt.close('all')
                title = 'TRMM precipitation distribution for ' + str(thisTime)
                imgFilename = MAIN_DIRECTORY + '/images/' + str(
                    thisTime) + eachNode['uniqueID'] + 'TRMMMCS.gif'
                xlabel = 'Precipitation [mm]'
                ylabel = 'Area [km^2]'
                numBins = 5
                if all(value == 0 for value in precip):
                    print 'NO Precipitation at ' + str(
                        thisTime) + eachNode['uniqueID']
                else:
                    plot_histogram(precip, title, imgFilename, xlabel, ylabel,
                                   numBins)

                precip = []

            # The image at the end of each MCS
            title = 'TRMM precipitation distribution for ' + startTime + ' to ' + str(
                thisTime)
            imgFilename = MAIN_DIRECTORY + '/images/' + startTime + '_' + str(
                thisTime) + eachNode['uniqueID'] + 'TRMMMCS.gif'
            xlabel = 'Precipitation [mm]'
            ylabel = 'Area [km^2]'
            numBins = 10
            for _, value in np.ndenumerate(totalPrecip):
                if value != 0.0:
                    precip.append(value)

            if all(value == 0 for value in precip):
                print 'No precipitation for MCS starting at ' + startTime + ' and ending at ' + str(
                    thisTime) + eachNode['uniqueID']
            else:
                plot_histogram(precip, title, imgFilename, xlabel, ylabel,
                               numBins)

            precip = []

    return
Exemplo n.º 19
0
def display_precip(finalMCCList, MAIN_DIRECTORY):
    '''
    Purpose:: To create a figure showing the precip rate verse time for each MCS

    Inputs:: finalMCCList: a list of dictionaries representing a list of nodes representing a MCC
            MAIN_DIRECTORY: a string representing the path to the main directory where the data generated is saved

    Returns:: None

    Generates:: A plot with the precipitation distribution over each feature in the list

    '''
    timeList = []
    oriTimeList = []
    colorBarTime = []
    count = 1
    imgFilename = ''
    percentagePrecipitating = []  #0.0
    cloudElementArea = []
    nodes = []
    xy = []
    x = []
    y = []
    precip = []
    partialArea = []
    totalSize = 0.0
    firstTime = True
    xStart = 0.0
    yStart = 0.0

    # For each node in the list, get the area information from the dictionary
    # in the graph and calculate the area
    if finalMCCList:
        for eachMCC in finalMCCList:
            # Get the info from the node
            for node in eachMCC:
                eachNode = mccSearch.this_dict(node)
                if firstTime is True:
                    xStart = eachNode['cloudElementCenter'][1]  # lon
                    yStart = eachNode['cloudElementCenter'][0]  # lat
                timeList.append(eachNode['cloudElementTime'])
                percentagePrecipitating.append((eachNode['TRMMArea'] / eachNode['cloudElementArea']) * 100.0)
                cloudElementArea.append(eachNode['cloudElementArea'])
                nodes.append(eachNode['uniqueID'])
                x.append(eachNode['cloudElementCenter'][1])  # -xStart)
                y.append(eachNode['cloudElementCenter'][0])  # -yStart)

                firstTime = False

            # Convert the timeList[] to list of floats
            for i in xrange(len(timeList)):
                colorBarTime.append(time.mktime(timeList[i].timetuple()))

            totalSize = sum(cloudElementArea)
            partialArea = [(a/totalSize)*30000 for a in cloudElementArea]

            # Plot info
            plt.close('all')

            title = 'Precipitation distribution of the MCS '
            fig, ax = plt.subplots(1, facecolor='white', figsize=(20, 7))

            cmap = cm.jet
            ax.scatter(x, y, s=partialArea, c=colorBarTime, edgecolors='none', marker='o', cmap=cmap)
            colorBarTime = []
            colorBarTime = list(set(timeList))
            colorBarTime.sort()
            cb = colorbar_index(ncolors=len(colorBarTime), nlabels=colorBarTime, cmap=cmap)

            # Axes and labels
            ax.set_xlabel('Degrees Longtude', fontsize=12)
            ax.set_ylabel('Degrees Latitude', fontsize=12)
            ax.set_title(title)
            ax.grid(True)
            plt.subplots_adjust(bottom=0.2)

            for i, txt in enumerate(nodes):
                if cloudElementArea[i] >= 2400.00:
                    ax.annotate('%d' % percentagePrecipitating[i]+'%', (x[i], y[i]))
                precip = []

            imgFilename = MAIN_DIRECTORY+'/images/MCSprecip' + str(count)+'.gif'
            plt.savefig(imgFilename, facecolor=fig.get_facecolor(), transparent=True)

            # Reset for next image
            timeList = []
            percentagePrecipitating = []
            cloudElementArea = []
            x = []
            y = []
            colorBarTime = []
            nodes = []
            precip = []
            count += 1
            firstTime = True
    return
Exemplo n.º 20
0
def create_text_file(finalMCCList, identifier, userVariables, graphVariables):
    '''
    Purpose::
        Create a text file with information about the MCS
        This function is expected to be especially of use regarding long term record checks

    Inputs::
        finalMCCList: a list of dictionaries representing a list of nodes representing a MCC
        identifier: an integer representing the type of list that has been entered...this is for creating file purposes
            1 - MCCList; 2- mcsList

    Returns:: None

    Generates::
        mcsUserFile: a user readable text file with all information about each MCS
        mcsSummaryFile: a user readable text file with the summary of the MCS
        mcsPostFile: a user readable text file with each MCS identified

    Assumptions::

    TODOs: provide visualizations for the calculations used to create the textFile
    '''

    durations = 0.0
    startTimes = []
    endTimes = []
    averagePropagationSpeed = 0.0
    speedCounter = 0
    maxArea = 0.0
    amax = 0.0
    avgMaxArea = []
    maxAreaCounter = 0.0
    maxAreaTime = ''
    eccentricity = 0.0
    firstTime = True
    matureFlag = True
    timeMCSMatures = ''
    maxCEprecipRate = 0.0
    minCEprecipRate = 0.0
    averageArea = 0.0
    averageAreaCounter = 0
    durationOfMatureMCC = 0
    avgMaxPrecipRate = 0.0
    avgMaxPrecipRateCounter = 0
    avgMinPrecipRate = 0.0
    avgMinPrecipRateCounter = 0
    cloudElementSpeed = 0.0
    mcsSpeed = 0.0
    mcsSpeedCounter = 0
    mcsPrecipTotal = 0.0
    avgmcsPrecipTotalCounter = 0
    bigPtotal = 0.0
    bigPtotalCounter = 0
    allPropagationSpeeds = []
    averageAreas = []
    areaAvg = 0.0
    avgPrecipTotal = 0.0
    avgPrecipTotalCounter = 0
    avgMaxmcsPrecipRate = 0.0
    avgMaxmcsPrecipRateCounter = 0
    avgMinmcsPrecipRate = 0.0
    avgMinmcsPrecipRateCounter = 0
    avgPrecipArea = []
    location = []
    avgPrecipAreaPercent = 0.0
    precipArea = 0.0
    precipAreaPercent = 0.0
    precipPercent = []
    precipCounter = 0
    precipAreaAvg = 0.0
    minSpeed = 0.0
    maxSpeed = 0.0

    if identifier == 1:
        mcsUserFile = open(
            (userVariables.DIRS['mainDirStr'] + '/textFiles/MCCsUserFile.txt'),
            'wb')
        mcsSummaryFile = open(
            (userVariables.DIRS['mainDirStr'] + '/textFiles/MCCSummary.txt'),
            'wb')
        mcsPostFile = open((userVariables.DIRS['mainDirStr'] +
                            '/textFiles/MCCPostPrecessing.txt'), 'wb')

    if identifier == 2:
        mcsUserFile = open(
            (userVariables.DIRS['mainDirStr'] + '/textFiles/MCSsUserFile.txt'),
            'wb')
        mcsSummaryFile = open(
            (userVariables.DIRS['mainDirStr'] + '/textFiles/MCSSummary.txt'),
            'wb')
        mcsPostFile = open((userVariables.DIRS['mainDirStr'] +
                            '/textFiles/MCSPostPrecessing.txt'), 'wb')

    for eachPath in finalMCCList:
        eachPath.sort(key=lambda nodeID: (len(nodeID.split('C')[
            0]), nodeID.split('C')[0], nodeID.split('CE')[1]))
        mcsPostFile.write('\n %s' % eachPath)

        startTime = mccSearch.this_dict(eachPath[0],
                                        graphVariables)['cloudElementTime']
        endTime = mccSearch.this_dict(eachPath[-1],
                                      graphVariables)['cloudElementTime']
        duration = (endTime - startTime) + timedelta(hours=userVariables.TRES)

        # Convert datatime duration to seconds and add to the total for the average duration of all MCS in finalMCCList
        durations += (duration.total_seconds())

        #durations += duration
        startTimes.append(startTime)
        endTimes.append(endTime)

        # Get the precip info
        for eachNode in eachPath:
            thisNode = mccSearch.this_dict(eachNode, graphVariables)
            # Set first time min 'fake' values
            if firstTime is True:
                minCEprecipRate = thisNode['CETRMMmin']
                avgMinmcsPrecipRate += thisNode['CETRMMmin']
                firstTime = False
            # Calculate the speed
            # if thisNode['cloudElementArea'] >= OUTER_CLOUD_SHIELD_AREA:
            #   averagePropagationSpeed += find_cloud_element_speed(eachNode, eachPath)
            #   speedCounter +=1

            # Amax: find max area
            if thisNode['cloudElementArea'] > maxArea:
                maxArea = thisNode['cloudElementArea']
                maxAreaTime = str(thisNode['cloudElementTime'])
                eccentricity = thisNode['cloudElementEccentricity']
                location = thisNode['cloudElementCenter']

                # Determine the time the feature matures
                if matureFlag is True:
                    timeMCSMatures = str(thisNode['cloudElementTime'])
                    matureFlag = False

            # Find min and max precip rate
            if thisNode['CETRMMmin'] < minCEprecipRate:
                minCEprecipRate = thisNode['CETRMMmin']

            if thisNode['CETRMMmax'] > maxCEprecipRate:
                maxCEprecipRate = thisNode['CETRMMmax']

            # If MCC, then calculate for only mature phase else, calculate for full MCS
            if identifier == 1:
                # Calculations for only the mature stage
                try:
                    if thisNode['nodeMCSIdentifier'] == 'M':
                        # Calculate average area of the maturity feature only
                        averageArea += thisNode['cloudElementArea']
                        averageAreaCounter += 1
                        durationOfMatureMCC += 1
                        avgMaxPrecipRate += thisNode['CETRMMmax']
                        avgMaxPrecipRateCounter += 1
                        avgMinPrecipRate += thisNode['CETRMMmin']
                        avgMinPrecipRateCounter += 1
                        avgMaxmcsPrecipRate += thisNode['CETRMMmax']
                        avgMaxmcsPrecipRateCounter += 1
                        avgMinmcsPrecipRate += thisNode['CETRMMmin']
                        avgMinmcsPrecipRateCounter += 1

                        # The precip percentage (TRMM area/CE area)
                        if thisNode['cloudElementArea'] >= 0.0 and thisNode[
                                'TRMMArea'] >= 0.0:
                            precipArea += thisNode['TRMMArea']
                            avgPrecipArea.append(thisNode['TRMMArea'])
                            avgPrecipAreaPercent += (
                                thisNode['TRMMArea'] /
                                thisNode['cloudElementArea'])
                            precipPercent.append(
                                (thisNode['TRMMArea'] /
                                 thisNode['cloudElementArea']))
                            precipCounter += 1

                        # System speed for only mature stage
                        # cloudElementSpeed = find_cloud_element_speed(eachNode,eachPath)
                        # if cloudElementSpeed > 0.0 :
                        #   mcsSpeed += cloudElementSpeed
                        #   mcsSpeedCounter += 1
                except:
                    print 'MCS node has no nodeMCSIdentifier ', thisNode[
                        'uniqueID']
                    avgMaxmcsPrecipRate += thisNode['CETRMMmax']
                    avgMaxmcsPrecipRateCounter += 1
                    avgMinmcsPrecipRate += thisNode['CETRMMmin']
                    avgMinmcsPrecipRateCounter += 1
            else:
                averageArea += thisNode['cloudElementArea']
                averageAreaCounter += 1
                durationOfMatureMCC += 1
                avgMaxPrecipRate += thisNode['CETRMMmax']
                avgMaxPrecipRateCounter += 1
                avgMinPrecipRate += thisNode['CETRMMmin']
                avgMinPrecipRateCounter += 1
                avgMaxmcsPrecipRate += thisNode['CETRMMmax']
                avgMaxmcsPrecipRateCounter += 1
                avgMinmcsPrecipRate += thisNode['CETRMMmin']
                avgMinmcsPrecipRateCounter += 1

            # The precip percentage (TRMM area/CE area)
            if thisNode['cloudElementArea'] >= 0.0 and thisNode[
                    'TRMMArea'] >= 0.0:
                precipArea += thisNode['TRMMArea']
                avgPrecipArea.append(thisNode['TRMMArea'])
                avgPrecipAreaPercent += (thisNode['TRMMArea'] /
                                         thisNode['cloudElementArea'])
                precipPercent.append(
                    (thisNode['TRMMArea'] / thisNode['cloudElementArea']))
                precipCounter += 1

            # System speed for only mature stage
            # cloudElementSpeed = find_cloud_element_speed(eachNode,eachPath)
            # if cloudElementSpeed > 0.0 :
            #   mcsSpeed += cloudElementSpeed
            #   mcsSpeedCounter += 1

            # Find accumulated precip
            if thisNode['cloudElementPrecipTotal'] > 0.0:
                mcsPrecipTotal += thisNode['cloudElementPrecipTotal']
                avgmcsPrecipTotalCounter += 1

        # A: calculate the average Area of the (mature) MCS
        if averageAreaCounter > 0:  # and averageAreaCounter > 0:
            averageArea /= averageAreaCounter
            averageAreas.append(averageArea)

        # V: MCS speed
        if mcsSpeedCounter > 0:  # and mcsSpeed > 0.0:
            mcsSpeed /= mcsSpeedCounter

        # smallP_max: calculate the average max precip rate (mm/h)
        if avgMaxmcsPrecipRateCounter > 0:  # and avgMaxPrecipRate > 0.0:
            avgMaxmcsPrecipRate /= avgMaxmcsPrecipRateCounter

        # smallP_min: calculate the average min precip rate (mm/h)
        if avgMinmcsPrecipRateCounter > 0:  # and avgMinPrecipRate > 0.0:
            avgMinmcsPrecipRate /= avgMinmcsPrecipRateCounter

        # smallP_avg: calculate the average precipitation (mm hr-1)
        if mcsPrecipTotal > 0.0:  # and avgmcsPrecipTotalCounter> 0:
            avgmcsPrecipTotal = mcsPrecipTotal / avgmcsPrecipTotalCounter
            avgPrecipTotal += avgmcsPrecipTotal
            avgPrecipTotalCounter += 1

        #smallP_total = mcsPrecipTotal
        # Precip over the MCS lifetime prep for bigP_total
        if mcsPrecipTotal > 0.0:
            bigPtotal += mcsPrecipTotal
            bigPtotalCounter += 1

        if maxArea > 0.0:
            avgMaxArea.append(maxArea)
            maxAreaCounter += 1

        # Average precipate area percentage (TRMM/CE area)
        if precipCounter > 0:
            avgPrecipAreaPercent /= precipCounter
            precipArea /= precipCounter

        # Write stuff to file
        mcsUserFile.write('\n\n\nStarttime is: %s ' % (str(startTime)))
        mcsUserFile.write('\nEndtime is: %s ' % (str(endTime)))
        mcsUserFile.write('\nLife duration is %s hrs' % (str(duration)))
        mcsUserFile.write('\nTime of maturity is %s ' % (timeMCSMatures))
        mcsUserFile.write('\nDuration mature stage is: %s ' %
                          durationOfMatureMCC * userVariables.TRES)
        mcsUserFile.write('\nAverage area is: %.4f km^2 ' % (averageArea))
        mcsUserFile.write('\nMax area is: %.4f km^2 ' % (maxArea))
        mcsUserFile.write('\nMax area time is: %s ' % (maxAreaTime))
        mcsUserFile.write('\nEccentricity at max area is: %.4f ' %
                          (eccentricity))
        mcsUserFile.write('\nCenter (lat,lon) at max area is: %.2f\t%.2f' %
                          (location[0], location[1]))
        mcsUserFile.write('\nPropagation speed is %.4f ' % (mcsSpeed))
        mcsUserFile.write('\nMCS minimum precip rate is %.4f mmh^-1' %
                          (minCEprecipRate))
        mcsUserFile.write('\nMCS maximum precip rate is %.4f mmh^-1' %
                          (maxCEprecipRate))
        mcsUserFile.write(
            '\nTotal precipitation during MCS is %.4f mm/lifetime' %
            (mcsPrecipTotal))
        mcsUserFile.write('\nAverage MCS precipitation is %.4f mm' %
                          (avgmcsPrecipTotal))
        mcsUserFile.write(
            '\nAverage MCS maximum precipitation is %.4f mmh^-1' %
            (avgMaxmcsPrecipRate))
        mcsUserFile.write(
            '\nAverage MCS minimum precipitation is %.4f mmh^-1' %
            (avgMinmcsPrecipRate))
        mcsUserFile.write('\nAverage precipitation area is %.4f km^2 ' %
                          (precipArea))
        mcsUserFile.write(
            '\nPrecipitation area percentage of mature system %.4f percent ' %
            (avgPrecipAreaPercent * 100))

        # Append stuff to lists for the summary file
        if mcsSpeed > 0.0:
            allPropagationSpeeds.append(mcsSpeed)
            averagePropagationSpeed += mcsSpeed
            speedCounter += 1

        # Reset vars for next MCS in list
        averageArea = 0.0
        averageAreaCounter = 0
        durationOfMatureMCC = 0
        mcsSpeed = 0.0
        mcsSpeedCounter = 0
        mcsPrecipTotal = 0.0
        avgMaxmcsPrecipRate = 0.0
        avgMaxmcsPrecipRateCounter = 0
        avgMinmcsPrecipRate = 0.0
        avgMinmcsPrecipRateCounter = 0
        firstTime = True
        matureFlag = True
        avgmcsPrecipTotalCounter = 0
        avgPrecipAreaPercent = 0.0
        precipArea = 0.0
        precipCounter = 0
        maxArea = 0.0
        maxAreaTime = ''
        eccentricity = 0.0
        timeMCSMatures = ''
        maxCEprecipRate = 0.0
        minCEprecipRate = 0.0
        location = []

    # LD: average duration
    if len(finalMCCList) > 1:
        durations /= len(finalMCCList)
        durations /= 3600.0  # Convert to hours

        # A: average area
        areaAvg = sum(averageAreas) / len(finalMCCList)
    # Create histogram plot here
    # if len(averageAreas) > 1:
    #   plotHistogram(averageAreas, 'Average Area [km^2]', 'Area [km^2]')

    # Amax: average maximum area
    if maxAreaCounter > 0.0:  # and avgMaxArea > 0.0 :
        amax = sum(avgMaxArea) / maxAreaCounter
        # Create histogram plot here
        #if len(avgMaxArea) > 1:
        #   plotHistogram(avgMaxArea, 'Maximum Area [km^2]', 'Area [km^2]')

    # v_avg: calculate the average propagation speed
    if speedCounter > 0:  # and averagePropagationSpeed > 0.0
        averagePropagationSpeed /= speedCounter

    # bigP_min: calculate the min rate in mature system
    if avgMinPrecipRate > 0.0:  # and avgMinPrecipRateCounter > 0.0:
        avgMinPrecipRate /= avgMinPrecipRateCounter

    # bigP_max: calculate the max rate in mature system
    if avgMinPrecipRateCounter > 0.0:  # and avgMaxPrecipRate >  0.0:
        avgMaxPrecipRate /= avgMaxPrecipRateCounter

    # bigP_avg: average total preicip rate mm/hr
    if avgPrecipTotalCounter > 0.0:  # and avgPrecipTotal > 0.0:
        avgPrecipTotal /= avgPrecipTotalCounter

    # bigP_total: total precip rate mm/LD
    if bigPtotalCounter > 0.0:  # and bigPtotal > 0.0:
        bigPtotal /= bigPtotalCounter

    # Precipitation area percentage
    if len(precipPercent) > 0:
        precipAreaPercent = (sum(precipPercent) / len(precipPercent)) * 100.0

    # average precipitation area
    if len(avgPrecipArea) > 0:
        precipAreaAvg = sum(avgPrecipArea) / len(avgPrecipArea)
        #if len(avgPrecipArea) > 1:
        #   plotHistogram(avgPrecipArea, 'Average Rainfall Area [km^2]', 'Area [km^2]')

    sTime = str(average_time(startTimes))
    eTime = str(average_time(endTimes))
    if len(allPropagationSpeeds) > 1:
        maxSpeed = max(allPropagationSpeeds)
        minSpeed = min(allPropagationSpeeds)

    # Write stuff to the summary file
    mcsSummaryFile.write('\nNumber of features is %d ' % (len(finalMCCList)))
    mcsSummaryFile.write('\nAverage duration is %.4f hrs ' % (durations))
    mcsSummaryFile.write('\nAverage startTime is %s ' % (sTime[-8:]))
    mcsSummaryFile.write('\nAverage endTime is %s ' % (eTime[-8:]))
    mcsSummaryFile.write('\nAverage size is %.4f km^2 ' % (areaAvg))
    mcsSummaryFile.write('\nAverage precipitation area is %.4f km^2 ' %
                         (precipAreaAvg))
    mcsSummaryFile.write('\nAverage maximum size is %.4f km^2 ' % (amax))
    mcsSummaryFile.write('\nAverage propagation speed is %.4f ms^-1' %
                         (averagePropagationSpeed))
    mcsSummaryFile.write('\nMaximum propagation speed is %.4f ms^-1 ' %
                         (maxSpeed))
    mcsSummaryFile.write('\nMinimum propagation speed is %.4f ms^-1 ' %
                         (minSpeed))
    mcsSummaryFile.write(
        '\nAverage minimum precipitation rate is %.4f mmh^-1' %
        (avgMinPrecipRate))
    mcsSummaryFile.write(
        '\nAverage maximum precipitation rate is %.4f mm h^-1' %
        (avgMaxPrecipRate))
    mcsSummaryFile.write('\nAverage precipitation is %.4f mm ' %
                         (avgPrecipTotal))
    mcsSummaryFile.write(
        '\nAverage total precipitation during MCSs is %.4f mm/LD ' %
        (bigPtotal))
    mcsSummaryFile.write(
        '\nAverage precipitation area percentage is %.4f percent ' %
        (precipAreaPercent))

    mcsUserFile.close()
    mcsSummaryFile.close()
    mcsPostFile.close()
    return