Files
remote-sensing/easi_tools/__pycache__/load_s2l2a.cpython-310.pyc
T

69 lines
6.5 KiB
Plaintext
Raw Normal View History

2025-11-02 23:36:32 +07:00
o
û\iî+ã@sddlZddlZddlZddlmZddlZddlZddl Z ddl
m Z ddl m
Z
mZmZddlmZe eeƒj¡Ze ej¡e e ej¡¡de
eeZhd£ZdZd Zd
ed e fd d
Z!de j"j#d e$fddZ%ddej#de&d ej#fddZ'de j(de d ej#fddZ)dS)éN)ÚPath)Ú
output_geobox)Ú SPATIAL_KEYSÚCRS_KEYSÚ
OTHER_KEYS)Úmasking)ÚproductÚtimeÚ
geopolygonÚlikeÚlimitÚensure_locationÚdataset_predicate>/Zswir_22ZB02Zband_08Zband_8aZB01ZB06Znir_2ÚB12Zband_03Úswir22Zswir_1Únir08Zrededge2Zband_01Zband_04Z
red_edge_3ZB8AZ
red_edge_1Zband_02Zswir_2Znir09ZB07ÚredZnir_3ÚgreenZ
red_edge_2Zband_09Zswir_16ZB09ZB03Zrededge1ÚblueZband_11ÚnirÚB11ÚcoastalZB05ZB08ZB04Zband_05Znir_1Úswir16Zband_07Zrededge3Zcoastal_aerosolZband_06Zband_12g-Cëâ6?gš™™™™™¹¿ÚmatchesÚreturncCt d¡}i}|D]F}|jd}| |¡}|s t d|¡q | d¡d| d¡}t| d¡ƒ}||vrIt||ƒd|krH||i||<q ||i||<q |S) zçFilter for the highest element84 processing sequence number per scene (scene label excluding the sequence number)
: return : { scene_id_excluding_sequence_number : { highest_sequence_number : datacube.model.Dataset }}
z(S2.+)_([0-9]+)_(L2A)Úlabelz/Dataset label does not match expected pattern: éÚér) ÚreÚcompileÚ metadata_docÚmatchÚlogÚwarningÚgroupÚintÚlist)rÚsorterÚdsrÚkeyÚseq©r/ú</home/x79/CSIROBoeingPhase5-Vietnam/easi_tools/load_s2l2a.pyÚhighest_sequence_number_s


 r1r+cCsj|jd}| dd¡}t d¡}| |¡}|s!t d|¡dSt| d¡ƒdkr,d S| d
d ¡}| S) zZReturn True if a dataset's metadata indicates that the offset correction should be appliedÚ
propertieszs2:processing_baselinez0.0z (\d+)\.(\d+)z=Dataset processing_baseline does not match expected pattern: NréFzearthsearch:boa_offset_applied) r"Úgetr r!r#r$r%r'r&)r+ÚpropsÚbaseliner)r,Zboa_offset_appliedr/r/r0Úds_requires_offsetxs
 

 r7ÚoffsetcCs^dd|jDƒ}t ||¡}|dkr || |¡t||<|S|| |¡t|||<|S)zeApply the scale and offset correction to each reflectance band where there is valid data (not nodata)cSsg|]}|tvr|qSr/)Ú
refl_bands)Ú.0Úxr/r/r0Ú
<listcomp>sz,apply_correction_to_data.<locals>.<listcomp>r)Ú data_varsrÚvalid_data_maskÚwhereÚ scale_factor)r+r8Z refl_varsÚmaskr/r/r0Úapply_correction_to_datasÿrBÚdcÚquerycCsZ| dd¡}|dkrt d|¡dSd}d|vr!|d}|d=|dur6dd| ¡Dƒ}|jdi|¤Ž}d |vr>d
|d <t|ƒ}gg}}| ¡D]"}t|| ¡ƒd } t | ƒ}
|
dur`qK|
rh| 
| ¡qK| 
| ¡qK||} d} t |ƒd krƒt  d ¡d
}
t
} t |ƒd krt  d¡d}
d } | dur¬|jdd| i|¤Ž}t|| ƒ}t  |
¡|St  d¡t  d¡tdd| i|¤Ž}d}d|vrÚ|d}| dd¡dkrÚ|d ddi¡dD] }||vrå||=qÜ|jd||dœ|¤Ž}|jd||dœ|¤Ž}t|ƒ}t|t
ƒ}tj||gdd}| d¡}|dur&| dd¡dkr&| |¡}t  d
¡|S)a_
Replaces datacube.load(**query) for s2_l2a products.
Method:
- Find all datasets matching the query (dc.find_datasets)
- Filter for the highest element84 processing sequence number per scene (scene label excluding the sequence number)
- Filter into two lists for datasets that have
- "s2:processing_baseline" >= "04.00" and "earthsearch:boa_offset_applied" == False (offset correction required)
- everything else (no correction required)
- If either list is empty then load the non-empty list, apply scale (and offset if required), and return the xarray Dataset
- Load and combine the two lists of datasets
- Load each list, apply scale (and offset if required)
- Concat on time dimension and sort by time
- Return the combined xarray Dataset
Notes:
- Any 'groupby' function is applied to each of the xarray Datasets prior to them being combined.
This could create "extra" (non-grouped) time layers in the combined Dataset if the groupby function
would have grouped datasets (scenes) from both lists.
- Scale and offset are applied to the reflectance bands where there is valid data (not `nodata`).
This includes applying the "scale_factor" even if no datasets require the offset correction.
Other masks can be applied by the user (e.g. pixel quality or cloud masking).
rz<all products>Ús2_l2az9This function only applies to the "s2_l2a" product, not: NÚdatasetscSsi|] \}}|tvr||qSr/)Ú search_keys)r:Úvr/r/r0Ú
<dictcomp>¾sz*load_s2l2a_with_offset.<locals>.<dictcomp>Úskip_broken_datasetsTrz&All datasets require offset correctionzPThe valid_data_mask, scale and offset have been applied to the reflectance bandsz%No datasets require offset correctionzTThe valid_data_mask and scale (no offset) have been applied to the reflectance bandsz9Mix of datasets found with either offset required or not.zQWe will load two xarrays, apply offset where required, and merge into one xarray.Ú dask_chunksr r)Ú
output_crsÚ
resolutionÚalign)rFr )Údimr/)r4r$ÚerrorÚitemsÚ
find_datasetsr1Úkeysr(Úvaluesr7ÚappendÚlenÚinfoÚ
add_offsetÚloadrBrÚupdateÚxrÚconcatÚsortbyÚchunk)rCrDrrZ
search_paramsr*Zoffset_appliedZoffset_requiredr-r+Z
isrequiredZmatches_combinedZ this_offsetÚmsgÚdataÚxxZ