# Makefile written by Lucas A. J. Bastien.
#
# Targets:
#
# - dir:    creates the directory tree that will host the built files (object
#           files, libraries, executabes, etc.).
#
# - pario:  makes the PARIO library.
#
# - stenex: makes the STENEX libraries.
#
# - jproc:  makes the JPROC executable.
#
# - cctm:   makes the CCTM executable.
#
# - clean:  removes all the built files (object files, libraries, executabes,
#           etc.). Use this target with care as it uses "rm -r"!

#############################################
# This section should be edited by users    #
# according to their system's configuration #
#############################################

# Assign "yes" to PARALLELIZED in order to build the code with support for
# parallelized computations. Assign any other value otherwise
PARALLELIZED = yes

# Chemical mechanism to use:
# - saprc99 for the SAPRC99 chemical mechanism.
# - hcho for formaldehyde in tracer mode (i.e. without photochemistry).
CHEMICAL_MECHANISM = saprc99

# Fortran compiler configuration. You probably want to use mpif90 as a fortran
# compiler if you want to build the code with support for parallelized
# computations. Use your compiler of choice to build the code without support
# for parallelized computations (example : FC = gfortran)
FC = mpif90
FC_FLAG_MOD = -J
F77_FLAGS = -ffixed-line-length-none -fmax-errors=1 -cpp
F90_FLAGS = -fmax-errors=1 -cpp
FOPT_FLAGS = -O2

# C compiler configuration. You probably want to use mpicc as a C compiler if
# you want to build the code with support for parallelized computations. Use
# your compiler of choice to build the code without support for parallelized
# computations (example : FC = gcc)
CC = mpicc
C_FLAGS =

# Linker configuration
LINK_FLAGS =

# Location of the directory that contains the source code
DIR_SRC = # Needs to be set by user

# Where to install the built files
DIR_INSTL = # Needs to be set by user

# Location of the IOAPI installation directory. This directory must contain a
# directory named "fixed_src_f77", which in turns contains the IOAPI include
# files (in FORTRAN 77 format)
DIR_IOAPI = # Needs to be set by user

# Location of the NetCDF installation directory
DIR_NETCDF = # Needs to be set by user

# Location of the MPI installation directory
DIR_MPI = # Needs to be set by user

# Name of the FORTRAN netcdf library (on some systems, the C and FORTRAN netcdf
# libraries are put together in libnetcdf while in some other systems, the
# FORTRAN library is in a separate file called libnetcdff)
LIB_NETCDF = -lnetcdff

# Name of the library of MPI fortran bindings (may be -lmpi_f90 or -lmpi_mpifh
# depending on the system's configuration
LIB_MPIFORTRAN = -lmpi_mpifh

######################################################
# The section below should not need any modification #
######################################################

# --------------- #
# Top-level rules #
# --------------- #

ifeq ($(PARALLELIZED),yes)
    PAR_POPT_DIR = par
else
    PAR_POPT_DIR = par_noop
endif

ifeq ($(CHEMICAL_MECHANISM),saprc99)
    CHEM_OPT_DIR = chem_SAPRC99ROS
else
    CHEM_OPT_DIR = chem_noop
endif

all: dir pario stenex jproc cctm

dir:
	mkdir -vp $(DIR_INSTL)
	mkdir -vp $(DIR_INSTL)/lib
	mkdir -vp $(DIR_INSTL)/lib/PARIO
	mkdir -vp $(DIR_INSTL)/lib/STENEX
	mkdir -vp $(DIR_INSTL)/bin
	mkdir -vp $(DIR_INSTL)/bin/JPROC
	mkdir -vp $(DIR_INSTL)/bin/CCTM
	mkdir -vp $(DIR_INSTL)/bld
	mkdir -vp $(DIR_INSTL)/bld/PARIO/
	mkdir -vp $(DIR_INSTL)/bld/PARIO/mod
	mkdir -vp $(DIR_INSTL)/bld/STENEX
	mkdir -vp $(DIR_INSTL)/bld/STENEX/noop_f90
	mkdir -vp $(DIR_INSTL)/bld/STENEX/noop_f90/mod
	mkdir -vp $(DIR_INSTL)/bld/STENEX/se_snl
	mkdir -vp $(DIR_INSTL)/bld/STENEX/se_snl/mod
	mkdir -vp $(DIR_INSTL)/bld/JPROC
	mkdir -vp $(DIR_INSTL)/bld/JPROC/driver
	mkdir -vp $(DIR_INSTL)/bld/JPROC/driver/jproc_table
	mkdir -vp $(DIR_INSTL)/bld/JPROC/driver/jproc_table/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM
	mkdir -vp $(DIR_INSTL)/bld/CCTM/driver
	mkdir -vp $(DIR_INSTL)/bld/CCTM/driver/ctm
	mkdir -vp $(DIR_INSTL)/bld/CCTM/driver/ctm/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/par
	mkdir -vp $(DIR_INSTL)/bld/CCTM/par/$(PAR_POPT_DIR)
	mkdir -vp $(DIR_INSTL)/bld/CCTM/par/$(PAR_POPT_DIR)/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/init
	mkdir -vp $(DIR_INSTL)/bld/CCTM/init/init
	mkdir -vp $(DIR_INSTL)/bld/CCTM/init/init/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/couple
	mkdir -vp $(DIR_INSTL)/bld/CCTM/couple/gencoor
	mkdir -vp $(DIR_INSTL)/bld/CCTM/couple/gencoor/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/hadv
	mkdir -vp $(DIR_INSTL)/bld/CCTM/hadv/hppm
	mkdir -vp $(DIR_INSTL)/bld/CCTM/hadv/hppm/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/vadv
	mkdir -vp $(DIR_INSTL)/bld/CCTM/vadv/vppm
	mkdir -vp $(DIR_INSTL)/bld/CCTM/vadv/vppm/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/hdiff
	mkdir -vp $(DIR_INSTL)/bld/CCTM/hdiff/multiscale
	mkdir -vp $(DIR_INSTL)/bld/CCTM/hdiff/multiscale/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/vdiff
	mkdir -vp $(DIR_INSTL)/bld/CCTM/vdiff/eddy
	mkdir -vp $(DIR_INSTL)/bld/CCTM/vdiff/eddy/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/phot
	mkdir -vp $(DIR_INSTL)/bld/CCTM/phot/phot
	mkdir -vp $(DIR_INSTL)/bld/CCTM/phot/phot/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/util
	mkdir -vp $(DIR_INSTL)/bld/CCTM/util/util
	mkdir -vp $(DIR_INSTL)/bld/CCTM/util/util/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/adjoint
	mkdir -vp $(DIR_INSTL)/bld/CCTM/adjoint/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)
	mkdir -vp $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/mod
	mkdir -vp $(DIR_INSTL)/bld/CCTM/adjutil
	mkdir -vp $(DIR_INSTL)/bld/CCTM/adjutil/mod

pario: $(DIR_INSTL)/lib/PARIO/libpario.a

stenex: $(DIR_INSTL)/lib/STENEX/libstenex_se_snl.a $(DIR_INSTL)/lib/STENEX/libstenex_noop_f90.a

jproc: $(DIR_INSTL)/bin/JPROC/jproc

cctm: $(DIR_INSTL)/bin/CCTM/cctm

clean:
	rm -rf $(DIR_INSTL)/bld
	rm -rf $(DIR_INSTL)/lib
	rm -rf $(DIR_INSTL)/bin

# ---------------- #
# Useful variables #
# ---------------- #

INC_CONST = $(DIR_SRC)/ICL/fixed/const
INC_MECH = $(DIR_SRC)/ICL/mech/$(CHEMICAL_MECHANISM)

# ------------------ #
# SUBSTS definitions #
# ------------------ #

ifeq ($(PARALLELIZED),yes)
    STENEX_POPT = SE
    STENEX_POPT_DIR = se_snl
else
    STENEX_POPT = NOOP
    STENEX_POPT_DIR = noop_f90
endif

