Example #1
0
def DTR_calculation(arr1, arr2, fill_val1=None, fill_val2=None):
    
    '''    
    Calculates the DTR indice: mean of daily temperature range.
    
    :param arr1: daily max temperature (e.g. "tasmax")
    :type arr1: numpy.ndarray (3D) or numpy.ma.MaskedArray (3D)
    :param arr2: daily min temperature (e.g. "tasmin")
    :type arr2: numpy.ndarray (3D) or numpy.ma.MaskedArray (3D) 
    
    :param fill_val1: fill value of arr1 
    :type fill_val1: float
    :param fill_val2: fill value of arr2 
    :type fill_val2: float
    
    :rtype: numpy.ndarray (2D)        (if "arr1" and "arr2" are numpy.ndarray)
         or numpy.ma.MaskedArray (2D) (if "arr1" and "arr2" are numpy.ma.MaskedArray)

    .. warning:: "arr1" and "arr2" must be the same type, shape and correspond to the same time step vector.
    
    '''
    #.. warning:: If "arr1" and "arr2" are masked arrays, the parameters "fill_val1" and "fill_val2" are ignored, because they have no sense in this case.
      
    arr1_masked = calc.get_masked_arr(arr1, fill_val1)
    arr2_masked = calc.get_masked_arr(arr2, fill_val2)
    
    range_ = arr1_masked - arr2_masked                              # masked array with fill_value = fill_value of the first masked array in expression (i.e. arr1_masked)
    DTR = range_.mean(axis=0)                                       # masked array with new fill_value
    numpy.ma.set_fill_value(DTR, arr1_masked.fill_value)            # we set a fill_value = fill_value of arr1_masked (or arr2_masked) 
    
    if not isinstance(arr1, numpy.ma.MaskedArray) :     #�or if not isinstance(arr2, numpy.ma.MaskedArray) [because the both input arrays are the same type]
        DTR = DTR.filled(fill_value=arr1_masked.fill_value)      
    
    return DTR    
Example #2
0
def GD4_calculation(arr, fill_val=None, threshold=4):
    """
    Calculates the GD4 indice: growing degree days (sum of daily mean temperature > 4 degrees Celsius).
    
    :param arr: daily mean temperature (e.g. "tas") in Kelvin
    :type arr: numpy.ndarray (3D) or numpy.ma.MaskedArray (3D)
    :param fill_val: fill value 
    :type fill_val: float
    
    :rtype: numpy.ndarray (2D)        (if "arr" is numpy.ndarray)
         or numpy.ma.MaskedArray (2D) (if "arr" is numpy.ma.MaskedArray)
         
    .. warning:: Units of "arr" must be Kelvin.
       
    .. warning:: If "arr" is a masked array, the parameter "fill_val" is ignored, because it has no sense in this case.
    """

    T = threshold + 273.15  # Celsius -> Kelvin

    arr_masked = calc.get_masked_arr(arr, fill_val)

    new_mask = arr_masked <= T
    new_arr_masked = numpy.ma.array(
        arr_masked, mask=new_mask, fill_value=arr_masked.fill_value
    )  # we masked the temperatures <= 4 C
    GD4 = new_arr_masked.sum(axis=0)
    numpy.ma.set_fill_value(GD4, arr_masked.fill_value)

    if not isinstance(arr, numpy.ma.MaskedArray):
        GD4 = GD4.filled(fill_value=arr_masked.fill_value)

    return GD4
Example #3
0
def HD17_calculation(arr, fill_val=None, threshold=17):
    """
    Calculates the HD17 indice: heating degree days (sum of (17 degrees Celsius - daily mean temperature)).
    
    :param arr: daily mean temperature (e.g. "tas") in Kelvin
    :type arr: numpy.ndarray (3D) or numpy.ma.MaskedArray (3D)
    :param fill_val: fill value 
    :type fill_val: float
    
    :rtype: numpy.ndarray (2D)        (if "arr" is numpy.ndarray)
         or numpy.ma.MaskedArray (2D) (if "arr" is numpy.ma.MaskedArray)
         
    .. warning:: Units of "arr" must be Kelvin.
       
    .. warning:: If "arr" is a masked array, the parameter "fill_val" is ignored, because it has no sense in this case.
    """

    T = threshold + 273.15  # Celsius -> Kelvin

    arr_masked = calc.get_masked_arr(arr, fill_val)

    a = T - arr_masked
    a[a < 0] = 0  # we set to zero values < 0
    HD17 = a.sum(axis=0)
    numpy.ma.set_fill_value(HD17, arr_masked.fill_value)

    if not isinstance(arr, numpy.ma.MaskedArray):
        HD17 = HD17.filled(fill_value=arr_masked.fill_value)

    return HD17
Example #4
0
def vDTR_calculation(arr1, arr2, fill_val1=None, fill_val2=None):

    """    
    Calculates the vDTR indice: mean absolute day-to-day difference in DTR.
    
    :param arr1: daily max temperature (e.g. "tasmax")
    :type arr1: numpy.ndarray (3D) or numpy.ma.MaskedArray (3D)
    :param arr2: daily min temperature (e.g. "tasmin")
    :type arr2: numpy.ndarray (3D) or numpy.ma.MaskedArray (3D) 
    
    :param fill_val1: fill value of arr1 
    :type fill_val1: float
    :param fill_val2: fill value of arr2 
    :type fill_val2: float
    
    :rtype: numpy.ndarray (2D)        (if "arr1" and "arr2" are numpy.ndarray)
         or numpy.ma.MaskedArray (2D) (if "arr1" and "arr2" are numpy.ma.MaskedArray)
         
    .. warning:: If "arr1" and "arr2" are masked arrays, the parameters "fill_val1" and "fill_val2" are ignored, because they have no sense in this case.
    
    .. warning:: "arr1" and "arr2" must be the same type, shape and correspond to the same time step vector.
    
    """

    arr1_masked = calc.get_masked_arr(arr1, fill_val1)
    arr2_masked = calc.get_masked_arr(arr2, fill_val2)

    a = arr1_masked[1:] - arr2_masked[1:]
    b = arr1_masked[:-1] - arr2_masked[:-1]
    c = abs(a - b)
    vDTR = c.mean(axis=0)
    numpy.ma.set_fill_value(vDTR, arr1_masked.fill_value)

    if not isinstance(arr1, numpy.ma.MaskedArray):
        vDTR = vDTR.filled(fill_value=arr1_masked.fill_value)

    return vDTR
Example #5
0
def ETR_calculation(arr1, arr2, fill_val1=None, fill_val2=None):

    """    
    Calculates the ETR indice: intra-period extreme temperature range.
    
    :param arr1: daily max temperature (e.g. "tasmax")
    :type arr1: numpy.ndarray (3D) or numpy.ma.MaskedArray (3D)
    :param arr2: daily min temperature (e.g. "tasmin")
    :type arr2: numpy.ndarray (3D) or numpy.ma.MaskedArray (3D) 
    
    :param fill_val1: fill value of arr1 
    :type fill_val1: float
    :param fill_val2: fill value of arr2 
    :type fill_val2: float
    
    :rtype: numpy.ndarray (2D)        (if "arr1" and "arr2" are numpy.ndarray)
         or numpy.ma.MaskedArray (2D) (if "arr1" and "arr2" are numpy.ma.MaskedArray)
         
    .. warning:: If "arr1" and "arr2" are masked arrays, the parameters "fill_val1" and "fill_val2" are ignored, because they have no sense in this case.
    
    .. warning:: "arr1" and "arr2" must be the same type, shape and correspond to the same time step vector.
    
    """

    arr1_masked = calc.get_masked_arr(arr1, fill_val1)
    arr2_masked = calc.get_masked_arr(arr2, fill_val2)

    max_arr1_masked = arr1_masked.max(axis=0)  # masked array with new fill_value (default)
    min_arr2_masked = arr2_masked.min(axis=0)  # masked array with new fill_value (default)
    ETR = max_arr1_masked - min_arr2_masked  # masked array with new fill_value (default)
    numpy.ma.set_fill_value(ETR, arr1_masked.fill_value)

    if not isinstance(arr1, numpy.ma.MaskedArray):
        ETR = ETR.filled(fill_value=arr1_masked.fill_value)

    return ETR