# Regridding I/O API files

**URL:** <https://forum.cmascenter.org/t/regridding-i-o-api-files/388>\
**Category:** I/O API\
**Created:** [November 2, 2018, 5:10pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388 "2018-11-02T17:10:10Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [November 2, 2018, 5:10pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/1 "2018-11-02T17:10:10Z")

</div>

What is the easiest way to regrid an existing I/O API file (originally with latlon grids at 0.1 degrees resolution to be changed to resolution of 0.5 degrees and the variable values regridded accordingly)?

---

<div class="post-metadata">

**Author:** ![cjcoats](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@cjcoats](https://forum.cmascenter.org/u/cjcoats)\
**Post date:** [November 5, 2018, 12:40pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/2 "2018-11-05T12:40:54Z")

</div>

Use I/O API M3TOOLS programs mtxcalc and mtxcple. See

[https://cjcoats.github.io/ioapi/MTXCALC.html](https://cjcoats.github.io/ioapi/MTXCALC.html)  
[https://cjcoats.github.io/ioapi/MTXCPLE.html](https://cjcoats.github.io/ioapi/MTXCPLE.html)

---

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [November 5, 2018, 1:38pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/3 "2018-11-05T13:38:15Z")

</div>

Thanks! Of the two functions you pointed to, one creates the conversion matrix, and the other actually converts. And to use them, an appropriate GRIDDESC file entry is needed. I’ll give it a try. Thanks again!

---

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [November 5, 2018, 4:53pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/4 "2018-11-05T16:53:36Z")

</div>

I wanted to get a GRIDDESC file for the output from WRF, and use its coordinate system + grid as a reference to convert all other files to. However, when I use the command wrfgriddesc, I get the message  
**wrfgriddesc: Command not found**  
I am using IO/API 3.1 version, by the way.

---

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [November 6, 2018, 5:24pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/5 "2018-11-06T17:24:09Z")

</div>

Just realized that the two M3 functions  
MTXCALC  
MTXCPLE  
can only be used if you are transforming IO/API file to a finer grid. I need to do the opposite - that is convert a fine 0.1 degree grid to a coarse 0.5 degree grid. What are my options here?

---

<div class="post-metadata">

**Author:** ![cjcoats](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@cjcoats](https://forum.cmascenter.org/u/cjcoats)\
**Post date:** [November 6, 2018, 5:43pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/6 "2018-11-06T17:43:37Z")

</div>

skunwar wrote:

> [skunwar] skunwar [https://forum.cmascenter.org/u/skunwar](https://forum.cmascenter.org/u/skunwar)  
> November 6
> 
> Just realized that the two M3 functions  
> MTXCALC  
> MTXCPLE  
> can only be used if you are transforming IO/API file to a finer grid. I  
> need to do the opposite - that is convert a fine 0.1 degree grid to a  
> coarse 0.5 degree grid. What are my options here?

No. You’re thinking about “m3cple”, which does bilinear interpolation.

What is being done is this: For a refinement of the output-grid,  
calculate where the output-grid cell-centers fall in the input-grid,  
and use that info to compute an aggregation-matrix.

Then “mtxcple” uses that aggregation-matrix to re-grid…

---

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [November 7, 2018, 2:52pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/7 "2018-11-07T14:52:40Z")

</div>

Thanks Carlie! Just to be clear on the concept of aggregation matrix when converting a fine grid to coarse grid, I made a diagram:

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/cmas/original/1X/b28a211869fcdd616c1407b7b25cfe83acc5a7ed.png)

The fine source grid A cell# 11 would provide value for the coarse grid B cell#1 in the aggregation matrix? As an example, I would need to write a script that would only select row 2, col 1 cell from the entire collection of 9 grid-cells in source grid A for the aggregation matrix in grid cell# 1 target grid B - I am understanding it correctly?

---

<div class="post-metadata">

**Author:** ![cjcoats](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@cjcoats](https://forum.cmascenter.org/u/cjcoats)\
**Post date:** [November 7, 2018, 3:15pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/8 "2018-11-07T15:15:00Z")

</div>

skunwar wrote:

> [skunwar] skunwar [https://forum.cmascenter.org/u/skunwar](https://forum.cmascenter.org/u/skunwar)  
> November 7
> 
> Thanks Carlie! Just to be clear on the concept of aggregation matrix when  
> converting a fine grid to coarse grid, I made a diagram:
> 
> image  
> [https://canada1.discourse-cdn.com/flex027/uploads/cmas/original/1X/b28a211869fcdd616c1407b7b25cfe83acc5a7ed.png](https://canada1.discourse-cdn.com/flex027/uploads/cmas/original/1X/b28a211869fcdd616c1407b7b25cfe83acc5a7ed.png)
> 
> The fine source grid A cell# 11 would provide value for the coarse grid B  
> cell#1 in the aggregation matrix? As an example, I would need to write a  
> script that would only select row 2, col 1 cell from the entire collection  
> of 9 grid-cells in source grid A for the aggregation matrix in grid cell#  
> 1 target grid B - I am understanding it correctly?

For grid-B cell-1 (“B1”), the aggregation matrix would take

```
 F*(A1 + A2 + A3 + a10 + A11 + A12 + A19 + A20 + A21)/9

```

where F is a scaling-constant from the user (default 1, as for averaging  
met data; for emissions data, F needs to be nesting-ratio^2 = 9).

This is an easy case, where grid-A is a “refinement” of grid-B. For the  
more general case (where A and B have different coordinate systems, and  
the grids don’t match up exactly), it is a bit more complicated, which is  
where the sub-sampling comes in…

---

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [November 7, 2018, 3:33pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/9 "2018-11-07T15:33:07Z")

</div>

Thanks- That makes it clear!

To do the above-said aggregation on each coarse cell in the IO/API file (containing the conversion matrix), what m3tool would you recommend? I am a little new to IO/API files processing.

---

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [May 27, 2020, 9:53pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/10 "2020-05-27T21:53:11Z")

</div>

Do you have guidance for us as to how to select values for COL\_REFINEMENT and ROW\_REFINEMENT while using mtxcalc and mtxcple to regrid? I am trying to convert from a coarse 2.5 deg by 2 deg lon-lat grid to a fine 12km by 12km lambert conformal grid.

---

<div class="post-metadata">

**Author:** ![cjcoats](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@cjcoats](https://forum.cmascenter.org/u/cjcoats)\
**Post date:** [May 27, 2020, 11:56pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/11 "2020-05-27T23:56:22Z")

</div>

For that great a difference in resolution, particularly because the output grid is much finer than the input grid, I’d just use M3Tools program _m3cple_, which does bilinear interpolation to the output-grid cell-centers. _mtxcple_ is much more for the cases when the output-grid resolution is as coarse or coarser than the input.

See [https://cjcoats.github.io/ioapi/M3CPLE.html](https://cjcoats.github.io/ioapi/M3CPLE.html)

---

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [May 28, 2020, 1:36am UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/12 "2020-05-28T01:36:39Z")

</div>

I used m3cple and overall, the result was better than mtxcple (which had only given only 0s everywhere) but I am seeing negative numbers in some gridcells in the OUTFILE and was wondering what could have caused it? I can only guess that I didn’t use synch file but not sure if that is the reason.

---

<div class="post-metadata">

**Author:** ![cjcoats](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@cjcoats](https://forum.cmascenter.org/u/cjcoats)\
**Post date:** [May 28, 2020, 11:06am UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/13 "2020-05-28T11:06:14Z")

</div>

> [skunwar] skunwar [https://forum.cmascenter.org/u/skunwar](https://forum.cmascenter.org/u/skunwar)  
> May 28
> 
> I used m3cple and overall, the result was better than mtxcple (which had  
> only given only 0s everywhere) but I am seeing negative numbers in some  
> gridcells in the OUTFILE and was wondering what could have caused it? I  
> can only guess that I didn’t use synch file but not sure if that is the  
> reason.

Do you have “missing” in the data? Simple bilinear interpolation does  
not know how to deal with that. – Carlie

---

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [May 28, 2020, 1:37pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/14 "2020-05-28T13:37:36Z")

</div>

I do have missing data filled with some standard value, so maybe setting that to zero would regrid fine?

---

<div class="post-metadata">

**Author:** ![cjcoats](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@cjcoats](https://forum.cmascenter.org/u/cjcoats)\
**Post date:** [May 28, 2020, 1:56pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/15 "2020-05-28T13:56:16Z")

</div>

That’s still going to cause artifacts.

For the missing-data-regridding case you need a gridding algorithm that checks for missings, and that does appropriate weighting even when they are present. Bilinear, spline, and other higher-order algorithms won’t work in that situation.

One reasonable approach for that might be to modify I/O API routine `CBARNES1`() (_cbarnes1.f_: see [https://cjcoats.github.io/ioapi/CBARNES.html](https://cjcoats.github.io/ioapi/CBARNES.html)) to handle missing data correctly, and then build a “driver” program to use that as its computational kernel. Or else filter out all the “missing” data, and feed the resulting filtered data into the existing `CBARNES1`.

Early on in the Models-3 project that created CMAQ, it was decided that bugs in “missing”-data handling were so pervasive that the entire situation should be excluded: “missing” data should be filtered out during the data-import process, so that “missing” data was never allowed into the system.

---

<div class="post-metadata">

**Author:** ![skunwar](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@skunwar](https://forum.cmascenter.org/u/skunwar)\
**Post date:** [February 25, 2021, 8:07pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/16 "2021-02-25T20:07:01Z")

</div>

I am regridding a larger and coarse ~250km (2 degrees lat/lon) data to finer and smaller 36km domain (lambert conformal conic) with the US at the center:

```
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 Input grid parameters
 NCOLS= 72
 NROWS= 45
 GDTYP= 1
 P_ALP -9.9989997574922324E+36
 P_BET -9.9989997574922324E+36
 P_GAM -9.9989997574922324E+36
 XCENT -9.9989997574922324E+36
 YCENT -9.9989997574922324E+36
 XORIG 1.8000000000000000E+02
 YORIG 0.0000000000000000E+00
 XCELL 2.5000000000000000E+00
 YCELL 2.0000000000000000E+00
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 Output grid parameters
 NCOLS= 172
 NROWS= 135
 GDTYP= 2
 P_ALP 3.3000000000000000E+01
 P_BET 4.5000000000000000E+01
 P_GAM -9.7000000000000000E+01
 XCENT -9.7000000000000000E+01
 YCENT 4.0000000000000000E+01
 XORIG -2.9520000000000000E+06
 YORIG -2.4120000000000000E+06
 XCELL 3.6000000000000000E+04
 YCELL 3.6000000000000000E+04
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

```

Regridding with ‘m3cple’ gives unmatched patterns between input and output:

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/cmas/original/2X/4/4113e61845475c9793f52f72693d86b5a3a7e709.png) ![image](https://canada1.discourse-cdn.com/flex027/uploads/cmas/original/2X/9/9dc39a73e59139b60aad0233674f155982d7be20.png)

There are no fill\_values in the input map as discussed a while ago in this thread, so I was not sure where to look for possible errors.

---

<div class="post-metadata">

**Author:** ![Jin](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jin](https://forum.cmascenter.org/u/Jin)\
**Post date:** [May 10, 2021, 3:39am UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/17 "2021-05-10T03:39:32Z")

</div>

Hi,

According to your suggestion, I used the pair of programs _mtxcalc+mtxcple_ to make a file going from a finer grid `CN09` to a coarser grid `CN27`. But I checked the value of the output file `GRIDCROD09_cn27.nc` (the following picture), which is as 9 times as the value of the input file `GRIDCROD09.nc`. My F is default 1, the value of the output file variable does not appear to be the average, but the sum of the nine cells.

Could you help me solve this problem? I want to calculate the average value of nine cells.

Regards,  
Jin

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/cmas/original/2X/7/7ffa135d96a580c0f48519f928faf8bd8d171b11.png)  
 ![image](https://canada1.discourse-cdn.com/flex027/uploads/cmas/original/2X/5/5d691bf1eb16e06fb7be26a75ae9be8e772594a7.png)

---

<div class="post-metadata">

**Author:** ![cjcoats](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@cjcoats](https://forum.cmascenter.org/u/cjcoats)\
**Post date:** [May 10, 2021, 11:20am UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/18 "2021-05-10T11:20:36Z")

</div>

You need to use the SCALEFAC environment variable for that: this program was initially used for emissions files, which have non-MKS-Standard **mass per grid cell** units, so that for emissions files you want cell-totals rather than cell-averages. For your case

_setenv SCALEFAC 0.1111111_

---

<div class="post-metadata">

**Author:** ![Jin](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jin](https://forum.cmascenter.org/u/Jin)\
**Post date:** [May 10, 2021, 1:09pm UTC](https://forum.cmascenter.org/t/regridding-i-o-api-files/388/19 "2021-05-10T13:09:43Z")

</div>

Thanks a lot. I solved my problem.
