52 netcdf
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
# !pip install xarray rioxarray netcdf4 localtileserver
# !pip install xarray rioxarray netcdf4 localtileserver
In [3]:
Copied!
import leafmap
import leafmap
Download a sample NetCDF dataset.
In [4]:
Copied!
url = 'https://github.com/giswqs/leafmap/raw/master/examples/data/wind_global.nc'
filename = 'wind_global.nc'
url = 'https://github.com/giswqs/leafmap/raw/master/examples/data/wind_global.nc'
filename = 'wind_global.nc'
In [5]:
Copied!
leafmap.download_file(url, output=filename)
leafmap.download_file(url, output=filename)
Downloading... From: https://github.com/giswqs/leafmap/raw/master/examples/data/wind_global.nc To: /home/runner/work/leafmap/leafmap/docs/notebooks/wind_global.nc 100%|██████████| 1.05M/1.05M [00:00<00:00, 7.89MB/s]
Out[5]:
'/home/runner/work/leafmap/leafmap/docs/notebooks/wind_global.nc'
Read the NetCDF dataset.
In [6]:
Copied!
data = leafmap.read_netcdf(filename)
data
data = leafmap.read_netcdf(filename)
data
Out[6]:
<xarray.Dataset> Dimensions: (lat: 181, lon: 360) Coordinates: * lat (lat) float64 90.0 89.0 88.0 87.0 86.0 ... -87.0 -88.0 -89.0 -90.0 * lon (lon) float64 0.0 1.0 2.0 3.0 4.0 ... 355.0 356.0 357.0 358.0 359.0 Data variables: u_wind (lat, lon) float64 ... v_wind (lat, lon) float64 ... Attributes: centerName: US National Weather Service - NCEP(WMC) disciplineName: Meteorological products refTime: 2016-04-30T06:00:00.000Z
Convert the NetCDF dataset to GeoTIFF. Note that the longitude range of the NetCDF dataset is [0, 360]
. We need to convert it to [-180, 180]
by setting shift_lon=True
so that it can be displayed on the map.
In [7]:
Copied!
tif = 'wind_global.tif'
leafmap.netcdf_to_tif(filename, tif, variables=['u_wind', 'v_wind'], shift_lon=True)
tif = 'wind_global.tif'
leafmap.netcdf_to_tif(filename, tif, variables=['u_wind', 'v_wind'], shift_lon=True)
Out[7]:
'/home/runner/work/leafmap/leafmap/docs/notebooks/wind_global.tif'
Add the GeoTIFF to the map. We can also overlay the country boundary on the map.
In [8]:
Copied!
geojson = 'https://github.com/giswqs/leafmap/raw/master/examples/data/countries.geojson'
geojson = 'https://github.com/giswqs/leafmap/raw/master/examples/data/countries.geojson'
In [9]:
Copied!
m = leafmap.Map(layers_control=True)
m.add_raster(tif, band=[1], palette='coolwarm', layer_name='u_wind')
m.add_geojson(geojson, layer_name='Countries')
m
m = leafmap.Map(layers_control=True)
m.add_raster(tif, band=[1], palette='coolwarm', layer_name='u_wind')
m.add_geojson(geojson, layer_name='Countries')
m
Out[9]: