Index: flexpart_code/fpmetbinary_mod.F90
===================================================================
--- flexpart_code/fpmetbinary_mod.F90	(revision ee13a7bba3b6845f825752ed36652bfa33f7825c)
+++ flexpart_code/fpmetbinary_mod.F90	(revision 030e3c19acf0c332ba058cc9e464f8d3ce8166b5)
@@ -304,4 +304,9 @@
 
 
+        ! Helper functions (in this module)
+        !INTEGER :: logical2integer
+        !LOGICAL :: integer2logical
+
+        INTEGER :: temp_integer   ! temporary value
         INTEGER :: ncret          ! Return value from NetCDF calls
         INTEGER :: ncvarid          ! NetCDF variable ID
@@ -309,5 +314,6 @@
         INTEGER :: nxmax_dimid, nymax_dimid, nzmax_dimid, nuvzmax_dimid, nwzmax_dimid, &
 &                  maxspec_dimid, numclass_dimid, maxnests_dimid, nxmaxn_dimid, nymaxn_dimid, &
-&                  zero_to_nzmax_dimid, zero_to_maxnests_dimid, polemap_dimid
+&                  zero_to_nzmax_dimid, zero_to_maxnests_dimid, polemap_dimid, &
+&                  nconvlevmax_dimid, na_dimid
 
 
@@ -327,5 +333,5 @@
 &                                       maxnests_dimname, nxmaxn_dimname, nymaxn_dimname, &
 &                                       zero_to_nzmax_dimname, zero_to_maxnests_dimname, &
-&                                       polemap_dimname
+&                                       polemap_dimname, nconvlevmax_dimname, na_dimname
 
         ! These are temporary variables, used in the LOAD option, for 
@@ -367,9 +373,20 @@
             ncret = nf90_def_dim(ncid, 'numclass', numclass, numclass_dimid)
             call handle_nf90_err(ncret)
+
+            ! There are a handful of variables indexed from 0 to n, rather than 0 to n-1,
+            ! so these dimensions handle that.  What a pain.
             ncret = nf90_def_dim(ncid, 'zero_to_nzmax', nzmax+1, zero_to_nzmax_dimid)
             call handle_nf90_err(ncret)
             ncret = nf90_def_dim(ncid, 'zero_to_maxnests', maxnests+1, zero_to_maxnests_dimid)
             call handle_nf90_err(ncret)
+
+            ! This is for a couple of small arrays that store polar stereographic stuff
             ncret = nf90_def_dim(ncid, 'polemap_dim', 9, polemap_dimid)
