Example #1
0
def spi_gamma(precip_monthly_values, 
              month_scale, 
              valid_min, 
              valid_max):

    
    return distribution_fitter.fit_to_gamma(precip_monthly_values, 
                                            month_scale, 
                                            valid_min, 
                                            valid_max)
def compute_indicator_by_lons_pooled(input_dataset,
                                     output_dataset,
                                     input_var_name,
                                     output_var_name,
                                     month_scale,
                                     valid_min, 
                                     valid_max):
    
    while True:
    
        # get the longitude index we'll use from the worker queue
        dim1_index = worker_queue.get()
        
        # lock the thread when doing I/O
        lock.acquire()
        
        # slice out the period of record for the x/y point
        data = input_dataset.variables[input_var_name][:, dim1_index, :]
        
        # release the lock since we'll not share anything else until doing the I/O to the output dataset                        
        lock.release()
        
        # keep the original data shape, we'll use this to reshape later
        original_shape = input_dataset.variables[input_var_name].shape
        
        for dim2_index in range(input_dataset.variables[input_var_name].shape[2]):
            
            # only process non-empty grid cells, i.e. data array contains at least some non-NaN values
            if isinstance(data[:, dim2_index], np.ma.MaskedArray) and data[:, dim2_index].mask.all():
        
                pass         
             
            else:  # we have some valid values to work with
        
                logger.info('Processing x/y {}/{}'.format(dim1_index, dim2_index))
        
                # perform a fitting to gamma     
                data[:, dim2_index] = distribution_fitter.fit_to_gamma(data[:, dim2_index],
                                                                       month_scale, 
                                                                       valid_min, 
                                                                       valid_max)
            
        # reacquire the thread lock for doing NetCDF I/O
        lock.acquire()
        
        # slice out the period of record for the x/y point
        output_dataset.variables[output_var_name][:, dim1_index, :] = np.reshape(data, (original_shape[0], 1, original_shape[2]))
        
        # release the lock since we'll not share anything else until doing the I/O to the output dataset                        
        lock.release()
        
        # communicate to the queue that this task has been completed and the item can be removed
        worker_queue.task_done()
def compute_indicator(input_dataset,
                      output_dataset,
                      input_var_name,
                      output_var_name,
                      month_scale,
                      valid_min, 
                      valid_max,
                      dim1_index,   # typically lon, for example with gridded datasets
                      dim2_index):  # typically lat, for example with gridded datasets
    
    # lock the thread when doing I/O
    lock.acquire()
    
    # slice out the period of record for the x/y point
    data = input_dataset.variables[input_var_name][:, dim1_index, dim2_index]
    
    # release the lock since we'll not share anything else until doing the I/O to the output dataset                        
    lock.release()
    
    # only process non-empty grid cells, i.e. data array contains at least some non-NaN values
    if (isinstance(data, np.ma.MaskedArray)) and data.mask.all():

        pass         
#         # create a filler data array for the time series which is all NaN values
#         data = np.full((data.size(),), np.NaN)
     
    else:  # we have some valid values to work with

#         logger.info('Processing x/y {}/{}'.format(dim1_index, dim2_index))

        # perform a fitting to gamma     
        data = distribution_fitter.fit_to_gamma(data,
                                                month_scale, 
                                                valid_min, 
                                                valid_max)
        
    # reacquire the thread lock for doing NetCDF I/O
    lock.acquire()
    
    # slice out the period of record for the x/y point
    output_dataset.variables[output_var_name][:, dim1_index, dim2_index] = data
    
    # release the lock since we'll not share anything else until doing the I/O to the output dataset                        
    lock.release()
Example #4
0
def spei_gamma(precip_monthly_values,
               temp_monthly_values,
               data_start_year,
               latitude,
               month_scale, 
               valid_min, 
               valid_max):

    # compute the PET values using Thornthwaite's equation
    pet_monthly_values = thornthwaite(temp_monthly_values, latitude, data_start_year, np.nan)
        
    # offset we add to the (P - PE values) in order to bring all values into the positive range     
    p_minus_pe_offset = 1000.0

    # get the difference of P - PE, assign the values into the temperature array (in order to reuse the memory)
    p_minus_pe = (precip_monthly_values - pet_monthly_values) + p_minus_pe_offset
    
    # perform the SPEI computation (fit to the Gamma distribution) and assign the values into the dataset
    return distribution_fitter.fit_to_gamma(p_minus_pe, 
                                            month_scale, 
                                            valid_min, 
                                            valid_max)