Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pls Help Me
#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


Messages In This Thread
Pls Help Me - by gnss - Feb-05-2023, 08:57 PM
RE: Pls Help Me - by farshid - Apr-20-2023, 05:38 PM

Forum Jump:

User Panel Messages

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