How to replace very small value with 0 in MEGANv3.2 result with ioapi library

Hi everyone
I run MEGANv3.2 to get input for CMAQv5.4, but the MEGANv3.2 result have very small values which leads to abort in CMAQv5.4:

I want to know if there are programs in the ioapi library that can replace these small values with 0 in the MEGANv3.2 result.
Thanks a lot.

No, there is no such M3Tools program. It should be easy for you to write your own, e.g., following such examples as m3diff and m3cple.

On the other hand, if this is true it is a bug in VDIFF that you ought to get fixed.

Based on the information shown, I’m also not necessarily convinced that the small MEGAN emission numbers you highlighted are the cause of the CCTM error you are encountering. Before changing any model inputs, I would recommend a detailed investigation of what is causing the problem reported by VDIFF by compiling the model in debug mode and then tracing back the occurrence of the NaN values. There might very well be issues with the MEGAN3.2 files, but it might not be the small numbers you highlighted.

1 Like

The only MEGAN 3.2 file that gets NaNs from the preprocessor is the CTF file. CMAQ has a check that should set these to zero. Maybe it’s not working with your compiler. In CMAQ 5.5 this check will be changed to use the intrinsic isnan function:

 Use, intrinsic :: ieee_arithmetic, only: isnan => ieee_is_nan
 .
 .
 .
 WHERE ( isnan( ctf ) ) ctf = 0.0 ! ensure no NaNs

Before putting in these checks I used to set the CTF nans to 0 using an NCL script:

 begin

 a=addfile("CT3_global.ncf","rw")

 ctsf=a->CTS
 ; delete nans
   guess     = 1                ; use zonal means
   is_cyclic = False             ; cyclic [global]
   nscan     = 1500             ; usually much less than this
   eps       = 1.e-2            ; variable dependent
   relc      = 0.6              ; relaxation coefficient
   opt       = 0                ; not used

   ctsf@_FillValue=-999
   replace_ieeenan (ctsf, ctsf@_FillValue, 0)
   poisson_grid_fill( ctsf, is_cyclic, guess, nscan, eps, relc, opt)

   a->CTS=(/ctsf/)

   end
2 Likes

Thank you very much for your reply. Here is my log file.
log.cctm_debug.txt (14.1 KB)

I checked the desid_module.F

And here is one of my MEGANv3.2 result.

Thank you very much for your help.

Thank you very much for your reply.
I am new to CMAQ so I am not very sure if this is a bug. Here is my log file.
log.cctm_debug.txt (14.1 KB)
Thank you for your help.

Thank you very much for your reply.
Do you mean to replace NaN in the CT.csv file? I didn’t find CT.ncf in the result of run.txt2ioapi.v32.csh but there is NaN in my CT.csv
image


Thank you very much for your help.

Thanks for posting this information.

Variable NO in file MEGANv31.tibet27km.CB6.2022208.BDSNP.ncf you posted has NaN values, triggering the “floating invalid” error in line 1202 of desid_module.F

