1
+ ##georeference output from MAGIC
2
+ #IRGS result colour: grounded ice (0,0,254), floating ice (0,254,254) (MAGIC automatically saves 255 to 254)
3
+ #IRGS result (.bmp) should have the same name as the SAR image (.tif).
4
+ #IRGS results and SAR images are in the same folder
5
+ #output in a separte folder
6
+ from osgeo import gdal
7
+ import numpy as np
8
+ import os
9
+ from PIL import Image
10
+ import subprocess
11
+
12
+ def georeference (filename ,output_path ,code ,percent ):
13
+ ds = gdal .Open (filename , gdal .GA_ReadOnly )
14
+ cols = ds .RasterXSize
15
+ rows = ds .RasterYSize
16
+
17
+ bmp = filename [:- 4 ] + '.bmp'
18
+ im = Image .open (bmp )
19
+ p = np .array (im )
20
+ data = np .zeros ((rows , cols ), dtype = np .float )
21
+ landmask = filename [:- 4 ] + '_landmask.bmp'
22
+ mask_array = gdal .Open (landmask ).ReadAsArray ()
23
+
24
+ for i in range (0 , rows ):
25
+ for j in range (0 , cols ):
26
+ if mask_array [i ,j ] == 0 :
27
+ data [i ,j ] = np .NAN
28
+ elif np .all (p [i ,j ] == [0 ,254 ,254 ]):
29
+ data [i ,j ] = 2
30
+ elif np .all (p [i ,j ] == [0 ,0 ,254 ]):
31
+ data [i ,j ] = 1
32
+
33
+ grounded = np .count_nonzero (data == 1 )
34
+ floating = np .count_nonzero (data == 2 )
35
+ percent_g = round (grounded / (grounded + floating ), 4 )
36
+ percent_f = round (floating / (grounded + floating ), 4 )
37
+ print ("grounded ice%: " , percent_g )
38
+ print ("floating ice%: " , percent_f )
39
+
40
+ ##write the image
41
+ ##DATE
42
+ d_start = filename .find ('_EW_GRDM' ) + 14
43
+ d_end = filename .find ('T' , d_start )
44
+ date = filename [d_start :d_end ]
45
+ #percent = output_path + "\\Percentage.txt"
46
+ with open (percent , "a" ) as file :
47
+ #file.write('IRGSEM\n'+'Date\tGrounded ice\tFloating ice'+'\n')
48
+ file .write (date + '\t ' + str (percent_g )+ '\t ' + str (percent_f )+ '\n ' )
49
+ outfile = output_path + '\\ ' + 'H2O.CGI.IRGSEM.SENT1.V01.' + date + '.' + code + '.tif'
50
+ driver = gdal .GetDriverByName ("GTiff" )
51
+ outdata = driver .Create (outfile , cols , rows , 1 , gdal .GDT_Byte )
52
+ outdata .SetGeoTransform (ds .GetGeoTransform ())
53
+ outdata .SetProjection (ds .GetProjection ())##sets same projection as input
54
+ outband = outdata .GetRasterBand (1 )
55
+ ct = gdal .ColorTable ()
56
+ #ct.SetColorEntry(0, (255,255,255,255)) #white background for NaN
57
+ ct .SetColorEntry (0 , (0 ,0 ,0 ,255 ))
58
+ ct .SetColorEntry (1 , (0 ,0 ,255 ,255 )) #grounded ice color(0,0,255), used to be (0,0,102)
59
+ ct .SetColorEntry (2 , (0 ,255 ,255 ,255 )) #floating ice color(0,255,255)
60
+ outband .SetColorTable (ct )
61
+ outband .WriteArray (data )
62
+ #outband.SetNoDataValue(0)
63
+ outband .FlushCache () ##saves to disk!!
64
+ outdata = None
65
+ outband = None
66
+ ds = None
67
+
68
+
69
+ def main ():
70
+ rootdir = r'D:\GlobPermafrost\raw images\C12_Central_Seward_Peninsula\EW\IRGSEM'
71
+ output_georef = rootdir + '\\ ' + 'georef'
72
+ if not os .path .exists (output_georef ):
73
+ os .makedirs (output_georef )
74
+ code = 'CS12_2'
75
+ #C01: Barrow (Alaska, USA)
76
+ #C03: Teshekpuk (Alaska, USA)
77
+ #C04: Mackenzie Delta (Canada)
78
+ #C06: Kytalyk (Russia)
79
+ #C07: Lena Delta, Russia
80
+ #C08: Yamal (Russia)
81
+ #CS12_1: Cape Espenberg Lowland (Alaska, USA)
82
+ #CS12_2: Central Seward Peninsula (Alaska, USA)
83
+ percent = output_georef + "\\ Percentage.txt"
84
+ with open (percent , "a" ) as file :
85
+ file .write ('IRGSEM\n ' + 'Date\t Grounded ice\t Floating ice' + '\n ' )
86
+ for file in os .listdir (rootdir ):
87
+ if file .endswith (".tif" ):
88
+ print (os .path .join (rootdir , file ))
89
+ georeference (os .path .join (rootdir , file ), output_georef , code , percent )
90
+ output_path = output_georef + '\\ ' + 'UTM'
91
+ if not os .path .exists (output_path ):
92
+ os .makedirs (output_path )
93
+ ##UTM Zone
94
+ # C01_Barrow: zone 4
95
+ # C03_Teshekpuk: zone 5
96
+ # C04_Mackenzie: zone 8
97
+ # C06_Kytalyk: zone 55
98
+ # C07_LenaDelta: zone 52
99
+ # C08_Yamal: zone 42
100
+ # CS12_1: Cape Espenberg Lowland (Alaska, USA):3
101
+ # CS12_2: Central Seward Peninsula (Alaska, USA):3
102
+ for file in os .listdir (output_georef ):
103
+ if file .endswith (".tif" ):
104
+ print (os .path .join (output_georef , file ))
105
+ subprocess .call (
106
+ ['gdalwarp' , '-t_srs' , "+proj=utm +zone=3 +datum=WGS84 +units=m +no_defs" , os .path .join (output_georef , file ),
107
+ os .path .join (output_path , file )], shell = True )
108
+
109
+ if __name__ == "__main__" :
110
+ main ()
0 commit comments