import numpy as np
import folium
import matplotlib.colors as colors
import matplotlib.pyplot as plt
from folium.features import CustomIcon
from branca.element import Element, Figure
import datetime
import http.server
import socketserver
import threading
import webbrowser
import os
import noise # Ensure you have the 'noise' module installed
from scipy.ndimage import gaussian_filter, map_coordinates
from PIL import Image
Load color scales
def load_color_scale(file_path):
with open(file_path, "r") as file:
color_scale = eval(file.read())
return np.array(color_scale) / 255
Ensure the 'colours' directory exists and color scale files are available
color_dir = "colours"
if not os.path.exists(color_dir):
os.makedirs(color_dir)
Load or define the color scales
Ensure that 'dBZ.txt', 'radarbeam.txt', and 'radarnoise.txt' exist in the 'colours' directory
beam_color_scale = load_color_scale("colours/radarbeam.txt")
radarnoise_color_scale = load_color_scale("colours/radarnoise.txt")
color_scale = load_color_scale("colours/dBZ.txt") # Ensure this includes the full dBZ range
Reverse the color scale if necessary
Uncomment the following line if the colors appear inverted
Replace 'interactive/radarsign.png' with the path to your radar icon image
icon_image = 'interactive/radarsign.png'
Check if the icon image exists, if not, create a simple placeholder
if not os.path.exists(icon_image):
if not os.path.exists('interactive'):
os.makedirs('interactive')
# Create a simple red square as a placeholder
img = Image.new('RGB', (30, 20), color='red')
img.save(icon_image)
Here, can you increase isolation of the generated cells, because right now these cells are oftenly connected by lower intensity dBZ values for example?
To increase the isolation of the generated radar cells and minimize the low-intensity dBZ values connecting them, you can adjust several parameters in your code that control the cell generation and processing steps. Here are the steps you can take:
Increase the Threshold Value
The threshold determines which parts of the noise field are considered as cells. By increasing the threshold, you exclude more low-noise areas, leading to more isolated cells.
# Increase threshold to exclude lower noise values
threshold = 0.5 # Increased from 0.3 to 0.5
cells = np.where(cells > threshold, cells, 0)
Reduce or Remove Small-Scale Noise
Adding small-scale noise can introduce low-level variations that connect cells via low dBZ values. By reducing or removing this noise, you minimize unwanted connections.
# Remove or reduce small-scale noise addition
# Option 1: Remove small-scale noise entirely
# small_scale_noise = np.random.normal(0, 0.02, (height, width))
# cells += small_scale_noise
# Option 2: Reduce the standard deviation of the small-scale noise
small_scale_noise = np.random.normal(0, 0.005, (height, width)) # Reduced from 0.02 to 0.005
cells += small_scale_noise
Increase the Non-Linear Transformation Exponent
Increasing the exponent in the non-linear transformation suppresses lower values and enhances higher values, making the cells more distinct and isolated.
# Increase exponent to suppress lower values
cells = cells ** 3 # Increased exponent from 1.5 to 3
Reduce the Smoothing Sigma
While smoothing helps mimic natural cell shapes, over-smoothing can cause cells to merge. Reducing the sigma value preserves sharpness and isolation.
# Reduce smoothing to prevent merging of cells
cells = gaussian_filter(cells, sigma=1) # Reduced sigma from 3 to 1
Adjust Fractal Noise Parameters
Modifying the noise generation parameters can create larger, more distinct cells that are naturally more isolated.
# Increase resolution to create larger cells
res = (20, 20) # Increased from (10, 10)
# Reduce octaves to decrease noise detail
octaves = 3 # Reduced from 4
# You can also experiment with persistence and lacunarity
persistence = 0.6
lacunarity = 1.8
Optional: Modify the Distance Mask
You can adjust the distance mask to further control where cells appear, ensuring they don't connect at the edges.
# Create a sharper distance mask if needed
distance_mask = np.sqrt(x**2 + y**2) <= (radius * 0.95) # Use 95% of the radius
Clip Cell Values After Transformation
Ensure that after exponentiation, the cell values are clipped to the range [0, 1] to prevent unintended connections due to overflow.
# Clip cells to [0, 1] after exponentiation
cells = np.clip(cells, 0, 1)
Visualize Intermediate Steps
To better understand the impact of each adjustment, visualize the intermediate outputs using matplotlib.
# Visualize cells after thresholding
plt.figure()
plt.imshow(cells, cmap='gray')
plt.title('Cells After Thresholding')
plt.show()
By applying these adjustments, your radar simulation should display more isolated cells with fewer connections due to low-intensity dBZ values. Here's how the modified section of your code would look:
# ... (previous code remains unchanged)
# Adjust fractal noise parameters
res = (20, 20) # Increased resolution to create larger cells
octaves = 3 # Reduced octaves to decrease noise detail
persistence = 0.6 # Slight adjustment for better cell formation
lacunarity = 1.8 # Slight adjustment for better cell formation
# Generate fractal noise
cells = generate_fractal_noise_2d((height, width), res, octaves, persistence, lacunarity)
# Increase threshold to exclude more low-noise areas
threshold = 0.5 # Increased threshold from 0.3 to 0.5
cells = np.where(cells > threshold, cells, 0)
# Apply smoothing with reduced sigma to prevent merging
cells = gaussian_filter(cells, sigma=1) # Reduced sigma from 3 to 1
# Remove small-scale noise addition
# small_scale_noise = np.random.normal(0, 0.02, (height, width))
# cells += small_scale_noise
# Increase exponent in non-linear transformation
cells = cells ** 3 # Increased exponent from 1.5 to 3
# Clip cell values after exponentiation
cells = np.clip(cells, 0, 1)
# Apply distance mask (optional adjustment)
cells[~distance_mask] = 0
# Map cell values to dBZ range
min_dbz = -20
max_dbz = 80
cells_dbz = cells * (max_dbz - min_dbz) + min_dbz
# ... (rest of your code remains unchanged)
Note: Feel free to experiment with different values for the parameters (threshold, sigma, exponent) to fine-tune the appearance of the cells according to your preferences.
Remember to re-run the entire script after making these changes to see the effects on the generated radar image. Also, consider visualizing the cells array at various stages to gain better insights into how each parameter affects the cell isolation.
I hope this helps you achieve the desired isolation of the generated cells in your radar simulation!