Sitemap
Thombson Chungkham

Thombson Chungkham

Cryosphere | Data Science | Remote Sensing | Indian Space Research Organization (ISRO) https://www.researchgate.net/profile/Thombson-Chungkham

Map Projection using Python

6 min readFeb 25, 2023

--

Press enter or click to view image in full size
SARAL/AltiKa Brightness Temperature projected at Polar Stereographic North.

Introduction

We are going to project the brightness temperature (Tb) corresponding to 37 GHz of the bi-frequency Passive Microwave Radiometer on-board the SARAL/AltiKa to NSIDC Sea Ice Polar Stereographic North. This article will get you started with reading Tb data in Network common data form (NetCDF) file using Python. The same data will be used here too for the sake of uniformity, and we will project it to Polar Stereographic North.

Pre-requisite knowledge

Knowledge of numpy, matplotlib and map projection in general are needed. And That’s it!!!

Data

The NetCDF file can be found here. Again, please refer to the above article for more information about this particular data.

Necessary Libraries

NetCDF files can be read with a few different Python modules. We will use netCDF4. We will also use numpy, matplotlib and basemap libraries. Basemap is used to plot on map projections (with coastlines and political boundaries) using matplotlib. Refer this for more details about basemap.

Installing Basemap library

Create a new virtual environment using conda and type in the following:

  • conda config — add channels conda-forge
  • conda config — set channel_priority strict
  • conda search basemap — channel conda-forge
  • conda install -c conda-forge basemap=1.3.6
  • conda install basemap basemap-data basemap-data-hires

The conda search basemap — channel conda-forge will search for the basemap version for your installed python. Check the version and type it here conda install -c conda-forge basemap=*. The latest version as of python 3.8 is 1.3.6 so it is as shown above. That’s it!!!

Reading netCDF file

We will use the netCDF.Dataset() attribute for reading the netCDF file. Let’s read the netCDF file and explore the variables:

#Importing the necessary libraries
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

#Reading the netCDF file
data = Dataset(r'\path\to\file.nc')
print (data)

The output:

<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
FileType: Z=F(X,Y)
Conventions: CF-1.0
lat_min: -90.0
lat_max: 90.0
lon_min: -180.0
lon_max: 179.66666666666663
dimensions(sizes): lat(541), lon(1080)
variables(dimensions): float64 lat(lat), float64 lon(lon), float64 tb_ka(lon, lat)
groups:

As we can see the shape of the variables: lat (541), lon (1080) and tb_ka (1080, 541).

Get Thombson Chungkham’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s read these variables:

#Reading the variables
lats = data.variables['lat'][:]
lons = data.variables['lon'][:]
tbka = data.variables['tb_ka'][:]
ka = np.rot90(tbka, k=1, axes=(0, 1))

The reason why the variable tbka is rotated anti-clockwise will be clearer by referring to this article again.

Map projection using Basemap

Before we project tbka on a map, we first need to define a 2-D array for each lats and lons variables just like a map. Notice that the shape of the variable's lats and lons are not the same with tbka. This can be achieved using np.meshgrid() and implementing in code as lons, lats = np.meshgrid(lons,lats). In other words, the np.meshgrid function is used to create a rectangular grid out of two given one-dimensional arrays (here, lats and lons)representing the Cartesian indexing or Matrix indexing. We can check the shape of lats and lons by using lats.shape and lons.shape respectively. The output for both gives:

(541, 1080)

Now, let’s project first to equirectangular projection (also called the equidistant cylindrical projection) using basemap.

lons, lats = np.meshgrid(lons,lats)

#Setting Equidistant cylindrical projection
# llcrnrlat,llcrnrlon,urcrnrlat,urcrnrlon
# are the lat/lon values of the lower left and upper right corners
# of the map.
# resolution = 'h' means use high resolution coastlines
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,
llcrnrlon=-180,urcrnrlon=180,resolution='h')

m.drawcoastlines(linewidth=0.2)
m.fillcontinents(color='coral',lake_color='aqua')
#draw parallels and meridians.
m.drawparallels(np.arange(-80.,81.,20.),labels=[1,0,0,0])
m.drawmeridians(np.arange(-180.,181.,20.),labels=[1,0,0,1])
#draw map boundary
m.drawmapboundary(fill_color='grey')
x,y=m(lons,lats) #Setting lons in x-axis and lats in y-axis
#matplotlib scatter-plot
m.scatter(x,y,c=ka,cmap=plt.cm.jet,marker='.',s=0.1)
#setting colorbar
m.colorbar(label='Brightness Temperature (K)')
plt.rcParams["figure.dpi"] = 300
plt.title ("SARAL/AltiKa Brightness Temperature January 2020")
plt.show()

