| 1 | C $Header: /u/gcmpack/MITgcm/pkg/seaice/seaice_cost_sss.F,v 1.2 2010/03/15 22:33:41 heimbach Exp $ | 
| 2 | C $Name:  $ | 
| 3 |  | 
| 4 | #include "SEAICE_OPTIONS.h" | 
| 5 |  | 
| 6 | c read the area dat file and compare against the averaged salinity file | 
| 7 |  | 
| 8 | subroutine seaice_cost_sss( | 
| 9 | &     nnzbar, localbarfile, localbar, xx_localbar_mean_dummy, | 
| 10 | &             areabarfile,   areabar, xx_areabar_mean_dummy, | 
| 11 | &     nnzobs, localobsfile, localobs, mult_local, | 
| 12 | &     nrecloc, localstartdate, localperiod, | 
| 13 | &     localmask, localweight, | 
| 14 | &     spminloc, spmaxloc, spzeroloc, | 
| 15 | &     objf_local, num_local, | 
| 16 | &     myiter, mytime, mythid ) | 
| 17 |  | 
| 18 | implicit none | 
| 19 |  | 
| 20 | c ian fenty | 
| 21 | c     == global variables == | 
| 22 |  | 
| 23 | #include "EEPARAMS.h" | 
| 24 | #include "SIZE.h" | 
| 25 | #include "PARAMS.h" | 
| 26 | #ifdef ALLOW_CAL | 
| 27 | # include "cal.h" | 
| 28 | #endif | 
| 29 | #ifdef ALLOW_COST | 
| 30 | # include "ecco_cost.h" | 
| 31 | # include "optim.h" | 
| 32 | # ifdef ALLOW_SEAICE | 
| 33 | #  include "SEAICE_COST.h" | 
| 34 | #  include "SEAICE_PARAMS.h" | 
| 35 | # endif | 
| 36 | #endif | 
| 37 |  | 
| 38 | c     == routine arguments == | 
| 39 |  | 
| 40 | integer nnzbar | 
| 41 | integer nnzobs | 
| 42 | integer nrecloc | 
| 43 | integer myiter | 
| 44 | integer mythid | 
| 45 | integer localstartdate(4) | 
| 46 |  | 
| 47 | _RL localbar   (1-olx:snx+olx,1-oly:sny+oly,nnzbar,nsx,nsy) | 
| 48 | _RL areabar   (1-olx:snx+olx,1-oly:sny+oly,nnzbar,nsx,nsy) | 
| 49 |  | 
| 50 | _RL localobs   (1-olx:snx+olx,1-oly:sny+oly,nnzobs,nsx,nsy) | 
| 51 | _RL localweight(1-olx:snx+olx,1-oly:sny+oly,nsx,nsy) | 
| 52 | _RL localmask  (1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy) | 
| 53 | _RL xx_localbar_mean_dummy | 
| 54 | _RL xx_areabar_mean_dummy | 
| 55 | _RL mult_local | 
| 56 | _RL mytime | 
| 57 | _RL localperiod | 
| 58 | _RL spminloc | 
| 59 | _RL spmaxloc | 
| 60 | _RL spzeroloc | 
| 61 | _RL objf_local(nsx,nsy) | 
| 62 | _RL num_local(nsx,nsy) | 
| 63 |  | 
| 64 | character*(MAX_LEN_FNAM) areabarfile | 
| 65 | character*(MAX_LEN_FNAM) localbarfile | 
| 66 | character*(MAX_LEN_FNAM) localobsfile | 
| 67 |  | 
| 68 | #ifdef ALLOW_COST | 
| 69 | c     == local variables == | 
| 70 |  | 
| 71 | integer bi,bj | 
| 72 | integer i,j,k | 
| 73 | integer itlo,ithi | 
| 74 | integer jtlo,jthi | 
| 75 | integer jmin,jmax | 
| 76 | integer imin,imax | 
| 77 | integer irec | 
| 78 | integer  il | 
| 79 | integer localrec | 
| 80 | integer obsrec | 
| 81 |  | 
| 82 | logical doglobalread | 
| 83 | logical ladinit | 
| 84 |  | 
| 85 | _RL spval | 
| 86 | parameter (spval = -9999. ) | 
| 87 | _RL localwww | 
| 88 | _RL localcost | 
| 89 | _RL junk | 
| 90 |  | 
| 91 | _RL cmask (1-olx:snx+olx,1-oly:sny+oly,nnzobs) | 
| 92 |  | 
| 93 | character*(128) fname1, fname2,fname3 | 
| 94 | character*(MAX_LEN_MBUF) msgbuf | 
| 95 |  | 
| 96 | cnew( | 
| 97 | _RL daytime | 
| 98 | _RL diffsecs | 
| 99 | integer dayiter | 
| 100 | integer daydate(4) | 
| 101 | integer difftime(4) | 
| 102 | integer middate(4) | 
| 103 | integer yday, ymod | 
| 104 | integer md, dd, sd, ld, wd | 
| 105 | integer mody, modm | 
| 106 | integer beginmodel, beginlocal | 
| 107 | logical exst | 
| 108 | cnew) | 
| 109 |  | 
| 110 | c     == external functions == | 
| 111 |  | 
| 112 | integer  ilnblnk | 
| 113 | external ilnblnk | 
| 114 |  | 
| 115 | c     == end of interface == | 
| 116 |  | 
| 117 | jtlo = mybylo(mythid) | 
| 118 | jthi = mybyhi(mythid) | 
| 119 | itlo = mybxlo(mythid) | 
| 120 | ithi = mybxhi(mythid) | 
| 121 | jmin = 1 | 
| 122 | jmax = sny | 
| 123 | imin = 1 | 
| 124 | imax = snx | 
| 125 |  | 
| 126 | c--   Initialise local variables. | 
| 127 |  | 
| 128 | localwww = 0. _d 0 | 
| 129 |  | 
| 130 | do bj = jtlo,jthi | 
| 131 | do bi = itlo,ithi | 
| 132 | objf_local(bi,bj) = 0. _d 0 | 
| 133 | num_local(bi,bj) = 0. _d 0 | 
| 134 | enddo | 
| 135 | enddo | 
| 136 |  | 
| 137 | c--   First, read tiled data. | 
| 138 | doglobalread = .false. | 
| 139 | ladinit      = .false. | 
| 140 |  | 
| 141 |  | 
| 142 | write(fname1(1:128),'(80a)') ' ' | 
| 143 | csss | 
| 144 | write(fname3(1:128),'(80a)') ' ' | 
| 145 |  | 
| 146 | il=ilnblnk( localbarfile ) | 
| 147 | write(fname1(1:128),'(2a,i10.10)') | 
| 148 | &     localbarfile(1:il),'.',optimcycle | 
| 149 |  | 
| 150 | csss | 
| 151 | il=ilnblnk( areabarfile ) | 
| 152 | write(fname3(1:128),'(2a,i10.10)') | 
| 153 | &     areabarfile(1:il),'.',optimcycle | 
| 154 |  | 
| 155 | if ( .NOT. ( localobsfile.EQ.' ' ) ) then | 
| 156 |  | 
| 157 | c--   Loop over records for the second time. | 
| 158 | do irec = 1, nrecloc | 
| 159 |  | 
| 160 | if ( nnzbar .EQ. 1 ) then | 
| 161 | call active_read_xy( fname1, localbar, irec, doglobalread, | 
| 162 | &                      ladinit, optimcycle, mythid, | 
| 163 | &                      xx_localbar_mean_dummy ) | 
| 164 | else | 
| 165 | call active_read_xyz( fname1, localbar, irec, doglobalread, | 
| 166 | &                       ladinit, optimcycle, mythid, | 
| 167 | &                       xx_localbar_mean_dummy ) | 
| 168 | endif | 
| 169 |  | 
| 170 | csss | 
| 171 | if ( nnzbar .EQ. 1 ) then | 
| 172 | call active_read_xy( fname3, areabar, irec, doglobalread, | 
| 173 | &                      ladinit, optimcycle, mythid, | 
| 174 | &                      xx_areabar_mean_dummy ) | 
| 175 | else | 
| 176 | call active_read_xyz( fname3, areabar, irec, doglobalread, | 
| 177 | &                       ladinit, optimcycle, mythid, | 
| 178 | &                       xx_areabar_mean_dummy ) | 
| 179 | endif | 
| 180 |  | 
| 181 | cnew( | 
| 182 | obsrec = irec | 
| 183 |  | 
| 184 | daytime = FLOAT(secondsperday*(irec-1)) + modelstart | 
| 185 | dayiter = hoursperday*(irec-1)+modeliter0 | 
| 186 |  | 
| 187 | call cal_getdate( dayiter, daytime, daydate, mythid ) | 
| 188 | call cal_convdate( daydate,yday,md,dd,sd,ld,wd,mythid ) | 
| 189 | ymod = smrareastartdate(1)/10000 | 
| 190 |  | 
| 191 | #ifdef SEAICE_DEBUG | 
| 192 | print *,'cost_seaice_sss' | 
| 193 | print *,'daydate        ', daydate | 
| 194 | print *,'areaobsfile:  ', localobsfile | 
| 195 | print *,'nrecloc        ', nrecloc | 
| 196 | print *,'obsrec,daytime ', obsrec,daytime | 
| 197 | print *,'dayiter        ', dayiter | 
| 198 | print *,'yday :         ', yday | 
| 199 | print *,'md,dd,sd       ', md,dd,sd | 
| 200 | print *,'ld,wd          ', ld,wd | 
| 201 | print *,'loclstrtdte(1) ', localstartdate(1) | 
| 202 | print *,'ymod, yday     ', ymod,yday | 
| 203 | print *,'smrarstrtdt(1) ', smrareastartdate(1) | 
| 204 | print *,'smrarstartdate ', smrareastartdate | 
| 205 | #endif /* SEAICE_DEBUG */ | 
| 206 |  | 
| 207 | if ( ymod .EQ. yday ) then | 
| 208 | middate(1) = smrareastartdate(1) | 
| 209 | else | 
| 210 | middate(1) = yday*10000+100+1 | 
| 211 | endif | 
| 212 | middate(2) = 0 | 
| 213 | middate(3) = modelstartdate(3) | 
| 214 | middate(4) = modelstartdate(4) | 
| 215 |  | 
| 216 |  | 
| 217 | call cal_TimePassed( middate, daydate, difftime, mythid ) | 
| 218 | call cal_ToSeconds( difftime, diffsecs, mythid ) | 
| 219 |  | 
| 220 | localrec = int(diffsecs/localperiod) + 1 | 
| 221 |  | 
| 222 | #ifdef SEAICE_DEBUG | 
| 223 | print *,'middate(1,2)     ',middate(1),middate(2) | 
| 224 | print *,'middate(3,4)     ', middate(3),middate(4) | 
| 225 | print *,'difftime,diffsecs',difftime,diffsecs | 
| 226 | print *,'localrec         ',localrec | 
| 227 | #endif | 
| 228 |  | 
| 229 | il=ilnblnk(localobsfile) | 
| 230 | write(fname2(1:128),'(2a,i4)') | 
| 231 | &      localobsfile(1:il), '_', yday | 
| 232 | inquire( file=fname2, exist=exst ) | 
| 233 |  | 
| 234 | #ifdef SEAICE_DEBUG | 
| 235 | print *,'fname2',fname2 | 
| 236 | #endif | 
| 237 | if (.NOT. exst) then | 
| 238 | write(fname2(1:128),'(a)') localobsfile(1:il) | 
| 239 | localrec = obsrec | 
| 240 | #ifdef SEAICE_DEBUG | 
| 241 | print *,'localrec ', localrec | 
| 242 | print *,'not exist' | 
| 243 | #endif | 
| 244 | endif | 
| 245 |  | 
| 246 | if ( localrec .GT. 0 ) then | 
| 247 |  | 
| 248 | #ifdef SEAICE_DEBUG | 
| 249 | print *,'calling mdsreadfile',fname2,localrec | 
| 250 | #endif | 
| 251 |  | 
| 252 | call mdsreadfield( fname2, cost_iprec, cost_yftype, nnzobs, | 
| 253 | &         localobs, localrec, mythid ) | 
| 254 | else | 
| 255 | do bj = jtlo,jthi | 
| 256 | do bi = itlo,ithi | 
| 257 | do k = 1,nnzobs | 
| 258 | do j = jmin,jmax | 
| 259 | do i = imin,imax | 
| 260 | localobs(i,j,k,bi,bj) = spval | 
| 261 | enddo | 
| 262 | enddo | 
| 263 | enddo | 
| 264 | enddo | 
| 265 | enddo | 
| 266 | endif | 
| 267 | cnew) | 
| 268 |  | 
| 269 | #ifdef SEAICE_DEBUG | 
| 270 | do bj = jtlo,jthi | 
| 271 | do bi = itlo,ithi | 
| 272 | do k = 1,nnzobs | 
| 273 | do i = imin,imax | 
| 274 | do j = jmin,jmax | 
| 275 | if (localobs(i,j,k,bi,bj) .LT. -1) THEN | 
| 276 | print *,'obs rec date: ', -localobs(i,j,1,bi,bj) | 
| 277 | endif | 
| 278 | enddo | 
| 279 | enddo | 
| 280 | enddo | 
| 281 | enddo | 
| 282 | enddo | 
| 283 | #endif | 
| 284 |  | 
| 285 | do bj = jtlo,jthi | 
| 286 | do bi = itlo,ithi | 
| 287 |  | 
| 288 | localcost    = 0. _d 0 | 
| 289 |  | 
| 290 | c--           Determine the mask on weights | 
| 291 | do k = 1,nnzobs | 
| 292 | do j = jmin,jmax | 
| 293 | do i = imin,imax | 
| 294 | cmask(i,j,k) = cosphi(i,j,bi,bj)*localmask(i,j,k,bi,bj) | 
| 295 | if ( localobs(i,j,k,bi,bj) .lt. spminloc .or. | 
| 296 | &               localobs(i,j,k,bi,bj) .gt. spmaxloc .or. | 
| 297 | &               localobs(i,j,k,bi,bj) .eq. spzeroloc ) then | 
| 298 | cmask(i,j,k) = 0. _d 0 | 
| 299 | endif | 
| 300 | enddo | 
| 301 | enddo | 
| 302 | enddo | 
| 303 | c-- | 
| 304 | do k = 1,nnzobs | 
| 305 | do j = jmin,jmax | 
| 306 | do i = imin,imax | 
| 307 | localwww  = localweight(i,j,bi,bj)*cmask(i,j,k) | 
| 308 |  | 
| 309 | c only accumulate cost if there is ice observed but not simulated.. | 
| 310 | if ( localobs(i,j,k,bi,bj) .GT. 0.0 .AND. | 
| 311 | &                areabar(i,j,1,bi,bj) .LE. 0.0) then | 
| 312 |  | 
| 313 | c                if ( localobs(i,j,k,bi,bj) .GT. | 
| 314 | c     &               areabar(i,j,1,bi,bj)) then | 
| 315 |  | 
| 316 | junk = ( localbar(i,j,k,bi,bj) - SEAICE_clamp_salt ) | 
| 317 |  | 
| 318 | c                   junk = localbar(i,j,k,bi,bj) - | 
| 319 | c     &                         ( localbar(i,j,k,bi,bj) - 1.0) | 
| 320 | else | 
| 321 | junk = 0.0 | 
| 322 | endif | 
| 323 |  | 
| 324 | localcost = localcost + junk*junk*localwww | 
| 325 |  | 
| 326 | #ifdef SEAICE_DEBUG | 
| 327 | if ((i == SEAICE_debugPointX) .and. | 
| 328 | &              (j == SEAICE_debugPointY)) then | 
| 329 |  | 
| 330 | print '(A,2i4,3(1x,1P2E15.3))', | 
| 331 | &                 'costg i j sbar, abar,obs     ',i,j, | 
| 332 | &                 localbar(i,j,k,bi,bj), | 
| 333 | &                 areabar(i,j,k,bi,bj), | 
| 334 | &                 localobs(i,j,k,bi,bj) | 
| 335 |  | 
| 336 | print '(A,2i4,2(1x,1P2E15.3))', | 
| 337 | &                 'costg i j bar-obs,wgt,loCost ',i,j, | 
| 338 | &                 junk, | 
| 339 | &                 localwww, | 
| 340 | &                 localcost | 
| 341 | endif | 
| 342 | #endif | 
| 343 |  | 
| 344 | if ( localwww .ne. 0. ) | 
| 345 | &               num_local(bi,bj) = num_local(bi,bj) + 1. _d 0 | 
| 346 | enddo | 
| 347 | enddo | 
| 348 | enddo | 
| 349 |  | 
| 350 | objf_local(bi,bj) = objf_local(bi,bj) + localcost | 
| 351 |  | 
| 352 | enddo !/bj | 
| 353 | enddo !/ bi | 
| 354 |  | 
| 355 | enddo | 
| 356 | c--   End of second loop over records. | 
| 357 |  | 
| 358 | c--   End of mult_local or localobsfile | 
| 359 | endif | 
| 360 | #endif /* ifdef ALLOW_COST */ | 
| 361 |  | 
| 362 | end |