-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmslpeurope.py
executable file
·92 lines (73 loc) · 2.75 KB
/
mslpeurope.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
# -*- coding: utf-8 -*-
#
# Author: Cayetano Benavent, 2016.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import os
import shutil
import rasterio
from rasterio.warp import calculate_default_transform, reproject, RESAMPLING
def createOutFolder(out_flname):
try:
outdir = os.path.dirname(out_flname)
if not os.path.exists(outdir):
os.makedirs(outdir)
else:
shutil.rmtree(outdir)
os.makedirs(outdir)
print("Output folder created")
except Exception as error:
print("Error creating out folder: {}".format(error))
def run(input_file, out_file, dst_crs):
try:
print("Starting proccess...")
createOutFolder(out_file)
with rasterio.open(input_file) as src:
affine, width, height = calculate_default_transform(
src.crs, dst_crs, src.width, src.height,*src.bounds)
kwargs = src.meta.copy()
kwargs.update({
'crs': dst_crs,
'transform': affine,
'affine': affine,
'width': width,
'height': height,
'compress': 'lzw',
'nodata': 0
})
with rasterio.open(out_file, 'w', **kwargs) as dst:
reproject(
source=rasterio.band(src, 1),
destination=rasterio.band(dst, 1),
src_transform=src.affine,
src_crs=src.crs,
dst_transform=affine,
dst_crs=dst_crs,
resampling=RESAMPLING.nearest)
for i in dst.indexes:
band_dst = dst.read(i) / 100
dst.write_band(i, band_dst)
print("Successfully finished proccess!")
except Exception as error:
print("Error creating out folder: {}".format(error))
def main():
input_file = "../../../data/mslp_gfs/mslp_europe.nc"
out_file = "/tmp/res_raster/mslp_europe_rst.tif"
dst_crs = 'EPSG:3034'
run(input_file, out_file, dst_crs)
if __name__ == '__main__':
main()