-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterSd.py
executable file
·98 lines (80 loc) · 2.8 KB
/
clusterSd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 12 15:54:52 2020
@author: GalinaJonat
"""
# standard deviation:: https://docs.python.org/3/library/statistics.html
import statistics
import rasterio as rio
import numpy as np
import os
import datetime as dt
import fnmatch
folder='/Volumes/ElementsSE/thesisData/toHist/mskClipped/'
img1 = folder+"FCC_Sigma0_HHHV_20190412_clipped_msk.tif"
clustersImg1 = folder+"clusters/FCC_Sigma0_HHHV_20190412_clipped_msk_clustersHH.data/class_indices.img"
# read rasters to array
def readBands(filepath,pol=''):
file = rio.open(filepath)
print('----File Information ----')
print(file.meta)
if (pol == 'HH' or pol==''): # hh bands or clusters
a = file.read(1)
elif pol == 'HV':
a = file.read(2) # hv band
a_nan = nansInArray(a)
return a_nan
#a[~np.isnan(a).all(axis=1)] # remove rows in which all values are nan
def nansInArray(inArray):
outArray = inArray.astype('float32')
outArray[outArray == 0] = np.nan
outArray[outArray < -999] = np.nan
return outArray
#dict with locations of cluster pixels
def clusterPol(cluster_array, pol_array):
clusterloc = {}
for cl in range(1,7): # for clusters 1-6:
# identify rows and columns with value in this cluster
r,c = np.where(cluster_array == cl)
# extract polarisation values of clusters in list
l_cluster = []
for i in range(0,len(r)):
l_cluster.append(pol_array[r[i],c[i]])
clusterloc[cl] = l_cluster
return clusterloc
# standard deviation for each cluster
def sdDict(clusterPolDict):
sdDict = {}
for k in clusterPolDict.keys():
sdDict[k] = np.std(clusterPolDict[k])
return sdDict
# extracting date from filename
def dateFromFilename(fn,dloc=1):
#s1 = os.path.split(fn)[-1]
s_tuple = fn.split('_')
print(s_tuple)
d_str = fnmatch.filter(s_tuple, '20*')[0]
d_dt = dt.datetime.strptime(d_str, '%Y%m%d')
return d_dt
# apply cluster pic to orig imag --> extract list of values in cluster
# calculate standard deviation
# ---WORKFLOW---
# im = readBand(fpImg,pol)
#cl = readBand(fpImg,'')
#polClusterDict = clusterPol(cluster_df, pol_df)
#sdDictPrCluster = sdDict(polClusterDict)
def workAll(inFolder,pol):
polAll = {}
clusterAll = {}
for f in os.listdir(inFolder):
if f.endswith('.tif'):
d = dateFromFilename(f)
clustersFp = inFolder+'/clusters/'+f.split('.')[0]+'_clusters'+pol+'.data/class_indices.img'
im=readBands(inFolder+f)
cl=readBands(clustersFp)
polClusterDict = clusterPol(cl, im)
sdDictPrCluster = sdDict(polClusterDict)
polAll[d] = polClusterDict
clusterAll[d] = sdDictPrCluster
return polAll, clusterAll