---
# System prepended metadata

title: Miaplpy Processing Note 分散式散射點演算法
tags: [insar, timeseries, mintpy, PSDS]

---


# Miaplpy Processing Note 分散式散射點演算法

Miaplpy([MIAmi Phase Linking in PYthon](https://github.com/insarlab/MiaplPy))為分散式散射體(Distributed scatters, DS)的InSAR地表變位架構。

其運算以Mintpy基礎，新增了相位連結法(Phase linking)，估計分散式散射點，提供更高覆蓋的地表變位(成果好壞筆者認為見仁見智)。

相關筆記本如附:
:::info
參考筆記：
- ISCE-TOPS stack 
https://hackmd.io/NfHR48A1Q8u2LiceJH84OQ
- mintpy 
https://hackmd.io/oPMP3nM7QD-woHP_KjN4BA
:::
## Installation 安裝
需要與mintpy安裝於同一個環境及版本需求，須注意相容性問題。

## Step 1-1: Prepare coregistration stack 準備資料
* 首先我們採用ISCE來進行影像配準(Coregistration)作業，與Mintpy不同，這邊需要建立slc stack而非interferogram stack，可以參考上面提到的TOPS stacks workflow。
* 但miaplpy的基線資料不來自merged/baselines，所以如果即使執行了interferogram workflow，還是可以直接執行miaplpy，無須重新執行
* 建議建立project的資料夾含有Sen字段，後續miaplpyApp才能夠自動判斷衛星種類
* 如果需要節省資料儲存空間，於stackSentinel.py 可加入 `-V true`的指令，對miaplpy沒有影響。
* 完成後可以獲得資料夾結構如下：
```
merged
├── baselines
│   ├── 20150116
│  ...  ├── 20150116
│       ├── 20150116.full.vrt
│       ├── 20150116.vrt
│       └── 20150116.xml
│
├── geom_reference
│   ├── lat.rdr.full ... 
│   ├── lon.rdr.full ...
│   ├── los.rdr.full ... 
│   └── shadowMask.rdr.full ...
│
└── SLC
    ├── 20221229
   ...  ├── 20221229.slc.full
        ├── 20221229.slc.full.aux.xml
        ├── 20221229.slc.full.vrt
        ├── 20221229.slc.full.xml
        └── 20221229.slc.hdr       
```
## step 1-2 crop SLC stack 裁減分析範圍
- 裁減方法1: 於miaplpyApp.cfg之load_data中指定subset。
- 裁減方法2: 可以參考本篇筆記 step 1 
https://hackmd.io/Je0fJkX_TsS1nqCoaW2bwA


## step 2: miaplpy comfiguration file 建立專案檔
- 可以透過miaplpyApp -H 來獲得template(包含下述兩個專案檔)。
- 需要建立"兩個"檔案(不可以放同一個): miaplpyApp.cfg, smallbaselinesApp.cfg，內容如template所示，檔名不建議自訂。
- maiplpyApp.cfg有2處要改(其他則是看需求)
    1. 專案檔路徑: 基本上與mintpy相同
    2. phase linking方法: 基本上也不需要更動
        - 統計方法(Default: anderson-darling test)
        - 統計窗函數大小
        - 相位估計方式: EMI、EVD、PTA、...(論文建議最佳為Sequential-EMI)
    3. 時間序列分析方法: 
        - 干涉對分析方式：single reference (Default)、mini-stack、sequential、delauney (建議可改為mini-stack或sequential)
        - filter strength: 尚未確定效果，可加可不佳(因為phase linking 本身可以視為一種濾波器)
- smallbaselineApp.cfg 同mintpy，改需要的參數即可。

下列$PROJECT請代換為自訂之路徑：
```bash=
miaplpy.load.processor      = auto  #[isce,snap,gamma,roipac], auto for isceTops
miaplpy.load.updateMode     = auto  #[yes / no], auto for yes, skip re-loading if HDF5 files are complete
miaplpy.load.compression    = no  #[gzip / lzf / no], auto for no.
miaplpy.load.autoPath       = no    # [yes, no] auto for no
##---------Coregistered SLC images:
miaplpy.load.slcFile        = $PROJECT/MERGED/SLC/*/*.slc.full  #[path2slc_file]                    
miaplpy.load.startDate      = auto  #auto for first date
miaplpy.load.endDate        = auto  #auto for last date                                           
##---------for ISCE only:                                                                         
miaplpy.load.metaFile       = $PROJECT/reference/IW*.xml  #[path2metadata_file], i.e.: ./reference/IW1.xml, ./referenceShelve/data.dat                                                  
miaplpy.load.baselineDir    = $PROJECT/baselines  #[path2baseline_dir], i.e.: ./baselines
##---------geometry datasets:
miaplpy.load.demFile        = $PROJECT/MERGED/geom_reference/hgt.rdr.full  #[path2hgt_file]
miaplpy.load.lookupYFile    = $PROJECT/MERGED/geom_reference/lat.rdr.full  #[path2lat_file], not required for geocoded data
miaplpy.load.lookupXFile    = $PROJECT/MERGED/geom_reference/lon.rdr.full  #[path2lon_file], not required for geocoded data
miaplpy.load.incAngleFile   = $PROJECT/MERGED/geom_reference/los.rdr.full  #[path2los_file], optional
miaplpy.load.azAngleFile    = $PROJECT/MERGED/geom_reference/los.rdr.full  #[path2los_file], optional
miaplpy.load.shadowMaskFile = $PROJECT/MERGED/geom_reference/shadowMask.rdr.full  #[path2shadow_file], optional
miaplpy.load.waterMaskFile  = auto  #[path2water_mask_file], optional
```



## step 3: run steps 執行
本階段執行基本與mintpy相同(也會解釋一些奇怪的bug)，故將著重於解釋個步驟之產品及目的。查看指令說明如下: 
```bash=
usage: miaplpyApp [-h] [--dir WORKDIR] [-g] [-H] [-v] [--walltime WALL_TIME] [--queue QUEUE] [--jobfiles] [--runfiles]
                  [--tmp] [--start STEP] [--end STEP] [--dostep STEP]
                  [customTemplateFile]

Routine Time Series Analysis for MiaplPy

positional arguments:
  customTemplateFile    Custom template with option settings.
                        ignored if the default miaplpyApp.cfg is input.

optional arguments:
  -h, --help            show this help message and exit
  --dir WORKDIR, --work-dir WORKDIR
                        Work directory, (default: None).
  -g                    Generate default template (if it does not exist) and exit.
  -H                    Print the default template file and exit.
  -v, --version         Print software version and exit
  --walltime WALL_TIME  walltime for submitting the script as a job
  --queue QUEUE         Queue name
  --jobfiles            Do not run the tasks, only write job files
  --runfiles            Create run files for all steps
  --tmp                 Copy and process on tmp

steps processing (start/end/dostep):
  Command line options for steps processing with names are chosen from the following list:
          ['load_data', 'phase_linking', 'concatenate_patches', 'generate_ifgram']
          ['unwrap_ifgram', 'load_ifgram', 'ifgram_correction', 'invert_network', 'timeseries_correction']

          In order to use either --start or --step, it is necessary that a
          previous run was done using one of the steps options to process at least
          through the step immediately preceding the starting step of the current run.


  --start STEP          Start processing at the named step, default: load_data
  --end STEP, --stop STEP
                        End processing at the named step, default: timeseries_correction
  --dostep STEP         Run processing at the named step only
```

#### miaplpy function explaination
1. load_data: 
    - 可能會有錯誤，因為在讀入的過程中呼叫(check monitor output)command line 指令prep_slc_isce.py中的file pattern為 `*.full`但是bash可能會自動帶入當下資料夾中的conf.full
    - 錯誤來自maiplpyApp.cfg中的slcPath的字段設定為"\*.full"非"\*.slc.full."
    - 可以`prep_slc_isce.py -f *.full` 改為`prep_slc_isce.py -f "*.full"`
    
2. phase_linking, concatenate_patches: 估計分散式散射點，時間長，通常沒問題(?)
    - 完成後產生inverted資料夾
3. generate_ifgram, unwrap_ifgram, load_ifgram:
    - 完成後產生相當於mintpy中的專案資料夾network_*
(根據干涉對連接方式檔名不同，如network_sequential_3、network_single_reference)
#### 接下來的步驟均為mintpy
4. ifgram_correction: 
    - 相當於在network_*資料夾中執行`smallbaseline .App --start modify_network --stop correct_unwrap_error`
5. invert_network:
    - 基本上就是計算時間序列
6. timeseries_correction
    - 一些剩下的步驟，如geocode、APS、demErr等等。
## Step4: Postprocessing
可參考mintpy筆記。