ncdump -v NO MEGANv31.tibet27km.CB6.2022208.BDSNP.ncf | more
netcdf MEGANv31.tibet27km.CB6.2022208.BDSNP {
dimensions:
TSTEP = UNLIMITED ; // (24 currently)
DATE-TIME = 2 ;
LAY = 1 ;
VAR = 34 ;
ROW = 115 ;
COL = 175 ;

…

0.04076951, 0.02598496, 0.03813488, 0.02312617, 0.02550854, 0.02628241,
0.02819956, 0.03518014, 0.03958071, 0.04633633, 0.06118023, 0.0765765,
0.0776834, 0.1098915, 0.08828563, 0.09810115, 0.07552952, 0.06449413,
0.03657012, 0.04044056, 0.03455531, 0.03459061, 0.03966175, 0.05362521,
0.04441707, 0.04557781, 0.05668181, 0.07518451, 0.04282502, 0.09871208,
0.1133665, 0.05309284, 0.09981976, 0.08575544, 0.1104998, 0.1180587,
0.09632298, 0.06107902, 0.07439312, 0.06171234, 0.04607532, 0.04027174,
0.07896861, 0.07552825, 0.07604636, 0.05142521, 0.1081867, 0.1213024,
0.09291191,
NaNf, NaNf, NaNf, 0, NaNf, 0, 0, 0.01823319, 0.09218207, 0.08048284,
0.07364602, 0.06272718, 0.03523219, 0.05210138, 0.03227433, 0.0144209,
0.01159746, 0.04796189, 0.09070752, 0.132624, 0.1437462, 0.1506041,
0.1539077, 0.1633602, 0.1727416, 0.1531, 0.157237, 0.1827559, 0.1820252,
0.1411281, 0.1227397, 0.1222927, 0.1240441, 0.1176117, 0.1220557,

The white pixels in this plot of variable NO from your file correspond to the NaN values:

@willison.jeff had suggested to you in your previous thread to consider using the CMAQ MEGAN inline feature, rather than running MEGAN offline and providing that gridded emission file (MEGANv31.tibet27km.CB6.2022208.BDSNP.ncf) as input to CMAQ. In his post above, the comment about CMAQ checking for these NaN values and setting them to zero refers to running MEGAN inline in CMAQ, not running MEGAN offline.

Thank you!
I have one more question. Do you know how to replace these “NaNf” with 0?
I tried willison.jeff scripts

begin

a=addfile(“MEGANv31.tibet27km.CB6.BDSNP.NoNaN.ncf”,“rw”)

ctsf = a->NO
; delete nans
guess = 1 ; use zonal means
is_cyclic = False ; cyclic [global]
nscan = 1500 ; usually much less than this
eps = 1.e-2 ; variable dependent
relc = 0.6 ; relaxation coefficient
opt = 0 ; not used

ctsf@_FillValue=-999
replace_ieeenan (ctsf, ctsf@_FillValue, 0)
poisson_grid_fill( ctsf, is_cyclic, guess, nscan, eps, relc, opt)

a->CTS=(/ctsf/)

end

and this:

ncatted -a _FillValue,NO,o,f,NaN MEGANv31.tibet27km.CB6.BDSNP.NoNaN.ncf
ncatted -a _FillValue ,NO,m,f,0 MEGANv31.tibet27km.CB6.BDSNP.NoNaN.ncf

but nothing works. Do you have any suggestions?
Thank you.

Personally, I would first try to see what modifications are needed in the MEGAN3.2 offline processing workflow to avoid these NaN values in the MEGAN3.2 output file in the first place. However, I have no experience with MEGAN, so I cannot provide any specific suggestions.

If this is not feasible, I would write a Fortran program that links in the I/O API library and its functions, uses I/O API library functions to open the MEGAN3.2 file that has the NaN values, reads in all fields with I/O API library functions, applies the Fortran intrinsic function ieee_is_nan (mentioned by @willison.jeff above and copied below) to replace any NaNs with 0 values, and then creates and opens a new file and writes out all fields to this new file using I/O API library functions.

Use, intrinsic :: ieee_arithmetic, only: isnan => ieee_is_nan
.
.
.
WHERE ( isnan( ctf ) ) ctf = 0.0 ! ensure no NaNs

Existing m3tools programs like m3xtract or m3thift may be useful as templates for how to write programs that use I/O API functions to open and close files, perform reads and writes, and manipulate fields, though for this particular case, the necessary code likely would be simpler than in these examples.

You should have a netcdf file for your canopy types, as it’s needed for the MEGCAN program when running MEGAN 3.2 outside of CMAQ.

Just to reiterate: the path of least resistance here is to use the time-invariant (static) MEGAN 3.2 inputs with CMAQ. It will take care of the NaNs issue and your previous issue with merging time-varying emissions.

1 Like

Thank you very much for your help. It helps me a lot.
It takes me a while to check the code. The issue is in themgn2mech.F, and I added the code below to fix it.

WHERE ( tmper .ne. tmper ) tmper = 0.0 ! ensure no NaNs

And I don’t know Fortran scripts, especially ones in atmospheric models, but I want to learn about them. Can you give me some advice (like books or websites)?
Thank you!

1 Like

Hi
Thank you very much for your help, the problem has been solved.
And I don’t know Fortran scripts, especially ones in atmospheric models, but I want to learn about them. Can you give me some advice (like books or websites)?
Thank you!

The obvious place to start are the texts by Chivers&Sleightholme or Metcalfe&Reid.

1 Like

Thank you very much for your kind help. I’m very grateful, thanks.