# Sample R code for reading EQUATES CMAQ output (netcdf files)

**URL:** <https://forum.cmascenter.org/t/sample-r-code-for-reading-equates-cmaq-output-netcdf-files/3007>\
**Category:** EQUATES\
**Created:** [February 17, 2022, 9:47pm UTC](https://forum.cmascenter.org/t/sample-r-code-for-reading-equates-cmaq-output-netcdf-files/3007 "2022-02-17T21:47:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![foley.kristen](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@foley.kristen](https://forum.cmascenter.org/u/foley.kristen)\
**Post date:** [February 17, 2022, 9:47pm UTC](https://forum.cmascenter.org/t/sample-r-code-for-reading-equates-cmaq-output-netcdf-files/3007/1 "2022-02-17T21:47:33Z")

</div>

Attached is some sample R code for accessing the 2017 EQUATES CMAQ daily average output netcdf file located in [this Google Drive folder](https://drive.google.com/drive/folders/1VnSxXL3MTovGuBAN__8T4nO3LsAkoB0T?usp=sharing). The script shows how to:

- open the netcdf file
- extract the variable names and units, as well as the date and model grid information
- read in the daily average PM2.5 data and calculate the annual mean PM2.5 for each grid cell
- plot the annual average PM2. 5 data on a map

[sample\_Rcode\_for\_EQUATES\_CMAQ\_data.txt](https://forum.cmascenter.org/uploads/short-url/plOl051T4Z0HjnftJqYYw0fM7Xt.txt) (5.0 KB)

---

<div class="post-metadata">

**Author:** ![DaveDixon](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@DaveDixon](https://forum.cmascenter.org/u/DaveDixon)\
**Post date:** [April 3, 2024, 10:08pm UTC](https://forum.cmascenter.org/t/sample-r-code-for-reading-equates-cmaq-output-netcdf-files/3007/2 "2024-04-03T22:08:46Z")

</div>

Here’s the same excellent example adapted to use the terra package instead of the rgdal and raster packages, and eliminating the M3 package which seems to have gone away.

[sample\_Rcode\_for\_EQUATES\_CMAQ\_data\_revised\_v2.txt](https://forum.cmascenter.org/uploads/short-url/qRGcGNsxdsIO0wqYQ9jP6WK6wBe.txt) (9.4 KB)

(revised to use the same sample NetCDF file as the original)  
(revised again to fix a problem with the leaflet image)

---

<div class="post-metadata">

**Author:** ![foley.kristen](https://avatars.discourse-cdn.com/v4/letter/f/d9b06d/32.png) [@foley.kristen](https://forum.cmascenter.org/u/foley.kristen)\
**Post date:** [April 3, 2024, 11:04pm UTC](https://forum.cmascenter.org/t/sample-r-code-for-reading-equates-cmaq-output-netcdf-files/3007/3 "2024-04-03T23:04:06Z")

</div>

Wow, this is fantastic @DaveDixon. Thank you so much for sharing!! 😀

---

<div class="post-metadata">

**Author:** ![DaveDixon](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@DaveDixon](https://forum.cmascenter.org/u/DaveDixon)\
**Post date:** [June 23, 2024, 7:17pm UTC](https://forum.cmascenter.org/t/sample-r-code-for-reading-equates-cmaq-output-netcdf-files/3007/4 "2024-06-23T19:17:26Z")

</div>

Prof. Jenise Swall just updated the M3 package (14-Jun-2024) so I’ve updated the example to restore the M3 methods, which greatly simplify the code. I’ve also documented what changed with the leaflet example at the end.

[sample\_Rcode\_for\_EQUATES\_CMAQ\_data\_rev1\_v2.txt](https://forum.cmascenter.org/uploads/short-url/j3ORW0OF0CX1yd1efkqpBryVqRq.txt) (5.9 KB)
