Python NetCDF Latitude Area Weighted Global Average

The earth is not flat, and it’s not a sphere. If you’re doing data analysis on climate netcdf files with the Python programming language, you may need to figure out how to properly average gridded data over oblate spheroid Earth.

Unfortunately there is few to no simple guides on how to do this properly. Searching google with “python netcdf latitude weighted area average” yielded pretty much nothing useful. Adding “tutorial” finally go me somewhere. It got me here: The Correct Way to Average the Globe .

The tutorial is okay, but the author does things in the most convoluted way possible.

The amount of code made my eyes glaze over, and so I abandoned using Python for this task. But recently I had some time to think and I came up with a very short solution.

It is in fact not very different from the Awk code I always used in many of my posts.

I hope this post will become the go-to guide for Python users on this question.

Here’s how it’s done:

See the source article to see the full code – Ed

This is what we get:

Yes, this is correct.

We can easily do a sanity check. The sum area of all the grid cells should add up to the surface area of the Earth:

See the source article to see the full code – Ed

This is the correct area in km2 when using this technique.

Please note that this code works as is because the grid cell is 1×1 degrees. The way you modify it for different resolution is to change the by_lat line:

by_lat = (a*r)**2 ...      # 1 degree
by_lat = (a*r*0.5)**2 ...  # 0.5 degree
by_lat = (a*r*0.25)**2 ... # 0.25 degree
... 

That’s it.

Enjoy doing things the easy way.

See more here: phzoe.com

Header image: Getty Images

Please Donate Below To Support Our Ongoing Work To Defend The Scientific Method

PRINCIPIA SCIENTIFIC INTERNATIONAL, legally registered in the UK as a company incorporated for charitable purposes. Head Office: 27 Old Gloucester Street, London WC1N 3AX. 

Trackback from your site.

Comments (9)

  • Avatar

    Whokoo

    |

    ttps://edition.cnn.com/videos/us/2022/06/23/largest-burmese-python-snake-recorded-found-in-florida-everglades-orig-llr.cnn/video/playli

    Reply

  • Avatar

    Graeme McMillan

    |

    Zoe
    Github?

    Reply

    • Avatar

      Zoe Phin

      |

      Nah. Draws attention away from blog.

      Reply

      • Avatar

        Graeme McMillan

        |

        😅 I guess so.

        Reply

  • Avatar

    Zoe Phin

    |

    Uhm, I don’t expect all my posts to be featured. This was just a little handy tip, probably not worth advertising, especially with the heavily search engine friendly title.

    Thank you!

    Reply

  • Avatar

    Jerry Krause

    |

    Hi Zoe,

    To average the globe I believe one should consider the following factual information.

    At a garage sale I just acquired The Century Atlas Hemispheres showing height of land and depth of water. The featured hemispheres are the Western Hemisphere and Eastern Hemisphere and not the more common Northern and Southern Hemispheres. Also featured (smaller size) are the hemispheres containing the Greatest Amount of Water and the Greatest Amount of Land. Then there is an intermediate sized circular map labeled Region Around the South Pole which southern portions of the Continents Africa, Australia, South America and, of course, all of Antarctica. Finally, the are diagrams of the highest mountain peaks (with their elevations) of the western and eastern hemispheres. All this information on a single, large, sheet of paper.

    I review this because in third grade geography (1950-1951) I was taught that the most productive of the world’s population lived in a temperate (tem·per·ate | temp(ə)rət | 1. relating to or denoting a region or climate characterized by mild temperatures: New Oxford American Dictionary) region. Which I believe many would agree lies between the Arctic Circle and the Tropic of Cancer (centered on the latitude of 45 degrees North) and the Antarctic Circle and the Topic of Capricorn (centered on the latitude of 45 degrees South).

    What I was not taught in the third grade is that in these temperate zones is found the most productive farm land for geological and climatic reasons and that a common atmospheric Jet Stream is often found to be located near the 45th latitudes, if not over them.

    Have a good day, Jerry

    Reply

  • Avatar

    Jerry Krause

    |

    Hi Zoe,

    Need to add: DO NOT AVERAGE DATA, only use maximum-minimum values (for they are actual measurements) of anything measured during each month of a year and compare this monthly data with that of the same month of the years for which there is measured data.

    For unless you do this, you can never see how much this data can differ from one year to another. And this practice greatly reduces the NUMBERS one needs to first consider.

    Have a good day, Jerry

    Reply

  • Avatar

    VOWG

    |

    Don’t care, can’t change it.

    Reply

    • Avatar

      Jerry Krause

      |

      Hi VOWG,

      Am disappointed in your comment because I do not know to what you are referring. Usually, this is obvious. Could be Zoe’s article or my comment or both

      Have a good day, Jerry

      Reply

Leave a comment

Save my name, email, and website in this browser for the next time I comment.
Share via