-
Notifications
You must be signed in to change notification settings - Fork 5
/
JoinSnowShielding.py
228 lines (164 loc) · 7.35 KB
/
JoinSnowShielding.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 27 17:35:16 2015
@author: smudd
"""
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# PrepareDirectoriesForBasinSpawn
# This function prepaers directories to be spawned
# by
"""
Created on Thu Jul 09 11:08:51 2015
@author: smudd
"""
import os
import LSDOSystemTools as LSDost
import numpy as np
import shutil
# This function goes into a CRNRasters.csv file and makes a directory
# for each raster listed. The c++ program prints the derivative rasters
# into these folders
def GetSnowShieldingFromRaster(path, prefix):
# get the DEM names
#DEM_names = GetListOfRasters(path,prefix)
Sample_names, SnowShield_values = GetCRNData(path, prefix)
# get rid of the underscores
Sample_names = RemoveUnderscoreFromSampleNames(Sample_names)
# now spawn the folders
UpdateRasterWithShielding(path, prefix,Sample_names,SnowShield_values)
new_extension = "SS"
# now copy the parameter and CRNData files
CopyDataAndParam(path, prefix,new_extension)
# this copies the CRNData and CNParameters file with a new extension
def CopyDataAndParam(path, prefix,new_extension):
#first get directory path into the correct format
fmt_path = LSDost.ReformatSeperators(path)
# add the trailing seperator
fmt_path = LSDost.AppendSepToDirectoryPath(fmt_path)
# now find the correct file
Datafname = fmt_path + prefix+"_CRNData.csv"
Paramfname = fmt_path + prefix+".CRNParam"
Datafname_out = fmt_path + prefix+"_"+new_extension+"_CRNData.csv"
Paramfname_out = fmt_path + prefix+"_"+new_extension+".CRNParam"
# copy the files
shutil.copyfile(Datafname, Datafname_out)
shutil.copyfile(Paramfname, Paramfname_out)
# sample names can't have an underscore so get rid of it
def RemoveUnderscoreFromSampleNames(Sample_names):
new_sample_names = []
for name in Sample_names:
namelist = name.split("_")
this_new_name = ""
for item in namelist:
this_new_name = this_new_name+item
new_sample_names.append(this_new_name)
#print "Old name was: "+name+" and new name is: "+ new_sample_names[-1]
return new_sample_names
# This updates the raster file with an effective shielding
def UpdateRasterWithShielding(path, prefix,Sample_names,Snowshield_values):
#first get directory path into the correct format
fmt_path = LSDost.ReformatSeperators(path)
# add the trailing seperator
fmt_path = LSDost.AppendSepToDirectoryPath(fmt_path)
# now find the correct file
fname = fmt_path + prefix+"_CRNRasters.csv"
# also make the outfile
outfname = fmt_path+prefix+"_SS_CRNRasters.csv"
outfile = open(outfname, 'w')
new_lines = []
print "The sample names are"
print Sample_names
print "The snow shield values are: "
print Snowshield_values
#See if the parameter files exist
if os.access(fname,os.F_OK):
this_file = open(fname, 'r')
lines = this_file.readlines()
# now get the list of DEM prefixes
for line in lines:
this_line = line.split(",")
DEM_prefix = this_line[0]
print "The DEM prefix is: " + DEM_prefix
# Now get the sample name
split_dem_prefix = DEM_prefix.split("_")
sample_name = split_dem_prefix[-1]
print "The sample name is: " + sample_name
# get the index of the sample name to reference the shielding value
i = Sample_names.index(sample_name)
print "the index of the sample names is: " + str(i)
# calculate the effective depth. The 160 is the attenuation thickness in g/cm^2
this_snow_depth = -160*np.log(Snowshield_values[i])
print "The shielding is: " +str(Snowshield_values[i])+ " and eff_depth is: " + str(this_snow_depth)
# update the snow effective depth
this_line[1] = str(this_snow_depth)
# update the line
this_new_line = ",".join(this_line)
new_lines.append(this_new_line)
# this will get printed to file
for line in new_lines:
# you have to get rid of the control characters
this_line = LSDost.RemoveEscapeCharacters(line)
outfile.write("%s\n" % this_line)
# This is the subfunction that actually makes the directories if none
# already exists
def GetCRNData(path, prefix):
#first get directory path into the correct format
fmt_path = LSDost.ReformatSeperators(path)
# add the trailing seperator
fmt_path = LSDost.AppendSepToDirectoryPath(fmt_path)
# now find the correct file
fname = fmt_path + prefix+"_CRNData.csv"
Sample_names = []
SnowShield_values = []
#See if the parameter files exist
if os.access(fname,os.F_OK):
this_file = open(fname, 'r')
lines = this_file.readlines()
# get rid of the first line, since this has header information
lines.pop(0)
# now get the list of DEM prefixes
for line in lines:
this_line = line.split(",")
SampleName = this_line[0]
print "This line is: "
print this_line
# check to see if there is a snow shield value
N_entries= len(this_line)
if (N_entries == 8):
SnowShield = float(this_line[7])
Sample_names.append(SampleName)
SnowShield_values.append(SnowShield)
else:
print "there is no snow shielding on this line"
SnowShield_values.append(1)
Sample_names.append(SampleName)
else:
print "*_CRNRData.csv file not found. Are you sure it is there and you have the correct path?"
return Sample_names, SnowShield_values
# this reads a list of DEM names from the parameter file
def GetListOfRasters(path,prefix):
#first get directory path into the correct format
fmt_path = LSDost.ReformatSeperators(path)
# add the trailing seperator
fmt_path = LSDost.AppendSepToDirectoryPath(fmt_path)
# now find the correct file
fname = fmt_path + prefix+"_CRNRasters.csv"
DEM_names = []
#See if the parameter files exist
if os.access(fname,os.F_OK):
this_file = open(fname, 'r')
lines = this_file.readlines()
# now get the list of DEM prefixes
for line in lines:
this_line = line.split(",")
DEM_prefix = this_line[0]
DEM_names.append(DEM_prefix)
else:
print "*_CRNRasters.csv file not found. Are you sure it is there and you have the correct path?"
return DEM_names
if __name__ == "__main__":
#path = "/home/smudd/SMMDataStore/test_clone/topodata"
path = "T:\test_clone\topodata"
#path = "c:\basin_data\Chile\test_Snow"
prefix = "SanBern_Spawned"
GetSnowShieldingFromRaster(path,prefix)