Refer this for more details about the basemap toolkit and this for more about equidistant cylindrical projection.

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

The output image:

Press enter or click to view image in full size
Equidistant Cylindrical Projection

As we can see that the image is projected to equidistant cylindrical projection but flipped upside down. We can fix this using np.flip() to flip the image in an upright position. Let’s do this and replaced the variable ka with the flipped variable f in the line m.scatter(x,y,c=f,cmap=plt.cm.jet,marker=’.’,s=0.1) as follows:

#Flipping the image upright in the axis = 0 i.e., along rows
f = np.flip(ka,0)

#Setting Equidistant cylindrical projection
# llcrnrlat,llcrnrlon,urcrnrlat,urcrnrlon
# are the lat/lon values of the lower left and upper right corners
# of the map.
# resolution = 'h' means use high resolution coastlines
m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,
llcrnrlon=-180,urcrnrlon=180,resolution='h')

m.drawcoastlines(linewidth=0.2)
m.fillcontinents(color='coral',lake_color='aqua')
#draw parallels and meridians.
m.drawparallels(np.arange(-80.,81.,20.),labels=[1,0,0,0])
m.drawmeridians(np.arange(-180.,181.,20.),labels=[1,0,0,1])
#draw map boundary
m.drawmapboundary(fill_color='grey')
x,y=m(lons,lats) #Setting lons in x-axis and lats in y-axis
#matplotlib scatter-plot
m.scatter(x,y,c=f,cmap=plt.cm.jet,marker='.',s=0.1)
#setting colorbar
m.colorbar(label='Brightness Temperature (K)')
plt.rcParams["figure.dpi"] = 300
plt.title ("SARAL/AltiKa Brightness Temperature January 2020")
plt.show()
Press enter or click to view image in full size
Equidistant Cylindrical Projection

Now, let’s project to Polar Stereographic North. Again, refer this for more details about the projection.

#Setting North Polar Stereographic Projection
# The longitude lon_0 is at 6-o'clock, and the
# latitude circle boundinglat is tangent to the edge
# of the map at lon_0. Default value of lat_ts
# (latitude of true scale) is pole.
m = Basemap(projection='npstere',boundinglat=45,lon_0=-45,resolution='h')

plt.figure(figsize=(12,8))

m.drawcoastlines(linewidth=0.2)
m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-80.,81.,20.),labels=[1,0,0,0])
m.drawmeridians(np.arange(-180.,181.,20.),labels=[1,0,0,1])
m.drawmapboundary(fill_color='grey')
x,y=m(lons,lats)
m.scatter(x,y,c=f,cmap=plt.cm.jet,marker='.',s=0.1)
m.colorbar(label='Brightness Temperature (K)')
plt.rcParams["figure.dpi"] = 300
plt.title ("SARAL/AltiKa Brightness Temperature Arctic January 2020")
plt.show()

Projection parameters information:

The output image:

Press enter or click to view image in full size
Polar Stereographic North

We can cut off pole-centered projection at boundinglat (so plot is a circle instead of a square). So, let’s add round=True in the basemap line of code.

#Setting North Polar Stereographic Projection
# The longitude lon_0 is at 6-o'clock, and the
# latitude circle boundinglat is tangent to the edge
# of the map at lon_0. Default value of lat_ts
# (latitude of true scale) is pole.
m = Basemap(projection='npstere',boundinglat=45,lon_0=-45,resolution='h',
round = True)

plt.figure(figsize=(12,8))

m.drawcoastlines(linewidth=0.2)
m.fillcontinents(color='coral',lake_color='aqua')
# draw parallels and meridians.
m.drawparallels(np.arange(-80.,81.,20.),labels=[1,0,0,0])
m.drawmeridians(np.arange(-180.,181.,20.),labels=[1,0,0,1])
m.drawmapboundary(fill_color='grey')
x,y=m(lons,lats)
m.scatter(x,y,c=f,cmap=plt.cm.jet,marker='.',s=0.1)
m.colorbar(label='Brightness Temperature (K)')
plt.rcParams["figure.dpi"] = 300
plt.title ("SARAL/AltiKa Brightness Temperature Arctic January 2020")
plt.show()

The output:

Press enter or click to view image in full size
Polar Stereographic North

Conclusion

We projected the Tb of SARAL/AltiKa to Polar Stereographic North. Feel free to tinker with the options provided by basemap for your convenience with the links provided in the above section.

--

--