# Grid ID
SUBST_GRID_ID = -DSUBST_GRID_ID=
# IOAPI include files
SUBST_IOPARMS = -DSUBST_IOPARMS=\"PARMS3.EXT\"
SUBST_IOFDESC = -DSUBST_IOFDESC=\"FDESC3.EXT\"
SUBST_IODECL = -DSUBST_IODECL=\"IODECL3.EXT\"
# CMAQ include files (general cctm)
SUBST_PE_COMM = -DSUBST_PE_COMM=\"PE_COMM.EXT\"
SUBST_CONST  = -DSUBST_CONST=\"CONST.EXT\"
SUBST_FILES_ID = -DSUBST_FILES_ID=\"FILES_CTM.EXT\"
SUBST_EMPR_VD = -DSUBST_EMPR_VD=\"EMISPRM.vdif.EXT\"
SUBST_RXCMMN = -DSUBST_RXCMMN=\"RXCM.EXT\"
SUBST_RXDATA = -DSUBST_RXDATA=\"RXDT.EXT\"
# CMAQ include files (GC)
SUBST_GC_SPC = -DSUBST_GC_SPC=\"GC_SPC.EXT\"
SUBST_GC_EMIS = -DSUBST_GC_EMIS=\"GC_EMIS.EXT\"
SUBST_GC_ICBC = -DSUBST_GC_ICBC=\"GC_ICBC.EXT\"
SUBST_GC_DIFF = -DSUBST_GC_DIFF=\"GC_DIFF.EXT\"
SUBST_GC_DDEP = -DSUBST_GC_DDEP=\"GC_DDEP.EXT\"
SUBST_GC_DEPV = -DSUBST_GC_DEPV=\"GC_DEPV.EXT\"
SUBST_GC_ADV = -DSUBST_GC_ADV=\"GC_ADV.EXT\"
SUBST_GC_CONC = -DSUBST_GC_CONC=\"GC_CONC.EXT\"
SUBST_GC_G2AE = -DSUBST_GC_G2AE=\"GC_G2AE.EXT\"
# CMAQ include files (AE)
SUBST_AE_SPC = -DSUBST_AE_SPC=\"AE_SPC.EXT\"
SUBST_AE_EMIS = -DSUBST_AE_EMIS=\"AE_EMIS.EXT\"
SUBST_AE_ICBC = -DSUBST_AE_ICBC=\"AE_ICBC.EXT\"
SUBST_AE_DIFF = -DSUBST_AE_DIFF=\"AE_DIFF.EXT\"
SUBST_AE_DDEP = -DSUBST_AE_DDEP=\"AE_DDEP.EXT\"
SUBST_AE_DEPV = -DSUBST_AE_DEPV=\"AE_DEPV.EXT\"
SUBST_AE_ADV = -DSUBST_AE_ADV=\"AE_ADV.EXT\"
SUBST_AE_CONC = -DSUBST_AE_CONC=\"AE_CONC.EXT\"
# CMAQ include files (NR)
SUBST_NR_SPC = -DSUBST_NR_SPC=\"NR_SPC.EXT\"
SUBST_NR_EMIS = -DSUBST_NR_EMIS=\"NR_EMIS.EXT\"
SUBST_NR_ICBC = -DSUBST_NR_ICBC=\"NR_ICBC.EXT\"
SUBST_NR_DIFF = -DSUBST_NR_DIFF=\"NR_DIFF.EXT\"
SUBST_NR_DDEP = -DSUBST_NR_DDEP=\"NR_DDEP.EXT\"
SUBST_NR_DEPV = -DSUBST_NR_DEPV=\"NR_DEPV.EXT\"
SUBST_NR_ADV = -DSUBST_NR_ADV=\"NR_ADV.EXT\"
SUBST_NR_N2AE = -DSUBST_NR_N2AE=\"NR_N2AE.EXT\"
# CMAQ include files (TR)
SUBST_TR_SPC = -DSUBST_TR_SPC=\"TR_SPC.EXT\"
SUBST_TR_EMIS = -DSUBST_TR_EMIS=\"TR_EMIS.EXT\"
SUBST_TR_ICBC = -DSUBST_TR_ICBC=\"TR_ICBC.EXT\"
SUBST_TR_DIFF = -DSUBST_TR_DIFF=\"TR_DIFF.EXT\"
SUBST_TR_DDEP = -DSUBST_TR_DDEP=\"TR_DDEP.EXT\"
SUBST_TR_DEPV = -DSUBST_TR_DEPV=\"TR_DEPV.EXT\"
SUBST_TR_ADV = -DSUBST_TR_ADV=\"TR_ADV.EXT\"
# CMAQ include files (process analysis)
SUBST_PACTL_ID = -DSUBST_PACTL_ID=\"PA_CTL.EXT\"
# MPI include files
SUBST_MPICH = -DSUBST_MPICH=\"mpif.h\"
# STENEX include files
SUBST_MODULES = -DSUBST_MODULES=${STENEX_POPT}_MODULES
SUBST_BARRIER = -DSUBST_BARRIER=${STENEX_POPT}_BARRIER
SUBST_GLOBAL_MAX = -DSUBST_GLOBAL_MAX=${STENEX_POPT}_GLOBAL_MAX
SUBST_GLOBAL_SUM = -DSUBST_GLOBAL_SUM=${STENEX_POPT}_GLOBAL_SUM
SUBST_LOOP_INDEX = -DSUBST_LOOP_INDEX=${STENEX_POPT}_LOOP_INDEX
SUBST_HI_LO_BND_PE = -DSUBST_HI_LO_BND_PE=${STENEX_POPT}_HI_LO_BND_PE
SUBST_SUM_CHK = -DSUBST_SUM_CHK=${STENEX_POPT}_SUM_CHK
SUBST_COMM = -DSUBST_COMM=${STENEX_POPT}_COMM

