MTXBUILD run error

Hi,
I met an error when running MTXBUILD to build an I/O API sparse-matrix output file for the transform matrix. The log is shown below:

 This program uses the EPA-AREAL/MCNC-EnvPgms/BAMS Models-3      
 I/O Applications Programming Interface, [I/O API] which is      
 built on top of the netCDF I/O library (Copyright 1993, 1996    
 University Corporation for Atmospheric Research/Unidata         
 Program) and the PVM parallel-programming library (from         
 Oak Ridge National Laboratory).                                 
 Copyright (C) 1992-2002 MCNC,                                   
 (C) 1992-2018 Carlie J. Coats, Jr.,                             
 (C) 2003-2012 Baron Advanced Meteorological Systems, LLC, and   
 (C) 2014-2021 UNC Institute for the Environment.                
 Released under the GNU LGPL  License, version 2.1.  See URL     
                                                                 
     https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html     
                                                                 
 for conditions of use.                                          
                                                                 
 ioapi-3.2: $Id: init3.F90 200 2021-05-10 14:06:20Z coats $
 Version with PARMS3.EXT/PARAMETER::MXVARS3= 2048
 netCDF version 4.8.0 of Jun  3 2021 20:12:25 $
  
  
 Missing environment variable EXECUTION_ID
  
 Program MTXBUILD to build a sparse (grid-to-grid transform)
 file from an ASCII "fractions" file with lines having the
 following fields
  
     output grid row number
     output grid col number
     input grid row number
     input grid col number
     fraction:  AREA(input-cell intersect output-cell)
              / AREA(input-cell)
 These lines should  be sorted into lexicographic order
 relative to the first four fields.
  
 See URL
 https://www.cmascenter.org/ioapi/documentation/3.1/html#tools
  
 Program copyright (C) 1992-2002 MCNC, (C) 1995-2013
 Carlie J. Coats, Jr., and (C) 2002-2010 Baron Advanced
 Meteorological Systems, LLC.  Released under Version 2
 of the GNU General Public License. See enclosed GPL.txt, or
 URL http://www.gnu.org/copyleft/gpl.html
  
 Comments and questions are welcome and can be sent to
  
     Carlie J. Coats, Jr.    carlie@jyarborough.com
 or
     UNC Institute for the Environment
     137 E. Franklin St. Suite 602 Room 613-C
     Campus Box 1105
     Chapel Hill, NC 27599-1105
  
 Program version: 
 $Id:: mtxbuild.f 435 2016-11-22 18:10:58Z coats               $
  
 Value for PROMPTFLAG not defined;returning default:   TRUE
 Continue with program? (Y/N) [Y] >>
 Returning default value TRUE for query:
 "Continue with program?"
 Value for PROMPTFLAG not defined;returning default:   TRUE
 Enter logical name for input "fractions" file [FRACTIONS] >>coef

 File "coef" opened for input on unit:  99
 /public/data/emission/tools/grd2grd/scripts/27to36.qgis.txt

 Input grid "M_27_08CHINA"
 Value for PROMPTFLAG not defined;returning default:   TRUE
 Enter output grid name ["BAR"] >>zj36
 Using response "zj36"

 File "GRIDDESC" opened for input on unit:  98
 /public/data/emission/tools/grd2grd/scripts/GRIDDESC.txt

 
 Value for PROMPTFLAG not defined;returning default:   TRUE
 Enter name for output SPARSE MATRIX file [MATRIX_FILE] >>outfile
 
 Value for IOAPI_CHECK_HEADERS not defined;returning default:   FALSE
 Value for IOAPI_OFFSET_64 not defined;returning default:   TRUE
 Value for IOAPI_CFMETA not defined;returning default:   FALSE
 Value for IOAPI_CMAQMETA not defined; returning defaultval ':  'NONE'
 Value for IOAPI_CMAQMETA not defined; returning defaultval ':  'NONE'
 Value for IOAPI_SMOKEMETA not defined; returning defaultval ':  'NONE'
 Value for IOAPI_SMOKEMETA not defined; returning defaultval ':  'NONE'
 Value for IOAPI_TEXTMETA not defined; returning defaultval ':  'NONE'
 Value for IOAPI_TEXTMETA not defined; returning defaultval ':  'NONE'
 Error creating netCDF variable COL-COUNT
 netCDF error number  -47  processing file "outfile"
 NetCDF: NC_UNLIMITED in the wrong index
 Could not open file "outfile".
 Try again? (Y/N) [Y] >>N
 Returning value FALSE for query:
 "Try again?"
 
 *** ERROR ABORT in subroutine MTXBUILD
 Ending program "MTXBUILD".