+            call handle_nf90_err(ncret)
+
+            ! These two values come from conv_mod
+            ncret = nf90_def_dim(ncid, 'nconvlevmax_dim', nconvlevmax, nconvlevmax_dimid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_def_dim(ncid, 'na_dim', na, na_dimid)
             call handle_nf90_err(ncret)
 
@@ -1872,18 +1889,19 @@
 
 
-
+            ! xglobal, sglobal, nglobal are LOGICAL vars, and need to be converted
+            ! to INTEGER for NetCDF storage
             ncret = nf90_def_var(ncid, 'xglobal', NF90_INT, ncvarid)
             call handle_nf90_err(ncret)
-            ncret = nf90_put_var(ncid, ncvarid, xglobal)
+            ncret = nf90_put_var(ncid, ncvarid, logical2integer(xglobal))
             call handle_nf90_err(ncret)
 
             ncret = nf90_def_var(ncid, 'sglobal', NF90_INT, ncvarid)
             call handle_nf90_err(ncret)
-            ncret = nf90_put_var(ncid, ncvarid, sglobal)
+            ncret = nf90_put_var(ncid, ncvarid, logical2integer(sglobal))
             call handle_nf90_err(ncret)
 
             ncret = nf90_def_var(ncid, 'nglobal', NF90_INT, ncvarid)
             call handle_nf90_err(ncret)
-            ncret = nf90_put_var(ncid, ncvarid, nglobal)
+            ncret = nf90_put_var(ncid, ncvarid, logical2integer(nglobal))
             call handle_nf90_err(ncret)
 
@@ -1924,4 +1942,190 @@
             WRITE(iounit) psconv, tt2conv, td2conv
             WRITE(iounit) nconvlev, nconvtop
+
+
+            dim1dids = (/nconvlevmax_dimid/)
+
+            ncret = nf90_def_var(ncid, 'pconv', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                pconv(:))
+
+            ncret = nf90_def_var(ncid, 'dpr', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                dpr(:))
+
+            ncret = nf90_def_var(ncid, 'pconv_hpa', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                pconv_hpa(:))
+
+            ncret = nf90_def_var(ncid, 'ft', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                ft(:))
+
+            ncret = nf90_def_var(ncid, 'fq', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                fq(:))
+
+            ncret = nf90_def_var(ncid, 'sub', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                sub(:))
+
+            dim1dids = (/na_dimid/)
+
+            ncret = nf90_def_var(ncid, 'phconv', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                phconv(:))
+
+            ncret = nf90_def_var(ncid, 'phconv_hpa', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                phconv_hpa(:))
+
+            ncret = nf90_def_var(ncid, 'tconv', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                tconv(:))
+
+            ncret = nf90_def_var(ncid, 'qconv', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                qconv(:))
+
+            ncret = nf90_def_var(ncid, 'qsconv', NF90_FLOAT, &
+&                                       dim1dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                qsconv(:))
+
+            ! New dimensions
+            dim2dids = (/nconvlevmax_dimid, nconvlevmax_dimid/)
+
+            ncret = nf90_def_var(ncid, 'fmass', NF90_FLOAT, &
+&                                       dim2dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                fmass(:,:))
+
+            ncret = nf90_def_var(ncid, 'fmassfrac', NF90_FLOAT, &
+&                                       dim2dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                fmassfrac(:,:))
+
+
+            ! New dimensions
+            dim2dids = (/nxmax_dimid, nymax_dimid/)
+
+            ncret = nf90_def_var(ncid, 'cbaseflux', NF90_FLOAT, &
+&                                       dim2dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                cbaseflux(0:nxmax-1,0:nymax-1))
+
+            ! New dimensions
+            dim3dids = (/nxmaxn_dimid, nymaxn_dimid, maxnests_dimid/)
+
+            ncret = nf90_def_var(ncid, 'cbasefluxn', NF90_FLOAT, &
+&                                       dim3dids, ncvarid)
+            ncret = nf90_def_var_deflate(ncid, ncvarid,   &
+&                                        shuffle=0,     &
+&                                        deflate=1,     &
+&                                        deflate_level=DEF_LEVEL)
+            ncret = nf90_put_var(ncid, ncvarid, &
+&                                cbasefluxn(0:nxmaxn-1,0:nymaxn-1,1:maxnests))
+
+
+            ! Scalars
+            ncret = nf90_def_var(ncid, 'psconv', NF90_FLOAT, ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_put_var(ncid, ncvarid, psconv)
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_def_var(ncid, 'tt2conv', NF90_FLOAT, ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_put_var(ncid, ncvarid, tt2conv)
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_def_var(ncid, 'td2conv', NF90_FLOAT, ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_put_var(ncid, ncvarid, td2conv)
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_def_var(ncid, 'nconvlev', NF90_INT, ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_put_var(ncid, ncvarid, nconvlev)
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_def_var(ncid, 'nconvtop', NF90_INT, ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_put_var(ncid, ncvarid, nconvtop)
+            call handle_nf90_err(ncret)
+
+            PRINT *, 'SUM(pconv): ', SUM(pconv)
+            PRINT *, 'SUM(qconv): ', SUM(qconv)
+            PRINT *, 'SUM(fmassfrac): ', SUM(fmassfrac)
+            PRINT *, 'SUM(cbasefluxn): ', SUM(cbasefluxn)
+            PRINT *, 'tt2conv: ', tt2conv
+            PRINT *, 'nconvlev: ', nconvlev
+
+
 
         ELSE IF (op == 'LOAD') THEN 
@@ -2844,18 +3048,23 @@
             call handle_nf90_err(ncret)
 
+            ! xglobal, sglobal, nglobal are LOGICAL vars, and need to be converted
+            ! to INTEGER for NetCDF storage
             ncret = nf90_inq_varid(ncid, 'xglobal', ncvarid)
             call handle_nf90_err(ncret)
-            ncret = nf90_get_var(ncid, ncvarid, xglobal)
-            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, temp_integer)
+            call handle_nf90_err(ncret)
+            xglobal = integer2logical(temp_integer)
 
             ncret = nf90_inq_varid(ncid, 'sglobal', ncvarid)
             call handle_nf90_err(ncret)
-            ncret = nf90_get_var(ncid, ncvarid, sglobal)
-            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, temp_integer)
+            call handle_nf90_err(ncret)
+            sglobal = integer2logical(temp_integer)
 
             ncret = nf90_inq_varid(ncid, 'nglobal', ncvarid)
             call handle_nf90_err(ncret)
-            ncret = nf90_get_var(ncid, ncvarid, nglobal)
-            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, temp_integer)
+            call handle_nf90_err(ncret)
+            nglobal = integer2logical(temp_integer)
 
             ncret = nf90_inq_varid(ncid, 'switchnorthg', ncvarid)
@@ -2898,4 +3107,122 @@
             READ(iounit) nconvlev, nconvtop
 
+            ncret = nf90_inq_varid(ncid, 'pconv', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, pconv(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'dpr', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, dpr(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'pconv_hpa', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, pconv_hpa(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'ft', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, ft(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'fq', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, fq(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'sub', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, sub(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'phconv', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, phconv(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'phconv_hpa', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, phconv_hpa(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'tconv', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, tconv(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'qconv', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, qconv(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'qsconv', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, qsconv(:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'fmass', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, fmass(:,:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'fmassfrac', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, fmassfrac(:,:))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'cbaseflux', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, cbaseflux(0:nxmax-1,0:nymax-1))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'cbasefluxn', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, cbasefluxn(0:nxmaxn-1,0:nymaxn-1,1:maxnests))
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'psconv', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, psconv)
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'tt2conv', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, tt2conv)
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'td2conv', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, td2conv)
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'nconvlev', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, nconvlev)
+            call handle_nf90_err(ncret)
+
+            ncret = nf90_inq_varid(ncid, 'nconvtop', ncvarid)
+            call handle_nf90_err(ncret)
+            ncret = nf90_get_var(ncid, ncvarid, nconvtop)
+            call handle_nf90_err(ncret)
+
+
+
+
+            PRINT *, 'SUM(pconv): ', SUM(pconv)
+            PRINT *, 'SUM(qconv): ', SUM(qconv)
+            PRINT *, 'SUM(fmassfrac): ', SUM(fmassfrac)
+            PRINT *, 'SUM(cbasefluxn): ', SUM(cbasefluxn)
+            PRINT *, 'tt2conv: ', tt2conv
+            PRINT *, 'nconvlev: ', nconvlev
+
+
+
+
+
+
+
+
+
         ELSE
             STOP 'fpio(): Illegal operation' 
@@ -2965,4 +3292,42 @@
 
 
+    INTEGER FUNCTION logical2integer(logical_value)
+        IMPLICIT NONE
+
+        ! Auxiliary function to convert logical values to
+        ! integers.  THIS DOES NO TYPE CHECKING!!!
+
+
+        LOGICAL, INTENT(IN) :: logical_value
+
+        IF (logical_value .EQV. .TRUE.) THEN
+            logical2integer = 1
+        ELSE
+            logical2integer = 0
+        ENDIF
+
+        RETURN
+
+    END FUNCTION logical2integer
+
+
+    LOGICAL FUNCTION integer2logical(integer_value)
+        IMPLICIT NONE
+
+        ! Auxiliary function to convert integer values to
+        ! logical.  THIS DOES NO TYPE CHECKING!!!
+
+
+        INTEGER, INTENT(IN) :: integer_value
+
+        IF (integer_value .EQ. 0) THEN
+            integer2logical = .FALSE.
+        ELSE
+            integer2logical = .TRUE.
+        ENDIF
+
+        RETURN
+
+    END FUNCTION integer2logical
 
 
