x_dim_name = 'lon' y_dim_name = 'lat' # create the variable name from the indicator, distribution, and month scale variable_name = 'spi_gamma_{}'.format(str(month_scale).zfill(2)) # make a basic set of variable attributes variable_attributes = {'valid_min' : valid_min, 'valid_max' : valid_max, 'long_name' : 'SPI (Gamma), {}-month scale'.format(str(month_scale))} # initialize the output NetCDF with the same dimensionality, coordinates, etc. as the input precipitation NetCDF output_dataset = netcdf_utils.initialize_dataset(output_file_base + variable_name + '.nc', precip_dataset, x_dim_name, y_dim_name, variable_name, variable_attributes, np.nan) # dictionary mapping process (thread) IDs to the lat/lon point into which the computed data from the target function is to be copied threads_to_join = [] # loop over the grid cells for x in range(precip_dataset.variables[x_dim_name].size): for y in range(precip_dataset.variables[y_dim_name].size): logger.info('Processing x/y {}/{}'.format(x, y)) # run a thread to compute the SPI/gamma on this x/y location thread = threading.Thread(target=compute_indicator,
# initialize the output NetCDFs for each month scale # month_scales = [1, 2, 3, 6, 9, 12, 24, 36, 48, 60, 72] month_scales = [9, 24] datasets = {} for month_scale_index, month_scale_var_name in enumerate(month_scales): # create the variable name from the indicator, distribution, and month scale variable_name = "spi_pearson_{}".format(str(month_scale_var_name).zfill(2)) # get the output files for each variable # initialize the output NetCDFs with the same dimensionality, coordinates, etc. as the input precipitation NetCDF dataset = netcdf_utils.initialize_dataset( output_file_base + variable_name + ".nc", precip_dataset, x_dim_name, y_dim_name, variable_name, "SPI (Pearson), {}-month scale".format(str(month_scale_var_name)), variable_attributes, np.nan, ) # add to the list of month scale datasets datasets[variable_name] = dataset # loop over the grid cells for x in range(precip_dataset.variables[x_dim_name].size): for y in range(precip_dataset.variables[y_dim_name].size): logger.info("Processing x/y {}/{}".format(x, y)) # slice out the period of record for the x/y point
'valid_max' : valid_max} # initialize the output NetCDFs for each month scale month_scales = [1, 2, 3, 6, 9, 12, 24, 36, 48, 60, 72] spi_gamma_datasets = {} for month_scale_index, month_scale_var_name in enumerate(month_scales): # create the variable name from the indicator, distribution, and month scale variable_name_spi_gamma = 'spi_gamma_{}'.format(str(month_scale_var_name).zfill(2)) # get the output files for each variable # initialize the output NetCDFs with the same dimensionality, coordinates, etc. as the input precipitation NetCDF spi_gamma_dataset = netcdf_utils.initialize_dataset(output_file_base + variable_name_spi_gamma + '.nc', precip_dataset, x_dim_name, y_dim_name, variable_name_spi_gamma, 'SPI (Gamma), {}-month scale'.format(str(month_scale_var_name)), variable_attributes, np.nan) # add to the list of month scale datasets spi_gamma_datasets[variable_name_spi_gamma] = spi_gamma_dataset # loop over the grid cells for x in range(precip_dataset.variables[x_dim_name].size): for y in range(precip_dataset.variables[y_dim_name].size): logger.info('Processing x/y {}/{}'.format(x, y)) # slice out the period of record for the x/y point precip_data = precip_dataset.variables[precip_var_name][:, x, y]
for month_scale in [1, 2, 3, 6, 9, 12, 24, 36, 48, 60, 72]: # create the variable name from the indicator, distribution, and month scale variable_name_spi_pearson = 'spi_pearson_{}'.format(str(month_scale).zfill(2)) variable_name_spi_gamma = 'spi_gamma_{}'.format(str(month_scale).zfill(2)) variable_name_spei_pearson = 'spei_pearson_{}'.format(str(month_scale).zfill(2)) variable_name_spei_gamma = 'spei_gamma_{}'.format(str(month_scale).zfill(2)) # get the output files for each variable # initialize the output NetCDFs with the same dimensionality, coordinates, etc. as the input precipitation NetCDF variable_name_spi_pearson = 'spi_pearson_{}'.format(str(month_scale).zfill(2)) long_name = 'SPI (Pearson), {}-month scale'.format(str(month_scale)) spi_pearson_dataset = netcdf_utils.initialize_dataset(output_file_base + variable_name_spi_pearson + '.nc', precip_dataset, x_dim_name, y_dim_name, variable_name_spi_pearson, long_name, variable_attributes, np.nan) variable_name_spi_gamma = 'spi_gamma_{}'.format(str(month_scale).zfill(2)) long_name = 'SPI (Gamma), {}-month scale'.format(str(month_scale)) spi_gamma_dataset = netcdf_utils.initialize_dataset(output_file_base + variable_name_spi_gamma + '.nc', precip_dataset, x_dim_name, y_dim_name, variable_name_spi_gamma, long_name, variable_attributes, np.nan) variable_name_spei_pearson = 'spei_pearson_{}'.format(str(month_scale).zfill(2)) long_name = 'SPEI (Pearson), {}-month scale'.format(str(month_scale))