Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pls Help Me
#1
The following code helps me to process only one image. However, I want to process all images in the path and detect the change.
My aim is to determine the mean value of SO2 values.
###Load and explore Sentinel-5p product###
# Set product directory and list SO2 files
s5p_path = "/shared/Training/ATMO03_VolcanoEmissions_Guatemala/Original/"
s5p_files = sorted([f for f in glob.glob(s5p_path + "*SO2*.nc", recursive=True)])
for i,j in enumerate(s5p_files):
    s5p_split = j.split('/')
    s5p_split2 = s5p_split[-1].split('_')
    print('#{}: {} \n'.format(i, s5p_split[-1]),
          colored('Mission:', 'blue'),'{}'.format(s5p_split2[0]),
          colored('Stream | Level:', 'blue'),'{} | {}'.format(s5p_split2[1],s5p_split2[2]),
          colored('Product identifier:', 'blue'), '{}'.format(s5p_split2[4]),
          colored('Start | End:', 'blue'), '{} | {}'.format(s5p_split2[8], s5p_split2[9]),
          colored('Orbit:', 'blue'), '{}'.format(s5p_split2[10]),
          colored('Collect.:', 'blue'), '{}'.format(s5p_split2[11]),
          colored('Processor:', 'blue'), '{}'.format(s5p_split2[12]),
          colored('Procss. time:', 'blue'), '{}'.format(s5p_split2[13]), '\n'
    )

with xr.open_dataset(s5p_files[1]) as s5p_img_GA:
    print(colored('Global attributes: \n', 'green'))
    display(s5p_img_GA)
       
with xr.open_dataset(s5p_files[1], group='METADATA/GRANULE_DESCRIPTION') as s5p_img_MT:
    print(colored('METADATA/GRANULE_DESCRIPTION group: \n', 'green'))
    display(s5p_img_MT)
with xr.open_dataset(s5p_files[1], group='PRODUCT').set_coords(['latitude', 'longitude']) as s5p_img_PRD:
    print(colored('PRODUCT group: \n', 'green'))
    display(s5p_img_PRD)
# Extract SO2 variable
so2 = s5p_img_PRD['sulfurdioxide_total_vertical_column']
# Convert to DU
so2 = so2 * 2254.15
Filter low quality pixels
so2_filter = so2.where(s5p_img_PRD['qa_value'] > 0.5, drop=True)
# Plot original data vs quality flag
plt.figure(figsize=(30, 10))
# Plot qa_value image
ax1 = plt.subplot(121, projection=ccrs.Orthographic(-90, 15))
s5p_img_PRD['qa_value'][0].plot.pcolormesh(ax=ax1, x='longitude', y='latitude', add_colorbar=True, cmap='Spectral', transform=ccrs.PlateCarree()) 
ax1.set_title('S-5p L2 NO$_2$ (2020-03-26) | Quality Flag Layer')
ax1.add_feature(cartopy.feature.LAND, edgecolor='black')
ax1.add_feature(cartopy.feature.OCEAN)
ax1.coastlines('10m')
ax1.gridlines()
ax1.set_global()

# Plot masked SO2 data
ax2 = plt.subplot(122, projection=ccrs.Orthographic(-90, 15))
so2_filter[0].plot.pcolormesh(ax=ax2, x='longitude', y='latitude', add_colorbar=True, cmap='magma_r',transform=ccrs.PlateCarree(), vmin=0)
ax2.set_title('Filtered S-5p L2 NO$_2$ (2020-03-26) | Quality Flag Layer > 0.75')
ax2.add_feature(cartopy.feature.LAND, edgecolor='black')
ax2.add_feature(cartopy.feature.OCEAN)
ax2.coastlines('10m')
ax2.gridlines()
ax2.set_global()
plt.show();
###Subset to study area###
# Set study area using ipyleaflet
m = Map(center=(14, -90), zoom=6)
draw_control = DrawControl(polyline={}, circlemarker={}, polygon={})
draw_control.rectangle = {
    "shapeOptions": {
        "fillColor": "#fca45d",
        "color": "#fca45d",
        "fillOpacity": 0.2
    }
}
m.add_control(draw_control)
m
# ##Extrat ur and ll coordinates in LON, LAT order###
draw_control.last_draw['geometry']['coordinates']
# Extract UR and LL corner in LAT, LON order
ll = draw_control.last_draw['geometry']['coordinates'][0][0][::-1]
ur = draw_control.last_draw['geometry']['coordinates'][0][2][::-1]
# Subset using xarray.where()
so2_fltr_subset = so2_filter.where((so2_filter.longitude < ur[1]) & (so2_filter.longitude > ll[1]) & (so2_filter.latitude > ll[0]) & (so2_filter.latitude < ur[0]), drop=True) 
so2_subset = so2.where((so2.longitude < ur[1]) & (so2.longitude > ll[1]) & (so2.latitude > ll[0]) & (so2.latitude < ur[0]), drop=True) 
###Visualize SO2 subset product###
# Plot original data vs filtered 
plt.figure(figsize=(30, 10))

# Plot 1
ax1 = plt.subplot(121, projection=ccrs.Orthographic(-90, 15))
so2_subset[0].plot.pcolormesh(ax=ax1, x='longitude', y='latitude', add_colorbar=True, cmap='magma_r', transform=ccrs.PlateCarree(), vmin=0) 

# Background
ax1.add_feature(cartopy.feature.LAND, edgecolor='black')
ax1.add_feature(cartopy.feature.BORDERS)
ax1.add_feature(cartopy.feature.OCEAN)
ax1.coastlines('10m')

