annotate zonal_statistics.py @ 1:4bb2f6091660 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:30 -0400
parents 4f14b5b97262
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4f14b5b97262 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
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
2
4f14b5b97262 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
4f14b5b97262 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
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
5
4f14b5b97262 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
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
7
4f14b5b97262 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
4f14b5b97262 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')
4f14b5b97262 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
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
11
4f14b5b97262 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
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
13
4f14b5b97262 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
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
15
4f14b5b97262 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
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
17
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
18
4f14b5b97262 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__':
4f14b5b97262 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")
4f14b5b97262 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()
4f14b5b97262 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(
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
23 'raster',
4f14b5b97262 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)'
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
25 )
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
26
4f14b5b97262 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(
4f14b5b97262 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 '
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
29 )
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
30
4f14b5b97262 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(
4f14b5b97262 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'
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
33 )
4f14b5b97262 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(
4f14b5b97262 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)'
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
36 )
4f14b5b97262 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(
4f14b5b97262 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]'
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
39 )
4f14b5b97262 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(
4f14b5b97262 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'
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
42 )
4f14b5b97262 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",
4f14b5b97262 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")
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
45
4f14b5b97262 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()
4f14b5b97262 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:
4f14b5b97262 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'
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
49 else:
4f14b5b97262 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
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
51
4f14b5b97262 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)
4f14b5b97262 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")
4f14b5b97262 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)
4f14b5b97262 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)
4f14b5b97262 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)
4f14b5b97262 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)
4f14b5b97262 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()
4f14b5b97262 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))
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
60
4f14b5b97262 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',
4f14b5b97262 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,
4f14b5b97262 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',
4f14b5b97262 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,
4f14b5b97262 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,
4f14b5b97262 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)
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
67 }
4f14b5b97262 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
4f14b5b97262 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")
4f14b5b97262 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")
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
71
4f14b5b97262 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:
4f14b5b97262 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")
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
74 else:
4f14b5b97262 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)
4f14b5b97262 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:
4f14b5b97262 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")
4f14b5b97262 planemo upload for repository https://github.com/NordicESMhub/galaxy-tools/tree/master/tools/mean-per-zone commit 24f20e7459e275c14376680f9bb91cd8ac26d9a5
climate
parents:
diff changeset
78
4f14b5b97262 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')