Back to home page

MITgcm

 
 

    


File indexing completed on 2025-08-05 05:09:14 UTC

view on githubraw file Latest commit 13ce79fe on 2025-08-04 21:05:34 UTC
13ce79fe94 Ivan*0001 #include "PROFILES_OPTIONS.h"
                0002 #ifdef ALLOW_CTRL
                0003 # include "CTRL_OPTIONS.h"
                0004 #endif
                0005 
                0006 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
                0007 CBOP
                0008 C !ROUTINE: PROFILES_COST
                0009 
                0010 C !INTERFACE:
                0011       SUBROUTINE PROFILES_COST( myTime, myIter, myThid )
                0012 
                0013 C     !DESCRIPTION:
                0014 C     Computes the cost for netcdf profiles data
                0015 C     Started: Gael Forget 15-March-2006
                0016 C-- Note: GLOBAL_SUM/MAX called from single thread block will prevent
                0017 C         this routine to work in multi-threaded env.
                0018 
                0019 C     !USES:
                0020       IMPLICIT NONE
                0021 C     == Global variables ===
                0022 #include "SIZE.h"
                0023 #include "EEPARAMS.h"
                0024 #include "PARAMS.h"
                0025 #include "GRID.h"
                0026 #include "DYNVARS.h"
                0027 #ifdef ALLOW_CAL
                0028 # include "cal.h"
                0029 #endif
                0030 #ifdef ALLOW_PROFILES
                0031 # include "PROFILES_SIZE.h"
                0032 # include "profiles.h"
                0033 # include "netcdf.inc"
                0034 #endif
                0035 #ifdef ALLOW_CTRL
                0036 # include "OPTIMCYCLE.h"
                0037 #endif
                0038 
                0039 C     !INPUT PARAMETERS:
                0040 C     myTime :: time counter for this thread
                0041 C     myIter :: iteration counter for this thread
                0042 C     myThid :: my thread ID number
                0043       _RL     myTime
                0044       INTEGER myIter
                0045       INTEGER myThid
                0046 C     !OUTPUT PARAMETERS:
                0047 
                0048 #ifdef ALLOW_PROFILES
                0049 C     !FUNCTIONS:
                0050       INTEGER  ILNBLNK
                0051       EXTERNAL ILNBLNK
                0052 
                0053 C     !LOCAL VARIABLES:
                0054       INTEGER kLev, kC, kCMax, kProf
                0055       INTEGER num_file,num_var,prof_num
                0056       INTEGER bi,bj
                0057       INTEGER err
                0058       _RL prof_traj1D(NLEVELMAX), prof_traj1D_mean(NLEVELMAX)
                0059       _RL prof_data1D(NLEVELMAX), prof_weights1D(NLEVELMAX)
                0060       CHARACTER*(MAX_LEN_MBUF) msgBuf
                0061 #ifndef ALLOW_CTRL
                0062       INTEGER optimcycle
                0063 #endif
                0064 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0065       INTEGER iL
                0066 #endif
                0067 CEOP
                0068 
                0069       _RL  objf_prof_tile (nSx,nSy)
                0070       _RL  objf_prof_glo
                0071       _RL  num_prof_tile (nSx,nSy)
                0072       _RL  num_prof_glo
                0073 
                0074 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0075       INTEGER iavgbin,ikzz
                0076       INTEGER itmp
                0077       INTEGER k2, ix9, iy9, ktmp
                0078       INTEGER cunit
                0079       CHARACTER*(MAX_LEN_FNAM) cfile
                0080 
                0081       _RL prof_data1D_mean(NLEVELMAX)
                0082       _RL prof_count1D(NLEVELMAX)
                0083       _RL prof_weights1D_mean(NLEVELMAX)
                0084       _RL recip_profiles_mean_indsamples(NVARMAX)
                0085 
                0086       _RL tmpr6, tmpr7, tmpr8, tmpr9
                0087       REAL*4 tmp99(NAVGBINMAX)
                0088       _RL tmp11, tmp12, tmp_recip_count
                0089       LOGICAL doglbsum
                0090 
                0091       _RL  objf_prof_mean_tile (nSx,nSy)
                0092       _RL  objf_prof_mean_glo
                0093       _RL  num_prof_mean_tile (nSx,nSy)
                0094       _RL  num_prof_mean_glo
                0095 #endif /* ALLOW_PROFILES_SAMPLESPLIT_COST */
                0096 
                0097 #ifndef ALLOW_CTRL
                0098       optimcycle = 0
                0099 #endif
                0100 
                0101       WRITE(msgBuf,'(A)') ' '
                0102       CALL PRINT_MESSAGE( msgBuf,
                0103      &     standardMessageUnit, SQUEEZE_RIGHT , myThid )
                0104       WRITE(msgBuf,'(A)') '== profiles_cost: begin =='
                0105       CALL PRINT_MESSAGE( msgBuf,
                0106      &     standardMessageUnit, SQUEEZE_RIGHT , myThid )
                0107 
                0108       _BEGIN_MASTER( myThid )
                0109 
                0110 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0111       NAVGBIN = 0
                0112 C Initialize
                0113       DO iavgbin = 1, NAVGBINMAX
                0114         avgbinglbsum(iavgbin) = 0
                0115         DO kCMax = 1, NLEVELCOMBMAX
                0116           DO num_var = 1, NVARMAX
                0117             prof_traj1D_all_mean(iavgbin,kCMax,num_var) = 0. _d 0
                0118             prof_data1D_all_mean(iavgbin,kCMax,num_var) = 0. _d 0
                0119             prof_weights1D_all_mean(iavgbin,kCMax,num_var) = 0. _d 0
                0120             prof_count1D_all_mean(iavgbin,kCMax,num_var) = 0. _d 0
                0121           ENDDO
                0122         ENDDO
                0123       ENDDO
                0124 
                0125       DO num_var = 1, NVARMAX
                0126         recip_profiles_mean_indsamples(num_var) = 0. _d 0
                0127         IF (profiles_mean_indsamples(num_var).GT.0. _d 0) THEN
                0128           recip_profiles_mean_indsamples(num_var) = 1. _d 0 /
                0129      &     profiles_mean_indsamples(num_var)
                0130         ENDIF
                0131       ENDDO
                0132 
                0133       DO bj = 1, nSy
                0134         DO bi = 1, nSx
                0135           DO num_file = 1, NFILESPROFMAX
                0136             IF ( (ProfNo(num_file,bi,bj).GT.0) .AND.
                0137      &           (profilesDoNcOutput) ) THEN
                0138 C Write data from buffer to .nc file
                0139               err = NF_SYNC( fidforward(num_file,bi,bj) )
                0140               CALL PROFILES_NF_ERROR( 'COST: NF_SYNC fidforward',
                0141      &             err,bi,bj,myThid )
                0142 
                0143             ENDIF
                0144 
                0145 C Find vertical indices
                0146             DO kLev = 1, NLEVELMAX
                0147               prof_lev_comb(kLev,num_file,bi,bj) = -999
                0148 
                0149               IF (kLev.LE.ProfDepthNo(num_file,bi,bj)) THEN
                0150                 DO kC = 1, NLEVELCOMB
                0151                   IF (prof_depth(num_file,kLev,bi,bj).EQ.
                0152      &                prof_depth_comb(kC,bi,bj) .AND.
                0153      &                prof_depth_comb(kC,bi,bj).GE.0. _d 0 .AND.
                0154      &                prof_lev_comb(kLev,num_file,bi,bj).EQ.-999) THEN
                0155                     prof_lev_comb(kLev,num_file,bi,bj) = kC
                0156                   ENDIF
                0157                 ENDDO
                0158               ENDIF
                0159 
                0160             ENDDO
                0161 
                0162             DO num_var = 1, NVARMAX
                0163               IF ( vec_quantities(num_file,num_var,bi,bj) ) THEN
                0164                 DO prof_num = 1, NOBSGLOB
                0165                   IF (prof_num.LE.ProfNo(num_file,bi,bj)) THEN
                0166                     DO kLev = 1, NLEVELMAX
                0167                       prof_traj1D(kLev) = 0.
                0168                       prof_data1D(kLev) = 0.
                0169                       prof_weights1D(kLev) = 0.
                0170                     ENDDO
                0171 
                0172                     ix9 = prof_interp_i(num_file,prof_num,1,bi,bj)
                0173                     iy9 = prof_interp_j(num_file,prof_num,1,bi,bj)
                0174 
                0175                     IF (prof_ind_avgbin(num_file,prof_num,bi,bj).GT.
                0176      &                  NAVGBIN)
                0177      &                NAVGBIN =
                0178      &                 prof_ind_avgbin(num_file,prof_num,bi,bj)
                0179 
                0180                     IF (ix9 .GE. 0. _d 0 .AND. iy9 .GE. 0. _d 0) THEN
                0181                       itmp = prof_ind_avgbin(num_file,prof_num,bi,bj)
                0182                       IF (avgbinglbsum(itmp).EQ.0)
                0183      &                  avgbinglbsum(itmp) = 1
                0184 
                0185                       CALL ACTIVE_READ_PROFILE( num_file,
                0186      &                     ProfDepthNo(num_file,bi,bj),prof_traj1D,
                0187      &                     num_var,prof_num,.false.,optimcycle,
                0188      &                     bi,bj,myThid,
                0189      &                     profiles_dummy(num_file,num_var,bi,bj) )
                0190 
                0191                       CALL PROFILES_READVECTOR( num_file, num_var,
                0192      &                     prof_ind_glob(num_file,prof_num,bi,bj),
                0193      &                     ProfDepthNo(num_file,bi,bj),prof_data1D,
                0194      &                     bi,bj,myThid )
                0195 
                0196                       CALL PROFILES_READVECTOR( num_file,-num_var,
                0197      &                     prof_ind_glob(num_file,prof_num,bi,bj),
                0198      &                     ProfDepthNo(num_file,bi,bj),prof_weights1D,
                0199      &                     bi,bj,myThid )
                0200 
                0201                       DO kProf = 1, ProfDepthNo(num_file,bi,bj)
                0202                         IF ( prof_weights1D(kProf).GT.0. _d 0 .AND.
                0203      &                       prof_mask1D_cur(kProf,bi,bj).NE.0. _d 0 )
                0204      &                  THEN
                0205                           prof_traj1D_all_mean(itmp,
                0206      &                     prof_lev_comb(kProf,num_file,bi,bj),
                0207      &                     num_var)
                0208      &                     = prof_traj1D_all_mean(itmp,
                0209      &                        prof_lev_comb(kProf,num_file,bi,bj),
                0210      &                        num_var)
                0211      &                       + prof_traj1D(kProf)
                0212 
                0213                           prof_data1D_all_mean(itmp,
                0214      &                     prof_lev_comb(kProf,num_file,bi,bj),
                0215      &                     num_var)
                0216      &                     = prof_data1D_all_mean(itmp,
                0217      &                        prof_lev_comb(kProf,num_file,bi,bj),
                0218      &                        num_var)
                0219      &                       + prof_data1D(kProf)
                0220 
                0221                           prof_weights1D_all_mean(itmp,
                0222      &                     prof_lev_comb(kProf,num_file,bi,bj),
                0223      &                     num_var)
                0224      &                     = prof_weights1D_all_mean(itmp,
                0225      &                        prof_lev_comb(kProf,num_file,bi,bj),
                0226      &                        num_var)
                0227      &                       + 1. _d 0 / prof_weights1D(kProf)
                0228 
                0229                           prof_count1D_all_mean(itmp,
                0230      &                     prof_lev_comb(kProf,num_file,bi,bj),
                0231      &                     num_var)
                0232      &                     = prof_count1D_all_mean(itmp,
                0233      &                        prof_lev_comb(kProf,num_file,bi,bj),
                0234      &                        num_var)
                0235      &                       + 1. _d 0
                0236 
                0237                         ENDIF
                0238                       ENDDO !DO kProf = 1, ProfDepthNo
                0239 
                0240                     ENDIF !IF (ix9.GE.0. _d 0 .AND. iy9.GE.0. _d 0)
                0241 
                0242                   ENDIF !IF (prof_num.LE.ProfNo(num_file,bi,bj))
                0243                 ENDDO !DO prof_num...
                0244               ENDIF !IF vec_quantities(num_file,num_var,bi,bj)
                0245             ENDDO !DO num_var
                0246 
                0247           ENDDO !DO num_file = 1, NFILESPROFMAX
                0248         ENDDO !DO bi
                0249       ENDDO !DO bj
                0250 
                0251       NAVGBINRL = NAVGBIN
                0252       _GLOBAL_MAX_RL( NAVGBINRL, myThid )
                0253       NAVGBIN = NAVGBINRL
                0254       DO iavgbin = 1, NAVGBIN
                0255         tmpr6 = avgbinglbsum(iavgbin)
                0256         _GLOBAL_SUM_RL(tmpr6, myThid)
                0257         IF (tmpr6.GT.1.1) avgbinglbsum(iavgbin) = tmpr6
                0258       ENDDO
                0259 
                0260 C Accumulate globally
                0261       DO num_var = 1, NVARMAX
                0262         doglbsum = .FALSE.
                0263         DO bj = 1, nSy
                0264           DO bi = 1, nSx
                0265             DO num_file = 1, NFILESPROFMAX
                0266               IF ( vec_quantities(num_file,num_var,bi,bj) )
                0267      &          doglbsum = .TRUE.
                0268             ENDDO !DO num_file
                0269           ENDDO !DO bi
                0270         ENDDO !DO bj
                0271 
                0272         IF (doglbsum) THEN
                0273           DO iavgbin = 1, NAVGBIN
                0274              DO kC = 1, NLEVELCOMB
                0275                tmpr6 = prof_count1D_all_mean(iavgbin,kC,num_var)
                0276                _GLOBAL_SUM_RL( tmpr6, myThid )
                0277                prof_count1D_all_mean(iavgbin,kC,num_var) = tmpr6
                0278 
                0279                tmpr9 = prof_weights1D_all_mean(iavgbin,kC,num_var)
                0280                _GLOBAL_SUM_RL( tmpr9, myThid )
                0281                prof_weights1D_all_mean(iavgbin,kC,num_var) = tmpr9
                0282 
                0283                tmpr7 = prof_traj1D_all_mean(iavgbin,kC,num_var)
                0284                _GLOBAL_SUM_RL( tmpr7, myThid )
                0285                prof_traj1D_all_mean(iavgbin,kC,num_var) = tmpr7
                0286 
                0287                tmpr8 = prof_data1D_all_mean(iavgbin,kC,num_var)
                0288                _GLOBAL_SUM_RL( tmpr8, myThid )
                0289                prof_data1D_all_mean(iavgbin,kC,num_var) = tmpr8
                0290              ENDDO !DO kC
                0291           ENDDO !DO iavgbin
                0292         ENDIF
                0293       ENDDO !DO num_var
                0294 
                0295 C Now do the averaging
                0296       DO iavgbin = 1, NAVGBIN
                0297         DO kC = 1, NLEVELCOMB
                0298           DO num_var = 1, NVARMAX
                0299             tmp_recip_count = 0. _d 0
                0300             IF (prof_count1D_all_mean(iavgbin,kC,num_var).GT.0) THEN
                0301               tmp_recip_count = 1. _d 0 /
                0302      &           prof_count1D_all_mean(iavgbin,kC,num_var)
                0303 
                0304               prof_traj1D_all_mean(iavgbin,kC,num_var)
                0305      &         = prof_traj1D_all_mean(iavgbin,kC,num_var)*
                0306      &           tmp_recip_count
                0307 
                0308               prof_data1D_all_mean(iavgbin,kC,num_var)
                0309      &         = prof_data1D_all_mean(iavgbin,kC,num_var)*
                0310      &           tmp_recip_count
                0311 
                0312               prof_weights1D_all_mean(iavgbin,kC,num_var)
                0313      &         = prof_weights1D_all_mean(iavgbin,kC,num_var)*
                0314      &           tmp_recip_count
                0315 
                0316             ENDIF
                0317           ENDDO !DO num_var
                0318         ENDDO !DO kC
                0319       ENDDO !DO iavgbin
                0320 
                0321       DO iavgbin = 1, NAVGBIN
                0322         DO kC = 1, NLEVELCOMB
                0323           DO num_var = 1, NVARMAX
                0324             IF (prof_count1D_all_mean(iavgbin,kC,num_var).GT.0) THEN
                0325 C Assuming each averaging bin has a max of 9 independent measurements
                0326               tmp11 = prof_weights1D_all_mean(iavgbin,kC,num_var)
                0327      &         / prof_count1D_all_mean(iavgbin,kC,num_var)
                0328 
                0329               tmp12 = prof_weights1D_all_mean(iavgbin,kC,num_var)
                0330      &         * recip_profiles_mean_indsamples(num_var)
                0331 
                0332               prof_weights1D_all_mean(iavgbin,kC,num_var) =
                0333      &         MAX(tmp11, tmp12)
                0334 
                0335 C prof_weights1D_all_mean is still sigam^2. Convert to weight
                0336               IF (prof_weights1D_all_mean(iavgbin,kC,num_var)
                0337      &            .NE.0. _d 0)
                0338      &          prof_weights1D_all_mean(iavgbin,kC,num_var) =
                0339      &           1. _d 0 / prof_weights1D_all_mean(iavgbin,kC,num_var)
                0340             ENDIF
                0341           ENDDO !DO num_var
                0342         ENDDO !DO kC
                0343       ENDDO !DO iavgbin
                0344 
                0345       IF (myProcId.EQ.0) THEN
                0346         DO num_var = 1, NVARMAX
                0347           iL = ILNBLNK( prof_names(1,num_var) )
                0348 
                0349           WRITE(cfile,'(2A)') prof_names(1,num_var)(1:iL),
                0350      &     '_data_mean.data'
                0351           CALL MDSFINDUNIT( cunit, myThid )
                0352           OPEN( cunit, FILE = cfile, STATUS = 'unknown',
                0353      &          ACCESS = 'direct', RECL = NAVGBINMAX*4 )
                0354 
                0355           DO kC = 1, NLEVELCOMB
                0356             tmp99(1:NAVGBINMAX) =
                0357      &        prof_data1D_all_mean(1:NAVGBINMAX,kC,num_var)
                0358             WRITE(cunit,REC = kC) tmp99
                0359           ENDDO
                0360           CLOSE( cunit )
                0361 
                0362           WRITE(cfile,'(2A)') prof_names(1,num_var)(1:iL),
                0363      &     '_model_mean.data'
                0364           CALL MDSFINDUNIT( cunit, myThid )
                0365           OPEN( cunit, FILE = cfile, STATUS = 'unknown',
                0366      &          ACCESS = 'direct', RECL = NAVGBINMAX*4 )
                0367 
                0368           DO kC = 1, NLEVELCOMB
                0369             tmp99(1:NAVGBINMAX) =
                0370      &        prof_traj1D_all_mean(1:NAVGBINMAX,kC,num_var)
                0371             WRITE(cunit,REC = kC) tmp99
                0372           ENDDO
                0373           CLOSE( cunit )
                0374 
                0375           WRITE(cfile,'(2A)') prof_names(1,num_var)(1:iL),
                0376      &     '_weight_mean.data'
                0377           CALL MDSFINDUNIT( cunit, myThid )
                0378           OPEN( cunit, FILE = cfile, STATUS = 'unknown',
                0379      &          ACCESS = 'direct', RECL = NAVGBINMAX*4 )
                0380 
                0381           DO kC = 1, NLEVELCOMB
                0382             tmp99(1:NAVGBINMAX)=
                0383      &        prof_weights1D_all_mean(1:NAVGBINMAX,kC,num_var)
                0384             WRITE(cunit,REC = kC) tmp99
                0385           ENDDO
                0386           CLOSE( cunit )
                0387 
                0388           WRITE(cfile,'(2A)') prof_names(1,num_var)(1:iL),
                0389      &     '_count_mean.data'
                0390           CALL MDSFINDUNIT( cunit, myThid )
                0391           OPEN( cunit, FILE = cfile, STATUS = 'unknown',
                0392      &          ACCESS = 'direct', RECL = NAVGBINMAX*4 )
                0393 
                0394           DO kC = 1, NLEVELCOMB
                0395             tmp99(1:NAVGBINMAX) =
                0396      &       prof_count1D_all_mean(1:NAVGBINMAX,kC,num_var)
                0397             WRITE(cunit,REC = kC) tmp99
                0398           ENDDO
                0399           CLOSE( cunit )
                0400 
                0401         ENDDO !DO num_var
                0402       ENDIF !IF (myProcId.EQ.0)
                0403 #endif /* ALLOW_PROFILES_SAMPLESPLIT_COST */
                0404 
                0405       DO bj = 1, nSy
                0406         DO bi = 1, nSx
                0407           DO num_file = 1, NFILESPROFMAX
                0408             IF ( (ProfNo(num_file,bi,bj).GT.0) .AND.
                0409      &           (profilesDoNcOutput) ) THEN
                0410 C Sync file so that data is not lost when run finishes
                0411               err = NF_SYNC( fidforward(num_file,bi,bj) )
                0412               CALL PROFILES_NF_ERROR( 'COST: NF_SYNC fidforward',
                0413      &             err,bi,bj,myThid )
                0414 
                0415             ENDIF
                0416 
                0417             DO prof_num = 1, NOBSGLOB
                0418               IF (prof_num.LE.ProfNo(num_file,bi,bj)) THEN
                0419                 DO num_var = 1, NVARMAX
                0420                   DO kLev = 1, NLEVELMAX
                0421                     prof_traj1D(kLev) = 0.
                0422                     prof_traj1D_mean(kLev) = 0.
                0423                     prof_mask1D_cur(kLev,bi,bj) = 0.
                0424                     prof_data1D(kLev) = 0.
                0425                     prof_weights1D(kLev) = 0.
                0426 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0427                     prof_data1D_mean(kLev) = 0.
                0428                     prof_weights1D_mean(kLev) = 0.
                0429 #endif
                0430                   ENDDO
                0431 
                0432                   IF ( vec_quantities(num_file,num_var,bi,bj) ) THEN
                0433 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0434                     itmp = prof_ind_avgbin(num_file,prof_num,bi,bj)
                0435 
                0436                     IF (itmp.GE. 0) THEN
                0437                       DO kProf = 1, ProfDepthNo(num_file,bi,bj)
                0438                         ktmp = prof_lev_comb(kProf,num_file,bi,bj)
                0439 
                0440                         prof_traj1D_mean(kProf) =
                0441      &                    prof_traj1D_all_mean(itmp,ktmp,num_var)
                0442 
                0443                         prof_data1D_mean(kProf) =
                0444      &                    prof_data1D_all_mean(itmp,ktmp,num_var)
                0445 
                0446                         prof_weights1D_mean(kProf) =
                0447      &                    prof_weights1D_all_mean(itmp,ktmp,num_var)
                0448 
                0449                       ENDDO
                0450                     ENDIF !IF (itmp.GE.0)
                0451 #endif /* ALLOW_PROFILES_SAMPLESPLIT_COST */
                0452 
                0453                     CALL ACTIVE_READ_PROFILE( num_file,
                0454      &                   ProfDepthNo(num_file,bi,bj),prof_traj1D,
                0455      &                   num_var,prof_num,.false.,optimcycle,
                0456      &                   bi,bj,myThid,
                0457      &                   profiles_dummy(num_file,num_var,bi,bj) )
                0458 
                0459                     CALL PROFILES_READVECTOR( num_file, num_var,
                0460      &                   prof_ind_glob(num_file,prof_num,bi,bj),
                0461      &                   ProfDepthNo(num_file,bi,bj),prof_data1D,
                0462      &                   bi,bj,myThid )
                0463 
                0464                     CALL PROFILES_READVECTOR( num_file,-num_var,
                0465      &                   prof_ind_glob(num_file,prof_num,bi,bj),
                0466      &                   ProfDepthNo(num_file,bi,bj),prof_weights1D,
                0467      &                   bi,bj,myThid )
                0468 
                0469                     DO kProf = 1, ProfDepthNo(num_file,bi,bj)
                0470                       IF (prof_weights1D(kProf).GT.0.
                0471 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0472      &                    .AND. prof_data1D_mean(kProf).NE. 0. _d 0
                0473 #endif
                0474      &                   ) THEN
                0475                         objf_profiles(num_file,num_var,bi,bj) =
                0476      &                    objf_profiles(num_file,num_var,bi,bj)
                0477      &                    +prof_weights1D(kProf)
                0478      &                    *prof_mask1D_cur(kProf,bi,bj)
                0479      &                    *(prof_traj1D(kProf)-prof_data1D(kProf)
                0480      &                    -prof_traj1D_mean(kProf)
                0481 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0482      &                    +prof_data1D_mean(kProf)
                0483 #endif
                0484      &                    )
                0485      &                    *(prof_traj1D(kProf)-prof_data1D(kProf)
                0486      &                    -prof_traj1D_mean(kProf)
                0487 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0488      &                    + prof_data1D_mean(kProf)
                0489 #endif
                0490      &                    )
                0491 
                0492                         num_profiles(num_file,num_var,bi,bj) =
                0493      &                    num_profiles(num_file,num_var,bi,bj)
                0494      &                    +prof_mask1D_cur(kProf,bi,bj)
                0495 
                0496                       ENDIF
                0497                     ENDDO !DO kProf
                0498 
                0499                   ENDIF !IF (vec_quantitities
                0500 
                0501                 ENDDO !DO num_var
                0502               ENDIF !IF (prof_num.LE.ProfNo(num_file,bi,bj))
                0503             ENDDO !DO prof_num
                0504 
                0505 #ifdef ALLOW_DEBUG
                0506             IF ( debugLevel .GE. debLevD ) THEN
                0507               IF (ProfNo(num_file,bi,bj).GT.0) THEN
                0508                 DO num_var = 1, NVARMAX
                0509                   WRITE(msgBuf,'(A,4I9)') 'bi,bj,prof_num,num_var ',
                0510      &             bi,bj,ProfNo(num_file,bi,bj),num_var
                0511                   CALL PRINT_MESSAGE( msgBuf,
                0512      &                 standardMessageUnit, SQUEEZE_RIGHT , myThid )
                0513                   WRITE(msgBuf,'(A,2D22.15)')
                0514      &             prof_names(num_file,num_var),
                0515      &             objf_profiles(num_file,num_var,bi,bj),
                0516      &             num_profiles(num_file,num_var,bi,bj)
                0517                   CALL PRINT_MESSAGE( msgBuf,
                0518      &                 standardMessageUnit, SQUEEZE_RIGHT , myThid )
                0519                 ENDDO !DO num_var
                0520               ENDIF
                0521             ENDIF
                0522 #endif /* ALLOW_DEBUG */
                0523           ENDDO !DO num_file
                0524 
                0525 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0526           DO num_var = 1, NVARMAX
                0527             DO iavgbin = 1, NAVGBINMAX
                0528               DO kC = 1, NLEVELCOMB
                0529                 prof_traj1D_mean(1) =
                0530      &           prof_traj1D_all_mean(iavgbin,kC,num_var)
                0531                 prof_data1D_mean(1) =
                0532      &           prof_data1D_all_mean(iavgbin,kC,num_var)
                0533                 prof_weights1D_mean(1) =
                0534      &           prof_weights1D_all_mean(iavgbin,kC,num_var)
                0535 
                0536                 IF (prof_weights1D_mean(1).GT.0. .AND.
                0537      &              prof_data1D_mean(1).NE. 0. _d 0 .AND.
                0538      &              prof_traj1D_mean(1).NE. 0. _d 0 .AND.
                0539      &              avgbinglbsum(iavgbin).GT.0 ) THEN
                0540                   IF (avgbinglbsum(iavgbin).EQ.1) THEN
                0541                     objf_profiles_mean(num_var,bi,bj) =
                0542      &                objf_profiles_mean(num_var,bi,bj)
                0543      &                + prof_weights1D_mean(1)
                0544      &                * (prof_traj1D_mean(1) - prof_data1D_mean(1))
                0545      &                * (prof_traj1D_mean(1) - prof_data1D_mean(1))
                0546 
                0547                     num_profiles_mean(num_var,bi,bj) =
                0548      &                num_profiles_mean(num_var,bi,bj) + 1. _d 0
                0549 
                0550                   ELSE
                0551                     objf_profiles_mean(num_var,bi,bj) =
                0552      &                objf_profiles_mean(num_var,bi,bj)
                0553      &                + prof_weights1D_mean(1)
                0554      &                * (prof_traj1D_mean(1) - prof_data1D_mean(1))
                0555      &                * (prof_traj1D_mean(1) - prof_data1D_mean(1))
                0556      &                / numberOfProcs
                0557 
                0558                     num_profiles_mean(num_var,bi,bj) =
                0559      &               num_profiles_mean(num_var,bi,bj) + 1. _d 0
                0560      &               /numberOfProcs
                0561 
                0562                   ENDIF !IF (avgbinglbsum(iavgbin).EQ.1)
                0563                  ENDIF !IF (prof_weights1D_mean(1).GT.0.)
                0564 
                0565               ENDDO !DO kC
                0566             ENDDO !DO iavgbin
                0567           ENDDO !DO num_var
                0568 
                0569 # ifdef ALLOW_DEBUG
                0570           IF ( debugLevel .GE. debLevD ) THEN
                0571             DO num_var = 1, NVARMAX
                0572               WRITE(msgBuf,'(A,4I9)') 'bi,bj,num_var ',bi,bj,num_var
                0573               CALL PRINT_MESSAGE( msgBuf,
                0574      &             standardMessageUnit, SQUEEZE_RIGHT , myThid )
                0575 
                0576               WRITE(msgBuf,'(A,A5,2D22.15)') prof_names(1,num_var),
                0577      &         '_mean',
                0578      &         objf_profiles_mean(num_var,bi,bj),
                0579      &         num_profiles_mean(num_var,bi,bj)
                0580               CALL PRINT_MESSAGE( msgBuf,
                0581      &             standardMessageUnit, SQUEEZE_RIGHT , myThid )
                0582 
                0583             ENDDO !DO num_var
                0584           ENDIF
                0585 # endif /* ALLOW_DEBUG */
                0586 
                0587 #endif /* ALLOW_PROFILES_SAMPLESPLIT_COST */
                0588 
                0589         ENDDO !DO bi
                0590       ENDDO !DO bj
                0591 
                0592       _END_MASTER( myThid )
                0593 
                0594 C Print cost function values
                0595       DO num_file = 1, NFILESPROFMAX
                0596         DO num_var = 1, NVARMAX
                0597 
                0598           DO bj = myByLo(myThid), myByHi(myThid)
                0599             DO bi = myBxLo(myThid), myBxHi(myThid)
                0600               objf_prof_tile(bi,bj) =
                0601      &          objf_profiles(num_file,num_var,bi,bj)
                0602 
                0603               num_prof_tile(bi,bj) =
                0604      &          num_profiles(num_file,num_var,bi,bj)
                0605 
                0606            ENDDO
                0607           ENDDO
                0608 
                0609         CALL GLOBAL_SUM_TILE_RL( objf_prof_tile, objf_prof_glo,
                0610      &       myThid )
                0611         CALL GLOBAL_SUM_TILE_RL( num_prof_tile, num_prof_glo,
                0612      &       myThid )
                0613 
                0614         WRITE(msgBuf,'(2(A,I2),A,2D22.15)')
                0615      &   ' profiles_cost(',num_file,',',num_var,') = ',
                0616      &   objf_prof_glo,num_prof_glo
                0617 
                0618         IF (num_prof_glo .GT. 0.)
                0619      &    CALL PRINT_MESSAGE( msgBuf,
                0620      &         standardMessageUnit, SQUEEZE_RIGHT , myThid )
                0621 
                0622         ENDDO !DO num_var
                0623       ENDDO !DO num_file
                0624 
                0625 #ifdef ALLOW_PROFILES_SAMPLESPLIT_COST
                0626       DO num_var = 1, NVARMAX
                0627 
                0628         DO bj = myByLo(myThid), myByHi(myThid)
                0629           DO bi = myBxLo(myThid), myBxHi(myThid)
                0630             objf_prof_mean_tile(bi,bj) =
                0631      &        objf_profiles_mean(num_var,bi,bj)
                0632 
                0633             num_prof_mean_tile(bi,bj) =
                0634      &        num_profiles_mean(num_var,bi,bj)
                0635 
                0636          ENDDO
                0637         ENDDO
                0638 
                0639         CALL GLOBAL_SUM_TILE_RL( objf_prof_mean_tile,
                0640      &       objf_prof_mean_glo, myThid )
                0641         CALL GLOBAL_SUM_TILE_RL( num_prof_mean_tile,
                0642      &       num_prof_mean_glo, myThid )
                0643 
                0644         WRITE(msgBuf,'(A,I2,A,2D22.15)')
                0645      &   ' profiles_cost_mean(',num_var,') = ',
                0646      &   objf_prof_mean_glo,num_prof_mean_glo
                0647 
                0648         IF (num_prof_mean_glo .GT. 0.)
                0649      &    CALL PRINT_MESSAGE( msgBuf,
                0650      &         standardMessageUnit, SQUEEZE_RIGHT, myThid )
                0651 
                0652       ENDDO !DO num_var
                0653 #endif /* ALLOW_PROFILES_SAMPLESPLIT_COST */
                0654 
                0655       WRITE(msgBuf,'(A)') '== profiles_cost: end   =='
                0656       CALL PRINT_MESSAGE( msgBuf,
                0657      &     standardMessageUnit, SQUEEZE_RIGHT , myThid )
                0658       WRITE(msgBuf,'(A)') ' '
                0659       CALL PRINT_MESSAGE( msgBuf,
                0660      &     standardMessageUnit, SQUEEZE_RIGHT , myThid )
                0661 
                0662       IF (prof_make_nc) THEN
                0663         CALL PROFILES_MAKE_NCFILE( myThid )
                0664       ENDIF
                0665 
                0666 #endif /* ALLOW_PROFILES */
                0667 
                0668       RETURN
                0669       END