# Metadata
ax1.plot(-90.876329828, 14.476331428, '^:r', markersize=10, transform=ccrs.Geodetic())
ax1.plot(-90.51327, 14.64072, 'X:g', markersize=10, transform=ccrs.Geodetic())
ax1.text(-90.51327, 14.69072, 'Ciudad de Guatemala', transform=ccrs.Geodetic())
ax1.set_title('S-5p L2 [SO$_2$] DU (2018-06-03)')
gl = ax1.gridlines(draw_labels=True, linewidth=1, color='gray', linestyle='--')
gl.top_labels = False
gl.right_labels = False
# Plot 2
ax2 = plt.subplot(122, projection=ccrs.Orthographic(-90, 15))
so2_fltr_subset[0].plot.pcolormesh(ax=ax2, x='longitude', y='latitude', add_colorbar=True, cmap='magma_r',transform=ccrs.PlateCarree(), vmin=0)
# Background 
ax2.add_feature(cartopy.feature.LAND, edgecolor='black')
ax2.add_feature(cartopy.feature.BORDERS)
ax2.add_feature(cartopy.feature.OCEAN)
ax2.coastlines('10m')
# Metadata
ax2.plot(-90.876329828, 14.476331428, '^:r', markersize=10, transform=ccrs.Geodetic())
ax2.plot(-90.51327, 14.64072, 'X:g', markersize=10, transform=ccrs.Geodetic())
ax2.text(-90.51327, 14.69072, 'Ciudad de Guatemala', transform=ccrs.Geodetic());
ax2.set_title('Filtered S-5p L2 [SO$_2$] DU (2018-06-03) | Quality Flag Layer > 0.5')
gl2 = ax2.gridlines(draw_labels=True, linewidth=1, color='gray', linestyle='--')
gl2.top_labels = False
gl2.right_labels = False

plt.show();
# ##Create custom colorbar with transparency for lower values###
cm_values = np.linspace(0.0, 1.0, 16536)
alpha_cm = plt.cm.YlGnBu(cm_values)
alpha_cm[:, -1] = np.sqrt(cm_values)
my_cmap = colors.ListedColormap(alpha_cm)
# Plot SO2 product
fig, ax = plt.subplots(figsize=(18, 12))
ax = plt.axes(projection=ccrs.PlateCarree())
im = so2_subset[0].plot.pcolormesh(ax=ax, transform=ccrs.PlateCarree(), infer_intervals=True, cmap=my_cmap, norm=colors.PowerNorm(gamma=1./2.), vmin=10,
        x='longitude', y='latitude')
# Background
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.LAND, edgecolor='black')
ax.add_feature(cartopy.feature.BORDERS)
ax.add_feature(cartopy.feature.OCEAN)
# Metadata
ax.set_title('S-5p L2 [SO$_2$] DU (2018-06-03), Volcan de Fuego - Guatemala')
ax.plot(-90.876329828, 14.476331428, '^:r', markersize=10, transform=ccrs.Geodetic())
ax.plot(-90.51327, 14.64072, 'X:g', markersize=10, transform=ccrs.Geodetic())
ax.text(-90.51327, 14.69072, 'Ciudad de Guatemala', transform=ccrs.Geodetic())
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1, color='gray', linestyle='--')
gl.top_labels = False
gl.right_labels = False
plt.show();
Reply
#2
To process all the images in the path and detect the change, you can modify your code by including a loop to iterate through each file in the directory. You can also add a list to store the mean SO2 values of each image. Here's an example code that you can use:


import glob
import xarray as xr
import numpy as np

s5p_path = "/shared/Training/ATMO03_VolcanoEmissions_Guatemala/Original/"
s5p_files = sorted([f for f in glob.glob(s5p_path + "*SO2*.nc", recursive=True)])
mean_SO2_values = []

for i, file in enumerate(s5p_files):
    with xr.open_dataset(file, group='PRODUCT').set_coords(['latitude', 'longitude']) as s5p_img_PRD:
        # Extract SO2 variable
        so2 = s5p_img_PRD['sulfurdioxide_total_vertical_column']
        # Convert to DU
        so2 = so2 * 2254.15
        # Filter low quality pixels
        so2_filter = so2.where(s5p_img_PRD['qa_value'] > 0.5, drop=True)
        # Subset to study area
        ll, ur = (-91.8, 13.6), (-89.7, 15.0)
        so2_fltr_subset = so2_filter.where((so2_filter.longitude < ur[1]) & (so2_filter.longitude > ll[1]) & (so2_filter.latitude > ll[0]) & (so2_filter.latitude < ur[0]), drop=True)
        so2_subset = so2.where((so2.longitude < ur[1]) & (so2.longitude > ll[1]) & (so2.latitude > ll[0]) & (so2.latitude < ur[0]), drop=True)
        # Calculate mean SO2 value
        mean_SO2 = np.mean(so2_fltr_subset.values)
        mean_SO2_values.append(mean_SO2)
        print(f"Processed {i+1}/{len(s5p_files)} images: {file} - Mean SO2 value: {mean_SO2}")
In this modified code, the glob library is used to find all the files with the name pattern "*SO2*.nc" in the directory. The loop iterates through each file and extracts the SO2 variable, applies the filter, and subsets the study area. The mean SO2 value is then calculated and added to the mean_SO2_values list. Finally, the loop prints the current progress and the mean SO2 value of the current file.

Note that I used a fixed study area with the coordinates of ll = (-91.8, 13.6) and ur = (-89.7, 15.0) in this code example. You may want to modify this to use the study area that you defined in your original code.
"никогда не сдавайся"
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020