# Put all the subst definitions together
SUBSTS = $(SUBST_GRID_ID)\
         $(SUBST_IOPARMS)\
         $(SUBST_IOFDESC)\
         $(SUBST_IODECL)\
         $(SUBST_PE_COMM)\
         $(SUBST_CONST)\
         $(SUBST_FILES_ID)\
         $(SUBST_EMPR_VD)\
         $(SUBST_RXCMMN)\
         $(SUBST_RXDATA)\
         $(SUBST_GC_SPC)\
         $(SUBST_GC_EMIS)\
         $(SUBST_GC_ICBC)\
         $(SUBST_GC_DIFF)\
         $(SUBST_GC_DDEP)\
         $(SUBST_GC_DEPV)\
         $(SUBST_GC_ADV)\
         $(SUBST_GC_CONC)\
         $(SUBST_GC_G2AE)\
         $(SUBST_AE_SPC)\
         $(SUBST_AE_EMIS)\
         $(SUBST_AE_ICBC)\
         $(SUBST_AE_DIFF)\
         $(SUBST_AE_DDEP)\
         $(SUBST_AE_DEPV)\
         $(SUBST_AE_ADV)\
         $(SUBST_AE_CONC)\
         $(SUBST_NR_SPC)\
         $(SUBST_NR_EMIS)\
         $(SUBST_NR_ICBC)\
         $(SUBST_NR_DIFF)\
         $(SUBST_NR_DDEP)\
         $(SUBST_NR_DEPV)\
         $(SUBST_NR_ADV)\
         $(SUBST_NR_N2AE)\
         $(SUBST_TR_SPC)\
         $(SUBST_TR_EMIS)\
         $(SUBST_TR_ICBC)\
         $(SUBST_TR_DIFF)\
         $(SUBST_TR_DDEP)\
         $(SUBST_TR_DEPV)\
         $(SUBST_TR_ADV)\
         $(SUBST_PACTL_ID)\
         $(SUBST_MPICH)\
         $(SUBST_MODULES)\
         $(SUBST_BARRIER)\
         ${SUBST_GLOBAL_MAX}\
         $(SUBST_GLOBAL_SUM)\
         $(SUBST_LOOP_INDEX)\
         $(SUBST_HI_LO_BND_PE)\
         $(SUBST_SUM_CHK)\
         $(SUBST_COMM)

# ----- #
# PARIO #
# ----- #

