Download this notebook from github.
XSAR example¶
open a dataset with xsar.open_dataset
[1]:
import xsar
import os
import numpy as np
[2]:
# get test file. You can replace with an path to other SAFE
filename = xsar.get_test_file('S1A_IW_GRDH_1SDV_20170907T103020_20170907T103045_018268_01EB76_Z010.SAFE')
Open a dataset with a xsar.SentinelMeta object¶
A xsar.SentinelMeta object handles all attributes and methods that can’t be embdeded in a xarray.Dataset
object. It can also replace a filename in xsar.open_dataset
When using it in a notebook, it also have an html representation.
[3]:
sar_meta = xsar.SentinelMeta(filename)
sar_meta
[3]:
Single dataset
SENTINEL1_DS:S1A_IW_GRDH_1SDV_20170907T103020_20170907T103045_018268_01EB76_Z010.SAFE:IW | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
A xsar.SentinelMeta object handles all attributes and methods relative to the SAFE metadata that can’t be embdeded in a xarray.Dataset
object.
It can be used to construct a xsar.SentinelDataset object.
[4]:
sar_ds_obj = xsar.SentinelDataset(sar_meta)
sar_ds_obj
[4]:
<xsar.Sentinel1.SentinelDataset at 0x7fcf1635aa00>
a xsar.SentinelDataset as a .dataset
property that is the already known xarray.Dataset
returned by xsar.open_dataset
So, those commands are equivalent:
ds = xsar.open_dataset(filename)
ds = xsar.SentinelDataset(xsar.SentinelMeta(filename)).dataset
By using the underlying objects, the user have access to properties and methods that are not available to the xarray.Dataset
object.
Open a dataset at lower resolution¶
We can use xsar.SentinelMeta properties to compute the pixel count we need to average to get a resolution of 200*200 meters
[5]:
resolution = {'atrack' : int(np.round(200 / sar_meta.pixel_atrack_m)), 'xtrack': int(np.round(200 / sar_meta.pixel_xtrack_m))}
resolution
[5]:
{'atrack': 20, 'xtrack': 20}
[6]:
sar_ds_obj = xsar.SentinelDataset(sar_meta, resolution = resolution)
{k:sar_ds_obj.dataset.attrs[k] for k in ['coverage','pixel_atrack_m','pixel_xtrack_m'] }
[6]:
{'coverage': '170km * 251km (atrack * xtrack )',
'pixel_atrack_m': 203.06471107503256,
'pixel_xtrack_m': 199.7315217724319}
Extract a sub image of 10*10km around a lon/lat point¶
Convert (lon,lat) to (atrack, xtrack)¶
we can use xsar.SentinelMeta.ll2coords to convert (lon,lat) to (atrack, xtrack):
[7]:
# from a shapely object
point_lonlat = sar_ds_obj.s1meta.footprint.centroid
point_coords = sar_ds_obj.s1meta.ll2coords(point_lonlat.x, point_lonlat.y)
point_coords
[7]:
(8421.566059451987, 12589.902956209728)
The result is floating, because it’s the position inside the pixel. To get the real coordinates in an existing dataset, we can use xsar.SentinelDataset.ll2coords
[8]:
point_coords = sar_ds_obj.ll2coords(point_lonlat.x, point_lonlat.y)
point_coords
[8]:
(8430.0, 12590.0)
Extract the sub-image¶
[9]:
box_size = 10000 # 10km
dist = {'atrack' : int(np.round(box_size / 2 / sar_meta.pixel_atrack_m)), 'xtrack': int(np.round(box_size / 2 / sar_meta.pixel_xtrack_m))}
dist
[9]:
{'atrack': 500, 'xtrack': 500}
[10]:
# select 10*10 km around point_coords
sar_ds_obj.dataset = sar_ds_obj.dataset.sel(atrack=slice(point_coords[0] - dist['atrack'], point_coords[0] + dist['atrack']), xtrack=slice(point_coords[1] - dist['xtrack'], point_coords[1] + dist['xtrack']))
sar_ds_obj.dataset
[10]:
<xarray.Dataset> Dimensions: (atrack: 51, pol: 2, xtrack: 51) Coordinates: * pol (pol) object 'VV' 'VH' * atrack (atrack) float64 7.93e+03 7.95e+03 ... 8.91e+03 8.93e+03 * xtrack (xtrack) float64 1.209e+04 1.211e+04 ... 1.307e+04 1.309e+04 Data variables: digital_number (pol, atrack, xtrack) uint16 dask.array<chunksize=(1, 51, 51), meta=np.ndarray> time (atrack) datetime64[ns] 2017-09-07T10:30:32.763824384 ...... longitude (atrack, xtrack) float32 dask.array<chunksize=(51, 51), meta=np.ndarray> latitude (atrack, xtrack) float32 dask.array<chunksize=(51, 51), meta=np.ndarray> elevation (atrack, xtrack) float32 dask.array<chunksize=(51, 51), meta=np.ndarray> incidence (atrack, xtrack) float32 dask.array<chunksize=(51, 51), meta=np.ndarray> sigma0_raw (pol, atrack, xtrack) float64 dask.array<chunksize=(1, 51, 51), meta=np.ndarray> nesz (pol, atrack, xtrack) float64 dask.array<chunksize=(1, 51, 51), meta=np.ndarray> gamma0_raw (pol, atrack, xtrack) float64 dask.array<chunksize=(1, 51, 51), meta=np.ndarray> negz (pol, atrack, xtrack) float64 dask.array<chunksize=(1, 51, 51), meta=np.ndarray> sigma0 (pol, atrack, xtrack) float64 dask.array<chunksize=(1, 51, 51), meta=np.ndarray> gamma0 (pol, atrack, xtrack) float64 dask.array<chunksize=(1, 51, 51), meta=np.ndarray> Attributes: (12/14) ipf: 2.84 platform: SENTINEL-1A swath: IW product: GRDH pols: VV VH name: SENTINEL1_DS:/tmp/S1A_IW_GRDH_1SDV_20170907T103020_201... ... ... footprint: POLYGON ((-69.12591138027886 20.21973939326448, -69.22... coverage: 10km * 10km (atrack * xtrack ) pixel_atrack_m: 203.5818629367982 pixel_xtrack_m: 199.7376649646133 orbit_pass: Descending platform_heading: -167.7668824808032
- atrack: 51
- pol: 2
- xtrack: 51
- pol(pol)object'VV' 'VH'
array(['VV', 'VH'], dtype=object)
- atrack(atrack)float647.93e+03 7.95e+03 ... 8.93e+03
array([7930., 7950., 7970., 7990., 8010., 8030., 8050., 8070., 8090., 8110., 8130., 8150., 8170., 8190., 8210., 8230., 8250., 8270., 8290., 8310., 8330., 8350., 8370., 8390., 8410., 8430., 8450., 8470., 8490., 8510., 8530., 8550., 8570., 8590., 8610., 8630., 8650., 8670., 8690., 8710., 8730., 8750., 8770., 8790., 8810., 8830., 8850., 8870., 8890., 8910., 8930.])
- xtrack(xtrack)float641.209e+04 1.211e+04 ... 1.309e+04
array([12090., 12110., 12130., 12150., 12170., 12190., 12210., 12230., 12250., 12270., 12290., 12310., 12330., 12350., 12370., 12390., 12410., 12430., 12450., 12470., 12490., 12510., 12530., 12550., 12570., 12590., 12610., 12630., 12650., 12670., 12690., 12710., 12730., 12750., 12770., 12790., 12810., 12830., 12850., 12870., 12890., 12910., 12930., 12950., 12970., 12990., 13010., 13030., 13050., 13070., 13090.])
- digital_number(pol, atrack, xtrack)uint16dask.array<chunksize=(1, 51, 51), meta=np.ndarray>
Array Chunk Bytes 10.16 kiB 5.08 kiB Shape (2, 51, 51) (1, 51, 51) Count 8 Tasks 2 Chunks Type uint16 numpy.ndarray - time(atrack)datetime64[ns]2017-09-07T10:30:32.763824384 .....
array(['2017-09-07T10:30:32.763824384', '2017-09-07T10:30:32.793691648', '2017-09-07T10:30:32.823558656', '2017-09-07T10:30:32.853425920', '2017-09-07T10:30:32.883293184', '2017-09-07T10:30:32.913160448', '2017-09-07T10:30:32.943027456', '2017-09-07T10:30:32.972894720', '2017-09-07T10:30:33.002761984', '2017-09-07T10:30:33.032629248', '2017-09-07T10:30:33.062496512', '2017-09-07T10:30:33.092363520', '2017-09-07T10:30:33.122230784', '2017-09-07T10:30:33.152098048', '2017-09-07T10:30:33.181965312', '2017-09-07T10:30:33.211832576', '2017-09-07T10:30:33.241699584', '2017-09-07T10:30:33.271566848', '2017-09-07T10:30:33.301434112', '2017-09-07T10:30:33.331301376', '2017-09-07T10:30:33.361168640', '2017-09-07T10:30:33.391035648', '2017-09-07T10:30:33.420902912', '2017-09-07T10:30:33.450770176', '2017-09-07T10:30:33.480637440', '2017-09-07T10:30:33.510504448', '2017-09-07T10:30:33.540371712', '2017-09-07T10:30:33.570238976', '2017-09-07T10:30:33.600106240', '2017-09-07T10:30:33.629973504', '2017-09-07T10:30:33.659840512', '2017-09-07T10:30:33.689707776', '2017-09-07T10:30:33.719575040', '2017-09-07T10:30:33.749442304', '2017-09-07T10:30:33.779309568', '2017-09-07T10:30:33.809176576', '2017-09-07T10:30:33.839043840', '2017-09-07T10:30:33.868911104', '2017-09-07T10:30:33.898778368', '2017-09-07T10:30:33.928645632', '2017-09-07T10:30:33.958512640', '2017-09-07T10:30:33.988379904', '2017-09-07T10:30:34.018247168', '2017-09-07T10:30:34.048114432', '2017-09-07T10:30:34.077981440', '2017-09-07T10:30:34.107848704', '2017-09-07T10:30:34.137715968', '2017-09-07T10:30:34.167583232', '2017-09-07T10:30:34.197450496', '2017-09-07T10:30:34.227317504', '2017-09-07T10:30:34.257184768'], dtype='datetime64[ns]')
- longitude(atrack, xtrack)float32dask.array<chunksize=(51, 51), meta=np.ndarray>
Array Chunk Bytes 10.16 kiB 10.16 kiB Shape (51, 51) (51, 51) Count 7 Tasks 1 Chunks Type float32 numpy.ndarray - latitude(atrack, xtrack)float32dask.array<chunksize=(51, 51), meta=np.ndarray>
Array Chunk Bytes 10.16 kiB 10.16 kiB Shape (51, 51) (51, 51) Count 7 Tasks 1 Chunks Type float32 numpy.ndarray - elevation(atrack, xtrack)float32dask.array<chunksize=(51, 51), meta=np.ndarray>
Array Chunk Bytes 10.16 kiB 10.16 kiB Shape (51, 51) (51, 51) Count 6 Tasks 1 Chunks Type float32 numpy.ndarray - incidence(atrack, xtrack)float32dask.array<chunksize=(51, 51), meta=np.ndarray>
Array Chunk Bytes 10.16 kiB 10.16 kiB Shape (51, 51) (51, 51) Count 6 Tasks 1 Chunks Type float32 numpy.ndarray - sigma0_raw(pol, atrack, xtrack)float64dask.array<chunksize=(1, 51, 51), meta=np.ndarray>
Array Chunk Bytes 40.64 kiB 20.32 kiB Shape (2, 51, 51) (1, 51, 51) Count 32 Tasks 2 Chunks Type float64 numpy.ndarray - nesz(pol, atrack, xtrack)float64dask.array<chunksize=(1, 51, 51), meta=np.ndarray>
Array Chunk Bytes 40.64 kiB 20.32 kiB Shape (2, 51, 51) (1, 51, 51) Count 41 Tasks 2 Chunks Type float64 numpy.ndarray - gamma0_raw(pol, atrack, xtrack)float64dask.array<chunksize=(1, 51, 51), meta=np.ndarray>
Array Chunk Bytes 40.64 kiB 20.32 kiB Shape (2, 51, 51) (1, 51, 51) Count 32 Tasks 2 Chunks Type float64 numpy.ndarray - negz(pol, atrack, xtrack)float64dask.array<chunksize=(1, 51, 51), meta=np.ndarray>
Array Chunk Bytes 40.64 kiB 20.32 kiB Shape (2, 51, 51) (1, 51, 51) Count 41 Tasks 2 Chunks Type float64 numpy.ndarray - sigma0(pol, atrack, xtrack)float64dask.array<chunksize=(1, 51, 51), meta=np.ndarray>
Array Chunk Bytes 40.64 kiB 20.32 kiB Shape (2, 51, 51) (1, 51, 51) Count 61 Tasks 2 Chunks Type float64 numpy.ndarray - gamma0(pol, atrack, xtrack)float64dask.array<chunksize=(1, 51, 51), meta=np.ndarray>
Array Chunk Bytes 40.64 kiB 20.32 kiB Shape (2, 51, 51) (1, 51, 51) Count 61 Tasks 2 Chunks Type float64 numpy.ndarray
- ipf :
- 2.84
- platform :
- SENTINEL-1A
- swath :
- IW
- product :
- GRDH
- pols :
- VV VH
- name :
- SENTINEL1_DS:/tmp/S1A_IW_GRDH_1SDV_20170907T103020_20170907T103045_018268_01EB76_Z010.SAFE:IW
- start_date :
- 2017-09-07 10:30:20.936409
- stop_date :
- 2017-09-07 10:30:45.935264
- footprint :
- POLYGON ((-69.12591138027886 20.21973939326448, -69.22181702124746 20.23689191938486, -69.24030337778873 20.1451444225978, -69.14445534654266 20.1279837605188, -69.12591138027886 20.21973939326448))
- coverage :
- 10km * 10km (atrack * xtrack )
- pixel_atrack_m :
- 203.5818629367982
- pixel_xtrack_m :
- 199.7376649646133
- orbit_pass :
- Descending
- platform_heading :
- -167.7668824808032
[ ]: