Last active
May 27, 2025 21:44
All the files necessary to calculate the area-weighted mean of geospatial data are here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def area_grid(lat, lon): | |
""" | |
Calculate the area of each grid cell | |
Area is in square meters | |
Input | |
----------- | |
lat: vector of latitude in degrees | |
lon: vector of longitude in degrees | |
Output | |
----------- | |
area: grid-cell area in square-meters with dimensions, [lat,lon] | |
Notes | |
----------- | |
Based on the function in | |
https://github.com/chadagreene/CDT/blob/master/cdt/cdtarea.m | |
""" | |
from numpy import meshgrid, deg2rad, gradient, cos | |
from xarray import DataArray | |
xlon, ylat = meshgrid(lon, lat) | |
R = earth_radius(ylat) | |
dlat = deg2rad(gradient(ylat, axis=0)) | |
dlon = deg2rad(gradient(xlon, axis=1)) | |
dy = dlat * R | |
dx = dlon * R * cos(deg2rad(ylat)) | |
area = dy * dx | |
xda = DataArray( | |
area, | |
dims=["latitude", "longitude"], | |
coords={"latitude": lat, "longitude": lon}, | |
attrs={ | |
"long_name": "area_per_pixel", | |
"description": "area per pixel", | |
"units": "m^2", | |
}, | |
) | |
return xda |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def earth_radius(lat): | |
''' | |
calculate radius of Earth assuming oblate spheroid | |
defined by WGS84 | |
Input | |
--------- | |
lat: vector or latitudes in degrees | |
Output | |
---------- | |
r: vector of radius in meters | |
Notes | |
----------- | |
WGS84: https://earth-info.nga.mil/GandG/publications/tr8350.2/tr8350.2-a/Chapter%203.pdf | |
''' | |
from numpy import deg2rad, sin, cos | |
# define oblate spheroid from WGS84 | |
a = 6378137 | |
b = 6356752.3142 | |
e2 = 1 - (b**2/a**2) | |
# convert from geodecic to geocentric | |
# see equation 3-110 in WGS84 | |
lat = deg2rad(lat) | |
lat_gc = np.arctan( (1-e2)*np.tan(lat) ) | |
# radius equation | |
# see equation 3-107 in WGS84 | |
r = ( | |
(a * (1 - e2)**0.5) | |
/ (1 - (e2 * np.cos(lat_gc)**2))**0.5 | |
) | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment