annotate zonal_statistics.py @ 1:639192aa63f5 draft default tip

planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit a5d176b7d0dc81bae07569d396edced43cfa2702
author climate
date Mon, 24 Jun 2019 02:41:08 -0400
parents 40727a2668d6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
1 #!/usr/bin/env python3
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
2
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
3 import argparse
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
4 import warnings
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
5
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
6 import geopandas
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
7
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
8 import matplotlib as mpl
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
9 mpl.use('Agg')
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
10 from matplotlib import pyplot # noqa: I202,E402
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
11
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
12 import pandas as pd # noqa: I202,E402
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
13
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
14 from rasterstats import zonal_stats # noqa: I202,E402
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
15
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
16 import xarray as xr # noqa: I202,E402
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
17
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
18
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
19 if __name__ == '__main__':
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
20 warnings.filterwarnings("ignore")
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
21 parser = argparse.ArgumentParser()
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
22 parser.add_argument(
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
23 'raster',
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
24 help='input raster file with geographical coordinates (netCDF format)'
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
25 )
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
26
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
27 parser.add_argument(
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
28 'shapefile', help='input shapefile name with polygons '
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
29 )
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
30
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
31 parser.add_argument(
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
32 'variable', help='name of the variable in raster file'
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
33 )
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
34 parser.add_argument(
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
35 'output', help='output filename to store resulting image (png format)'
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
36 )
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
37 parser.add_argument(
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
38 '-s', '--stat', help='type of statistics [min, max, mean, count]'
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
39 )
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
40 parser.add_argument(
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
41 '-t', '--title', help='title for the generated plot'
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
42 )
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
43 parser.add_argument("-v", "--verbose", help="switch on verbose mode",
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
44 action="store_true")
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
45
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
46 args = parser.parse_args()
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
47 if args.stat is None:
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
48 stat_type = 'mean'
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
49 else:
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
50 stat_type = args.stat
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
51
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
52 dset = xr.open_dataset(args.raster)
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
53 dset[args.variable].to_netcdf(".tmp.nc")
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
54 zs = zonal_stats(args.shapefile, ".tmp.nc", stats=stat_type)
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
55 df_zonal_stats = pd.DataFrame(zs)
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
56 world = geopandas.read_file(args.shapefile)
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
57 df = pd.concat([world, df_zonal_stats], axis=1)
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
58 ddf = df.dropna()
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
59 f, ax = pyplot.subplots(1, figsize=(20, 10))
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
60
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
61 visu = ddf.plot(column=stat_type, scheme='Quantiles', k=15, cmap='jet',
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
62 legend=True, ax=ax,
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
63 legend_kwds={'loc': 'lower left',
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
64 'frameon': True,
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
65 'title': args.variable,
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
66 'bbox_to_anchor': (1, 0.05)
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
67 }
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
68 ) # plot done
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
69 ax.set_xlabel("Longitude")
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
70 ax.set_ylabel("Latitude")
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
71
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
72 if args.title is None:
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
73 f.suptitle(stat_type + " computed over areas")
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
74 else:
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
75 f.suptitle(args.title)
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
76 if args.verbose:
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
77 print("plot generated")
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
78
40727a2668d6 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
79 pyplot.savefig(args.output, format='png')