The environment variable outfile is set to “/public/data/emission/tools/grd2grd/scripts/27to36.qgis.nc” and I have the permission to write in the directory.

I have no idea what’s wrong with it.
The attachments are my input fraction file and GRIDDESC file.
27to36.qgis.txt (3.0 MB)
GRIDDESC.txt (382 Bytes)

Generally, you can find the netCDF errors list here: https://cjcoats.github.io/ioapi/ERRORS.html#ncf331
For -47, we find:

nceunlim = nf_eunlimpos = -47: ncunlimited in the wrong index: Could be caused by incorrectly-set (or un-set) grid dimensions NCOLS3D, NROWS3D, NLAYS3D, or NTHIK3D
Error creating netCDF variable COL-COUNT further suggests that the matrix’s column-dimension is bad, and looking at the code, my suspicion is that something is wrong with the input-file or how it is being read.

Thanks for your reply.
I think maybe the code has a bug.
I add a few WRITE statements in m3tools/mtxbuild.f to see the values of the variables.

  234         WRITE(*,*) "NCOLS3D"
  235         WRITE(*,*) NCOLS3D
  236         WRITE(*,*) "NROWS3D"
  237         WRITE(*,*) NROWS3D                                                                                                            
  238         WRITE(*,*) "MCOEF"
  239         WRITE(*,*) MCOEF
  240         WRITE(*,*) "MROWS"
  241         WRITE(*,*) MROWS

And it shows like this:

NCOLS3D
0
NROWS3D
0
MCOEF
118665
MROWS
0

So I think there is something wrong with the code.

218        NCOL1 = NCOLS3D
...
231        NCOL2 = NCOLS3D
232        MROWS = NCOLS3D * NROWS3D

NCOLS3D and NROWS3D are not assigned with a value in the code.
However, I do not understand the sparse matrix data type well. So I do not know how to fix it.

I guess the variable MROWS is equal to NCOLS3D * NROWS3D of output grid. So I tried revising the code (the left is original and the right is revised):

218c218
<         NCOL1 = NCOLS3D
---
> C       NCOL1 = NCOLS3D
231,232c231,232
<         NCOL2 = NCOLS3D
<         MROWS = NCOLS3D * NROWS3D
---
> C       NCOL2 = NCOLS3D
>         MROWS = NCOLS2 * NROWS2

Besides, I fixed another little bug in getting output grid name.

181,182c181,182
<             ELSE IF ( LINE( 1:7 ) .EQ. '#OUTGRID' ) THEN
<                 V     = 8 + LBLANK( LINE( 8:256 ) )
---
>             ELSE IF ( LINE( 1:8 ) .EQ. '#OUTGRID' ) THEN
>                 V     = 9 + LBLANK( LINE( 9:256 ) )