OBJ_PARIO = $(DIR_INSTL)/bld/PARIO/pinterpb_mod.o\
            $(DIR_INSTL)/bld/PARIO/alloc_data_mod.o\
            $(DIR_INSTL)/bld/PARIO/piomaps_mod.o\
            $(DIR_INSTL)/bld/PARIO/boundary.o\
            $(DIR_INSTL)/bld/PARIO/get_write_map.o\
            $(DIR_INSTL)/bld/PARIO/get_read_map.o\
            $(DIR_INSTL)/bld/PARIO/growbuf.o\
            $(DIR_INSTL)/bld/PARIO/gtndxhdv.o\
            $(DIR_INSTL)/bld/PARIO/initm3io.o\
            $(DIR_INSTL)/bld/PARIO/interpol.o\
            $(DIR_INSTL)/bld/PARIO/pinterpb.o\
            $(DIR_INSTL)/bld/PARIO/pio_end.o\
            $(DIR_INSTL)/bld/PARIO/pio_init.o\
            $(DIR_INSTL)/bld/PARIO/pm3err.o\
            $(DIR_INSTL)/bld/PARIO/pm3exit.o\
            $(DIR_INSTL)/bld/PARIO/pm3warn.o\
            $(DIR_INSTL)/bld/PARIO/pshut3.o\
            $(DIR_INSTL)/bld/PARIO/pwrgrdd.o\
            $(DIR_INSTL)/bld/PARIO/prdgrdd.o\
            $(DIR_INSTL)/bld/PARIO/pwrite3.o\
            $(DIR_INSTL)/bld/PARIO/pread3.o\
            $(DIR_INSTL)/bld/PARIO/readbndy.o\
            $(DIR_INSTL)/bld/PARIO/subdmap.o\
            $(DIR_INSTL)/bld/PARIO/wrsubmap.o

