wrfup package

wrfup.main.main(argv=None)[source]

Main entry point for wrfup package.

wrfup.download.download_and_extract_zip(zip_url, extraction_path, file_num, total_files)[source]

Download and extract a zip file from a URL with real-time progress tracking.

wrfup.download.download_tiles(tile_names, save_dir, download_url)[source]

Download the urban fraction or URB_PARAM tiles for the given tile names.

Parameters:
  • tile_names (list) – List of tile names to download.

  • save_dir (str) – Directory to save the downloaded tiles.

  • download_url (str) – Base URL for downloading the tiles.

wrfup.download.get_tile_names_in_aoi(lat_min, lat_max, lon_min, lon_max, field)[source]

Get the list of tile names for the area of interest (AOI) based on latitude and longitude, with different naming conventions for FRC_URB2D and URB_PARAM fields.

Parameters:
  • lat_min (float) – Minimum latitude of AOI.

  • lat_max (float) – Maximum latitude of AOI.

  • lon_min (float) – Minimum longitude of AOI.

  • lon_max (float) – Maximum longitude of AOI.

  • field (str) – The field type (‘FRC_URB2D’ or ‘URB_PARAM’).

Returns:

List of tile names.

Return type:

list

wrfup.download.get_total_download_size(tile_names, download_url)[source]

Calculate the total size of all tiles to be downloaded.

Parameters:
  • tile_names (list) – List of tile names.

  • download_url (str) – Base URL to check file size.

Returns:

Total size in bytes.

Return type:

int

wrfup.download.lat_lon_to_tile_indices(lat, lon, grid_rows=16, grid_cols=16)[source]

Convert latitude and longitude to grid tile index based on zoom level.

Parameters:
  • lat (float) – Latitude in degrees.

  • lon (float) – Longitude in degrees.

  • grid_rows (int) – Number of rows in the grid.

  • grid_cols (int) – Number of columns in the grid.

Returns:

Row and column index of the tile.

Return type:

(int, int)

wrfup.download.merge_tiles(tile_paths, output_path)[source]

Merge multiple tiles into a single mosaic and save as a compressed GeoTIFF.

Parameters:
  • tile_paths (list) – List of file paths for the tiles to be merged.

  • output_path (str) – Path to save the compressed merged file.

wrfup.calculation.add_frc_urb2d_field_if_not_exists(geo_em_ds, field_name)[source]

Ensure that the geo_em file contains the FRC_URB2D field, initialized if necessary.

wrfup.calculation.add_urb_param_fields_if_not_exists(geo_em_ds)[source]

Ensure that the geo_em file contains the URB_PARAM fields, initialized if necessary.

wrfup.calculation.calculate_frc_urb2d(info, geo_em_ds, merged_tiff_path, field_name='FRC_URB2D')[source]

Calculate the FRC_URB2D field by averaging urban fraction values within each geo_em grid cell.

Parameters:
  • info (Info) – The configuration object containing paths and settings.

  • geo_em_ds (xarray.Dataset) – The opened geo_em dataset.

  • merged_tiff_path (str) – Path to the merged GeoTIFF file containing urban fraction data.

  • field_name (str) – The field name to store the data (default: ‘FRC_URB2D’).

Returns:

2D array of calculated FRC_URB2D values.

Return type:

np.ndarray

wrfup.calculation.calculate_urb_param(info, geo_em_ds, merged_tiff_path, field_name='URB_PARAM')[source]

Calculate the URB_PARAM field.

This calculation follows the NUDAPT 44 field structure:

  • LAMBDA_P (Plan Area Fraction): Stored in slice [90,:,:] of URB_PARAM. It represents the fraction of the grid cell’s area covered by building footprints.

  • Mean Building Height (Geometric Mean): Stored in slice [91,:,:] of URB_PARAM. It is the geometric mean of building heights within the grid cell.

  • Standard Deviation of Building Heights: Stored in slice [92,:,:] of URB_PARAM. It calculates the standard deviation of building heights.

  • Weighted Building Height: Stored in slice [93,:,:] of URB_PARAM. It represents the average building height weighted by the planar surface area (LAMBDA_P).

  • LAMBDA_B (Frontal Area Fraction): Stored in slice [94,:,:] of URB_PARAM. It represents the fraction of the grid cell’s frontal area occupied by building walls.

  • Frontal Area Index (FAI): - North: Stored in slice [96,:,:] of URB_PARAM. - South: Stored in slice [97,:,:] of URB_PARAM. - East: Stored in slice [98,:,:] of URB_PARAM. - West: Stored in slice [99,:,:] of URB_PARAM.

  • Building Height Distribution: Stored in slices [117:132,:,:] of URB_PARAM.

The building height distribution is computed using the following bin ranges (in meters): - 0-5, 5-10, 10-15, …, up to 70+ meters.

wrfup.calculation.crop_opened_tiff_by_lat_lon_bounds_and_return_mosaic(src, band, lat_min, lat_max, lon_min, lon_max)[source]

Crop an open rasterio dataset to the specified latitude and longitude bounds and return the cropped mosaic as a numpy array.

Parameters:
  • src – rasterio.io.DatasetReader, an open rasterio dataset.

  • band – int, the band to read.

  • lat_min – float, minimum latitude of the cropping boundary.

  • lat_max – float, maximum latitude of the cropping boundary.

  • lon_min – float, minimum longitude of the cropping boundary.

  • lon_max – float, maximum longitude of the cropping boundary.

Returns:

The cropped mosaic array. rasterio.transform.Affine: The transformation of the cropped mosaic.

Return type:

numpy.ndarray

wrfup.utils.check_geo_em_file(geo_em_file, field)[source]

Check the geo_em file for the required fields before processing.

Parameters:
  • geo_em_file (str) – Path to the geo_em file.

  • field (str) – The field to check for (FRC_URB2D or URB_PARAM).

Returns:

The opened geo_em dataset if the file is valid and all required fields are present. None: If the file is invalid or fields are missing.

Return type:

dataset (xarray.Dataset)

wrfup.utils.clean_up(temp_dir)[source]

Remove temporary files and directories.

Parameters:

temp_dir (str) – The directory where temporary files are stored.

wrfup.utils.get_lat_lon_extent(geo_em_file)[source]

Extract the latitude and longitude extents from the geo_em file using xarray.

Parameters:

geo_em_file (str) – Path to the geo_em file.

Returns:

Tuple containing min/max latitudes and longitudes.

Return type:

tuple

class wrfup.info.Info(geo_em_file, field, work_dir, temp_dir)[source]

Store configuration and paths for processing.