And I tested the revised code, the log is shown below:

     This program uses the EPA-AREAL/MCNC-EnvPgms/BAMS Models-3      
     I/O Applications Programming Interface, [I/O API] which is      
     built on top of the netCDF I/O library (Copyright 1993, 1996    
     University Corporation for Atmospheric Research/Unidata         
     Program) and the PVM parallel-programming library (from         
     Oak Ridge National Laboratory).                                 
     Copyright (C) 1992-2002 MCNC,                                   
     (C) 1992-2018 Carlie J. Coats, Jr.,                             
     (C) 2003-2012 Baron Advanced Meteorological Systems, LLC, and   
     (C) 2014-2021 UNC Institute for the Environment.                
     Released under the GNU LGPL  License, version 2.1.  See URL     
                                                                     
         https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html     
                                                                     
     for conditions of use.                                          
                                                                     
     ioapi-3.2: $Id: init3.F90 200 2021-05-10 14:06:20Z coats $
     Version with PARMS3.EXT/PARAMETER::MXVARS3= 2048
     netCDF version 4.8.0 of Jun  3 2021 20:12:25 $
      
      
     Missing environment variable EXECUTION_ID
      
     Program MTXBUILD to build a sparse (grid-to-grid transform)
     file from an ASCII "fractions" file with lines having the
     following fields
      
         output grid row number
         output grid col number
         input grid row number
         input grid col number
         fraction:  AREA(input-cell intersect output-cell)
                  / AREA(input-cell)
     These lines should  be sorted into lexicographic order
     relative to the first four fields.
      
     See URL
     https://www.cmascenter.org/ioapi/documentation/3.1/html#tools
      
     Program copyright (C) 1992-2002 MCNC, (C) 1995-2013
     Carlie J. Coats, Jr., and (C) 2002-2010 Baron Advanced
     Meteorological Systems, LLC.  Released under Version 2
     of the GNU General Public License. See enclosed GPL.txt, or
     URL http://www.gnu.org/copyleft/gpl.html
      
     Comments and questions are welcome and can be sent to
      
         Carlie J. Coats, Jr.    carlie@jyarborough.com
     or
         UNC Institute for the Environment
         137 E. Franklin St. Suite 602 Room 613-C
         Campus Box 1105
         Chapel Hill, NC 27599-1105
      
     Program version: 
     $Id:: mtxbuild.f 435 2016-11-22 18:10:58Z coats               $
      
     Value for PROMPTFLAG not defined;returning default:   TRUE
     Continue with program? (Y/N) [Y] >>
     Returning default value TRUE for query:
     "Continue with program?"
     Value for PROMPTFLAG not defined;returning default:   TRUE
     Enter logical name for input "fractions" file [FRACTIONS] >>coef

     File "coef" opened for input on unit:  99
     /public/data/emission/tools/grd2grd/scripts/27to36.matrix.txt

     Value for PROMPTFLAG not defined;returning default:   TRUE
     Enter  input grid name ["FOO"] >>M_27_08CHINA
     Using response "M_27_08CHINA"
     Enter output grid name ["BAR"] >>zj36
     Using response "zj36"

     File "GRIDDESC" opened for input on unit:  98
     /public/data/emission/tools/grd2grd/scripts/GRIDDESC.txt

 MROWS
       22816
 MCOEF
      117675
     
     Value for PROMPTFLAG not defined;returning default:   TRUE
     Enter name for output SPARSE MATRIX file [MATRIX_FILE] >>outfile
     
     Value for IOAPI_CHECK_HEADERS not defined;returning default:   FALSE
     Value for IOAPI_OFFSET_64 not defined;returning default:   TRUE
     Value for IOAPI_CFMETA not defined;returning default:   FALSE
     Value for IOAPI_CMAQMETA not defined; returning defaultval ':  'NONE'
     Value for IOAPI_CMAQMETA not defined; returning defaultval ':  'NONE'
     Value for IOAPI_SMOKEMETA not defined; returning defaultval ':  'NONE'
     Value for IOAPI_SMOKEMETA not defined; returning defaultval ':  'NONE'
     Value for IOAPI_TEXTMETA not defined; returning defaultval ':  'NONE'
     Value for IOAPI_TEXTMETA not defined; returning defaultval ':  'NONE'
      
     "outfile" opened as UNKNOWN(R-W)    
     File name "/public/data/emission/tools/grd2grd/scripts/27to36.matrix.test.nc"
     File type SMATRX3 
     Execution ID "????????????????"
     Grid name "zj36"
     Dimensions: 22816 rows, 117675 cols, 1 lays, 1 vbles
     NetCDF ID:     65536  opened as READWRITE           
     Time-independent data.
     Value for IOAPI_LOG_WRITE not defined;returning default:   TRUE
     Record written to outfile
     
     --->> Normal Completion of program MTXBUILD
     Success in program MTXBUILD
     
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image              PC                Routine            Line        Source             
mtxbuild           00000000004FA74A  Unknown               Unknown  Unknown
libpthread-2.28.s  0000149733E72B20  Unknown               Unknown  Unknown
mtxbuild           00000000006605CD  Unknown               Unknown  Unknown
mtxbuild           00000000005FB335  Unknown               Unknown  Unknown
mtxbuild           00000000005FB16F  Unknown               Unknown  Unknown
mtxbuild           00000000005BE8D6  Unknown               Unknown  Unknown
libc-2.28.so       0000149731FD7B0C  Unknown               Unknown  Unknown
libc-2.28.so       0000149731FD7C40  on_exit               Unknown  Unknown
mtxbuild           00000000004F954F  Unknown               Unknown  Unknown
mtxbuild           000000000042CA1D  m3exit_                   119  m3exit.F
mtxbuild           0000000000407D68  MAIN__                    333  mtxbuild.f
mtxbuild           0000000000405DA2  Unknown               Unknown  Unknown
libc-2.28.so       0000149731FC1493  __libc_start_main     Unknown  Unknown
mtxbuild           0000000000405CAE  Unknown               Unknown  Unknown

The generated sparse matrix netCDF file is all zero.
This revision does not solve the problem.

I finally find that the matrix’s dimension is not assigned correctly in the code. I fix it and create a pull request in github of ioapi.