-
Notifications
You must be signed in to change notification settings - Fork 18
/
Create_hillshade_series_in_directory.py
140 lines (109 loc) · 5.06 KB
/
Create_hillshade_series_in_directory.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
"""
This function reads hillshades and elevation files in a directory
INSTRUCTIONS FOR ffmpeg
This will produce a series of images that you can convert to a movie using ffmpeg
Here is a typical command line
ffmpeg -framerate 5 -pattern_type glob -i '*.png' -vcodec libx264 -s 1230x566 -pix_fmt yuv420p movie7.mp4
Some gotchas:
The -framerate flag must come after ffmpeg (I think)
You can be more specific about the pattern but this seems to work
For the -s flag you must have pixel sizes that are divisible by 2.
Written by Simon Mudd
June 2017
git
GPL3
"""
import LSDChiMappingExamples as CME
import LSDMapWrappers as MW
import os
from sys import platform
import sys
from glob import glob
def get_filenames(root):
# Create and empty list for the filenames
these_filenames = []
for filename in glob(root+'*.bil'):
if not 'hs' in filename:
if not 'Raster' in filename:
these_filenames.append(filename)
these_filenames.sort()
print these_filenames
return these_filenames
def run_plots(DataDirectory,Base_file, cbar_min_max = [0,400]):
root = DataDirectory+Base_file
filenames = get_filenames(root)
counter = 0
for fname in filenames:
counter = counter+1
# remove the extension form the files
this_base_file = fname[:-4]
if "win" in platform:
split_file = this_base_file.split("\\")
elif "linux" in platform:
split_file = this_base_file.split("/")
print("Split file is: ")
print split_file
this_base_file = split_file[-1]
print("This base file is: "+ this_base_file)
print("I am getting figures for the animation.")
MW.SimpleHillshadeForAnimation(DataDirectory,this_base_file,cmap = "terrain", dpi = 250, imgnumber=counter, full_basefile = root,custom_cbar_min_max = cbar_min_max,coord_type='UTM_km', hide_ticklabels=True)
def animate_plots(base_directory, fname_prefix):
"""
This function creates a movie of the plots using ffmpeg
Args:
base_directory (str): the directory with the plots.
fname_prefix (str): the filename for the model run
Returns:
none but creates mp4 from pngs in base directory
Author: FJC
"""
import subprocess
# animate the pngs using ffmpeg
system_call = "ffmpeg -framerate 5 -pattern_type glob -i '"+base_directory+"*.png' -vcodec libx264 -s 1230x566 -pix_fmt yuv420p "+base_directory+fname_prefix+"_movie.mp4"
print system_call
subprocess.call(system_call, shell=True)
#=============================================================================
# This is just a welcome screen that is displayed if no arguments are provided.
#=============================================================================
def print_welcome():
print("\n\n=======================================================================")
print("Hello! I'm going to plot a series of hillshades for you.")
print("You will need to tell me the directory and the base file name.")
print("Use the -dir flag to define the working directory.")
print("If you don't do this I will assume the data is in the same directory as this script.")
print("Use the -fname flag to define the base file name.")
print("For help type:")
print(" python PlotMOverNAnalysis.py -h\n")
print("=======================================================================\n\n ")
#=============================================================================
# This is the main function that runs the whole thing
#=============================================================================
def main(argv):
"""
This is just a few lines for keeping track of how long the program is taking.
You can ignore it.
"""
import time
tic = time.clock()
# If there are no arguments, send to the welcome screen
if not len(sys.argv) > 1:
full_paramfile = print_welcome()
sys.exit()
# Get the arguments
import argparse
parser = argparse.ArgumentParser()
# The location of the data files
parser.add_argument("-dir", "--base_directory", type=str, help="The base directory with the hillshades. If this isn't defined I'll assume it's the same as the current directory.")
parser.add_argument("-fname", "--fname_prefix", type=str, help="The base file name of the hillshades.")
parser.add_argument("-animate", "--animate", type=bool, default=False, help="If this is true I'll create a movie of the model run.")
parser.add_argument("-zmax", "--maximum_elevation_for_plotting", type=float, default = 400, help="This is the maximum elevation in the colourbar of the landscape plot.")
args = parser.parse_args()
cbar_min_max = [0,args.maximum_elevation_for_plotting]
run_plots(args.base_directory,args.fname_prefix,cbar_min_max)
if (args.animate):
animate_plots(args.base_directory, args.fname_prefix)
toc = time.clock()
print("This took: "+str(toc - tic)+" units of time")
#=============================================================================
if __name__ == "__main__":
main(sys.argv[1:])