-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadarDataLibraryCrawler.py
More file actions
164 lines (115 loc) · 5.61 KB
/
RadarDataLibraryCrawler.py
File metadata and controls
164 lines (115 loc) · 5.61 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright ETH-VAW / Glaciology
#
# Module : Scripts.RadarDataLibrary.RadarDataLibraryCrawler
#
# Created by: yvow
# Created on: 05.05.2015
# Imports
import os
from os import walk
from os.path import splitext, join
import logging
from RadarData.RadarLine import RadarLine
from RadarData.RadarDataShapefileWriter import EsriShapefileWriter
class RadarDataLibraryCrawler(object):
'''
Helper class crawling through the given directories to retrieve all available radar data header files.
Based on the header files the detailed information about each individual radar survey line will be retrieved.
The class allows further analysis and data storage of the found radar information.
'''
__RADAR_DATA_FILE_TYPE = ".txt"
__LOG_FILE_NAME = "RadarCrawlerLog.log"
__headerDirectory = None
__dataDirectory = None
__shapeDirectory = None
__shapefileLine = None
__shapefilePoint = None
__selectedFiles = []
@property
def radarLineDataLineCount(self):
return len(self.__selectedFiles)
@radarLineDataLineCount.getter
def radarLineDataLineCount(self):
return len(self.__selectedFiles)
@property
def radarLineDataLines(self):
return self.__selectedFiles
@radarLineDataLines.getter
def radarLineDataLines(self):
return self.__selectedFiles
def __init__(self, headerDirectory, dataDirectory, shapeDirectory, shapefileNameLine, shapefileNamePoint):
self.__headerDirectory = headerDirectory
self.__dataDirectory = dataDirectory
self.__shapeDirectory = shapeDirectory
self.__shapefileLine = os.path.join(shapeDirectory, shapefileNameLine)
self.__shapefilePoint = os.path.join(shapeDirectory, shapefileNamePoint)
self.__selectedFiles = self.__buildRecursiveDirectoryTree(self.__headerDirectory, [self.__RADAR_DATA_FILE_TYPE])
# Getting the default of the logger set.
logging.basicConfig(filename = self.__LOG_FILE_NAME, level = logging.WARNING)
def __buildRecursiveDirectoryTree(self, path, fileExtensions):
"""
Walks recursively through a tree of directories.
@param path: Root directory to start analysis.
@type path: UNC path
@return: Array with selected files matching a given pattern.
"""
print "The following file extensions are analyzed: "
for fileExtension in fileExtensions:
print "-> " + fileExtension
selectedFiles = []
for root, dirs, files in walk(path):
print "Current depth of directory from root: " + str(len(dirs))
selectedFiles += self.__selectFiles(root, files, fileExtensions)
return selectedFiles
def __selectFiles(self, root, files, fileExtensions):
"""
Recursive function to walk through a given directory tree.
Adds all the files with the given (hard coded) extensions into an array.
@param root: Current directory to be analyzed.
@param files: Files within the directory to be analyzed.
@return: Array with all the dataset fitting the given file extensions.
"""
selectedFiles = []
for foundFile in files:
# Concatenation to get full path
fullPath = join(root, foundFile)
ext = splitext(foundFile)[1]
for fileExtension in fileExtensions:
if ext.upper() == fileExtension.upper():
selectedFiles.append(fullPath)
return selectedFiles
def writeGeometries(self, doAppend):
"""
Writes the found information of the radar lines and radar points into
the given shapefiles.
"""
message = "Number of header files found: " + str(len(self.__selectedFiles))
print message
logging.info(message)
if len(self.__selectedFiles) == 0:
message = "WARNING: No header files found!"
print message
logging.info(message)
fileCounter = 0
for selectedFile in self.__selectedFiles:
fileCounter += 1
message = str(fileCounter) + " -> " + selectedFile
print message
logging.info(message)
radarLine = RadarLine(selectedFile, self.__headerDirectory, self.__dataDirectory)
radarLine.analyzeRadarData()
# Definition if the data are appended to an existing data set.
# The master definition is to append: All data have to be appended.
doAppendFinal = True
if doAppend == True:
doAppendFinal = True
# The master definition is not the append: The first line will be creating a new set of shapefiles.
else:
if fileCounter == 1:
doAppendFinal = False
else:
doAppendFinal = True
esriShapefileWriter = EsriShapefileWriter(radarLine, self.__shapefileLine, self.__shapefilePoint, doAppendFinal)
esriShapefileWriter.writeData()
print str(radarLine)
logging.info(str(radarLine))