source: LMDZ6/trunk/libf/phylmdiso/cospv2/cosp.F90 @ 3927

Last change on this file since 3927 was 3927, checked in by Laurent Fairhead, 3 years ago

Initial import of the physics wih isotopes from Camille Risi
CR

File size: 215.5 KB
Line 
1! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2! Copyright (c) 2015, Regents of the University of Colorado
3! All rights reserved.
4!
5! Redistribution and use in source and binary forms, with or without modification, are
6! permitted provided that the following conditions are met:
7!
8! 1. Redistributions of source code must retain the above copyright notice, this list of
9!    conditions and the following disclaimer.
10!
11! 2. Redistributions in binary form must reproduce the above copyright notice, this list
12!    of conditions and the following disclaimer in the documentation and/or other
13!    materials provided with the distribution.
14!
15! 3. Neither the name of the copyright holder nor the names of its contributors may be
16!    used to endorse or promote products derived from this software without specific prior
17!    written permission.
18!
19! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20! EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21! MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22! THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24! OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27! OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28!
29! History:
30! May 2015- D. Swales - Original version
31! Mar 2018- R. Guzman - Added OPAQ diagnostics and GLID simulator
32! Apr 2018- R. Guzman - Added ATLID simulator
33!
34! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35
36
37MODULE MOD_COSP
38  USE COSP_KINDS,                  ONLY: wp
39  USE MOD_COSP_CONFIG,             ONLY: R_UNDEF,PARASOL_NREFL,LIDAR_NCAT,LIDAR_NTYPE, SR_BINS,&
40                                         N_HYDRO,RTTOV_MAX_CHANNELS,numMISRHgtBins,      &
41                                         cloudsat_DBZE_BINS,LIDAR_NTEMP,calipso_histBsct,&
42                                         use_vgrid,Nlvgrid,vgrid_zu,vgrid_zl,vgrid_z,    &
43                                         numMODISTauBins,numMODISPresBins,               &
44                                         numMODISReffIceBins,numMODISReffLiqBins,        &
45                                         numISCCPTauBins,numISCCPPresBins,numMISRTauBins,&
46                                         ntau,modis_histTau,tau_binBounds,               &
47                                         modis_histTauEdges,tau_binEdges,nCloudsatPrecipClass,&
48                                         modis_histTauCenters,tau_binCenters,            &
49!                                         cloudsat_preclvl,grLidar532_histBsct,atlid_histBsct !PREC_BUG
50                                         grLidar532_histBsct,atlid_histBsct                               
51  USE MOD_COSP_MODIS_INTERFACE,      ONLY: cosp_modis_init,       modis_IN
52!  USE MOD_COSP_RTTOV_INTERFACE,      ONLY: cosp_rttov_init,       rttov_IN
53  USE MOD_COSP_MISR_INTERFACE,       ONLY: cosp_misr_init,        misr_IN
54  USE MOD_COSP_ISCCP_INTERFACE,      ONLY: cosp_isccp_init,       isccp_IN
55  USE MOD_COSP_CALIPSO_INTERFACE,    ONLY: cosp_calipso_init,     calipso_IN
56  USE MOD_COSP_ATLID_INTERFACE,      ONLY: cosp_atlid_init,       atlid_IN
57  USE MOD_COSP_GRLIDAR532_INTERFACE, ONLY: cosp_grLidar532_init, grLidar532_IN
58  USE MOD_COSP_PARASOL_INTERFACE,    ONLY: cosp_parasol_init,     parasol_in
59  USE MOD_COSP_CLOUDSAT_INTERFACE,   ONLY: cosp_cloudsat_init,    cloudsat_IN
60  USE quickbeam,                     ONLY: quickbeam_subcolumn,   quickbeam_column, radar_cfg
61  USE MOD_ICARUS,                    ONLY: icarus_subcolumn,      icarus_column
62  USE MOD_MISR_SIMULATOR,            ONLY: misr_subcolumn,        misr_column
63  USE MOD_LIDAR_SIMULATOR,           ONLY: lidar_subcolumn,       lidar_column
64  USE MOD_MODIS_SIM,                 ONLY: modis_subcolumn,       modis_column
65  USE MOD_PARASOL,                   ONLY: parasol_subcolumn,     parasol_column
66!  use mod_cosp_rttov,                ONLY: rttov_column
67  USE MOD_COSP_STATS,                ONLY: COSP_LIDAR_ONLY_CLOUD,COSP_CHANGE_VERTICAL_GRID
68
69  IMPLICIT NONE
70
71  logical :: linitialization ! Initialization flag
72
73  ! ######################################################################################
74  ! TYPE cosp_column_inputs
75  ! ######################################################################################
76  type cosp_column_inputs
77     integer :: &
78          Npoints,             & ! Number of gridpoints.
79          Ncolumns,            & ! Number of columns.
80          Nlevels                ! Number of levels.
81
82     integer,allocatable,dimension(:) :: &
83          sunlit                 ! Sunlit flag                            (0-1)
84
85     real(wp),allocatable,dimension(:,:) :: &
86          at,                  & ! Temperature                            (K)
87          pfull,               & ! Pressure                               (Pa)
88          phalf,               & ! Pressure at half-levels                (Pa)
89          qv,                  & ! Specific humidity                      (kg/kg)
90          hgt_matrix,          & ! Height of hydrometeors                 (km)
91          hgt_matrix_half        ! Height of hydrometeors at half levels  (km)
92
93     real(wp),allocatable,dimension(:) :: &
94          land,                & ! Land/Sea mask                          (0-1)
95          skt,                 & ! Surface temperature                    (K)
96          surfelev               ! Surface Elevation                      (m)
97     ! Fields used ONLY by RTTOV
98     integer :: &
99          month                  ! Month for surface emissivty atlas      (1-12)
100     real(wp) :: &
101          zenang,              & ! Satellite zenith angle for RTTOV       (deg)
102          co2,                 & ! CO2                                    (kg/kg)
103          ch4,                 & ! Methane                                (kg/kg)
104          n2o,                 & ! N2O                                    (kg/kg)
105          co                     ! CO                                     (kg/kg)
106     real(wp),allocatable,dimension(:) :: &
107          emis_sfc,            & ! Surface emissivity                     (1)
108          u_sfc,               & ! Surface u-wind                         (m/s)
109          v_sfc,               & ! Surface v-wind                         (m/s)
110          seaice,              & ! Sea-ice fraction                       (0-1)
111          lat,                 & ! Latitude                              (deg)
112          lon                    ! Longitude                              (deg)
113     real(wp),allocatable,dimension(:,:) :: &
114          o3,                  & ! Ozone                                  (kg/kg)
115          tca,                 & ! Total column cloud fraction            (0-1)
116          cloudIce,            & ! Cloud ice water mixing ratio           (kg/kg)
117          cloudLiq,            & ! Cloud liquid water mixing ratio        (kg/kg)
118          fl_rain,             & ! Precipitation (rain) flux              (kg/m2/s)
119          fl_snow                ! Precipitation (snow) flux              (kg/m2/s)
120  end type cosp_column_inputs
121
122  ! ######################################################################################
123  ! TYPE cosp_optical_inputs
124  ! ######################################################################################
125  type cosp_optical_inputs
126     integer :: &
127          Npoints,             & ! Number of gridpoints.
128          Ncolumns,            & ! Number of columns.
129          Nlevels,             & ! Number of levels.
130          Npart,               & ! Number of cloud meteors for LIDAR simulators.
131          Nrefl                  ! Number of reflectances for PARASOL simulator
132     real(wp) :: &
133          emsfc_lw               ! Surface emissivity @ 11micron
134     real(wp),allocatable,dimension(:,:,:) :: &
135          frac_out,            & ! Cloud fraction
136          tau_067,             & ! Optical depth @ 0.67micron
137          emiss_11,            & ! Emissivity @ 11 micron
138          fracLiq,             & ! Fraction of optical-depth due to liquid (MODIS)
139          asym,                & ! Assymetry parameter @ 3.7micron (MODIS)
140          ss_alb,              & ! Single-scattering albedo @ 3.7micron (MODIS)
141          betatot_calipso,     & ! Lidar backscatter coefficient (calipso @ 532nm)
142          betatot_grLidar532,  & ! Lidar backscatter coefficient (ground-lidar @ 532nm)
143          betatot_atlid,       & ! Lidar backscatter coefficient (atlid @ 355nm)
144          betatot_ice_calipso, & ! Lidar backscatter coefficient ICE (calipso @ 532nm)
145          betatot_liq_calipso, & ! Lidar backscatter coefficient LIQUID (calipso @ 532nm)
146          tautot_calipso,      & ! Lidar Optical thickness (calipso @ 532nm)
147          tautot_grLidar532,   & ! Lidar Optical thickness (ground-lidar @ 532nm) 
148          tautot_atlid,        & ! Lidar Optical thickness (atlid @ 355nm)
149          tautot_ice_calipso,  & ! Lidar Ice Optical thickness (calipso @ 532nm)
150          tautot_liq_calipso,  & ! Lidar Liquid Optical thickness (calipso @ 532nm)
151          z_vol_cloudsat,      & ! Effective reflectivity factor (mm^6/m^3)
152          kr_vol_cloudsat,     & ! Attenuation coefficient hydro (dB/km)
153          g_vol_cloudsat         ! Attenuation coefficient gases (dB/km)
154     real(wp),allocatable,dimension(:,:) :: &
155          beta_mol_calipso,    & ! Lidar molecular backscatter coefficient (calipso @ 532nm)
156          beta_mol_grLidar532, & ! Lidar molecular backscatter coefficient (ground-lidar @ 532nm)
157          beta_mol_atlid,      & ! Lidar molecular backscatter coefficient (atlid @ 355nm)
158          tau_mol_calipso,     & ! Lidar molecular optical depth (calipso @ 532nm)
159          tau_mol_grLidar532,  & ! Lidar molecular optical depth (ground-lidar @ 532nm)
160          tau_mol_atlid,       & ! Lidar molecular optical depth (atlid @ 355nm)
161          tautot_S_liq,        & ! Parasol Liquid water optical thickness, from TOA to SFC
162          tautot_S_ice,        & ! Parasol Ice water optical thickness, from TOA to SFC
163          fracPrecipIce          ! Fraction of precipitation which is frozen (1).
164     type(radar_cfg) :: &
165          rcfg_cloudsat          ! Radar configuration information (CLOUDSAT)
166  end type cosp_optical_inputs
167
168  ! ######################################################################################
169  ! TYPE cosp_outputs
170  ! ######################################################################################
171  type cosp_outputs
172
173     ! CALIPSO outputs
174     real(wp),dimension(:,:,:),pointer :: &
175          calipso_betaperp_tot => null(),  & ! Total backscattered signal
176          calipso_beta_tot => null(),      & ! Total backscattered signal
177          calipso_tau_tot => null(),       & ! Optical thickness integrated from top to level z
178          calipso_lidarcldphase => null(), & ! 3D "lidar" phase cloud fraction
179          calipso_lidarcldtype => null(),  & ! 3D "lidar" OPAQ type fraction
180          calipso_cldlayerphase => null(), & ! low, mid, high-level lidar phase cloud cover
181          calipso_lidarcldtmp => null(),   & ! 3D "lidar" phase cloud temperature
182          calipso_cfad_sr => null()          ! CFAD of scattering ratio
183     real(wp), dimension(:,:),pointer :: &
184          calipso_lidarcld => null(),      & ! 3D "lidar" cloud fraction
185          calipso_cldlayer => null(),      & ! low, mid, high-level, total lidar cloud cover
186          calipso_cldtype => null(),       & ! opaque and thin lidar cloud cover + z_opaque altitude
187          calipso_cldtypetemp => null(),   & ! opaque and thin cloud temperature 
188          calipso_cldtypemeanz => null(),  & ! opaque and thin cloud altitude
189          calipso_cldtypemeanzse => null(),& ! same as just above with respect to SE
190          calipso_beta_mol => null(),      & ! Molecular backscatter
191          calipso_temp_tot => null()
192     real(wp), dimension(:),pointer :: &
193          calipso_cldthinemis => null(),   & ! thin cloud emissivity
194          calipso_srbval => null()           ! SR bins in cfad_sr
195
196     ! GROUND LIDAR outputs
197     real(wp),dimension(:,:,:),pointer :: & 
198          grLidar532_beta_tot => null(),   & ! Total GROUND LIDAR backscattered signal
199          grLidar532_cfad_sr => null()       ! CFAD of GROUND LIDAR scattering ratio
200     real(wp), dimension(:,:),pointer :: & 
201          grLidar532_lidarcld => null(),   & ! 3D GROUND "lidar" cloud fraction 
202          grLidar532_cldlayer => null(),   & ! low, mid, high-level, total GROUND lidar cloud cover
203          grLidar532_beta_mol => null()      ! GROUND LIDAR Molecular backscatter
204     real(wp), dimension(:),pointer :: & 
205          grLidar532_srbval => null()        ! SR bins in cfad_sr
206
207     ! ATLID outputs
208     real(wp),dimension(:,:,:),pointer :: &
209          atlid_beta_tot => null(),   & ! Total ATLID backscattered signal
210          atlid_cfad_sr => null()       ! CFAD of ATLID scattering ratio
211     real(wp), dimension(:,:),pointer :: &
212          atlid_lidarcld => null(),   & ! 3D ATLID cloud fraction
213          atlid_cldlayer => null(),   & ! low, mid, high-level, total ATLID cloud cover
214          atlid_beta_mol => null()      ! ATLID Molecular backscatter
215     real(wp), dimension(:),pointer :: &
216          atlid_srbval => null()        ! SR bins in cfad_sr
217
218
219     ! PARASOL outputs
220     real(wp),dimension(:,:,:),pointer :: &
221          parasolPix_refl => null()            ! PARASOL reflectances (subcolumn)
222     real(wp),dimension(:,:),pointer :: &
223          parasolGrid_refl => null()           ! PARASOOL reflectances (column)
224
225     ! CLOUDSAT outputs
226     real(wp),dimension(:,:,:),pointer :: &
227          cloudsat_Ze_tot => null(),         & ! Effective reflectivity factor (Npoints,Ncolumns,Nlevels)
228          cloudsat_cfad_ze => null()           ! Ze CFAD(Npoints,dBZe_bins,Nlevels)
229     real(wp), dimension(:,:),pointer :: &
230          lidar_only_freq_cloud => null(),   & ! (Npoints,Nlevels)
231          cloudsat_precip_cover => null()      ! Radar total cloud amount by CloudSat precip flag (Npoints,dBZe_bins)
232     real(wp),dimension(:),pointer :: &
233          cloudsat_tcc => null(),             &
234          cloudsat_tcc2 => null(),            &         
235          radar_lidar_tcc => null(),         & ! Radar&lidar total cloud amount, grid-box scale (Npoints)
236          cloudsat_pia => null()               ! Radar path integrated attenuation (Npoints)
237         
238     ! ISCCP outputs       
239     real(wp),dimension(:),pointer :: &
240          isccp_totalcldarea => null(), & ! The fraction of model grid box columns with cloud
241                                          ! somewhere in them. (%)
242          isccp_meantb => null(),       & ! Mean all-sky 10.5 micron brightness temperature. (K)
243          isccp_meantbclr => null(),    & ! Mean clear-sky 10.5 micron brightness temperature. (K)
244          isccp_meanptop => null(),     & ! Mean cloud top pressure (mb).
245          isccp_meantaucld => null(),   & ! Mean optical thickness. (1)
246          isccp_meanalbedocld => null()   ! Mean cloud albedo. (1)
247     real(wp),dimension(:,:),pointer ::&
248          isccp_boxtau => null(),       & ! Optical thickness in each column. (1)
249          isccp_boxptop => null()         ! Cloud top pressure in each column. (mb)
250     real(wp),dimension(:,:,:),pointer :: &
251          isccp_fq  => null()             ! The fraction of the model grid box covered by each of
252                                          ! the 49 ISCCP D level cloud types. (%)
253
254     ! MISR outptus
255     real(wp),dimension(:,:,:),pointer ::   & !
256          misr_fq => null()          ! Fraction of the model grid box covered by each of the MISR
257                           ! cloud types
258     real(wp),dimension(:,:),pointer ::   & !
259          misr_dist_model_layertops => null() !
260     real(wp),dimension(:),pointer ::   & !
261          misr_meanztop => null(), & ! Mean MISR cloud top height
262          misr_cldarea => null()     ! Mean MISR cloud cover area
263
264     ! MODIS outptus
265     real(wp),pointer,dimension(:) ::      & !
266          modis_Cloud_Fraction_Total_Mean => null(),       & ! L3 MODIS retrieved cloud fraction (total)
267          modis_Cloud_Fraction_Water_Mean => null(),       & ! L3 MODIS retrieved cloud fraction (liq)
268          modis_Cloud_Fraction_Ice_Mean => null(),         & ! L3 MODIS retrieved cloud fraction (ice)
269          modis_Cloud_Fraction_High_Mean => null(),        & ! L3 MODIS retrieved cloud fraction (high)
270          modis_Cloud_Fraction_Mid_Mean => null(),         & ! L3 MODIS retrieved cloud fraction (middle)
271          modis_Cloud_Fraction_Low_Mean => null(),         & ! L3 MODIS retrieved cloud fraction (low )
272          modis_Optical_Thickness_Total_Mean => null(),    & ! L3 MODIS retrieved optical thickness (tot)
273          modis_Optical_Thickness_Water_Mean => null(),    & ! L3 MODIS retrieved optical thickness (liq)
274          modis_Optical_Thickness_Ice_Mean => null(),      & ! L3 MODIS retrieved optical thickness (ice)
275          modis_Optical_Thickness_Total_LogMean => null(), & ! L3 MODIS retrieved log10 optical thickness
276          modis_Optical_Thickness_Water_LogMean => null(), & ! L3 MODIS retrieved log10 optical thickness
277          modis_Optical_Thickness_Ice_LogMean => null(),   & ! L3 MODIS retrieved log10 optical thickness
278          modis_Cloud_Particle_Size_Water_Mean => null(),  & ! L3 MODIS retrieved particle size (liquid)
279          modis_Cloud_Particle_Size_Ice_Mean => null(),    & ! L3 MODIS retrieved particle size (ice)
280          modis_Cloud_Top_Pressure_Total_Mean => null(),   & ! L3 MODIS retrieved cloud top pressure
281          modis_Liquid_Water_Path_Mean => null(),          & ! L3 MODIS retrieved liquid water path
282          modis_Ice_Water_Path_Mean => null()                ! L3 MODIS retrieved ice water path
283     real(wp),pointer,dimension(:,:,:) ::  &
284          modis_Optical_Thickness_vs_Cloud_Top_Pressure => null(), & ! Tau/Pressure joint histogram
285          modis_Optical_Thickness_vs_ReffICE => null(),            & ! Tau/ReffICE joint histogram
286          modis_Optical_Thickness_vs_ReffLIQ => null()               ! Tau/ReffLIQ joint histogram
287
288     ! RTTOV outputs
289     real(wp),pointer :: &
290          rttov_tbs(:,:) => null() ! Brightness Temperature
291
292  end type cosp_outputs
293
294CONTAINS
295  ! ######################################################################################
296  ! FUNCTION cosp_simulator
297  ! ######################################################################################
298  function COSP_SIMULATOR(cospIN,cospgridIN,cospOUT,start_idx,stop_idx,debug)
299    type(cosp_optical_inputs),intent(in),target :: cospIN     ! Optical inputs to COSP simulator
300    type(cosp_column_inputs), intent(in),target :: cospgridIN ! Host model inputs to COSP
301
302    ! Inputs into the simulators
303    type(isccp_IN)    :: isccpIN    ! Input to the ISCCP simulator
304    type(misr_IN)     :: misrIN     ! Input to the LIDAR simulator
305    type(calipso_IN)  :: calipsoIN  ! Input to the LIDAR simulator
306    type(grLidar532_IN) :: grLidar532IN ! Input to the GROUND LIDAR simulator
307    type(atlid_IN)    :: atlidIN    ! Input to the ATLID simulator
308    type(parasol_IN)  :: parasolIN  ! Input to the PARASOL simulator
309    type(cloudsat_IN) :: cloudsatIN ! Input to the CLOUDSAT radar simulator
310    type(modis_IN)    :: modisIN    ! Input to the MODIS simulator
311!    type(rttov_IN)    :: rttovIN    ! Input to the RTTOV simulator
312    integer,optional  :: start_idx,stop_idx
313    logical,optional  :: debug
314
315    ! Outputs from the simulators (nested simulator output structure)
316    type(cosp_outputs), intent(inout) :: cospOUT
317    character(len=256),dimension(100) :: cosp_simulator
318
319    ! Local variables
320    integer :: &
321         i,icol,ij,ik,nError
322    integer,target :: &
323         Npoints
324    logical :: &
325         Lisccp_subcolumn,     & ! On/Off switch for subcolumn ISCCP simulator
326         Lmisr_subcolumn,      & ! On/Off switch for subcolumn MISR simulator
327         Lcalipso_subcolumn,   & ! On/Off switch for subcolumn CALIPSO simulator
328         LgrLidar532_subcolumn,& ! On/Off switch for subcolumn GROUND LIDAR simulator
329         Latlid_subcolumn,     & ! On/Off switch for subcolumn ATLID simulator
330         Lparasol_subcolumn,   & ! On/Off switch for subcolumn PARASOL simulator
331         Lcloudsat_subcolumn,  & ! On/Off switch for subcolumn CLOUDSAT simulator
332         Lmodis_subcolumn,     & ! On/Off switch for subcolumn MODIS simulator
333         Lrttov_subcolumn,     & ! On/Off switch for subcolumn RTTOV simulator
334         Lisccp_column,        & ! On/Off switch for column ISCCP simulator
335         Lmisr_column,         & ! On/Off switch for column MISR simulator
336         Lcalipso_column,      & ! On/Off switch for column CALIPSO simulator
337         LgrLidar532_column,   & ! On/Off switch for column GROUND LIDAR simulator
338         Latlid_column,        & ! On/Off switch for column ATLID simulator
339         Lparasol_column,      & ! On/Off switch for column PARASOL simulator
340         Lcloudsat_column,     & ! On/Off switch for column CLOUDSAT simulator
341         Lmodis_column,        & ! On/Off switch for column MODIS simulator
342         Lrttov_column,        & ! On/Off switch for column RTTOV simulator (not used)
343         Lradar_lidar_tcc,     & ! On/Off switch from joint Calipso/Cloudsat product
344         Lcloudsat_tcc,       & !
345         Lcloudsat_tcc2,      & !         
346         Llidar_only_freq_cloud  ! On/Off switch from joint Calipso/Cloudsat product
347    logical :: &
348         ok_lidar_cfad    = .false., &
349         ok_lidar_cfad_grLidar532 = .false., &
350         ok_lidar_cfad_atlid = .false., &
351         lrttov_cleanUp   = .false.
352   
353    integer, dimension(:,:),allocatable  :: &
354         modisRetrievedPhase,isccpLEVMATCH
355    real(wp), dimension(:),  allocatable  :: &
356         modisCfTotal,modisCfLiquid,modisMeanIceWaterPath, isccp_meantbclr,     &
357         modisCfIce, modisCfHigh, modisCfMid, modisCfLow,modisMeanTauTotal,     &
358         modisMeanTauLiquid, modisMeanTauIce, modisMeanLogTauTotal,             &
359         modisMeanLogTauLiquid, modisMeanLogTauIce, modisMeanSizeLiquid,        &
360         modisMeanSizeIce, modisMeanCloudTopPressure, modisMeanLiquidWaterPath, &
361         radar_lidar_tcc, cloudsat_tcc, cloudsat_tcc2
362    REAL(WP), dimension(:,:),allocatable  :: &
363         modisRetrievedCloudTopPressure,modisRetrievedTau,modisRetrievedSize,   &
364         misr_boxtau,misr_boxztop,misr_dist_model_layertops,isccp_boxtau,       &
365         isccp_boxttop,isccp_boxptop,calipso_beta_mol,lidar_only_freq_cloud,    &
366         grLidar532_beta_mol,atlid_beta_mol
367    REAL(WP), dimension(:,:,:),allocatable :: &
368         modisJointHistogram,modisJointHistogramIce,modisJointHistogramLiq,     &
369         calipso_beta_tot,calipso_betaperp_tot, cloudsatDBZe,parasolPix_refl,   &
370         grLidar532_beta_tot,atlid_beta_tot,cloudsatZe_non
371    real(wp),dimension(:),allocatable,target :: &
372         out1D_1,out1D_2,out1D_3,out1D_4,out1D_5,out1D_6,out1D_7,out1D_8,       &
373         out1D_9,out1D_10,out1D_11,out1D_12
374    real(wp),dimension(:,:,:),allocatable :: &
375       betamol_in,betamoli,pnormi,ze_toti,ze_noni
376
377    ! Initialize error reporting for output
378    cosp_simulator(:)=''
379
380    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381    ! 1) Determine if using full inputs or subset
382    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383    if (present(start_idx) .and. present(stop_idx)) then
384       ij=start_idx
385       ik=stop_idx
386    else
387       ij=1
388       ik=cospIN%Npoints
389    endif
390    Npoints = ik-ij+1
391
392    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393    ! 2a) Determine which simulators to run and which statistics to compute
394    !    - If any of the subcolumn fields are allocated, then run the subcolumn simulators.
395    !    - If any of the column fields are allocated, then compute the statistics for that
396    !      simulator, but only save the requested fields.
397    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398    ! Start with all simulators and joint-diagnostics off
399    Lisccp_subcolumn    = .false.
400    Lmisr_subcolumn     = .false.
401    Lcalipso_subcolumn  = .false.
402    LgrLidar532_subcolumn = .false.
403    Latlid_subcolumn    = .false.
404    Lparasol_subcolumn  = .false.
405    Lcloudsat_subcolumn = .false.
406    Lmodis_subcolumn    = .false.
407    Lrttov_subcolumn    = .false.
408    Lisccp_column       = .false.
409    Lmisr_column        = .false.
410    Lcalipso_column     = .false.
411    LgrLidar532_column = .false.
412    Latlid_column       = .false.
413    Lparasol_column     = .false.
414    Lcloudsat_column    = .false.
415    Lmodis_column       = .false.
416    Lrttov_column       = .false.
417    Lradar_lidar_tcc    = .false.
418    Llidar_only_freq_cloud = .false.
419    Lcloudsat_tcc       = .false.
420    Lcloudsat_tcc2      = .false.
421
422    ! CLOUDSAT subcolumn
423    if (associated(cospOUT%cloudsat_Ze_tot)) Lcloudsat_subcolumn = .true.
424
425    ! MODIS subcolumn
426    if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean)                .or.          &
427        associated(cospOUT%modis_Cloud_Fraction_Total_Mean)                .or.          &
428        associated(cospOUT%modis_Cloud_Fraction_Ice_Mean)                  .or.          &
429        associated(cospOUT%modis_Cloud_Fraction_High_Mean)                 .or.          &
430        associated(cospOUT%modis_Cloud_Fraction_Mid_Mean)                  .or.          &
431        associated(cospOUT%modis_Cloud_Fraction_Low_Mean)                  .or.          &
432        associated(cospOUT%modis_Optical_Thickness_Total_Mean)             .or.          &
433        associated(cospOUT%modis_Optical_Thickness_Water_Mean)             .or.          &
434        associated(cospOUT%modis_Optical_Thickness_Ice_Mean)               .or.          &
435        associated(cospOUT%modis_Optical_Thickness_Total_LogMean)          .or.          &
436        associated(cospOUT%modis_Optical_Thickness_Water_LogMean)          .or.          &
437        associated(cospOUT%modis_Optical_Thickness_Ice_LogMean)            .or.          &
438        associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean)           .or.          &
439        associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean)             .or.          &
440        associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean)            .or.          &
441        associated(cospOUT%modis_Liquid_Water_Path_Mean)                   .or.          &
442        associated(cospOUT%modis_Ice_Water_Path_Mean)                      .or.          &
443        associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure))               &
444       Lmodis_subcolumn    = .true.
445
446    ! ISCCP subcolumn
447    if (associated(cospOUT%isccp_boxtau)                                   .or.          &
448        associated(cospOUT%isccp_boxptop))                                               &
449       Lisccp_subcolumn    = .true.
450
451    ! MISR subcolumn
452    if (associated(cospOUT%misr_dist_model_layertops))                                   &
453       Lmisr_subcolumn     = .true.
454
455    ! CALIPOSO subcolumn
456    if (associated(cospOUT%calipso_tau_tot)                                .or.          &
457        associated(cospOUT%calipso_beta_mol)                               .or.          &
458        associated(cospOUT%calipso_temp_tot)                               .or.          &
459        associated(cospOUT%calipso_betaperp_tot)                           .or.          &
460        associated(cospOUT%calipso_beta_tot))                                            &
461       Lcalipso_subcolumn  = .true.
462
463    ! GROUND LIDAR subcolumn
464    if (associated(cospOUT%grLidar532_beta_mol)                           .or.          &
465        associated(cospOUT%grLidar532_beta_tot))                                        &
466       LgrLidar532_subcolumn  = .true.
467
468    ! ATLID subcolumn
469    if (associated(cospOUT%atlid_beta_mol)                                 .or.          &
470        associated(cospOUT%atlid_beta_tot))                                              &
471       Latlid_subcolumn  = .true. 
472
473    ! PARASOL subcolumn
474    if (associated(cospOUT%parasolPix_refl))                                             &
475       Lparasol_subcolumn  = .true.
476
477    ! RTTOV column
478    if (associated(cospOUT%rttov_tbs))                                                   &
479       Lrttov_column    = .true.
480
481    ! Set flag to deallocate rttov types (only done on final call to simulator)
482    if (size(cospOUT%isccp_meantb) .eq. stop_idx) lrttov_cleanUp = .true.
483
484    ! ISCCP column
485    if (associated(cospOUT%isccp_fq)                                       .or.          &
486        associated(cospOUT%isccp_meanalbedocld)                            .or.          &
487        associated(cospOUT%isccp_meanptop)                                 .or.          &
488        associated(cospOUT%isccp_meantaucld)                               .or.          &
489        associated(cospOUT%isccp_totalcldarea)                             .or.          &
490        associated(cospOUT%isccp_meantb)) then
491       Lisccp_column    = .true.
492       Lisccp_subcolumn = .true.
493    endif
494
495    ! MISR column
496    if (associated(cospOUT%misr_cldarea)                                   .or.          &
497        associated(cospOUT%misr_meanztop)                                  .or.          &
498        associated(cospOUT%misr_fq)) then
499       Lmisr_column    = .true.
500       Lmisr_subcolumn = .true.
501    endif
502
503    ! CALIPSO column
504    if (associated(cospOUT%calipso_cfad_sr)                                .or.          &
505        associated(cospOUT%calipso_lidarcld)                               .or.          &
506        associated(cospOUT%calipso_lidarcldphase)                          .or.          &
507        associated(cospOUT%calipso_lidarcldtype)                           .or.          &
508        associated(cospOUT%calipso_cldlayer)                               .or.          &
509        associated(cospOUT%calipso_cldtype)                                .or.          &
510        associated(cospOUT%calipso_cldtypetemp)                            .or.          &
511        associated(cospOUT%calipso_cldtypemeanz)                           .or.          &
512        associated(cospOUT%calipso_cldtypemeanzse)                         .or.          &
513        associated(cospOUT%calipso_cldthinemis)                            .or.          &
514        associated(cospOUT%calipso_cldlayerphase)                          .or.          &
515        associated(cospOUT%calipso_lidarcldtmp)) then
516       Lcalipso_column    = .true.
517       Lcalipso_subcolumn = .true.
518    endif
519
520    ! GROUND LIDAR column
521    if (associated(cospOUT%grLidar532_cfad_sr)                            .or.          &
522        associated(cospOUT%grLidar532_lidarcld)                           .or.          &
523        associated(cospOUT%grLidar532_cldlayer)) then
524       LgrLidar532_column    = .true.
525       LgrLidar532_subcolumn = .true.
526    endif
527
528    ! ATLID column
529    if (associated(cospOUT%atlid_cfad_sr)                                  .or.          &
530        associated(cospOUT%atlid_lidarcld)                                 .or.          &
531        associated(cospOUT%atlid_cldlayer)) then 
532       Latlid_column    = .true.
533       Latlid_subcolumn = .true.
534    endif
535
536    ! PARASOL column
537    if (associated(cospOUT%parasolGrid_refl)) then
538       Lparasol_column    = .true.
539       Lparasol_subcolumn = .true.
540    endif
541
542    ! CLOUDSAT column
543    if (associated(cospOUT%cloudsat_cfad_ze)) then
544       Lcloudsat_column    = .true.
545       Lcloudsat_subcolumn = .true.
546    endif
547
548    ! MODIS column
549    if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean)                .or.          &
550        associated(cospOUT%modis_Cloud_Fraction_Water_Mean)                .or.          &
551        associated(cospOUT%modis_Cloud_Fraction_Ice_Mean)                  .or.          &
552        associated(cospOUT%modis_Cloud_Fraction_High_Mean)                 .or.          &
553        associated(cospOUT%modis_Cloud_Fraction_Mid_Mean)                  .or.          &
554        associated(cospOUT%modis_Cloud_Fraction_Low_Mean)                  .or.          &
555        associated(cospOUT%modis_Optical_Thickness_Total_Mean)             .or.          &
556        associated(cospOUT%modis_Optical_Thickness_Water_Mean)             .or.          &
557        associated(cospOUT%modis_Optical_Thickness_Ice_Mean)               .or.          &
558        associated(cospOUT%modis_Optical_Thickness_Total_LogMean)          .or.          &
559        associated(cospOUT%modis_Optical_Thickness_Water_LogMean)          .or.          &
560        associated(cospOUT%modis_Optical_Thickness_Ice_LogMean)            .or.          &
561        associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean)           .or.          &
562        associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean)             .or.          &
563        associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean)            .or.          &
564        associated(cospOUT%modis_Liquid_Water_Path_Mean)                   .or.          &
565        associated(cospOUT%modis_Ice_Water_Path_Mean)                      .or.          &
566        associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure)) then
567       Lmodis_column    = .true.
568       Lmodis_subcolumn = .true.
569    endif
570
571    ! Joint simulator products
572    if (associated(cospOUT%lidar_only_freq_cloud) .or. associated(cospOUT%radar_lidar_tcc) .or. &
573        associated(cospOUT%cloudsat_tcc) .or. associated(cospOUT%cloudsat_tcc2)) then
574       Lcalipso_column     = .true.
575       Lcalipso_subcolumn  = .true.
576       Lcloudsat_column    = .true.
577       Lcloudsat_subcolumn = .true.
578       Lradar_lidar_tcc    = .true.
579       Llidar_only_freq_cloud = .true.
580       Lcloudsat_tcc       = .true.
581       Lcloudsat_tcc2      = .true.
582    endif
583
584    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585    ! 2b) Error Checking
586    !     Enforce bounds on input fields. If input field is out-of-bounds, report error
587    !     and turn off simulator
588    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
589    call cosp_errorCheck(cospgridIN, cospIN, Lisccp_subcolumn, Lisccp_column,            &
590         Lmisr_subcolumn, Lmisr_column, Lmodis_subcolumn, Lmodis_column,                 &
591         Lcloudsat_subcolumn, Lcloudsat_column, Lcalipso_subcolumn, Lcalipso_column,     &
592         Latlid_subcolumn, Latlid_column, LgrLidar532_subcolumn, LgrLidar532_column,     &
593         Lrttov_subcolumn, Lrttov_column, Lparasol_subcolumn, Lparasol_column,           &
594         Lradar_lidar_tcc, Llidar_only_freq_cloud, Lcloudsat_tcc,Lcloudsat_tcc2, cospOUT,&
595         cosp_simulator, nError)
596
597    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598    ! 3) Populate instrument simulator inputs
599    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
600    if (Lisccp_subcolumn .or. Lmodis_subcolumn) then
601       isccpIN%Npoints  => Npoints
602       isccpIN%Ncolumns => cospIN%Ncolumns
603       isccpIN%Nlevels  => cospIN%Nlevels
604       isccpIN%emsfc_lw => cospIN%emsfc_lw
605       isccpIN%skt      => cospgridIN%skt
606       isccpIN%qv       => cospgridIN%qv
607       isccpIN%at       => cospgridIN%at
608       isccpIN%frac_out => cospIN%frac_out
609       isccpIN%dtau     => cospIN%tau_067
610       isccpIN%dem      => cospIN%emiss_11
611       isccpIN%phalf    => cospgridIN%phalf
612       isccpIN%sunlit   => cospgridIN%sunlit
613       isccpIN%pfull    => cospgridIN%pfull
614    endif
615
616    if (Lmisr_subcolumn) then
617       misrIN%Npoints  => Npoints
618       misrIN%Ncolumns => cospIN%Ncolumns
619       misrIN%Nlevels  => cospIN%Nlevels
620       misrIN%dtau     => cospIN%tau_067
621       misrIN%sunlit   => cospgridIN%sunlit
622       misrIN%zfull    => cospgridIN%hgt_matrix
623       misrIN%at       => cospgridIN%at
624    endif
625
626    if (Lcalipso_subcolumn) then
627       calipsoIN%Npoints     => Npoints
628       calipsoIN%Ncolumns    => cospIN%Ncolumns
629       calipsoIN%Nlevels     => cospIN%Nlevels
630       calipsoIN%beta_mol    => cospIN%beta_mol_calipso
631       calipsoIN%betatot     => cospIN%betatot_calipso
632       calipsoIN%betatot_liq => cospIN%betatot_liq_calipso
633       calipsoIN%betatot_ice => cospIN%betatot_ice_calipso
634       calipsoIN%tau_mol     => cospIN%tau_mol_calipso
635       calipsoIN%tautot      => cospIN%tautot_calipso
636       calipsoIN%tautot_liq  => cospIN%tautot_liq_calipso
637       calipsoIN%tautot_ice  => cospIN%tautot_ice_calipso
638    endif
639
640    if (LgrLidar532_subcolumn) then
641       grLidar532IN%Npoints  => Npoints
642       grLidar532IN%Ncolumns => cospIN%Ncolumns
643       grLidar532IN%Nlevels  => cospIN%Nlevels
644       grLidar532IN%beta_mol => cospIN%beta_mol_grLidar532
645       grLidar532IN%betatot  => cospIN%betatot_grLidar532
646       grLidar532IN%tau_mol  => cospIN%tau_mol_grLidar532
647       grLidar532IN%tautot   => cospIN%tautot_grLidar532 
648    endif
649   
650    if (Latlid_subcolumn) then
651       atlidIN%Npoints        => Npoints
652       atlidIN%Ncolumns       => cospIN%Ncolumns
653       atlidIN%Nlevels        => cospIN%Nlevels
654       atlidIN%beta_mol_atlid => cospIN%beta_mol_atlid
655       atlidIN%betatot_atlid  => cospIN%betatot_atlid
656       atlidIN%tau_mol_atlid  => cospIN%tau_mol_atlid
657       atlidIN%tautot_atlid   => cospIN%tautot_atlid
658    endif
659   
660    if (Lparasol_subcolumn) then
661       parasolIN%Npoints      => Npoints
662       parasolIN%Nlevels      => cospIN%Nlevels
663       parasolIN%Ncolumns     => cospIN%Ncolumns
664       parasolIN%Nrefl        => cospIN%Nrefl
665       parasolIN%tautot_S_liq => cospIN%tautot_S_liq
666       parasolIN%tautot_S_ice => cospIN%tautot_S_ice
667    endif
668
669    if (Lcloudsat_subcolumn) then
670       cloudsatIN%Npoints    => Npoints
671       cloudsatIN%Nlevels    => cospIN%Nlevels
672       cloudsatIN%Ncolumns   => cospIN%Ncolumns
673       cloudsatIN%z_vol      => cospIN%z_vol_cloudsat
674       cloudsatIN%kr_vol     => cospIN%kr_vol_cloudsat
675       cloudsatIN%g_vol      => cospIN%g_vol_cloudsat
676       cloudsatIN%rcfg       => cospIN%rcfg_cloudsat
677       cloudsatIN%hgt_matrix => cospgridIN%hgt_matrix
678    endif
679
680    if (Lmodis_subcolumn) then
681       modisIN%Ncolumns  => cospIN%Ncolumns
682       modisIN%Nlevels   => cospIN%Nlevels
683       modisIN%Npoints   => Npoints
684       modisIN%liqFrac   => cospIN%fracLiq
685       modisIN%tau       => cospIN%tau_067
686       modisIN%g         => cospIN%asym
687       modisIN%w0        => cospIN%ss_alb
688       modisIN%Nsunlit   = count(cospgridIN%sunlit > 0)
689       if (modisIN%Nsunlit .gt. 0) then
690          allocate(modisIN%sunlit(modisIN%Nsunlit),modisIN%pres(modisIN%Nsunlit,cospIN%Nlevels+1))
691          modisIN%sunlit    = pack((/ (i, i = 1, Npoints ) /),mask = cospgridIN%sunlit > 0)
692          modisIN%pres      = cospgridIN%phalf(int(modisIN%sunlit(:)),:)
693       endif
694       if (count(cospgridIN%sunlit <= 0) .gt. 0) then
695          allocate(modisIN%notSunlit(count(cospgridIN%sunlit <= 0)))
696          modisIN%notSunlit = pack((/ (i, i = 1, Npoints ) /),mask = .not. cospgridIN%sunlit > 0)
697       endif
698    endif
699
700!    if (Lrttov_column) then
701!       rttovIN%nPoints    => Npoints
702!       rttovIN%nLevels    => cospIN%nLevels
703!       rttovIN%nSubCols   => cospIN%nColumns
704!       rttovIN%zenang     => cospgridIN%zenang
705!       rttovIN%co2        => cospgridIN%co2
706!       rttovIN%ch4        => cospgridIN%ch4
707!       rttovIN%n2o        => cospgridIN%n2o
708!       rttovIN%co         => cospgridIN%co
709!       rttovIN%surfem     => cospgridIN%emis_sfc
710!       rttovIN%h_surf     => cospgridIN%hgt_matrix_half(:,cospIN%Nlevels+1)
711!       rttovIN%u_surf     => cospgridIN%u_sfc
712!       rttovIN%v_surf     => cospgridIN%v_sfc
713!       rttovIN%t_skin     => cospgridIN%skt
714!       rttovIN%p_surf     => cospgridIN%phalf(:,cospIN%Nlevels+1)
715!       rttovIN%q2m        => cospgridIN%qv(:,cospIN%Nlevels)
716!       rttovIN%t2m        => cospgridIN%at(:,cospIN%Nlevels)
717!       rttovIN%lsmask     => cospgridIN%land
718!       rttovIN%latitude   => cospgridIN%lat
719!       rttovIN%longitude  => cospgridIN%lon
720!       rttovIN%seaice     => cospgridIN%seaice
721!       rttovIN%p          => cospgridIN%pfull
722!       rttovIN%ph         => cospgridIN%phalf
723!       rttovIN%t          => cospgridIN%at
724!       rttovIN%q          => cospgridIN%qv
725!       rttovIN%o3         => cospgridIN%o3
726       ! Below only needed for all-sky RTTOV calculation
727!       rttovIN%month      => cospgridIN%month
728!       rttovIN%tca        => cospgridIN%tca
729!       rttovIN%cldIce     => cospgridIN%cloudIce
730!       rttovIN%cldLiq     => cospgridIN%cloudLiq
731!       rttovIN%fl_rain    => cospgridIN%fl_rain
732!       rttovIN%fl_snow    => cospgridIN%fl_snow
733!    endif
734
735    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736    ! 4) Call subcolumn simulators
737    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738
739    ! ISCCP (icarus) subcolumn simulator
740    if (Lisccp_subcolumn .or. Lmodis_subcolumn) then
741       ! Allocate space for local variables
742       allocate(isccpLEVMATCH(Npoints,isccpIN%Ncolumns),                                 &
743                isccp_boxttop(Npoints,isccpIN%Ncolumns),                                 &
744                isccp_boxptop(Npoints,isccpIN%Ncolumns),                                 &
745                isccp_boxtau(Npoints,isccpIN%Ncolumns), isccp_meantbclr(Npoints))
746       ! Call simulator
747       call icarus_subcolumn(isccpIN%npoints,isccpIN%ncolumns,isccpIN%nlevels,           &
748                             isccpIN%sunlit,isccpIN%dtau,isccpIN%dem,isccpIN%skt,        &
749                             isccpIN%emsfc_lw,isccpIN%qv,isccpIN%at,isccpIN%pfull,       &
750                             isccpIN%phalf,isccpIN%frac_out,isccpLEVMATCH,               &
751                             isccp_boxtau(:,:),isccp_boxptop(:,:),                       &
752                             isccp_boxttop(:,:),isccp_meantbclr(:))
753       ! Store output (if requested)
754       if (associated(cospOUT%isccp_boxtau)) then
755          cospOUT%isccp_boxtau(ij:ik,:)  = isccp_boxtau
756       endif
757       if (associated(cospOUT%isccp_boxptop)) then
758          cospOUT%isccp_boxptop(ij:ik,:) = isccp_boxptop
759       endif
760       if (associated(cospOUT%isccp_meantbclr)) then
761          cospOUT%isccp_meantbclr(ij:ik) = isccp_meantbclr
762       endif
763   endif
764
765   ! MISR subcolumn simulator
766    if (Lmisr_subcolumn) then
767       ! Allocate space for local variables
768       allocate(misr_boxztop(Npoints,misrIN%Ncolumns),                                   &
769                misr_boxtau(Npoints,misrIN%Ncolumns),                                    &
770                misr_dist_model_layertops(Npoints,numMISRHgtBins))
771       ! Call simulator
772       call misr_subcolumn(misrIN%Npoints,misrIN%Ncolumns,misrIN%Nlevels,misrIN%dtau,    &
773                           misrIN%zfull,misrIN%at,misrIN%sunlit,misr_boxtau,             &
774                           misr_dist_model_layertops,misr_boxztop)
775       ! Store output (if requested)
776       if (associated(cospOUT%misr_dist_model_layertops)) then
777          cospOUT%misr_dist_model_layertops(ij:ik,:) = misr_dist_model_layertops
778       endif
779    endif
780
781    ! Calipso subcolumn simulator
782    if (Lcalipso_subcolumn) then
783       ! Allocate space for local variables
784       allocate(calipso_beta_mol(calipsoIN%Npoints,calipsoIN%Nlevels),                   &
785                calipso_beta_tot(calipsoIN%Npoints,calipsoIN%Ncolumns,calipsoIN%Nlevels),&
786                calipso_betaperp_tot(calipsoIN%Npoints,calipsoIN%Ncolumns,calipsoIN%Nlevels))
787       ! Call simulator
788       call lidar_subcolumn(calipsoIN%npoints, calipsoIN%ncolumns, calipsoIN%nlevels, .false., &
789            calipsoIN%beta_mol, calipsoIN%tau_mol, calipsoIN%betatot, calipsoIN%tautot,  &
790            calipso_beta_mol(:,:), calipso_beta_tot(:,:,:), calipsoIN%betatot_ice,       &
791            calipsoIN%tautot_ice, calipsoIN%betatot_liq, calipsoIN%tautot_liq,           &
792            calipso_betaperp_tot(:,:,:))
793       ! Store output (if requested)
794       if (associated(cospOUT%calipso_beta_mol))                                         &
795            cospOUT%calipso_beta_mol(ij:ik,calipsoIN%Nlevels:1:-1) = calipso_beta_mol
796       if (associated(cospOUT%calipso_beta_tot))                                         &
797            cospOUT%calipso_beta_tot(ij:ik,:,calipsoIN%Nlevels:1:-1) = calipso_beta_tot
798       if (associated(cospOUT%calipso_betaperp_tot))                                     &
799            cospOUT%calipso_betaperp_tot(ij:ik,:,:) = calipso_betaperp_tot
800    endif
801
802    ! GROUND LIDAR subcolumn simulator
803    if (LgrLidar532_subcolumn) then 
804       ! Allocate space for local variables 
805       allocate(grLidar532_beta_mol(grLidar532IN%Npoints,grLidar532IN%Nlevels),       & 
806                grLidar532_beta_tot(grLidar532IN%Npoints,grLidar532IN%Ncolumns,grLidar532IN%Nlevels))
807       ! Call simulator 
808       call lidar_subcolumn(grLidar532IN%npoints, grLidar532IN%ncolumns, grLidar532IN%nlevels,&
809            .true., grLidar532IN%beta_mol, grLidar532IN%tau_mol, grLidar532IN%betatot,&
810            grLidar532IN%tautot, grLidar532_beta_mol(:,:), grLidar532_beta_tot(:,:,:))
811       ! Store output (if requested)
812       if (associated(cospOUT%grLidar532_beta_mol))                                      &
813            cospOUT%grLidar532_beta_mol(ij:ik,grLidar532IN%Nlevels:1:-1) = grLidar532_beta_mol
814       if (associated(cospOUT%grLidar532_beta_tot))                                         &
815            cospOUT%grLidar532_beta_tot(ij:ik,:,grLidar532IN%Nlevels:1:-1) = grLidar532_beta_tot
816    endif
817
818    ! ATLID subcolumn simulator
819    if (Latlid_subcolumn) then
820       ! Allocate space for local variables
821       allocate(atlid_beta_mol(atlidIN%Npoints,atlidIN%Nlevels),                      &
822                atlid_beta_tot(atlidIN%Npoints,atlidIN%Ncolumns,atlidIN%Nlevels))
823       ! Call simulator
824       call lidar_subcolumn(atlidIN%npoints, atlidIN%ncolumns, atlidIN%nlevels,&
825            .false., atlidIN%beta_mol_atlid, atlidIN%tau_mol_atlid, atlidIN%betatot_atlid,&
826            atlidIN%tautot_atlid, atlid_beta_mol(:,:), atlid_beta_tot(:,:,:))
827       ! Store output (if requested)
828       if (associated(cospOUT%atlid_beta_mol))                                        &
829            cospOUT%atlid_beta_mol(ij:ik,atlidIN%Nlevels:1:-1) = atlid_beta_mol
830       if (associated(cospOUT%atlid_beta_tot))                                        &
831            cospOUT%atlid_beta_tot(ij:ik,:,atlidIN%Nlevels:1:-1) = atlid_beta_tot   
832    endif
833
834    ! PARASOL subcolumn simulator
835    if (Lparasol_subcolumn) then
836       ! Allocate space for local variables
837       allocate(parasolPix_refl(parasolIN%Npoints,parasolIN%Ncolumns,PARASOL_NREFL))
838       ! Call simulator
839       do icol=1,parasolIN%Ncolumns
840          call parasol_subcolumn(parasolIN%npoints, PARASOL_NREFL,                       &
841                                 parasolIN%tautot_S_liq(1:parasolIN%Npoints,icol),       &
842                                 parasolIN%tautot_S_ice(1:parasolIN%Npoints,icol),       &
843                                 parasolPix_refl(:,icol,1:PARASOL_NREFL))
844          ! Store output (if requested)
845          if (associated(cospOUT%parasolPix_refl)) then
846             cospOUT%parasolPix_refl(ij:ik,icol,1:PARASOL_NREFL) =                          &
847                  parasolPix_refl(:,icol,1:PARASOL_NREFL)
848          endif
849       enddo
850    endif
851
852    ! Cloudsat (quickbeam) subcolumn simulator
853    if (Lcloudsat_subcolumn) then
854       ! Allocate space for local variables
855       allocate(cloudsatDBZe(cloudsatIN%Npoints,cloudsatIN%Ncolumns,cloudsatIN%Nlevels), &
856                cloudsatZe_non(cloudsatIN%Npoints,cloudsatIN%Ncolumns,cloudsatIN%Nlevels))
857       do icol=1,cloudsatIN%ncolumns
858          call quickbeam_subcolumn(cloudsatIN%rcfg,cloudsatIN%Npoints,cloudsatIN%Nlevels,&
859                                   cloudsatIN%hgt_matrix/1000._wp,                       &
860                                   cloudsatIN%z_vol(:,icol,:),                           &
861                                   cloudsatIN%kr_vol(:,icol,:),                          &
862                                   cloudsatIN%g_vol(:,1,:),cloudsatDBze(:,icol,:),cloudsatZe_non(:,icol,:))
863       enddo
864       ! Store output (if requested)
865       if (associated(cospOUT%cloudsat_Ze_tot)) then
866          cospOUT%cloudsat_Ze_tot(ij:ik,:,:) = cloudsatDBZe(:,:,1:cloudsatIN%Nlevels)
867       endif
868    endif
869
870    if (Lmodis_subcolumn) then
871       if(modisiN%nSunlit > 0) then
872          ! Allocate space for local variables
873          allocate(modisRetrievedTau(modisIN%nSunlit,modisIN%nColumns),                  &
874                   modisRetrievedSize(modisIN%nSunlit,modisIN%nColumns),                 &
875                   modisRetrievedPhase(modisIN%nSunlit,modisIN%nColumns),                &
876                   modisRetrievedCloudTopPressure(modisIN%nSunlit,modisIN%nColumns))
877          ! Call simulator
878          do i = 1, modisIN%nSunlit
879             call modis_subcolumn(modisIN%Ncolumns,modisIN%Nlevels,modisIN%pres(i,:),    &
880                                  modisIN%tau(int(modisIN%sunlit(i)),:,:),               &
881                                  modisIN%liqFrac(int(modisIN%sunlit(i)),:,:),           &
882                                  modisIN%g(int(modisIN%sunlit(i)),:,:),                 &
883                                  modisIN%w0(int(modisIN%sunlit(i)),:,:),                &
884                                  isccp_boxptop(int(modisIN%sunlit(i)),:),               &
885                                  modisRetrievedPhase(i,:),                              &
886                                  modisRetrievedCloudTopPressure(i,:),                   &
887                                  modisRetrievedTau(i,:),modisRetrievedSize(i,:))
888          end do
889       endif
890    endif
891
892    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
893    ! 5) Call column simulators
894    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895
896    ! ISCCP
897    if (Lisccp_column) then
898       ! Check to see which outputs are requested. If not requested, use a local dummy array
899       if(.not. associated(cospOUT%isccp_meanalbedocld)) then
900          allocate(out1D_1(Npoints))
901          cospOUT%isccp_meanalbedocld(ij:ik) => out1D_1
902       endif
903       if(.not. associated(cospOUT%isccp_meanptop)) then
904          allocate(out1D_2(Npoints))
905          cospOUT%isccp_meanptop(ij:ik) => out1D_2
906       endif
907       if(.not. associated(cospOUT%isccp_meantaucld)) then
908          allocate(out1D_3(Npoints))
909          cospOUT%isccp_meantaucld(ij:ik) => out1D_3
910       endif
911       if(.not. associated(cospOUT%isccp_totalcldarea)) then
912          allocate(out1D_4(Npoints))
913          cospOUT%isccp_totalcldarea(ij:ik) => out1D_4
914       endif
915       if(.not. associated(cospOUT%isccp_meantb)) then
916          allocate(out1D_5(Npoints))
917          cospOUT%isccp_meantb(ij:ik) => out1D_5
918       endif
919       if(.not. associated(cospOUT%isccp_fq)) then
920          allocate(out1D_6(Npoints*numISCCPTauBins*numISCCPPresBins))
921          cospOUT%isccp_fq(ij:ik,1:numISCCPTauBins,1:numISCCPPresBins) => out1D_6
922       endif
923
924       ! Call simulator
925       call icarus_column(isccpIN%npoints, isccpIN%ncolumns,isccp_boxtau(:,:),           &
926                          isccp_boxptop(:,:)/100._wp, isccpIN%sunlit,isccp_boxttop,      &
927                          cospOUT%isccp_fq(ij:ik,:,:),                                   &
928                          cospOUT%isccp_meanalbedocld(ij:ik),                            &
929                          cospOUT%isccp_meanptop(ij:ik),cospOUT%isccp_meantaucld(ij:ik), &
930                          cospOUT%isccp_totalcldarea(ij:ik),cospOUT%isccp_meantb(ij:ik))
931       cospOUT%isccp_fq(ij:ik,:,:) = cospOUT%isccp_fq(ij:ik,:,7:1:-1)
932
933       ! Check if there is any value slightly greater than 1
934       where ((cospOUT%isccp_totalcldarea > 1.0-1.e-5) .and.                             &
935              (cospOUT%isccp_totalcldarea < 1.0+1.e-5))
936              cospOUT%isccp_totalcldarea = 1.0
937       endwhere
938
939       ! Clear up memory (if necessary)
940       if (allocated(isccp_boxttop))   deallocate(isccp_boxttop)
941       if (allocated(isccp_boxptop))   deallocate(isccp_boxptop)
942       if (allocated(isccp_boxtau))    deallocate(isccp_boxtau)
943       if (allocated(isccp_meantbclr)) deallocate(isccp_meantbclr)
944       if (allocated(isccpLEVMATCH))   deallocate(isccpLEVMATCH)
945       if (allocated(out1D_1)) then
946          deallocate(out1D_1)
947          nullify(cospOUT%isccp_meanalbedocld)
948       endif
949       if (allocated(out1D_2)) then
950          deallocate(out1D_2)
951          nullify(cospOUT%isccp_meanptop)
952       endif
953       if (allocated(out1D_3)) then
954          deallocate(out1D_3)
955          nullify(cospOUT%isccp_meantaucld)
956       endif
957       if (allocated(out1D_4)) then
958          deallocate(out1D_4)
959          nullify(cospOUT%isccp_totalcldarea)
960       endif
961       if (allocated(out1D_5)) then
962          deallocate(out1D_5)
963          nullify(cospOUT%isccp_meantb)
964       endif
965       if (allocated(out1D_6)) then
966          deallocate(out1D_6)
967          nullify(cospOUT%isccp_fq)
968       endif
969    endif
970
971    ! MISR
972    if (Lmisr_column) then
973       ! Check to see which outputs are requested. If not requested, use a local dummy array
974       if (.not. associated(cospOUT%misr_cldarea)) then
975          allocate(out1D_1(Npoints))
976          cospOUT%misr_cldarea(ij:ik) => out1D_1
977       endif
978       if (.not. associated(cospOUT%misr_meanztop)) then
979          allocate(out1D_2(Npoints))
980          cospOUT%misr_meanztop(ij:ik) => out1D_2
981       endif
982       if (.not. associated(cospOUT%misr_fq)) then
983          allocate(out1D_3(Npoints*numMISRTauBins*numMISRHgtBins))
984          cospOUT%misr_fq(ij:ik,1:numMISRTauBins,1:numMISRHgtBins) => out1D_3
985        endif
986
987       ! Call simulator
988        call misr_column(misrIN%Npoints,misrIN%Ncolumns,misr_boxztop,misrIN%sunlit,&
989                         misr_boxtau,cospOUT%misr_cldarea(ij:ik),                  &
990                         cospOUT%misr_meanztop(ij:ik),cospOUT%misr_fq(ij:ik,:,:))
991
992       ! Clear up memory
993       if (allocated(misr_boxtau))               deallocate(misr_boxtau)
994       if (allocated(misr_boxztop))              deallocate(misr_boxztop)
995       if (allocated(misr_dist_model_layertops)) deallocate(misr_dist_model_layertops)
996       if (allocated(out1D_1)) then
997          deallocate(out1D_1)
998          nullify(cospOUT%misr_cldarea)
999       endif
1000       if (allocated(out1D_2)) then
1001          deallocate(out1D_2)
1002          nullify(cospOUT%misr_meanztop)
1003       endif
1004       if (allocated(out1D_3)) then
1005          deallocate(out1D_3)
1006          nullify(cospOUT%misr_fq)
1007       endif
1008    endif
1009
1010    ! CALIPSO LIDAR Simulator
1011    if (Lcalipso_column) then
1012       ! Check to see which outputs are requested. If not requested, use a local dummy array
1013       if (.not. associated(cospOUT%calipso_cfad_sr)) then
1014          allocate(out1D_1(Npoints*SR_BINS*Nlvgrid))
1015          cospOUT%calipso_cfad_sr(ij:ik,1:SR_BINS,1:Nlvgrid) => out1D_1
1016       endif
1017       if (.not. associated(cospOUT%calipso_lidarcld)) then
1018          allocate(out1D_2(Npoints*Nlvgrid))
1019          cospOUT%calipso_lidarcld(ij:ik,1:Nlvgrid) => out1D_2
1020       endif
1021       if (.not. associated(cospOUT%calipso_lidarcldphase)) then
1022          allocate(out1D_3(Npoints*Nlvgrid*6))
1023          cospOUT%calipso_lidarcldphase(ij:ik,1:Nlvgrid,1:6) => out1D_3
1024       endif
1025       if (.not. associated(cospOUT%calipso_cldlayer)) then
1026          allocate(out1D_4(Npoints*LIDAR_NCAT))
1027          cospOUT%calipso_cldlayer(ij:ik,1:LIDAR_NCAT) => out1D_4
1028       endif
1029       if (.not. associated(cospOUT%calipso_cldlayerphase)) then
1030          allocate(out1D_5(Npoints*LIDAR_NCAT*6))
1031          cospOUT%calipso_cldlayerphase(ij:ik,1:LIDAR_NCAT,1:6) => out1D_5
1032       endif
1033       if (.not. associated(cospOUT%calipso_lidarcldtmp)) then
1034          allocate(out1D_6(Npoints*40*5))
1035          cospOUT%calipso_lidarcldtmp(ij:ik,1:40,1:5) => out1D_6
1036       endif   
1037       if (.not. associated(cospOUT%calipso_lidarcldtype)) then
1038          allocate(out1D_7(Npoints*Nlvgrid*4))
1039          cospOUT%calipso_lidarcldtype(ij:ik,1:Nlvgrid,1:4) => out1D_7
1040       endif
1041       if (.not. associated(cospOUT%calipso_cldtype)) then
1042          allocate(out1D_8(Npoints*LIDAR_NTYPE))
1043          cospOUT%calipso_cldtype(ij:ik,1:LIDAR_NTYPE) => out1D_8 
1044       endif
1045       if (.not. associated(cospOUT%calipso_cldtypetemp)) then
1046          allocate(out1D_9(Npoints*LIDAR_NTYPE)) 
1047          cospOUT%calipso_cldtypetemp(ij:ik,1:LIDAR_NTYPE) => out1D_9
1048       endif
1049       if (.not. associated(cospOUT%calipso_cldtypemeanz)) then
1050          allocate(out1D_10(Npoints*2))
1051          cospOUT%calipso_cldtypemeanz(ij:ik,1:2) => out1D_10
1052       endif
1053       if (.not. associated(cospOUT%calipso_cldtypemeanzse)) then
1054          allocate(out1D_12(Npoints*3))
1055          cospOUT%calipso_cldtypemeanzse(ij:ik,1:3) => out1D_12
1056       endif
1057       if (.not. associated(cospOUT%calipso_cldthinemis)) then
1058          allocate(out1D_11(Npoints))
1059          cospOUT%calipso_cldthinemis(ij:ik) => out1D_11
1060       endif
1061       
1062       ! Call simulator
1063       ok_lidar_cfad=.true.
1064       call lidar_column(calipsoIN%Npoints, calipsoIN%Ncolumns, calipsoIN%Nlevels,       &
1065            Nlvgrid, SR_BINS, LIDAR_NTYPE, 'calipso',calipso_beta_tot(:,:,:), calipso_beta_mol(:,:),&
1066            cospgridIN%phalf(:,2:calipsoIN%Nlevels),cospgridIN%hgt_matrix,               &
1067            cospgridIN%hgt_matrix_half, vgrid_z(:), ok_lidar_cfad, LIDAR_NCAT,           &
1068            cospOUT%calipso_cfad_sr(ij:ik,:,:), cospOUT%calipso_lidarcld(ij:ik,:),       &
1069            cospOUT%calipso_cldlayer(ij:ik,:),                                           &
1070            cospgridIN%at(:,:), calipso_betaperp_tot(:,:,:), cospgridIN%surfelev,        &
1071            cospOUT%calipso_lidarcldphase(ij:ik,:,:),                       &
1072            cospOUT%calipso_lidarcldtype(ij:ik,:,:),  cospOUT%calipso_cldtype(ij:ik,:),  &
1073            cospOUT%calipso_cldtypetemp(ij:ik,:), cospOUT%calipso_cldtypemeanz(ij:ik,:), &
1074            cospOUT%calipso_cldtypemeanzse(ij:ik,:), cospOUT%calipso_cldthinemis(ij:ik), &
1075            cospOUT%calipso_cldlayerphase(ij:ik,:,:), cospOUT%calipso_lidarcldtmp(ij:ik,:,:))
1076
1077         if (associated(cospOUT%calipso_srbval)) cospOUT%calipso_srbval = calipso_histBsct
1078
1079       ! Free up memory (if necessary)
1080       if (allocated(out1D_1)) then
1081          deallocate(out1D_1)
1082          nullify(cospOUT%calipso_cfad_sr)
1083       endif
1084       if (allocated(out1D_2)) then
1085          deallocate(out1D_2)
1086          nullify(cospOUT%calipso_lidarcld)
1087       endif
1088       if (allocated(out1D_3)) then
1089          deallocate(out1D_3)
1090          nullify(cospOUT%calipso_lidarcldphase)
1091       endif
1092       if (allocated(out1D_4)) then
1093          deallocate(out1D_4)
1094          nullify(cospOUT%calipso_cldlayer)
1095       endif
1096       if (allocated(out1D_5)) then
1097          deallocate(out1D_5)
1098          nullify(cospOUT%calipso_cldlayerphase)
1099       endif
1100       if (allocated(out1D_6)) then
1101          deallocate(out1D_6)
1102          nullify(cospOUT%calipso_lidarcldtmp)
1103       endif
1104       if (allocated(out1D_7)) then
1105          deallocate(out1D_7) 
1106          nullify(cospOUT%calipso_lidarcldtype)
1107       endif 
1108       if (allocated(out1D_8)) then
1109          deallocate(out1D_8)
1110          nullify(cospOUT%calipso_cldtype)
1111       endif
1112       if (allocated(out1D_9)) then
1113          deallocate(out1D_9)
1114          nullify(cospOUT%calipso_cldtypetemp)
1115       endif
1116       if (allocated(out1D_10)) then
1117          deallocate(out1D_10)
1118          nullify(cospOUT%calipso_cldtypemeanz)
1119       endif
1120       if (allocated(out1D_12)) then
1121          deallocate(out1D_12)
1122          nullify(cospOUT%calipso_cldtypemeanzse)
1123       endif
1124       if (allocated(out1D_11)) then
1125          deallocate(out1D_11)
1126          nullify(cospOUT%calipso_cldthinemis)
1127       endif
1128
1129    endif
1130
1131    ! GROUND LIDAR Simulator
1132    if (LgrLidar532_column) then
1133       ! Check to see which outputs are requested. If not requested, use a local dummy array
1134       if (.not. associated(cospOUT%grLidar532_cfad_sr)) then
1135          allocate(out1D_1(Npoints*SR_BINS*Nlvgrid))
1136          cospOUT%grLidar532_cfad_sr(ij:ik,1:SR_BINS,1:Nlvgrid) => out1D_1
1137       endif
1138       if (.not. associated(cospOUT%grLidar532_lidarcld)) then
1139          allocate(out1D_2(Npoints*Nlvgrid))
1140          cospOUT%grLidar532_lidarcld(ij:ik,1:Nlvgrid) => out1D_2
1141       endif
1142       if (.not. associated(cospOUT%grLidar532_cldlayer)) then
1143          allocate(out1D_3(Npoints*LIDAR_NCAT))
1144          cospOUT%grLidar532_cldlayer(ij:ik,1:LIDAR_NCAT) => out1D_3
1145       endif
1146       
1147       ! Call simulator
1148       ok_lidar_cfad_grLidar532=.true.
1149       call lidar_column(grLidar532IN%Npoints, grLidar532IN%Ncolumns, grLidar532IN%Nlevels,       &
1150            Nlvgrid, SR_BINS, LIDAR_NTYPE, 'grlidar532',grLidar532_beta_tot(:,:,:), grLidar532_beta_mol(:,:),&
1151            cospgridIN%phalf(:,2:grLidar532IN%Nlevels),cospgridIN%hgt_matrix,               &
1152            cospgridIN%hgt_matrix_half, vgrid_z(:), ok_lidar_cfad_grLidar532, LIDAR_NCAT,      &
1153            cospOUT%grLidar532_cfad_sr(ij:ik,:,:), cospOUT%grLidar532_lidarcld(ij:ik,:),       &
1154            cospOUT%grLidar532_cldlayer(ij:ik,:))
1155
1156       if (associated(cospOUT%grLidar532_srbval)) cospOUT%grLidar532_srbval = grLidar532_histBsct
1157
1158       ! Free up memory (if necessary)
1159       if (allocated(out1D_1)) then
1160          deallocate(out1D_1)
1161          nullify(cospOUT%grLidar532_cfad_sr)
1162       endif
1163       if (allocated(out1D_2)) then
1164          deallocate(out1D_2)
1165          nullify(cospOUT%grLidar532_lidarcld)
1166       endif
1167       if (allocated(out1D_3)) then
1168          deallocate(out1D_3)
1169          nullify(cospOUT%grLidar532_cldlayer)
1170       endif
1171
1172    endif
1173
1174    ! ATLID Simulator
1175    if (Latlid_column) then
1176       ! Check to see which outputs are requested. If not requested, use a local dummy array
1177       if (.not. associated(cospOUT%atlid_cfad_sr)) then
1178          allocate(out1D_1(Npoints*SR_BINS*Nlvgrid))
1179          cospOUT%atlid_cfad_sr(ij:ik,1:SR_BINS,1:Nlvgrid) => out1D_1
1180       endif                                   
1181       if (.not. associated(cospOUT%atlid_lidarcld)) then
1182          allocate(out1D_2(Npoints*Nlvgrid))                 
1183          cospOUT%atlid_lidarcld(ij:ik,1:Nlvgrid) => out1D_2
1184       endif                                                   
1185       if (.not. associated(cospOUT%atlid_cldlayer)) then   
1186          allocate(out1D_3(Npoints*LIDAR_NCAT))                 
1187          cospOUT%atlid_cldlayer(ij:ik,1:LIDAR_NCAT) => out1D_3
1188       endif                                                     
1189       
1190       ! Call simulator                                                             
1191       ok_lidar_cfad_atlid=.true.
1192       call lidar_column(atlidIN%Npoints, atlidIN%Ncolumns, atlidIN%Nlevels,       &
1193            Nlvgrid, SR_BINS, LIDAR_NTYPE, 'atlid',atlid_beta_tot(:,:,:),     &
1194            atlid_beta_mol(:,:), cospgridIN%phalf(:,2:atlidIN%Nlevels),            &
1195            cospgridIN%hgt_matrix, cospgridIN%hgt_matrix_half, vgrid_z(:),         &
1196            ok_lidar_cfad_atlid, LIDAR_NCAT, cospOUT%atlid_cfad_sr(ij:ik,:,:),     &
1197            cospOUT%atlid_lidarcld(ij:ik,:), cospOUT%atlid_cldlayer(ij:ik,:))
1198       
1199       if (associated(cospOUT%atlid_srbval)) cospOUT%atlid_srbval = atlid_histBsct
1200
1201       ! Free up memory (if necessary)       
1202       if (allocated(out1D_1)) then           
1203          deallocate(out1D_1)                 
1204          nullify(cospOUT%atlid_cfad_sr)
1205       endif                                 
1206       if (allocated(out1D_2)) then         
1207          deallocate(out1D_2)               
1208          nullify(cospOUT%atlid_lidarcld)
1209       endif                                 
1210       if (allocated(out1D_3)) then         
1211          deallocate(out1D_3)
1212          nullify(cospOUT%atlid_cldlayer)
1213       endif         
1214
1215    endif
1216
1217    ! PARASOL
1218    if (Lparasol_column) then
1219       call parasol_column(parasolIN%Npoints,PARASOL_NREFL,parasolIN%Ncolumns,           &
1220                            cospgridIN%land(:),parasolPix_refl(:,:,:),                   &
1221                            cospOUT%parasolGrid_refl(ij:ik,:))
1222       if (allocated(parasolPix_refl)) deallocate(parasolPix_refl)
1223    endif
1224
1225    ! CLOUDSAT
1226    if (Lcloudsat_column) then
1227       ! Check to see which outputs are requested. If not requested, use a local dummy array
1228       if (.not. associated(cospOUT%cloudsat_cfad_ze)) then
1229          allocate(out1D_1(Npoints*cloudsat_DBZE_BINS*Nlvgrid))
1230          cospOUT%cloudsat_cfad_ze(ij:ik,1:cloudsat_DBZE_BINS,1:Nlvgrid) => out1D_1
1231       endif
1232
1233       if (.not. associated(cospOUT%cloudsat_pia)) then
1234          allocate(out1D_2(Npoints))
1235          cospOUT%cloudsat_pia(ij:ik) => out1D_2
1236       endif
1237       if (.not. associated(cospOUT%cloudsat_precip_cover)) then
1238          allocate(out1D_3(Npoints*nCloudsatPrecipClass))
1239          cospOUT%cloudsat_precip_cover(ij:ik,1:nCloudsatPrecipClass) => out1D_3
1240       endif
1241         
1242       ! Call simulator
1243       call quickbeam_column(cloudsatIN%Npoints, cloudsatIN%Ncolumns, cloudsatIN%Nlevels,&
1244            Nlvgrid, cloudsat_DBZE_BINS, 'cloudsat', cloudsatDBZe, cloudsatZe_non,       &
1245!            cospgridIN%land(:), cospgridIN%at(:,cospIN%Nlevels), cospIN%fracPrecipIce,   & !PREC_BUG
1246!            cospgridIN%hgt_matrix, cospgridIN%hgt_matrix_half,                           & !PREC_BUG
1247            cospgridIN%land(:), cospgridIN%surfelev(:), cospgridIN%at(:,cospIN%Nlevels), & !PREC_BUG
1248            cospIN%fracPrecipIce, cospgridIN%hgt_matrix, cospgridIN%hgt_matrix_half,     & !PREC_BUG
1249            cospOUT%cloudsat_cfad_ze(ij:ik,:,:), cospOUT%cloudsat_precip_cover,          &
1250            cospOUT%cloudsat_pia)
1251       ! Free up memory  (if necessary)
1252       if (allocated(out1D_1)) then
1253          deallocate(out1D_1)
1254          nullify(cospOUT%cloudsat_cfad_ze)
1255       endif
1256       if (allocated(out1D_2)) then
1257          deallocate(out1D_2)
1258          nullify(cospOUT%cloudsat_pia)
1259       endif
1260       if (allocated(out1D_3)) then
1261          deallocate(out1D_3)
1262          nullify(cospOUT%cloudsat_precip_cover)
1263       endif
1264    endif
1265
1266    ! MODIS
1267    if (Lmodis_column) then
1268       if(modisiN%nSunlit > 0) then
1269          ! Allocate space for local variables
1270          allocate(modisCftotal(modisIN%nSunlit), modisCfLiquid(modisIN%nSunlit),        &
1271                   modisCfIce(modisIN%nSunlit),modisCfHigh(modisIN%nSunlit),             &
1272                   modisCfMid(modisIN%nSunlit),modisCfLow(modisIN%nSunlit),              &
1273                   modisMeanTauTotal(modisIN%nSunlit),                                   &
1274                   modisMeanTauLiquid(modisIN%nSunlit),modisMeanTauIce(modisIN%nSunlit), &
1275                   modisMeanLogTauTotal(modisIN%nSunlit),                                &
1276                   modisMeanLogTauLiquid(modisIN%nSunlit),                               &
1277                   modisMeanLogTauIce(modisIN%nSunlit),                                  &
1278                   modisMeanSizeLiquid(modisIN%nSunlit),                                 &
1279                   modisMeanSizeIce(modisIN%nSunlit),                                    &
1280                   modisMeanCloudTopPressure(modisIN%nSunlit),                           &
1281                   modisMeanLiquidWaterPath(modisIN%nSunlit),                            &
1282                   modisMeanIceWaterPath(modisIN%nSunlit),                               &
1283                   modisJointHistogram(modisIN%nSunlit,numMODISTauBins,numMODISPresBins),&
1284                   modisJointHistogramIce(modisIN%nSunlit,numModisTauBins,numMODISReffIceBins),&
1285                   modisJointHistogramLiq(modisIN%nSunlit,numModisTauBins,numMODISReffLiqBins))
1286          ! Call simulator
1287          call modis_column(modisIN%nSunlit, modisIN%Ncolumns,modisRetrievedPhase,       &
1288                             modisRetrievedCloudTopPressure,modisRetrievedTau,           &
1289                             modisRetrievedSize, modisCfTotal, modisCfLiquid, modisCfIce,&
1290                             modisCfHigh, modisCfMid, modisCfLow, modisMeanTauTotal,     &
1291                             modisMeanTauLiquid, modisMeanTauIce, modisMeanLogTauTotal,  &
1292                             modisMeanLogTauLiquid, modisMeanLogTauIce,                  &
1293                             modisMeanSizeLiquid, modisMeanSizeIce,                      &
1294                             modisMeanCloudTopPressure, modisMeanLiquidWaterPath,        &
1295                             modisMeanIceWaterPath, modisJointHistogram,                 &
1296                             modisJointHistogramIce,modisJointHistogramLiq)
1297          ! Store data (if requested)
1298          if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean)) then
1299             cospOUT%modis_Cloud_Fraction_Total_Mean(ij+int(modisIN%sunlit(:))-1)   =    &
1300                  modisCfTotal
1301          endif
1302          if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean)) then
1303             cospOUT%modis_Cloud_Fraction_Water_Mean(ij+int(modisIN%sunlit(:))-1)   =    &
1304                  modisCfLiquid
1305          endif
1306          if (associated(cospOUT%modis_Cloud_Fraction_Ice_Mean)) then
1307             cospOUT%modis_Cloud_Fraction_Ice_Mean(ij+int(modisIN%sunlit(:))-1)     =    &
1308                  modisCfIce
1309          endif
1310          if (associated(cospOUT%modis_Cloud_Fraction_High_Mean)) then
1311             cospOUT%modis_Cloud_Fraction_High_Mean(ij+int(modisIN%sunlit(:))-1)    =    &
1312                  modisCfHigh
1313          endif
1314          if (associated(cospOUT%modis_Cloud_Fraction_Mid_Mean)) then
1315             cospOUT%modis_Cloud_Fraction_Mid_Mean(ij+int(modisIN%sunlit(:))-1)     =    &
1316                  modisCfMid
1317          endif
1318          if (associated(cospOUT%modis_Cloud_Fraction_Low_Mean)) then
1319             cospOUT%modis_Cloud_Fraction_Low_Mean(ij+int(modisIN%sunlit(:))-1)     =    &
1320                  modisCfLow
1321          endif
1322          if (associated(cospOUT%modis_Optical_Thickness_Total_Mean)) then
1323             cospOUT%modis_Optical_Thickness_Total_Mean(ij+int(modisIN%sunlit(:))-1) =   &
1324                  modisMeanTauTotal
1325          endif
1326          if (associated(cospOUT%modis_Optical_Thickness_Water_Mean)) then
1327             cospOUT%modis_Optical_Thickness_Water_Mean(ij+int(modisIN%sunlit(:))-1) =   &
1328                  modisMeanTauLiquid
1329          endif
1330          if (associated(cospOUT%modis_Optical_Thickness_Ice_Mean)) then
1331             cospOUT%modis_Optical_Thickness_Ice_Mean(ij+int(modisIN%sunlit(:))-1)  =    &
1332                  modisMeanTauIce
1333          endif
1334          if (associated(cospOUT%modis_Optical_Thickness_Total_LogMean)) then
1335             cospOUT%modis_Optical_Thickness_Total_LogMean(ij+int(modisIN%sunlit(:))-1)= &
1336                  modisMeanLogTauTotal
1337          endif
1338          if (associated(cospOUT%modis_Optical_Thickness_Water_LogMean)) then
1339             cospOUT%modis_Optical_Thickness_Water_LogMean(ij+int(modisIN%sunlit(:))-1) = &
1340                  modisMeanLogTauLiquid
1341          endif
1342          if (associated(cospOUT%modis_Optical_Thickness_Ice_LogMean)) then
1343             cospOUT%modis_Optical_Thickness_Ice_LogMean(ij+int(modisIN%sunlit(:))-1) =  &
1344                  modisMeanLogTauIce
1345          endif
1346          if (associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean)) then
1347             cospOUT%modis_Cloud_Particle_Size_Water_Mean(ij+int(modisIN%sunlit(:))-1) = &
1348                  modisMeanSizeLiquid
1349          endif
1350          if (associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean)) then
1351             cospOUT%modis_Cloud_Particle_Size_Ice_Mean(ij+int(modisIN%sunlit(:))-1) =   &
1352                  modisMeanSizeIce
1353          endif
1354          if (associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean)) then
1355             cospOUT%modis_Cloud_Top_Pressure_Total_Mean(ij+int(modisIN%sunlit(:))-1) =  &
1356                  modisMeanCloudTopPressure
1357          endif
1358          if (associated(cospOUT%modis_Liquid_Water_Path_Mean)) then
1359             cospOUT%modis_Liquid_Water_Path_Mean(ij+int(modisIN%sunlit(:))-1)      =    &
1360                  modisMeanLiquidWaterPath
1361          endif
1362          if (associated(cospOUT%modis_Ice_Water_Path_Mean)) then
1363              cospOUT%modis_Ice_Water_Path_Mean(ij+int(modisIN%sunlit(:))-1)         =   &
1364                  modisMeanIceWaterPath
1365          endif
1366          if (associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure)) then
1367             cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(ij+            &
1368                  int(modisIN%sunlit(:))-1, 1:numModisTauBins, :) = modisJointHistogram(:, :, :)
1369             ! Reorder pressure bins in joint histogram to go from surface to TOA
1370             cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(ij:ik,:,:) = &
1371                  cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(ij:ik,:,numMODISPresBins:1:-1)
1372          endif
1373          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffIce)) then
1374             cospOUT%modis_Optical_Thickness_vs_ReffIce(ij+int(modisIN%sunlit(:))-1, 1:numMODISTauBins,:) = &
1375                modisJointHistogramIce(:,:,:)
1376          endif
1377          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffLiq)) then
1378             cospOUT%modis_Optical_Thickness_vs_ReffLiq(ij+int(modisIN%sunlit(:))-1, 1:numMODISTauBins,:) = &
1379                modisJointHistogramLiq(:,:,:)
1380          endif
1381
1382          if(modisIN%nSunlit < modisIN%Npoints) then
1383             ! Where it's night and we haven't done the retrievals the values are undefined
1384             if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean))                    &
1385                cospOUT%modis_Cloud_Fraction_Total_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1386             if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean))                    &
1387                cospOUT%modis_Cloud_Fraction_Water_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1388             if (associated(cospOUT%modis_Cloud_Fraction_Ice_Mean))                      &
1389                cospOUT%modis_Cloud_Fraction_Ice_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1390             if (associated(cospOUT%modis_Cloud_Fraction_High_Mean))                     &
1391                cospOUT%modis_Cloud_Fraction_High_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1392             if (associated(cospOUT%modis_Cloud_Fraction_Mid_Mean))                      &
1393                cospOUT%modis_Cloud_Fraction_Mid_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1394             if (associated(cospOUT%modis_Cloud_Fraction_Low_Mean))                      &
1395                cospOUT%modis_Cloud_Fraction_Low_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1396             if (associated(cospOUT%modis_Optical_Thickness_Total_Mean))                 &
1397                cospOUT%modis_Optical_Thickness_Total_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1398             if (associated(cospOUT%modis_Optical_Thickness_Water_Mean))                 &
1399                cospOUT%modis_Optical_Thickness_Water_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1400             if (associated(cospOUT%modis_Optical_Thickness_Ice_Mean))                   &
1401                cospOUT%modis_Optical_Thickness_Ice_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1402             if (associated(cospOUT%modis_Optical_Thickness_Total_LogMean))              &
1403                cospOUT%modis_Optical_Thickness_Total_LogMean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1404             if (associated(cospOUT%modis_Optical_Thickness_Water_LogMean))              &
1405                cospOUT%modis_Optical_Thickness_Water_LogMean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1406             if (associated(cospOUT%modis_Optical_Thickness_Ice_LogMean))                &
1407                cospOUT%modis_Optical_Thickness_Ice_LogMean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1408             if (associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean))               &
1409                cospOUT%modis_Cloud_Particle_Size_Water_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1410             if (associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean))                 &
1411                cospOUT%modis_Cloud_Particle_Size_Ice_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1412             if (associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean))                &
1413                cospOUT%modis_Cloud_Top_Pressure_Total_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1414             if (associated(cospOUT%modis_Liquid_Water_Path_Mean))                       &
1415                cospOUT%modis_Liquid_Water_Path_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1416             if (associated(cospOUT%modis_Ice_Water_Path_Mean))                          &
1417                cospOUT%modis_Ice_Water_Path_Mean(ij+int(modisIN%notSunlit(:))-1) = R_UNDEF
1418             if (associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure))      &
1419                cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(ij+int(modisIN%notSunlit(:))-1, :, :) = R_UNDEF
1420          end if
1421       else
1422          ! It's nightime everywhere - everything is undefined
1423          if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean))                       &
1424             cospOUT%modis_Cloud_Fraction_Total_Mean(ij:ik) = R_UNDEF
1425          if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean))                       &
1426             cospOUT%modis_Cloud_Fraction_Water_Mean(ij:ik) = R_UNDEF
1427          if (associated(cospOUT%modis_Cloud_Fraction_Ice_Mean))                         &
1428             cospOUT%modis_Cloud_Fraction_Ice_Mean(ij:ik) = R_UNDEF
1429          if (associated(cospOUT%modis_Cloud_Fraction_High_Mean))                        &
1430             cospOUT%modis_Cloud_Fraction_High_Mean(ij:ik) = R_UNDEF
1431          if (associated(cospOUT%modis_Cloud_Fraction_Mid_Mean))                         &
1432             cospOUT%modis_Cloud_Fraction_Mid_Mean(ij:ik) = R_UNDEF
1433          if (associated(cospOUT%modis_Cloud_Fraction_Low_Mean))                         &
1434             cospOUT%modis_Cloud_Fraction_Low_Mean(ij:ik) = R_UNDEF
1435          if (associated(cospOUT%modis_Optical_Thickness_Total_Mean))                    &
1436             cospOUT%modis_Optical_Thickness_Total_Mean(ij:ik) = R_UNDEF
1437          if (associated(cospOUT%modis_Optical_Thickness_Water_Mean))                    &
1438             cospOUT%modis_Optical_Thickness_Water_Mean(ij:ik) = R_UNDEF
1439          if (associated(cospOUT%modis_Optical_Thickness_Ice_Mean))                      &
1440             cospOUT%modis_Optical_Thickness_Ice_Mean(ij:ik) = R_UNDEF
1441          if (associated(cospOUT%modis_Optical_Thickness_Total_LogMean))                 &
1442             cospOUT%modis_Optical_Thickness_Total_LogMean(ij:ik) = R_UNDEF
1443          if (associated(cospOUT%modis_Optical_Thickness_Water_LogMean))                 &
1444             cospOUT%modis_Optical_Thickness_Water_LogMean(ij:ik) = R_UNDEF
1445          if (associated(cospOUT%modis_Optical_Thickness_Ice_LogMean))                   &
1446             cospOUT%modis_Optical_Thickness_Ice_LogMean(ij:ik) = R_UNDEF
1447          if (associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean))                  &
1448             cospOUT%modis_Cloud_Particle_Size_Water_Mean(ij:ik) = R_UNDEF
1449          if (associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean))                    &
1450              cospOUT%modis_Cloud_Particle_Size_Ice_Mean(ij:ik) = R_UNDEF
1451          if (associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean))                   &
1452             cospOUT%modis_Cloud_Top_Pressure_Total_Mean(ij:ik) = R_UNDEF
1453          if (associated(cospOUT%modis_Liquid_Water_Path_Mean))                          &
1454             cospOUT%modis_Liquid_Water_Path_Mean(ij:ik) = R_UNDEF
1455          if (associated(cospOUT%modis_Ice_Water_Path_Mean))                             &
1456             cospOUT%modis_Ice_Water_Path_Mean(ij:ik) = R_UNDEF
1457          if (associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure))         &
1458             cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(ij:ik, :, :) = R_UNDEF
1459       endif
1460       ! Free up memory (if necessary)
1461       if (allocated(modisRetrievedTau))               deallocate(modisRetrievedTau)
1462       if (allocated(modisRetrievedSize))              deallocate(modisRetrievedSize)
1463       if (allocated(modisRetrievedPhase))             deallocate(modisRetrievedPhase)
1464       if (allocated(modisRetrievedCloudTopPressure))  deallocate(modisRetrievedCloudTopPressure)
1465       if (allocated(modisCftotal))                    deallocate(modisCftotal)
1466       if (allocated(modisCfLiquid))                   deallocate(modisCfLiquid)
1467       if (allocated(modisCfIce))                      deallocate(modisCfIce)
1468       if (allocated(modisCfHigh))                     deallocate(modisCfHigh)
1469       if (allocated(modisCfMid))                      deallocate(modisCfMid)
1470       if (allocated(modisCfLow))                      deallocate(modisCfLow)
1471       if (allocated(modisMeanTauTotal))               deallocate(modisMeanTauTotal)
1472       if (allocated(modisMeanTauLiquid))              deallocate(modisMeanTauLiquid)
1473       if (allocated(modisMeanTauIce))                 deallocate(modisMeanTauIce)
1474       if (allocated(modisMeanLogTauTotal))            deallocate(modisMeanLogTauTotal)
1475       if (allocated(modisMeanLogTauLiquid))           deallocate(modisMeanLogTauLiquid)
1476       if (allocated(modisMeanLogTauIce))              deallocate(modisMeanLogTauIce)
1477       if (allocated(modisMeanSizeLiquid))             deallocate(modisMeanSizeLiquid)
1478       if (allocated(modisMeanSizeIce))                deallocate(modisMeanSizeIce)
1479       if (allocated(modisMeanCloudTopPressure))       deallocate(modisMeanCloudTopPressure)
1480       if (allocated(modisMeanLiquidWaterPath))        deallocate(modisMeanLiquidWaterPath)
1481       if (allocated(modisMeanIceWaterPath))           deallocate(modisMeanIceWaterPath)
1482       if (allocated(modisJointHistogram))             deallocate(modisJointHistogram)
1483       if (allocated(modisJointHistogramIce))          deallocate(modisJointHistogramIce)
1484       if (allocated(modisJointHistogramLiq))          deallocate(modisJointHistogramLiq)
1485       if (allocated(isccp_boxttop))                   deallocate(isccp_boxttop)
1486       if (allocated(isccp_boxptop))                   deallocate(isccp_boxptop)
1487       if (allocated(isccp_boxtau))                    deallocate(isccp_boxtau)
1488       if (allocated(isccp_meantbclr))                 deallocate(isccp_meantbclr)
1489       if (allocated(isccpLEVMATCH))                   deallocate(isccpLEVMATCH)
1490    endif
1491
1492    ! RTTOV
1493!    if (lrttov_column) then
1494!       call rttov_column(rttovIN%nPoints,rttovIN%nLevels,rttovIN%nSubCols,rttovIN%q,    &
1495!                         rttovIN%p,rttovIN%t,rttovIN%o3,rttovIN%ph,rttovIN%h_surf,      &
1496!                         rttovIN%u_surf,rttovIN%v_surf,rttovIN%p_surf,rttovIN%t_skin,   &
1497!                         rttovIN%t2m,rttovIN%q2m,rttovIN%lsmask,rttovIN%longitude,      &
1498!                         rttovIN%latitude,rttovIN%seaice,rttovIN%co2,rttovIN%ch4,       &
1499!                         rttovIN%n2o,rttovIN%co,rttovIN%zenang,lrttov_cleanUp,          &
1500!                         cospOUT%rttov_tbs(ij:ik,:),cosp_simulator(nError+1),           &
1501!                         ! Optional arguments for surface emissivity calculation
1502!                         month=rttovIN%month)
1503!                         ! Optional arguments to rttov for all-sky calculation
1504!                         ! rttovIN%month, rttovIN%tca,rttovIN%cldIce,rttovIN%cldLiq,     &
1505!                         ! rttovIN%fl_rain,rttovIN%fl_snow)
1506!    endif
1507
1508    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1509    ! 6) Compute multi-instrument products
1510    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1511
1512    ! CLOUDSAT/CALIPSO products
1513    if (Lradar_lidar_tcc .or. Llidar_only_freq_cloud .or. Lcloudsat_tcc .or. Lcloudsat_tcc2) then
1514
1515       if (use_vgrid) then
1516          allocate(lidar_only_freq_cloud(cloudsatIN%Npoints,Nlvgrid),                    &
1517               radar_lidar_tcc(cloudsatIN%Npoints), cloudsat_tcc(cloudsatIN%Npoints),    &
1518               cloudsat_tcc2(cloudsatIN%Npoints))
1519          allocate(betamol_in(cloudsatIN%Npoints,1,cloudsatIN%Nlevels),                  &
1520                   betamoli(cloudsatIN%Npoints,1,Nlvgrid),                               &
1521                   pnormI(cloudsatIN%Npoints,cloudsatIN%Ncolumns,Nlvgrid),               &
1522                   Ze_totI(cloudsatIN%Npoints,cloudsatIN%Ncolumns,Nlvgrid))
1523
1524          ! Regrid in the vertical (*NOTE* This routine requires SFC-2-TOA ordering, so flip
1525          ! inputs and outputs to maintain TOA-2-SFC ordering convention in COSP2.)
1526          betamol_in(:,1,:) = calipso_beta_mol(:,cloudsatIN%Nlevels:1:-1)
1527          call cosp_change_vertical_grid(cloudsatIN%Npoints,1,cloudsatIN%Nlevels,        &
1528               cospgridIN%hgt_matrix(:,cloudsatIN%Nlevels:1:-1),                         &
1529               cospgridIN%hgt_matrix_half(:,cloudsatIN%Nlevels:1:-1),betamol_in,         &
1530               Nlvgrid,vgrid_zl(Nlvgrid:1:-1),vgrid_zu(Nlvgrid:1:-1),                    &
1531               betamolI(:,1,Nlvgrid:1:-1))
1532
1533          call cosp_change_vertical_grid(cloudsatIN%Npoints,cloudsatIN%Ncolumns,         &
1534               cloudsatIN%Nlevels,cospgridIN%hgt_matrix(:,cloudsatIN%Nlevels:1:-1),      &
1535               cospgridIN%hgt_matrix_half(:,cloudsatIN%Nlevels:1:-1),                    &
1536               calipso_beta_tot(:,:,cloudsatIN%Nlevels:1:-1),Nlvgrid,                    &
1537               vgrid_zl(Nlvgrid:1:-1),vgrid_zu(Nlvgrid:1:-1),pnormI(:,:,Nlvgrid:1:-1))
1538
1539          call cosp_change_vertical_grid(cloudsatIN%Npoints,cloudsatIN%Ncolumns,         &
1540               cloudsatIN%Nlevels,cospgridIN%hgt_matrix(:,cloudsatIN%Nlevels:1:-1),      &
1541               cospgridIN%hgt_matrix_half(:,cloudsatIN%Nlevels:1:-1),                    &
1542               cloudsatDBZe(:,:,cloudsatIN%Nlevels:1:-1),Nlvgrid,vgrid_zl(Nlvgrid:1:-1), &
1543               vgrid_zu(Nlvgrid:1:-1),Ze_totI(:,:,Nlvgrid:1:-1),log_units=.true.)
1544
1545          call cosp_lidar_only_cloud(cloudsatIN%Npoints, cloudsatIN%Ncolumns, Nlvgrid,   &
1546             pnormI, betamolI, Ze_totI, lidar_only_freq_cloud, radar_lidar_tcc,          &
1547             cloudsat_tcc, cloudsat_tcc2)
1548
1549          deallocate(betamol_in,betamolI,pnormI,ze_totI)
1550       else
1551          allocate(lidar_only_freq_cloud(cloudsatIN%Npoints,cloudsatIN%Nlevels),         &
1552               radar_lidar_tcc(cloudsatIN%Npoints), cloudsat_tcc(cloudsatIN%Npoints),    &
1553               cloudsat_tcc2(cloudsatIN%Npoints))
1554          call cosp_lidar_only_cloud(cloudsatIN%Npoints,cloudsatIN%Ncolumns,             &
1555               cospIN%Nlevels,calipso_beta_tot(:,:,cloudsatIN%Nlevels:1:-1),             &
1556               calipso_beta_mol(:,cloudsatIN%Nlevels:1:-1),                              &
1557               cloudsatDBZe(:,:,cloudsatIN%Nlevels:1:-1),lidar_only_freq_cloud,          &
1558               radar_lidar_tcc, cloudsat_tcc, cloudsat_tcc2)
1559       endif
1560
1561       ! Store, when necessary
1562       if (associated(cospOUT%lidar_only_freq_cloud)) then
1563          cospOUT%lidar_only_freq_cloud(ij:ik,:) = lidar_only_freq_cloud
1564       endif
1565       if (associated(cospOUT%radar_lidar_tcc)) then
1566          cospOUT%radar_lidar_tcc(ij:ik) = radar_lidar_tcc
1567       endif
1568       if (associated(cospOUT%cloudsat_tcc)) then
1569          cospOUT%cloudsat_tcc(ij:ik) = cloudsat_tcc
1570       endif
1571       if (associated(cospOUT%cloudsat_tcc2)) then
1572          cospOUT%cloudsat_tcc2(ij:ik) = cloudsat_tcc2
1573       endif
1574    endif
1575
1576    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1577    ! 7) Cleanup
1578    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1579    if (Lisccp_subcolumn .or. Lmodis_subcolumn) then
1580       nullify(isccpIN%Npoints,isccpIN%Ncolumns,isccpIN%Nlevels,isccpIN%emsfc_lw,        &
1581               isccpIN%skt,isccpIN%qv,isccpIN%at,isccpIN%frac_out,isccpIN%dtau,          &
1582               isccpIN%dem,isccpIN%phalf,isccpIN%sunlit,isccpIN%pfull)
1583    endif
1584
1585    if (Lmisr_subcolumn) then
1586       nullify(misrIN%Npoints,misrIN%Ncolumns,misrIN%Nlevels,misrIN%dtau,misrIN%sunlit,  &
1587               misrIN%zfull,misrIN%at)
1588    endif
1589
1590    if (Lcalipso_subcolumn) then
1591       nullify(calipsoIN%Npoints,calipsoIN%Ncolumns,calipsoIN%Nlevels,calipsoIN%beta_mol,&
1592               calipsoIN%betatot,calipsoIN%betatot_liq,calipsoIN%betatot_ice,            &
1593               calipsoIN%tau_mol,calipsoIN%tautot,calipsoIN%tautot_liq,calipsoIN%tautot_ice)
1594    endif
1595
1596    if (LgrLidar532_subcolumn) then
1597       nullify(grLidar532IN%Npoints,grLidar532IN%Ncolumns,grLidar532IN%Nlevels,grLidar532IN%beta_mol, &
1598               grLidar532IN%betatot,grLidar532IN%tau_mol,grLidar532IN%tautot)
1599    endif
1600
1601    if (Latlid_subcolumn) then
1602       nullify(atlidIN%Npoints,atlidIN%Ncolumns,atlidIN%Nlevels,atlidIN%beta_mol_atlid, &
1603               atlidIN%betatot_atlid,atlidIN%tau_mol_atlid,atlidIN%tautot_atlid)
1604    endif
1605
1606    if (Lparasol_subcolumn) then
1607       nullify(parasolIN%Npoints,parasolIN%Nlevels,parasolIN%Ncolumns,parasolIN%Nrefl,   &
1608            parasolIN%tautot_S_liq,parasolIN%tautot_S_ice)
1609    endif
1610
1611
1612    if (Lcloudsat_subcolumn) then
1613       nullify(cloudsatIN%Npoints,cloudsatIN%Nlevels,cloudsatIN%Ncolumns,cloudsatIN%rcfg,&
1614               cloudsatIN%kr_vol,cloudsatIN%g_vol,cloudsatIN%z_vol,cloudsatIN%hgt_matrix)
1615    endif
1616
1617    if (Lmodis_subcolumn) then
1618       nullify(modisIN%Npoints,modisIN%Ncolumns,modisIN%Nlevels,modisIN%tau,modisIN%g,   &
1619               modisIN%liqFrac,modisIN%w0)
1620       if (allocated(modisIN%sunlit))    deallocate(modisIN%sunlit)
1621       if (allocated(modisIN%notSunlit)) deallocate(modisIN%notSunlit)
1622       if (allocated(modisIN%pres))      deallocate(modisIN%pres)
1623    endif
1624
1625    if (allocated(calipso_beta_tot))      deallocate(calipso_beta_tot)
1626    if (allocated(grLidar532_beta_tot))  deallocate(grLidar532_beta_tot)
1627    if (allocated(atlid_beta_tot))        deallocate(atlid_beta_tot)
1628    if (allocated(calipso_beta_mol))      deallocate(calipso_beta_mol)
1629    if (allocated(grLidar532_beta_mol))  deallocate(grLidar532_beta_mol)
1630    if (allocated(atlid_beta_mol))        deallocate(atlid_beta_mol)
1631    if (allocated(calipso_betaperp_tot))  deallocate(calipso_betaperp_tot)
1632    if (allocated(cloudsatDBZe))          deallocate(cloudsatDBZe)
1633    if (allocated(lidar_only_freq_cloud)) deallocate(lidar_only_freq_cloud)
1634    if (allocated(radar_lidar_tcc))       deallocate(radar_lidar_tcc)
1635    if (allocated(cloudsat_tcc))          deallocate(cloudsat_tcc)
1636    if (allocated(cloudsat_tcc2))         deallocate(cloudsat_tcc2)
1637
1638  end function COSP_SIMULATOR
1639  ! ######################################################################################
1640  ! SUBROUTINE cosp_init
1641  ! ######################################################################################
1642  SUBROUTINE COSP_INIT(Lisccp, Lmodis, Lmisr, Lcloudsat, Lcalipso, LgrLidar532, Latlid, Lparasol, Lrttov,     &
1643       cloudsat_radar_freq, cloudsat_k2, cloudsat_use_gas_abs, cloudsat_do_ray,          &
1644       isccp_top_height, isccp_top_height_direction, surface_radar, rcfg, lusevgrid,     &
1645       luseCSATvgrid, Nvgrid, Nlevels, cloudsat_micro_scheme)
1646
1647    ! INPUTS
1648    logical,intent(in) :: Lisccp,Lmodis,Lmisr,Lcloudsat,Lcalipso,LgrLidar532,Latlid,Lparasol,Lrttov
1649    integer,intent(in)  :: &
1650         cloudsat_use_gas_abs,       & !
1651         cloudsat_do_ray,            & !
1652         isccp_top_height,           & !
1653         isccp_top_height_direction, & !
1654         Nlevels,                    & !
1655         Nvgrid,                     & ! Number of levels for new L3 grid
1656         surface_radar                 !
1657    real(wp),intent(in) :: &
1658         cloudsat_radar_freq,        & !
1659         cloudsat_k2                   !
1660    logical,intent(in) :: &
1661         lusevgrid,                  & ! Switch to use different vertical grid
1662         luseCSATvgrid                 ! Switch to use CLOUDSAT grid spacing for new
1663                                       ! vertical grid
1664    character(len=64),intent(in) :: &
1665       cloudsat_micro_scheme           ! Microphysical scheme used by CLOUDSAT
1666
1667    ! OUTPUTS
1668    type(radar_cfg) :: rcfg
1669
1670    ! Local variables
1671    integer  :: i
1672    real(wp) :: zstep
1673
1674    ! Initialize MODIS optical-depth bin boundaries for joint-histogram. (defined in cosp_config.F90)
1675    if (.not. allocated(modis_histTau)) then
1676       allocate(modis_histTau(ntau+1),modis_histTauEdges(2,ntau),modis_histTauCenters(ntau))
1677       numMODISTauBins      = ntau
1678       modis_histTau        = tau_binBounds
1679       modis_histTauEdges   = tau_binEdges
1680       modis_histTauCenters = tau_binCenters
1681    endif
1682
1683    ! Set up vertical grid used by CALIPSO and CLOUDSAT L3
1684    use_vgrid = lusevgrid
1685
1686    if (use_vgrid) then
1687      Nlvgrid  = Nvgrid
1688       allocate(vgrid_zl(Nlvgrid),vgrid_zu(Nlvgrid),vgrid_z(Nlvgrid))
1689       ! CloudSat grid requested
1690       if (luseCSATvgrid)       zstep = 480._wp
1691       ! Other grid requested. Constant vertical spacing with top at 20 km
1692       if (.not. luseCSATvgrid) zstep = 20000._wp/Nvgrid
1693       do i=1,Nvgrid
1694          vgrid_zl(Nlvgrid-i+1) = (i-1)*zstep
1695          vgrid_zu(Nlvgrid-i+1) = i*zstep
1696       enddo
1697       vgrid_z = (vgrid_zl+vgrid_zu)/2._wp
1698    else
1699       Nlvgrid = Nlevels
1700       allocate(vgrid_zl(Nlvgrid),vgrid_zu(Nlvgrid),vgrid_z(Nlvgrid))
1701    endif
1702
1703    ! Initialize simulators
1704    if (Lisccp) call cosp_isccp_init(isccp_top_height,isccp_top_height_direction)
1705    if (Lmodis) call cosp_modis_init()
1706    if (Lmisr)  call cosp_misr_init()
1707!    if (Lrttov) call cosp_rttov_init()
1708    if (Lcloudsat) call cosp_cloudsat_init(cloudsat_radar_freq,cloudsat_k2,              &
1709         cloudsat_use_gas_abs,cloudsat_do_ray,R_UNDEF,N_HYDRO, surface_radar,            &
1710         rcfg,cloudsat_micro_scheme)
1711    if (Lcalipso) call cosp_calipso_init()
1712    if (LgrLidar532) call cosp_grLidar532_init()
1713    if (Latlid) call cosp_atlid_init()
1714    if (Lparasol) call cosp_parasol_init()
1715
1716    linitialization = .FALSE.
1717  END SUBROUTINE COSP_INIT
1718
1719  !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1720  ! SUBROUTINE cosp_cleanUp
1721  !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1722  subroutine cosp_cleanUp()
1723    deallocate(vgrid_zl,vgrid_zu,vgrid_z)
1724  end subroutine cosp_cleanUp
1725
1726  !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727  ! SUBROUTINE cosp_errorCheck
1728  !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1729  subroutine cosp_errorCheck(cospgridIN, cospIN, Lisccp_subcolumn, Lisccp_column,           &
1730       Lmisr_subcolumn, Lmisr_column, Lmodis_subcolumn, Lmodis_column, Lcloudsat_subcolumn, &
1731       Lcloudsat_column, Lcalipso_subcolumn, Lcalipso_column, Latlid_subcolumn,             &
1732       Latlid_column, LgrLidar532_subcolumn, LgrLidar532_column, Lrttov_subcolumn,        &
1733       Lrttov_column, Lparasol_subcolumn, Lparasol_column, Lradar_lidar_tcc,                &
1734       Llidar_only_freq_cloud, Lcloudsat_tcc, Lcloudsat_tcc2, cospOUT, errorMessage, nError)
1735   
1736    ! Inputs
1737    type(cosp_column_inputs),intent(in) :: &
1738         cospgridIN       ! Model grid inputs to COSP
1739    type(cosp_optical_inputs),intent(in) :: &
1740         cospIN           ! Derived (optical) input to COSP
1741   
1742    ! Outputs
1743    logical,intent(inout) :: &
1744         Lisccp_subcolumn,    & ! ISCCP subcolumn simulator on/off switch
1745         Lisccp_column,       & ! ISCCP column simulator on/off switch
1746         Lmisr_subcolumn,     & ! MISR subcolumn simulator on/off switch
1747         Lmisr_column,        & ! MISR column simulator on/off switch
1748         Lmodis_subcolumn,    & ! MODIS subcolumn simulator on/off switch
1749         Lmodis_column,       & ! MODIS column simulator on/off switch
1750         Lcloudsat_subcolumn, & ! CLOUDSAT subcolumn simulator on/off switch
1751         Lcloudsat_column,    & ! CLOUDSAT column simulator on/off switch
1752         Lcalipso_subcolumn,  & ! CALIPSO subcolumn simulator on/off switch
1753         Lcalipso_column,     & ! CALIPSO column simulator on/off switch
1754         Latlid_subcolumn,    & ! EarthCare subcolumn simulator on/off switch
1755         Latlid_column,       & ! EarthCare column simulator on/off switch
1756         LgrLidar532_subcolumn, & ! Ground Lidar subcolumn simulator on/off switch
1757         LgrLidar532_column, & ! Ground Lidar column simulator on/off switch
1758         Lparasol_subcolumn,  & ! PARASOL subcolumn simulator on/off switch
1759         Lparasol_column,     & ! PARASOL column simulator on/off switch
1760         Lrttov_subcolumn,    & ! RTTOV subcolumn simulator on/off switch
1761         Lrttov_column,       & ! RTTOV column simulator on/off switch
1762         Lcloudsat_tcc,       & !
1763         Lcloudsat_tcc2,      & !
1764         Lradar_lidar_tcc,    & ! On/Off switch for joint Calipso/Cloudsat product
1765         Llidar_only_freq_cloud ! On/Off switch for joint Calipso/Cloudsat product
1766    type(cosp_outputs),intent(inout) :: &
1767         cospOUT                ! COSP Outputs
1768    character(len=256),dimension(100) :: errorMessage
1769    integer,intent(out) :: nError
1770   
1771    ! Local variables
1772    character(len=100) :: parasolErrorMessage
1773    logical :: alloc_status
1774   
1775    nError = 0
1776    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1777    ! PART 0: Ensure that the inputs needed by the requested simulators are allocated.
1778    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1779    ! ISCCP simulator
1780    if (Lisccp_subcolumn .or. Lisccp_column) then
1781       alloc_status = .true.
1782       if (.not. allocated(cospgridIN%skt)) then
1783          nError=nError+1
1784          errorMessage(nError) = 'ERROR: COSP input variable (ISSCP simulator): cospgridIN%skt has not been allocated'
1785          alloc_status = .false.
1786       endif
1787       if (.not. allocated(cospgridIN%qv)) then
1788          nError=nError+1
1789          errorMessage(nError) = 'ERROR: COSP input variable (ISSCP simulator): cospgridIN%qv has not been allocated'
1790          alloc_status = .false.
1791       endif
1792       if (.not. allocated(cospgridIN%at)) then
1793          nError=nError+1
1794          errorMessage(nError) = 'ERROR: COSP input variable (ISSCP simulator): cospgridIN%at has not been allocated'
1795          alloc_status = .false.
1796       endif
1797       if (.not. allocated(cospIN%frac_out)) then
1798          nError=nError+1
1799          errorMessage(nError) = 'ERROR: COSP input variable (ISSCP simulator): cospIN%frac_out has not been allocated'
1800          alloc_status = .false.
1801       endif
1802       if (.not. allocated(cospIN%tau_067)) then
1803          nError=nError+1
1804          errorMessage(nError) = 'ERROR: COSP input variable (ISSCP simulator): cospIN%tau_067 has not been allocated'
1805          alloc_status = .false.
1806       endif
1807       if (.not. allocated(cospIN%emiss_11)) then
1808          nError=nError+1
1809          errorMessage(nError) = 'ERROR: COSP input variable (ISSCP simulator): cospIN%emiss_11 has not been allocated'
1810          alloc_status = .false.
1811       endif
1812       if (.not. allocated(cospgridIN%phalf)) then
1813          nError=nError+1
1814          errorMessage(nError) = 'ERROR: COSP input variable (ISSCP simulator): cospgridIN%phalf has not been allocated'
1815          alloc_status = .false.
1816       endif
1817       if (.not. allocated(cospgridIN%sunlit)) then
1818          nError=nError+1
1819          errorMessage(nError) = 'ERROR: COSP input variable (ISSCP simulator): cospgridIN%sunlit has not been allocated'
1820          alloc_status = .false.
1821       endif
1822       if (.not. allocated(cospgridIN%pfull)) then
1823          nError=nError+1
1824          errorMessage(nError) = 'ERROR: COSP input variable (ISSCP simulator): cospgridIN%pfull has not been allocated'
1825          alloc_status = .false.
1826       endif
1827       if (.not. alloc_status) then
1828          Lisccp_subcolumn = .false.
1829          Lisccp_column    = .false.
1830          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
1831          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
1832          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
1833          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
1834          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
1835          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
1836          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
1837          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
1838          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
1839       endif
1840    endif
1841   
1842    ! MISR simulator
1843    if (Lmisr_subcolumn .or. Lmisr_column) then
1844       alloc_status = .true.
1845       if (.not. allocated(cospIN%tau_067)) then
1846          nError=nError+1
1847          errorMessage(nError) = 'ERROR: COSP input variable (MISR simulator): cospIN%tau_067 has not been allocated'
1848          alloc_status = .false.
1849       endif
1850       if (.not. allocated(cospgridIN%sunlit)) then
1851          nError=nError+1
1852          errorMessage(nError) = 'ERROR: COSP input variable (MISR simulator): cospgridIN%sunlit has not been allocated'
1853          alloc_status = .false.
1854       endif
1855       if (.not. allocated(cospgridIN%hgt_matrix)) then
1856          nError=nError+1
1857          errorMessage(nError) = 'ERROR: COSP input variable (MISR simulator): cospgridIN%hgt_matrix has not been allocated'
1858          alloc_status = .false.
1859       endif
1860       if (.not. allocated(cospgridIN%at)) then
1861          nError=nError+1
1862          errorMessage(nError) = 'ERROR: COSP input variable (MISR simulator): cospgridIN%at has not been allocated'
1863          alloc_status = .false.
1864       endif
1865       if (.not. alloc_status) then
1866          Lmisr_subcolumn = .false.
1867          Lmisr_column    = .false.
1868          if (associated(cospOUT%misr_fq))                   cospOUT%misr_fq(:,:,:)                 = R_UNDEF
1869          if (associated(cospOUT%misr_dist_model_layertops)) cospOUT%misr_dist_model_layertops(:,:) = R_UNDEF
1870          if (associated(cospOUT%misr_meanztop))             cospOUT%misr_meanztop(:)               = R_UNDEF
1871          if (associated(cospOUT%misr_cldarea))              cospOUT%misr_cldarea(:)                = R_UNDEF         
1872       endif
1873    endif
1874
1875    ! EarthCare Lidar simulator.
1876    if (Latlid_subcolumn .or. Latlid_column) then
1877       alloc_status = .true.
1878       if (.not. allocated(cospIN%beta_mol_atlid)) then
1879          nError=nError+1
1880          errorMessage(nError) = 'ERROR: COSP input variable (EarthCare Lidar simulator): cospIN%beta_mol_atlid has not been allocated'
1881          alloc_status = .false.
1882       endif
1883       if (.not. allocated(cospIN%betatot_atlid)) then
1884          nError=nError+1
1885          errorMessage(nError) = 'ERROR: COSP input variable (EarthCare Lidar simulator): cospIN%betatot_atlid has not been allocated'
1886          alloc_status = .false.
1887       endif
1888       if (.not. allocated(cospIN%tau_mol_atlid)) then
1889          nError=nError+1
1890          errorMessage(nError) = 'ERROR: COSP input variable (EarthCare Lidar simulator): cospIN%tau_mol_atlid has not been allocated'
1891          alloc_status = .false.
1892       endif
1893       if (.not. allocated(cospIN%tautot_atlid)) then
1894          nError=nError+1
1895          errorMessage(nError) = 'ERROR: COSP input variable (EarthCare Lidar simulator): cospIN%tautot_atlid has not been allocated'
1896          alloc_status = .false.
1897       endif
1898       if (.not. alloc_status) then
1899          Latlid_subcolumn = .false.
1900          Latlid_column    = .false.
1901          if (associated(cospOUT%atlid_cfad_sr))  cospOUT%atlid_cfad_sr(:,:,:)  = R_UNDEF
1902          if (associated(cospOUT%atlid_lidarcld)) cospOUT%atlid_lidarcld(:,:)   = R_UNDEF
1903          if (associated(cospOUT%atlid_cldlayer)) cospOUT%atlid_cldlayer(:,:)   = R_UNDEF
1904          if (associated(cospOUT%atlid_beta_mol)) cospOUT%atlid_beta_mol(:,:)   = R_UNDEF
1905          if (associated(cospOUT%atlid_beta_tot)) cospOUT%atlid_beta_tot(:,:,:) = R_UNDEF
1906       endif
1907       
1908       ! EarthCare column simulator requires additional inputs not required by the subcolumn simulator.
1909       alloc_status = .true.
1910       if (.not. allocated(cospgridIN%hgt_matrix)) then
1911          nError=nError+1
1912          errorMessage(nError) = 'ERROR: COSP input variable (EarthCare Lidar simulator): cospgridIN%hgt_matrix has not been allocated'
1913          alloc_status = .false.
1914       endif
1915       if (.not. allocated(cospgridIN%hgt_matrix_half)) then
1916          nError=nError+1
1917          errorMessage(nError) = 'ERROR: COSP input variable (EarthCare Lidar simulator): cospgridIN%hgt_matrix_half has not been allocated'
1918          alloc_status = .false.
1919       endif
1920       if (.not. allocated(cospgridIN%phalf)) then
1921          nError=nError+1
1922          errorMessage(nError) = 'ERROR: COSP input variable (EarthCare Lidar simulator): cospgridIN%phalf has not been allocated'
1923          alloc_status = .false.
1924       endif
1925       if (.not. alloc_status) then
1926          Latlid_column  = .false.
1927          if (associated(cospOUT%atlid_cfad_sr)) cospOUT%atlid_cfad_sr(:,:,:) = R_UNDEF
1928       endif
1929    endif
1930
1931    ! Ground Lidar simulator.
1932    if (LgrLidar532_subcolumn .or. LgrLidar532_column) then
1933       alloc_status = .true.
1934       if (.not. allocated(cospIN%beta_mol_grLidar532)) then
1935          nError=nError+1
1936          errorMessage(nError) = 'ERROR: COSP input variable (Ground Lidar simulator): cospIN%beta_mol_grLidar532 has not been allocated'
1937          alloc_status = .false.
1938       endif
1939       if (.not. allocated(cospIN%betatot_grLidar532)) then
1940          nError=nError+1
1941          errorMessage(nError) = 'ERROR: COSP input variable (Ground Lidar simulator): cospIN%betatot_grLidar532 has not been allocated'
1942          alloc_status = .false.
1943       endif
1944       if (.not. allocated(cospIN%tau_mol_grLidar532)) then
1945          nError=nError+1
1946          errorMessage(nError) = 'ERROR: COSP input variable (Ground Lidar simulator): cospIN%tau_mol_grLidar532 has not been allocated'
1947          alloc_status = .false.
1948       endif
1949       if (.not. allocated(cospIN%tautot_grLidar532)) then
1950          nError=nError+1
1951          errorMessage(nError) = 'ERROR: COSP input variable (Ground Lidar simulator): cospIN%tautot_grLidar532 has not been allocated'
1952          alloc_status = .false.
1953       endif
1954       if (.not. alloc_status) then
1955          LgrLidar532_subcolumn = .false.
1956          LgrLidar532_column    = .false.
1957          if (associated(cospOUT%grLidar532_cfad_sr))  cospOUT%grLidar532_cfad_sr(:,:,:)  = R_UNDEF
1958          if (associated(cospOUT%grLidar532_lidarcld)) cospOUT%grLidar532_lidarcld(:,:)   = R_UNDEF
1959          if (associated(cospOUT%grLidar532_cldlayer)) cospOUT%grLidar532_cldlayer(:,:)   = R_UNDEF
1960          if (associated(cospOUT%grLidar532_beta_mol)) cospOUT%grLidar532_beta_mol(:,:)   = R_UNDEF
1961          if (associated(cospOUT%grLidar532_beta_tot)) cospOUT%grLidar532_beta_tot(:,:,:) = R_UNDEF
1962       endif
1963       
1964       ! Ground Lidar column simulator requires additional inputs not required by the subcolumn simulator.
1965       alloc_status = .true.
1966       if (.not. allocated(cospgridIN%hgt_matrix)) then
1967          nError=nError+1
1968          errorMessage(nError) = 'ERROR: COSP input variable (Ground Lidar simulator): cospgridIN%hgt_matrix has not been allocated'
1969          alloc_status = .false.
1970       endif
1971       if (.not. allocated(cospgridIN%hgt_matrix_half)) then
1972          nError=nError+1
1973          errorMessage(nError) = 'ERROR: COSP input variable (Ground Lidar simulator): cospgridIN%hgt_matrix_half has not been allocated'
1974          alloc_status = .false.
1975       endif
1976       if (.not. allocated(cospgridIN%phalf)) then
1977          nError=nError+1
1978          errorMessage(nError) = 'ERROR: COSP input variable (Ground Lidar simulator): cospgridIN%phalf has not been allocated'
1979          alloc_status = .false.
1980       endif
1981       if (.not. alloc_status) then
1982          LgrLidar532_column  = .false.
1983          if (associated(cospOUT%grLidar532_cfad_sr)) cospOUT%grLidar532_cfad_sr(:,:,:) = R_UNDEF
1984       endif
1985    endif
1986   
1987    ! Calipso Lidar simulator
1988    if (Lcalipso_subcolumn .or. Lcalipso_column) then
1989       alloc_status = .true.
1990       if (.not. allocated(cospIN%beta_mol_calipso)) then
1991          nError=nError+1
1992          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator): cospIN%beta_mol_calipso has not been allocated'
1993          alloc_status = .false.
1994       endif
1995       if (.not. allocated(cospIN%betatot_calipso)) then
1996          nError=nError+1
1997          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator): cospIN%betatot_calipso has not been allocated'
1998          alloc_status = .false.
1999       endif
2000       if (.not. allocated(cospIN%betatot_liq_calipso)) then
2001          nError=nError+1
2002          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator):'//&
2003               ' cospIN%betatot_liq_calipso has not been allocated'
2004          alloc_status = .false.
2005       endif
2006       if (.not. allocated(cospIN%betatot_ice_calipso)) then
2007          nError=nError+1
2008          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator):'//&
2009               ' cospIN%betatot_ice_calipso has not been allocated'
2010          alloc_status = .false.
2011       endif
2012       if (.not. allocated(cospIN%tau_mol_calipso)) then
2013          nError=nError+1
2014          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator): cospIN%tau_mol_calipso has not been allocated'
2015          alloc_status = .false.
2016       endif
2017       if (.not. allocated(cospIN%tautot_calipso)) then
2018          nError=nError+1
2019          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator): cospIN%tautot_calipso has not been allocated'
2020          alloc_status = .false.
2021       endif
2022       if (.not. allocated(cospIN%tautot_liq_calipso)) then
2023          nError=nError+1
2024          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator):'//&
2025               ' cospIN%tautot_liq has not been allocated'
2026          alloc_status = .false.
2027       endif
2028       if (.not. allocated(cospIN%tautot_ice_calipso)) then
2029          nError=nError+1
2030          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator):'//&
2031               ' cospIN%tautot_ice has not been allocated'
2032          alloc_status = .false.
2033       endif
2034       if (.not. alloc_status) then
2035          Lcalipso_subcolumn = .false.
2036          Lcalipso_column    = .false.
2037          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
2038          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
2039          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
2040          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
2041          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
2042          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
2043          if (associated(cospOUT%calipso_beta_mol))      cospOUT%calipso_beta_mol(:,:)        = R_UNDEF
2044          if (associated(cospOUT%calipso_beta_tot))      cospOUT%calipso_beta_tot(:,:,:)      = R_UNDEF
2045          if (associated(cospOUT%calipso_betaperp_tot))  cospOUT%calipso_betaperp_tot(:,:,:)  = R_UNDEF
2046          ! Also, turn-off joint-products
2047          if (Lradar_lidar_tcc) then
2048             Lradar_lidar_tcc = .false.
2049             if (associated(cospOUT%radar_lidar_tcc)) cospOUT%radar_lidar_tcc(:) = R_UNDEF
2050          endif
2051          if (Lcloudsat_tcc) then
2052             Lcloudsat_tcc = .false.
2053             if (associated(cospOUT%cloudsat_tcc)) cospOUT%cloudsat_tcc(:) = R_UNDEF
2054          endif
2055          if (Lcloudsat_tcc2) then
2056             Lcloudsat_tcc2 = .false.
2057             if (associated(cospOUT%cloudsat_tcc2)) cospOUT%cloudsat_tcc2(:) = R_UNDEF
2058          endif
2059          if (Llidar_only_freq_cloud) then
2060             Llidar_only_freq_cloud = .false.
2061             if (associated(cospOUT%lidar_only_freq_cloud)) cospOUT%lidar_only_freq_cloud(:,:) = R_UNDEF
2062          endif
2063       endif
2064       
2065       ! Calipso column simulator requires additional inputs not required by the subcolumn simulator.
2066       alloc_status = .true.
2067       if (.not. allocated(cospgridIN%hgt_matrix)) then
2068          nError=nError+1
2069          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator):'//&
2070               ' cospgridIN%hgt_matrix has not been allocated'
2071          alloc_status = .false.
2072       endif
2073       if (.not. allocated(cospgridIN%hgt_matrix_half)) then
2074          nError=nError+1
2075          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator):'//&
2076               ' cospgridIN%hgt_matrix_half has not been allocated'
2077          alloc_status = .false.
2078       endif
2079       if (.not. allocated(cospgridIN%at)) then
2080          nError=nError+1
2081          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator): cospgridIN%at has not been allocated'
2082          alloc_status = .false.
2083       endif
2084       if (.not. allocated(cospgridIN%phalf)) then
2085          nError=nError+1
2086          errorMessage(nError) = 'ERROR: COSP input variable (Calipso Lidar simulator): cospgridIN%phalf has not been allocated'
2087          alloc_status = .false.
2088       endif
2089       if (.not. alloc_status) then
2090          Lcalipso_column  = .false.
2091          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
2092          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
2093          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
2094          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
2095          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
2096          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
2097          if (Lcloudsat_tcc) then
2098             Lcloudsat_tcc = .false.
2099             if (associated(cospOUT%cloudsat_tcc)) cospOUT%cloudsat_tcc(:) = R_UNDEF
2100          endif
2101          if (Lcloudsat_tcc2) then
2102             Lcloudsat_tcc2 = .false.
2103             if (associated(cospOUT%cloudsat_tcc2)) cospOUT%cloudsat_tcc2(:) = R_UNDEF
2104          endif
2105          ! Also, turn-off joint-products
2106          if (Lradar_lidar_tcc) then
2107             Lradar_lidar_tcc = .false.
2108             if (associated(cospOUT%radar_lidar_tcc)) cospOUT%radar_lidar_tcc(:) = R_UNDEF
2109          endif
2110          if (Llidar_only_freq_cloud) then
2111             Llidar_only_freq_cloud = .false.
2112             if (associated(cospOUT%lidar_only_freq_cloud)) cospOUT%lidar_only_freq_cloud(:,:) = R_UNDEF
2113          endif
2114       endif
2115    endif
2116   
2117    ! PARASOL simulator
2118    if (Lparasol_subcolumn .or. Lparasol_column) then
2119       alloc_status = .true.
2120       if (.not. allocated(cospIN%tautot_S_liq)) then
2121          nError=nError+1
2122          errorMessage(nError) = 'ERROR: COSP input variable (PARASOL simulator): cospIN%tautot_S_liq has not been allocated'
2123          alloc_status = .false.
2124       endif
2125       if (.not. allocated(cospIN%tautot_S_ice)) then
2126          nError=nError+1
2127          errorMessage(nError) = 'ERROR: COSP input variable (PARASOL simulator): cospIN%tautot_S_ice has not been allocated'
2128          alloc_status = .false.
2129       endif
2130       if (.not. alloc_status) then
2131          Lparasol_subcolumn  = .false.
2132          Lparasol_column     = .false.
2133          if (associated(cospOUT%parasolPix_refl))  cospOUT%parasolPix_refl(:,:,:) = R_UNDEF
2134          if (associated(cospOUT%parasolGrid_refl)) cospOUT%parasolGrid_refl(:,:)  = R_UNDEF
2135       endif
2136       
2137       ! PARASOL column simulator requires additional inputs not required by the subcolumn simulator.
2138       alloc_status = .true.
2139       if (.not. allocated(cospgridIN%land)) then
2140          nError=nError+1
2141          errorMessage(nError) = 'ERROR: COSP input variable (PARASOL simulator): cospgridIN%land has not been allocated'
2142          alloc_status = .false.
2143       endif
2144       if (.not. alloc_status) then
2145          Lparasol_column  = .false.
2146          if (associated(cospOUT%parasolGrid_refl)) cospOUT%parasolGrid_refl(:,:)  = R_UNDEF
2147       endif
2148    endif
2149   
2150    ! Cloudsat radar simulator
2151    if (Lcloudsat_subcolumn .or. Lcloudsat_column) then
2152       alloc_status = .true.
2153       if (.not. allocated(cospIN%z_vol_cloudsat)) then
2154          nError=nError+1
2155          errorMessage(nError) = 'ERROR: COSP input variable (Cloudsat radar simulator):'//&
2156               ' cospIN%z_vol_cloudsat has not been allocated'
2157          alloc_status = .false.
2158       endif
2159       if (.not. allocated(cospIN%kr_vol_cloudsat)) then
2160          nError=nError+1
2161          errorMessage(nError) = 'ERROR: COSP input variable (Cloudsat radar simulator):'//&
2162               ' cospIN%kr_vol_cloudsat has not been allocated'
2163          alloc_status = .false.
2164       endif
2165       if (.not. allocated(cospIN%g_vol_cloudsat)) then
2166          nError=nError+1
2167          errorMessage(nError) = 'ERROR: COSP input variable (Cloudsat radar simulator):'//&
2168               ' cospIN%g_vol_cloudsat has not been allocated'
2169          alloc_status = .false.
2170       endif
2171       if (.not. allocated(cospgridIN%hgt_matrix)) then
2172          nError=nError+1
2173          errorMessage(nError) = 'ERROR: COSP input variable (Cloudsat radar simulator):'//&
2174               ' cospgridIN%hgt_matrix has not been allocated'
2175          alloc_status = .false.
2176       endif
2177       if (.not. alloc_status) then
2178          Lcloudsat_subcolumn  = .false.
2179          Lcloudsat_column     = .false.
2180          if (associated(cospOUT%cloudsat_cfad_ze)) cospOUT%cloudsat_cfad_ze(:,:,:) = R_UNDEF
2181          if (associated(cospOUT%cloudsat_Ze_tot))  cospOUT%cloudsat_Ze_tot(:,:,:)  = R_UNDEF
2182          if (Lcloudsat_tcc) then
2183             Lcloudsat_tcc = .false.
2184             if (associated(cospOUT%cloudsat_tcc)) cospOUT%cloudsat_tcc(:) = R_UNDEF
2185          endif
2186          if (Lcloudsat_tcc2) then
2187             Lcloudsat_tcc2 = .false.
2188             if (associated(cospOUT%cloudsat_tcc2)) cospOUT%cloudsat_tcc2(:) = R_UNDEF
2189          endif
2190          ! Also, turn-off joint-products
2191          if (Lradar_lidar_tcc) then
2192             Lradar_lidar_tcc = .false.
2193             if (associated(cospOUT%radar_lidar_tcc)) cospOUT%radar_lidar_tcc(:) = R_UNDEF
2194          endif
2195          if (Llidar_only_freq_cloud) then
2196             Llidar_only_freq_cloud = .false.
2197             if (associated(cospOUT%lidar_only_freq_cloud)) cospOUT%lidar_only_freq_cloud(:,:) = R_UNDEF
2198          endif
2199       endif
2200       
2201       ! Cloudsat column simulator requires additional inputs not required by the subcolumn simulator.
2202       alloc_status = .true.
2203       if (.not. allocated(cospgridIN%hgt_matrix_half)) then
2204          nError=nError+1
2205          errorMessage(nError) = 'ERROR: COSP input variable (Cloudsat radar simulator):'//&
2206               ' cospgridIN%hgt_matrix_half has not been allocated'
2207          alloc_status = .false.
2208       endif
2209       if (.not. alloc_status) then
2210          Lcloudsat_column  = .false.
2211          if (associated(cospOUT%cloudsat_cfad_ze))      cospOUT%cloudsat_cfad_ze(:,:,:)    = R_UNDEF
2212          if (Lcloudsat_tcc) then
2213             Lcloudsat_tcc = .false.
2214             if (associated(cospOUT%cloudsat_tcc)) cospOUT%cloudsat_tcc(:) = R_UNDEF
2215          endif
2216          if (Lcloudsat_tcc2) then
2217             Lcloudsat_tcc2 = .false.
2218             if (associated(cospOUT%cloudsat_tcc2)) cospOUT%cloudsat_tcc2(:) = R_UNDEF
2219          endif
2220          ! Also, turn-off joint-products
2221          if (Lradar_lidar_tcc) then
2222             Lradar_lidar_tcc = .false.
2223             if (associated(cospOUT%radar_lidar_tcc)) cospOUT%radar_lidar_tcc(:) = R_UNDEF
2224          endif
2225          if (Llidar_only_freq_cloud) then
2226             Llidar_only_freq_cloud = .false.
2227             if (associated(cospOUT%lidar_only_freq_cloud)) cospOUT%lidar_only_freq_cloud(:,:) = R_UNDEF
2228          endif
2229       endif
2230    endif
2231   
2232    ! MODIS simulator
2233    if (Lmodis_subcolumn .or. Lmodis_column) then
2234       alloc_status = .true.
2235       if (.not. allocated(cospIN%fracLiq)) then
2236          nError=nError+1
2237          errorMessage(nError) = 'ERROR: COSP input variable (MODIS simulator): cospIN%fracLiq has not been allocated'
2238          alloc_status = .false.
2239       endif
2240       if (.not. allocated(cospIN%tau_067)) then
2241          nError=nError+1
2242          errorMessage(nError) = 'ERROR: COSP input variable (MODIS simulator): cospIN%tau_067 has not been allocated'
2243          alloc_status = .false.
2244       endif
2245       if (.not. allocated(cospIN%asym)) then
2246          nError=nError+1
2247          errorMessage(nError) = 'ERROR: COSP input variable (MODIS simulator): cospIN%asym has not been allocated'
2248          alloc_status = .false.
2249       endif
2250       if (.not. allocated(cospIN%ss_alb)) then
2251          nError=nError+1
2252          errorMessage(nError) = 'ERROR: COSP input variable (MODIS simulator): cospIN%ss_alb has not been allocated'
2253          alloc_status = .false.
2254       endif
2255       if (.not. allocated(cospgridIN%sunlit)) then
2256          nError=nError+1
2257          errorMessage(nError) = 'ERROR: COSP input variable (MODIS simulator): cospgridIN%sunlit has not been allocated'
2258          alloc_status = .false.
2259       endif
2260       if (.not. alloc_status) then
2261          Lmodis_subcolumn  = .false.
2262          Lmodis_column     = .false.
2263          if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean))                          &
2264               cospOUT%modis_Cloud_Fraction_Total_Mean(:)                   = R_UNDEF
2265          if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean))                          &
2266               cospOUT%modis_Cloud_Fraction_Water_Mean(:)                   = R_UNDEF
2267          if (associated(cospOUT%modis_Cloud_Fraction_Ice_Mean))                            &
2268               cospOUT%modis_Cloud_Fraction_Ice_Mean(:)                     = R_UNDEF
2269          if (associated(cospOUT%modis_Cloud_Fraction_High_Mean))                           &
2270               cospOUT%modis_Cloud_Fraction_High_Mean(:)                    = R_UNDEF
2271          if (associated(cospOUT%modis_Cloud_Fraction_Mid_Mean))                            &
2272               cospOUT%modis_Cloud_Fraction_Mid_Mean(:)                     = R_UNDEF
2273          if (associated(cospOUT%modis_Cloud_Fraction_Low_Mean))                            &
2274               cospOUT%modis_Cloud_Fraction_Low_Mean(:)                     = R_UNDEF
2275          if (associated(cospOUT%modis_Optical_Thickness_Total_Mean))                       &
2276               cospOUT%modis_Optical_Thickness_Total_Mean(:)                = R_UNDEF
2277          if (associated(cospOUT%modis_Optical_Thickness_Water_Mean))                       &
2278               cospOUT%modis_Optical_Thickness_Water_Mean(:)                = R_UNDEF
2279          if (associated(cospOUT%modis_Optical_Thickness_Ice_Mean))                         &
2280               cospOUT%modis_Optical_Thickness_Ice_Mean(:)                  = R_UNDEF
2281          if (associated(cospOUT%modis_Optical_Thickness_Total_LogMean))                    &
2282               cospOUT%modis_Optical_Thickness_Total_LogMean(:)             = R_UNDEF
2283          if (associated(cospOUT%modis_Optical_Thickness_Water_LogMean))                    &
2284               cospOUT%modis_Optical_Thickness_Water_LogMean(:)             = R_UNDEF
2285          if (associated(cospOUT%modis_Optical_Thickness_Ice_LogMean))                      &
2286               cospOUT%modis_Optical_Thickness_Ice_LogMean(:)               = R_UNDEF
2287          if (associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean))                     &
2288               cospOUT%modis_Cloud_Particle_Size_Water_Mean(:)              = R_UNDEF
2289          if (associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean))                       &
2290               cospOUT%modis_Cloud_Particle_Size_Ice_Mean(:)                = R_UNDEF
2291          if (associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean))                      &
2292               cospOUT%modis_Cloud_Top_Pressure_Total_Mean(:)               = R_UNDEF
2293          if (associated(cospOUT%modis_Liquid_Water_Path_Mean))                             &
2294               cospOUT%modis_Liquid_Water_Path_Mean(:)                      = R_UNDEF
2295          if (associated(cospOUT%modis_Ice_Water_Path_Mean))                                &
2296               cospOUT%modis_Ice_Water_Path_Mean(:)                         = R_UNDEF
2297          if (associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure))            &
2298               cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(:,:,:) = R_UNDEF
2299          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffICE))                       &
2300               cospOUT%modis_Optical_Thickness_vs_ReffICE(:,:,:)            = R_UNDEF
2301          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffLIQ))                       &
2302               cospOUT%modis_Optical_Thickness_vs_ReffLIQ(:,:,:)            = R_UNDEF         
2303       endif
2304    endif
2305   
2306    ! RTTOV
2307    if (Lrttov_column) then
2308       alloc_status = .true.
2309       if (.not. allocated(cospgridIN%emis_sfc)) then
2310          nError=nError+1
2311          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%emis_sfc has not been allocated'
2312          alloc_status = .false.
2313       endif
2314       if (.not. allocated(cospgridIN%hgt_matrix_half)) then
2315          nError=nError+1
2316          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%emis_sfc has not been allocated'
2317          alloc_status = .false.
2318       endif
2319       if (.not. allocated(cospgridIN%u_sfc)) then
2320          nError=nError+1
2321          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%u_sfc has not been allocated'
2322          alloc_status = .false.
2323       endif
2324       if (.not. allocated(cospgridIN%v_sfc)) then
2325          nError=nError+1
2326          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%v_sfc has not been allocated'
2327          alloc_status = .false.
2328       endif
2329       if (.not. allocated(cospgridIN%skt)) then
2330          nError=nError+1
2331          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%skt has not been allocated'
2332          alloc_status = .false.
2333       endif
2334       if (.not. allocated(cospgridIN%phalf)) then
2335          nError=nError+1
2336          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%phalf has not been allocated'
2337          alloc_status = .false.
2338       endif
2339       if (.not. allocated(cospgridIN%qv)) then
2340          nError=nError+1
2341          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%qv has not been allocated'
2342          alloc_status = .false.
2343       endif
2344       if (.not. allocated(cospgridIN%at)) then
2345          nError=nError+1
2346          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%at has not been allocated'
2347          alloc_status = .false.
2348       endif
2349       if (.not. allocated(cospgridIN%land)) then
2350          nError=nError+1
2351          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%land has not been allocated'
2352          alloc_status = .false.
2353       endif
2354       if (.not. allocated(cospgridIN%lat)) then
2355          nError=nError+1
2356          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%lat has not been allocated'
2357          alloc_status = .false.
2358       endif
2359       if (.not. allocated(cospgridIN%lon)) then
2360          nError=nError+1
2361          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%lon has not been allocated'
2362          alloc_status = .false.
2363       endif
2364       if (.not. allocated(cospgridIN%seaice)) then
2365          nError=nError+1
2366          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%seaice has not been allocated'
2367          alloc_status = .false.
2368       endif
2369       if (.not. allocated(cospgridIN%pfull)) then
2370          nError=nError+1
2371          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%pfull has not been allocated'
2372          alloc_status = .false.
2373       endif
2374       if (.not. allocated(cospgridIN%phalf)) then
2375          nError=nError+1
2376          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%phalf has not been allocated'
2377          alloc_status = .false.
2378       endif
2379       if (.not. allocated(cospgridIN%at)) then
2380          nError=nError+1
2381          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%at has not been allocated'
2382          alloc_status = .false.
2383       endif
2384       if (.not. allocated(cospgridIN%qv)) then
2385          nError=nError+1
2386          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%qv has not been allocated'
2387          alloc_status = .false.
2388       endif
2389       if (.not. allocated(cospgridIN%o3)) then
2390          nError=nError+1
2391          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%o3 has not been allocated'
2392          alloc_status = .false.
2393       endif
2394       if (.not. allocated(cospgridIN%tca)) then
2395          nError=nError+1
2396          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%tca has not been allocated'
2397          alloc_status = .false.
2398       endif
2399       if (.not. allocated(cospgridIN%cloudIce)) then
2400          nError=nError+1
2401          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%cloudIce has not been allocated'
2402          alloc_status = .false.
2403       endif
2404       if (.not. allocated(cospgridIN%cloudLiq)) then
2405          nError=nError+1
2406          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%cloudLiq has not been allocated'
2407          alloc_status = .false.
2408       endif
2409       if (.not. allocated(cospgridIN%fl_rain)) then
2410          nError=nError+1
2411          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%fl_rain has not been allocated'
2412          alloc_status = .false.
2413       endif
2414       if (.not. allocated(cospgridIN%fl_snow)) then
2415          nError=nError+1
2416          errorMessage(nError) = 'ERROR: COSP input variable (RTTOV): cospgridIN%fl_snow has not been allocated'
2417          alloc_status = .false.
2418       endif
2419       if (.not. alloc_status) then
2420          Lrttov_column     = .false.
2421          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:)         = R_UNDEF         
2422       endif
2423    endif
2424   
2425    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2426    ! PART 1: Check input array values for out-of-bounds values. When an out-of-bound value
2427    !         is encountered, COSP outputs that are dependent on that input are filled with
2428    !         an undefined value (set in cosp_config.f90) and if necessary, that simulator
2429    !         is turned off.
2430    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2431    if (any([Lisccp_subcolumn, Lisccp_column, Lmisr_subcolumn, Lmisr_column, Lmodis_subcolumn, Lmodis_column])) then
2432       if (any(cospgridIN%sunlit .lt. 0)) then
2433          nError=nError+1
2434          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%sunlit contains values out of range (0 or 1)'
2435          Lisccp_subcolumn = .false.
2436          Lisccp_column    = .false.
2437          Lmisr_subcolumn  = .false.
2438          Lmisr_column     = .false.
2439          Lmodis_subcolumn = .false.
2440          Lmodis_column    = .false.
2441          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
2442          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
2443          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
2444          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
2445          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
2446          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
2447          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
2448          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
2449          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
2450          if (associated(cospOUT%misr_fq))                   cospOUT%misr_fq(:,:,:)                 = R_UNDEF
2451          if (associated(cospOUT%misr_dist_model_layertops)) cospOUT%misr_dist_model_layertops(:,:) = R_UNDEF
2452          if (associated(cospOUT%misr_meanztop))             cospOUT%misr_meanztop(:)               = R_UNDEF
2453          if (associated(cospOUT%misr_cldarea))              cospOUT%misr_cldarea(:)                = R_UNDEF
2454          if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean))                          &
2455               cospOUT%modis_Cloud_Fraction_Total_Mean(:)                   = R_UNDEF
2456          if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean))                          &
2457               cospOUT%modis_Cloud_Fraction_Water_Mean(:)                   = R_UNDEF
2458          if (associated(cospOUT%modis_Cloud_Fraction_Ice_Mean))                            &
2459               cospOUT%modis_Cloud_Fraction_Ice_Mean(:)                     = R_UNDEF
2460          if (associated(cospOUT%modis_Cloud_Fraction_High_Mean))                           &
2461               cospOUT%modis_Cloud_Fraction_High_Mean(:)                    = R_UNDEF
2462          if (associated(cospOUT%modis_Cloud_Fraction_Mid_Mean))                            &
2463               cospOUT%modis_Cloud_Fraction_Mid_Mean(:)                     = R_UNDEF
2464          if (associated(cospOUT%modis_Cloud_Fraction_Low_Mean))                            &
2465               cospOUT%modis_Cloud_Fraction_Low_Mean(:)                     = R_UNDEF
2466          if (associated(cospOUT%modis_Optical_Thickness_Total_Mean))                       &
2467               cospOUT%modis_Optical_Thickness_Total_Mean(:)                = R_UNDEF
2468          if (associated(cospOUT%modis_Optical_Thickness_Water_Mean))                       &
2469               cospOUT%modis_Optical_Thickness_Water_Mean(:)                = R_UNDEF
2470          if (associated(cospOUT%modis_Optical_Thickness_Ice_Mean))                         &
2471               cospOUT%modis_Optical_Thickness_Ice_Mean(:)                  = R_UNDEF
2472          if (associated(cospOUT%modis_Optical_Thickness_Total_LogMean))                    &
2473               cospOUT%modis_Optical_Thickness_Total_LogMean(:)             = R_UNDEF
2474          if (associated(cospOUT%modis_Optical_Thickness_Water_LogMean))                    &
2475               cospOUT%modis_Optical_Thickness_Water_LogMean(:)             = R_UNDEF
2476          if (associated(cospOUT%modis_Optical_Thickness_Ice_LogMean))                      &
2477               cospOUT%modis_Optical_Thickness_Ice_LogMean(:)               = R_UNDEF
2478          if (associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean))                     &
2479               cospOUT%modis_Cloud_Particle_Size_Water_Mean(:)              = R_UNDEF
2480          if (associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean))                       &
2481               cospOUT%modis_Cloud_Particle_Size_Ice_Mean(:)                = R_UNDEF
2482          if (associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean))                      &
2483               cospOUT%modis_Cloud_Top_Pressure_Total_Mean(:)               = R_UNDEF
2484          if (associated(cospOUT%modis_Liquid_Water_Path_Mean))                             &
2485               cospOUT%modis_Liquid_Water_Path_Mean(:)                      = R_UNDEF
2486          if (associated(cospOUT%modis_Ice_Water_Path_Mean))                                &
2487               cospOUT%modis_Ice_Water_Path_Mean(:)                         = R_UNDEF
2488          if (associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure))            &
2489               cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(:,:,:) = R_UNDEF
2490          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffICE))                       &
2491               cospOUT%modis_Optical_Thickness_vs_ReffICE(:,:,:)            = R_UNDEF
2492          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffLIQ))                       &
2493               cospOUT%modis_Optical_Thickness_vs_ReffLIQ(:,:,:)            = R_UNDEF
2494       endif
2495    endif
2496
2497    if (any([Lisccp_subcolumn, Lisccp_column, Lmisr_subcolumn, Lmisr_column, Lrttov_column,&
2498         Lcalipso_column, Lcloudsat_column, Lradar_lidar_tcc,Llidar_only_freq_cloud, &
2499         Lcloudsat_tcc, Lcloudsat_tcc2])) then
2500       if (any(cospgridIN%at .lt. 0)) then
2501          nError=nError+1
2502          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%at contains values out of range (at<0), expected units (K)'
2503          Lisccp_subcolumn = .false.
2504          Lisccp_column    = .false.
2505          Lmisr_subcolumn  = .false.
2506          Lmisr_column     = .false.
2507          Lrttov_column    = .false.
2508          Lcalipso_column  = .false.
2509          Lcloudsat_column = .false.
2510          Lradar_lidar_tcc = .false.
2511          Llidar_only_freq_cloud = .false.
2512          Lcloudsat_tcc    = .false.
2513          Lcloudsat_tcc2   = .false.
2514          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:)         = R_UNDEF
2515          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
2516          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
2517          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
2518          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
2519          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
2520          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
2521          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
2522          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
2523          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
2524          if (associated(cospOUT%misr_fq))                   cospOUT%misr_fq(:,:,:)                 = R_UNDEF
2525          if (associated(cospOUT%misr_dist_model_layertops)) cospOUT%misr_dist_model_layertops(:,:) = R_UNDEF
2526          if (associated(cospOUT%misr_meanztop))             cospOUT%misr_meanztop(:)               = R_UNDEF
2527          if (associated(cospOUT%misr_cldarea))              cospOUT%misr_cldarea(:)                = R_UNDEF
2528          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
2529          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
2530          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
2531          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
2532          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
2533          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
2534          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
2535          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
2536          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
2537          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
2538          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
2539          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
2540          if (associated(cospOUT%cloudsat_cfad_ze))      cospOUT%cloudsat_cfad_ze(:,:,:)      = R_UNDEF
2541          if (associated(cospOUT%lidar_only_freq_cloud)) cospOUT%lidar_only_freq_cloud(:,:)   = R_UNDEF
2542          if (associated(cospOUT%radar_lidar_tcc))       cospOUT%radar_lidar_tcc(:)           = R_UNDEF
2543          if (associated(cospOUT%cloudsat_tcc)) cospOUT%cloudsat_tcc(:) = R_UNDEF
2544          if (associated(cospOUT%cloudsat_tcc2)) cospOUT%cloudsat_tcc2(:) = R_UNDEF
2545       endif
2546    endif
2547    if (any([Lisccp_subcolumn, Lisccp_column, Lrttov_column])) then
2548       if (any(cospgridIN%pfull .lt. 0)) then
2549          nError=nError+1
2550          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%pfull contains values out of range'
2551          Lisccp_subcolumn = .false.
2552          Lisccp_column    = .false.
2553          Lrttov_column    = .false.
2554          if (associated(cospOUT%rttov_tbs))           cospOUT%rttov_tbs(:,:)         = R_UNDEF
2555          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
2556          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
2557          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
2558          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
2559          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
2560          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
2561          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
2562          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
2563          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
2564       endif
2565    endif
2566    if (any([Lisccp_subcolumn,Lisccp_column,Lmodis_subcolumn,Lmodis_column,Lcalipso_column,Lrttov_column,&
2567             LgrLidar532_column,Latlid_column])) then
2568       if (any(cospgridIN%phalf .lt. 0)) then
2569          nError=nError+1
2570          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%phalf contains values out of range'
2571          Lisccp_subcolumn = .false.
2572          Lisccp_column    = .false.
2573          Lmodis_subcolumn = .false.
2574          Lmodis_column    = .false.
2575          Lcalipso_column  = .false.
2576          Lrttov_column    = .false.
2577          Latlid_column    = .false.
2578          LgrLidar532_column = .false.
2579          if (associated(cospOUT%rttov_tbs))           cospOUT%rttov_tbs(:,:)         = R_UNDEF
2580          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
2581          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
2582          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
2583          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
2584          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
2585          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
2586          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
2587          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
2588          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
2589          if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean))                          &
2590               cospOUT%modis_Cloud_Fraction_Total_Mean(:)                   = R_UNDEF
2591          if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean))                          &
2592               cospOUT%modis_Cloud_Fraction_Water_Mean(:)                   = R_UNDEF
2593          if (associated(cospOUT%modis_Cloud_Fraction_Ice_Mean))                            &
2594               cospOUT%modis_Cloud_Fraction_Ice_Mean(:)                     = R_UNDEF
2595          if (associated(cospOUT%modis_Cloud_Fraction_High_Mean))                           &
2596               cospOUT%modis_Cloud_Fraction_High_Mean(:)                    = R_UNDEF
2597          if (associated(cospOUT%modis_Cloud_Fraction_Mid_Mean))                            &
2598               cospOUT%modis_Cloud_Fraction_Mid_Mean(:)                     = R_UNDEF
2599          if (associated(cospOUT%modis_Cloud_Fraction_Low_Mean))                            &
2600               cospOUT%modis_Cloud_Fraction_Low_Mean(:)                     = R_UNDEF
2601          if (associated(cospOUT%modis_Optical_Thickness_Total_Mean))                       &
2602               cospOUT%modis_Optical_Thickness_Total_Mean(:)                = R_UNDEF
2603          if (associated(cospOUT%modis_Optical_Thickness_Water_Mean))                       &
2604               cospOUT%modis_Optical_Thickness_Water_Mean(:)                = R_UNDEF
2605          if (associated(cospOUT%modis_Optical_Thickness_Ice_Mean))                         &
2606               cospOUT%modis_Optical_Thickness_Ice_Mean(:)                  = R_UNDEF
2607          if (associated(cospOUT%modis_Optical_Thickness_Total_LogMean))                    &
2608               cospOUT%modis_Optical_Thickness_Total_LogMean(:)             = R_UNDEF
2609          if (associated(cospOUT%modis_Optical_Thickness_Water_LogMean))                    &
2610               cospOUT%modis_Optical_Thickness_Water_LogMean(:)             = R_UNDEF
2611          if (associated(cospOUT%modis_Optical_Thickness_Ice_LogMean))                      &
2612               cospOUT%modis_Optical_Thickness_Ice_LogMean(:)               = R_UNDEF
2613          if (associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean))                     &
2614               cospOUT%modis_Cloud_Particle_Size_Water_Mean(:)              = R_UNDEF
2615          if (associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean))                       &
2616               cospOUT%modis_Cloud_Particle_Size_Ice_Mean(:)                = R_UNDEF
2617          if (associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean))                      &
2618               cospOUT%modis_Cloud_Top_Pressure_Total_Mean(:)               = R_UNDEF
2619          if (associated(cospOUT%modis_Liquid_Water_Path_Mean))                             &
2620               cospOUT%modis_Liquid_Water_Path_Mean(:)                      = R_UNDEF
2621          if (associated(cospOUT%modis_Ice_Water_Path_Mean))                                &
2622               cospOUT%modis_Ice_Water_Path_Mean(:)                         = R_UNDEF
2623          if (associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure))            &
2624               cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(:,:,:) = R_UNDEF
2625          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffICE))                       &
2626               cospOUT%modis_Optical_Thickness_vs_ReffICE(:,:,:)            = R_UNDEF
2627          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffLIQ))                       &
2628               cospOUT%modis_Optical_Thickness_vs_ReffLIQ(:,:,:)            = R_UNDEF
2629          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
2630          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
2631          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
2632          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
2633          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
2634          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
2635          if (associated(cospOUT%atlid_cfad_sr))         cospOUT%atlid_cfad_sr(:,:,:)         = R_UNDEF
2636          if (associated(cospOUT%atlid_lidarcld))        cospOUT%atlid_lidarcld(:,:)          = R_UNDEF
2637          if (associated(cospOUT%atlid_cldlayer))        cospOUT%atlid_cldlayer(:,:)          = R_UNDEF
2638          if (associated(cospOUT%grLidar532_cfad_sr))   cospOUT%grLidar532_cfad_sr(:,:,:)   = R_UNDEF
2639          if (associated(cospOUT%grLidar532_lidarcld))  cospOUT%grLidar532_lidarcld(:,:)    = R_UNDEF
2640          if (associated(cospOUT%grLidar532_cldlayer))  cospOUT%grLidar532_cldlayer(:,:)    = R_UNDEF
2641          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
2642          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
2643          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
2644          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
2645          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
2646          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
2647       endif
2648    endif
2649    if (any([Lisccp_subcolumn,Lisccp_column,Lrttov_column])) then
2650       if (any(cospgridIN%qv .lt. 0)) then
2651          nError=nError+1
2652          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%qv contains values out of range'
2653          Lisccp_subcolumn = .false.
2654          Lisccp_column    = .false.
2655          Lrttov_column    = .false.
2656          if (associated(cospOUT%rttov_tbs))           cospOUT%rttov_tbs(:,:)         = R_UNDEF
2657          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
2658          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
2659          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
2660          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
2661          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
2662          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
2663          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
2664          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
2665          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
2666       endif
2667    endif
2668    if (any([Lmisr_subcolumn,Lmisr_column,Lcloudsat_subcolumn,Lcloudsat_column,Lcalipso_column,Lradar_lidar_tcc,&
2669         Llidar_only_freq_cloud,LgrLidar532_column,Latlid_column,Lcloudsat_tcc, Lcloudsat_tcc2])) then
2670       if (any(cospgridIN%hgt_matrix .lt. -300)) then
2671          nError=nError+1
2672          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%hgt_matrix contains values out of range'
2673          Lmisr_subcolumn     = .false.
2674          Lmisr_column        = .false.
2675          Lcloudsat_subcolumn = .false.
2676          Lcloudsat_column    = .false.
2677          Lcalipso_column     = .false.
2678          Lradar_lidar_tcc    = .false.
2679          Llidar_only_freq_cloud = .false.
2680          Lcloudsat_tcc       = .false.
2681          Lcloudsat_tcc2      = .false.
2682          Latlid_column       = .false.
2683          LgrLidar532_column  = .false.
2684          if (associated(cospOUT%misr_fq))                   cospOUT%misr_fq(:,:,:)                 = R_UNDEF
2685          if (associated(cospOUT%misr_dist_model_layertops)) cospOUT%misr_dist_model_layertops(:,:) = R_UNDEF
2686          if (associated(cospOUT%misr_meanztop))             cospOUT%misr_meanztop(:)               = R_UNDEF
2687          if (associated(cospOUT%misr_cldarea))              cospOUT%misr_cldarea(:)                = R_UNDEF
2688          if (associated(cospOUT%calipso_cfad_sr))           cospOUT%calipso_cfad_sr(:,:,:)         = R_UNDEF
2689          if (associated(cospOUT%calipso_lidarcld))          cospOUT%calipso_lidarcld(:,:)          = R_UNDEF
2690          if (associated(cospOUT%calipso_lidarcldphase))     cospOUT%calipso_lidarcldphase(:,:,:)   = R_UNDEF
2691          if (associated(cospOUT%calipso_cldlayer))          cospOUT%calipso_cldlayer(:,:)          = R_UNDEF
2692          if (associated(cospOUT%calipso_cldlayerphase))     cospOUT%calipso_cldlayerphase(:,:,:)   = R_UNDEF
2693          if (associated(cospOUT%calipso_lidarcldtmp))       cospOUT%calipso_lidarcldtmp(:,:,:)     = R_UNDEF
2694          if (associated(cospOUT%cloudsat_cfad_ze))          cospOUT%cloudsat_cfad_ze(:,:,:)        = R_UNDEF
2695          if (associated(cospOUT%cloudsat_Ze_tot))           cospOUT%cloudsat_Ze_tot(:,:,:)         = R_UNDEF
2696          if (associated(cospOUT%lidar_only_freq_cloud))     cospOUT%lidar_only_freq_cloud(:,:)     = R_UNDEF
2697          if (associated(cospOUT%radar_lidar_tcc))           cospOUT%radar_lidar_tcc(:)             = R_UNDEF
2698          if (associated(cospOUT%cloudsat_tcc))              cospOUT%cloudsat_tcc(:)                = R_UNDEF
2699          if (associated(cospOUT%cloudsat_tcc2))             cospOUT%cloudsat_tcc2(:)               = R_UNDEF
2700          if (associated(cospOUT%atlid_cfad_sr))             cospOUT%atlid_cfad_sr(:,:,:)           = R_UNDEF
2701          if (associated(cospOUT%atlid_lidarcld))            cospOUT%atlid_lidarcld(:,:)            = R_UNDEF
2702          if (associated(cospOUT%atlid_cldlayer))            cospOUT%atlid_cldlayer(:,:)            = R_UNDEF
2703          if (associated(cospOUT%grLidar532_cfad_sr))        cospOUT%grLidar532_cfad_sr(:,:,:)      = R_UNDEF
2704          if (associated(cospOUT%grLidar532_lidarcld))       cospOUT%grLidar532_lidarcld(:,:)       = R_UNDEF
2705          if (associated(cospOUT%grLidar532_cldlayer))       cospOUT%grLidar532_cldlayer(:,:)       = R_UNDEF
2706          if (associated(cospOUT%calipso_lidarcldtype))      cospOUT%calipso_lidarcldtype(:,:,:)    = R_UNDEF
2707          if (associated(cospOUT%calipso_cldtype))           cospOUT%calipso_cldtype(:,:)           = R_UNDEF
2708          if (associated(cospOUT%calipso_cldtypetemp))       cospOUT%calipso_cldtypetemp(:,:)       = R_UNDEF
2709          if (associated(cospOUT%calipso_cldtypemeanz))      cospOUT%calipso_cldtypemeanz(:,:)      = R_UNDEF
2710          if (associated(cospOUT%calipso_cldtypemeanzse))    cospOUT%calipso_cldtypemeanzse(:,:)    = R_UNDEF
2711          if (associated(cospOUT%calipso_cldthinemis))       cospOUT%calipso_cldthinemis(:)         = R_UNDEF
2712       endif
2713    endif
2714    if (any([Lrttov_column,Lcloudsat_column,Lcalipso_column,Lradar_lidar_tcc,Llidar_only_freq_cloud, &
2715             LgrLidar532_column, Latlid_column, Lcloudsat_tcc, Lcloudsat_tcc2])) then
2716       if (any(cospgridIN%hgt_matrix_half .lt. -300)) then
2717          nError=nError+1
2718          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%hgt_matrix_half contains values out of range'
2719          Lrttov_column    = .false.
2720          Lcloudsat_column = .false.
2721          Lcalipso_column  = .false.
2722          Lradar_lidar_tcc = .false.
2723          Llidar_only_freq_cloud = .false.
2724          Lcloudsat_tcc    = .false.
2725          Lcloudsat_tcc2   = .false.
2726          Latlid_column       = .false.
2727          LgrLidar532_column = .false.
2728          if (associated(cospOUT%rttov_tbs))              cospOUT%rttov_tbs(:,:)               = R_UNDEF
2729          if (associated(cospOUT%calipso_cfad_sr))        cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
2730          if (associated(cospOUT%calipso_lidarcld))       cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
2731          if (associated(cospOUT%calipso_lidarcldphase))  cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
2732          if (associated(cospOUT%calipso_cldlayer))       cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
2733          if (associated(cospOUT%calipso_cldlayerphase))  cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
2734          if (associated(cospOUT%calipso_lidarcldtmp))    cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
2735          if (associated(cospOUT%cloudsat_cfad_ze))       cospOUT%cloudsat_cfad_ze(:,:,:)      = R_UNDEF
2736          if (associated(cospOUT%lidar_only_freq_cloud))  cospOUT%lidar_only_freq_cloud(:,:)   = R_UNDEF
2737          if (associated(cospOUT%radar_lidar_tcc))        cospOUT%radar_lidar_tcc(:)           = R_UNDEF
2738          if (associated(cospOUT%cloudsat_tcc))           cospOUT%cloudsat_tcc(:)              = R_UNDEF
2739          if (associated(cospOUT%cloudsat_tcc2))          cospOUT%cloudsat_tcc2(:)             = R_UNDEF         
2740          if (associated(cospOUT%atlid_cfad_sr))          cospOUT%atlid_cfad_sr(:,:,:)         = R_UNDEF
2741          if (associated(cospOUT%atlid_lidarcld))         cospOUT%atlid_lidarcld(:,:)          = R_UNDEF
2742          if (associated(cospOUT%atlid_cldlayer))         cospOUT%atlid_cldlayer(:,:)          = R_UNDEF
2743          if (associated(cospOUT%grLidar532_cfad_sr))     cospOUT%grLidar532_cfad_sr(:,:,:)    = R_UNDEF
2744          if (associated(cospOUT%grLidar532_lidarcld))    cospOUT%grLidar532_lidarcld(:,:)     = R_UNDEF
2745          if (associated(cospOUT%grLidar532_cldlayer))    cospOUT%grLidar532_cldlayer(:,:)     = R_UNDEF
2746          if (associated(cospOUT%calipso_lidarcldtype))   cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
2747          if (associated(cospOUT%calipso_cldtype))        cospOUT%calipso_cldtype(:,:)         = R_UNDEF
2748          if (associated(cospOUT%calipso_cldtypetemp))    cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
2749          if (associated(cospOUT%calipso_cldtypemeanz))   cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
2750          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:)  = R_UNDEF
2751          if (associated(cospOUT%calipso_cldthinemis))    cospOUT%calipso_cldthinemis(:)       = R_UNDEF
2752       endif
2753    endif
2754    if (any([Lrttov_column,Lcalipso_column,Lparasol_column])) then
2755       if (any(cospgridIN%land .lt. 0)) then
2756          nError=nError+1
2757          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%land contains values out of range'
2758          Lrttov_column    = .false.
2759          Lcalipso_column  = .false.
2760          Lparasol_column  = .false.
2761          if (associated(cospOUT%rttov_tbs))             cospOUT%rttov_tbs(:,:)               = R_UNDEF
2762          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
2763          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
2764          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
2765          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
2766          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
2767          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
2768          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
2769          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
2770          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
2771          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
2772          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
2773          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
2774          if (associated(cospOUT%parasolGrid_refl))      cospOUT%parasolGrid_refl(:,:)        = R_UNDEF
2775       endif
2776    endif
2777    if (any([Lisccp_subcolumn,Lisccp_column,Lrttov_column])) then
2778       if (any(cospgridIN%skt .lt. 0)) then
2779          nError=nError+1
2780          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%skt contains values out of range'
2781          Lisccp_subcolumn = .false.
2782          Lisccp_column    = .false.
2783          Lrttov_column    = .false.
2784          if (associated(cospOUT%rttov_tbs))           cospOUT%rttov_tbs(:,:)         = R_UNDEF
2785          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
2786          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
2787          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
2788          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
2789          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
2790          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
2791          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
2792          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
2793          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
2794       endif
2795    endif
2796   
2797    ! RTTOV Inputs
2798    if (Lrttov_column) then
2799       if (cospgridIN%zenang .lt. -90. .OR. cospgridIN%zenang .gt. 90) then
2800          nError=nError+1
2801          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%zenang contains values out of range'
2802          Lrttov_column = .false.
2803          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2804       endif
2805       if (cospgridIN%co2 .lt. 0) then
2806          nError=nError+1
2807          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%co2 contains values out of range'
2808          Lrttov_column = .false.
2809          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2810       endif
2811       if (cospgridIN%ch4 .lt. 0) then
2812          nError=nError+1
2813          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%ch4 contains values out of range'
2814          Lrttov_column = .false.
2815          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2816       endif
2817       if (cospgridIN%n2o .lt. 0) then
2818          nError=nError+1
2819          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%n2o contains values out of range'
2820          Lrttov_column = .false.
2821          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2822       endif
2823       if (cospgridIN%co.lt. 0) then
2824          nError=nError+1
2825          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%co contains values out of range'
2826          Lrttov_column = .false.
2827          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2828       endif
2829       if (any(cospgridIN%o3 .lt. 0)) then
2830          nError=nError+1
2831          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%o3 contains values out of range'
2832          Lrttov_column = .false.
2833          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2834       endif
2835       if (any(cospgridIN%emis_sfc .lt. 0. .OR. cospgridIN%emis_sfc .gt. 1)) then
2836          nError=nError+1
2837          errorMessage(nError) = 'ERROR: COSP input variable: cospgridIN%emis_sfc contains values out of range'
2838          Lrttov_column = .false.
2839          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2840       endif
2841       if (any(cospgridIN%u_sfc .lt. -100. .OR. cospgridIN%u_sfc .gt. 100.)) then
2842          nError=nError+1
2843          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%u_sfc contains values out of range'
2844          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2845          Lrttov_column = .false.
2846       endif
2847       if (any(cospgridIN%v_sfc .lt. -100. .OR. cospgridIN%v_sfc .gt. 100.)) then
2848          nError=nError+1
2849          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%v_sfc contains values out of range'
2850          Lrttov_column = .false.
2851          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2852       endif
2853       if (any(cospgridIN%lat .lt. -90 .OR. cospgridIN%lat .gt. 90)) then
2854          nError=nError+1
2855          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%lat contains values out of range'
2856          Lrttov_column = .false.
2857          if (associated(cospOUT%rttov_tbs)) cospOUT%rttov_tbs(:,:) = R_UNDEF
2858       endif
2859    endif
2860   
2861    ! COSP_INPUTS
2862    if (any([Lisccp_subcolumn,Lisccp_column])) then
2863       if (cospIN%emsfc_lw .lt. 0. .OR. cospIN%emsfc_lw .gt. 1.) then
2864          nError=nError+1
2865          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%emsfc_lw contains values out of range'
2866          Lisccp_subcolumn = .false.
2867          Lisccp_column    = .false.
2868          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
2869          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
2870          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
2871          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
2872          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
2873          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
2874          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
2875          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
2876          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
2877       endif
2878    endif
2879    if (any([Lisccp_subcolumn,Lisccp_column,Lmisr_subcolumn,Lmisr_column,Lmodis_subcolumn,Lmodis_column])) then
2880       if (any(cospIN%tau_067 .lt. 0)) then
2881          nError=nError+1
2882          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tau_067 contains values out of range'
2883          Lisccp_subcolumn = .false.
2884          Lisccp_column    = .false.
2885          Lmisr_subcolumn  = .false.
2886          Lmisr_column     = .false.
2887          Lmodis_subcolumn = .false.
2888          Lmodis_column    = .false.
2889          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
2890          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
2891          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
2892          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
2893          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
2894          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
2895          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
2896          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
2897          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
2898          if (associated(cospOUT%misr_fq))                   cospOUT%misr_fq(:,:,:)                 = R_UNDEF
2899          if (associated(cospOUT%misr_dist_model_layertops)) cospOUT%misr_dist_model_layertops(:,:) = R_UNDEF
2900          if (associated(cospOUT%misr_meanztop))             cospOUT%misr_meanztop(:)               = R_UNDEF
2901          if (associated(cospOUT%misr_cldarea))              cospOUT%misr_cldarea(:)                = R_UNDEF
2902          if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean))                          &
2903               cospOUT%modis_Cloud_Fraction_Total_Mean(:)                   = R_UNDEF
2904          if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean))                          &
2905               cospOUT%modis_Cloud_Fraction_Water_Mean(:)                   = R_UNDEF
2906          if (associated(cospOUT%modis_Cloud_Fraction_Ice_Mean))                            &
2907               cospOUT%modis_Cloud_Fraction_Ice_Mean(:)                     = R_UNDEF
2908          if (associated(cospOUT%modis_Cloud_Fraction_High_Mean))                           &
2909               cospOUT%modis_Cloud_Fraction_High_Mean(:)                    = R_UNDEF
2910          if (associated(cospOUT%modis_Cloud_Fraction_Mid_Mean))                            &
2911               cospOUT%modis_Cloud_Fraction_Mid_Mean(:)                     = R_UNDEF
2912          if (associated(cospOUT%modis_Cloud_Fraction_Low_Mean))                            &
2913               cospOUT%modis_Cloud_Fraction_Low_Mean(:)                     = R_UNDEF
2914          if (associated(cospOUT%modis_Optical_Thickness_Total_Mean))                       &
2915               cospOUT%modis_Optical_Thickness_Total_Mean(:)                = R_UNDEF
2916          if (associated(cospOUT%modis_Optical_Thickness_Water_Mean))                       &
2917               cospOUT%modis_Optical_Thickness_Water_Mean(:)                = R_UNDEF
2918          if (associated(cospOUT%modis_Optical_Thickness_Ice_Mean))                         &
2919               cospOUT%modis_Optical_Thickness_Ice_Mean(:)                  = R_UNDEF
2920          if (associated(cospOUT%modis_Optical_Thickness_Total_LogMean))                    &
2921               cospOUT%modis_Optical_Thickness_Total_LogMean(:)             = R_UNDEF
2922          if (associated(cospOUT%modis_Optical_Thickness_Water_LogMean))                    &
2923               cospOUT%modis_Optical_Thickness_Water_LogMean(:)             = R_UNDEF
2924          if (associated(cospOUT%modis_Optical_Thickness_Ice_LogMean))                      &
2925               cospOUT%modis_Optical_Thickness_Ice_LogMean(:)               = R_UNDEF
2926          if (associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean))                     &
2927               cospOUT%modis_Cloud_Particle_Size_Water_Mean(:)              = R_UNDEF
2928          if (associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean))                       &
2929               cospOUT%modis_Cloud_Particle_Size_Ice_Mean(:)                = R_UNDEF
2930          if (associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean))                      &
2931               cospOUT%modis_Cloud_Top_Pressure_Total_Mean(:)               = R_UNDEF
2932          if (associated(cospOUT%modis_Liquid_Water_Path_Mean))                             &
2933               cospOUT%modis_Liquid_Water_Path_Mean(:)                      = R_UNDEF
2934          if (associated(cospOUT%modis_Ice_Water_Path_Mean))                                &
2935               cospOUT%modis_Ice_Water_Path_Mean(:)                         = R_UNDEF
2936          if (associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure))            &
2937               cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(:,:,:) = R_UNDEF
2938          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffICE))                       &
2939               cospOUT%modis_Optical_Thickness_vs_ReffICE(:,:,:)            = R_UNDEF
2940          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffLIQ))                       &
2941               cospOUT%modis_Optical_Thickness_vs_ReffLIQ(:,:,:)            = R_UNDEF
2942       endif
2943    endif
2944    if (any([Lisccp_subcolumn,Lisccp_column])) then
2945       if (any(cospIN%emiss_11 .lt. 0. .OR. cospIN%emiss_11 .gt. 1)) then
2946          nError=nError+1
2947          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%emiss_11 contains values out of range'
2948          Lisccp_subcolumn = .false.
2949          Lisccp_column    = .false.
2950          if (associated(cospOUT%isccp_totalcldarea))  cospOUT%isccp_totalcldarea(:)  = R_UNDEF
2951          if (associated(cospOUT%isccp_meantb))        cospOUT%isccp_meantb(:)        = R_UNDEF
2952          if (associated(cospOUT%isccp_meantbclr))     cospOUT%isccp_meantbclr(:)     = R_UNDEF
2953          if (associated(cospOUT%isccp_meanptop))      cospOUT%isccp_meanptop(:)      = R_UNDEF
2954          if (associated(cospOUT%isccp_meantaucld))    cospOUT%isccp_meantaucld(:)    = R_UNDEF
2955          if (associated(cospOUT%isccp_meanalbedocld)) cospOUT%isccp_meanalbedocld(:) = R_UNDEF
2956          if (associated(cospOUT%isccp_boxtau))        cospOUT%isccp_boxtau(:,:)      = R_UNDEF
2957          if (associated(cospOUT%isccp_boxptop))       cospOUT%isccp_boxptop(:,:)     = R_UNDEF
2958          if (associated(cospOUT%isccp_fq))            cospOUT%isccp_fq(:,:,:)        = R_UNDEF
2959       endif
2960    endif
2961    if (any([Lmodis_subcolumn,Lmodis_column])) then
2962       if (any(cospIN%asym .lt. -1. .OR. cospIN%asym .gt. 1)) then
2963          nError=nError+1
2964          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%asym contains values out of range'
2965          Lmodis_subcolumn = .false.
2966          Lmodis_column    = .false.
2967          if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean))                          &
2968               cospOUT%modis_Cloud_Fraction_Total_Mean(:)                   = R_UNDEF
2969          if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean))                          &
2970               cospOUT%modis_Cloud_Fraction_Water_Mean(:)                   = R_UNDEF
2971          if (associated(cospOUT%modis_Cloud_Fraction_Ice_Mean))                            &
2972               cospOUT%modis_Cloud_Fraction_Ice_Mean(:)                     = R_UNDEF
2973          if (associated(cospOUT%modis_Cloud_Fraction_High_Mean))                           &
2974               cospOUT%modis_Cloud_Fraction_High_Mean(:)                    = R_UNDEF
2975          if (associated(cospOUT%modis_Cloud_Fraction_Mid_Mean))                            &
2976               cospOUT%modis_Cloud_Fraction_Mid_Mean(:)                     = R_UNDEF
2977          if (associated(cospOUT%modis_Cloud_Fraction_Low_Mean))                            &
2978               cospOUT%modis_Cloud_Fraction_Low_Mean(:)                     = R_UNDEF
2979          if (associated(cospOUT%modis_Optical_Thickness_Total_Mean))                       &
2980               cospOUT%modis_Optical_Thickness_Total_Mean(:)                = R_UNDEF
2981          if (associated(cospOUT%modis_Optical_Thickness_Water_Mean))                       &
2982               cospOUT%modis_Optical_Thickness_Water_Mean(:)                = R_UNDEF
2983          if (associated(cospOUT%modis_Optical_Thickness_Ice_Mean))                         &
2984               cospOUT%modis_Optical_Thickness_Ice_Mean(:)                  = R_UNDEF
2985          if (associated(cospOUT%modis_Optical_Thickness_Total_LogMean))                    &
2986               cospOUT%modis_Optical_Thickness_Total_LogMean(:)             = R_UNDEF
2987          if (associated(cospOUT%modis_Optical_Thickness_Water_LogMean))                    &
2988               cospOUT%modis_Optical_Thickness_Water_LogMean(:)             = R_UNDEF
2989          if (associated(cospOUT%modis_Optical_Thickness_Ice_LogMean))                      &
2990               cospOUT%modis_Optical_Thickness_Ice_LogMean(:)               = R_UNDEF
2991          if (associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean))                     &
2992               cospOUT%modis_Cloud_Particle_Size_Water_Mean(:)              = R_UNDEF
2993          if (associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean))                       &
2994               cospOUT%modis_Cloud_Particle_Size_Ice_Mean(:)                = R_UNDEF
2995          if (associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean))                      &
2996               cospOUT%modis_Cloud_Top_Pressure_Total_Mean(:)               = R_UNDEF
2997          if (associated(cospOUT%modis_Liquid_Water_Path_Mean))                             &
2998               cospOUT%modis_Liquid_Water_Path_Mean(:)                      = R_UNDEF
2999          if (associated(cospOUT%modis_Ice_Water_Path_Mean))                                &
3000               cospOUT%modis_Ice_Water_Path_Mean(:)                         = R_UNDEF
3001          if (associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure))            &
3002               cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(:,:,:) = R_UNDEF
3003          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffICE))                       &
3004               cospOUT%modis_Optical_Thickness_vs_ReffICE(:,:,:)            = R_UNDEF
3005          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffLIQ))                       &
3006               cospOUT%modis_Optical_Thickness_vs_ReffLIQ(:,:,:)            = R_UNDEF
3007       endif
3008       if (any(cospIN%ss_alb .lt. 0 .OR. cospIN%ss_alb .gt. 1)) then
3009          nError=nError+1
3010          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%ss_alb contains values out of range'
3011          Lmodis_subcolumn = .false.
3012          Lmodis_column    = .false.
3013          if (associated(cospOUT%modis_Cloud_Fraction_Total_Mean))                          &
3014               cospOUT%modis_Cloud_Fraction_Total_Mean(:)                   = R_UNDEF
3015          if (associated(cospOUT%modis_Cloud_Fraction_Water_Mean))                          &
3016               cospOUT%modis_Cloud_Fraction_Water_Mean(:)                   = R_UNDEF
3017          if (associated(cospOUT%modis_Cloud_Fraction_Ice_Mean))                            &
3018               cospOUT%modis_Cloud_Fraction_Ice_Mean(:)                     = R_UNDEF
3019          if (associated(cospOUT%modis_Cloud_Fraction_High_Mean))                           &
3020               cospOUT%modis_Cloud_Fraction_High_Mean(:)                    = R_UNDEF
3021          if (associated(cospOUT%modis_Cloud_Fraction_Mid_Mean))                            &
3022               cospOUT%modis_Cloud_Fraction_Mid_Mean(:)                     = R_UNDEF
3023          if (associated(cospOUT%modis_Cloud_Fraction_Low_Mean))                            &
3024               cospOUT%modis_Cloud_Fraction_Low_Mean(:)                     = R_UNDEF
3025          if (associated(cospOUT%modis_Optical_Thickness_Total_Mean))                       &
3026               cospOUT%modis_Optical_Thickness_Total_Mean(:)                = R_UNDEF
3027          if (associated(cospOUT%modis_Optical_Thickness_Water_Mean))                       &
3028               cospOUT%modis_Optical_Thickness_Water_Mean(:)                = R_UNDEF
3029          if (associated(cospOUT%modis_Optical_Thickness_Ice_Mean))                         &
3030               cospOUT%modis_Optical_Thickness_Ice_Mean(:)                  = R_UNDEF
3031          if (associated(cospOUT%modis_Optical_Thickness_Total_LogMean))                    &
3032               cospOUT%modis_Optical_Thickness_Total_LogMean(:)             = R_UNDEF
3033          if (associated(cospOUT%modis_Optical_Thickness_Water_LogMean))                    &
3034               cospOUT%modis_Optical_Thickness_Water_LogMean(:)             = R_UNDEF
3035          if (associated(cospOUT%modis_Optical_Thickness_Ice_LogMean))                      &
3036               cospOUT%modis_Optical_Thickness_Ice_LogMean(:)               = R_UNDEF
3037          if (associated(cospOUT%modis_Cloud_Particle_Size_Water_Mean))                     &
3038               cospOUT%modis_Cloud_Particle_Size_Water_Mean(:)              = R_UNDEF
3039          if (associated(cospOUT%modis_Cloud_Particle_Size_Ice_Mean))                       &
3040               cospOUT%modis_Cloud_Particle_Size_Ice_Mean(:)                = R_UNDEF
3041          if (associated(cospOUT%modis_Cloud_Top_Pressure_Total_Mean))                      &
3042               cospOUT%modis_Cloud_Top_Pressure_Total_Mean(:)               = R_UNDEF
3043          if (associated(cospOUT%modis_Liquid_Water_Path_Mean))                             &
3044               cospOUT%modis_Liquid_Water_Path_Mean(:)                      = R_UNDEF
3045          if (associated(cospOUT%modis_Ice_Water_Path_Mean))                                &
3046               cospOUT%modis_Ice_Water_Path_Mean(:)                         = R_UNDEF
3047          if (associated(cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure))            &
3048               cospOUT%modis_Optical_Thickness_vs_Cloud_Top_Pressure(:,:,:) = R_UNDEF
3049          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffICE))                       &
3050               cospOUT%modis_Optical_Thickness_vs_ReffICE(:,:,:)            = R_UNDEF
3051          if (associated(cospOUT%modis_Optical_Thickness_vs_ReffLIQ))                       &
3052               cospOUT%modis_Optical_Thickness_vs_ReffLIQ(:,:,:)            = R_UNDEF
3053       endif
3054    endif
3055    if (any([Latlid_subcolumn,Latlid_column])) then
3056       if (any(cospIN%betatot_atlid .lt. 0)) then
3057          nError=nError+1
3058          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%betatot_atlid contains values out of range'
3059          Latlid_subcolumn = .false.
3060          Latlid_column    = .false.
3061          if (associated(cospOUT%atlid_cfad_sr))       cospOUT%atlid_cfad_sr(:,:,:)  = R_UNDEF
3062          if (associated(cospOUT%atlid_lidarcld))      cospOUT%atlid_lidarcld(:,:)   = R_UNDEF
3063          if (associated(cospOUT%atlid_cldlayer))      cospOUT%atlid_cldlayer(:,:)   = R_UNDEF
3064          if (associated(cospOUT%atlid_beta_tot))      cospOUT%atlid_beta_tot(:,:,:) = R_UNDEF
3065          if (associated(cospOUT%atlid_beta_mol))      cospOUT%atlid_beta_mol(:,:)   = R_UNDEF
3066       endif
3067       if (any(cospIN%beta_mol_atlid .lt. 0)) then
3068          nError=nError+1
3069          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%beta_mol_atlid contains values out of range'
3070          Latlid_subcolumn = .false.
3071          Latlid_column    = .false.
3072          if (associated(cospOUT%atlid_cfad_sr))       cospOUT%atlid_cfad_sr(:,:,:)  = R_UNDEF
3073          if (associated(cospOUT%atlid_lidarcld))      cospOUT%atlid_lidarcld(:,:)   = R_UNDEF
3074          if (associated(cospOUT%atlid_cldlayer))      cospOUT%atlid_cldlayer(:,:)   = R_UNDEF
3075          if (associated(cospOUT%atlid_beta_tot))      cospOUT%atlid_beta_tot(:,:,:) = R_UNDEF
3076          if (associated(cospOUT%atlid_beta_mol))      cospOUT%atlid_beta_mol(:,:)   = R_UNDEF
3077       endif
3078       if (any(cospIN%tautot_atlid .lt. 0)) then
3079          nError=nError+1
3080          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tautot_atlid contains values out of range'
3081          Latlid_subcolumn = .false.
3082          Latlid_column    = .false.
3083          if (associated(cospOUT%atlid_cfad_sr))       cospOUT%atlid_cfad_sr(:,:,:)  = R_UNDEF
3084          if (associated(cospOUT%atlid_lidarcld))      cospOUT%atlid_lidarcld(:,:)   = R_UNDEF
3085          if (associated(cospOUT%atlid_cldlayer))      cospOUT%atlid_cldlayer(:,:)   = R_UNDEF
3086          if (associated(cospOUT%atlid_beta_tot))      cospOUT%atlid_beta_tot(:,:,:) = R_UNDEF
3087          if (associated(cospOUT%atlid_beta_mol))      cospOUT%atlid_beta_mol(:,:)   = R_UNDEF
3088       endif
3089       if (any(cospIN%tau_mol_atlid .lt. 0)) then
3090          nError=nError+1
3091          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tau_mol_atlid contains values out of range'
3092          Latlid_subcolumn = .false.
3093          Latlid_column    = .false.
3094          if (associated(cospOUT%atlid_cfad_sr))       cospOUT%atlid_cfad_sr(:,:,:)  = R_UNDEF
3095          if (associated(cospOUT%atlid_lidarcld))      cospOUT%atlid_lidarcld(:,:)   = R_UNDEF
3096          if (associated(cospOUT%atlid_cldlayer))      cospOUT%atlid_cldlayer(:,:)   = R_UNDEF
3097          if (associated(cospOUT%atlid_beta_tot))      cospOUT%atlid_beta_tot(:,:,:) = R_UNDEF
3098          if (associated(cospOUT%atlid_beta_mol))      cospOUT%atlid_beta_mol(:,:)   = R_UNDEF
3099       endif
3100    endif
3101   
3102    if (any([LgrLidar532_subcolumn,LgrLidar532_column])) then
3103       if (any(cospIN%betatot_grLidar532 .lt. 0)) then
3104          nError=nError+1
3105          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%betatot_grLidar532 contains values out of range'
3106          LgrLidar532_subcolumn = .false.
3107          LgrLidar532_column    = .false.
3108          if (associated(cospOUT%grLidar532_cfad_sr))       cospOUT%grLidar532_cfad_sr(:,:,:)  = R_UNDEF
3109          if (associated(cospOUT%grLidar532_lidarcld))      cospOUT%grLidar532_lidarcld(:,:)   = R_UNDEF
3110          if (associated(cospOUT%grLidar532_cldlayer))      cospOUT%grLidar532_cldlayer(:,:)   = R_UNDEF
3111          if (associated(cospOUT%grLidar532_beta_tot))      cospOUT%grLidar532_beta_tot(:,:,:) = R_UNDEF
3112          if (associated(cospOUT%grLidar532_beta_mol))      cospOUT%grLidar532_beta_mol(:,:)   = R_UNDEF
3113       endif
3114       if (any(cospIN%beta_mol_grLidar532 .lt. 0)) then
3115          nError=nError+1
3116          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%beta_mol_grLidar532 contains values out of range'
3117          LgrLidar532_subcolumn = .false.
3118          LgrLidar532_column    = .false.
3119          if (associated(cospOUT%grLidar532_cfad_sr))       cospOUT%grLidar532_cfad_sr(:,:,:)  = R_UNDEF
3120          if (associated(cospOUT%grLidar532_lidarcld))      cospOUT%grLidar532_lidarcld(:,:)   = R_UNDEF
3121          if (associated(cospOUT%grLidar532_cldlayer))      cospOUT%grLidar532_cldlayer(:,:)   = R_UNDEF
3122          if (associated(cospOUT%grLidar532_beta_tot))      cospOUT%grLidar532_beta_tot(:,:,:) = R_UNDEF
3123          if (associated(cospOUT%grLidar532_beta_mol))      cospOUT%grLidar532_beta_mol(:,:)   = R_UNDEF
3124       endif
3125       if (any(cospIN%tautot_grLidar532 .lt. 0)) then
3126          nError=nError+1
3127          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tautot_grLidar532 contains values out of range'
3128          LgrLidar532_subcolumn = .false.
3129          LgrLidar532_column    = .false.
3130          if (associated(cospOUT%grLidar532_cfad_sr))       cospOUT%grLidar532_cfad_sr(:,:,:)  = R_UNDEF
3131          if (associated(cospOUT%grLidar532_lidarcld))      cospOUT%grLidar532_lidarcld(:,:)   = R_UNDEF
3132          if (associated(cospOUT%grLidar532_cldlayer))      cospOUT%grLidar532_cldlayer(:,:)   = R_UNDEF
3133          if (associated(cospOUT%grLidar532_beta_tot))      cospOUT%grLidar532_beta_tot(:,:,:) = R_UNDEF
3134          if (associated(cospOUT%grLidar532_beta_mol))      cospOUT%grLidar532_beta_mol(:,:)   = R_UNDEF
3135       endif
3136       if (any(cospIN%tau_mol_grLidar532 .lt. 0)) then
3137          nError=nError+1
3138          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tau_mol_grLidar532 contains values out of range'
3139          LgrLidar532_subcolumn = .false.
3140          LgrLidar532_column    = .false.
3141          if (associated(cospOUT%grLidar532_cfad_sr))       cospOUT%grLidar532_cfad_sr(:,:,:)  = R_UNDEF
3142          if (associated(cospOUT%grLidar532_lidarcld))      cospOUT%grLidar532_lidarcld(:,:)   = R_UNDEF
3143          if (associated(cospOUT%grLidar532_cldlayer))      cospOUT%grLidar532_cldlayer(:,:)   = R_UNDEF
3144          if (associated(cospOUT%grLidar532_beta_tot))      cospOUT%grLidar532_beta_tot(:,:,:) = R_UNDEF
3145          if (associated(cospOUT%grLidar532_beta_mol))      cospOUT%grLidar532_beta_mol(:,:)   = R_UNDEF
3146       endif
3147    endif
3148
3149    if (any([Lcalipso_subcolumn,Lcalipso_column])) then
3150       if (any(cospIN%betatot_calipso .lt. 0)) then
3151          nError=nError+1
3152          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%betatot_calipso contains values out of range'
3153          Lcalipso_subcolumn = .false.
3154          Lcalipso_column    = .false.
3155          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
3156          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
3157          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
3158          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
3159          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
3160          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
3161          if (associated(cospOUT%calipso_srbval))        cospOUT%calipso_srbval(:)            = R_UNDEF
3162          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
3163          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
3164          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
3165          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
3166          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
3167          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
3168       endif
3169       if (any(cospIN%betatot_liq_calipso .lt. 0)) then
3170          nError=nError+1
3171          errorMessage(nError) = ('ERROR: COSP input variable: cospIN%betatot_liq_calipso contains values out of range')
3172          Lcalipso_subcolumn = .false.
3173          Lcalipso_column    = .false.
3174          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
3175          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
3176          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
3177          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
3178          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
3179          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
3180          if (associated(cospOUT%calipso_srbval))        cospOUT%calipso_srbval(:)            = R_UNDEF
3181          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
3182          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
3183          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
3184          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
3185          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
3186          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
3187       endif
3188       if (any(cospIN%betatot_ice_calipso .lt. 0)) then
3189          nError=nError+1
3190          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%betatot_ice_calipso contains values out of range'
3191          Lcalipso_subcolumn = .false.
3192          Lcalipso_column    = .false.
3193          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
3194          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
3195          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
3196          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
3197          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
3198          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
3199          if (associated(cospOUT%calipso_srbval))        cospOUT%calipso_srbval(:)            = R_UNDEF
3200          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
3201          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
3202          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
3203          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
3204          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
3205          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
3206       endif
3207       if (any(cospIN%tautot_calipso .lt. 0)) then
3208          nError=nError+1
3209          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tautot_calipso contains values out of range'
3210          Lcalipso_subcolumn = .false.
3211          Lcalipso_column    = .false.
3212          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
3213          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
3214          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
3215          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
3216          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
3217          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
3218          if (associated(cospOUT%calipso_srbval))        cospOUT%calipso_srbval(:)            = R_UNDEF
3219          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
3220          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
3221          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
3222          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
3223          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
3224          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
3225       endif
3226       if (any(cospIN%tautot_liq_calipso .lt. 0)) then
3227          nError=nError+1
3228          errorMessage(nError) = ('ERROR: COSP input variable: cospIN%tautot_liq_calipso contains values out of range')
3229          Lcalipso_subcolumn = .false.
3230          Lcalipso_column    = .false.
3231          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
3232          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
3233          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
3234          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
3235          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
3236          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
3237          if (associated(cospOUT%calipso_srbval))        cospOUT%calipso_srbval(:)            = R_UNDEF
3238          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
3239          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
3240          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
3241          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
3242          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
3243          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
3244       endif
3245       if (any(cospIN%tautot_ice_calipso .lt. 0)) then
3246          nError=nError+1
3247          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tautot_ice_calipso contains values out of range'
3248          Lcalipso_subcolumn = .false.
3249          Lcalipso_column    = .false.
3250          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
3251          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
3252          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
3253          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
3254          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
3255          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
3256          if (associated(cospOUT%calipso_srbval))        cospOUT%calipso_srbval(:)            = R_UNDEF
3257          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
3258          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
3259          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
3260          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
3261          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
3262          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
3263       endif
3264       if (any(cospIN%tau_mol_calipso .lt. 0)) then
3265          nError=nError+1
3266          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tau_mol_calipso contains values out of range'
3267          Lcalipso_subcolumn = .false.
3268          Lcalipso_column    = .false.
3269          if (associated(cospOUT%calipso_cfad_sr))       cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
3270          if (associated(cospOUT%calipso_lidarcld))      cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
3271          if (associated(cospOUT%calipso_lidarcldphase)) cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
3272          if (associated(cospOUT%calipso_cldlayer))      cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
3273          if (associated(cospOUT%calipso_cldlayerphase)) cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
3274          if (associated(cospOUT%calipso_lidarcldtmp))   cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
3275          if (associated(cospOUT%calipso_srbval))        cospOUT%calipso_srbval(:)            = R_UNDEF
3276          if (associated(cospOUT%calipso_lidarcldtype))  cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
3277          if (associated(cospOUT%calipso_cldtype))       cospOUT%calipso_cldtype(:,:)         = R_UNDEF
3278          if (associated(cospOUT%calipso_cldtypetemp))   cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
3279          if (associated(cospOUT%calipso_cldtypemeanz))  cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
3280          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:) = R_UNDEF
3281          if (associated(cospOUT%calipso_cldthinemis))   cospOUT%calipso_cldthinemis(:)       = R_UNDEF
3282       endif
3283    endif
3284    if (any([Lcalipso_subcolumn,Lcalipso_column,Lcloudsat_column,Lradar_lidar_tcc,       &
3285        Llidar_only_freq_cloud, Lcloudsat_tcc, Lcloudsat_tcc2])) then
3286       if (any(cospIN%beta_mol_calipso .lt. 0)) then
3287          nError=nError+1
3288          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%beta_mol_calipso contains values out of range'
3289          Lcalipso_subcolumn = .false.
3290          Lcalipso_column    = .false.
3291          Lcloudsat_column   = .false.
3292          Lradar_lidar_tcc   = .false.
3293          Llidar_only_freq_cloud = .false.
3294          Lcloudsat_tcc      = .false.
3295          Lcloudsat_tcc2     = .false.
3296          if (associated(cospOUT%calipso_cfad_sr))        cospOUT%calipso_cfad_sr(:,:,:)       = R_UNDEF
3297          if (associated(cospOUT%calipso_lidarcld))       cospOUT%calipso_lidarcld(:,:)        = R_UNDEF
3298          if (associated(cospOUT%calipso_lidarcldphase))  cospOUT%calipso_lidarcldphase(:,:,:) = R_UNDEF
3299          if (associated(cospOUT%calipso_cldlayer))       cospOUT%calipso_cldlayer(:,:)        = R_UNDEF
3300          if (associated(cospOUT%calipso_cldlayerphase))  cospOUT%calipso_cldlayerphase(:,:,:) = R_UNDEF
3301          if (associated(cospOUT%calipso_lidarcldtmp))    cospOUT%calipso_lidarcldtmp(:,:,:)   = R_UNDEF
3302          if (associated(cospOUT%calipso_srbval))         cospOUT%calipso_srbval(:)            = R_UNDEF
3303          if (associated(cospOUT%cloudsat_cfad_ze))       cospOUT%cloudsat_cfad_ze(:,:,:)      = R_UNDEF
3304          if (associated(cospOUT%lidar_only_freq_cloud))  cospOUT%lidar_only_freq_cloud(:,:)   = R_UNDEF
3305          if (associated(cospOUT%radar_lidar_tcc))        cospOUT%radar_lidar_tcc(:)           = R_UNDEF
3306          if (associated(cospOUT%cloudsat_tcc))           cospOUT%cloudsat_tcc(:)              = R_UNDEF
3307          if (associated(cospOUT%cloudsat_tcc2))          cospOUT%cloudsat_tcc2(:)             = R_UNDEF
3308          if (associated(cospOUT%calipso_lidarcldtype))   cospOUT%calipso_lidarcldtype(:,:,:)  = R_UNDEF
3309          if (associated(cospOUT%calipso_cldtype))        cospOUT%calipso_cldtype(:,:)         = R_UNDEF
3310          if (associated(cospOUT%calipso_cldtypetemp))    cospOUT%calipso_cldtypetemp(:,:)     = R_UNDEF
3311          if (associated(cospOUT%calipso_cldtypemeanz))   cospOUT%calipso_cldtypemeanz(:,:)    = R_UNDEF
3312          if (associated(cospOUT%calipso_cldtypemeanzse)) cospOUT%calipso_cldtypemeanzse(:,:)  = R_UNDEF
3313          if (associated(cospOUT%calipso_cldthinemis))    cospOUT%calipso_cldthinemis(:)       = R_UNDEF
3314       endif
3315    endif
3316    if (any([Lparasol_subcolumn,Lparasol_column])) then
3317       if (any(cospIN%tautot_S_liq .lt. 0)) then
3318          nError=nError+1
3319          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tautot_S_liq contains values out of range'
3320          Lparasol_subcolumn = .false.
3321          Lparasol_column    = .false.
3322          if (associated(cospOUT%parasolPix_refl))  cospOUT%parasolPix_refl(:,:,:) = R_UNDEF
3323          if (associated(cospOUT%parasolGrid_refl)) cospOUT%parasolGrid_refl(:,:)  = R_UNDEF
3324       endif
3325       if (any(cospIN%tautot_S_ice .lt. 0)) then
3326          nError=nError+1
3327          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%tautot_S_ice contains values out of range'
3328          Lparasol_subcolumn = .false.
3329          Lparasol_column    = .false.
3330          if (associated(cospOUT%parasolPix_refl))  cospOUT%parasolPix_refl(:,:,:) = R_UNDEF
3331          if (associated(cospOUT%parasolGrid_refl)) cospOUT%parasolGrid_refl(:,:)  = R_UNDEF
3332       endif
3333    endif
3334    if (any([Lcloudsat_subcolumn,Lcloudsat_column,Lradar_lidar_tcc,Llidar_only_freq_cloud, &
3335        Lcloudsat_tcc, Lcloudsat_tcc2])) then
3336       if (any(cospIN%z_vol_cloudsat .lt. 0)) then
3337          nError=nError+1
3338          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%z_vol_cloudsat contains values out of range'
3339          Lcloudsat_subcolumn = .false.
3340          Lcloudsat_column    = .false.
3341          Lradar_lidar_tcc    = .false.
3342          Llidar_only_freq_cloud = .false.
3343          Lcloudsat_tcc       = .false.
3344          Lcloudsat_tcc2      = .false.
3345          if (associated(cospOUT%cloudsat_cfad_ze))          cospOUT%cloudsat_cfad_ze(:,:,:)        = R_UNDEF
3346          if (associated(cospOUT%cloudsat_Ze_tot))           cospOUT%cloudsat_Ze_tot(:,:,:)         = R_UNDEF
3347          if (associated(cospOUT%lidar_only_freq_cloud))     cospOUT%lidar_only_freq_cloud(:,:)     = R_UNDEF
3348          if (associated(cospOUT%radar_lidar_tcc))           cospOUT%radar_lidar_tcc(:)             = R_UNDEF
3349          if (associated(cospOUT%cloudsat_tcc)) cospOUT%cloudsat_tcc(:) = R_UNDEF
3350          if (associated(cospOUT%cloudsat_tcc2)) cospOUT%cloudsat_tcc2(:) = R_UNDEF
3351       endif
3352       if (any(cospIN%kr_vol_cloudsat .lt. 0)) then
3353          nError=nError+1
3354          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%kr_vol_cloudsat contains values out of range'
3355          Lcloudsat_subcolumn = .false.
3356          Lcloudsat_column    = .false.
3357          Lradar_lidar_tcc    = .false.
3358          Llidar_only_freq_cloud = .false.
3359          Lcloudsat_tcc       = .false.
3360          Lcloudsat_tcc2      = .false.
3361          if (associated(cospOUT%cloudsat_cfad_ze))          cospOUT%cloudsat_cfad_ze(:,:,:)        = R_UNDEF
3362          if (associated(cospOUT%cloudsat_Ze_tot))           cospOUT%cloudsat_Ze_tot(:,:,:)         = R_UNDEF
3363          if (associated(cospOUT%lidar_only_freq_cloud))     cospOUT%lidar_only_freq_cloud(:,:)     = R_UNDEF
3364          if (associated(cospOUT%radar_lidar_tcc))           cospOUT%radar_lidar_tcc(:)             = R_UNDEF
3365          if (associated(cospOUT%cloudsat_tcc)) cospOUT%cloudsat_tcc(:) = R_UNDEF
3366          if (associated(cospOUT%cloudsat_tcc2)) cospOUT%cloudsat_tcc2(:) = R_UNDEF
3367       endif
3368       if (any(cospIN%g_vol_cloudsat .lt. 0)) then
3369          nError=nError+1
3370          errorMessage(nError) = 'ERROR: COSP input variable: cospIN%g_vol_cloudsat contains values out of range'
3371          Lcloudsat_subcolumn = .false.
3372          Lcloudsat_column    = .false.
3373          Lradar_lidar_tcc    = .false.
3374          Llidar_only_freq_cloud = .false.
3375          Lcloudsat_tcc       = .false.
3376          Lcloudsat_tcc2      = .false.         
3377          if (associated(cospOUT%cloudsat_cfad_ze))          cospOUT%cloudsat_cfad_ze(:,:,:)        = R_UNDEF
3378          if (associated(cospOUT%cloudsat_Ze_tot))           cospOUT%cloudsat_Ze_tot(:,:,:)         = R_UNDEF
3379          if (associated(cospOUT%lidar_only_freq_cloud))     cospOUT%lidar_only_freq_cloud(:,:)     = R_UNDEF
3380          if (associated(cospOUT%radar_lidar_tcc))           cospOUT%radar_lidar_tcc(:)             = R_UNDEF
3381          if (associated(cospOUT%cloudsat_tcc)) cospOUT%cloudsat_tcc(:) = R_UNDEF
3382          if (associated(cospOUT%cloudsat_tcc2)) cospOUT%cloudsat_tcc2(:) = R_UNDEF         
3383       endif
3384    endif
3385    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3386    ! Part 2: Check input fields array size for consistency. This needs to be done for each
3387    !         simulator
3388    !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3389    ! ISCCP
3390    if (Lisccp_subcolumn .or. Lisccp_column) then
3391       if (size(cospIN%frac_out,1)  .ne. cospIN%Npoints .OR. &
3392           size(cospIN%tau_067,1)   .ne. cospIN%Npoints .OR. &
3393           size(cospIN%emiss_11,1)  .ne. cospIN%Npoints .OR. &
3394           size(cospgridIN%skt)     .ne. cospIN%Npoints .OR. &
3395           size(cospgridIN%qv,1)    .ne. cospIN%Npoints .OR. &
3396           size(cospgridIN%at,1)    .ne. cospIN%Npoints .OR. &
3397           size(cospgridIN%phalf,1) .ne. cospIN%Npoints .OR. &
3398           size(cospgridIN%sunlit)  .ne. cospIN%Npoints .OR. &
3399           size(cospgridIN%pfull,1) .ne. cospIN%Npoints) then
3400          Lisccp_subcolumn = .false.
3401          Lisccp_column    = .false.
3402          nError=nError+1
3403          errorMessage(nError) = 'ERROR(isccp_simulator): The number of points in the input fields are inconsistent'
3404       endif
3405       if (size(cospIN%frac_out,2) .ne. cospIN%Ncolumns .OR. &
3406           size(cospIN%tau_067,2)  .ne. cospIN%Ncolumns .OR. &
3407           size(cospIN%emiss_11,2) .ne. cospIN%Ncolumns) then
3408          Lisccp_subcolumn = .false.
3409          Lisccp_column    = .false.
3410          nError=nError+1
3411          errorMessage(nError) = 'ERROR(isccp_simulator): The number of sub-columns in the input fields are inconsistent'
3412       endif
3413       if (size(cospIN%frac_out,3)  .ne. cospIN%Nlevels .OR. &
3414           size(cospIN%tau_067,3)   .ne. cospIN%Nlevels .OR. &
3415           size(cospIN%emiss_11,3)  .ne. cospIN%Nlevels .OR. &
3416           size(cospgridIN%qv,2)    .ne. cospIN%Nlevels .OR. &
3417           size(cospgridIN%at,2)    .ne. cospIN%Nlevels .OR. &
3418           size(cospgridIN%pfull,2) .ne. cospIN%Nlevels .OR. &
3419           size(cospgridIN%phalf,2) .ne. cospIN%Nlevels+1) then
3420          Lisccp_subcolumn = .false.
3421          Lisccp_column    = .false.
3422          nError=nError+1
3423          errorMessage(nError) = 'ERROR(isccp_simulator): The number of levels in the input fields are inconsistent'
3424       endif
3425    endif
3426   
3427    ! MISR
3428    if (Lmisr_subcolumn .or. Lmisr_column) then
3429       if (size(cospIN%tau_067,1)        .ne. cospIN%Npoints .OR. &
3430           size(cospgridIN%sunlit)       .ne. cospIN%Npoints .OR. &
3431           size(cospgridIN%hgt_matrix,1) .ne. cospIN%Npoints .OR. &
3432           size(cospgridIN%at,1)         .ne. cospIN%Npoints) then
3433          Lmisr_subcolumn = .false.
3434          Lmisr_column    = .false.
3435          nError=nError+1
3436          errorMessage(nError) = 'ERROR(misr_simulator): The number of points in the input fields are inconsistent'
3437       endif
3438       if (size(cospIN%tau_067,2) .ne. cospIN%Ncolumns) then
3439          Lmisr_subcolumn = .false.
3440          Lmisr_column    = .false.
3441          nError=nError+1
3442          errorMessage(nError) = 'ERROR(misr_simulator): The number of sub-columns in the input fields are inconsistent'
3443       endif
3444       if (size(cospIN%tau_067,3)        .ne. cospIN%Nlevels .OR. &
3445           size(cospgridIN%hgt_matrix,2) .ne. cospIN%Nlevels .OR. &
3446           size(cospgridIN%at,2)         .ne. cospIN%Nlevels) then
3447          Lmisr_subcolumn = .false.
3448          Lmisr_column    = .false.
3449          nError=nError+1
3450          errorMessage(nError) = 'ERROR(misr_simulator): The number of levels in the input fields are inconsistent'
3451       endif
3452    endif
3453   
3454    ! MODIS
3455    if (Lmodis_subcolumn .or. Lmodis_column) then
3456       if (size(cospIN%fracLiq,1) .ne. cospIN%Npoints .OR. &
3457           size(cospIN%tau_067,1) .ne. cospIN%Npoints .OR. &
3458           size(cospIN%asym,1)    .ne. cospIN%Npoints .OR. &
3459           size(cospIN%ss_alb,1)  .ne. cospIN%Npoints) then
3460          Lmodis_subcolumn = .false.
3461          Lmodis_column    = .false.
3462          nError=nError+1
3463          errorMessage(nError) = 'ERROR(modis_simulator): The number of points in the input fields are inconsistent'
3464       endif
3465       if (size(cospIN%fracLiq,2) .ne. cospIN%Ncolumns .OR. &
3466           size(cospIN%tau_067,2) .ne. cospIN%Ncolumns .OR. &
3467           size(cospIN%asym,2)    .ne. cospIN%Ncolumns .OR. &
3468           size(cospIN%ss_alb,2)  .ne. cospIN%Ncolumns) then
3469          Lmodis_subcolumn = .false.
3470          Lmodis_column    = .false.
3471          nError=nError+1
3472          errorMessage(nError) = 'ERROR(modis_simulator): The number of sub-columns in the input fields are inconsistent'
3473       endif
3474       if (size(cospIN%fracLiq,3) .ne. cospIN%Nlevels .OR. &
3475           size(cospIN%tau_067,3) .ne. cospIN%Nlevels .OR. &
3476           size(cospIN%asym,3)    .ne. cospIN%Nlevels .OR. &
3477           size(cospIN%ss_alb,3)  .ne. cospIN%Nlevels) then
3478          Lmodis_subcolumn = .false.
3479          Lmodis_column    = .false.
3480          nError=nError+1
3481          errorMessage(nError) = 'ERROR(modis_simulator): The number of levels in the input fields are inconsistent'
3482       endif
3483    endif
3484   
3485    ! CLOUDSAT
3486    if (Lcloudsat_subcolumn .or. Lcloudsat_column) then
3487       if (size(cospIN%z_vol_cloudsat,1)   .ne. cospIN%Npoints .OR. &
3488           size(cospIN%kr_vol_cloudsat,1)  .ne. cospIN%Npoints .OR. &
3489           size(cospIN%g_vol_cloudsat,1)   .ne. cospIN%Npoints .OR. &
3490           size(cospgridIN%hgt_matrix,1)   .ne. cospIN%Npoints) then
3491          Lcloudsat_subcolumn = .false.
3492          Lcloudsat_column    = .false.
3493          nError=nError+1
3494          errorMessage(nError) = 'ERROR(cloudsat_simulator): The number of points in the input fields are inconsistent'
3495       endif
3496       if (size(cospIN%z_vol_cloudsat,2)  .ne. cospIN%Ncolumns .OR. &
3497           size(cospIN%kr_vol_cloudsat,2) .ne. cospIN%Ncolumns .OR. &
3498           size(cospIN%g_vol_cloudsat,2)  .ne. cospIN%Ncolumns) then
3499          Lcloudsat_subcolumn = .false.
3500          Lcloudsat_column    = .false.
3501          nError=nError+1
3502          errorMessage(nError) = 'ERROR(cloudsat_simulator): The number of sub-columns in the input fields are inconsistent'
3503       endif
3504       if (size(cospIN%z_vol_cloudsat,3)  .ne. cospIN%Nlevels .OR. &
3505           size(cospIN%kr_vol_cloudsat,3) .ne. cospIN%Nlevels .OR. &
3506           size(cospIN%g_vol_cloudsat,3)  .ne. cospIN%Nlevels .OR. &
3507           size(cospgridIN%hgt_matrix,2)  .ne. cospIN%Nlevels) then
3508          Lcloudsat_subcolumn = .false.
3509          Lcloudsat_column    = .false.
3510          nError=nError+1
3511          errorMessage(nError) = 'ERROR(cloudsat_simulator): The number of levels in the input fields are inconsistent'
3512       endif
3513    endif
3514
3515    ! GROUND LIDAR @ 532nm
3516    if (LgrLidar532_subcolumn .or. LgrLidar532_column) then
3517       if (size(cospIN%beta_mol_grLidar532,1)    .ne. cospIN%Npoints .OR. &
3518           size(cospIN%betatot_grLidar532,1)     .ne. cospIN%Npoints .OR. &
3519           size(cospIN%tau_mol_grLidar532,1)     .ne. cospIN%Npoints .OR. &
3520           size(cospIN%tautot_grLidar532,1)      .ne. cospIN%Npoints) then
3521          LgrLidar532_subcolumn = .false.
3522          LgrLidar532_column    = .false.
3523          nError=nError+1
3524          errorMessage(nError) = 'ERROR(grLidar532_simulator): The number of points in the input fields are inconsistent'
3525       endif
3526       if (size(cospIN%betatot_grLidar532,2)    .ne. cospIN%Ncolumns .OR. &
3527           size(cospIN%tautot_grLidar532,2)     .ne. cospIN%Ncolumns) then
3528          LgrLidar532_subcolumn = .false.
3529          LgrLidar532_column    = .false.
3530          nError=nError+1
3531          errorMessage(nError) = 'ERROR(grLidar532_simulator): The number of sub-columns in the input fields are inconsistent'
3532       endif
3533       if (size(cospIN%beta_mol_grLidar532,2)    .ne. cospIN%Nlevels .OR. &
3534           size(cospIN%betatot_grLidar532,3)     .ne. cospIN%Nlevels .OR. &
3535           size(cospIN%tau_mol_grLidar532,2)     .ne. cospIN%Nlevels .OR. &
3536           size(cospIN%tautot_grLidar532,3)      .ne. cospIN%Nlevels) then
3537          LgrLidar532_subcolumn = .false.
3538          LgrLidar532_column    = .false.
3539          nError=nError+1
3540          errorMessage(nError) = 'ERROR(grLidar532_simulator): The number of levels in the input fields are inconsistent'
3541       endif
3542    endif
3543   
3544    ! ATLID
3545    if (Latlid_subcolumn .or. Latlid_column) then
3546       if (size(cospIN%beta_mol_atlid,1)    .ne. cospIN%Npoints .OR. &
3547           size(cospIN%betatot_atlid,1)     .ne. cospIN%Npoints .OR. &
3548           size(cospIN%tau_mol_atlid,1)     .ne. cospIN%Npoints .OR. &
3549           size(cospIN%tautot_atlid,1)      .ne. cospIN%Npoints) then
3550          Latlid_subcolumn = .false.
3551          Latlid_column    = .false.
3552          nError=nError+1             
3553          errorMessage(nError) = 'ERROR(atlid_simulator): The number of points in the input fields are inconsistent'
3554       endif
3555       if (size(cospIN%betatot_atlid,2)    .ne. cospIN%Ncolumns .OR. &
3556           size(cospIN%tautot_atlid,2)     .ne. cospIN%Ncolumns) then
3557          Latlid_subcolumn = .false.
3558          Latlid_column    = .false.
3559          nError=nError+1             
3560          errorMessage(nError) = 'ERROR(atlid_simulator): The number of sub-columns in the input fields are inconsistent'
3561       endif
3562       if (size(cospIN%beta_mol_atlid,2)    .ne. cospIN%Nlevels .OR. &
3563           size(cospIN%betatot_atlid,3)     .ne. cospIN%Nlevels .OR. &
3564           size(cospIN%tau_mol_atlid,2)     .ne. cospIN%Nlevels .OR. &
3565           size(cospIN%tautot_atlid,3)      .ne. cospIN%Nlevels) then
3566          Latlid_subcolumn = .false.
3567          Latlid_column    = .false.
3568          nError=nError+1
3569          errorMessage(nError) = 'ERROR(atlid_simulator): The number of levels in the input fields are inconsistent'
3570       endif
3571    endif
3572
3573    ! CALIPSO
3574    if (Lcalipso_subcolumn .or. Lcalipso_column) then
3575       if (size(cospIN%beta_mol_calipso,1)    .ne. cospIN%Npoints .OR. &
3576           size(cospIN%betatot_calipso,1)     .ne. cospIN%Npoints .OR. &
3577           size(cospIN%betatot_liq_calipso,1) .ne. cospIN%Npoints .OR. &
3578           size(cospIN%betatot_ice_calipso,1) .ne. cospIN%Npoints .OR. &
3579           size(cospIN%tau_mol_calipso,1)     .ne. cospIN%Npoints .OR. &
3580           size(cospIN%tautot_calipso,1)      .ne. cospIN%Npoints .OR. &
3581           size(cospIN%tautot_liq_calipso,1)  .ne. cospIN%Npoints .OR. &
3582           size(cospIN%tautot_ice_calipso,1)  .ne. cospIN%Npoints) then
3583          Lcalipso_subcolumn = .false.
3584          Lcalipso_column    = .false.
3585          nError=nError+1
3586          errorMessage(nError) = 'ERROR(calipso_simulator): The number of points in the input fields are inconsistent'
3587       endif
3588       if (size(cospIN%betatot_calipso,2)     .ne. cospIN%Ncolumns .OR. &
3589           size(cospIN%betatot_liq_calipso,2) .ne. cospIN%Ncolumns .OR. &
3590           size(cospIN%betatot_ice_calipso,2) .ne. cospIN%Ncolumns .OR. &
3591           size(cospIN%tautot_calipso,2)      .ne. cospIN%Ncolumns .OR. &
3592           size(cospIN%tautot_liq_calipso,2)  .ne. cospIN%Ncolumns .OR. &
3593           size(cospIN%tautot_ice_calipso,2)  .ne. cospIN%Ncolumns) then
3594          Lcalipso_subcolumn = .false.
3595          Lcalipso_column    = .false.
3596          nError=nError+1
3597          errorMessage(nError) = 'ERROR(calipso_simulator): The number of sub-columns in the input fields are inconsistent'
3598       endif
3599       if (size(cospIN%beta_mol_calipso,2)    .ne. cospIN%Nlevels .OR. &
3600           size(cospIN%betatot_calipso,3)     .ne. cospIN%Nlevels .OR. &
3601           size(cospIN%betatot_liq_calipso,3) .ne. cospIN%Nlevels .OR. &
3602           size(cospIN%betatot_ice_calipso,3) .ne. cospIN%Nlevels .OR. &
3603           size(cospIN%tau_mol_calipso,2)     .ne. cospIN%Nlevels .OR. &
3604           size(cospIN%tautot_calipso,3)      .ne. cospIN%Nlevels .OR. &
3605           size(cospIN%tautot_liq_calipso,3)  .ne. cospIN%Nlevels .OR. &
3606           size(cospIN%tautot_ice_calipso,3)  .ne. cospIN%Nlevels) then
3607          Lcalipso_subcolumn = .false.
3608          Lcalipso_column    = .false.
3609          nError=nError+1
3610          errorMessage(nError) = 'ERROR(calipso_simulator): The number of levels in the input fields are inconsistent'
3611       endif
3612    endif
3613   
3614    ! PARASOL
3615    if (Lparasol_subcolumn .or. Lparasol_column) then
3616       if (size(cospIN%tautot_S_liq,1) .ne. cospIN%Npoints .OR. &
3617           size(cospIN%tautot_S_ice,1) .ne. cospIN%Npoints) then
3618          Lparasol_subcolumn = .false.
3619          Lparasol_column    = .false.
3620          nError=nError+1
3621          errorMessage(nError) = 'ERROR(parasol_simulator): The number of points in the input fields are inconsistent'
3622       endif
3623       if (size(cospIN%tautot_S_liq,2) .ne. cospIN%Ncolumns .OR. &
3624           size(cospIN%tautot_S_ice,2) .ne. cospIN%Ncolumns) then
3625          Lparasol_subcolumn = .false.
3626          Lparasol_column    = .false.
3627          nError=nError+1
3628          errorMessage(nError) = 'ERROR(parasol_simulator): The number of levels in the input fields are inconsistent'
3629       endif
3630    endif
3631
3632    ! RTTOV
3633    if (Lrttov_column) then
3634       if (size(cospgridIN%pfull,1)           .ne. cospIN%Npoints .OR. &
3635           size(cospgridIN%at,1)              .ne. cospIN%Npoints .OR. &
3636           size(cospgridIN%qv,1)              .ne. cospIN%Npoints .OR. &
3637           size(cospgridIN%hgt_matrix_half,1) .ne. cospIN%Npoints .OR. &
3638           size(cospgridIN%u_sfc)             .ne. cospIN%Npoints .OR. &
3639           size(cospgridIN%v_sfc)             .ne. cospIN%Npoints .OR. &
3640           size(cospgridIN%skt)               .ne. cospIN%Npoints .OR. &
3641           size(cospgridIN%phalf,1)           .ne. cospIN%Npoints .OR. &
3642           size(cospgridIN%qv,1)              .ne. cospIN%Npoints .OR. &
3643           size(cospgridIN%land)              .ne. cospIN%Npoints .OR. &
3644           size(cospgridIN%lat)               .ne. cospIN%Npoints) then
3645          Lrttov_column    = .false.
3646          nError=nError+1
3647          errorMessage(nError) = 'ERROR(rttov_simulator): The number of points in the input fields are inconsistent'
3648       endif
3649       if (size(cospgridIN%pfull,2)           .ne. cospIN%Nlevels   .OR. &
3650           size(cospgridIN%at,2)              .ne. cospIN%Nlevels   .OR. &
3651           size(cospgridIN%qv,2)              .ne. cospIN%Nlevels   .OR. &
3652           size(cospgridIN%hgt_matrix_half,2) .ne. cospIN%Nlevels+1 .OR. &
3653           size(cospgridIN%phalf,2)           .ne. cospIN%Nlevels+1 .OR. &
3654           size(cospgridIN%qv,2)              .ne. cospIN%Nlevels) then
3655          Lrttov_column    = .false.
3656          nError=nError+1
3657          errorMessage(nError) = 'ERROR(rttov_simulator): The number of levels in the input fields are inconsistent'
3658       endif
3659    endif
3660  end subroutine cosp_errorCheck
3661
3662  !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3663  ! END MODULE
3664  !%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3665END MODULE MOD_COSP
Note: See TracBrowser for help on using the repository browser.