Custom Water Masks

629 views
Skip to first unread message

Jasmine Hansen

unread,
May 10, 2022, 2:04:29 PM5/10/22
to MintPy
Hi all,

Is it possible to use a custom water mask with mintpy?

I have a binary geotiff file that is set to a particular NDWI threshold that it would be great to use. I have tried a simple geocode.py command on the file but that yields an empty h5 file.

I have alternatively tried to download the USGS water body mask using the command below, but it is not able to find files within my AOI (perhaps because it is so far north?).

wbd.py 65.882356 67.746285 -54.302186 -48.615277

Any help with either the custom water mask, or wbd.py command issues would be fantastic,

cheers
Jasmine

Zhang Yunjun

unread,
May 12, 2022, 8:28:57 PM5/12/22
to MintPy
Hi Jasmine,

wbd.py is downloading the water body product from SRTM, which does not have data beyond N60, if I understand it right.

Maybe you have checked this notebook (https://nbviewer.org/github/insarlab/MintPy-tutorial/blob/main/applications/water_mask.ipynb). For input data generated by isce-2 stack processors, you could use geocode.py --geo2radar to convert the water mask from geo to radar coordinates. I think geocode.py could take geotiff as input, if this is not the case, please let me know, it should be easily fixed.

Cheers,
Yunjun

Jasmine Hansen

unread,
Aug 22, 2022, 1:03:29 PM8/22/22
to MintPy
Hi Yunjun,
Apologies for getting back to this so late.
Thank you for the link - I have tried creating masks from the geometry file, however I have lots of intricate lakes that do not fit well with the threshold method.
I have a binary geotiff file which I have tried to convert to radar coordinates using the command below. However, it states it cannot geocode without a lookup table.

Do you have any advice?
geocode.py --geo2radar kanger_water_mask_0_4.tif kanger_water_rad.tif

No lookup table info range/lat found in files.
Traceback (most recent call last):
  File "/Users/jasminehansen/miniconda3/envs/mintpy/bin/geocode.py", line 10, in <module>
    sys.exit(main())
  File "/Users/jasminehansen/miniconda3/envs/mintpy/lib/python3.8/site-packages/mintpy/geocode.py", line 367, in main
    inps = cmd_line_parse(iargs)
  File "/Users/jasminehansen/miniconda3/envs/mintpy/lib/python3.8/site-packages/mintpy/geocode.py", line 119, in cmd_line_parse
    inps = _check_inps(inps)
  File "/Users/jasminehansen/miniconda3/envs/mintpy/lib/python3.8/site-packages/mintpy/geocode.py", line 143, in _check_inps
    raise FileNotFoundError('No lookup table found! Can not geocode without it.')
FileNotFoundError: No lookup table found! Can not geocode without it.

thanks,
jasmine

bbuzz

unread,
Sep 3, 2022, 6:40:36 PM9/3/22
to MintPy
Hi Jasmine, you have to provide information regarding the mapping from radar to geo coordinates to geocode.py.
As Yunjun said, if you used ISCE (and possibly others) to need to provide paths to the relevant files to geocode.py with --lat-file / --lon-file

Manudeo Singh

unread,
Sep 4, 2022, 3:08:28 AM9/4/22
to min...@googlegroups.com
Hi Jasmin,
Load your data without using the waterMask file. This will produce a Geometry file in your input folder. Use that geometry file as lookup file for your processings with waterMask or to create that mask. 

Best,
Manu

--
You received this message because you are subscribed to the Google Groups "MintPy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mintpy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mintpy/4fc92cb1-0d92-461a-adab-3438e2baf363n%40googlegroups.com.
--
_________________________________
Dr. Manudeo Singh
Alexander von Humboldt Postdoctoral Fellow
Universität Potsdam
University of Potsdam
Institut für Geowissenschaften
Institute of Geosciences
Potsdam, Germany

Jasmine Hansen

unread,
Sep 9, 2022, 6:23:44 PM9/9/22
to MintPy
Hi all,
thanks for the replies - I shall try again with the lat lon files from ISCE.
cheers
Jasmine

Panji Brotoisworo

unread,
Sep 11, 2022, 6:15:42 AM9/11/22
to MintPy
Hi,

I'm trying to create a water mask from a binary tiff file using SNAP as processor. I created a binary water mask manually in QGIS. I tried the suggestions from this thread but I get errors.

Here are the results

geometryGeo.h5 as lookup table

python geocode.py C:\Users\panji\Documents\Projects\panij-geospatial\test-out\water_mask.tif -o C:\Users\panji\Documents\Projects\panij-geospatial\test-out\water_mask.h5 -l C:\Users\panji\Documents\Projects\panij-geospatial\test-out\data-out-mintpy\inputs\geometryGeo.h5 --geo2radar

number of processor to be used: 1
resampling software: pyresample

Traceback (most recent call last):
  File "geocode.py", line 376, in <module>
    main(sys.argv[1:])
  File "geocode.py", line 369, in main
    run_geocode(inps)
  File "geocode.py", line 273, in run_geocode
    res_obj.prepare()
  File "C:\Users\panji\.conda\envs\insar\lib\site-packages\mintpy\objects\resample.py", line 166, in prepare
    self.prepare_geometry_definition_geo()
  File "C:\Users\panji\.conda\envs\insar\lib\site-packages\mintpy\objects\resample.py", line 585, in prepare_geometry_definition_geo
    raise ValueError('geo2radar with lookup table in geo-coord it NOT supported yet!')
ValueError: geo2radar with lookup table in geo-coord it NOT supported yet!

latlon files as lookup table

python geocode.py C:\Users\panji\Documents\Projects\panij-geospatial\test-out\water_mask.tif -o C:\Users\panji\Documents\Projects\panij-geospatial\test-out\water_mask.h5 --lat-file C:\Users\panji\Documents\Projects\panij-geospatial\test-out\latitude_tc.data\latitude.img --lon-file C:\Users\panji\Documents\Projects\panij-geospatial\test-out\longitude_tc.data\longitude.img --geo2radar

No lookup table info range/lat found in files.
number of processor to be used: 1
resampling software: pyresample

Traceback (most recent call last):
  File "geocode.py", line 376, in <module>
    main(sys.argv[1:])
  File "geocode.py", line 369, in main
    run_geocode(inps)
  File "geocode.py", line 273, in run_geocode
    res_obj.prepare()
  File "C:\Users\panji\.conda\envs\insar\lib\site-packages\mintpy\objects\resample.py", line 166, in prepare
    self.prepare_geometry_definition_geo()
  File "C:\Users\panji\.conda\envs\insar\lib\site-packages\mintpy\objects\resample.py", line 585, in prepare_geometry_definition_geo
    raise ValueError('geo2radar with lookup table in geo-coord it NOT supported yet!')
ValueError: geo2radar with lookup table in geo-coord it NOT supported yet!

Zhang Yunjun

unread,
Sep 11, 2022, 4:10:47 PM9/11/22
to MintPy
Hi Panji,

The geocode.py --geo2radar option is resampling the geo-coded water mask into radar coordinates. Assuming your SNAP products are already in geo-coordinates, you would not want to use this. 

Simply resizing your water mask into the same resolution and spatial extent as your InSAR products will do. There is no mintpy script for this, but it would be very easy to do in python with skimage.transform.resize().

Cheers,
Yunjun

Sven Borgstrom

unread,
Sep 21, 2022, 8:14:09 AM9/21/22
to MintPy
Hi Yunjun,

I'm trying to get a water mask in radar coordinates following your Tutorial in Jupyter Notebook but unsuccessfully, as I regularly get the waterBody.h5 file, but the waterMask.h5 file results in a light green one (see the attached files).

This result is the same for both option 1, using "generate_mask.py" and option 2, using "wbd.py".

In the past I never had this problem: how can I fix this?

Thanks,
Sven.
waterMask.png
waterBody.png

Zhang Yunjun

unread,
Sep 22, 2022, 9:00:44 PM9/22/22
to MintPy
Hi Sven,

I was able to reproduce the same error as yours. It was due to a change in mintpy while reading the *.wbd file, but I did not try to locate which change caused this. Anyway, the fix is easy and committed. 

Please check the latest version of the water_mask.ipynb here (https://github.com/insarlab/MintPy-tutorial/blob/main/applications/water_mask.ipynb).

Thank you for reporting this issue.

Cheers,
Yunjun

Sven Borgstrom

unread,
Sep 23, 2022, 1:44:37 PM9/23/22
to MintPy
Hi Yunjun,
your waterMask Tutorial woks fine now.
Thanks,
Sven.

Jasmine Hansen

unread,
Oct 19, 2022, 6:36:05 PM10/19/22
to MintPy
Hi Yunjun,
Hope you are well.
I followed the recommendations above to try and make a custom water mask but am still running into some issues. I do the following steps:
1. Generate binary geotiff mask in EPSG:4326
2. Use gdalwarp to convert the geotiff to the bounds and resolution of the insar data. (-tr xres yres -te xmin ymin xmax ymax)
3. geocode to radar coordinates: geocode.py --geo2radar kanger_water_mask_0_11_reshape.tif -l geometryRadar.h5
4. this results in the following files: rdr_kanger_water_mask_0_11_reshape.tif and rdr_kanger_water_mask_0_11_reshape.tif.rsc
5. I then path to this rdr tif file in the configuration file for mintpy as normal.
6. When i runn smallbaselineApp I get the following error:

create HDF5 file /home/jasmine/JasmineShare/ISCE_OUTPUTS/august/90_216_2/mintpy_test_water/inputs/geometryRadar.h5 with w mode
create dataset /height             of <class 'numpy.float32'>   in size of (4531, 7549) with compression = lzf
create dataset /latitude           of <class 'numpy.float32'>   in size of (4531, 7549) with compression = lzf
create dataset /longitude          of <class 'numpy.float32'>   in size of (4531, 7549) with compression = lzf
create dataset /incidenceAngle     of <class 'numpy.float32'>   in size of (4531, 7549) with compression = lzf
create dataset /azimuthAngle       of <class 'numpy.float32'>   in size of (4531, 7549) with compression = lzf
create dataset /shadowMask         of <class 'numpy.bool_'>     in size of (4531, 7549) with compression = lzf
create dataset /waterMask          of <class 'numpy.bool_'>     in size of (4531, 7549) with compression = lzf
ERROR 4: `inputs/rdr_kanger_water_mask_0_11_reshape.tif' not recognized as a supported file format.

Traceback (most recent call last):
  File "/home/jasmine/miniconda3/envs/mintpy_jh/bin/smallbaselineApp.py", line 10, in <module>
    sys.exit(main())
  File "/home/jasmine/miniconda3/envs/mintpy_jh/lib/python3.8/site-packages/mintpy/smallbaselineApp.py", line 1291, in main
    app.run(steps=inps.runSteps)
  File "/home/jasmine/miniconda3/envs/mintpy_jh/lib/python3.8/site-packages/mintpy/smallbaselineApp.py", line 1047, in run
    self.run_load_data(sname)
  File "/home/jasmine/miniconda3/envs/mintpy_jh/lib/python3.8/site-packages/mintpy/smallbaselineApp.py", line 356, in run_load_data
    mintpy.load_data.main(iargs)
  File "/home/jasmine/miniconda3/envs/mintpy_jh/lib/python3.8/site-packages/mintpy/load_data.py", line 936, in main
    geom_radar_obj.write2hdf5(
  File "/home/jasmine/miniconda3/envs/mintpy_jh/lib/python3.8/site-packages/mintpy/objects/stackDict.py", line 698, in write2hdf5
    data = np.array(self.read(family=dsName,
  File "/home/jasmine/miniconda3/envs/mintpy_jh/lib/python3.8/site-packages/mintpy/objects/stackDict.py", line 464, in read
    data, metadata = readfile.read(self.file,
  File "/home/jasmine/miniconda3/envs/mintpy_jh/lib/python3.8/site-packages/mintpy/utils/readfile.py", line 295, in read
    data, atr = read_binary_file(fname,
  File "/home/jasmine/miniconda3/envs/mintpy_jh/lib/python3.8/site-packages/mintpy/utils/readfile.py", line 655, in read_binary_file
    data = read_gdal(
  File "/home/jasmine/miniconda3/envs/mintpy_jh/lib/python3.8/site-packages/mintpy/utils/readfile.py", line 1825, in read_gdal
    bnd = ds.GetRasterBand(band)
AttributeError: 'NoneType' object has no attribute 'GetRasterBand'

Would this mean that the data needs to be converted to a HDF5 file?
cheers
Jasmine

Zhang Yunjun

unread,
Oct 27, 2022, 12:30:55 AM10/27/22
to MintPy
Hi Jasmine,

I think your guess is right: using `geocode.py -o ` option to save the rdr_kanger_water_mask_0_11_reshape file in HDF5 format should fix the issue.

mintpy could read *.tif file, but could not write *.tif file in a general way (except in save_gdal.py where special care are taken, but this is not the case for other scripts/modules)

Yunjun
Reply all
Reply to author
Forward
0 new messages