-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPrepareCRNRastersFileFromDirectory.py
138 lines (112 loc) · 5.23 KB
/
PrepareCRNRastersFileFromDirectory.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
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
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 22 09:41:48 2016
@author: smudd
"""
# This contains the CRNResults object, used for interpreting and plotting
# CRN dat
import LSDOSystemTools as LSDOst
import os
import numpy as np
from glob import glob
def prepare_CRNRasters_file(path,prefix):
print "\n\nHello! I am here to help you format your LSDRasters file."
print "I will look in your directories for DEMs and shielding rasters, and write them into your LSDRasters file."
print "It is my pleasure to perform this service for you!"
# first, make sure path is working
path = LSDOst.ReformatSeperators(path)
path = LSDOst.AppendSepToDirectoryPath(path)
# initiate some empty lists
raster_names = []
toposhield_names = []
basin_names = []
self_shield_names = []
snow_shield_names = []
csv_strings =[]
# now check to see if the path exists
if not os.access(path,os.F_OK):
print "The path you have chosen: " + path + " does not exist. Try with a new path."
else:
# now find all the rasters. These all have *.bil in them
for FileName in glob(path+"*.bil"):
# now remove the extension from the file
print "The filename is: " + FileName
print "Removing the bil extension"
Prefix = FileName[:-4]
print "new_filename is" + Prefix
# now we see if the files have shielding rasters
if Prefix[-3:] == "_SH":
print "This is a shielding raster"
toposhield_names.append(Prefix)
elif Prefix[-7:] == "_snowBL":
print "This is a snow raster"
snow_shield_names.append(Prefix)
elif Prefix[-7:] == "_SnowBL":
print "This is a snow raster"
snow_shield_names.append(Prefix)
elif Prefix[-9:] == "_snowclip":
print "This is a snow raster"
snow_shield_names.append(Prefix)
elif Prefix[-9:] == "_selfclip":
print "This is a self raster"
self_shield_names.append(Prefix)
elif Prefix[-7:] == "_BASINS":
print "This is a basins raster"
basin_names.append(Prefix)
elif Prefix[-7:] == "_HS":
print "This is a hillshade raster"
else:
print "No matching pattern for shielding (topo, self or snow) so I am assuming this is a DEM\n"
raster_names.append(Prefix)
# now print these to a rasters csv file
for raster_name in raster_names:
this_ts_name = "NULL"
this_sns_name = "NULL"
this_slfs_name = "NULL"
# search for toposhield
# There is probably a more efficient way to do this but it gets the job done
# Loop through all the toposhield names and find the one that contains the
# raster name
for ts in toposhield_names:
if raster_name in ts:
this_ts_name = ts
# now the snow shield
for sns in snow_shield_names:
if raster_name in sns:
this_sns_name = sns
# now the snow shield
for slfs in self_shield_names:
if raster_name in slfs:
this_slfs_name = slfs
if (this_ts_name == "NULL" and this_sns_name == "NULL" and this_slfs_name == "NULL"):
print "I have a DEM: " + raster_name + " only (no snow, self or toposhield rasters)."
this_csv_line = raster_name
else:
this_csv_line = raster_name+","
if this_sns_name == "NULL":
this_csv_line = this_csv_line+"0,"
else:
this_csv_line = this_csv_line+this_sns_name+","
if this_slfs_name == "NULL":
this_csv_line = this_csv_line+"0"
else:
this_csv_line = this_csv_line+this_slfs_name
if not this_ts_name == "NULL":
this_csv_line = this_csv_line+","+this_ts_name
# low append to the list
csv_strings.append(this_csv_line)
# now print the file
fname_csv = path+prefix+"_CRNRasters.csv"
f = open(fname_csv, 'w')
for item in csv_strings:
print>>f, item
f.close()
if __name__ == "__main__":
#path = "T:\\analysis_for_papers\\Manny_idaho\\HarringCreek"
#path = "T:\\analysis_for_papers\\Cosmo_paper\\Dethier\\zone54"
#path = "/home/smudd/SMMDataStore/analysis_for_papers/Cosmo_paper/Dethier/zone54"
#prefix = "Dethier"
path = "/home/smudd/SMMDataStore/analysis_for_papers/Cosmo_paper/new/Dethier"
prefix = "zone54"
print "path is: " + path
prepare_CRNRasters_file(path,prefix)