|
| 1 | +""" |
| 2 | +Main script |
| 3 | +- converts lat/lon coordinates of corner reflectors into azimuth and range |
| 4 | + pixel positions |
| 5 | +- can be run after SLC images have been coregistered to a DEM and |
| 6 | + lat/lon coordinates have been derived (using calc_lat_lon_TxxxX) |
| 7 | +
|
| 8 | +INPUT: |
| 9 | +txtfile: CR_site_lon_lat_hgt_date.txt located in ../GAMMA/TxxxX/ |
| 10 | + *sar_latlon.txt file containing lon/lat values, located in ../GAMMA/TxxxX/DEM |
| 11 | + ./DEM/diff*.par file containing length/width of radar-coded DEM, located in ../GAMMA/TxxxX/DEM |
| 12 | +
|
| 13 | +OUTPUT: |
| 14 | +textfile: CR_site_lon_lat_hgt_date_az_rg.txt |
| 15 | + same as Input file with two columns added containing |
| 16 | + azimuth and range position of central pixel at CR location |
| 17 | +
|
| 18 | +@author: Thomas Fuhrmann @ GA June, 2018 |
| 19 | +
|
| 20 | +usage: |
| 21 | +this script is currently located and executed in the TxxxX directory |
| 22 | +Load python3 module on NCI: module load python3 |
| 23 | +then execute, e.g.: python3 get_CR_position_rdc.py |
| 24 | +for effective parallel processing and if memory is exceeded, start an interactive session: |
| 25 | +qsub -I -q express -lstorage=gdata/dg9,walltime=1:00:00,mem=32Gb,ncpus=7,wd |
| 26 | +""" |
| 27 | + |
| 28 | + |
| 29 | +# import modules |
| 30 | +import sys |
| 31 | +import os |
| 32 | +import os.path |
| 33 | +import math |
| 34 | +import fnmatch |
| 35 | +import numpy as np |
| 36 | +# for parallel processing: module joblib has to be installed |
| 37 | +from joblib import Parallel, delayed |
| 38 | +import multiprocessing |
| 39 | + |
| 40 | + |
| 41 | +####################### |
| 42 | +### Path and file names |
| 43 | +####################### |
| 44 | +# give general processing path here |
| 45 | +io_path = "/g/data/dg9/INSAR_ANALYSIS/CAMDEN/S1/GAMMA/T147D/" |
| 46 | +# note that the GAMMA "DEM" folder is assumed inside this path |
| 47 | +dem_path = io_path + "DEM/" |
| 48 | +# give name of CR input file consisting of site name, longitude, latitude (+ optional column fields) |
| 49 | +cr_filename = "CR_site_lon_lat_hgt_date1_date2.txt" |
| 50 | +# name of CR output file (automatic naming): |
| 51 | +cr_filename_out = cr_filename[:-4] + "_az_rg_initial.txt" |
| 52 | +######## |
| 53 | +# set number of processors here: |
| 54 | +nCPU = 7 |
| 55 | +######## |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +# Welome |
| 60 | +print() |
| 61 | +print("########################################") |
| 62 | +print("# Get CR position in radar-coordinates #") |
| 63 | +print("########################################") |
| 64 | + |
| 65 | +print() |
| 66 | + |
| 67 | + |
| 68 | +# Read CR_site_lon_lat_hgt_date1_date2.txt |
| 69 | +print("Reading textfile with CR positions (latitude longitude height date)\ |
| 70 | +...") |
| 71 | +filename_in = io_path + cr_filename |
| 72 | +if os.path.isfile(filename_in) and os.access(filename_in, os.R_OK): |
| 73 | + print("File", filename_in, "exists and is readable.") |
| 74 | + f = open(filename_in) |
| 75 | + sites = f.readlines() |
| 76 | + sites[:] = [line.split("\t")[0] for line in sites] |
| 77 | + f = open(filename_in) |
| 78 | + lons = f.readlines() |
| 79 | + lons[:] = [float(line.split("\t")[1]) for line in lons] |
| 80 | + f = open(filename_in) |
| 81 | + lats = f.readlines() |
| 82 | + lats[:] = [float(line.split("\t")[2]) for line in lats] |
| 83 | +else: |
| 84 | + print("ERROR: Can't read file", filename) |
| 85 | + sys.exit() |
| 86 | +print() |
| 87 | + |
| 88 | + |
| 89 | +# Read diff.par file |
| 90 | +for file in os.listdir(dem_path): |
| 91 | + if fnmatch.fnmatch(file, "diff*.par"): |
| 92 | + filename = dem_path + file |
| 93 | + if os.path.isfile(filename) and os.access(filename, os.R_OK): |
| 94 | + f = open(filename) |
| 95 | + lines = f.readlines() |
| 96 | + for line in lines: |
| 97 | + if "range_samp_1" in line: |
| 98 | + width = int(line.split()[1]) |
| 99 | + if "az_samp_1" in line: |
| 100 | + length = int(line.split()[1]) |
| 101 | + else: |
| 102 | + print("ERROR: Can't read file", filename) |
| 103 | + sys.exit() |
| 104 | +print("Width of radar-coded data (i.e. number of range samples):", width) |
| 105 | +print("Length of radar-coded data (i.e. number of azimuth lines):", length) |
| 106 | +print() |
| 107 | + |
| 108 | + |
| 109 | +# Read lat/lon coordinate file |
| 110 | +for file in os.listdir(dem_path): |
| 111 | + if fnmatch.fnmatch(file, "*sar_latlon.txt"): |
| 112 | + filename = dem_path + file |
| 113 | + if os.path.isfile(filename) and os.access(filename, os.R_OK): |
| 114 | + print("File", filename, "exists and is readable.") |
| 115 | + else: |
| 116 | + print("ERROR: Can't read file", filename) |
| 117 | + sys.exit() |
| 118 | +print() |
| 119 | + |
| 120 | + |
| 121 | +# Convert lat/lon diff to metric using the CR lat/lon values |
| 122 | +# Constants |
| 123 | +a = 6378137 |
| 124 | +f = 0.0033528107 |
| 125 | +e2 = 1/(1-f)**2-1 |
| 126 | +c = a*math.sqrt(1+e2) |
| 127 | + |
| 128 | + |
| 129 | +def _inner(cr): |
| 130 | + lonCR = cr[0][0] |
| 131 | + latCR = cr[0][1] |
| 132 | + f = open(filename) |
| 133 | + lines = f.readlines() |
| 134 | + dist_sq_min = 999 # in deg |
| 135 | + az = 0 |
| 136 | + rg = 0 |
| 137 | + for line in lines: |
| 138 | + lat = float(line.split(" ")[0]) |
| 139 | + lon = float(line.split(" ")[1]) |
| 140 | + dist_sq = (lon-lonCR)**2 + (lat-latCR)**2 |
| 141 | + if dist_sq < dist_sq_min: |
| 142 | + dist_sq_min = dist_sq |
| 143 | + az_CR = az |
| 144 | + rg_CR = rg |
| 145 | + if rg < width-1: |
| 146 | + rg = rg + 1 |
| 147 | + else: |
| 148 | + rg = 0 |
| 149 | + az = az + 1 |
| 150 | + # save az and rg coordinates to list |
| 151 | + #az_CRs.append(az_CR) |
| 152 | + #rg_CRs.append(rg_CR) |
| 153 | + # print distance of pixel coordinate to CR coordinate |
| 154 | + dist_min = math.sqrt(dist_sq_min) |
| 155 | + V = math.sqrt((1+(e2*math.cos(latCR/180*math.pi)**2))) |
| 156 | + N = c/V |
| 157 | + M = c/V**3 |
| 158 | + dist_min_m = dist_min/180*math.pi*math.sqrt(M*N) |
| 159 | + print("Pixel with minimum distance to CR location is:") |
| 160 | + print("azimuth: %d, range: %d" % (az_CR, rg_CR)) |
| 161 | + print("Distance is %8.6f degree or %3.1f m" % (dist_min, dist_min_m)) |
| 162 | + return az_CR, rg_CR |
| 163 | +print() |
| 164 | + |
| 165 | + |
| 166 | +################################## |
| 167 | +# function for parallel loop processing |
| 168 | +keys = sites |
| 169 | +values = (np.array([[lons[i], lats[i]]], dtype=float) \ |
| 170 | + for i in range(0,len(sites))) |
| 171 | +sites = dict(zip(keys, values)) |
| 172 | +names = sites.keys() |
| 173 | +# note that dictionaries are unsorted and the variable name is hence not ordered |
| 174 | +def processInput(name): |
| 175 | + cr = sites[name] |
| 176 | + az, rg = _inner(cr) |
| 177 | + return az, rg |
| 178 | + |
| 179 | +# parallel processing of MLI read and RCS, SCR calculation |
| 180 | +num_cores = multiprocessing.cpu_count() |
| 181 | +# num_cores results in 32 on the NCI which in turn results in an error |
| 182 | +# hence the number of 16 cores is hard-coded here |
| 183 | +results = Parallel(n_jobs=nCPU)(delayed(processInput)(name) for name in names) |
| 184 | +az_CRs = [] |
| 185 | +rg_CRs = [] |
| 186 | +for i in range(0,len(names)): |
| 187 | + az_CRs.append(results[i][0]) |
| 188 | + rg_CRs.append(results[i][1]) |
| 189 | +# print(az_CRs) |
| 190 | +# print(rg_CRs) |
| 191 | + |
| 192 | +# save the range and azimuth numbers of pixels in a new txt file |
| 193 | +print("Reading textfile with CR positions (latitude longitude height date)\ |
| 194 | +...") |
| 195 | +filename_out = io_path + cr_filename_out |
| 196 | +fout = open(filename_out,'w') |
| 197 | +if os.path.isfile(filename_in) and os.access(filename_in, os.R_OK): |
| 198 | + print("File", filename_in, "exists and is readable.") |
| 199 | + f = open(filename_in) |
| 200 | + lines = f.readlines() |
| 201 | + idx = 0 |
| 202 | + for line in lines: |
| 203 | + out_line = line.strip("\n") + "\t" + str(az_CRs[idx]) + "\t" + \ |
| 204 | + str(rg_CRs[idx]) + "\n" |
| 205 | + fout.write(out_line) |
| 206 | + idx = idx + 1 |
| 207 | +else: |
| 208 | + print("ERROR: Can't read file", filename_in) |
| 209 | + sys.exit() |
| 210 | +fout.close() |
| 211 | +print() |
| 212 | +print("Azimuth and Range number of CR pixels has been written to " + \ |
| 213 | +filename_out + ".") |
| 214 | +print() |
| 215 | + |
0 commit comments