$(DIR_INSTL)/lib/PARIO/libpario.a: $(OBJ_PARIO)
	ar rv $@ $(DIR_INSTL)/bld/PARIO/*.o

$(DIR_INSTL)/bld/PARIO/%.o: $(DIR_SRC)/PARIO/%.f
	$(FC) $(F77_FLAGS) -I$(DIR_IOAPI)/fixed_src_f77 -I$(DIR_MPI)/include $(FC_FLAG_MOD)bld/PARIO/mod -c $(DIR_SRC)/PARIO/$*.f -o $(DIR_INSTL)/bld/PARIO/$*.o

# ------ #
# STENEX #
# ------ #

OBJ_STENEX_se_snl = $(DIR_INSTL)/bld/STENEX/se_snl/se_comm_info_ext.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_disp_info_ext.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_domain_info_ext.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_ori_ext.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_pe_info_ext.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_subgrid_info_ext.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_reconfig_grid_info_ext.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_bndy_copy_info_ext.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_internal_util_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_global_max_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_global_min_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_global_sum_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_util_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_data_send_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_data_recv_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_slice_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_comm_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_gather_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_data_copy_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_reconfig_grid_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_bndy_copy_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/swap_sandia.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/swap_sandia_routines.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_init_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_term_module.o\
                    $(DIR_INSTL)/bld/STENEX/se_snl/se_modules.o

OBJ_STENEX_noop_f90 = $(DIR_INSTL)/bld/STENEX/noop_f90/noop_comm_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_data_copy_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_gather_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_global_max_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_global_min_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_global_sum_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_init_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_term_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_slice_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_util_module.o\
                      $(DIR_INSTL)/bld/STENEX/noop_f90/noop_modules.o

$(DIR_INSTL)/lib/STENEX/libstenex_se_snl.a: $(OBJ_STENEX_se_snl)
	ar rv $@ $(DIR_INSTL)/bld/STENEX/se_snl/*.o

$(DIR_INSTL)/bld/STENEX/se_snl/%.o: $(DIR_SRC)/STENEX/se_snl/%.f
	$(FC) $(F77_FLAGS) -I$(DIR_MPI)/include $(FC_FLAG_MOD)$(DIR_INSTL)/bld/STENEX/se_snl/mod -c $(DIR_SRC)/STENEX/se_snl/$*.f -o $(DIR_INSTL)/bld/STENEX/se_snl/$*.o

$(DIR_INSTL)/lib/STENEX/libstenex_noop_f90.a: $(OBJ_STENEX_noop_f90)
	ar rv $@ $(DIR_INSTL)/bld/STENEX/noop_f90/*.o

$(DIR_INSTL)/bld/STENEX/noop_f90/%.o: $(DIR_SRC)/STENEX/noop_f90/%.f
	$(FC) $(F77_FLAGS) -I$(DIR_MPI)/include $(FC_FLAG_MOD)$(DIR_INSTL)/bld/STENEX/noop_f90/mod -c $(DIR_SRC)/STENEX/noop_f90/$*.f -o $(DIR_INSTL)/bld/STENEX/noop_f90/$*.o

# ----- #
# JPROC #
# ----- #

OBJ_JPROC_jproc_table = $(DIR_INSTL)/bld/JPROC/driver/jproc_table/calczen.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/chj.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/index2.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/intavg.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/interp.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/jproc.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/o3scal.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/optics.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/pntavg.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/readcsqy.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/readet.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/reado2.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/reado3.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/readprof.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/readtoms.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/setaer.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/setair.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/setalb.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/setcld.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/srband.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/subgrid.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/tridiag.o\
                        $(DIR_INSTL)/bld/JPROC/driver/jproc_table/twostr.o

$(DIR_INSTL)/bin/JPROC/jproc: $(OBJ_JPROC_jproc_table)
	$(FC) $(LINK_FLAGS) $^ -L$(DIR_IOAPI)/lib -lioapi -L$(DIR_NETCDF)/lib $(LIB_NETCDF) -o $@

$(DIR_INSTL)/bld/JPROC/driver/jproc_table/%.o: $(DIR_SRC)/JPROC/driver/jproc_table/%.[Ff]
	$(FC) $(F77_FLAGS) -I$(INC_CONST) -I$(INC_MECH) $(SUBSTS) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/JPROC/driver/jproc_table/mod -c $< -o $@

# ---- #
# CCTM #
# ---- #

ifeq ($(PARALLELIZED),yes)
    s_OR_p = p
    LIB_PARIO = -L$(DIR_INSTL)/lib/PARIO -lpario
    PAR_GOODIES = -Dparallel -Dcluster -DINTERPB=PINTERPB -DM3ERR=PM3ERR -DM3EXIT=PM3EXIT -DM3WARN=PM3WARN -DSHUT3=PSHUT3 -DWRITE3=PWRITE3 -DSUBST_READ3=PREAD3
else
    s_OR_p = s
    LIB_PARIO =
    PAR_GOODIES = -DINTERPB=INTERP3 -DSUBST_READ3=READ3
endif

LIBS = $(LIB_PARIO)\
       -L$(DIR_INSTL)/lib/STENEX/ -lstenex_$(STENEX_POPT_DIR)\
       -L$(DIR_IOAPI)/lib -lioapi\
       -L$(DIR_NETCDF)/lib $(LIB_NETCDF)\
       -L$(DIR_MPI)/lib $(LIB_MPIFORTRAN)

F_FLAGS_ADD = -I$(DIR_MPI)/include\
              -I$(INC_CONST)\
              -I$(DIR_SRC)/ICL/fixed/filenames\
              -I$(INC_MECH)\
              -I$(DIR_SRC)/ICL/mech/trac0\
              -I$(DIR_SRC)/ICL/fixed/par\
              -I$(DIR_SRC)/ICL/fixed/emctrl\
              -I$(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/mod\
              -I$(DIR_INSTL)/bld/CCTM/adjutil/mod\
              -I$(DIR_INSTL)/bld/CCTM/driver/ctm/mod\
              -I$(DIR_INSTL)/bld/CCTM/vdiff/eddy/mod\
              -I$(DIR_INSTL)/bld/STENEX/$(STENEX_POPT_DIR)/mod\
              -I$(DIR_SRC)/ICL/procan/pa_noop\
              -I$(DIR_IOAPI)/fixed_src_f77\
              $(PAR_GOODIES)\
              $(SUBSTS)

OBJ_CCTM_adjoint = $(DIR_INSTL)/bld/CCTM/adjoint/barrier_${s_OR_p}.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/walltime_${s_OR_p}.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/adjverbose.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/forcing_adj.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/hdiff_adj_${s_OR_p}.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/sciproc.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/advstepmin.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/find_lun.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/rd_advstepmin.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/rd_chk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/rd_l2chk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/rd_l4chk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/rd_l5chk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/rd_l5fchk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/rd_receptor.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/tridiag_adj.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/wr_advstepmin.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/wr_chk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/wr_l2chk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/wr_l4chk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/wr_l5chk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/wr_l5fchk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/wr_al5chk.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/wr_aconc.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/xadvppm_adj.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/yadvppm_adj.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/zadvppm_adj.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/sciproc_adj.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/vdiffim_adj.o\
                   $(DIR_INSTL)/bld/CCTM/adjoint/driver.o

ifeq ($(CHEMICAL_MECHANISM),saprc99)
    OBJ_CCTM_adjoint_chem = $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_Precision.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_Parameters.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_Monitor.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_Global.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_Initialize.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_Rates.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_Function.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_JacobianSP.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_Jacobian.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_HessianSP.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_Hessian.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_LinearAlgebra.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/${CHEM_OPT_DIR}/saprc99ros_Integrator.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/saprc99ros_adj_Integrator.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/CMAQKPP_SAPRC99_INTERFACE.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/chem.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/chem_adj.o
else
    OBJ_CCTM_adjoint_chem = $(DIR_INSTL)/bld/CCTM/adjoint/${CHEM_OPT_DIR}/chem.o\
                            $(DIR_INSTL)/bld/CCTM/adjoint/${CHEM_OPT_DIR}/chem_adj.o
endif

OBJ_CCTM_adjutil = $(DIR_INSTL)/bld/CCTM/adjutil/ADJPRM.o

OBJ_CCTM_driver = $(DIR_INSTL)/bld/CCTM/driver/ctm/HGRD_DEFN.o\
                  $(DIR_INSTL)/bld/CCTM/driver/ctm/VGRD_DEFN.o\
                  $(DIR_INSTL)/bld/CCTM/driver/ctm/CGRID_SPCS.o\
                  $(DIR_INSTL)/bld/CCTM/driver/ctm/GRID_CONF.o\
                  $(DIR_INSTL)/bld/CCTM/driver/ctm/PCGRID_DEFN.o\
                  $(DIR_INSTL)/bld/CCTM/driver/ctm/advstep.o\
                  $(DIR_INSTL)/bld/CCTM/driver/ctm/hcontvel.o\
                  $(DIR_INSTL)/bld/CCTM/driver/ctm/wr_conc.o

ifeq ($(PARALLELIZED),yes)
    OBJ_CCTM_par = $(DIR_INSTL)/bld/CCTM/par/par/par_init.o\
                   $(DIR_INSTL)/bld/CCTM/par/par/par_term.o\
                   $(DIR_INSTL)/bld/CCTM/par/par/distr_env.o
else
    OBJ_CCTM_par = $(DIR_INSTL)/bld/CCTM/par/par_noop/par_init_noop.o\
                   $(DIR_INSTL)/bld/CCTM/par/par_noop/par_term_noop.o
endif

OBJ_CCTM_init = $(DIR_INSTL)/bld/CCTM/init/init/diffmsg.o\
                $(DIR_INSTL)/bld/CCTM/init/init/flcheck.o\
                $(DIR_INSTL)/bld/CCTM/init/init/grdcheck.o\
                $(DIR_INSTL)/bld/CCTM/init/init/initscen.o\
                $(DIR_INSTL)/bld/CCTM/init/init/load_cgrid.o\
                $(DIR_INSTL)/bld/CCTM/init/init/opconc.o\
                $(DIR_INSTL)/bld/CCTM/init/init/setup_logdev.o

OBJ_CCTM_couple = $(DIR_INSTL)/bld/CCTM/couple/gencoor/couple.o\
                  $(DIR_INSTL)/bld/CCTM/couple/gencoor/ppmv_msmr.o

OBJ_CCTM_hadv = $(DIR_INSTL)/bld/CCTM/hadv/hppm/advbc_map.o\
                $(DIR_INSTL)/bld/CCTM/hadv/hppm/hppm.o\
                $(DIR_INSTL)/bld/CCTM/hadv/hppm/rdbcon.o\
                $(DIR_INSTL)/bld/CCTM/hadv/hppm/xadvppm.o\
                $(DIR_INSTL)/bld/CCTM/hadv/hppm/yadvppm.o\
                $(DIR_INSTL)/bld/CCTM/hadv/hppm/zfdbc.o

OBJ_CCTM_vadv = $(DIR_INSTL)/bld/CCTM/vadv/vppm/vcontvel.o\
                $(DIR_INSTL)/bld/CCTM/vadv/vppm/vppm.o\
                $(DIR_INSTL)/bld/CCTM/vadv/vppm/zadvppm.o

OBJ_CCTM_hdiff = $(DIR_INSTL)/bld/CCTM/hdiff/multiscale/deform.o\
                 $(DIR_INSTL)/bld/CCTM/hdiff/multiscale/hcdiff3d.o\
                 $(DIR_INSTL)/bld/CCTM/hdiff/multiscale/hdiff.o\
                 $(DIR_INSTL)/bld/CCTM/hdiff/multiscale/rho_j.o

OBJ_CCTM_vdiff = $(DIR_INSTL)/bld/CCTM/vdiff/eddy/AERO_EMIS.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/conv_cgrid.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/edyintb.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/opddep.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/opemis.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/opmet.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/opssemis.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/rddepv.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/rdemis.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/rdmet.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/ssemis.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/tridiag.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/vdiff_map.o\
                 $(DIR_INSTL)/bld/CCTM/vdiff/eddy/vdiffim.o

OBJ_CCTM_phot = $(DIR_INSTL)/bld/CCTM/phot/phot/phot.o

OBJ_CCTM_util = $(DIR_INSTL)/bld/CCTM/util/util/cksummer.o\
                $(DIR_INSTL)/bld/CCTM/util/util/findex.o\
                $(DIR_INSTL)/bld/CCTM/util/util/get_envlist.o\
                $(DIR_INSTL)/bld/CCTM/util/util/lstepf.o\
                $(DIR_INSTL)/bld/CCTM/util/util/subhfile.o

OBJ_CCTM = $(OBJ_CCTM_adjutil)\
           $(OBJ_CCTM_driver)\
           $(OBJ_CCTM_par)\
           $(OBJ_CCTM_init)\
           $(OBJ_CCTM_couple)\
           $(OBJ_CCTM_hadv)\
           $(OBJ_CCTM_vadv)\
           $(OBJ_CCTM_hdiff)\
           $(OBJ_CCTM_vdiff)\
           $(OBJ_CCTM_phot)\
           $(OBJ_CCTM_util)\
           $(OBJ_CCTM_adjoint_chem)\
           $(OBJ_CCTM_adjoint)

$(DIR_INSTL)/bin/CCTM/cctm: $(OBJ_CCTM)
	$(FC) $(LINK_FLAGS) $^ $(LIBS) -o $@

$(DIR_INSTL)/bld/CCTM/driver/ctm/%.o: $(DIR_SRC)/CCTM/driver/ctm/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/driver/ctm/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/par/$(PAR_POPT_DIR)/%.o: $(DIR_SRC)/CCTM/par/$(PAR_POPT_DIR)/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/par/$(PAR_POPT_DIR)/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/par/$(PAR_POPT_DIR)/%.o: $(DIR_SRC)/CCTM/par/$(PAR_POPT_DIR)/%.c
	$(CC) $(C_FLAGS) $(F_FLAGS_ADD) -c $< -o $@

$(DIR_INSTL)/bld/CCTM/init/init/%.o: $(DIR_SRC)/CCTM/init/init/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/init/init/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/couple/gencoor/%.o: $(DIR_SRC)/CCTM/couple/gencoor/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/couple/gencoor/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/hadv/hppm/%.o: $(DIR_SRC)/CCTM/hadv/hppm/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/hadv/hppm/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/vadv/vppm/%.o: $(DIR_SRC)/CCTM/vadv/vppm/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/vadv/vppm/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/hdiff/multiscale/%.o: $(DIR_SRC)/CCTM/hdiff/multiscale/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/hdiff/multiscale/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/vdiff/eddy/%.o: $(DIR_SRC)/CCTM/vdiff/eddy/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/vdiff/eddy/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/phot/phot/%.o: $(DIR_SRC)/CCTM/phot/phot/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/phot/phot/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/util/util/%.o: $(DIR_SRC)/CCTM/util/util/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/util/util/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/adjoint/%.o: $(DIR_SRC)/CCTM/adjoint/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/adjoint/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/%.o: $(DIR_SRC)/CCTM/adjoint/$(CHEM_OPT_DIR)/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/%.o: $(DIR_SRC)/CCTM/adjoint/$(CHEM_OPT_DIR)/%.[Ff]90
	$(FC) $(F90_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/adjoint/$(CHEM_OPT_DIR)/mod -c $< -o $@

$(DIR_INSTL)/bld/CCTM/adjutil/%.o: $(DIR_SRC)/CCTM/adjutil/%.[Ff]
	$(FC) $(F77_FLAGS) $(F_FLAGS_ADD) $(FC_FLAG_MOD)$(DIR_INSTL)/bld/CCTM/adjutil/mod -c $< -o $@
