#! /usr/bin/env python3
"""
# pop_config is part of pyGMTSAR. 
# It is migrated from pop_config.csh.
# Dunyu Liu, 20230206.

# pop_config creates a configuration file config.ini for p2p_processing.
# Syntax: pop_config SAT
# SAT can be ERS, ENVI, ALOS, ALOS_SLC, ALOS2, ALOS2_SCAN.
# S1_STRIP, S1_TOPS,, CSK_RAW, CSK_SLC, TSX, RS2.
"""
import sys, os

def pop_config():
    n = len(sys.argv)
    # n should be 2. pop_config SAT
    if n!=2:
        print(" ")
        print("Usage: pop_config.csh SAT")
        print(" ")
        print("       SAT can be ERS, ENVI, ALOS, ALOS_SLC, ALOS2, ALOS2_SCAN")
        print("       S1_STRIP, S1_TOPS, CSK_RAW, CSK_SLC, TSX, RS2, GF3")
        print(" ")
        sys.exit("Error: necessary input file(s) not provided ... ...")
    
    SAT = sys.argv[1]
        
    print( "Generating config files for " + SAT + " formats ... ...")
    
    conf_f = "config.py"
    sys.stdout = open(conf_f, "w")
    
    print( "#! /usr/bin/env python3 ")
    
    # Below are printouts from pop_config.csh.
    print( "#")
    print( "# This is an example configuration file for p2p_processing.csh")
    print( "#")
    print( "# all the comments or explanations are marked by '#'")
    print( "# The parameters in this configuration file is distinguished by their first word so ")
    print( "# user should follow the naming of each parameter.")
    print( "# the parameter name, '=' sign, parameter value should be separated by space \"  \". ")
    print( "# leave the parameter value blank if using default value. ")
    print( "# ")
    print( "# DO NOT DIRECTLY COMMENT PARAMTERS WITH '#' !!! ")
    print( "# THIS WILL DUPLICATE PARAMETERS AND CAUSE TROUBLE !!! ")
    print( "# ")
    print( "  ")
    print( "#####################")
    print( "# processing stage  #")
    print( "#####################")
    print( "# 1 - start from preprocess")
    print( "# 2 - start from align SLC images")
    print( "# 3 - start from make topo_ra ")
    print( "# 4 - start from make and filter interferograms ")
    print( "# 5 - start from unwrap phase")
    print( "# 6 - start from geocode  ")
    proc_stage = 1
    #skip_stage = -999
    print( "proc_stage = 1")
    print( "skip_stage = -999")
    print( "skip_1 = 0")
    print( "skip_2 = 0")
    print( "skip_3 = 0")
    print( "skip_4 = 0")
    print( "skip_5 = 0")
    print( "skip_6 = 0")
    print( "")
    print( "# to work on both use 0, on only aligned image use 1 (assuming master image is done)")
    print( "# to work on only master image use 2")
    skip_master = 0
    print( "skip_master = 0")
    print( "")
    print( "##################################")
    print( "#   parameters for preprocess    #")
    print( "#   - pre_proc.csh               #")
    print( "##################################")
    print( "# num of patches")
    #num_patches = -999
    print( "num_patches = -999")
    print( "")
    print( "# earth radius ")
    #earth_radius = -999
    print( "earth_radius = -999")
    print( "")
    print( "# near_range")
    #near_range = -999
    print( "near_range = -999")
    print( "")
    print( "# Doppler centroid ")
    #fd1 = -999
    print( "fd1 = -999")
    print( "")
    
    if SAT == "S1_TOPS":
        print( "# apply spectral diversity to remove burst discontinuity")
        spec_div  = 0
        print( "spec_div = " + str(spec_div))
        print( "# spectral diversity mode, run align_tops_esd.csh to figure out the mode specification")
        spec_mode = 1
        print( "spec_mode = " + str(spec_mode))
    
    print(" ")
    
    if SAT == "ALOS_SLC":
        print( "# SLC scale factor to convert float to int ")
        SLC_factor = 0.02
        print( "SLC_factor = " + str(SLC_factor))
        print( "")
    else: 
        if SAT == "ALOS2" or SAT == "ALOS2_SCAN":
            print( "# SLC scale factor to convert float to int")
            SLC_factor = 2.0
            print( "SLC_factor = " + str(SLC_factor))
            print( " ")

    print( "################################################")
    print( "#   parameters for focus and align SLC images  #")
    print( "#   - align.csh                                #")
    print( "################################################")
    print( "# region to cut in radar coordinates (leave it blank if process the whole image)")
    print( "# example 300/5900/0/25000")
    print( "region_cut = -999")
    print( "")
    print( "#")
    print( "#####################################")
    print( "#   parameters for make topo_ra     #")
    print( "#   - dem2topo_ra.csh               #")
    print( "#####################################")
    print( "# subtract topo_ra from the phase")
    print( "#  (1 -- yes; 0 -- no)")
    topo_phase = 1
    print( "topo_phase = 1")
    print( "# if above parameter = 1 then one should have put dem.grd in topo/")
    print( "")
    print( "# interpolation approach, 0 for surface, 1 for triangulation")
    topo_interp_mode = 0
    print( "topo_interp_mode = 0")
    print( "")
    print( "# topo_ra shift (1 -- yes; 0 -- no)")
    
    if SAT == "ALOS_SLC" or SAT == "ALOS" or SAT == "ERS":
        shift_topo = 1
        print( "shift_topo = 1")
    else: 
        shift_topo = 0
        print( "shift_topo = 0")

    print( "")
    print( "####################################################")
    print( "#   parameters for make and filter interferograms  #")
    print( "#   - intf.csh                                     #")
    print( "#   - filter.csh                                   #")
    print( "####################################################")
    print( "# switch the master and aligned when doing intf. ")
    print( "# put '1' if assume master as repeat and aligned as reference ")
    print( "# put '0' if assume master as reference and aligned as repeat [Default]")
    print( "# phase = repeat phase - reference phase")
    switch_master = 0
    print( "switch_master = 0")
    print( "")
    print( "# filters ")
    print( "# look at the filter/ folder to choose other filters")
    print( "# for tops processing, to force the decimation factor")
    print( "# recommended range decimation to be 8, azimuth decimation to be 2")

    if SAT == "ALOS2_SCAN":
        filter_wavelength = 400
        print( "filter_wavelength = 400")
    elif SAT == "RS2" or SAT == "TSX":
        filter_wavelength = 100
        print( "filter_wavelength = 100")
    else:
        filter_wavelength = 200
        print( "filter_wavelength = 200")

    print( "")
    print( "# decimation of images ")
    print( "# decimation control the size of the amplitude and phase images. It is either 1 or 2.")
    print( "# Set the decimation to be 1 if you want higher resolution images.")
    print( "# Set the decimation to be 2 if you want images with smaller file size.")
    print( "# ")

    if SAT == "RS2" or SAT == "TSX":
        dec_factor = 1 
        print( "dec_factor = 1 ")
    elif SAT == "ALOS2_SCAN":
        dec_factor = 4 
        print( "dec_factor = 4 ")
    else:
        dec_factor = 2 
        print( "dec_factor = 2 ")

    if SAT == "S1_TOPS":
        range_dec = 8
        azimuth_dec = 2
        print( "range_dec = 8")
        print( "azimuth_dec = 2")
    elif SAT == "ALOS2_SCAN":
        range_dec = 4
        azimuth_dec = 8
        print( "range_dec = 4")
        print( "azimuth_dec = 8")

    print( "#")
    print( "# compute phase gradient, make decimation to 1 above and filter wavelength small for better quality")
    print( "#")
    compute_phase_gradient = 0
    print( "compute_phase_gradient = 0")
    print( "#")
    print( "# make ionospheric phase corrections using split spectrum method")
    correct_iono = 0
    iono_filt_rng = 1.0
    iono_filt_azi = 1.0
    iono_dsamp = 1
    print( "correct_iono = 0")
    print( "iono_filt_rng = 1.0")
    print( "iono_filt_azi = 1.0")
    print( "iono_dsamp = 1")
    print( "# ")
    print( "# set the following parameter to skip ionospheric phase estimation")
    iono_skip_est = 1 
    print( "iono_skip_est = 1 ")
    print( "#")
    print( "#####################################")
    print( "#   parameters for unwrap phase     #")
    print( "#   - snaphu.csh                    #")
    print( "#####################################")
    print( "# correlation threshold for snaphu.csh (0~1)")
    print( "# set it to be 0 to skip unwrapping.")
    threshold_snaphu = 0
    print( "threshold_snaphu = 0")
    print( "")
    print( "# interpolate masked or low coherence pixels with their nearest neighbors, 1 means interpolate, ")
    print( "# others or blank means using original phase, see snaphu.csh and snaphu_interp.csh for details")
    print( "# this could be very slow in case a large blank area exist")
    near_interp = 0
    print( "near_interp = 0")
    print( "")
    print( "# mask the wet region (Lakes/Oceans) before unwrapping (1 -- yes; else -- no)")
    mask_water = 1
    print( "mask_water = 1")
    print( "")
    print( "#")
    print( "# Allow phase discontinuity in unrapped phase. This is needed for interferograms having sharp phase jumps.")
    print( "# defo_max = 0 - used for smooth unwrapped phase such as interseismic deformation")
    print( "# defo_max = 65 - will allow a phase jump of 65 cycles or 1.82 m of deformation at C-band")
    print( "#")
    defomax = 0
    print( "defomax = 0")
    print( "")
    print( "#####################################")
    print( "#   parameters for geocode          #")
    print( "#   - geocode.csh                   #")
    print( "#####################################")
    print( "# correlation threshold for geocode.csh (0< threshold <=1), set 0 to skip")
    threshold_geocode = .10
    print( "threshold_geocode = .10")
    print( "")
    print( "#####################################")
    print( "#   Other parameters                #")
    print( "#####################################")
    print( "")
    
    if SAT == "S1_TOPS" or SAT == "ALOS2_SCAN":
        print( "# determine stitching location for TOPS and ALOS2SanSAR data using nan-s surrounding images from subswaths")
        det_stitch = 0
        print( "det_stitch = 0")
    
    sys.stdout.close()

def _main_func(description):
    pop_config()

if __name__ == "__main__":
    _main_func(__doc__)
