global_cycle  1.4.0
 All Data Structures Files Functions Variables
sfcsub.F
Go to the documentation of this file.
1 C> @file
2 C> @brief This is a limited point version of surface program.
3 C> @author Shrinivas Moorthi Mark Iredell NOAA/EMC
4 
47  implicit none
48  save
49 !
50 ! grib code for each parameter - used in subroutines sfccycle and setrmsk.
51 !
52  integer kpdtsf
53  integer kpdwet
54  integer kpdsno
56  integer kpdzor
57  integer kpdais
58  integer kpdtg3
60  integer kpdplr
61  integer kpdgla
62  integer kpdmxi
63  integer kpdscv
64  integer kpdsmc
65  integer kpdoro
66  integer kpdmsk
67  integer kpdstc
68  integer kpdacn
69  integer kpdveg
70  integer kpdvet
71  integer kpdsot
72  integer kpdvmn
74  integer kpdvmx
76  integer kpdslp
77  integer kpdabs
78  integer kpdsnd
79  integer kpdabs_0
81  integer kpdabs_1
83  integer kpdalb(4)
85  parameter(kpdtsf=11, kpdwet=86, kpdsno=65, kpdzor=83,
86 ! 1 kpdalb=84, kpdais=91, kpdtg3=11, kpdplr=224,
87  1 kpdais=91, kpdtg3=11, kpdplr=224,
88  2 kpdgla=238, kpdmxi=91, kpdscv=238, kpdsmc=144,
89  3 kpdoro=8, kpdmsk=81, kpdstc=11, kpdacn=91, kpdveg=87,
90 !cbosu max snow albedo uses a grib id number of 159, not 255.
91  & kpdvmn=255, kpdvmx=255,kpdslp=236, kpdabs_0=255,
92  & kpdvet=225, kpdsot=224,kpdabs_1=159,
93  & kpdsnd=66 )
94 !
95  integer, parameter :: kpdalb_0(4)=(/212,215,213,216/)
98  integer, parameter :: kpdalb_1(4)=(/189,190,191,192/)
101  integer, parameter :: kpdalf(2)=(/214,217/)
104  integer, parameter :: xdata=5000
105  integer, parameter :: ydata=2500
106  integer, parameter :: mdata=xdata*ydata
107  integer :: veg_type_landice
109  integer :: soil_type_landice
111  end module sfccyc_module
112 
175  subroutine sfccycle(lugb,len,lsoil,sig1t,deltsfc
176  &, iy,im,id,ih,fh
177  &, rla, rlo, slmask,orog,orog_uf,use_ufo,nst_anl
178  &, sihfcs,sicfcs,sitfcs
179  &, swdfcs,slcfcs
180  &, vmnfcs,vmxfcs,slpfcs,absfcs
181  &, tsffcs,snofcs,zorfcs,albfcs,tg3fcs
182  &, cnpfcs,smcfcs,stcfcs,slifcs,aisfcs
183  &, vegfcs,vetfcs,sotfcs,alffcs
184  &, cvfcs,cvbfcs,cvtfcs,me,nlunit
185  &, sz_nml,input_nml_file
186  &, ialb,isot,ivegsrc,tile_num_ch,i_index,j_index)
187 !
188  use machine , only : kind_io8,kind_io4
189  use sfccyc_module
190  implicit none
191  character(len=*), intent(in) :: tile_num_ch
192  integer,intent(in) :: i_index(len), j_index(len)
193  logical use_ufo, nst_anl
194  real (kind=kind_io8) sllnd,slsea,aicice,aicsea,tgice,rlapse,
195  & orolmx,orolmn,oroomx,oroomn,orosmx,
196  & orosmn,oroimx,oroimn,orojmx,orojmn,
197  & alblmx,alblmn,albomx,albomn,albsmx,
198  & albsmn,albimx,albimn,albjmx,albjmn,
199  & wetlmx,wetlmn,wetomx,wetomn,wetsmx,
200  & wetsmn,wetimx,wetimn,wetjmx,wetjmn,
201  & snolmx,snolmn,snoomx,snoomn,snosmx,
202  & snosmn,snoimx,snoimn,snojmx,snojmn,
203  & zorlmx,zorlmn,zoromx,zoromn,zorsmx,
204  & zorsmn,zorimx,zorimn,zorjmx, zorjmn,
205  & plrlmx,plrlmn,plromx,plromn,plrsmx,
206  & plrsmn,plrimx,plrimn,plrjmx,plrjmn,
207  & tsflmx,tsflmn,tsfomx,tsfomn,tsfsmx,
208  & tsfsmn,tsfimx,tsfimn,tsfjmx,tsfjmn,
209  & tg3lmx,tg3lmn,tg3omx,tg3omn,tg3smx,
210  & tg3smn,tg3imx,tg3imn,tg3jmx,tg3jmn,
211  & stclmx,stclmn,stcomx,stcomn,stcsmx,
212  & stcsmn,stcimx,stcimn,stcjmx,stcjmn,
213  & smclmx,smclmn,smcomx,smcomn,smcsmx,
214  & smcsmn,smcimx,smcimn,smcjmx,smcjmn,
215  & scvlmx,scvlmn,scvomx,scvomn,scvsmx,
216  & scvsmn,scvimx,scvimn,scvjmx,scvjmn,
217  & veglmx,veglmn,vegomx,vegomn,vegsmx,
218  & vegsmn,vegimx,vegimn,vegjmx,vegjmn,
219  & vetlmx,vetlmn,vetomx,vetomn,vetsmx,
220  & vetsmn,vetimx,vetimn,vetjmx,vetjmn,
221  & sotlmx,sotlmn,sotomx,sotomn,sotsmx,
222  & sotsmn,sotimx,sotimn,sotjmx,sotjmn,
223  & alslmx,alslmn,alsomx,alsomn,alssmx,
224  & alssmn,alsimx,alsimn,alsjmx,alsjmn,
225  & epstsf,epsalb,epssno,epswet,epszor,
226  & epsplr,epsoro,epssmc,epsscv,eptsfc,
227  & epstg3,epsais,epsacn,epsveg,epsvet,
228  & epssot,epsalf,qctsfs,qcsnos,qctsfi,
229  & aislim,snwmin,snwmax,cplrl,cplrs,
230  & cvegl,czors,csnol,csnos,czorl,csots,
231  & csotl,cvwgs,cvetl,cvets,calfs,
232  & fcalfl,fcalfs,ccvt,ccnp,ccv,ccvb,
233  & calbl,calfl,calbs,ctsfs,grboro,
234  & grbmsk,ctsfl,deltf,caisl,caiss,
235  & fsalfl,fsalfs,flalfs,falbl,ftsfl,
236  & ftsfs,fzorl,fzors,fplrl,fsnos,faisl,
237  & faiss,fsnol,bltmsk,falbs,cvegs,percrit,
238  & deltsfc,critp2,critp3,blnmsk,critp1,
239  & fcplrl,fcplrs,fczors,fvets,fsotl,fsots,
240  & fvetl,fplrs,fvegl,fvegs,fcsnol,fcsnos,
241  & fczorl,fcalbs,fctsfl,fctsfs,fcalbl,
242  & falfs,falfl,fh,crit,zsca,ztsfc,tem1,tem2
243  &, fsihl,fsihs,fsicl,fsics,
244  & csihl,csihs,csicl,csics,epssih,epssic
245  &, fvmnl,fvmns,fvmxl,fvmxs,fslpl,fslps,
246  & fabsl,fabss,cvmnl,cvmns,cvmxl,cvmxs,
247  & cslpl,cslps,cabsl,cabss,epsvmn,epsvmx,
248  & epsslp,epsabs
249  &, sihlmx,sihlmn,sihomx,sihomn,sihsmx,
250  & sihsmn,sihimx,sihimn,sihjmx,sihjmn,
251  & siclmx,siclmn,sicomx,sicomn,sicsmx,
252  & sicsmn,sicimx,sicimn,sicjmx,sicjmn
253  &, glacir_hice
254  &, vmnlmx,vmnlmn,vmnomx,vmnomn,vmnsmx,
255  & vmnsmn,vmnimx,vmnimn,vmnjmx,vmnjmn,
256  & vmxlmx,vmxlmn,vmxomx,vmxomn,vmxsmx,
257  & vmxsmn,vmximx,vmximn,vmxjmx,vmxjmn,
258  & slplmx,slplmn,slpomx,slpomn,slpsmx,
259  & slpsmn,slpimx,slpimn,slpjmx,slpjmn,
260  & abslmx,abslmn,absomx,absomn,abssmx,
261  & abssmn,absimx,absimn,absjmx,absjmn
262  &, sihnew
263 
264  integer imsk,jmsk,ifp,irtscv,irtacn,irtais,irtsno,irtzor,
265  & irtalb,irtsot,irtalf,j,irtvet,irtsmc,irtstc,irtveg,
266  & irtwet,k,iprnt,kk,irttsf,iret,i,igrdbg,iy,im,id,
267  & icalbl,icalbs,icalfl,ictsfs,lugb,len,lsoil,ih,
268  & ictsfl,iczors,icplrl,icplrs,iczorl,icalfs,icsnol,
269  & icsnos,irttg3,me,kqcm,nlunit,sz_nml,ialb
270  &, irtvmn, irtvmx, irtslp, irtabs, isot, ivegsrc
271  logical gausm, deads, qcmsk, znlst, monclm, monanl,
272  & monfcs, monmer, mondif, landice
273  character(len=*), intent(in) :: input_nml_file(sz_nml)
274 
275  integer num_parthds
276 !
277 ! variable naming conventions:
278 !
279 ! oro .. orography
280 ! alb .. albedo
281 ! wet .. soil wetness as defined for bucket model
282 ! sno .. snow depth
283 ! zor .. surface roughness length
284 ! vet .. vegetation type
285 ! plr .. plant evaporation resistance
286 ! tsf .. surface skin temperature. sea surface temp. over ocean.
287 ! tg3 .. deep soil temperature (at 500cm)
288 ! stc .. soil temperature (lsoil layrs)
289 ! smc .. soil moisture (lsoil layrs)
290 ! scv .. snow cover (not snow depth)
291 ! ais .. sea ice mask (0 or 1)
292 ! acn .. sea ice concentration (fraction)
293 ! gla .. glacier (permanent snow) mask (0 or 1)
294 ! mxi .. maximum sea ice extent (0 or 1)
295 ! msk .. land ocean mask (0=ocean 1=land)
296 ! cnp .. canopy water content
297 ! cv .. convective cloud cover
298 ! cvb .. convective cloud base
299 ! cvt .. convective cloud top
300 ! sli .. land/sea/sea-ice mask. (1/0/2 respectively)
301 ! veg .. vegetation cover
302 ! sot .. soil type
303 !cwu [+2l] add sih & sic
304 ! sih .. sea ice thickness
305 ! sic .. sea ice concentration
306 !clu [+6l] add swd,slc,vmn,vmx,slp,abs
307 ! swd .. actual snow depth
308 ! slc .. liquid soil moisture (lsoil layers)
309 ! vmn .. vegetation cover minimum
310 ! vmx .. vegetation cover maximum
311 ! slp .. slope type
312 ! abs .. maximum snow albedo
313 
314 !
315 ! definition of land/sea mask. sllnd for land and slsea for sea.
316 ! definition of sea/ice mask. aicice for ice, aicsea for sea.
317 ! tgice=max ice temperature
318 ! rlapse=lapse rate for sst correction due to surface angulation
319 !
320  parameter(sllnd =1.0,slsea =0.0)
321  parameter(aicice=1.0,aicsea=0.0)
322  parameter(tgice=271.2)
323  parameter(rlapse=0.65e-2)
324 !
325 ! max/min of fields for check and replace.
326 !
327 ! ???lmx .. max over bare land
328 ! ???lmn .. min over bare land
329 ! ???omx .. max over open ocean
330 ! ???omn .. min over open ocean
331 ! ???smx .. max over snow surface (land and sea-ice)
332 ! ???smn .. min over snow surface (land and sea-ice)
333 ! ???imx .. max over bare sea ice
334 ! ???imn .. min over bare sea ice
335 ! ???jmx .. max over snow covered sea ice
336 ! ???jmn .. min over snow covered sea ice
337 !
338  parameter(orolmx=8000.,orolmn=-1000.,oroomx=3000.,oroomn=-1000.,
339  & orosmx=8000.,orosmn=-1000.,oroimx=3000.,oroimn=-1000.,
340  & orojmx=3000.,orojmn=-1000.)
341 ! parameter(alblmx=0.80,alblmn=0.06,albomx=0.06,albomn=0.06,
342 ! & albsmx=0.80,albsmn=0.06,albimx=0.80,albimn=0.80,
343 ! & albjmx=0.80,albjmn=0.80)
344 !cwu [-3l/+9l] change min/max for alb; add min/max for sih & sic
345 ! parameter(alblmx=0.80,alblmn=0.01,albomx=0.01,albomn=0.01,
346 ! & albsmx=0.80,albsmn=0.01,albimx=0.01,albimn=0.01,
347 ! & albjmx=0.01,albjmn=0.01)
348 ! note: the range values for bare land and snow covered land
349 ! (alblmx, alblmn, albsmx, albsmn) are set below
350 ! based on whether the old or new radiation is selected
351  parameter(albomx=0.06,albomn=0.06,
352  & albimx=0.80,albimn=0.06,
353  & albjmx=0.80,albjmn=0.06)
354  parameter(sihlmx=0.0,sihlmn=0.0,sihomx=5.0,sihomn=0.0,
355  & sihsmx=5.0,sihsmn=0.0,sihimx=5.0,sihimn=0.10,
356  & sihjmx=5.0,sihjmn=0.10,glacir_hice=3.0)
357 !cwu change sicimn & sicjmn Jan 2015
358 ! parameter(siclmx=0.0,siclmn=0.0,sicomx=1.0,sicomn=0.0,
359 ! & sicsmx=1.0,sicsmn=0.0,sicimx=1.0,sicimn=0.50,
360 ! & sicjmx=1.0,sicjmn=0.50)
361 !
362 ! parameter(sihlmx=0.0,sihlmn=0.0,sihomx=8.0,sihomn=0.0,
363 ! & sihsmx=8.0,sihsmn=0.0,sihimx=8.0,sihimn=0.10,
364 ! & sihjmx=8.0,sihjmn=0.10,glacir_hice=3.0)
365  parameter(siclmx=0.0,siclmn=0.0,sicomx=1.0,sicomn=0.0,
366  & sicsmx=1.0,sicsmn=0.0,sicimx=1.0,sicimn=0.15,
367  & sicjmx=1.0,sicjmn=0.15)
368 
369  parameter(wetlmx=0.15,wetlmn=0.00,wetomx=0.15,wetomn=0.15,
370  & wetsmx=0.15,wetsmn=0.15,wetimx=0.15,wetimn=0.15,
371  & wetjmx=0.15,wetjmn=0.15)
372  parameter(snolmx=0.0,snolmn=0.0,snoomx=0.0,snoomn=0.0,
373  & snosmx=55000.,snosmn=0.001,snoimx=0.,snoimn=0.0,
374  & snojmx=10000.,snojmn=0.01)
375  parameter(zorlmx=300.,zorlmn=1.0,zoromx=1.0,zoromn=1.e-05,
376  & zorsmx=300.,zorsmn=1.0,zorimx=1.0,zorimn=1.0,
377  & zorjmx=1.0,zorjmn=1.0)
378  parameter(plrlmx=1000.,plrlmn=0.0,plromx=1000.0,plromn=0.0,
379  & plrsmx=1000.,plrsmn=0.0,plrimx=1000.,plrimn=0.0,
380  & plrjmx=1000.,plrjmn=0.0)
381 !clu [-1l/+1l] relax tsfsmx (for noah lsm)
382  parameter(tsflmx=353.,tsflmn=173.0,tsfomx=313.0,tsfomn=271.2,
383  & tsfsmx=305.0,tsfsmn=173.0,tsfimx=271.2,tsfimn=173.0,
384  & tsfjmx=273.16,tsfjmn=173.0)
385 ! parameter(tsflmx=353.,tsflmn=173.0,tsfomx=313.0,tsfomn=271.21,
386 !* & tsfsmx=273.16,tsfsmn=173.0,tsfimx=271.21,tsfimn=173.0,
387 ! & tsfsmx=305.0,tsfsmn=173.0,tsfimx=271.21,tsfimn=173.0,
388  parameter(tg3lmx=310.,tg3lmn=200.0,tg3omx=310.0,tg3omn=200.0,
389  & tg3smx=310.,tg3smn=200.0,tg3imx=310.0,tg3imn=200.0,
390  & tg3jmx=310.,tg3jmn=200.0)
391  parameter(stclmx=353.,stclmn=173.0,stcomx=313.0,stcomn=200.0,
392  & stcsmx=310.,stcsmn=200.0,stcimx=310.0,stcimn=200.0,
393  & stcjmx=310.,stcjmn=200.0)
394 !landice mods force a flag value of soil moisture of 1.0
395 ! at non-land points
396  parameter(smclmx=0.55,smclmn=0.0,smcomx=1.0,smcomn=1.0,
397  & smcsmx=0.55,smcsmn=0.0,smcimx=1.0,smcimn=1.0,
398  & smcjmx=1.0,smcjmn=1.0)
399  parameter(scvlmx=0.0,scvlmn=0.0,scvomx=0.0,scvomn=0.0,
400  & scvsmx=1.0,scvsmn=1.0,scvimx=0.0,scvimn=0.0,
401  & scvjmx=1.0,scvjmn=1.0)
402  parameter(veglmx=1.0,veglmn=0.0,vegomx=0.0,vegomn=0.0,
403  & vegsmx=1.0,vegsmn=0.0,vegimx=0.0,vegimn=0.0,
404  & vegjmx=0.0,vegjmn=0.0)
405  parameter(vmnlmx=1.0,vmnlmn=0.0,vmnomx=0.0,vmnomn=0.0,
406  & vmnsmx=1.0,vmnsmn=0.0,vmnimx=0.0,vmnimn=0.0,
407  & vmnjmx=0.0,vmnjmn=0.0)
408  parameter(vmxlmx=1.0,vmxlmn=0.0,vmxomx=0.0,vmxomn=0.0,
409  & vmxsmx=1.0,vmxsmn=0.0,vmximx=0.0,vmximn=0.0,
410  & vmxjmx=0.0,vmxjmn=0.0)
411  parameter(slplmx=9.0,slplmn=1.0,slpomx=0.0,slpomn=0.0,
412  & slpsmx=9.0,slpsmn=1.0,slpimx=0.,slpimn=0.,
413  & slpjmx=0.,slpjmn=0.)
414 ! note: the range values for bare land and snow covered land
415 ! (alblmx, alblmn, albsmx, albsmn) are set below
416 ! based on whether the old or new radiation is selected
417  parameter(absomx=0.0,absomn=0.0,
418  & absimx=0.0,absimn=0.0,
419  & absjmx=0.0,absjmn=0.0)
420 ! vegetation type
421  parameter(vetlmx=20.,vetlmn=1.0,vetomx=0.0,vetomn=0.0,
422  & vetsmx=20.,vetsmn=1.0,vetimx=0.,vetimn=0.,
423  & vetjmx=0.,vetjmn=0.)
424 ! soil type
425  parameter(sotlmx=16.,sotlmn=1.0,sotomx=0.0,sotomn=0.0,
426  & sotsmx=16.,sotsmn=1.0,sotimx=0.,sotimn=0.,
427  & sotjmx=0.,sotjmn=0.)
428 ! fraction of vegetation for strongly and weakly zeneith angle dependent
429 ! albedo
430  parameter(alslmx=1.0,alslmn=0.0,alsomx=0.0,alsomn=0.0,
431  & alssmx=1.0,alssmn=0.0,alsimx=0.0,alsimn=0.0,
432  & alsjmx=0.0,alsjmn=0.0)
433 !
434 ! criteria used for monitoring
435 !
436  parameter(epstsf=0.01,epsalb=0.001,epssno=0.01,
437  & epswet=0.01,epszor=0.0000001,epsplr=1.,epsoro=0.,
438  & epssmc=0.0001,epsscv=0.,eptsfc=0.01,epstg3=0.01,
439  & epsais=0.,epsacn=0.01,epsveg=0.01,
440  & epssih=0.001,epssic=0.001,
441  & epsvmn=0.01,epsvmx=0.01,epsabs=0.001,epsslp=0.01,
442  & epsvet=.01,epssot=.01,epsalf=.001)
443 !
444 ! quality control of analysis snow and sea ice
445 !
446 ! qctsfs .. surface temperature above which no snow allowed
447 ! qcsnos .. snow depth above which snow must exist
448 ! qctsfi .. sst above which sea-ice is not allowed
449 !
450 !clu relax qctsfs (for noah lsm)
451 !* parameter(qctsfs=283.16,qcsnos=100.,qctsfi=280.16)
452 !* parameter(qctsfs=288.16,qcsnos=100.,qctsfi=280.16)
453  parameter(qctsfs=293.16,qcsnos=100.,qctsfi=280.16)
454 !
455 !cwu [-2l]
456 !* ice concentration for ice limit (55 percent)
457 !
458 !* parameter(aislim=0.55)
459 !
460 ! parameters to obtain snow depth from snow cover and temperature
461 !
462 ! parameter(snwmin=25.,snwmax=100.)
463  parameter(snwmin=5.0,snwmax=100.)
464  real (kind=kind_io8), parameter :: ten=10.0, one=1.0
465 !
466 ! coeeficients of blending forecast and interpolated clim
467 ! (or analyzed) fields over sea or land(l) (not for clouds)
468 ! 1.0 = use of forecast
469 ! 0.0 = replace with interpolated analysis
470 !
471 ! these values are set for analysis mode.
472 !
473 ! variables land sea
474 ! ---------------------------------------------------------
475 ! surface temperature forecast analysis
476 ! surface temperature forecast forecast (over sea ice)
477 ! albedo analysis analysis
478 ! sea-ice analysis analysis
479 ! snow analysis forecast (over sea ice)
480 ! roughness analysis forecast
481 ! plant resistance analysis analysis
482 ! soil wetness (layer) weighted average analysis
483 ! soil temperature forecast analysis
484 ! canopy waver content forecast forecast
485 ! convective cloud cover forecast forecast
486 ! convective cloud bottm forecast forecast
487 ! convective cloud top forecast forecast
488 ! vegetation cover analysis analysis
489 ! vegetation type analysis analysis
490 ! soil type analysis analysis
491 ! sea-ice thickness forecast forecast
492 ! sea-ice concentration analysis analysis
493 ! vegetation cover min analysis analysis
494 ! vegetation cover max analysis analysis
495 ! max snow albedo analysis analysis
496 ! slope type analysis analysis
497 ! liquid soil wetness analysis-weighted analysis
498 ! actual snow depth analysis-weighted analysis
499 !
500 ! note: if analysis file is not given, then time interpolated climatology
501 ! is used. if analyiss file is given, it will be used as far as the
502 ! date and time matches. if they do not match, it uses forecast.
503 !
504 ! critical percentage value for aborting bad points when lgchek=.true.
505 !
506  logical lgchek
507  data lgchek/.true./
508  data critp1,critp2,critp3/80.,80.,25./
509 !
510 ! integer kpdalb(4), kpdalf(2)
511 ! data kpdalb/212,215,213,216/, kpdalf/214,217/
512 ! save kpdalb, kpdalf
513 !
514 ! mask orography and variance on gaussian grid
515 !
516  real (kind=kind_io8) slmask(len),orog(len), orog_uf(len)
517  &, orogd(len)
518  real (kind=kind_io8) rla(len), rlo(len)
519 !
520 ! permanent/extremes
521 !
522  character*500 fnglac,fnmxic
523  real (kind=kind_io8), allocatable :: glacir(:),amxice(:),tsfcl0(:)
524 !
525 ! tsfcl0 is the climatological tsf at fh=0
526 !
527 ! climatology surface fields (last character 'c' or 'clm' indicate climatology)
528 !
529  character*500 fntsfc,fnwetc,fnsnoc,fnzorc,fnalbc,fnaisc,
530  & fnplrc,fntg3c,fnscvc,fnsmcc,fnstcc,fnacnc,
531  & fnvegc,fnvetc,fnsotc
532  &, fnvmnc,fnvmxc,fnslpc,fnabsc, fnalbc2
533  real (kind=kind_io8) tsfclm(len), wetclm(len), snoclm(len),
534  & zorclm(len), albclm(len,4), aisclm(len),
535  & tg3clm(len), acnclm(len), cnpclm(len),
536  & cvclm(len), cvbclm(len), cvtclm(len),
537  & scvclm(len), tsfcl2(len), vegclm(len),
538  & vetclm(len), sotclm(len), alfclm(len,2), sliclm(len),
539  & smcclm(len,lsoil), stcclm(len,lsoil)
540  &, sihclm(len), sicclm(len)
541  &, vmnclm(len), vmxclm(len), slpclm(len), absclm(len)
542 !
543 ! analyzed surface fields (last character 'a' or 'anl' indicate analysis)
544 !
545  character*500 fntsfa,fnweta,fnsnoa,fnzora,fnalba,fnaisa,
546  & fnplra,fntg3a,fnscva,fnsmca,fnstca,fnacna,
547  & fnvega,fnveta,fnsota
548  &, fnvmna,fnvmxa,fnslpa,fnabsa
549 !
550  real (kind=kind_io8) tsfanl(len), wetanl(len), snoanl(len),
551  & zoranl(len), albanl(len,4), aisanl(len),
552  & tg3anl(len), acnanl(len), cnpanl(len),
553  & cvanl(len), cvbanl(len), cvtanl(len),
554  & scvanl(len), tsfan2(len), veganl(len),
555  & vetanl(len), sotanl(len), alfanl(len,2), slianl(len),
556  & smcanl(len,lsoil), stcanl(len,lsoil)
557  &, sihanl(len), sicanl(len)
558  &, vmnanl(len), vmxanl(len), slpanl(len), absanl(len)
559 !
560  real (kind=kind_io8) tsfan0(len) ! sea surface temperature analysis at ft=0.
561 !
562 ! predicted surface fields (last characters 'fcs' indicates forecast)
563 !
564  real (kind=kind_io8) tsffcs(len), wetfcs(len), snofcs(len),
565  & zorfcs(len), albfcs(len,4), aisfcs(len),
566  & tg3fcs(len), acnfcs(len), cnpfcs(len),
567  & cvfcs(len), cvbfcs(len), cvtfcs(len),
568  & slifcs(len), vegfcs(len),
569  & vetfcs(len), sotfcs(len), alffcs(len,2),
570  & smcfcs(len,lsoil), stcfcs(len,lsoil)
571  &, sihfcs(len), sicfcs(len), sitfcs(len)
572  &, vmnfcs(len), vmxfcs(len), slpfcs(len), absfcs(len)
573  &, swdfcs(len), slcfcs(len,lsoil)
574 !
575 ! ratio of sigma level 1 wind and 10m wind (diagnozed by model and not touched
576 ! in this program).
577 !
578  real (kind=kind_io8) f10m (len)
579  real (kind=kind_io8) fsmcl(25),fsmcs(25),fstcl(25),fstcs(25)
580  real (kind=kind_io8) fcsmcl(25),fcsmcs(25),fcstcl(25),fcstcs(25)
581 
582 !clu [+1l] add swratio (soil moisture liquid-to-total ratio)
583  real (kind=kind_io8) swratio(len,lsoil)
584 !clu [+1l] add fixratio (option to adjust slc from smc)
585  logical fixratio(lsoil)
586 !
587  integer icsmcl(25), icsmcs(25), icstcl(25), icstcs(25)
588 !
589  real (kind=kind_io8) csmcl(25), csmcs(25)
590  real (kind=kind_io8) cstcl(25), cstcs(25)
591 !
592  real (kind=kind_io8) slmskh(mdata)
593  character*500 fnmskh
594  integer kpd7, kpd9
595 !
596  logical icefl1(len), icefl2(len)
597 !
598 ! input and output surface fields (bges) file names
599 !
600 !
601 ! sigma level 1 temperature for dead start
602 !
603  real (kind=kind_io8) sig1t(len)
604 !
605  character*32 label
606 !
607 ! = 1 ==> forecast is used
608 ! = 0 ==> analysis (or climatology) is used
609 !
610 ! output file ... primary surface file for radiation and forecast
611 !
612 ! rec. 1 label
613 ! rec. 2 date record
614 ! rec. 3 tsf
615 ! rec. 4 soilm(two layers) ----> 4 layers
616 ! rec. 5 snow
617 ! rec. 6 soilt(two layers) ----> 4 layers
618 ! rec. 7 tg3
619 ! rec. 8 zor
620 ! rec. 9 cv
621 ! rec. 10 cvb
622 ! rec. 11 cvt
623 ! rec. 12 albedo (four types)
624 ! rec. 13 slimsk
625 ! rec. 14 vegetation cover
626 ! rec. 14 plantr -----> skip this record
627 ! rec. 15 f10m -----> canopy
628 ! rec. 16 canopy water content (cnpanl) -----> f10m
629 ! rec. 17 vegetation type
630 ! rec. 18 soil type
631 ! rec. 19 zeneith angle dependent vegetation fraction (two types)
632 ! rec. 20 uustar
633 ! rec. 21 ffmm
634 ! rec. 22 ffhh
635 !cwu add sih & sic
636 ! rec. 23 sih(one category only)
637 ! rec. 24 sic
638 !clu [+8l] add prcp, flag, swd, slc, vmn, vmx, slp, abs
639 ! rec. 25 tprcp
640 ! rec. 26 srflag
641 ! rec. 27 swd
642 ! rec. 28 slc (4 layers)
643 ! rec. 29 vmn
644 ! rec. 30 vmx
645 ! rec. 31 slp
646 ! rec. 32 abs
647 
648 !
649 ! debug only
650 ! ldebug=.true. creates bges files for climatology and analysis
651 ! lqcbgs=.true. quality controls input bges file before merging (should have been
652 ! qced in the forecast program)
653 !
654  logical ldebug,lqcbgs
655  logical lprnt
656 !
657 ! debug only
658 !
659  character*500 fndclm,fndanl
660 !
661  logical lanom
662 
663 !
664  namelist/namsfc/fnglac,fnmxic,
665  & fntsfc,fnwetc,fnsnoc,fnzorc,fnalbc,fnaisc,
666  & fnplrc,fntg3c,fnscvc,fnsmcc,fnstcc,fnacnc,
667  & fnvegc,fnvetc,fnsotc,fnalbc2,
668  & fnvmnc,fnvmxc,fnslpc,fnabsc,
669  & fntsfa,fnweta,fnsnoa,fnzora,fnalba,fnaisa,
670  & fnplra,fntg3a,fnscva,fnsmca,fnstca,fnacna,
671  & fnvega,fnveta,fnsota,
672  & fnvmna,fnvmxa,fnslpa,fnabsa,
673  & fnmskh,
674  & ldebug,lgchek,lqcbgs,critp1,critp2,critp3,
675  & fndclm,fndanl,
676  & lanom,
677  & ftsfl,ftsfs,falbl,falbs,faisl,faiss,fsnol,fsnos,
678  & fzorl,fzors,fplrl,fplrs,fsmcl,fsmcs,
679  & fstcl,fstcs,fvegl,fvegs,fvetl,fvets,fsotl,fsots,
680  & fctsfl,fctsfs,fcalbl,fcalbs,fcsnol,fcsnos,
681  & fczorl,fczors,fcplrl,fcplrs,fcsmcl,fcsmcs,
682  & fcstcl,fcstcs,fsalfl,fsalfs,fcalfl,flalfs,
683  & fsihl,fsicl,fsihs,fsics,aislim,sihnew,
684  & fvmnl,fvmns,fvmxl,fvmxs,fslpl,fslps,
685  & fabsl,fabss,
686  & ictsfl,ictsfs,icalbl,icalbs,icsnol,icsnos,
687  & iczorl,iczors,icplrl,icplrs,icsmcl,icsmcs,
688  & icstcl,icstcs,icalfl,icalfs,
689  & gausm, deads, qcmsk, znlst,
690  & monclm, monanl, monfcs, monmer, mondif, igrdbg,
691  & blnmsk, bltmsk, landice
692 !
693  data gausm/.true./, deads/.false./, blnmsk/0.0/, bltmsk/90.0/
694  &, qcmsk/.false./, znlst/.false./, igrdbg/-1/
695  &, monclm/.false./, monanl/.false./, monfcs/.false./
696  &, monmer/.false./, mondif/.false./, landice/.true./
697 !
698 ! defaults file names
699 !
700  data fnmskh/'global_slmask.t126.grb'/
701  data fnalbc/'global_albedo4.1x1.grb'/
702  data fnalbc2/'global_albedo4.1x1.grb'/
703  data fntsfc/'global_sstclim.2x2.grb'/
704  data fnsotc/'global_soiltype.1x1.grb'/
705  data fnvegc/'global_vegfrac.1x1.grb'/
706  data fnvetc/'global_vegtype.1x1.grb'/
707  data fnglac/'global_glacier.2x2.grb'/
708  data fnmxic/'global_maxice.2x2.grb'/
709  data fnsnoc/'global_snoclim.1.875.grb'/
710  data fnzorc/'global_zorclim.1x1.grb'/
711  data fnaisc/'global_iceclim.2x2.grb'/
712  data fntg3c/'global_tg3clim.2.6x1.5.grb'/
713  data fnsmcc/'global_soilmcpc.1x1.grb'/
714 !clu [+4l] add fn()c for vmn, vmx, abs, slp
715  data fnvmnc/'global_shdmin.0.144x0.144.grb'/
716  data fnvmxc/'global_shdmax.0.144x0.144.grb'/
717  data fnslpc/'global_slope.1x1.grb'/
718  data fnabsc/'global_snoalb.1x1.grb'/
719 !
720  data fnwetc/' '/
721  data fnplrc/' '/
722  data fnstcc/' '/
723  data fnscvc/' '/
724  data fnacnc/' '/
725 !
726  data fntsfa/' '/
727  data fnweta/' '/
728  data fnsnoa/' '/
729  data fnzora/' '/
730  data fnalba/' '/
731  data fnaisa/' '/
732  data fnplra/' '/
733  data fntg3a/' '/
734  data fnsmca/' '/
735  data fnstca/' '/
736  data fnscva/' '/
737  data fnacna/' '/
738  data fnvega/' '/
739  data fnveta/' '/
740  data fnsota/' '/
741 !clu [+4l] add fn()a for vmn, vmx, abs, slp
742  data fnvmna/' '/
743  data fnvmxa/' '/
744  data fnslpa/' '/
745  data fnabsa/' '/
746 !
747  data ldebug/.false./, lqcbgs/.true./
748  data fndclm/' '/
749  data fndanl/' '/
750  data lanom/.false./
751 !
752 ! default relaxation time in hours to analysis or climatology
753  data ftsfl/99999.0/, ftsfs/0.0/
754  data falbl/0.0/, falbs/0.0/
755  data falfl/0.0/, falfs/0.0/
756  data faisl/0.0/, faiss/0.0/
757  data fsnol/0.0/, fsnos/99999.0/
758  data fzorl/0.0/, fzors/99999.0/
759  data fplrl/0.0/, fplrs/0.0/
760  data fvetl/0.0/, fvets/99999.0/
761  data fsotl/0.0/, fsots/99999.0/
762  data fvegl/0.0/, fvegs/99999.0/
763 !cwu [+4l] add f()l and f()s for sih, sic and aislim, sihlim
764  data fsihl/99999.0/, fsihs/99999.0/
765 ! data fsicl/99999.0/, fsics/99999.0/
766  data fsicl/0.0/, fsics/0.0/
767 ! default ice concentration limit (50%), new ice thickness (20cm)
768 !cwu change ice concentration limit (15%) Jan 2015
769 ! data aislim/0.50/, sihnew/0.2/
770  data aislim/0.15/, sihnew/0.2/
771 !clu [+4l] add f()l and f()s for vmn, vmx, abs, slp
772  data fvmnl/0.0/, fvmns/99999.0/
773  data fvmxl/0.0/, fvmxs/99999.0/
774  data fslpl/0.0/, fslps/99999.0/
775  data fabsl/0.0/, fabss/99999.0/
776 ! default relaxation time in hours to climatology if analysis missing
777  data fctsfl/99999.0/, fctsfs/99999.0/
778  data fcalbl/99999.0/, fcalbs/99999.0/
779  data fcsnol/99999.0/, fcsnos/99999.0/
780  data fczorl/99999.0/, fczors/99999.0/
781  data fcplrl/99999.0/, fcplrs/99999.0/
782 ! default flag to apply climatological annual cycle
783  data ictsfl/0/, ictsfs/1/
784  data icalbl/1/, icalbs/1/
785  data icalfl/1/, icalfs/1/
786  data icsnol/0/, icsnos/0/
787  data iczorl/1/, iczors/0/
788  data icplrl/1/, icplrs/0/
789 !
790  data ccnp/1.0/
791  data ccv/1.0/, ccvb/1.0/, ccvt/1.0/
792 !
793  data ifp/0/
794 !
795  save ifp,fnglac,fnmxic,
796  & fntsfc,fnwetc,fnsnoc,fnzorc,fnalbc,fnaisc,
797  & fnplrc,fntg3c,fnscvc,fnsmcc,fnstcc,fnacnc,fnvegc,
798  & fntsfa,fnweta,fnsnoa,fnzora,fnalba,fnaisa,
799  & fnplra,fntg3a,fnscva,fnsmca,fnstca,fnacna,fnvega,
800  & fnvetc,fnveta,
801  & fnsotc,fnsota,
802 !clu [+2l] add fn()c and fn()a for vmn, vmx, slp, abs
803  & fnvmnc,fnvmxc,fnabsc,fnslpc,
804  & fnvmna,fnvmxa,fnabsa,fnslpa,
805  & ldebug,lgchek,lqcbgs,critp1,critp2,critp3,
806  & fndclm,fndanl,
807  & lanom,
808  & ftsfl,ftsfs,falbl,falbs,faisl,faiss,fsnol,fsnos,
809  & fzorl,fzors,fplrl,fplrs,fsmcl,fsmcs,falfl,falfs,
810  & fstcl,fstcs,fvegl,fvegs,fvetl,fvets,fsotl,fsots,
811  & fctsfl,fctsfs,fcalbl,fcalbs,fcsnol,fcsnos,
812  & fczorl,fczors,fcplrl,fcplrs,fcsmcl,fcsmcs,
813  & fcstcl,fcstcs,fcalfl,fcalfs,
814 !cwu [+1l] add f()l and f()s for sih, sic and aislim, sihnew
815  & fsihl,fsihs,fsicl,fsics,aislim,sihnew,
816 !clu [+2l] add f()l and f()s for vmn, vmx, slp, abs
817  & fvmnl,fvmns,fvmxl,fvmxs,fslpl,fslps,
818  & fabsl,fabss,
819  & ictsfl,ictsfs,icalbl,icalbs,icsnol,icsnos,
820  & iczorl,iczors,icplrl,icplrs,icsmcl,icsmcs,
821  & icstcl,icstcs,icalfl,icalfs,
822  & gausm, deads, qcmsk,
823  & monclm, monanl, monfcs, monmer, mondif, igrdbg,
824  & grboro, grbmsk,
825 !
826  & ctsfl, ctsfs, calbl, calfl, calbs, calfs, csmcs,
827  & csnol, csnos, czorl, czors, cplrl, cplrs, cstcl,
828  & cstcs, cvegl, cvwgs, cvetl, cvets, csotl, csots,
829  & csmcl
830 !cwu [+1l] add c()l and c()s for sih, sic
831  &, csihl, csihs, csicl, csics
832 !clu [+2l] add c()l and c()s for vmn, vmx, slp, abs
833  &, cvmnl, cvmns, cvmxl, cvmxs, cslpl, cslps,
834  & cabsl, cabss
835  &, imsk, jmsk, slmskh, blnmsk, bltmsk
836  &, glacir, amxice, tsfcl0
837  &, caisl, caiss, cvegs
838 !
839  lprnt = .false.
840  iprnt = 1
841 ! do i=1,len
842 ! if (ifp .eq. 0 .and. rla(i) .gt. 80.0) print *,' rla=',rla(i)
843 ! *,' rlo=',rlo(i)
844 ! tem1 = abs(rla(i) - 48.75)
845 ! tem2 = abs(rlo(i) - (-68.50))
846 ! if(tem1 .lt. 0.25 .and. tem2 .lt. 0.50) then
847 ! lprnt = .true.
848 ! iprnt = i
849 ! print *,' lprnt=',lprnt,' iprnt=',iprnt
850 ! print *,' rla(i)=',rla(i),' rlo(i)=',rlo(i)
851 ! endif
852 ! enddo
853  if (ialb == 1) then
854  kpdabs = kpdabs_1
855  kpdalb = kpdalb_1
856  alblmx = .99
857  albsmx = .99
858  alblmn = .01
859  albsmn = .01
860  abslmx = 1.0
861  abssmx = 1.0
862  abssmn = .01
863  abslmn = .01
864  else
865  kpdabs = kpdabs_0
866  kpdalb = kpdalb_0
867  alblmx = .80
868  albsmx = .80
869  alblmn = .06
870  albsmn = .06
871  abslmx = .80
872  abssmx = .80
873  abslmn = .01
874  abssmn = .01
875  endif
876  if(ifp.eq.0) then
877  ifp = 1
878  do k=1,lsoil
879  fsmcl(k) = 99999.
880  fsmcs(k) = 0.
881  fstcl(k) = 99999.
882  fstcs(k) = 0.
883  enddo
884 #ifdef INTERNAL_FILE_NML
885  read(input_nml_file, nml=namsfc)
886 #else
887 ! print *,' in sfcsub nlunit=',nlunit,' me=',me,' ialb=',ialb
888  rewind(nlunit)
889  read (nlunit,namsfc)
890 #endif
891 ! write(6,namsfc)
892 !
893  if (me .eq. 0) then
894  print *,'ftsfl,falbl,faisl,fsnol,fzorl=',
895  & ftsfl,falbl,faisl,fsnol,fzorl
896  print *,'fsmcl=',fsmcl(1:lsoil)
897  print *,'fstcl=',fstcl(1:lsoil)
898  print *,'ftsfs,falbs,faiss,fsnos,fzors=',
899  & ftsfs,falbs,faiss,fsnos,fzors
900  print *,'fsmcs=',fsmcs(1:lsoil)
901  print *,'fstcs=',fstcs(1:lsoil)
902  print *,' aislim=',aislim,' sihnew=',sihnew
903  print *,' isot=', isot,' ivegsrc=',ivegsrc
904  endif
905 
906  if (ivegsrc == 2) then ! sib
907  veg_type_landice=13
908  else
909  veg_type_landice=15
910  endif
911  if (isot == 0) then
912  soil_type_landice=9
913  else
914  soil_type_landice=16
915  endif
916 !
917  deltf = deltsfc / 24.0
918 !
919  ctsfl=0. !... tsfc over land
920  if(ftsfl.ge.99999.) ctsfl=1.
921  if((ftsfl.gt.0.).and.(ftsfl.lt.99999)) ctsfl=exp(-deltf/ftsfl)
922 !
923  ctsfs=0. !... tsfc over sea
924  if(ftsfs.ge.99999.) ctsfs=1.
925  if((ftsfs.gt.0.).and.(ftsfs.lt.99999)) ctsfs=exp(-deltf/ftsfs)
926 !
927  do k=1,lsoil
928  csmcl(k)=0. !... soilm over land
929  if(fsmcl(k).ge.99999.) csmcl(k)=1.
930  if((fsmcl(k).gt.0.).and.(fsmcl(k).lt.99999))
931  & csmcl(k)=exp(-deltf/fsmcl(k))
932  csmcs(k)=0. !... soilm over sea
933  if(fsmcs(k).ge.99999.) csmcs(k)=1.
934  if((fsmcs(k).gt.0.).and.(fsmcs(k).lt.99999))
935  & csmcs(k)=exp(-deltf/fsmcs(k))
936  enddo
937 !
938  calbl=0. !... albedo over land
939  if(falbl.ge.99999.) calbl=1.
940  if((falbl.gt.0.).and.(falbl.lt.99999)) calbl=exp(-deltf/falbl)
941 !
942  calfl=0. !... fraction field for albedo over land
943  if(falfl.ge.99999.) calfl=1.
944  if((falfl.gt.0.).and.(falfl.lt.99999)) calfl=exp(-deltf/falfl)
945 !
946  calbs=0. !... albedo over sea
947  if(falbs.ge.99999.) calbs=1.
948  if((falbs.gt.0.).and.(falbs.lt.99999)) calbs=exp(-deltf/falbs)
949 !
950  calfs=0. !... fraction field for albedo over sea
951  if(falfs.ge.99999.) calfs=1.
952  if((falfs.gt.0.).and.(falfs.lt.99999)) calfs=exp(-deltf/falfs)
953 !
954  caisl=0. !... sea ice over land
955  if(faisl.ge.99999.) caisl=1.
956  if((faisl.gt.0.).and.(faisl.lt.99999)) caisl=1.
957 !
958  caiss=0. !... sea ice over sea
959  if(faiss.ge.99999.) caiss=1.
960  if((faiss.gt.0.).and.(faiss.lt.99999)) caiss=1.
961 !
962  csnol=0. !... snow over land
963  if(fsnol.ge.99999.) csnol=1.
964  if((fsnol.gt.0.).and.(fsnol.lt.99999)) csnol=exp(-deltf/fsnol)
965 ! using the same way to bending snow as narr when fsnol is the negative value
966 ! the magnitude of fsnol is the thread to determine the lower and upper bound
967 ! of final swe
968  if(fsnol.lt.0.)csnol=fsnol
969 !
970  csnos=0. !... snow over sea
971  if(fsnos.ge.99999.) csnos=1.
972  if((fsnos.gt.0.).and.(fsnos.lt.99999)) csnos=exp(-deltf/fsnos)
973 !
974  czorl=0. !... roughness length over land
975  if(fzorl.ge.99999.) czorl=1.
976  if((fzorl.gt.0.).and.(fzorl.lt.99999)) czorl=exp(-deltf/fzorl)
977 !
978  czors=0. !... roughness length over sea
979  if(fzors.ge.99999.) czors=1.
980  if((fzors.gt.0.).and.(fzors.lt.99999)) czors=exp(-deltf/fzors)
981 !
982 ! cplrl=0. !... plant resistance over land
983 ! if(fplrl.ge.99999.) cplrl=1.
984 ! if((fplrl.gt.0.).and.(fplrl.lt.99999)) cplrl=exp(-deltf/fplrl)
985 !
986 ! cplrs=0. !... plant resistance over sea
987 ! if(fplrs.ge.99999.) cplrs=1.
988 ! if((fplrs.gt.0.).and.(fplrs.lt.99999)) cplrs=exp(-deltf/fplrs)
989 !
990  do k=1,lsoil
991  cstcl(k)=0. !... soilt over land
992  if(fstcl(k).ge.99999.) cstcl(k)=1.
993  if((fstcl(k).gt.0.).and.(fstcl(k).lt.99999))
994  & cstcl(k)=exp(-deltf/fstcl(k))
995  cstcs(k)=0. !... soilt over sea
996  if(fstcs(k).ge.99999.) cstcs(k)=1.
997  if((fstcs(k).gt.0.).and.(fstcs(k).lt.99999))
998  & cstcs(k)=exp(-deltf/fstcs(k))
999  enddo
1000 !
1001  cvegl=0. !... vegetation fraction over land
1002  if(fvegl.ge.99999.) cvegl=1.
1003  if((fvegl.gt.0.).and.(fvegl.lt.99999)) cvegl=exp(-deltf/fvegl)
1004 !
1005  cvegs=0. !... vegetation fraction over sea
1006  if(fvegs.ge.99999.) cvegs=1.
1007  if((fvegs.gt.0.).and.(fvegs.lt.99999)) cvegs=exp(-deltf/fvegs)
1008 !
1009  cvetl=0. !... vegetation type over land
1010  if(fvetl.ge.99999.) cvetl=1.
1011  if((fvetl.gt.0.).and.(fvetl.lt.99999)) cvetl=exp(-deltf/fvetl)
1012 !
1013  cvets=0. !... vegetation type over sea
1014  if(fvets.ge.99999.) cvets=1.
1015  if((fvets.gt.0.).and.(fvets.lt.99999)) cvets=exp(-deltf/fvets)
1016 !
1017  csotl=0. !... soil type over land
1018  if(fsotl.ge.99999.) csotl=1.
1019  if((fsotl.gt.0.).and.(fsotl.lt.99999)) csotl=exp(-deltf/fsotl)
1020 !
1021  csots=0. !... soil type over sea
1022  if(fsots.ge.99999.) csots=1.
1023  if((fsots.gt.0.).and.(fsots.lt.99999)) csots=exp(-deltf/fsots)
1024 
1025 !cwu [+16l]---------------------------------------------------------------
1026 !
1027  csihl=0. !... sea ice thickness over land
1028  if(fsihl.ge.99999.) csihl=1.
1029  if((fsihl.gt.0.).and.(fsihl.lt.99999)) csihl=exp(-deltf/fsihl)
1030 !
1031  csihs=0. !... sea ice thickness over sea
1032  if(fsihs.ge.99999.) csihs=1.
1033  if((fsihs.gt.0.).and.(fsihs.lt.99999)) csihs=exp(-deltf/fsihs)
1034 !
1035  csicl=0. !... sea ice concentration over land
1036  if(fsicl.ge.99999.) csicl=1.
1037  if((fsicl.gt.0.).and.(fsicl.lt.99999)) csicl=exp(-deltf/fsicl)
1038 !
1039  csics=0. !... sea ice concentration over sea
1040  if(fsics.ge.99999.) csics=1.
1041  if((fsics.gt.0.).and.(fsics.lt.99999)) csics=exp(-deltf/fsics)
1042 
1043 !clu [+32l]---------------------------------------------------------------
1044 !
1045  cvmnl=0. !... min veg cover over land
1046  if(fvmnl.ge.99999.) cvmnl=1.
1047  if((fvmnl.gt.0.).and.(fvmnl.lt.99999)) cvmnl=exp(-deltf/fvmnl)
1048 !
1049  cvmns=0. !... min veg cover over sea
1050  if(fvmns.ge.99999.) cvmns=1.
1051  if((fvmns.gt.0.).and.(fvmns.lt.99999)) cvmns=exp(-deltf/fvmns)
1052 !
1053  cvmxl=0. !... max veg cover over land
1054  if(fvmxl.ge.99999.) cvmxl=1.
1055  if((fvmxl.gt.0.).and.(fvmxl.lt.99999)) cvmxl=exp(-deltf/fvmxl)
1056 !
1057  cvmxs=0. !... max veg cover over sea
1058  if(fvmxs.ge.99999.) cvmxs=1.
1059  if((fvmxs.gt.0.).and.(fvmxs.lt.99999)) cvmxs=exp(-deltf/fvmxs)
1060 !
1061  cslpl=0. !... slope type over land
1062  if(fslpl.ge.99999.) cslpl=1.
1063  if((fslpl.gt.0.).and.(fslpl.lt.99999)) cslpl=exp(-deltf/fslpl)
1064 !
1065  cslps=0. !... slope type over sea
1066  if(fslps.ge.99999.) cslps=1.
1067  if((fslps.gt.0.).and.(fslps.lt.99999)) cslps=exp(-deltf/fslps)
1068 !
1069  cabsl=0. !... snow albedo over land
1070  if(fabsl.ge.99999.) cabsl=1.
1071  if((fabsl.gt.0.).and.(fabsl.lt.99999)) cabsl=exp(-deltf/fabsl)
1072 !
1073  cabss=0. !... snow albedo over sea
1074  if(fabss.ge.99999.) cabss=1.
1075  if((fabss.gt.0.).and.(fabss.lt.99999)) cabss=exp(-deltf/fabss)
1076 !clu ----------------------------------------------------------------------
1077 !
1078 ! read a high resolution mask field for use in grib interpolation
1079 !
1080  call hmskrd(lugb,imsk,jmsk,fnmskh,
1081  & kpdmsk,slmskh,gausm,blnmsk,bltmsk,me)
1082 ! if (qcmsk) call qcmask(slmskh,sllnd,slsea,imsk,jmsk,rla,rlo)
1083 !
1084  if (me .eq. 0) then
1085  write(6,*) ' '
1086  write(6,*) ' lugb=',lugb,' len=',len, ' lsoil=',lsoil
1087  write(6,*) 'iy=',iy,' im=',im,' id=',id,' ih=',ih,' fh=',fh
1088  &, ' sig1t(1)=',sig1t(1)
1089  &, ' gausm=',gausm,' blnmsk=',blnmsk,' bltmsk=',bltmsk
1090  write(6,*) ' '
1091  endif
1092 !
1093 ! reading permanent/extreme features (glacier points and maximum ice extent)
1094 !
1095  allocate (tsfcl0(len))
1096  allocate (glacir(len))
1097  allocate (amxice(len))
1098 !
1099 ! read glacier
1100 !
1101  kpd9 = -1
1102  kpd7 = -1
1103  call fixrdc(lugb,fnglac,kpdgla,kpd7,kpd9,slmask,
1104  & glacir,len,iret
1105  &, imsk, jmsk, slmskh, gausm, blnmsk, bltmsk
1106  &, rla, rlo, me)
1107 ! znnt=1.
1108 ! call nntprt(glacir,len,znnt)
1109 !
1110 ! read maximum ice extent
1111 !
1112  kpd7 = -1
1113  call fixrdc(lugb,fnmxic,kpdmxi,kpd7,kpd9,slmask,
1114  & amxice,len,iret
1115  &, imsk, jmsk, slmskh, gausm, blnmsk, bltmsk
1116  &, rla, rlo, me)
1117 ! znnt=1.
1118 ! call nntprt(amxice,len,znnt)
1119 !
1120  crit=0.5
1121  call rof01(glacir,len,'ge',crit)
1122  call rof01(amxice,len,'ge',crit)
1123 !
1124 ! quality control max ice limit based on glacier points
1125 !
1126  call qcmxice(glacir,amxice,len,me)
1127 !
1128  endif ! first time loop finished
1129 !
1130  do i=1,len
1131  sliclm(i) = 1.
1132  snoclm(i) = 0.
1133  icefl1(i) = .true.
1134  enddo
1135 ! if(lprnt) print *,' tsffcsin=',tsffcs(iprnt)
1136 !
1137 ! read climatology fields
1138 !
1139  if (me .eq. 0) then
1140  write(6,*) '=============='
1141  write(6,*) 'climatology'
1142  write(6,*) '=============='
1143  endif
1144 !
1145  percrit=critp1
1146 !
1147  call clima(lugb,iy,im,id,ih,fh,len,lsoil,slmask,
1148  & fntsfc,fnwetc,fnsnoc,fnzorc,fnalbc,fnaisc,
1149  & fntg3c,fnscvc,fnsmcc,fnstcc,fnacnc,fnvegc,
1150  & fnvetc,fnsotc,
1151  & fnvmnc,fnvmxc,fnslpc,fnabsc,
1152  & tsfclm,tsfcl2,wetclm,snoclm,zorclm,albclm,aisclm,
1153  & tg3clm,cvclm ,cvbclm,cvtclm,
1154  & cnpclm,smcclm,stcclm,sliclm,scvclm,acnclm,vegclm,
1155  & vetclm,sotclm,alfclm,
1156  & vmnclm,vmxclm,slpclm,absclm,
1157  & kpdtsf,kpdwet,kpdsno,kpdzor,kpdalb,kpdais,
1158  & kpdtg3,kpdscv,kpdacn,kpdsmc,kpdstc,kpdveg,
1159  & kpdvet,kpdsot,kpdalf,tsfcl0,
1160  & kpdvmn,kpdvmx,kpdslp,kpdabs,
1161  & deltsfc, lanom
1162  &, imsk, jmsk, slmskh, rla, rlo, gausm, blnmsk, bltmsk,me
1163  &, lprnt,iprnt,fnalbc2,ialb,tile_num_ch,i_index,j_index)
1164 ! if(lprnt) print *,'tsfclm=',tsfclm(iprnt),' tsfcl2=',tsfcl2(iprnt)
1165 !
1166 ! scale surface roughness and albedo to model required units
1167 !
1168  zsca=100.
1169  call scale(zorclm,len,zsca)
1170  zsca=0.01
1171  call scale(albclm,len,zsca)
1172  call scale(albclm(1,2),len,zsca)
1173  call scale(albclm(1,3),len,zsca)
1174  call scale(albclm(1,4),len,zsca)
1175  call scale(alfclm,len,zsca)
1176  call scale(alfclm(1,2),len,zsca)
1177 !clu [+4l] scale vmn, vmx, abs from percent to fraction
1178  zsca=0.01
1179  call scale(vmnclm,len,zsca)
1180  call scale(vmxclm,len,zsca)
1181  call scale(absclm,len,zsca)
1182 
1183 !
1184 ! set albedo over ocean to albomx
1185 !
1186  call albocn(albclm,slmask,albomx,len)
1187 !
1188 ! make sure vegetation type and soil type are non zero over land
1189 !
1190  call landtyp(vetclm,sotclm,slpclm,slmask,len)
1191 !
1192 !cwu [-1l/+1l]
1193 !* ice concentration or ice mask (only ice mask used in the model now)
1194 ! ice concentration and ice mask (both are used in the model now)
1195 !
1196  if(fnaisc(1:8).ne.' ') then
1197 !cwu [+5l/-1l] update sihclm, sicclm
1198  do i=1,len
1199  sihclm(i) = 3.0*aisclm(i)
1200  sicclm(i) = aisclm(i)
1201  if(slmask(i).eq.0..and.glacir(i).eq.1..and.
1202  & sicclm(i).ne.1.) then
1203  sicclm(i) = sicimx
1204  sihfcs(i) = glacir_hice
1205  endif
1206  enddo
1207  crit=aislim
1208 !* crit=0.5
1209  call rof01(aisclm,len,'ge',crit)
1210  elseif(fnacnc(1:8).ne.' ') then
1211 !cwu [+4l] update sihclm, sicclm
1212  do i=1,len
1213  sihclm(i) = 3.0*acnclm(i)
1214  sicclm(i) = acnclm(i)
1215  if(slmask(i).eq.0..and.glacir(i).eq.1..and.
1216  & sicclm(i).ne.1.) then
1217  sicclm(i) = sicimx
1218  sihfcs(i) = glacir_hice
1219  endif
1220  enddo
1221  call rof01(acnclm,len,'ge',aislim)
1222  do i=1,len
1223  aisclm(i) = acnclm(i)
1224  enddo
1225  endif
1226 !
1227 ! quality control of sea ice mask
1228 !
1229  call qcsice(aisclm,glacir,amxice,aicice,aicsea,sllnd,slmask,
1230  & rla,rlo,len,me)
1231 !
1232 ! set ocean/land/sea-ice mask
1233 !
1234  call setlsi(slmask,aisclm,len,aicice,sliclm)
1235 ! if(lprnt) print *,' aisclm=',aisclm(iprnt),' sliclm='
1236 ! *,sliclm(iprnt),' slmask=',slmask(iprnt)
1237 !
1238 ! write(6,*) 'sliclm'
1239 ! znnt=1.
1240 ! call nntprt(sliclm,len,znnt)
1241 !
1242 ! quality control of snow
1243 !
1244  call qcsnow(snoclm,slmask,aisclm,glacir,len,snosmx,landice,me)
1245 !
1246  call setzro(snoclm,epssno,len)
1247 !
1248 ! snow cover handling (we assume climatological snow depth is available)
1249 ! quality control of snow depth (note that snow should be corrected first
1250 ! because it influences tsf
1251 !
1252  kqcm=1
1253  call qcmxmn('snow ',snoclm,sliclm,snoclm,icefl1,
1254  & snolmx,snolmn,snoomx,snoomn,snoimx,snoimn,
1255  & snojmx,snojmn,snosmx,snosmn,epssno,
1256  & rla,rlo,len,kqcm,percrit,lgchek,me)
1257 ! write(6,*) 'snoclm'
1258 ! znnt=1.
1259 ! call nntprt(snoclm,len,znnt)
1260 !
1261 ! get snow cover from snow depth array
1262 !
1263  if(fnscvc(1:8).eq.' ') then
1264  call getscv(snoclm,scvclm,len)
1265  endif
1266 !
1267 ! set tsfc over snow to tsfsmx if greater
1268 !
1269  call snosfc(snoclm,tsfclm,tsfsmx,len,me)
1270 ! call snosfc(snoclm,tsfcl2,tsfsmx,len)
1271 
1272 !
1273 ! quality control
1274 !
1275  do i=1,len
1276  icefl2(i) = sicclm(i) .gt. 0.99999
1277  enddo
1278  kqcm=1
1279  call qcmxmn('tsfc ',tsfclm,sliclm,snoclm,icefl2,
1280  & tsflmx,tsflmn,tsfomx,tsfomn,tsfimx,tsfimn,
1281  & tsfjmx,tsfjmn,tsfsmx,tsfsmn,epstsf,
1282  & rla,rlo,len,kqcm,percrit,lgchek,me)
1283  call qcmxmn('tsf2 ',tsfcl2,sliclm,snoclm,icefl2,
1284  & tsflmx,tsflmn,tsfomx,tsfomn,tsfimx,tsfimn,
1285  & tsfjmx,tsfjmn,tsfsmx,tsfsmn,epstsf,
1286  & rla,rlo,len,kqcm,percrit,lgchek,me)
1287  do kk = 1, 4
1288  call qcmxmn('albc ',albclm(1,kk),sliclm,snoclm,icefl1,
1289  & alblmx,alblmn,albomx,albomn,albimx,albimn,
1290  & albjmx,albjmn,albsmx,albsmn,epsalb,
1291  & rla,rlo,len,kqcm,percrit,lgchek,me)
1292  enddo
1293  if(fnwetc(1:8).ne.' ') then
1294  call qcmxmn('wetc ',wetclm,sliclm,snoclm,icefl1,
1295  & wetlmx,wetlmn,wetomx,wetomn,wetimx,wetimn,
1296  & wetjmx,wetjmn,wetsmx,wetsmn,epswet,
1297  & rla,rlo,len,kqcm,percrit,lgchek,me)
1298  endif
1299  call qcmxmn('zorc ',zorclm,sliclm,snoclm,icefl1,
1300  & zorlmx,zorlmn,zoromx,zoromn,zorimx,zorimn,
1301  & zorjmx,zorjmn,zorsmx,zorsmn,epszor,
1302  & rla,rlo,len,kqcm,percrit,lgchek,me)
1303 ! if(fnplrc(1:8).ne.' ') then
1304 ! call qcmxmn('plntc ',plrclm,sliclm,snoclm,icefl1,
1305 ! & plrlmx,plrlmn,plromx,plromn,plrimx,plrimn,
1306 ! & plrjmx,plrjmn,plrsmx,plrsmn,epsplr,
1307 ! & rla,rlo,len,kqcm,percrit,lgchek,me)
1308 ! endif
1309  call qcmxmn('tg3c ',tg3clm,sliclm,snoclm,icefl1,
1310  & tg3lmx,tg3lmn,tg3omx,tg3omn,tg3imx,tg3imn,
1311  & tg3jmx,tg3jmn,tg3smx,tg3smn,epstg3,
1312  & rla,rlo,len,kqcm,percrit,lgchek,me)
1313 !
1314 ! get soil temp and moisture (after all the qcs are completed)
1315 !
1316  if(fnsmcc(1:8).eq.' ') then
1317  call getsmc(wetclm,len,lsoil,smcclm,me)
1318  endif
1319  call qcmxmn('smc1c ',smcclm(1,1),sliclm,snoclm,icefl1,
1320  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1321  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1322  & rla,rlo,len,kqcm,percrit,lgchek,me)
1323  call qcmxmn('smc2c ',smcclm(1,2),sliclm,snoclm,icefl1,
1324  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1325  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1326  & rla,rlo,len,kqcm,percrit,lgchek,me)
1327 !clu [+8l] add smcclm(3:4)
1328  if(lsoil.gt.2) then
1329  call qcmxmn('smc3c ',smcclm(1,3),sliclm,snoclm,icefl1,
1330  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1331  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1332  & rla,rlo,len,kqcm,percrit,lgchek,me)
1333  call qcmxmn('smc4c ',smcclm(1,4),sliclm,snoclm,icefl1,
1334  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1335  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1336  & rla,rlo,len,kqcm,percrit,lgchek,me)
1337  endif
1338  if(fnstcc(1:8).eq.' ') then
1339  call getstc(tsfclm,tg3clm,sliclm,len,lsoil,stcclm,tsfimx)
1340  endif
1341  call qcmxmn('stc1c ',stcclm(1,1),sliclm,snoclm,icefl1,
1342  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1343  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1344  & rla,rlo,len,kqcm,percrit,lgchek,me)
1345  call qcmxmn('stc2c ',stcclm(1,2),sliclm,snoclm,icefl1,
1346  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1347  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1348  & rla,rlo,len,kqcm,percrit,lgchek,me)
1349 !clu [+8l] add stcclm(3:4)
1350  if(lsoil.gt.2) then
1351  call qcmxmn('stc3c ',stcclm(1,3),sliclm,snoclm,icefl1,
1352  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1353  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1354  & rla,rlo,len,kqcm,percrit,lgchek,me)
1355  call qcmxmn('stc4c ',stcclm(1,4),sliclm,snoclm,icefl1,
1356  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1357  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1358  & rla,rlo,len,kqcm,percrit,lgchek,me)
1359  endif
1360  call qcmxmn('vegc ',vegclm,sliclm,snoclm,icefl1,
1361  & veglmx,veglmn,vegomx,vegomn,vegimx,vegimn,
1362  & vegjmx,vegjmn,vegsmx,vegsmn,epsveg,
1363  & rla,rlo,len,kqcm,percrit,lgchek,me)
1364  call qcmxmn('vetc ',vetclm,sliclm,snoclm,icefl1,
1365  & vetlmx,vetlmn,vetomx,vetomn,vetimx,vetimn,
1366  & vetjmx,vetjmn,vetsmx,vetsmn,epsvet,
1367  & rla,rlo,len,kqcm,percrit,lgchek,me)
1368  call qcmxmn('sotc ',sotclm,sliclm,snoclm,icefl1,
1369  & sotlmx,sotlmn,sotomx,sotomn,sotimx,sotimn,
1370  & sotjmx,sotjmn,sotsmx,sotsmn,epssot,
1371  & rla,rlo,len,kqcm,percrit,lgchek,me)
1372 !cwu [+8l] ---------------------------------------------------------------
1373  call qcmxmn('sihc ',sihclm,sliclm,snoclm,icefl1,
1374  & sihlmx,sihlmn,sihomx,sihomn,sihimx,sihimn,
1375  & sihjmx,sihjmn,sihsmx,sihsmn,epssih,
1376  & rla,rlo,len,kqcm,percrit,lgchek,me)
1377  call qcmxmn('sicc ',sicclm,sliclm,snoclm,icefl1,
1378  & siclmx,siclmn,sicomx,sicomn,sicimx,sicimn,
1379  & sicjmx,sicjmn,sicsmx,sicsmn,epssic,
1380  & rla,rlo,len,kqcm,percrit,lgchek,me)
1381 !clu [+16l] ---------------------------------------------------------------
1382  call qcmxmn('vmnc ',vmnclm,sliclm,snoclm,icefl1,
1383  & vmnlmx,vmnlmn,vmnomx,vmnomn,vmnimx,vmnimn,
1384  & vmnjmx,vmnjmn,vmnsmx,vmnsmn,epsvmn,
1385  & rla,rlo,len,kqcm,percrit,lgchek,me)
1386  call qcmxmn('vmxc ',vmxclm,sliclm,snoclm,icefl1,
1387  & vmxlmx,vmxlmn,vmxomx,vmxomn,vmximx,vmximn,
1388  & vmxjmx,vmxjmn,vmxsmx,vmxsmn,epsvmx,
1389  & rla,rlo,len,kqcm,percrit,lgchek,me)
1390  call qcmxmn('slpc ',slpclm,sliclm,snoclm,icefl1,
1391  & slplmx,slplmn,slpomx,slpomn,slpimx,slpimn,
1392  & slpjmx,slpjmn,slpsmx,slpsmn,epsslp,
1393  & rla,rlo,len,kqcm,percrit,lgchek,me)
1394  call qcmxmn('absc ',absclm,sliclm,snoclm,icefl1,
1395  & abslmx,abslmn,absomx,absomn,absimx,absimn,
1396  & absjmx,absjmn,abssmx,abssmn,epsabs,
1397  & rla,rlo,len,kqcm,percrit,lgchek,me)
1398 !clu ----------------------------------------------------------------------
1399 !
1400 ! monitoring prints
1401 !
1402  if (monclm) then
1403  if (me .eq. 0) then
1404  print *,' '
1405  print *,'monitor of time and space interpolated climatology'
1406  print *,' '
1407 ! call count(sliclm,snoclm,len)
1408  print *,' '
1409  call monitr('tsfclm',tsfclm,sliclm,snoclm,len)
1410  call monitr('albclm',albclm(1,1),sliclm,snoclm,len)
1411  call monitr('albclm',albclm(1,2),sliclm,snoclm,len)
1412  call monitr('albclm',albclm(1,3),sliclm,snoclm,len)
1413  call monitr('albclm',albclm(1,4),sliclm,snoclm,len)
1414  call monitr('aisclm',aisclm,sliclm,snoclm,len)
1415  call monitr('snoclm',snoclm,sliclm,snoclm,len)
1416  call monitr('scvclm',scvclm,sliclm,snoclm,len)
1417  call monitr('smcclm1',smcclm(1,1),sliclm,snoclm,len)
1418  call monitr('smcclm2',smcclm(1,2),sliclm,snoclm,len)
1419  call monitr('stcclm1',stcclm(1,1),sliclm,snoclm,len)
1420  call monitr('stcclm2',stcclm(1,2),sliclm,snoclm,len)
1421 !clu [+4l] add smcclm(3:4) and stcclm(3:4)
1422  if(lsoil.gt.2) then
1423  call monitr('smcclm3',smcclm(1,3),sliclm,snoclm,len)
1424  call monitr('smcclm4',smcclm(1,4),sliclm,snoclm,len)
1425  call monitr('stcclm3',stcclm(1,3),sliclm,snoclm,len)
1426  call monitr('stcclm4',stcclm(1,4),sliclm,snoclm,len)
1427  endif
1428  call monitr('tg3clm',tg3clm,sliclm,snoclm,len)
1429  call monitr('zorclm',zorclm,sliclm,snoclm,len)
1430 ! if (gaus) then
1431  call monitr('cvaclm',cvclm ,sliclm,snoclm,len)
1432  call monitr('cvbclm',cvbclm,sliclm,snoclm,len)
1433  call monitr('cvtclm',cvtclm,sliclm,snoclm,len)
1434 ! endif
1435  call monitr('sliclm',sliclm,sliclm,snoclm,len)
1436 ! call monitr('plrclm',plrclm,sliclm,snoclm,len)
1437  call monitr('orog ',orog ,sliclm,snoclm,len)
1438  call monitr('vegclm',vegclm,sliclm,snoclm,len)
1439  call monitr('vetclm',vetclm,sliclm,snoclm,len)
1440  call monitr('sotclm',sotclm,sliclm,snoclm,len)
1441 !cwu [+2l] add sih, sic
1442  call monitr('sihclm',sihclm,sliclm,snoclm,len)
1443  call monitr('sicclm',sicclm,sliclm,snoclm,len)
1444 !clu [+4l] add vmn, vmx, slp, abs
1445  call monitr('vmnclm',vmnclm,sliclm,snoclm,len)
1446  call monitr('vmxclm',vmxclm,sliclm,snoclm,len)
1447  call monitr('slpclm',slpclm,sliclm,snoclm,len)
1448  call monitr('absclm',absclm,sliclm,snoclm,len)
1449  endif
1450  endif
1451 !
1452 !
1453  if (me .eq. 0) then
1454  write(6,*) '=============='
1455  write(6,*) ' analysis'
1456  write(6,*) '=============='
1457  endif
1458 !
1459 ! fill in analysis array with climatology before reading analysis.
1460 !
1461  call filanl(tsfanl,tsfan2,wetanl,snoanl,zoranl,albanl,aisanl,
1462  & tg3anl,cvanl ,cvbanl,cvtanl,
1463  & cnpanl,smcanl,stcanl,slianl,scvanl,veganl,
1464  & vetanl,sotanl,alfanl,
1465  & sihanl,sicanl,
1466  & vmnanl,vmxanl,slpanl,absanl,
1467  & tsfclm,tsfcl2,wetclm,snoclm,zorclm,albclm,aisclm,
1468  & tg3clm,cvclm ,cvbclm,cvtclm,
1469  & cnpclm,smcclm,stcclm,sliclm,scvclm,vegclm,
1470  & vetclm,sotclm,alfclm,
1471  & sihclm,sicclm,
1472  & vmnclm,vmxclm,slpclm,absclm,
1473  & len,lsoil)
1474 !
1475 ! reverse scaling to match with grib analysis input
1476 !
1477  zsca=0.01
1478  call scale(zoranl,len, zsca)
1479  zsca=100.
1480  call scale(albanl,len,zsca)
1481  call scale(albanl(1,2),len,zsca)
1482  call scale(albanl(1,3),len,zsca)
1483  call scale(albanl(1,4),len,zsca)
1484  call scale(alfanl,len,zsca)
1485  call scale(alfanl(1,2),len,zsca)
1486 !clu [+4l] reverse scale for vmn, vmx, abs
1487  zsca=100.
1488  call scale(vmnanl,len,zsca)
1489  call scale(vmxanl,len,zsca)
1490  call scale(absanl,len,zsca)
1491 !
1492  percrit=critp2
1493 !
1494 ! read analysis fields
1495 !
1496  call analy(lugb,iy,im,id,ih,fh,len,lsoil,slmask,
1497  & fntsfa,fnweta,fnsnoa,fnzora,fnalba,fnaisa,
1498  & fntg3a,fnscva,fnsmca,fnstca,fnacna,fnvega,
1499  & fnveta,fnsota,
1500  & fnvmna,fnvmxa,fnslpa,fnabsa,
1501  & tsfanl,wetanl,snoanl,zoranl,albanl,aisanl,
1502  & tg3anl,cvanl ,cvbanl,cvtanl,
1503  & smcanl,stcanl,slianl,scvanl,acnanl,veganl,
1504  & vetanl,sotanl,alfanl,tsfan0,
1505  & vmnanl,vmxanl,slpanl,absanl,
1506  & kpdtsf,kpdwet,kpdsno,kpdsnd,kpdzor,kpdalb,kpdais,
1507  & kpdtg3,kpdscv,kpdacn,kpdsmc,kpdstc,kpdveg,
1508  & kpdvet,kpdsot,kpdalf,
1509  & kpdvmn,kpdvmx,kpdslp,kpdabs,
1510  & irttsf,irtwet,irtsno,irtzor,irtalb,irtais,
1511  & irttg3,irtscv,irtacn,irtsmc,irtstc,irtveg,
1512  & irtvet,irtsot,irtalf
1513  &, irtvmn,irtvmx,irtslp,irtabs,
1514  & imsk, jmsk, slmskh, rla, rlo, gausm, blnmsk, bltmsk
1515  &, me, lanom)
1516 ! if(lprnt) print *,' tsfanl=',tsfanl(iprnt)
1517 !
1518 ! scale zor and alb to match forecast model units
1519 !
1520  zsca=100.
1521  call scale(zoranl,len, zsca)
1522  zsca=0.01
1523  call scale(albanl,len,zsca)
1524  call scale(albanl(1,2),len,zsca)
1525  call scale(albanl(1,3),len,zsca)
1526  call scale(albanl(1,4),len,zsca)
1527  call scale(alfanl,len,zsca)
1528  call scale(alfanl(1,2),len,zsca)
1529 !clu [+4] scale vmn, vmx, abs from percent to fraction
1530  zsca=0.01
1531  call scale(vmnanl,len,zsca)
1532  call scale(vmxanl,len,zsca)
1533  call scale(absanl,len,zsca)
1534 !
1535 ! interpolate climatology but fixing initial anomaly
1536 !
1537  if(fh > 0.0 .and. fntsfa(1:8) /= ' ' .and. lanom) then
1538  call anomint(tsfan0,tsfclm,tsfcl0,tsfanl,len)
1539  endif
1540 !
1541 ! if the tsfanl is at sea level, then bring it to the surface using
1542 ! unfiltered orography (for lakes). if the analysis is at lake surface
1543 ! as in the nst model, then this call should be removed - moorthi 09/23/2011
1544 !
1545  if (use_ufo .and. .not. nst_anl) then
1546  ztsfc = 0.0
1547  call tsfcor(tsfanl,orog_uf,slmask,ztsfc,len,rlapse)
1548  endif
1549 !
1550 ! ice concentration or ice mask (only ice mask used in the model now)
1551 !
1552  if(fnaisa(1:8).ne.' ') then
1553 !cwu [+5l/-1l] update sihanl, sicanl
1554  do i=1,len
1555  sihanl(i) = 3.0*aisanl(i)
1556  sicanl(i) = aisanl(i)
1557  if(slmask(i).eq.0..and.glacir(i).eq.1..and.
1558  & sicanl(i).ne.1.) then
1559  sicanl(i) = sicimx
1560  sihfcs(i) = glacir_hice
1561  endif
1562  enddo
1563  crit=aislim
1564 !* crit=0.5
1565  call rof01(aisanl,len,'ge',crit)
1566  elseif(fnacna(1:8).ne.' ') then
1567 !cwu [+17l] update sihanl, sicanl
1568  do i=1,len
1569  sihanl(i) = 3.0*acnanl(i)
1570  sicanl(i) = acnanl(i)
1571  if(slmask(i).eq.0..and.glacir(i).eq.1..and.
1572  & sicanl(i).ne.1.) then
1573  sicanl(i) = sicimx
1574  sihfcs(i) = glacir_hice
1575  endif
1576  enddo
1577  crit=aislim
1578  do i=1,len
1579  if((slianl(i).eq.0.).and.(sicanl(i).ge.crit)) then
1580  slianl(i)=2.
1581 ! print *,'cycle - new ice form: fice=',sicanl(i)
1582  else if((slianl(i).ge.2.).and.(sicanl(i).lt.crit)) then
1583  slianl(i)=0.
1584 ! print *,'cycle - ice free: fice=',sicanl(i)
1585  else if((slianl(i).eq.1.).and.(sicanl(i).ge.sicimn)) then
1586 ! print *,'cycle - land covered by sea-ice: fice=',sicanl(i)
1587  sicanl(i)=0.
1588  endif
1589  enddo
1590 ! znnt=10.
1591 ! call nntprt(acnanl,len,znnt)
1592 ! if(lprnt) print *,' acnanl=',acnanl(iprnt)
1593 ! do i=1,len
1594 ! if (acnanl(i) .gt. 0.3 .and. aisclm(i) .eq. 1.0
1595 ! & .and. aisfcs(i) .ge. 0.75) acnanl(i) = aislim
1596 ! enddo
1597 ! if(lprnt) print *,' acnanl=',acnanl(iprnt)
1598  call rof01(acnanl,len,'ge',aislim)
1599  do i=1,len
1600  aisanl(i)=acnanl(i)
1601  enddo
1602  endif
1603 ! if(lprnt) print *,' aisanl1=',aisanl(iprnt),' glacir='
1604 ! &,glacir(iprnt),' slmask=',slmask(iprnt)
1605 !
1606  call qcsice(aisanl,glacir,amxice,aicice,aicsea,sllnd,slmask,
1607  & rla,rlo,len,me)
1608 !
1609 ! set ocean/land/sea-ice mask
1610 !
1611  call setlsi(slmask,aisanl,len,aicice,slianl)
1612 ! if(lprnt) print *,' aisanl=',aisanl(iprnt),' slianl='
1613 ! *,slianl(iprnt),' slmask=',slmask(iprnt)
1614 !
1615 !
1616  do k=1,lsoil
1617  do i=1,len
1618  if (slianl(i) .eq. 0) then
1619  smcanl(i,k) = smcomx
1620  stcanl(i,k) = tsfanl(i)
1621  endif
1622  enddo
1623  enddo
1624 
1625 ! write(6,*) 'slianl'
1626 ! znnt=1.
1627 ! call nntprt(slianl,len,znnt)
1628 !cwu [+8l]----------------------------------------------------------------------
1629  call qcmxmn('siha ',sihanl,slianl,snoanl,icefl1,
1630  & sihlmx,sihlmn,sihomx,sihomn,sihimx,sihimn,
1631  & sihjmx,sihjmn,sihsmx,sihsmn,epssih,
1632  & rla,rlo,len,kqcm,percrit,lgchek,me)
1633  call qcmxmn('sica ',sicanl,slianl,snoanl,icefl1,
1634  & siclmx,siclmn,sicomx,sicomn,sicimx,sicimn,
1635  & sicjmx,sicjmn,sicsmx,sicsmn,epssic,
1636  & rla,rlo,len,kqcm,percrit,lgchek,me)
1637 !
1638 ! set albedo over ocean to albomx
1639 !
1640  call albocn(albanl,slmask,albomx,len)
1641 !
1642 ! quality control of snow and sea-ice
1643 ! process snow depth or snow cover
1644 !
1645  if(fnsnoa(1:8).ne.' ') then
1646  call setzro(snoanl,epssno,len)
1647  call qcsnow(snoanl,slmask,aisanl,glacir,len,ten,landice,me)
1648  if (.not.landice) then
1649  call snodpth2(glacir,snosmx,snoanl, len, me)
1650  endif
1651  kqcm=1
1652  call snosfc(snoanl,tsfanl,tsfsmx,len,me)
1653  call qcmxmn('snoa ',snoanl,slianl,snoanl,icefl1,
1654  & snolmx,snolmn,snoomx,snoomn,snoimx,snoimn,
1655  & snojmx,snojmn,snosmx,snosmn,epssno,
1656  & rla,rlo,len,kqcm,percrit,lgchek,me)
1657  call getscv(snoanl,scvanl,len)
1658  call qcmxmn('sncva ',scvanl,slianl,snoanl,icefl1,
1659  & scvlmx,scvlmn,scvomx,scvomn,scvimx,scvimn,
1660  & scvjmx,scvjmn,scvsmx,scvsmn,epsscv,
1661  & rla,rlo,len,kqcm,percrit,lgchek,me)
1662  else
1663  crit=0.5
1664  call rof01(scvanl,len,'ge',crit)
1665  call qcsnow(scvanl,slmask,aisanl,glacir,len,one,landice,me)
1666  call qcmxmn('sncva ',scvanl,slianl,scvanl,icefl1,
1667  & scvlmx,scvlmn,scvomx,scvomn,scvimx,scvimn,
1668  & scvjmx,scvjmn,scvsmx,scvsmn,epsscv,
1669  & rla,rlo,len,kqcm,percrit,lgchek,me)
1670  call snodpth(scvanl,slianl,tsfanl,snoclm,
1671  & glacir,snwmax,snwmin,landice,len,snoanl,me)
1672  call qcsnow(scvanl,slmask,aisanl,glacir,len,snosmx,landice,me)
1673  call snosfc(snoanl,tsfanl,tsfsmx,len,me)
1674  call qcmxmn('snowa ',snoanl,slianl,snoanl,icefl1,
1675  & snolmx,snolmn,snoomx,snoomn,snoimx,snoimn,
1676  & snojmx,snojmn,snosmx,snosmn,epssno,
1677  & rla,rlo,len,kqcm,percrit,lgchek,me)
1678  endif
1679 !
1680  do i=1,len
1681  icefl2(i) = sicanl(i) .gt. 0.99999
1682  enddo
1683  call qcmxmn('tsfa ',tsfanl,slianl,snoanl,icefl2,
1684  & tsflmx,tsflmn,tsfomx,tsfomn,tsfimx,tsfimn,
1685  & tsfjmx,tsfjmn,tsfsmx,tsfsmn,epstsf,
1686  & rla,rlo,len,kqcm,percrit,lgchek,me)
1687  do kk = 1, 4
1688  call qcmxmn('alba ',albanl(1,kk),slianl,snoanl,icefl1,
1689  & alblmx,alblmn,albomx,albomn,albimx,albimn,
1690  & albjmx,albjmn,albsmx,albsmn,epsalb,
1691  & rla,rlo,len,kqcm,percrit,lgchek,me)
1692  enddo
1693  if(fnwetc(1:8).ne.' ' .or. fnweta(1:8).ne.' ' ) then
1694  call qcmxmn('weta ',wetanl,slianl,snoanl,icefl1,
1695  & wetlmx,wetlmn,wetomx,wetomn,wetimx,wetimn,
1696  & wetjmx,wetjmn,wetsmx,wetsmn,epswet,
1697  & rla,rlo,len,kqcm,percrit,lgchek,me)
1698  endif
1699  call qcmxmn('zora ',zoranl,slianl,snoanl,icefl1,
1700  & zorlmx,zorlmn,zoromx,zoromn,zorimx,zorimn,
1701  & zorjmx,zorjmn,zorsmx,zorsmn,epszor,
1702  & rla,rlo,len,kqcm,percrit,lgchek,me)
1703 ! if(fnplrc(1:8).ne.' ' .or. fnplra(1:8).ne.' ' ) then
1704 ! call qcmxmn('plna ',plranl,slianl,snoanl,icefl1,
1705 ! & plrlmx,plrlmn,plromx,plromn,plrimx,plrimn,
1706 ! & plrjmx,plrjmn,plrsmx,plrsmn,epsplr,
1707 ! & rla,rlo,len,kqcm,percrit,lgchek,me)
1708 ! endif
1709  call qcmxmn('tg3a ',tg3anl,slianl,snoanl,icefl1,
1710  & tg3lmx,tg3lmn,tg3omx,tg3omn,tg3imx,tg3imn,
1711  & tg3jmx,tg3jmn,tg3smx,tg3smn,epstg3,
1712  & rla,rlo,len,kqcm,percrit,lgchek,me)
1713 !
1714 ! get soil temp and moisture
1715 !
1716  if(fnsmca(1:8).eq.' ' .and. fnsmcc(1:8).eq.' ') then
1717  call getsmc(wetanl,len,lsoil,smcanl,me)
1718  endif
1719  call qcmxmn('smc1a ',smcanl(1,1),slianl,snoanl,icefl1,
1720  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1721  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1722  & rla,rlo,len,kqcm,percrit,lgchek,me)
1723  call qcmxmn('smc2a ',smcanl(1,2),slianl,snoanl,icefl1,
1724  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1725  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1726  & rla,rlo,len,kqcm,percrit,lgchek,me)
1727 !clu [+8l] add smcanl(3:4)
1728  if(lsoil.gt.2) then
1729  call qcmxmn('smc3a ',smcanl(1,3),slianl,snoanl,icefl1,
1730  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1731  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1732  & rla,rlo,len,kqcm,percrit,lgchek,me)
1733  call qcmxmn('smc4a ',smcanl(1,4),slianl,snoanl,icefl1,
1734  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1735  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1736  & rla,rlo,len,kqcm,percrit,lgchek,me)
1737  endif
1738  if(fnstca(1:8).eq.' ') then
1739  call getstc(tsfanl,tg3anl,slianl,len,lsoil,stcanl,tsfimx)
1740  endif
1741  call qcmxmn('stc1a ',stcanl(1,1),slianl,snoanl,icefl1,
1742  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1743  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1744  & rla,rlo,len,kqcm,percrit,lgchek,me)
1745  call qcmxmn('stc2a ',stcanl(1,2),slianl,snoanl,icefl1,
1746  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1747  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1748  & rla,rlo,len,kqcm,percrit,lgchek,me)
1749 !clu [+8l] add stcanl(3:4)
1750  if(lsoil.gt.2) then
1751  call qcmxmn('stc3a ',stcanl(1,3),slianl,snoanl,icefl1,
1752  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1753  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1754  & rla,rlo,len,kqcm,percrit,lgchek,me)
1755  call qcmxmn('stc4a ',stcanl(1,4),slianl,snoanl,icefl1,
1756  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1757  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1758  & rla,rlo,len,kqcm,percrit,lgchek,me)
1759  endif
1760  call qcmxmn('vega ',veganl,slianl,snoanl,icefl1,
1761  & veglmx,veglmn,vegomx,vegomn,vegimx,vegimn,
1762  & vegjmx,vegjmn,vegsmx,vegsmn,epsveg,
1763  & rla,rlo,len,kqcm,percrit,lgchek,me)
1764  call qcmxmn('veta ',vetanl,slianl,snoanl,icefl1,
1765  & vetlmx,vetlmn,vetomx,vetomn,vetimx,vetimn,
1766  & vetjmx,vetjmn,vetsmx,vetsmn,epsvet,
1767  & rla,rlo,len,kqcm,percrit,lgchek,me)
1768  call qcmxmn('sota ',sotanl,slianl,snoanl,icefl1,
1769  & sotlmx,sotlmn,sotomx,sotomn,sotimx,sotimn,
1770  & sotjmx,sotjmn,sotsmx,sotsmn,epssot,
1771  & rla,rlo,len,kqcm,percrit,lgchek,me)
1772 !clu [+16l]----------------------------------------------------------------------
1773  call qcmxmn('vmna ',vmnanl,slianl,snoanl,icefl1,
1774  & vmnlmx,vmnlmn,vmnomx,vmnomn,vmnimx,vmnimn,
1775  & vmnjmx,vmnjmn,vmnsmx,vmnsmn,epsvmn,
1776  & rla,rlo,len,kqcm,percrit,lgchek,me)
1777  call qcmxmn('vmxa ',vmxanl,slianl,snoanl,icefl1,
1778  & vmxlmx,vmxlmn,vmxomx,vmxomn,vmximx,vmximn,
1779  & vmxjmx,vmxjmn,vmxsmx,vmxsmn,epsvmx,
1780  & rla,rlo,len,kqcm,percrit,lgchek,me)
1781  call qcmxmn('slpa ',slpanl,slianl,snoanl,icefl1,
1782  & slplmx,slplmn,slpomx,slpomn,slpimx,slpimn,
1783  & slpjmx,slpjmn,slpsmx,slpsmn,epsslp,
1784  & rla,rlo,len,kqcm,percrit,lgchek,me)
1785  call qcmxmn('absa ',absanl,slianl,snoanl,icefl1,
1786  & abslmx,abslmn,absomx,absomn,absimx,absimn,
1787  & absjmx,absjmn,abssmx,abssmn,epsabs,
1788  & rla,rlo,len,kqcm,percrit,lgchek,me)
1789 !clu ----------------------------------------------------------------------------
1790 !
1791 ! monitoring prints
1792 !
1793  if (monanl) then
1794  if (me .eq. 0) then
1795  print *,' '
1796  print *,'monitor of time and space interpolated analysis'
1797  print *,' '
1798 ! call count(slianl,snoanl,len)
1799  print *,' '
1800  call monitr('tsfanl',tsfanl,slianl,snoanl,len)
1801  call monitr('albanl',albanl,slianl,snoanl,len)
1802  call monitr('aisanl',aisanl,slianl,snoanl,len)
1803  call monitr('snoanl',snoanl,slianl,snoanl,len)
1804  call monitr('scvanl',scvanl,slianl,snoanl,len)
1805  call monitr('smcanl1',smcanl(1,1),slianl,snoanl,len)
1806  call monitr('smcanl2',smcanl(1,2),slianl,snoanl,len)
1807  call monitr('stcanl1',stcanl(1,1),slianl,snoanl,len)
1808  call monitr('stcanl2',stcanl(1,2),slianl,snoanl,len)
1809 !clu [+4l] add smcanl(3:4) and stcanl(3:4)
1810  if(lsoil.gt.2) then
1811  call monitr('smcanl3',smcanl(1,3),slianl,snoanl,len)
1812  call monitr('smcanl4',smcanl(1,4),slianl,snoanl,len)
1813  call monitr('stcanl3',stcanl(1,3),slianl,snoanl,len)
1814  call monitr('stcanl4',stcanl(1,4),slianl,snoanl,len)
1815  endif
1816  call monitr('tg3anl',tg3anl,slianl,snoanl,len)
1817  call monitr('zoranl',zoranl,slianl,snoanl,len)
1818 ! if (gaus) then
1819  call monitr('cvaanl',cvanl ,slianl,snoanl,len)
1820  call monitr('cvbanl',cvbanl,slianl,snoanl,len)
1821  call monitr('cvtanl',cvtanl,slianl,snoanl,len)
1822 ! endif
1823  call monitr('slianl',slianl,slianl,snoanl,len)
1824 ! call monitr('plranl',plranl,slianl,snoanl,len)
1825  call monitr('orog ',orog ,slianl,snoanl,len)
1826  call monitr('veganl',veganl,slianl,snoanl,len)
1827  call monitr('vetanl',vetanl,slianl,snoanl,len)
1828  call monitr('sotanl',sotanl,slianl,snoanl,len)
1829 !cwu [+2l] add sih, sic
1830  call monitr('sihanl',sihanl,slianl,snoanl,len)
1831  call monitr('sicanl',sicanl,slianl,snoanl,len)
1832 !clu [+4l] add vmn, vmx, slp, abs
1833  call monitr('vmnanl',vmnanl,slianl,snoanl,len)
1834  call monitr('vmxanl',vmxanl,slianl,snoanl,len)
1835  call monitr('slpanl',slpanl,slianl,snoanl,len)
1836  call monitr('absanl',absanl,slianl,snoanl,len)
1837  endif
1838 
1839  endif
1840 !
1841 ! read in forecast fields if needed
1842 !
1843  if (me .eq. 0) then
1844  write(6,*) '=============='
1845  write(6,*) ' fcst guess'
1846  write(6,*) '=============='
1847  endif
1848 !
1849  percrit=critp2
1850 !
1851  if(deads) then
1852 !
1853 ! fill in guess array with analysis if dead start.
1854 !
1855  percrit=critp3
1856  if (me .eq. 0) write(6,*) 'this run is dead start run'
1857  call filfcs(tsffcs,wetfcs,snofcs,zorfcs,albfcs,
1858  & tg3fcs,cvfcs ,cvbfcs,cvtfcs,
1859  & cnpfcs,smcfcs,stcfcs,slifcs,aisfcs,
1860  & vegfcs,vetfcs,sotfcs,alffcs,
1861 !cwu [+1l] add ()fcs for sih, sic
1862  & sihfcs,sicfcs,
1863 !clu [+1l] add ()fcs for vmn, vmx, slp, abs
1864  & vmnfcs,vmxfcs,slpfcs,absfcs,
1865  & tsfanl,wetanl,snoanl,zoranl,albanl,
1866  & tg3anl,cvanl ,cvbanl,cvtanl,
1867  & cnpanl,smcanl,stcanl,slianl,aisanl,
1868  & veganl,vetanl,sotanl,alfanl,
1869 !cwu [+1l] add ()anl for sih, sic
1870  & sihanl,sicanl,
1871 !clu [+1l] add ()anl for vmn, vmx, slp, abs
1872  & vmnanl,vmxanl,slpanl,absanl,
1873  & len,lsoil)
1874  if(sig1t(1).ne.0.) then
1875  call usesgt(sig1t,slianl,tg3anl,len,lsoil,tsffcs,stcfcs,
1876  & tsfimx)
1877  do i=1,len
1878  icefl2(i) = sicfcs(i) .gt. 0.99999
1879  enddo
1880  kqcm=1
1881  call qcmxmn('tsff ',tsffcs,slifcs,snofcs,icefl2,
1882  & tsflmx,tsflmn,tsfomx,tsfomn,tsfimx,tsfimn,
1883  & tsfjmx,tsfjmn,tsfsmx,tsfsmn,epstsf,
1884  & rla,rlo,len,kqcm,percrit,lgchek,me)
1885  call qcmxmn('stc1f ',stcfcs(1,1),slifcs,snofcs,icefl1,
1886  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1887  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1888  & rla,rlo,len,kqcm,percrit,lgchek,me)
1889  call qcmxmn('stc2f ',stcfcs(1,2),slifcs,snofcs,icefl1,
1890  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
1891  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
1892  & rla,rlo,len,kqcm,percrit,lgchek,me)
1893  endif
1894  else
1895  percrit=critp2
1896 !
1897 ! make reverse angulation correction to tsf
1898 ! make reverse orography correction to tg3
1899 !
1900  if (use_ufo) then
1901  orogd = orog - orog_uf
1902 !
1903 ! The tiled version of the substrate temperature is properly
1904 ! adjusted to the terrain. Only invoke when using the old
1905 ! global tg3 grib file.
1906 !
1907  if ( index(fntg3c, "tileX.nc") == 0) then ! global file
1908  ztsfc = 1.0
1909  call tsfcor(tg3fcs,orogd,slmask,ztsfc,len,-rlapse)
1910  endif
1911  ztsfc = 0.
1912  call tsfcor(tsffcs,orogd,slmask,ztsfc,len,-rlapse)
1913  else
1914  ztsfc = 0.
1915  call tsfcor(tsffcs,orog,slmask,ztsfc,len,-rlapse)
1916  endif
1917 
1918 !clu [+12l] --------------------------------------------------------------
1919 !
1920 ! compute soil moisture liquid-to-total ratio over land
1921 !
1922  do j=1, lsoil
1923  do i=1, len
1924  if(smcfcs(i,j) .ne. 0.) then
1925  swratio(i,j) = slcfcs(i,j)/smcfcs(i,j)
1926  else
1927  swratio(i,j) = -999.
1928  endif
1929  enddo
1930  enddo
1931 !clu -----------------------------------------------------------------------
1932 !
1933  if(lqcbgs .and. irtacn .eq. 0) then
1934  call qcsli(slianl,slifcs,len,me)
1935  call albocn(albfcs,slmask,albomx,len)
1936  do i=1,len
1937  icefl2(i) = sicfcs(i) .gt. 0.99999
1938  enddo
1939  kqcm=1
1940  call qcmxmn('snof ',snofcs,slifcs,snofcs,icefl1,
1941  & snolmx,snolmn,snoomx,snoomn,snoimx,snoimn,
1942  & snojmx,snojmn,snosmx,snosmn,epssno,
1943  & rla,rlo,len,kqcm,percrit,lgchek,me)
1944  call qcmxmn('tsff ',tsffcs,slifcs,snofcs,icefl2,
1945  & tsflmx,tsflmn,tsfomx,tsfomn,tsfimx,tsfimn,
1946  & tsfjmx,tsfjmn,tsfsmx,tsfsmn,epstsf,
1947  & rla,rlo,len,kqcm,percrit,lgchek,me)
1948  do kk = 1, 4
1949  call qcmxmn('albf ',albfcs(1,kk),slifcs,snofcs,icefl1,
1950  & alblmx,alblmn,albomx,albomn,albimx,albimn,
1951  & albjmx,albjmn,albsmx,albsmn,epsalb,
1952  & rla,rlo,len,kqcm,percrit,lgchek,me)
1953  enddo
1954  if(fnwetc(1:8).ne.' ' .or. fnweta(1:8).ne.' ' )
1955  & then
1956  call qcmxmn('wetf ',wetfcs,slifcs,snofcs,icefl1,
1957  & wetlmx,wetlmn,wetomx,wetomn,wetimx,wetimn,
1958  & wetjmx,wetjmn,wetsmx,wetsmn,epswet,
1959  & rla,rlo,len,kqcm,percrit,lgchek,me)
1960  endif
1961  call qcmxmn('zorf ',zorfcs,slifcs,snofcs,icefl1,
1962  & zorlmx,zorlmn,zoromx,zoromn,zorimx,zorimn,
1963  & zorjmx,zorjmn,zorsmx,zorsmn,epszor,
1964  & rla,rlo,len,kqcm,percrit,lgchek,me)
1965 ! if(fnplrc(1:8).ne.' ' .or. fnplra(1:8).ne.' ' )
1966 ! call qcmxmn('plnf ',plrfcs,slifcs,snofcs,icefl1,
1967 ! & plrlmx,plrlmn,plromx,plromn,plrimx,plrimn,
1968 ! & plrjmx,plrjmn,plrsmx,plrsmn,epsplr,
1969 ! & rla,rlo,len,kqcm,percrit,lgchek,me)
1970 ! endif
1971  call qcmxmn('tg3f ',tg3fcs,slifcs,snofcs,icefl1,
1972  & tg3lmx,tg3lmn,tg3omx,tg3omn,tg3imx,tg3imn,
1973  & tg3jmx,tg3jmn,tg3smx,tg3smn,epstg3,
1974  & rla,rlo,len,kqcm,percrit,lgchek,me)
1975 !cwu [+8l] ---------------------------------------------------------------
1976  call qcmxmn('sihf ',sihfcs,slifcs,snofcs,icefl1,
1977  & sihlmx,sihlmn,sihomx,sihomn,sihimx,sihimn,
1978  & sihjmx,sihjmn,sihsmx,sihsmn,epssih,
1979  & rla,rlo,len,kqcm,percrit,lgchek,me)
1980  call qcmxmn('sicf ',sicfcs,slifcs,snofcs,icefl1,
1981  & siclmx,siclmn,sicomx,sicomn,sicimx,sicimn,
1982  & sicjmx,sicjmn,sicsmx,sicsmn,epssic,
1983  & rla,rlo,len,kqcm,percrit,lgchek,me)
1984  call qcmxmn('smc1f ',smcfcs(1,1),slifcs,snofcs,icefl1,
1985  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1986  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1987  & rla,rlo,len,kqcm,percrit,lgchek,me)
1988  call qcmxmn('smc2f ',smcfcs(1,2),slifcs,snofcs,icefl1,
1989  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1990  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1991  & rla,rlo,len,kqcm,percrit,lgchek,me)
1992 !clu [+8l] add smcfcs(3:4)
1993  if(lsoil.gt.2) then
1994  call qcmxmn('smc3f ',smcfcs(1,3),slifcs,snofcs,icefl1,
1995  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
1996  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
1997  & rla,rlo,len,kqcm,percrit,lgchek,me)
1998  call qcmxmn('smc4f ',smcfcs(1,4),slifcs,snofcs,icefl1,
1999  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
2000  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
2001  & rla,rlo,len,kqcm,percrit,lgchek,me)
2002  endif
2003  call qcmxmn('stc1f ',stcfcs(1,1),slifcs,snofcs,icefl1,
2004  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
2005  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
2006  & rla,rlo,len,kqcm,percrit,lgchek,me)
2007  call qcmxmn('stc2f ',stcfcs(1,2),slifcs,snofcs,icefl1,
2008  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
2009  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
2010  & rla,rlo,len,kqcm,percrit,lgchek,me)
2011 !clu [+8l] add stcfcs(3:4)
2012  if(lsoil.gt.2) then
2013  call qcmxmn('stc3f ',stcfcs(1,3),slifcs,snofcs,icefl1,
2014  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
2015  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
2016  & rla,rlo,len,kqcm,percrit,lgchek,me)
2017  call qcmxmn('stc4f ',stcfcs(1,4),slifcs,snofcs,icefl1,
2018  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
2019  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
2020  & rla,rlo,len,kqcm,percrit,lgchek,me)
2021  endif
2022  call qcmxmn('vegf ',vegfcs,slifcs,snofcs,icefl1,
2023  & veglmx,veglmn,vegomx,vegomn,vegimx,vegimn,
2024  & vegjmx,vegjmn,vegsmx,vegsmn,epsveg,
2025  & rla,rlo,len,kqcm,percrit,lgchek,me)
2026  call qcmxmn('vetf ',vetfcs,slifcs,snofcs,icefl1,
2027  & vetlmx,vetlmn,vetomx,vetomn,vetimx,vetimn,
2028  & vetjmx,vetjmn,vetsmx,vetsmn,epsvet,
2029  & rla,rlo,len,kqcm,percrit,lgchek,me)
2030  call qcmxmn('sotf ',sotfcs,slifcs,snofcs,icefl1,
2031  & sotlmx,sotlmn,sotomx,sotomn,sotimx,sotimn,
2032  & sotjmx,sotjmn,sotsmx,sotsmn,epssot,
2033  & rla,rlo,len,kqcm,percrit,lgchek,me)
2034 
2035 !clu [+16l] ---------------------------------------------------------------
2036  call qcmxmn('vmnf ',vmnfcs,slifcs,snofcs,icefl1,
2037  & vmnlmx,vmnlmn,vmnomx,vmnomn,vmnimx,vmnimn,
2038  & vmnjmx,vmnjmn,vmnsmx,vmnsmn,epsvmn,
2039  & rla,rlo,len,kqcm,percrit,lgchek,me)
2040  call qcmxmn('vmxf ',vmxfcs,slifcs,snofcs,icefl1,
2041  & vmxlmx,vmxlmn,vmxomx,vmxomn,vmximx,vmximn,
2042  & vmxjmx,vmxjmn,vmxsmx,vmxsmn,epsvmx,
2043  & rla,rlo,len,kqcm,percrit,lgchek,me)
2044  call qcmxmn('slpf ',slpfcs,slifcs,snofcs,icefl1,
2045  & slplmx,slplmn,slpomx,slpomn,slpimx,slpimn,
2046  & slpjmx,slpjmn,slpsmx,slpsmn,epsslp,
2047  & rla,rlo,len,kqcm,percrit,lgchek,me)
2048  call qcmxmn('absf ',absfcs,slifcs,snofcs,icefl1,
2049  & abslmx,abslmn,absomx,absomn,absimx,absimn,
2050  & absjmx,absjmn,abssmx,abssmn,epsabs,
2051  & rla,rlo,len,kqcm,percrit,lgchek,me)
2052 !clu -----------------------------------------------------------------------
2053  endif
2054  endif
2055 !
2056  if (monfcs) then
2057  if (me .eq. 0) then
2058  print *,' '
2059  print *,'monitor of guess'
2060  print *,' '
2061 ! call count(slifcs,snofcs,len)
2062  print *,' '
2063  call monitr('tsffcs',tsffcs,slifcs,snofcs,len)
2064  call monitr('albfcs',albfcs,slifcs,snofcs,len)
2065  call monitr('aisfcs',aisfcs,slifcs,snofcs,len)
2066  call monitr('snofcs',snofcs,slifcs,snofcs,len)
2067  call monitr('smcfcs1',smcfcs(1,1),slifcs,snofcs,len)
2068  call monitr('smcfcs2',smcfcs(1,2),slifcs,snofcs,len)
2069  call monitr('stcfcs1',stcfcs(1,1),slifcs,snofcs,len)
2070  call monitr('stcfcs2',stcfcs(1,2),slifcs,snofcs,len)
2071 !clu [+4l] add smcfcs(3:4) and stcfcs(3:4)
2072  if(lsoil.gt.2) then
2073  call monitr('smcfcs3',smcfcs(1,3),slifcs,snofcs,len)
2074  call monitr('smcfcs4',smcfcs(1,4),slifcs,snofcs,len)
2075  call monitr('stcfcs3',stcfcs(1,3),slifcs,snofcs,len)
2076  call monitr('stcfcs4',stcfcs(1,4),slifcs,snofcs,len)
2077  endif
2078  call monitr('tg3fcs',tg3fcs,slifcs,snofcs,len)
2079  call monitr('zorfcs',zorfcs,slifcs,snofcs,len)
2080 ! if (gaus) then
2081  call monitr('cvafcs',cvfcs ,slifcs,snofcs,len)
2082  call monitr('cvbfcs',cvbfcs,slifcs,snofcs,len)
2083  call monitr('cvtfcs',cvtfcs,slifcs,snofcs,len)
2084 ! endif
2085  call monitr('slifcs',slifcs,slifcs,snofcs,len)
2086 ! call monitr('plrfcs',plrfcs,slifcs,snofcs,len)
2087  call monitr('orog ',orog ,slifcs,snofcs,len)
2088  call monitr('vegfcs',vegfcs,slifcs,snofcs,len)
2089  call monitr('vetfcs',vetfcs,slifcs,snofcs,len)
2090  call monitr('sotfcs',sotfcs,slifcs,snofcs,len)
2091 !cwu [+2l] add sih, sic
2092  call monitr('sihfcs',sihfcs,slifcs,snofcs,len)
2093  call monitr('sicfcs',sicfcs,slifcs,snofcs,len)
2094 !clu [+4l] add vmn, vmx, slp, abs
2095  call monitr('vmnfcs',vmnfcs,slifcs,snofcs,len)
2096  call monitr('vmxfcs',vmxfcs,slifcs,snofcs,len)
2097  call monitr('slpfcs',slpfcs,slifcs,snofcs,len)
2098  call monitr('absfcs',absfcs,slifcs,snofcs,len)
2099  endif
2100  endif
2101 !
2102 !... update annual cycle in the sst guess..
2103 !
2104 ! if(lprnt) print *,'tsfclm=',tsfclm(iprnt),' tsfcl2=',tsfcl2(iprnt)
2105 ! *,' tsffcs=',tsffcs(iprnt),' slianl=',slianl(iprnt)
2106 
2107  if (fh-deltsfc > -0.001 ) then
2108  do i=1,len
2109  if(slianl(i) == 0.0) then
2110  tsffcs(i) = tsffcs(i) + (tsfclm(i) - tsfcl2(i))
2111  endif
2112  enddo
2113  endif
2114 !
2115 ! quality control analysis using forecast guess
2116 !
2117  call qcbyfc(tsffcs,snofcs,qctsfs,qcsnos,qctsfi,len,lsoil,
2118  & snoanl,aisanl,slianl,tsfanl,albanl,
2119  & zoranl,smcanl,
2120  & smcclm,tsfsmx,albomx,zoromx,me)
2121 !
2122 ! blend climatology and predicted fields
2123 !
2124  if(me .eq. 0) then
2125  write(6,*) '=============='
2126  write(6,*) ' merging'
2127  write(6,*) '=============='
2128  endif
2129 ! if(lprnt) print *,' tsffcs=',tsffcs(iprnt)
2130 !
2131  percrit=critp3
2132 !
2133 ! merge analysis and forecast. note tg3, ais are not merged
2134 !
2135  call merge(len,lsoil,iy,im,id,ih,fh,deltsfc,
2136  & sihfcs,sicfcs,
2137  & vmnfcs,vmxfcs,slpfcs,absfcs,
2138  & tsffcs,wetfcs,snofcs,zorfcs,albfcs,aisfcs,
2139  & cvfcs ,cvbfcs,cvtfcs,
2140  & cnpfcs,smcfcs,stcfcs,slifcs,vegfcs,
2141  & vetfcs,sotfcs,alffcs,
2142  & sihanl,sicanl,
2143  & vmnanl,vmxanl,slpanl,absanl,
2144  & tsfanl,tsfan2,wetanl,snoanl,zoranl,albanl,aisanl,
2145  & cvanl ,cvbanl,cvtanl,
2146  & cnpanl,smcanl,stcanl,slianl,veganl,
2147  & vetanl,sotanl,alfanl,
2148  & ctsfl,calbl,caisl,csnol,csmcl,czorl,cstcl,cvegl,
2149  & ctsfs,calbs,caiss,csnos,csmcs,czors,cstcs,cvegs,
2150  & ccv,ccvb,ccvt,ccnp,cvetl,cvets,csotl,csots,
2151  & calfl,calfs,
2152  & csihl,csihs,csicl,csics,
2153  & cvmnl,cvmns,cvmxl,cvmxs,cslpl,cslps,cabsl,cabss,
2154  & irttsf,irtwet,irtsno,irtzor,irtalb,irtais,
2155  & irttg3,irtscv,irtacn,irtsmc,irtstc,irtveg,
2156  & irtvmn,irtvmx,irtslp,irtabs,
2157  & irtvet,irtsot,irtalf,landice,me)
2158 
2159  call setzro(snoanl,epssno,len)
2160 
2161 ! if(lprnt) print *,' tanlm=',tsfanl(iprnt),' tfcsm=',tsffcs(iprnt)
2162 ! if(lprnt) print *,' sliam=',slianl(iprnt),' slifm=',slifcs(iprnt)
2163 
2164 !
2165 ! new ice/melted ice
2166 !
2167  call newice(slianl,slifcs,tsfanl,tsffcs,len,lsoil,
2168 !cwu [+1l] add sihnew, aislim, sihanl & sicanl
2169  & sihnew,aislim,sihanl,sicanl,
2170  & albanl,snoanl,zoranl,smcanl,stcanl,
2171  & albomx,snoomx,zoromx,smcomx,smcimx,
2172 !cwu [-1l/+1l] change albimx to albimn - note albimx & albimn have been modified
2173 ! & tsfomn,tsfimx,albimx,zorimx,tgice,
2174  & tsfomn,tsfimx,albimn,zorimx,tgice,
2175  & rla,rlo,me)
2176 
2177 ! if(lprnt) print *,'tsfanl=',tsfanl(iprnt),' tsffcs=',tsffcs(iprnt)
2178 ! if(lprnt) print *,' slian=',slianl(iprnt),' slifn=',slifcs(iprnt)
2179 !
2180 ! set tsfc to tsnow over snow
2181 !
2182  call snosfc(snoanl,tsfanl,tsfsmx,len,me)
2183 !
2184  do i=1,len
2185  icefl2(i) = sicanl(i) .gt. 0.99999
2186  enddo
2187  kqcm=0
2188  call qcmxmn('snowm ',snoanl,slianl,snoanl,icefl1,
2189  & snolmx,snolmn,snoomx,snoomn,snoimx,snoimn,
2190  & snojmx,snojmn,snosmx,snosmn,epssno,
2191  & rla,rlo,len,kqcm,percrit,lgchek,me)
2192  call qcmxmn('tsfm ',tsfanl,slianl,snoanl,icefl2,
2193  & tsflmx,tsflmn,tsfomx,tsfomn,tsfimx,tsfimn,
2194  & tsfjmx,tsfjmn,tsfsmx,tsfsmn,epstsf,
2195  & rla,rlo,len,kqcm,percrit,lgchek,me)
2196  do kk = 1, 4
2197  call qcmxmn('albm ',albanl(1,kk),slianl,snoanl,icefl1,
2198  & alblmx,alblmn,albomx,albomn,albimx,albimn,
2199  & albjmx,albjmn,albsmx,albsmn,epsalb,
2200  & rla,rlo,len,kqcm,percrit,lgchek,me)
2201  enddo
2202  if(fnwetc(1:8).ne.' ' .or. fnweta(1:8).ne.' ' )
2203  & then
2204  call qcmxmn('wetm ',wetanl,slianl,snoanl,icefl1,
2205  & wetlmx,wetlmn,wetomx,wetomn,wetimx,wetimn,
2206  & wetjmx,wetjmn,wetsmx,wetsmn,epswet,
2207  & rla,rlo,len,kqcm,percrit,lgchek,me)
2208  endif
2209  call qcmxmn('zorm ',zoranl,slianl,snoanl,icefl1,
2210  & zorlmx,zorlmn,zoromx,zoromn,zorimx,zorimn,
2211  & zorjmx,zorjmn,zorsmx,zorsmn,epszor,
2212  & rla,rlo,len,kqcm,percrit,lgchek,me)
2213 ! if(fnplrc(1:8).ne.' ' .or. fnplra(1:8).ne.' ' )
2214 ! & then
2215 ! call qcmxmn('plntm ',plranl,slianl,snoanl,icefl1,
2216 ! & plrlmx,plrlmn,plromx,plromn,plrimx,plrimn,
2217 ! & plrjmx,plrjmn,plrsmx,plrsmn,epsplr,
2218 ! & rla,rlo,len,kqcm,percrit,lgchek,me)
2219 ! endif
2220  call qcmxmn('stc1m ',stcanl(1,1),slianl,snoanl,icefl1,
2221  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
2222  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
2223  & rla,rlo,len,kqcm,percrit,lgchek,me)
2224  call qcmxmn('stc2m ',stcanl(1,2),slianl,snoanl,icefl1,
2225  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
2226  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
2227  & rla,rlo,len,kqcm,percrit,lgchek,me)
2228 !clu [+8l] add stcanl(3:4)
2229  if(lsoil.gt.2) then
2230  call qcmxmn('stc3m ',stcanl(1,3),slianl,snoanl,icefl1,
2231  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
2232  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
2233  & rla,rlo,len,kqcm,percrit,lgchek,me)
2234  call qcmxmn('stc4m ',stcanl(1,4),slianl,snoanl,icefl1,
2235  & stclmx,stclmn,stcomx,stcomn,stcimx,stcimn,
2236  & stcjmx,stcjmn,stcsmx,stcsmn,eptsfc,
2237  & rla,rlo,len,kqcm,percrit,lgchek,me)
2238  endif
2239  call qcmxmn('smc1m ',smcanl(1,1),slianl,snoanl,icefl1,
2240  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
2241  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
2242  & rla,rlo,len,kqcm,percrit,lgchek,me)
2243  call qcmxmn('smc2m ',smcanl(1,2),slianl,snoanl,icefl1,
2244  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
2245  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
2246  & rla,rlo,len,kqcm,percrit,lgchek,me)
2247 !clu [+8l] add smcanl(3:4)
2248  if(lsoil.gt.2) then
2249  call qcmxmn('smc3m ',smcanl(1,3),slianl,snoanl,icefl1,
2250  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
2251  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
2252  & rla,rlo,len,kqcm,percrit,lgchek,me)
2253  call qcmxmn('smc4m ',smcanl(1,4),slianl,snoanl,icefl1,
2254  & smclmx,smclmn,smcomx,smcomn,smcimx,smcimn,
2255  & smcjmx,smcjmn,smcsmx,smcsmn,epssmc,
2256  & rla,rlo,len,kqcm,percrit,lgchek,me)
2257  endif
2258  kqcm=1
2259  call qcmxmn('vegm ',veganl,slianl,snoanl,icefl1,
2260  & veglmx,veglmn,vegomx,vegomn,vegimx,vegimn,
2261  & vegjmx,vegjmn,vegsmx,vegsmn,epsveg,
2262  & rla,rlo,len,kqcm,percrit,lgchek,me)
2263  call qcmxmn('vetm ',vetanl,slianl,snoanl,icefl1,
2264  & vetlmx,vetlmn,vetomx,vetomn,vetimx,vetimn,
2265  & vetjmx,vetjmn,vetsmx,vetsmn,epsvet,
2266  & rla,rlo,len,kqcm,percrit,lgchek,me)
2267  call qcmxmn('sotm ',sotanl,slianl,snoanl,icefl1,
2268  & sotlmx,sotlmn,sotomx,sotomn,sotimx,sotimn,
2269  & sotjmx,sotjmn,sotsmx,sotsmn,epssot,
2270  & rla,rlo,len,kqcm,percrit,lgchek,me)
2271 !cwu [+8l] add sih, sic,
2272  call qcmxmn('sihm ',sihanl,slianl,snoanl,icefl1,
2273  & sihlmx,sihlmn,sihomx,sihomn,sihimx,sihimn,
2274  & sihjmx,sihjmn,sihsmx,sihsmn,epssih,
2275  & rla,rlo,len,kqcm,percrit,lgchek,me)
2276  call qcmxmn('sicm ',sicanl,slianl,snoanl,icefl1,
2277  & siclmx,siclmn,sicomx,sicomn,sicimx,sicimn,
2278  & sicjmx,sicjmn,sicsmx,sicsmn,epssic,
2279  & rla,rlo,len,kqcm,percrit,lgchek,me)
2280 !clu [+16l] add vmn, vmx, slp, abs
2281  call qcmxmn('vmnm ',vmnanl,slianl,snoanl,icefl1,
2282  & vmnlmx,vmnlmn,vmnomx,vmnomn,vmnimx,vmnimn,
2283  & vmnjmx,vmnjmn,vmnsmx,vmnsmn,epsvmn,
2284  & rla,rlo,len,kqcm,percrit,lgchek,me)
2285  call qcmxmn('vmxm ',vmxanl,slianl,snoanl,icefl1,
2286  & vmxlmx,vmxlmn,vmxomx,vmxomn,vmximx,vmximn,
2287  & vmxjmx,vmxjmn,vmxsmx,vmxsmn,epsvmx,
2288  & rla,rlo,len,kqcm,percrit,lgchek,me)
2289  call qcmxmn('slpm ',slpanl,slianl,snoanl,icefl1,
2290  & slplmx,slplmn,slpomx,slpomn,slpimx,slpimn,
2291  & slpjmx,slpjmn,slpsmx,slpsmn,epsslp,
2292  & rla,rlo,len,kqcm,percrit,lgchek,me)
2293  call qcmxmn('absm ',absanl,slianl,snoanl,icefl1,
2294  & abslmx,abslmn,absomx,absomn,absimx,absimn,
2295  & absjmx,absjmn,abssmx,abssmn,epsabs,
2296  & rla,rlo,len,kqcm,percrit,lgchek,me)
2297 
2298 !
2299  if(me .eq. 0) then
2300  write(6,*) '=============='
2301  write(6,*) 'final results'
2302  write(6,*) '=============='
2303  endif
2304 !
2305 ! foreward correction to tg3 and tsf at the last stage
2306 !
2307 ! if(lprnt) print *,' tsfbc=',tsfanl(iprnt)
2308  if (use_ufo) then
2309 !
2310 ! The tiled version of the substrate temperature is properly
2311 ! adjusted to the terrain. Only invoke when using the old
2312 ! global tg3 grib file.
2313 !
2314  if ( index(fntg3c, "tileX.nc") == 0) then ! global file
2315  ztsfc = 1.
2316  call tsfcor(tg3anl,orogd,slmask,ztsfc,len,rlapse)
2317  endif
2318  ztsfc = 0.
2319  call tsfcor(tsfanl,orogd,slmask,ztsfc,len,rlapse)
2320  else
2321  ztsfc = 0.
2322  call tsfcor(tsfanl,orog,slmask,ztsfc,len,rlapse)
2323  endif
2324 ! if(lprnt) print *,' tsfaf=',tsfanl(iprnt)
2325 !
2326 ! check the final merged product
2327 !
2328  if (monmer) then
2329  if(me .eq. 0) then
2330  print *,' '
2331  print *,'monitor of updated surface fields'
2332  print *,' (includes angulation correction)'
2333  print *,' '
2334 ! call count(slianl,snoanl,len)
2335  print *,' '
2336  call monitr('tsfanl',tsfanl,slianl,snoanl,len)
2337  call monitr('albanl',albanl,slianl,snoanl,len)
2338  call monitr('aisanl',aisanl,slianl,snoanl,len)
2339  call monitr('snoanl',snoanl,slianl,snoanl,len)
2340  call monitr('smcanl1',smcanl(1,1),slianl,snoanl,len)
2341  call monitr('smcanl2',smcanl(1,2),slianl,snoanl,len)
2342  call monitr('stcanl1',stcanl(1,1),slianl,snoanl,len)
2343  call monitr('stcanl2',stcanl(1,2),slianl,snoanl,len)
2344 !clu [+4l] add smcanl(3:4) and stcanl(3:4)
2345  if(lsoil.gt.2) then
2346  call monitr('smcanl3',smcanl(1,3),slianl,snoanl,len)
2347  call monitr('smcanl4',smcanl(1,4),slianl,snoanl,len)
2348  call monitr('stcanl3',stcanl(1,3),slianl,snoanl,len)
2349  call monitr('stcanl4',stcanl(1,4),slianl,snoanl,len)
2350  call monitr('tg3anl',tg3anl,slianl,snoanl,len)
2351  call monitr('zoranl',zoranl,slianl,snoanl,len)
2352  endif
2353 ! if (gaus) then
2354  call monitr('cvaanl',cvanl ,slianl,snoanl,len)
2355  call monitr('cvbanl',cvbanl,slianl,snoanl,len)
2356  call monitr('cvtanl',cvtanl,slianl,snoanl,len)
2357 ! endif
2358  call monitr('slianl',slianl,slianl,snoanl,len)
2359 ! call monitr('plranl',plranl,slianl,snoanl,len)
2360  call monitr('orog ',orog ,slianl,snoanl,len)
2361  call monitr('cnpanl',cnpanl,slianl,snoanl,len)
2362  call monitr('veganl',veganl,slianl,snoanl,len)
2363  call monitr('vetanl',vetanl,slianl,snoanl,len)
2364  call monitr('sotanl',sotanl,slianl,snoanl,len)
2365 !cwu [+2l] add sih, sic,
2366  call monitr('sihanl',sihanl,slianl,snoanl,len)
2367  call monitr('sicanl',sicanl,slianl,snoanl,len)
2368 !clu [+4l] add vmn, vmx, slp, abs
2369  call monitr('vmnanl',vmnanl,slianl,snoanl,len)
2370  call monitr('vmxanl',vmxanl,slianl,snoanl,len)
2371  call monitr('slpanl',slpanl,slianl,snoanl,len)
2372  call monitr('absanl',absanl,slianl,snoanl,len)
2373  endif
2374  endif
2375 !
2376  if (mondif) then
2377  do i=1,len
2378  tsffcs(i) = tsfanl(i) - tsffcs(i)
2379  snofcs(i) = snoanl(i) - snofcs(i)
2380  tg3fcs(i) = tg3anl(i) - tg3fcs(i)
2381  zorfcs(i) = zoranl(i) - zorfcs(i)
2382 ! plrfcs(i) = plranl(i) - plrfcs(i)
2383 ! albfcs(i) = albanl(i) - albfcs(i)
2384  slifcs(i) = slianl(i) - slifcs(i)
2385  aisfcs(i) = aisanl(i) - aisfcs(i)
2386  cnpfcs(i) = cnpanl(i) - cnpfcs(i)
2387  vegfcs(i) = veganl(i) - vegfcs(i)
2388  vetfcs(i) = vetanl(i) - vetfcs(i)
2389  sotfcs(i) = sotanl(i) - sotfcs(i)
2390 !clu [+2l] add sih, sic
2391  sihfcs(i) = sihanl(i) - sihfcs(i)
2392  sicfcs(i) = sicanl(i) - sicfcs(i)
2393 !clu [+4l] add vmn, vmx, slp, abs
2394  vmnfcs(i) = vmnanl(i) - vmnfcs(i)
2395  vmxfcs(i) = vmxanl(i) - vmxfcs(i)
2396  slpfcs(i) = slpanl(i) - slpfcs(i)
2397  absfcs(i) = absanl(i) - absfcs(i)
2398  enddo
2399  do j = 1,lsoil
2400  do i = 1,len
2401  smcfcs(i,j) = smcanl(i,j) - smcfcs(i,j)
2402  stcfcs(i,j) = stcanl(i,j) - stcfcs(i,j)
2403  enddo
2404  enddo
2405  do j = 1,4
2406  do i = 1,len
2407  albfcs(i,j) = albanl(i,j) - albfcs(i,j)
2408  enddo
2409  enddo
2410 !
2411 ! monitoring prints
2412 !
2413  if(me .eq. 0) then
2414  print *,' '
2415  print *,'monitor of difference'
2416  print *,' (includes angulation correction)'
2417  print *,' '
2418  call monitr('tsfdif',tsffcs,slianl,snoanl,len)
2419  call monitr('albdif',albfcs,slianl,snoanl,len)
2420  call monitr('albdif1',albfcs,slianl,snoanl,len)
2421  call monitr('albdif2',albfcs(1,2),slianl,snoanl,len)
2422  call monitr('albdif3',albfcs(1,3),slianl,snoanl,len)
2423  call monitr('albdif4',albfcs(1,4),slianl,snoanl,len)
2424  call monitr('aisdif',aisfcs,slianl,snoanl,len)
2425  call monitr('snodif',snofcs,slianl,snoanl,len)
2426  call monitr('smcanl1',smcfcs(1,1),slianl,snoanl,len)
2427  call monitr('smcanl2',smcfcs(1,2),slianl,snoanl,len)
2428  call monitr('stcanl1',stcfcs(1,1),slianl,snoanl,len)
2429  call monitr('stcanl2',stcfcs(1,2),slianl,snoanl,len)
2430 !clu [+4l] add smcfcs(3:4) and stc(3:4)
2431  if(lsoil.gt.2) then
2432  call monitr('smcanl3',smcfcs(1,3),slianl,snoanl,len)
2433  call monitr('smcanl4',smcfcs(1,4),slianl,snoanl,len)
2434  call monitr('stcanl3',stcfcs(1,3),slianl,snoanl,len)
2435  call monitr('stcanl4',stcfcs(1,4),slianl,snoanl,len)
2436  endif
2437  call monitr('tg3dif',tg3fcs,slianl,snoanl,len)
2438  call monitr('zordif',zorfcs,slianl,snoanl,len)
2439 ! if (gaus) then
2440  call monitr('cvadif',cvfcs ,slianl,snoanl,len)
2441  call monitr('cvbdif',cvbfcs,slianl,snoanl,len)
2442  call monitr('cvtdif',cvtfcs,slianl,snoanl,len)
2443 ! endif
2444  call monitr('slidif',slifcs,slianl,snoanl,len)
2445 ! call monitr('plrdif',plrfcs,slianl,snoanl,len)
2446  call monitr('cnpdif',cnpfcs,slianl,snoanl,len)
2447  call monitr('vegdif',vegfcs,slianl,snoanl,len)
2448  call monitr('vetdif',vetfcs,slianl,snoanl,len)
2449  call monitr('sotdif',sotfcs,slianl,snoanl,len)
2450 !cwu [+2l] add sih, sic
2451  call monitr('sihdif',sihfcs,slianl,snoanl,len)
2452  call monitr('sicdif',sicfcs,slianl,snoanl,len)
2453 !clu [+4l] add vmn, vmx, slp, abs
2454  call monitr('vmndif',vmnfcs,slianl,snoanl,len)
2455  call monitr('vmxdif',vmxfcs,slianl,snoanl,len)
2456  call monitr('slpdif',slpfcs,slianl,snoanl,len)
2457  call monitr('absdif',absfcs,slianl,snoanl,len)
2458  endif
2459  endif
2460 !
2461 !
2462  do i=1,len
2463  tsffcs(i) = tsfanl(i)
2464  snofcs(i) = snoanl(i)
2465  tg3fcs(i) = tg3anl(i)
2466  zorfcs(i) = zoranl(i)
2467 ! plrfcs(i) = plranl(i)
2468 ! albfcs(i) = albanl(i)
2469  slifcs(i) = slianl(i)
2470  aisfcs(i) = aisanl(i)
2471  cvfcs(i) = cvanl(i)
2472  cvbfcs(i) = cvbanl(i)
2473  cvtfcs(i) = cvtanl(i)
2474  cnpfcs(i) = cnpanl(i)
2475  vegfcs(i) = veganl(i)
2476  vetfcs(i) = vetanl(i)
2477  sotfcs(i) = sotanl(i)
2478 !clu [+4l] add vmn, vmx, slp, abs
2479  vmnfcs(i) = vmnanl(i)
2480  vmxfcs(i) = vmxanl(i)
2481  slpfcs(i) = slpanl(i)
2482  absfcs(i) = absanl(i)
2483  enddo
2484  do j = 1,lsoil
2485  do i = 1,len
2486  smcfcs(i,j) = smcanl(i,j)
2487  if (slifcs(i) .gt. 0.0) then
2488  stcfcs(i,j) = stcanl(i,j)
2489  else
2490  stcfcs(i,j) = tsffcs(i)
2491  endif
2492  enddo
2493  enddo
2494  do j = 1,4
2495  do i = 1,len
2496  albfcs(i,j) = albanl(i,j)
2497  enddo
2498  enddo
2499  do j = 1,2
2500  do i = 1,len
2501  alffcs(i,j) = alfanl(i,j)
2502  enddo
2503  enddo
2504 
2505 !cwu [+20l] update sihfcs, sicfcs. remove sea ice over non-ice points
2506  crit=aislim
2507  do i=1,len
2508  sihfcs(i) = sihanl(i)
2509  sitfcs(i) = tsffcs(i)
2510  if (slifcs(i).ge.2.) then
2511  if (sicfcs(i).gt.crit) then
2512  tsffcs(i) = (sicanl(i)*tsffcs(i)
2513  & + (sicfcs(i)-sicanl(i))*tgice)/sicfcs(i)
2514  sitfcs(i) = (tsffcs(i)-tgice*(1.0-sicfcs(i))) / sicfcs(i)
2515  else
2516  tsffcs(i) = tsfanl(i)
2517 ! tsffcs(i) = tgice
2518  sihfcs(i) = sihnew
2519  endif
2520  endif
2521  sicfcs(i) = sicanl(i)
2522  enddo
2523  do i=1,len
2524  if (slifcs(i).lt.1.5) then
2525  sihfcs(i) = 0.
2526  sicfcs(i) = 0.
2527  sitfcs(i) = tsffcs(i)
2528  else if ((slifcs(i).ge.1.5).and.(sicfcs(i).lt.crit)) then
2529  print *,'warning: check, slifcs and sicfcs',
2530  & slifcs(i),sicfcs(i)
2531  endif
2532  enddo
2533 
2534 !
2535 ! ensure the consistency between slc and smc
2536 !
2537  do k=1, lsoil
2538  fixratio(k) = .false.
2539  if (fsmcl(k).lt.99999.) fixratio(k) = .true.
2540  enddo
2541 
2542  if(me .eq. 0) then
2543  print *,'dbgx --fixratio:',(fixratio(k),k=1,lsoil)
2544  endif
2545 
2546  do k=1, lsoil
2547  if(fixratio(k)) then
2548  do i = 1, len
2549  if(swratio(i,k) .eq. -999.) then
2550  slcfcs(i,k) = smcfcs(i,k)
2551  else
2552  slcfcs(i,k) = swratio(i,k) * smcfcs(i,k)
2553  endif
2554  if (slifcs(i) .ne. 1.0) slcfcs(i,k) = 1.0 ! flag value for non-land points.
2555  enddo
2556  endif
2557  enddo
2558 ! set liquid soil moisture to a flag value of 1.0
2559  if (landice) then
2560  do i = 1, len
2561  if (slifcs(i) .eq. 1.0 .and.
2562  & nint(vetfcs(i)) == veg_type_landice) then
2563  do k=1, lsoil
2564  slcfcs(i,k) = 1.0
2565  enddo
2566  endif
2567  enddo
2568  end if
2569 !
2570 ! ensure the consistency between snwdph and sheleg
2571 !
2572  if(fsnol .lt. 99999.) then
2573  if(me .eq. 0) then
2574  print *,'dbgx -- scale snwdph from sheleg'
2575  endif
2576  do i = 1, len
2577  if(slifcs(i).eq.1.) swdfcs(i) = 10.* snofcs(i)
2578  enddo
2579  endif
2580 
2581 ! sea ice model only uses the liquid equivalent depth.
2582 ! so update the physical depth only for display purposes.
2583 ! use the same 3:1 ratio used by ice model.
2584 
2585  do i = 1, len
2586  if (slifcs(i).ne.1) swdfcs(i) = 3.*snofcs(i)
2587  enddo
2588 
2589  do i = 1, len
2590  if(slifcs(i).eq.1.) then
2591  if(snofcs(i).ne.0. .and. swdfcs(i).eq.0.) then
2592  print *,'dbgx --scale snwdph from sheleg',
2593  + i, swdfcs(i), snofcs(i)
2594  swdfcs(i) = 10.* snofcs(i)
2595  endif
2596  endif
2597  enddo
2598 ! landice mods - impose same minimum snow depth at
2599 ! landice as noah lsm. also ensure
2600 ! lower thermal boundary condition
2601 ! and skin t is no warmer than freezing
2602 ! after adjustment to terrain.
2603  if (landice) then
2604  do i = 1, len
2605  if (slifcs(i) .eq. 1.0 .and.
2606  & nint(vetfcs(i)) == veg_type_landice) then
2607  snofcs(i) = max(snofcs(i),100.0) ! in mm
2608  swdfcs(i) = max(swdfcs(i),1000.0) ! in mm
2609  tg3fcs(i) = min(tg3fcs(i),273.15)
2610  tsffcs(i) = min(tsffcs(i),273.15)
2611  endif
2612  enddo
2613  end if
2614 !
2615 ! if(lprnt) print *,' tsffcsf=',tsffcs(iprnt)
2616  return
2617  end subroutine sfccycle
2618 
2626  subroutine count(slimsk,sno,ijmax)
2627  use machine , only : kind_io8,kind_io4
2628  implicit none
2629  real (kind=kind_io8) rl3,rl1,rl0,rl2,rl6,rl7,rl4,rl5
2630  integer l8,l7,l1,l2,ijmax,l0,l3,l5,l6,l4,ij
2631 !
2632  real (kind=kind_io8) slimsk(1),sno(1)
2633 !
2634 ! count number of points for the four surface conditions
2635 !
2636  l0 = 0
2637  l1 = 0
2638  l2 = 0
2639  l3 = 0
2640  l4 = 0
2641  do ij=1,ijmax
2642  if(slimsk(ij).eq.0.) l1 = l1 + 1
2643  if(slimsk(ij).eq.1. .and. sno(ij).le.0.) l0 = l0 + 1
2644  if(slimsk(ij).eq.2. .and. sno(ij).le.0.) l2 = l2 + 1
2645  if(slimsk(ij).eq.1. .and. sno(ij).gt.0.) l3 = l3 + 1
2646  if(slimsk(ij).eq.2. .and. sno(ij).gt.0.) l4 = l4 + 1
2647  enddo
2648  l5 = l0 + l3
2649  l6 = l2 + l4
2650  l7 = l1 + l6
2651  l8 = l1 + l5 + l6
2652  rl0 = float(l0) / float(l8)*100.
2653  rl3 = float(l3) / float(l8)*100.
2654  rl1 = float(l1) / float(l8)*100.
2655  rl2 = float(l2) / float(l8)*100.
2656  rl4 = float(l4) / float(l8)*100.
2657  rl5 = float(l5) / float(l8)*100.
2658  rl6 = float(l6) / float(l8)*100.
2659  rl7 = float(l7) / float(l8)*100.
2660  print *,'1) no. of not snow-covered land points ',l0,' ',rl0,' '
2661  print *,'2) no. of snow covered land points ',l3,' ',rl3,' '
2662  print *,'3) no. of open sea points ',l1,' ',rl1,' '
2663  print *,'4) no. of not snow-covered seaice points ',l2,' ',rl2,' '
2664  print *,'5) no. of snow covered sea ice points ',l4,' ',rl4,' '
2665  print *,' '
2666  print *,'6) no. of land points ',l5,' ',rl5,' '
2667  print *,'7) no. sea points (including sea ice) ',l7,' ',rl7,' '
2668  print *,' (no. of sea ice points) (',l6,')',' ',rl6,' '
2669  print *,' '
2670  print *,'9) no. of total grid points ',l8
2671 ! print *,' '
2672 ! print *,' '
2673 
2674 !
2675 ! if(lprnt) print *,' tsffcsf=',tsffcs(iprnt)
2676  return
2677  end
2678 
2689  subroutine monitr(lfld,fld,slimsk,sno,ijmax)
2690  use machine , only : kind_io8,kind_io4
2691  implicit none
2692  integer ij,n,ijmax
2693 !
2694  real (kind=kind_io8) fld(ijmax), slimsk(ijmax),sno(ijmax)
2695 !
2696  real (kind=kind_io8) rmax(5),rmin(5)
2697  character(len=*) lfld
2698 !
2699 ! find max/min
2700 !
2701  do n=1,5
2702  rmax(n) = -9.e20
2703  rmin(n) = 9.e20
2704  enddo
2705 !
2706  do ij=1,ijmax
2707  if(slimsk(ij).eq.0.) then
2708  rmax(1) = max(rmax(1), fld(ij))
2709  rmin(1) = min(rmin(1), fld(ij))
2710  elseif(slimsk(ij).eq.1.) then
2711  if(sno(ij).le.0.) then
2712  rmax(2) = max(rmax(2), fld(ij))
2713  rmin(2) = min(rmin(2), fld(ij))
2714  else
2715  rmax(4) = max(rmax(4), fld(ij))
2716  rmin(4) = min(rmin(4), fld(ij))
2717  endif
2718  else
2719  if(sno(ij).le.0.) then
2720  rmax(3) = max(rmax(3), fld(ij))
2721  rmin(3) = min(rmin(3), fld(ij))
2722  else
2723  rmax(5) = max(rmax(5), fld(ij))
2724  rmin(5) = min(rmin(5), fld(ij))
2725  endif
2726  endif
2727  enddo
2728 !
2729  print 100,lfld
2730  print 101,rmax(1),rmin(1)
2731  print 102,rmax(2),rmin(2), rmax(4), rmin(4)
2732  print 103,rmax(3),rmin(3), rmax(5), rmin(5)
2733 !
2734 ! print 102,rmax(2),rmin(2)
2735 ! print 103,rmax(3),rmin(3)
2736 ! print 104,rmax(4),rmin(4)
2737 ! print 105,rmax(5),rmin(5)
2738  100 format('0 *** ',a8,' ***')
2739  101 format(' open sea ......... max=',e12.4,' min=',e12.4)
2740  102 format(' land nosnow/snow .. max=',e12.4,' min=',e12.4
2741  &, ' max=',e12.4,' min=',e12.4)
2742  103 format(' seaice nosnow/snow max=',e12.4,' min=',e12.4
2743  &, ' max=',e12.4,' min=',e12.4)
2744 !
2745 ! 100 format('0',2x,'*** ',a8,' ***')
2746 ! 102 format(2x,' land without snow ..... max=',e12.4,' min=',e12.4)
2747 ! 103 format(2x,' seaice without snow ... max=',e12.4,' min=',e12.4)
2748 ! 104 format(2x,' land with snow ........ max=',e12.4,' min=',e12.4)
2749 ! 105 format(2x,' sea ice with snow ..... max=',e12.4,' min=',e12.4)
2750 !
2751  return
2752  end
2753 
2761  subroutine dayoyr(iyr,imo,idy,ldy)
2762  implicit none
2763  integer ldy,i,idy,iyr,imo
2764 !
2765 ! this routine figures out the day of the year given imo and idy
2766 !
2767  integer month(13)
2768  data month/0,31,28,31,30,31,30,31,31,30,31,30,31/
2769  if(mod(iyr,4).eq.0) month(3) = 29
2770  ldy = idy
2771  do i = 1, imo
2772  ldy = ldy + month(i)
2773  enddo
2774  return
2775  end
2776 
2792  subroutine hmskrd(lugb,imsk,jmsk,fnmskh,
2793  & kpds5,slmskh,gausm,blnmsk,bltmsk,me)
2794  use machine , only : kind_io8,kind_io4
2795  use sfccyc_module, only : mdata, xdata, ydata
2796  implicit none
2797  integer kpds5,me,i,imsk,jmsk,lugb
2798 !
2799  character*500 fnmskh
2800 !
2801  real (kind=kind_io8) slmskh(mdata)
2802  logical gausm
2803  real (kind=kind_io8) blnmsk,bltmsk
2804 !
2805  imsk = xdata
2806  jmsk = ydata
2807 
2808  if (me .eq. 0) then
2809  write(6,*)' imsk=',imsk,' jmsk=',jmsk,' xdata=',xdata,' ydata='
2810  &, ydata
2811  endif
2812 
2813  call fixrdg(lugb,imsk,jmsk,fnmskh,
2814  & kpds5,slmskh,gausm,blnmsk,bltmsk,me)
2815 
2816 ! print *,'in sfc_sub, aft fixrdg,slmskh=',maxval(slmskh),
2817 ! & minval(slmskh),'mdata=',mdata,'imsk*jmsk=',imsk*jmsk
2818 
2819  do i=1,imsk*jmsk
2820  slmskh(i) = nint(slmskh(i))
2821  enddo
2822 !
2823  return
2824  end
2825 
2840  subroutine fixrdg(lugb,idim,jdim,fngrib,
2841  & kpds5,gdata,gaus,blno,blto,me)
2842  use machine , only : kind_io8,kind_io4
2843  use sfccyc_module, only : mdata
2844  implicit none
2845  integer lgrib,n,lskip,jret,j,ndata,lugi,jdim,idim,lugb,
2846  & iret, me,kpds5,kdata,i,w3kindreal,w3kindint
2847 !
2848  character*(*) fngrib
2849 !
2850  real (kind=kind_io8) gdata(idim*jdim)
2851  logical gaus
2852  real (kind=kind_io8) blno,blto
2853  real (kind=kind_io8), allocatable :: data8(:)
2854  real (kind=kind_io4), allocatable :: data4(:)
2855 !
2856  logical*1, allocatable :: lbms(:)
2857 !
2858  integer kpds(200),kgds(200)
2859  integer jpds(200),jgds(200), kpds0(200)
2860 !
2861  allocate(data8(1:idim*jdim))
2862  allocate(lbms(1:mdata))
2863  kpds = 0
2864  kgds = 0
2865  jpds = 0
2866  jgds = 0
2867  kpds0 = 0
2868 !
2869 ! if(me .eq. 0) then
2870 ! write(6,*) ' '
2871 ! write(6,*) '************************************************'
2872 ! endif
2873 !
2874  close(lugb)
2875  call baopenr(lugb,fngrib,iret)
2876  if (iret .ne. 0) then
2877  write(6,*) ' FATAL ERROR: in opening file ',trim(fngrib)
2878  print *,'FATAL ERROR: in opening file ',trim(fngrib)
2879  call abort
2880  endif
2881  if (me .eq. 0) write(6,*) ' file ',trim(fngrib),
2882  & ' opened. unit=',lugb
2883  lugi = 0
2884  lskip = -1
2885  n = 0
2886  jpds = -1
2887  jgds = -1
2888  jpds(5) = kpds5
2889  kpds = jpds
2890 !
2891  call getgbh(lugb,lugi,lskip,jpds,jgds,lgrib,ndata,
2892  & lskip,kpds,kgds,iret)
2893 !
2894  if(me .eq. 0) then
2895  write(6,*) ' first grib record.'
2896  write(6,*) ' kpds( 1-10)=',(kpds(j),j= 1,10)
2897  write(6,*) ' kpds(11-20)=',(kpds(j),j=11,20)
2898  write(6,*) ' kpds(21- )=',(kpds(j),j=21,22)
2899  endif
2900 !
2901  kpds0=jpds
2902  kpds0(4)=-1
2903  kpds0(18)=-1
2904  if(iret.ne.0) then
2905  write(6,*) ' FATAL ERROR: in getgbh. iret: ', iret
2906  if (iret == 99) write(6,*) ' field not found.'
2907  call abort
2908  endif
2909 !
2910  jpds = kpds0
2911  lskip = -1
2912  kdata=idim*jdim
2913  call w3kind(w3kindreal,w3kindint)
2914  if (w3kindreal == 8) then
2915  call getgb(lugb,lugi,kdata,lskip,jpds,jgds,ndata,lskip,
2916  & kpds,kgds,lbms,data8,jret)
2917  else if (w3kindreal == 4) then
2918  allocate(data4(1:idim*jdim))
2919  call getgb(lugb,lugi,kdata,lskip,jpds,jgds,ndata,lskip,
2920  & kpds,kgds,lbms,data4,jret)
2921  data8 = real(data4, kind=kind_io8)
2922  deallocate(data4)
2923  else
2924  write(0,*)' FATAL ERROR: Invalid w3kindreal '
2925  call abort
2926  endif
2927 !
2928  if(jret == 0) then
2929  if(ndata.eq.0) then
2930  write(6,*) ' FATAL ERROR: in getgb'
2931  write(6,*) ' kpds=',kpds
2932  write(6,*) ' kgds=',kgds
2933  call abort
2934  endif
2935  idim = kgds(2)
2936  jdim = kgds(3)
2937  gaus = kgds(1).eq.4
2938  blno = kgds(5)*1.d-3
2939  blto = kgds(4)*1.d-3
2940  gdata(1:idim*jdim) = data8(1:idim*jdim)
2941  if (me == 0) write(6,*) 'idim,jdim=',idim,jdim
2942  &, ' gaus=',gaus,' blno=',blno,' blto=',blto
2943  else
2944  if (me ==. 0) write(6,*) 'idim,jdim=',idim,jdim
2945  &, ' gaus=',gaus,' blno=',blno,' blto=',blto
2946  write(6,*) ' FATAL ERROR in getgb : jret=',jret
2947  write(6,*) ' kpds(13)=',kpds(13),' kpds(15)=',kpds(15)
2948  call abort
2949  endif
2950 !
2951  deallocate(data8)
2952  deallocate(lbms)
2953  return
2954  end
2955 
2970  subroutine getarea(kgds,dlat,dlon,rslat,rnlat,wlon,elon,ijordr
2971  &, me)
2972  use machine , only : kind_io8,kind_io4
2973  implicit none
2974  integer j,me,kgds11
2975  real (kind=kind_io8) f0lon,f0lat,elon,dlon,dlat,rslat,wlon,rnlat
2976 !
2977 ! get area of the grib record
2978 !
2979  integer kgds(22)
2980  logical ijordr
2981 !
2982  if (me .eq. 0) then
2983  write(6,*) ' kgds( 1-12)=',(kgds(j),j= 1,12)
2984  write(6,*) ' kgds(13-22)=',(kgds(j),j=13,22)
2985  endif
2986 !
2987  if(kgds(1).eq.0) then ! lat/lon grid
2988 !
2989  if (me .eq. 0) write(6,*) 'lat/lon grid'
2990  dlat = float(kgds(10)) * 0.001
2991  dlon = float(kgds( 9)) * 0.001
2992  f0lon = float(kgds(5)) * 0.001
2993  f0lat = float(kgds(4)) * 0.001
2994  kgds11 = kgds(11)
2995  if(kgds11.ge.128) then
2996  wlon = f0lon - dlon*(kgds(2)-1)
2997  elon = f0lon
2998  if(dlon*kgds(2).gt.359.99) then
2999  wlon =f0lon - dlon*kgds(2)
3000  endif
3001  dlon = -dlon
3002  kgds11 = kgds11 - 128
3003  else
3004  wlon = f0lon
3005  elon = f0lon + dlon*(kgds(2)-1)
3006  if(dlon*kgds(2).gt.359.99) then
3007  elon = f0lon + dlon*kgds(2)
3008  endif
3009  endif
3010  if(kgds11.ge.64) then
3011  rnlat = f0lat + dlat*(kgds(3)-1)
3012  rslat = f0lat
3013  kgds11 = kgds11 - 64
3014  else
3015  rnlat = f0lat
3016  rslat = f0lat - dlat*(kgds(3)-1)
3017  dlat = -dlat
3018  endif
3019  if(kgds11.ge.32) then
3020  ijordr = .false.
3021  else
3022  ijordr = .true.
3023  endif
3024 
3025  if(wlon.gt.180.) wlon = wlon - 360.
3026  if(elon.gt.180.) elon = elon - 360.
3027  wlon = nint(wlon*1000.) * 0.001
3028  elon = nint(elon*1000.) * 0.001
3029  rslat = nint(rslat*1000.) * 0.001
3030  rnlat = nint(rnlat*1000.) * 0.001
3031  return
3032 !
3033  elseif(kgds(1).eq.1) then ! mercator projection
3034  write(6,*) 'FATAL ERROR: cannot process mercator grid.'
3035  call abort
3036 !
3037  elseif(kgds(1).eq.2) then ! gnomonic projection
3038  write(6,*) 'FATAL ERROR: cannot process gnomonic grid.'
3039  call abort
3040 !
3041  elseif(kgds(1).eq.3) then ! lambert conformal
3042  write(6,*) 'FATAL ERROR: cannot process lambert conf grid.'
3043  call abort
3044  elseif(kgds(1).eq.4) then ! gaussian grid
3045 !
3046  if (me .eq. 0) write(6,*) 'gaussian grid'
3047  dlat = 99.
3048  dlon = float(kgds( 9)) / 1000.0
3049  f0lon = float(kgds(5)) / 1000.0
3050  f0lat = 99.
3051  kgds11 = kgds(11)
3052  if(kgds11.ge.128) then
3053  wlon = f0lon
3054  elon = f0lon
3055  if(dlon*kgds(2).gt.359.99) then
3056  wlon = f0lon - dlon*kgds(2)
3057  endif
3058  dlon = -dlon
3059  kgds11 = kgds11-128
3060  else
3061  wlon = f0lon
3062  elon = f0lon + dlon*(kgds(2)-1)
3063  if(dlon*kgds(2).gt.359.99) then
3064  elon = f0lon + dlon*kgds(2)
3065  endif
3066  endif
3067  if(kgds11.ge.64) then
3068  rnlat = 99.
3069  rslat = 99.
3070  kgds11 = kgds11 - 64
3071  else
3072  rnlat = 99.
3073  rslat = 99.
3074  dlat = -99.
3075  endif
3076  if(kgds11.ge.32) then
3077  ijordr = .false.
3078  else
3079  ijordr = .true.
3080  endif
3081  return
3082 !
3083  elseif(kgds(1).eq.5) then ! polar strereographic
3084  write(6,*) 'FATAL ERROR: cannot process'
3085  write(6,*) 'polar stereographic grid.'
3086  call abort
3087  return
3088 !
3089  elseif(kgds(1).eq.13) then ! oblique lambert conformal
3090  write(6,*) 'FATAL ERROR: cannot process'
3091  write(6,*) 'oblique lambert conformal grid.'
3092  call abort
3093 !
3094  elseif(kgds(1).eq.50) then ! spherical coefficient
3095  write(6,*) 'FATAL ERROR: cannot process'
3096  write(6,*) 'spherical coefficient grid.'
3097  call abort
3098  return
3099 !
3100  elseif(kgds(1).eq.90) then ! space view perspective
3101 ! (orthographic grid)
3102  write(6,*) 'FATAL ERROR: cannot process'
3103  write(6,*) 'space view perspective grid.'
3104  call abort
3105  return
3106 !
3107  else ! unknown projection. abort.
3108  write(6,*) 'FATAL ERROR: unknown map projection'
3109  write(6,*) 'kgds(1)=',kgds(1)
3110  print *,'FATAL ERROR: unknown map projection'
3111  print *,'kgds(1)=',kgds(1)
3112  call abort
3113  endif
3114 !
3115  return
3116  end
3117 
3130  subroutine subst(data,imax,jmax,dlon,dlat,ijordr)
3131  use machine , only : kind_io8,kind_io4
3132  implicit none
3133  integer i,j,ii,jj,jmax,imax,iret
3134  real (kind=kind_io8) dlat,dlon
3135 !
3136  logical ijordr
3137 !
3138  real (kind=kind_io8) data(imax,jmax)
3139  real (kind=kind_io8), allocatable :: work(:,:)
3140 !
3141  if(.not.ijordr.or.
3142  & (ijordr.and.(dlat.gt.0..or.dlon.lt.0.))) then
3143  allocate (work(imax,jmax))
3144 
3145  if(.not.ijordr) then
3146  do j=1,jmax
3147  do i=1,imax
3148  work(i,j) = data(j,i)
3149  enddo
3150  enddo
3151  else
3152  do j=1,jmax
3153  do i=1,imax
3154  work(i,j) = data(i,j)
3155  enddo
3156  enddo
3157  endif
3158  if (dlat > 0.0) then
3159  if (dlon > 0.0) then
3160  do j=1,jmax
3161  jj = jmax - j + 1
3162  do i=1,imax
3163  data(i,jj) = work(i,j)
3164  enddo
3165  enddo
3166  else
3167  do i=1,imax
3168  data(imax-i+1,jj) = work(i,j)
3169  enddo
3170  endif
3171  else
3172  if (dlon > 0.0) then
3173  do j=1,jmax
3174  do i=1,imax
3175  data(i,j) = work(i,j)
3176  enddo
3177  enddo
3178  else
3179  do j=1,jmax
3180  do i=1,imax
3181  data(imax-i+1,j) = work(i,j)
3182  enddo
3183  enddo
3184  endif
3185  endif
3186  deallocate (work, stat=iret)
3187  endif
3188  return
3189  end
3190 
3210  subroutine la2ga(regin,imxin,jmxin,rinlon,rinlat,rlon,rlat,inttyp,
3211  & gauout,len,lmask,rslmsk,slmask
3212  &, outlat, outlon,me)
3213  use machine , only : kind_io8,kind_io4
3214  implicit none
3215  real (kind=kind_io8) wei4,wei3,wei2,sum2,sum1,sum3,wei1,sum4,
3216  & wsum,tem,wsumiv,sums,sumn,wi2j2,x,y,wi1j1,
3217  & wi1j2,wi2j1,rlat,rlon,aphi,
3218  & rnume,alamd,denom
3219  integer jy,ifills,ix,len,inttyp,me,i,j,jmxin,imxin,jq,jx,j1,j2,
3220  & ii,i1,i2,kmami,it
3221  integer nx,kxs,kxt
3222  integer, allocatable, save :: imxnx(:)
3223  integer, allocatable :: ifill(:)
3224 !
3225 ! interpolation from lat/lon or gaussian grid to other lat/lon grid
3226 !
3227  real (kind=kind_io8) outlon(len),outlat(len),gauout(len),
3228  & slmask(len)
3229  real (kind=kind_io8) regin (imxin,jmxin),rslmsk(imxin,jmxin)
3230 !
3231  real (kind=kind_io8) rinlat(jmxin), rinlon(imxin)
3232  integer iindx1(len), iindx2(len)
3233  integer jindx1(len), jindx2(len)
3234  real (kind=kind_io8) ddx(len), ddy(len), wrk(len)
3235 !
3236  logical lmask
3237 !
3238  logical first
3239  integer num_threads
3240  data first /.true./
3241  save num_threads, first
3242 !
3243  integer len_thread_m, len_thread, i1_t, i2_t
3244  integer num_parthds
3245 !
3246  if (first) then
3247  num_threads = num_parthds()
3248  first = .false.
3249  if (.not. allocated(imxnx)) allocate (imxnx(num_threads))
3250  endif
3251 !
3252 ! if (me == 0) print *,' num_threads =',num_threads,' me=',me
3253 !
3254 ! if(me .eq. 0) then
3255 ! print *,'rlon=',rlon,' me=',me
3256 ! print *,'rlat=',rlat,' me=',me,' imxin=',imxin,' jmxin=',jmxin
3257 ! endif
3258 !
3259 ! do j=1,jmxin
3260 ! if(rlat.gt.0.) then
3261 ! rinlat(j) = rlat - float(j-1)*dlain
3262 ! else
3263 ! rinlat(j) = rlat + float(j-1)*dlain
3264 ! endif
3265 ! enddo
3266 !
3267 ! if (me .eq. 0) then
3268 ! print *,'rinlat='
3269 ! print *,(rinlat(j),j=1,jmxin)
3270 ! print *,'rinlon='
3271 ! print *,(rinlon(i),i=1,imxin)
3272 !
3273 ! print *,'outlat='
3274 ! print *,(outlat(j),j=1,len)
3275 ! print *,(outlon(j),j=1,len)
3276 ! endif
3277 !
3278 ! do i=1,imxin
3279 ! rinlon(i) = rlon + float(i-1)*dloin
3280 ! enddo
3281 !
3282 ! print *,'rinlon='
3283 ! print *,(rinlon(i),i=1,imxin)
3284 !
3285  len_thread_m = (len+num_threads-1) / num_threads
3286 
3287  if (inttyp /=1) allocate (ifill(num_threads))
3288 !
3289 !$omp parallel do default(none)
3290 !$omp+private(i1_t,i2_t,len_thread,it,i,ii,i1,i2)
3291 !$omp+private(j,j1,j2,jq,ix,jy,nx,kxs,kxt,kmami)
3292 !$omp+private(alamd,denom,rnume,aphi,x,y,wsum,wsumiv,sum1,sum2)
3293 !$omp+private(sum3,sum4,wi1j1,wi2j1,wi1j2,wi2j2,wei1,wei2,wei3,wei4)
3294 !$omp+private(sumn,sums)
3295 !$omp+shared(imxin,jmxin,ifill)
3296 !$omp+shared(outlon,outlat,wrk,iindx1,rinlon,jindx1,rinlat,ddx,ddy)
3297 !$omp+shared(rlon,rlat,regin,gauout,imxnx)
3298 !$omp+private(tem)
3299 !$omp+shared(num_threads,len_thread_m,len,lmask,iindx2,jindx2,rslmsk)
3300 !$omp+shared(inttyp,me,slmask)
3301 !
3302  do it=1,num_threads ! start of threaded loop ...................
3303  i1_t = (it-1)*len_thread_m+1
3304  i2_t = min(i1_t+len_thread_m-1,len)
3305  len_thread = i2_t-i1_t+1
3306 !
3307 ! find i-index for interpolation
3308 !
3309  do i=i1_t, i2_t
3310  alamd = outlon(i)
3311  if (alamd .lt. rlon) alamd = alamd + 360.0
3312  if (alamd .gt. 360.0+rlon) alamd = alamd - 360.0
3313  wrk(i) = alamd
3314  iindx1(i) = imxin
3315  enddo
3316  do i=i1_t,i2_t
3317  do ii=1,imxin
3318  if(wrk(i) .ge. rinlon(ii)) iindx1(i) = ii
3319  enddo
3320  enddo
3321  do i=i1_t,i2_t
3322  i1 = iindx1(i)
3323  if (i1 .lt. 1) i1 = imxin
3324  i2 = i1 + 1
3325  if (i2 .gt. imxin) i2 = 1
3326  iindx1(i) = i1
3327  iindx2(i) = i2
3328  denom = rinlon(i2) - rinlon(i1)
3329  if(denom.lt.0.) denom = denom + 360.
3330  rnume = wrk(i) - rinlon(i1)
3331  if(rnume.lt.0.) rnume = rnume + 360.
3332  ddx(i) = rnume / denom
3333  enddo
3334 !
3335 ! find j-index for interplation
3336 !
3337  if(rlat.gt.0.) then
3338  do j=i1_t,i2_t
3339  jindx1(j)=0
3340  enddo
3341  do jx=1,jmxin
3342  do j=i1_t,i2_t
3343  if(outlat(j).le.rinlat(jx)) jindx1(j) = jx
3344  enddo
3345  enddo
3346  do j=i1_t,i2_t
3347  jq = jindx1(j)
3348  aphi=outlat(j)
3349  if(jq.ge.1 .and. jq .lt. jmxin) then
3350  j2=jq+1
3351  j1=jq
3352  ddy(j)=(aphi-rinlat(j1))/(rinlat(j2)-rinlat(j1))
3353  elseif (jq .eq. 0) then
3354  j2=1
3355  j1=1
3356  if(abs(90.-rinlat(j1)).gt.0.001) then
3357  ddy(j)=(aphi-rinlat(j1))/(90.-rinlat(j1))
3358  else
3359  ddy(j)=0.0
3360  endif
3361  else
3362  j2=jmxin
3363  j1=jmxin
3364  if(abs(-90.-rinlat(j1)).gt.0.001) then
3365  ddy(j)=(aphi-rinlat(j1))/(-90.-rinlat(j1))
3366  else
3367  ddy(j)=0.0
3368  endif
3369  endif
3370  jindx1(j)=j1
3371  jindx2(j)=j2
3372  enddo
3373  else
3374  do j=i1_t,i2_t
3375  jindx1(j) = jmxin+1
3376  enddo
3377  do jx=jmxin,1,-1
3378  do j=i1_t,i2_t
3379  if(outlat(j).le.rinlat(jx)) jindx1(j) = jx
3380  enddo
3381  enddo
3382  do j=i1_t,i2_t
3383  jq = jindx1(j)
3384  aphi=outlat(j)
3385  if(jq.gt.1 .and. jq .le. jmxin) then
3386  j2=jq
3387  j1=jq-1
3388  ddy(j)=(aphi-rinlat(j1))/(rinlat(j2)-rinlat(j1))
3389  elseif (jq .eq. 1) then
3390  j2=1
3391  j1=1
3392  if(abs(-90.-rinlat(j1)).gt.0.001) then
3393  ddy(j)=(aphi-rinlat(j1))/(-90.-rinlat(j1))
3394  else
3395  ddy(j)=0.0
3396  endif
3397  else
3398  j2=jmxin
3399  j1=jmxin
3400  if(abs(90.-rinlat(j1)).gt.0.001) then
3401  ddy(j)=(aphi-rinlat(j1))/(90.-rinlat(j1))
3402  else
3403  ddy(j)=0.0
3404  endif
3405  endif
3406  jindx1(j)=j1
3407  jindx2(j)=j2
3408  enddo
3409  endif
3410 !
3411 ! if (me .eq. 0 .and. inttyp .eq. 1) then
3412 ! print *,'la2ga'
3413 ! print *,'iindx1'
3414 ! print *,(iindx1(n),n=1,len)
3415 ! print *,'iindx2'
3416 ! print *,(iindx2(n),n=1,len)
3417 ! print *,'jindx1'
3418 ! print *,(jindx1(n),n=1,len)
3419 ! print *,'jindx2'
3420 ! print *,(jindx2(n),n=1,len)
3421 ! print *,'ddy'
3422 ! print *,(ddy(n),n=1,len)
3423 ! print *,'ddx'
3424 ! print *,(ddx(n),n=1,len)
3425 ! endif
3426 !
3427  sum1 = 0.
3428  sum2 = 0.
3429  sum3 = 0.
3430  sum4 = 0.
3431  if (lmask) then
3432  wei1 = 0.
3433  wei2 = 0.
3434  wei3 = 0.
3435  wei4 = 0.
3436  do i=1,imxin
3437  sum1 = sum1 + regin(i,1) * rslmsk(i,1)
3438  sum2 = sum2 + regin(i,jmxin) * rslmsk(i,jmxin)
3439  wei1 = wei1 + rslmsk(i,1)
3440  wei2 = wei2 + rslmsk(i,jmxin)
3441 !
3442  sum3 = sum3 + regin(i,1) * (1.0-rslmsk(i,1))
3443  sum4 = sum4 + regin(i,jmxin) * (1.0-rslmsk(i,jmxin))
3444  wei3 = wei3 + (1.0-rslmsk(i,1))
3445  wei4 = wei4 + (1.0-rslmsk(i,jmxin))
3446  enddo
3447 !
3448  if(wei1.gt.0.) then
3449  sum1 = sum1 / wei1
3450  else
3451  sum1 = 0.
3452  endif
3453  if(wei2.gt.0.) then
3454  sum2 = sum2 / wei2
3455  else
3456  sum2 = 0.
3457  endif
3458  if(wei3.gt.0.) then
3459  sum3 = sum3 / wei3
3460  else
3461  sum3 = 0.
3462  endif
3463  if(wei4.gt.0.) then
3464  sum4 = sum4 / wei4
3465  else
3466  sum4 = 0.
3467  endif
3468  else
3469  do i=1,imxin
3470  sum1 = sum1 + regin(i,1)
3471  sum2 = sum2 + regin(i,jmxin)
3472  enddo
3473  sum1 = sum1 / imxin
3474  sum2 = sum2 / imxin
3475  sum3 = sum1
3476  sum4 = sum2
3477  endif
3478 !
3479 ! print *,' sum1=',sum1,' sum2=',sum2
3480 ! *,' sum3=',sum3,' sum4=',sum4
3481 ! print *,' rslmsk=',(rslmsk(i,1),i=1,imxin)
3482 ! print *,' slmask=',(slmask(i),i=1,imxout)
3483 ! *,' j1=',jindx1(1),' j2=',jindx2(1)
3484 !
3485 !
3486 ! inttyp=1 take the closest point value
3487 !
3488  if(inttyp.eq.1) then
3489 
3490  do i=i1_t,i2_t
3491  jy = jindx1(i)
3492  if(ddy(i) .ge. 0.5) jy = jindx2(i)
3493  ix = iindx1(i)
3494  if(ddx(i) .ge. 0.5) ix = iindx2(i)
3495 !
3496 !cggg start
3497 !
3498  if (.not. lmask) then
3499 
3500  gauout(i) = regin(ix,jy)
3501 
3502  else
3503 
3504  if(slmask(i).eq.rslmsk(ix,jy)) then
3505 
3506  gauout(i) = regin(ix,jy)
3507 
3508  else
3509 
3510  i1 = ix
3511  j1 = jy
3512 
3513 ! spiral around until matching mask is found.
3514  do nx=1,jmxin*imxin/2
3515  kxs=sqrt(4*nx-2.5)
3516  kxt=nx-int(kxs**2/4+1)
3517  select case(mod(kxs,4))
3518  case(1)
3519  ix=i1-kxs/4+kxt
3520  jx=j1-kxs/4
3521  case(2)
3522  ix=i1+1+kxs/4
3523  jx=j1-kxs/4+kxt
3524  case(3)
3525  ix=i1+1+kxs/4-kxt
3526  jx=j1+1+kxs/4
3527  case default
3528  ix=i1-kxs/4
3529  jx=j1+kxs/4-kxt
3530  end select
3531  if(jx.lt.1) then
3532  ix=ix+imxin/2
3533  jx=2-jx
3534  elseif(jx.gt.jmxin) then
3535  ix=ix+imxin/2
3536  jx=2*jmxin-jx
3537  endif
3538  ix=modulo(ix-1,imxin)+1
3539  if(slmask(i).eq.rslmsk(ix,jx)) then
3540  gauout(i) = regin(ix,jx)
3541  go to 81
3542  endif
3543  enddo
3544 
3545 !cggg here, set the gauout value to be 0, and let's sarah's land
3546 !cggg routine assign a default.
3547 
3548  if (num_threads == 1) then
3549  print*,'no matching mask found ',i,i1,j1,ix,jx
3550  print*,'set to default value.'
3551  endif
3552  gauout(i) = 0.0
3553 
3554 
3555  81 continue
3556 
3557  end if
3558 
3559  end if
3560 
3561 !cggg end
3562 
3563  enddo
3564 ! kmami=1
3565 ! if (me == 0 .and. num_threads == 1)
3566 ! & call maxmin(gauout(i1_t),len_thread,kmami)
3567  else ! nearest neighbor interpolation
3568 
3569 !
3570 ! quasi-bilinear interpolation
3571 !
3572  ifill(it) = 0
3573  imxnx(it) = 0
3574  do i=i1_t,i2_t
3575  y = ddy(i)
3576  j1 = jindx1(i)
3577  j2 = jindx2(i)
3578  x = ddx(i)
3579  i1 = iindx1(i)
3580  i2 = iindx2(i)
3581 !
3582  wi1j1 = (1.-x) * (1.-y)
3583  wi2j1 = x *( 1.-y)
3584  wi1j2 = (1.-x) * y
3585  wi2j2 = x * y
3586 !
3587  tem = 4.*slmask(i) - rslmsk(i1,j1) - rslmsk(i2,j1)
3588  & - rslmsk(i1,j2) - rslmsk(i2,j2)
3589  if(lmask .and. abs(tem) .gt. 0.01) then
3590  if(slmask(i).eq.1.) then
3591  wi1j1 = wi1j1 * rslmsk(i1,j1)
3592  wi2j1 = wi2j1 * rslmsk(i2,j1)
3593  wi1j2 = wi1j2 * rslmsk(i1,j2)
3594  wi2j2 = wi2j2 * rslmsk(i2,j2)
3595  else
3596  wi1j1 = wi1j1 * (1.0-rslmsk(i1,j1))
3597  wi2j1 = wi2j1 * (1.0-rslmsk(i2,j1))
3598  wi1j2 = wi1j2 * (1.0-rslmsk(i1,j2))
3599  wi2j2 = wi2j2 * (1.0-rslmsk(i2,j2))
3600  endif
3601  endif
3602 !
3603  wsum = wi1j1 + wi2j1 + wi1j2 + wi2j2
3604  wrk(i) = wsum
3605  if(wsum.ne.0.) then
3606  wsumiv = 1./wsum
3607 !
3608  if(j1.ne.j2) then
3609  gauout(i) = (wi1j1*regin(i1,j1) + wi2j1*regin(i2,j1) +
3610  & wi1j2*regin(i1,j2) + wi2j2*regin(i2,j2))
3611  & *wsumiv
3612  else
3613 !
3614  if (rlat .gt. 0.0) then
3615  if (slmask(i) .eq. 1.0) then
3616  sumn = sum1
3617  sums = sum2
3618  else
3619  sumn = sum3
3620  sums = sum4
3621  endif
3622  if( j1 .eq. 1) then
3623  gauout(i) = (wi1j1*sumn +wi2j1*sumn +
3624  & wi1j2*regin(i1,j2)+wi2j2*regin(i2,j2))
3625  & * wsumiv
3626  elseif (j1 .eq. jmxin) then
3627  gauout(i) = (wi1j1*regin(i1,j1)+wi2j1*regin(i2,j1)+
3628  & wi1j2*sums +wi2j2*sums )
3629  & * wsumiv
3630  endif
3631 ! print *,' slmask=',slmask(i),' sums=',sums,' sumn=',sumn
3632 ! & ,' regin=',regin(i1,j2),regin(i2,j2),' j1=',j1,' j2=',j2
3633 ! & ,' wij=',wi1j1, wi2j1, wi1j2, wi2j2,wsumiv
3634  else
3635  if (slmask(i) .eq. 1.0) then
3636  sums = sum1
3637  sumn = sum2
3638  else
3639  sums = sum3
3640  sumn = sum4
3641  endif
3642  if( j1 .eq. 1) then
3643  gauout(i) = (wi1j1*regin(i1,j1)+wi2j1*regin(i2,j1)+
3644  & wi1j2*sums +wi2j2*sums )
3645  & * wsumiv
3646  elseif (j1 .eq. jmxin) then
3647  gauout(i) = (wi1j1*sumn +wi2j1*sumn +
3648  & wi1j2*regin(i1,j2)+wi2j2*regin(i2,j2))
3649  & * wsumiv
3650  endif
3651  endif
3652  endif ! if j1 .ne. j2
3653  endif
3654  enddo
3655  do i=i1_t,i2_t
3656  j1 = jindx1(i)
3657  j2 = jindx2(i)
3658  i1 = iindx1(i)
3659  i2 = iindx2(i)
3660  if(wrk(i) .eq. 0.0) then
3661  if(.not.lmask) then
3662  write(6,*) ' FATAL ERROR: la2ga called with lmask=true'
3663  write(6,*) ' But bad rslmsk or slmask given.'
3664  call abort
3665  endif
3666  ifill(it) = ifill(it) + 1
3667  if(ifill(it) <= 2 ) then
3668  if (me == 0 .and. num_threads == 1) then
3669  write(6,*) 'i1,i2,j1,j2=',i1,i2,j1,j2
3670  write(6,*) 'rslmsk=',rslmsk(i1,j1),rslmsk(i1,j2),
3671  & rslmsk(i2,j1),rslmsk(i2,j2)
3672 ! write(6,*) 'i,j=',i,j,' slmask(i)=',slmask(i)
3673  write(6,*) 'i=',i,' slmask(i)=',slmask(i)
3674  &, ' outlon=',outlon(i),' outlat=',outlat(i)
3675  endif
3676  endif
3677 ! spiral around until matching mask is found.
3678  do nx=1,jmxin*imxin/2
3679  kxs=sqrt(4*nx-2.5)
3680  kxt=nx-int(kxs**2/4+1)
3681  select case(mod(kxs,4))
3682  case(1)
3683  ix=i1-kxs/4+kxt
3684  jx=j1-kxs/4
3685  case(2)
3686  ix=i1+1+kxs/4
3687  jx=j1-kxs/4+kxt
3688  case(3)
3689  ix=i1+1+kxs/4-kxt
3690  jx=j1+1+kxs/4
3691  case default
3692  ix=i1-kxs/4
3693  jx=j1+kxs/4-kxt
3694  end select
3695  if(jx.lt.1) then
3696  ix=ix+imxin/2
3697  jx=2-jx
3698  elseif(jx.gt.jmxin) then
3699  ix=ix+imxin/2
3700  jx=2*jmxin-jx
3701  endif
3702  ix=modulo(ix-1,imxin)+1
3703  if(slmask(i).eq.rslmsk(ix,jx)) then
3704  gauout(i) = regin(ix,jx)
3705  imxnx(it) = max(imxnx(it),nx)
3706  go to 71
3707  endif
3708  enddo
3709 !
3710  write(6,*) ' FATAL ERROR: no filling value'
3711  write(6,*) ' found in la2ga.'
3712 ! write(6,*) ' i ix jx slmask(i) rslmsk ',
3713 ! & i,ix,jx,slmask(i),rslmsk(ix,jx)
3714  call abort
3715 !
3716  71 continue
3717  endif
3718 !
3719  enddo
3720  endif
3721  enddo ! end of threaded loop ...................
3722 !$omp end parallel do
3723 !
3724  if(inttyp /= 1)then
3725  ifills = 0
3726  do it=1,num_threads
3727  ifills = ifills + ifill(it)
3728  enddo
3729 
3730  if(ifills.gt.1) then
3731  if (me .eq. 0) then
3732  write(6,*) ' unable to interpolate. filled with nearest',
3733  & ' point value at ',ifills,' points'
3734 ! & ' point value at ',ifills,' points imxnx=',imxnx(:)
3735  endif
3736  endif
3737  deallocate (ifill)
3738  endif
3739 !
3740 ! kmami = 1
3741 ! if (me == 0) call maxmin(gauout,len,kmami)
3742 !
3743  return
3744  end subroutine la2ga
3745 
3752  subroutine maxmin(f,imax,kmax)
3753  use machine , only : kind_io8,kind_io4
3754  implicit none
3755  integer i,iimin,iimax,kmax,imax,k
3756  real (kind=kind_io8) fmin,fmax
3757 !
3758  real (kind=kind_io8) f(imax,kmax)
3759 !
3760  do k=1,kmax
3761 !
3762  fmax = f(1,k)
3763  fmin = f(1,k)
3764 !
3765  do i=1,imax
3766  if(fmax.le.f(i,k)) then
3767  fmax = f(i,k)
3768  iimax = i
3769  endif
3770  if(fmin.ge.f(i,k)) then
3771  fmin = f(i,k)
3772  iimin = i
3773  endif
3774  enddo
3775 !
3776 ! write(6,100) k,fmax,iimax,fmin,iimin
3777 ! 100 format(2x,'level=',i2,' max=',e11.4,' at i=',i7,
3778 ! & ' min=',e11.4,' at i=',i7)
3779 !
3780  enddo
3781 !
3782  return
3783  end
3784 
3852  subroutine filanl(tsfanl,tsfan2,wetanl,snoanl,zoranl,albanl,
3853  & aisanl,
3854  & tg3anl,cvanl ,cvbanl,cvtanl,
3855  & cnpanl,smcanl,stcanl,slianl,scvanl,veganl,
3856  & vetanl,sotanl,alfanl,
3857  & sihanl,sicanl,
3858  & vmnanl,vmxanl,slpanl,absanl,
3859  & tsfclm,tsfcl2,wetclm,snoclm,zorclm,albclm,
3860  & aisclm,
3861  & tg3clm,cvclm ,cvbclm,cvtclm,
3862  & cnpclm,smcclm,stcclm,sliclm,scvclm,vegclm,
3863  & vetclm,sotclm,alfclm,
3864  & sihclm,sicclm,
3865  & vmnclm,vmxclm,slpclm,absclm,
3866  & len,lsoil)
3867  use machine , only : kind_io8,kind_io4
3868  implicit none
3869  integer i,j,len,lsoil
3870 !
3871  real (kind=kind_io8) tsfanl(len),tsfan2(len),wetanl(len),
3872  & snoanl(len),
3873  & zoranl(len),albanl(len,4),aisanl(len),
3874  & tg3anl(len),
3875  & cvanl(len),cvbanl(len),cvtanl(len),
3876  & cnpanl(len),
3877  & smcanl(len,lsoil),stcanl(len,lsoil),
3878  & slianl(len),scvanl(len),veganl(len),
3879  & vetanl(len),sotanl(len),alfanl(len,2)
3880 !cwu [+1l] add ()anl for sih, sic
3881  &, sihanl(len),sicanl(len)
3882 !clu [+1l] add ()anl for vmn, vmx, slp, abs
3883  &, vmnanl(len),vmxanl(len),slpanl(len),absanl(len)
3884  real (kind=kind_io8) tsfclm(len),tsfcl2(len),wetclm(len),
3885  & snoclm(len),
3886  & zorclm(len),albclm(len,4),aisclm(len),
3887  & tg3clm(len),
3888  & cvclm(len),cvbclm(len),cvtclm(len),
3889  & cnpclm(len),
3890  & smcclm(len,lsoil),stcclm(len,lsoil),
3891  & sliclm(len),scvclm(len),vegclm(len),
3892  & vetclm(len),sotclm(len),alfclm(len,2)
3893 !cwu [+1l] add ()clm for sih, sic
3894  &, sihclm(len),sicclm(len)
3895 !clu [+1l] add ()clm for vmn, vmx, slp, abs
3896  &, vmnclm(len),vmxclm(len),slpclm(len),absclm(len)
3897 !
3898  do i=1,len
3899  tsfanl(i) = tsfclm(i) ! tsf at t
3900  tsfan2(i) = tsfcl2(i) ! tsf at t-deltsfc
3901  wetanl(i) = wetclm(i) ! soil wetness
3902  snoanl(i) = snoclm(i) ! snow
3903  scvanl(i) = scvclm(i) ! snow cover
3904  aisanl(i) = aisclm(i) ! seaice
3905  slianl(i) = sliclm(i) ! land/sea/snow mask
3906  zoranl(i) = zorclm(i) ! surface roughness
3907 ! plranl(i) = plrclm(i) ! maximum stomatal resistance
3908  tg3anl(i) = tg3clm(i) ! deep soil temperature
3909  cnpanl(i) = cnpclm(i) ! canopy water content
3910  veganl(i) = vegclm(i) ! vegetation cover
3911  vetanl(i) = vetclm(i) ! vegetation type
3912  sotanl(i) = sotclm(i) ! soil type
3913  cvanl(i) = cvclm(i) ! cv
3914  cvbanl(i) = cvbclm(i) ! cvb
3915  cvtanl(i) = cvtclm(i) ! cvt
3916 !cwu [+4l] add sih, sic
3917  sihanl(i) = sihclm(i) ! sea ice thickness
3918  sicanl(i) = sicclm(i) ! sea ice concentration
3919 !clu [+4l] add vmn, vmx, slp, abs
3920  vmnanl(i) = vmnclm(i) ! min vegetation cover
3921  vmxanl(i) = vmxclm(i) ! max vegetation cover
3922  slpanl(i) = slpclm(i) ! slope type
3923  absanl(i) = absclm(i) ! max snow albedo
3924  enddo
3925 !
3926  do j=1,lsoil
3927  do i=1,len
3928  smcanl(i,j) = smcclm(i,j) ! layer soil wetness
3929  stcanl(i,j) = stcclm(i,j) ! soil temperature
3930  enddo
3931  enddo
3932  do j=1,4
3933  do i=1,len
3934  albanl(i,j) = albclm(i,j) ! albedo
3935  enddo
3936  enddo
3937  do j=1,2
3938  do i=1,len
3939  alfanl(i,j) = alfclm(i,j) ! vegetation fraction for albedo
3940  enddo
3941  enddo
3942 !
3943  return
3944  end
3945 
4072  subroutine analy(lugb,iy,im,id,ih,fh,len,lsoil,
4073  & slmask,fntsfa,fnweta,fnsnoa,fnzora,fnalba,fnaisa,
4074  & fntg3a,fnscva,fnsmca,fnstca,fnacna,fnvega,
4075  & fnveta,fnsota,
4076  & fnvmna,fnvmxa,fnslpa,fnabsa,
4077  & tsfanl,wetanl,snoanl,zoranl,albanl,aisanl,
4078  & tg3anl,cvanl ,cvbanl,cvtanl,
4079  & smcanl,stcanl,slianl,scvanl,acnanl,veganl,
4080  & vetanl,sotanl,alfanl,tsfan0,
4081  & vmnanl,vmxanl,slpanl,absanl,
4082  & kpdtsf,kpdwet,kpdsno,kpdsnd,kpdzor,kpdalb,kpdais,
4083  & kpdtg3,kpdscv,kpdacn,kpdsmc,kpdstc,kpdveg,
4084  & kprvet,kpdsot,kpdalf,
4085  & kpdvmn,kpdvmx,kpdslp,kpdabs,
4086  & irttsf,irtwet,irtsno,irtzor,irtalb,irtais,
4087  & irttg3,irtscv,irtacn,irtsmc,irtstc,irtveg,
4088  & irtvet,irtsot,irtalf
4089  &, irtvmn,irtvmx,irtslp,irtabs
4090  &, imsk, jmsk, slmskh, outlat, outlon
4091  &, gaus, blno, blto, me, lanom)
4092  use machine , only : kind_io8,kind_io4
4093  implicit none
4094  logical lanom
4095  integer irtsmc,irtacn,irtstc,irtvet,irtveg,irtscv,irtzor,irtsno,
4096  & irtalb,irttg3,irtais,iret,me,kk,kpdvet,i,irtalf,irtsot,
4097 !cggg snow mods start & imsk,jmsk,irtwet,lsoil,len, kpdtsf,kpdsno,kpdwet,iy,
4098  & imsk,jmsk,irtwet,lsoil,len,kpdtsf,kpdsno,kpdsnd,kpdwet,iy,
4099 !cggg snow mods end
4100  & lugb,im,ih,id,kpdveg,kpdstc,kprvet,irttsf,kpdsot,kpdsmc,
4101  & kpdais,kpdzor,kpdtg3,kpdacn,kpdscv,j
4102 !clu [+1l] add kpd() and irt() for vmn, vmx, slp, abs
4103  &, kpdvmn,kpdvmx,kpdslp,kpdabs,irtvmn,irtvmx,irtslp,irtabs
4104  real (kind=kind_io8) blto,blno,fh
4105 !
4106  real (kind=kind_io8) slmask(len)
4107  real (kind=kind_io8) slmskh(imsk,jmsk)
4108  real (kind=kind_io8) outlat(len), outlon(len)
4109  integer kpdalb(4), kpdalf(2)
4110 !cggg snow mods start
4111  integer kpds(1000),kgds(1000),jpds(1000),jgds(1000)
4112  integer lugi, lskip, lgrib, ndata
4113 !cggg snow mods end
4114 !
4115  character*500 fntsfa,fnweta,fnsnoa,fnzora,fnalba,fnaisa,
4116  & fntg3a,fnscva,fnsmca,fnstca,fnacna,fnvega,
4117  & fnveta,fnsota
4118 !clu [+1l] add fn()a for vmn, vmx, slp, abs
4119  &, fnvmna,fnvmxa,fnslpa,fnabsa
4120 
4121  real (kind=kind_io8) tsfanl(len), wetanl(len), snoanl(len),
4122  & zoranl(len), albanl(len,4), aisanl(len),
4123  & tg3anl(len), acnanl(len),
4124  & cvanl(len), cvbanl(len), cvtanl(len),
4125  & slianl(len), scvanl(len), veganl(len),
4126  & vetanl(len), sotanl(len), alfanl(len,2),
4127  & smcanl(len,lsoil), stcanl(len,lsoil),
4128  & tsfan0(len)
4129 !clu [+1l] add ()anl for vmn, vmx, slp, abs
4130  &, vmnanl(len),vmxanl(len),slpanl(len),absanl(len)
4131 !
4132  logical gaus
4133 !
4134 ! tsf
4135 !
4136  irttsf = 1
4137  if(fntsfa(1:8).ne.' ') then
4138  call fixrda(lugb,fntsfa,kpdtsf,slmask,
4139  & iy,im,id,ih,fh,tsfanl,len,iret
4140  &, imsk, jmsk, slmskh, gaus,blno, blto
4141  &, outlat, outlon, me)
4142  irttsf = iret
4143  if(iret == 1) then
4144  write(6,*) 'FATAL ERROR: t surface analysis read error.'
4145  call abort
4146  elseif(iret == -1) then
4147  if (me == 0) then
4148  print *,'old t surface analysis provided, indicating proper'
4149  &, ' file name is given. no error suspected.'
4150  write(6,*) 'forecast guess will be used'
4151  endif
4152  else
4153  if (me == 0) print *,'t surface analysis provided.'
4154  endif
4155  else
4156  if (me == 0) then
4157 ! print *,'************************************************'
4158  print *,'no tsf analysis available. climatology used'
4159  endif
4160  endif
4161 !
4162 ! tsf0
4163 !
4164  if(fntsfa(1:8).ne.' ' .and. lanom) then
4165  call fixrda(lugb,fntsfa,kpdtsf,slmask,
4166  & iy,im,id,ih,0.,tsfan0,len,iret
4167  &, imsk, jmsk, slmskh, gaus,blno, blto
4168  &, outlat, outlon, me)
4169  if(iret == 1) then
4170  write(6,*) 'FATAL ERROR: t surface at ft=0 analysis'
4171  write(6,*) 'read error.'
4172  call abort
4173  elseif(iret == -1) then
4174  if (me == 0) then
4175  write(6,*) 'FATAL ERROR: Could not find t surface'
4176  write(6,*) 'analysis at ft=0.'
4177  endif
4178  call abort
4179  else
4180  print *,'t surface analysis at ft=0 found.'
4181  endif
4182  else
4183  do i=1,len
4184  tsfan0(i)=-999.9
4185  enddo
4186  endif
4187 !
4188 ! albedo
4189 !
4190  irtalb=0
4191  if(fnalba(1:8).ne.' ') then
4192  do kk = 1, 4
4193  call fixrda(lugb,fnalba,kpdalb(kk),slmask,
4194  & iy,im,id,ih,fh,albanl(1,kk),len,iret
4195  &, imsk, jmsk, slmskh, gaus,blno, blto
4196  &, outlat, outlon, me)
4197  irtalb=iret
4198  if(iret.eq.1) then
4199  write(6,*) 'FATAL ERROR: Albedo analysis read error.'
4200  call abort
4201  elseif(iret.eq.-1) then
4202  if (me .eq. 0) then
4203  print *,'old albedo analysis provided, indicating proper',
4204  & ' file name is given. no error suspected.'
4205  write(6,*) 'forecast guess will be used'
4206  endif
4207  else
4208  if (me .eq. 0 .and. kk .eq. 4)
4209  & print *,'albedo analysis provided.'
4210  endif
4211  enddo
4212  else
4213  if (me .eq. 0) then
4214 ! print *,'************************************************'
4215  print *,'no albedo analysis available. climatology used'
4216  endif
4217  endif
4218 !
4219 ! vegetation fraction for albedo
4220 !
4221  irtalf=0
4222  if(fnalba(1:8).ne.' ') then
4223  do kk = 1, 2
4224  call fixrda(lugb,fnalba,kpdalf(kk),slmask,
4225  & iy,im,id,ih,fh,alfanl(1,kk),len,iret
4226  &, imsk, jmsk, slmskh, gaus,blno, blto
4227  &, outlat, outlon, me)
4228  irtalf=iret
4229  if(iret.eq.1) then
4230  write(6,*) 'FATAL ERROR: Albedo analysis read error.'
4231  call abort
4232  elseif(iret.eq.-1) then
4233  if (me .eq. 0) then
4234  print *,'old albedo analysis provided, indicating proper',
4235  & ' file name is given. no error suspected.'
4236  write(6,*) 'forecast guess will be used'
4237  endif
4238  else
4239  if (me .eq. 0 .and. kk .eq. 4)
4240  & print *,'albedo analysis provided.'
4241  endif
4242  enddo
4243  else
4244  if (me .eq. 0) then
4245 ! print *,'************************************************'
4246  print *,'no vegfalbedo analysis available. climatology used'
4247  endif
4248  endif
4249 !
4250 ! soil wetness
4251 !
4252  irtwet=0
4253  irtsmc=0
4254  if(fnweta(1:8).ne.' ') then
4255  call fixrda(lugb,fnweta,kpdwet,slmask,
4256  & iy,im,id,ih,fh,wetanl,len,iret
4257  &, imsk, jmsk, slmskh, gaus,blno, blto
4258  &, outlat, outlon, me)
4259  irtwet=iret
4260  if(iret.eq.1) then
4261  write(6,*) 'FATAL ERROR: Bucket wetness analysis read error.'
4262  call abort
4263  elseif(iret.eq.-1) then
4264  if (me .eq. 0) then
4265  print *,'old wetness analysis provided, indicating proper',
4266  & ' file name is given. no error suspected.'
4267  write(6,*) 'forecast guess will be used'
4268  endif
4269  else
4270  if (me .eq. 0) print *,'bucket wetness analysis provided.'
4271  endif
4272  elseif(fnsmca(1:8).ne.' ') then
4273  call fixrda(lugb,fnsmca,kpdsmc,slmask,
4274  & iy,im,id,ih,fh,smcanl(1,1),len,iret
4275  &, imsk, jmsk, slmskh, gaus,blno, blto
4276  &, outlat, outlon, me)
4277  call fixrda(lugb,fnsmca,kpdsmc,slmask,
4278  & iy,im,id,ih,fh,smcanl(1,2),len,iret
4279  &, imsk, jmsk, slmskh, gaus,blno, blto
4280  &, outlat, outlon, me)
4281  irtsmc=iret
4282  if(iret.eq.1) then
4283  write(6,*) 'FATAL ERROR: Layer soil wetness analysis'
4284  write(6,*) 'read error.'
4285  call abort
4286  elseif(iret.eq.-1) then
4287  if (me .eq. 0) then
4288  print *,'old layer soil wetness analysis provided',
4289  & ' indicating proper file name is given.'
4290  print *,' no error suspected.'
4291  write(6,*) 'forecast guess will be used'
4292  endif
4293  else
4294  if (me .eq. 0) print *,'layer soil wetness analysis provided.'
4295  endif
4296  else
4297  if (me .eq. 0) then
4298 ! print *,'************************************************'
4299  print *,'no soil wetness analysis available. climatology used'
4300  endif
4301  endif
4302 !
4303 ! read in snow depth/snow cover
4304 !
4305  irtscv=0
4306  if(fnsnoa(1:8).ne.' ') then
4307  do i=1,len
4308  scvanl(i)=0.
4309  enddo
4310 !cggg snow mods start
4311 !cggg need to determine if the snow data is on the gaussian grid
4312 !cggg or not. if gaussian, then data is a depth, not liq equiv
4313 !cggg depth. if not gaussian, then data is from hua-lu's
4314 !cggg program and is a liquid equiv. need to communicate
4315 !cggg this to routine fixrda via the 3rd argument which is
4316 !cggg the grib parameter id number.
4317  call baopenr(lugb,fnsnoa,iret)
4318  if (iret .ne. 0) then
4319  write(6,*) 'FATAL ERROR: in opening file ',trim(fnsnoa)
4320  print *,'FATAL ERROR: in opening file ',trim(fnsnoa)
4321  call abort
4322  endif
4323  lugi=0
4324  lskip=-1
4325  jpds=-1
4326  jgds=-1
4327  kpds=jpds
4328  call getgbh(lugb,lugi,lskip,jpds,jgds,lgrib,ndata,
4329  & lskip,kpds,kgds,iret)
4330  close(lugb)
4331  if (iret .ne. 0) then
4332  write(6,*) ' FATAL ERROR: Reading header'
4333  write(6,*) ' of file: ',trim(fnsnoa)
4334  print *,'FATAL ERROR: Reading header of file: ',trim(fnsnoa)
4335  call abort
4336  endif
4337  if (kgds(1) == 4) then ! gaussian data is depth
4338  call fixrda(lugb,fnsnoa,kpdsnd,slmask,
4339  & iy,im,id,ih,fh,snoanl,len,iret
4340  &, imsk, jmsk, slmskh, gaus,blno, blto
4341  &, outlat, outlon, me)
4342  snoanl=snoanl*100. ! convert from meters to liq. eq.
4343  ! depth in mm using 10:1 ratio
4344  else ! lat/lon data is liq equv. depth
4345  call fixrda(lugb,fnsnoa,kpdsno,slmask,
4346  & iy,im,id,ih,fh,snoanl,len,iret
4347  &, imsk, jmsk, slmskh, gaus,blno, blto
4348  &, outlat, outlon, me)
4349  endif
4350 !cggg snow mods end
4351  irtscv=iret
4352  if(iret.eq.1) then
4353  write(6,*) 'FATAL ERROR: snow depth analysis read error.'
4354  call abort
4355  elseif(iret.eq.-1) then
4356  if (me .eq. 0) then
4357  print *,'old snow depth analysis provided, indicating proper',
4358  & ' file name is given. no error suspected.'
4359  write(6,*) 'forecast guess will be used'
4360  endif
4361  else
4362  if (me .eq. 0) print *,'snow depth analysis provided.'
4363  endif
4364  irtsno=0
4365  elseif(fnscva(1:8).ne.' ') then
4366  do i=1,len
4367  snoanl(i)=0.
4368  enddo
4369  call fixrda(lugb,fnscva,kpdscv,slmask,
4370  & iy,im,id,ih,fh,scvanl,len,iret
4371  &, imsk, jmsk, slmskh, gaus,blno, blto
4372  &, outlat, outlon, me)
4373  irtsno=iret
4374  if(iret.eq.1) then
4375  write(6,*) 'FATAL ERROR: snow cover analysis read error.'
4376  call abort
4377  elseif(iret.eq.-1) then
4378  if (me .eq. 0) then
4379  print *,'old snow cover analysis provided, indicating proper',
4380  & ' file name is given. no error suspected.'
4381  write(6,*) 'forecast guess will be used'
4382  endif
4383  else
4384  if (me .eq. 0) print *,'snow cover analysis provided.'
4385  endif
4386  else
4387  if (me .eq. 0) then
4388 ! print *,'************************************************'
4389  print *,'no snow/snocov analysis available. climatology used'
4390  endif
4391  endif
4392 !
4393 ! sea ice mask
4394 !
4395  irtacn=0
4396  irtais=0
4397  if(fnacna(1:8).ne.' ') then
4398  call fixrda(lugb,fnacna,kpdacn,slmask,
4399  & iy,im,id,ih,fh,acnanl,len,iret
4400  &, imsk, jmsk, slmskh, gaus,blno, blto
4401  &, outlat, outlon, me)
4402  irtacn=iret
4403  if(iret.eq.1) then
4404  write(6,*) 'FATAL ERROR: ice concentration'
4405  write(6,*) 'analysis read error.'
4406  call abort
4407  elseif(iret.eq.-1) then
4408  if (me .eq. 0) then
4409  print *,'old ice concentration analysis provided',
4410  & ' indicating proper file name is given'
4411  print *,' no error suspected.'
4412  write(6,*) 'forecast guess will be used'
4413  endif
4414  else
4415  if (me .eq. 0) print *,'ice concentration analysis provided.'
4416  endif
4417  elseif(fnaisa(1:8).ne.' ') then
4418  call fixrda(lugb,fnaisa,kpdais,slmask,
4419  & iy,im,id,ih,fh,aisanl,len,iret
4420  &, imsk, jmsk, slmskh, gaus,blno, blto
4421  &, outlat, outlon, me)
4422  irtais=iret
4423  if(iret.eq.1) then
4424  write(6,*) 'FATAL ERROR: ice mask analysis read error.'
4425  call abort
4426  elseif(iret.eq.-1) then
4427  if (me .eq. 0) then
4428  print *,'old ice-mask analysis provided, indicating proper',
4429  & ' file name is given. no error suspected.'
4430  write(6,*) 'forecast guess will be used'
4431  endif
4432  else
4433  if (me .eq. 0) print *,'ice mask analysis provided.'
4434  endif
4435  else
4436  if (me .eq. 0) then
4437 ! print *,'************************************************'
4438  print *,'no sea-ice analysis available. climatology used'
4439  endif
4440  endif
4441 !
4442 ! surface roughness
4443 !
4444  irtzor=0
4445  if(fnzora(1:8).ne.' ') then
4446  call fixrda(lugb,fnzora,kpdzor,slmask,
4447  & iy,im,id,ih,fh,zoranl,len,iret
4448  &, imsk, jmsk, slmskh, gaus,blno, blto
4449  &, outlat, outlon, me)
4450  irtzor=iret
4451  if(iret.eq.1) then
4452  write(6,*) 'FATAL ERROR: roughness analysis read error.'
4453  call abort
4454  elseif(iret.eq.-1) then
4455  if (me .eq. 0) then
4456  print *,'old roughness analysis provided, indicating proper',
4457  & ' file name is given. no error suspected.'
4458  write(6,*) 'forecast guess will be used'
4459  endif
4460  else
4461  if (me .eq. 0) print *,'roughness analysis provided.'
4462  endif
4463  else
4464  if (me .eq. 0) then
4465 ! print *,'************************************************'
4466  print *,'no srfc roughness analysis available. climatology used'
4467  endif
4468  endif
4469 !
4470 ! deep soil temperature
4471 !
4472  irttg3=0
4473  irtstc=0
4474  if(fntg3a(1:8).ne.' ') then
4475  call fixrda(lugb,fntg3a,kpdtg3,slmask,
4476  & iy,im,id,ih,fh,tg3anl,len,iret
4477  &, imsk, jmsk, slmskh, gaus,blno, blto
4478  &, outlat, outlon, me)
4479  irttg3=iret
4480  if(iret.eq.1) then
4481  write(6,*) 'FATAL ERROR: deep soil tmp analysis read error.'
4482  call abort
4483  elseif(iret.eq.-1) then
4484  if (me .eq. 0) then
4485  print *,'old deep soil temp analysis provided',
4486  & ' indicating proper file name is given.'
4487  print *,' no error suspected.'
4488  write(6,*) 'forecast guess will be used'
4489  endif
4490  else
4491  if (me .eq. 0) print *,'deep soil tmp analysis provided.'
4492  endif
4493  elseif(fnstca(1:8).ne.' ') then
4494  call fixrda(lugb,fnstca,kpdstc,slmask,
4495  & iy,im,id,ih,fh,stcanl(1,1),len,iret
4496  &, imsk, jmsk, slmskh, gaus,blno, blto
4497  &, outlat, outlon, me)
4498  call fixrda(lugb,fnstca,kpdstc,slmask,
4499  & iy,im,id,ih,fh,stcanl(1,2),len,iret
4500  &, imsk, jmsk, slmskh, gaus,blno, blto
4501  &, outlat, outlon, me)
4502  irtstc=iret
4503  if(iret.eq.1) then
4504  write(6,*) 'FATAL ERROR: layer soil tmp analysis read error.'
4505  call abort
4506  elseif(iret.eq.-1) then
4507  if (me .eq. 0) then
4508  print *,'old deep soil temp analysis provided',
4509  & 'iindicating proper file name is given.'
4510  print *,' no error suspected.'
4511  write(6,*) 'forecast guess will be used'
4512  endif
4513  else
4514  if (me .eq. 0) print *,'layer soil tmp analysis provided.'
4515  endif
4516  else
4517  if (me .eq. 0) then
4518 ! print *,'************************************************'
4519  print *,'no deep soil temp analy available. climatology used'
4520  endif
4521  endif
4522 !
4523 ! vegetation cover
4524 !
4525  irtveg=0
4526  if(fnvega(1:8).ne.' ') then
4527  call fixrda(lugb,fnvega,kpdveg,slmask,
4528  & iy,im,id,ih,fh,veganl,len,iret
4529  &, imsk, jmsk, slmskh, gaus,blno, blto
4530  &, outlat, outlon, me)
4531  irtveg=iret
4532  if(iret.eq.1) then
4533  write(6,*) 'FATAL ERROR: vegetation cover analysis'
4534  write(6,*) 'read error.'
4535  call abort
4536  elseif(iret.eq.-1) then
4537  if (me .eq. 0) then
4538  print *,'old vegetation cover analysis provided',
4539  & ' indicating proper file name is given.'
4540  print *,' no error suspected.'
4541  write(6,*) 'forecast guess will be used'
4542  endif
4543  else
4544  if (me .eq. 0) print *,'vegetation cover analysis provided.'
4545  endif
4546  else
4547  if (me .eq. 0) then
4548 ! print *,'************************************************'
4549  print *,'no vegetation cover anly available. climatology used'
4550  endif
4551  endif
4552 !
4553 ! vegetation type
4554 !
4555  irtvet=0
4556  if(fnveta(1:8).ne.' ') then
4557  call fixrda(lugb,fnveta,kpdvet,slmask,
4558  & iy,im,id,ih,fh,vetanl,len,iret
4559  &, imsk, jmsk, slmskh, gaus,blno, blto
4560  &, outlat, outlon, me)
4561  irtvet=iret
4562  if(iret.eq.1) then
4563  write(6,*) 'FATAL ERROR: vegetation type analysis read error.'
4564  call abort
4565  elseif(iret.eq.-1) then
4566  if (me .eq. 0) then
4567  print *,'old vegetation type analysis provided',
4568  & ' indicating proper file name is given.'
4569  print *,' no error suspected.'
4570  write(6,*) 'forecast guess will be used'
4571  endif
4572  else
4573  if (me .eq. 0) print *,'vegetation type analysis provided.'
4574  endif
4575  else
4576  if (me .eq. 0) then
4577 ! print *,'************************************************'
4578  print *,'no vegetation type anly available. climatology used'
4579  endif
4580  endif
4581 !
4582 ! soil type
4583 !
4584  irtsot=0
4585  if(fnsota(1:8).ne.' ') then
4586  call fixrda(lugb,fnsota,kpdsot,slmask,
4587  & iy,im,id,ih,fh,sotanl,len,iret
4588  &, imsk, jmsk, slmskh, gaus,blno, blto
4589  &, outlat, outlon, me)
4590  irtsot=iret
4591  if(iret.eq.1) then
4592  write(6,*) 'FATAL ERROR: soil type analysis read error.'
4593  call abort
4594  elseif(iret.eq.-1) then
4595  if (me .eq. 0) then
4596  print *,'old soil type analysis provided',
4597  & ' indicating proper file name is given.'
4598  print *,' no error suspected.'
4599  write(6,*) 'forecast guess will be used'
4600  endif
4601  else
4602  if (me .eq. 0) print *,'soil type analysis provided.'
4603  endif
4604  else
4605  if (me .eq. 0) then
4606 ! print *,'************************************************'
4607  print *,'no soil type anly available. climatology used'
4608  endif
4609  endif
4610 
4611 !clu [+120l]--------------------------------------------------------------
4612 !
4613 ! min vegetation cover
4614 !
4615  irtvmn=0
4616  if(fnvmna(1:8).ne.' ') then
4617  call fixrda(lugb,fnvmna,kpdvmn,slmask,
4618  & iy,im,id,ih,fh,vmnanl,len,iret
4619  &, imsk, jmsk, slmskh, gaus,blno, blto
4620  &, outlat, outlon, me)
4621  irtvmn=iret
4622  if(iret.eq.1) then
4623  write(6,*) 'FATAL ERROR: shdmin analysis read error.'
4624  call abort
4625  elseif(iret.eq.-1) then
4626  if (me .eq. 0) then
4627  print *,'old shdmin analysis provided',
4628  & ' indicating proper file name is given.'
4629  print *,' no error suspected.'
4630  write(6,*) 'forecast guess will be used'
4631  endif
4632  else
4633  if (me .eq. 0) print *,'shdmin analysis provided.'
4634  endif
4635  else
4636  if (me .eq. 0) then
4637 ! print *,'************************************************'
4638  print *,'no shdmin anly available. climatology used'
4639  endif
4640  endif
4641 
4642 !
4643 ! max vegetation cover
4644 !
4645  irtvmx=0
4646  if(fnvmxa(1:8).ne.' ') then
4647  call fixrda(lugb,fnvmxa,kpdvmx,slmask,
4648  & iy,im,id,ih,fh,vmxanl,len,iret
4649  &, imsk, jmsk, slmskh, gaus,blno, blto
4650  &, outlat, outlon, me)
4651  irtvmx=iret
4652  if(iret.eq.1) then
4653  write(6,*) 'FATAL ERROR: shdmax analysis read error.'
4654  call abort
4655  elseif(iret.eq.-1) then
4656  if (me .eq. 0) then
4657  print *,'old shdmax analysis provided',
4658  & ' indicating proper file name is given.'
4659  print *,' no error suspected.'
4660  write(6,*) 'forecast guess will be used'
4661  endif
4662  else
4663  if (me .eq. 0) print *,'shdmax analysis provided.'
4664  endif
4665  else
4666  if (me .eq. 0) then
4667 ! print *,'************************************************'
4668  print *,'no shdmax anly available. climatology used'
4669  endif
4670  endif
4671 
4672 !
4673 ! slope type
4674 !
4675  irtslp=0
4676  if(fnslpa(1:8).ne.' ') then
4677  call fixrda(lugb,fnslpa,kpdslp,slmask,
4678  & iy,im,id,ih,fh,slpanl,len,iret
4679  &, imsk, jmsk, slmskh, gaus,blno, blto
4680  &, outlat, outlon, me)
4681  irtslp=iret
4682  if(iret.eq.1) then
4683  write(6,*) 'FATAL ERROR: slope type analysis read error.'
4684  call abort
4685  elseif(iret.eq.-1) then
4686  if (me .eq. 0) then
4687  print *,'old slope type analysis provided',
4688  & ' indicating proper file name is given.'
4689  print *,' no error suspected.'
4690  write(6,*) 'forecast guess will be used'
4691  endif
4692  else
4693  if (me .eq. 0) print *,'slope type analysis provided.'
4694  endif
4695  else
4696  if (me .eq. 0) then
4697 ! print *,'************************************************'
4698  print *,'no slope type anly available. climatology used'
4699  endif
4700  endif
4701 
4702 !
4703 ! max snow albedo
4704 !
4705  irtabs=0
4706  if(fnabsa(1:8).ne.' ') then
4707  call fixrda(lugb,fnabsa,kpdabs,slmask,
4708  & iy,im,id,ih,fh,absanl,len,iret
4709  &, imsk, jmsk, slmskh, gaus,blno, blto
4710  &, outlat, outlon, me)
4711  irtabs=iret
4712  if(iret.eq.1) then
4713  write(6,*) 'FATAL ERROR: snoalb analysis read error.'
4714  call abort
4715  elseif(iret.eq.-1) then
4716  if (me .eq. 0) then
4717  print *,'old snoalb analysis provided',
4718  & ' indicating proper file name is given.'
4719  print *,' no error suspected.'
4720  write(6,*) 'forecast guess will be used'
4721  endif
4722  else
4723  if (me .eq. 0) print *,'snoalb analysis provided.'
4724  endif
4725  else
4726  if (me .eq. 0) then
4727 ! print *,'************************************************'
4728  print *,'no snoalb anly available. climatology used'
4729  endif
4730  endif
4731 
4732 !clu ----------------------------------------------------------------------
4733 !
4734  return
4735  end
4736 
4793  subroutine filfcs(tsffcs,wetfcs,snofcs,zorfcs,albfcs,
4794  & tg3fcs,cvfcs ,cvbfcs,cvtfcs,
4795  & cnpfcs,smcfcs,stcfcs,slifcs,aisfcs,
4796  & vegfcs, vetfcs, sotfcs, alffcs,
4797  & sihfcs,sicfcs,
4798  & vmnfcs,vmxfcs,slpfcs,absfcs,
4799  & tsfanl,wetanl,snoanl,zoranl,albanl,
4800  & tg3anl,cvanl ,cvbanl,cvtanl,
4801  & cnpanl,smcanl,stcanl,slianl,aisanl,
4802  & veganl, vetanl, sotanl, alfanl,
4803  & sihanl,sicanl,
4804  & vmnanl,vmxanl,slpanl,absanl,
4805  & len,lsoil)
4806 !
4807  use machine , only : kind_io8,kind_io4
4808  implicit none
4809  integer i,j,len,lsoil
4810  real (kind=kind_io8) tsffcs(len),wetfcs(len),snofcs(len),
4811  & zorfcs(len),albfcs(len,4),aisfcs(len),
4812  & tg3fcs(len),
4813  & cvfcs(len),cvbfcs(len),cvtfcs(len),
4814  & cnpfcs(len),
4815  & smcfcs(len,lsoil),stcfcs(len,lsoil),
4816  & slifcs(len),vegfcs(len),
4817  & vetfcs(len),sotfcs(len),alffcs(len,2)
4818 !cwu [+1l] add ()fcs for sih, sic
4819  &, sihfcs(len),sicfcs(len)
4820 !clu [+1l] add ()fcs for vmn, vmx, slp, abs
4821  &, vmnfcs(len),vmxfcs(len),slpfcs(len),absfcs(len)
4822  real (kind=kind_io8) tsfanl(len),wetanl(len),snoanl(len),
4823  & zoranl(len),albanl(len,4),aisanl(len),
4824  & tg3anl(len),
4825  & cvanl(len),cvbanl(len),cvtanl(len),
4826  & cnpanl(len),
4827  & smcanl(len,lsoil),stcanl(len,lsoil),
4828  & slianl(len),veganl(len),
4829  & vetanl(len),sotanl(len),alfanl(len,2)
4830 !cwu [+1l] add ()anl for sih, sic
4831  &, sihanl(len),sicanl(len)
4832 !clu [+1l] add ()anl for vmn, vmx, slp, abs
4833  &, vmnanl(len),vmxanl(len),slpanl(len),absanl(len)
4834 !
4835  write(6,*) ' this is a dead start run, tsfc over land is',
4836  & ' set as lowest sigma level temperture if given.'
4837  write(6,*) ' if not, set to climatological tsf over land is used'
4838 !
4839 !
4840  do i=1,len
4841  tsffcs(i) = tsfanl(i) ! tsf
4842  albfcs(i,1) = albanl(i,1) ! albedo
4843  albfcs(i,2) = albanl(i,2) ! albedo
4844  albfcs(i,3) = albanl(i,3) ! albedo
4845  albfcs(i,4) = albanl(i,4) ! albedo
4846  wetfcs(i) = wetanl(i) ! soil wetness
4847  snofcs(i) = snoanl(i) ! snow
4848  aisfcs(i) = aisanl(i) ! seaice
4849  slifcs(i) = slianl(i) ! land/sea/snow mask
4850  zorfcs(i) = zoranl(i) ! surface roughness
4851 ! plrfcs(i) = plranl(i) ! maximum stomatal resistance
4852  tg3fcs(i) = tg3anl(i) ! deep soil temperature
4853  cnpfcs(i) = cnpanl(i) ! canopy water content
4854  cvfcs(i) = cvanl(i) ! cv
4855  cvbfcs(i) = cvbanl(i) ! cvb
4856  cvtfcs(i) = cvtanl(i) ! cvt
4857  vegfcs(i) = veganl(i) ! vegetation cover
4858  vetfcs(i) = vetanl(i) ! vegetation type
4859  sotfcs(i) = sotanl(i) ! soil type
4860  alffcs(i,1) = alfanl(i,1) ! vegetation fraction for albedo
4861  alffcs(i,2) = alfanl(i,2) ! vegetation fraction for albedo
4862 !cwu [+2l] add sih, sic
4863  sihfcs(i) = sihanl(i) ! sea ice thickness
4864  sicfcs(i) = sicanl(i) ! sea ice concentration
4865 !clu [+4l] add vmn, vmx, slp, abs
4866  vmnfcs(i) = vmnanl(i) ! min vegetation cover
4867  vmxfcs(i) = vmxanl(i) ! max vegetation cover
4868  slpfcs(i) = slpanl(i) ! slope type
4869  absfcs(i) = absanl(i) ! max snow albedo
4870  enddo
4871 !
4872  do j=1,lsoil
4873  do i=1,len
4874  smcfcs(i,j) = smcanl(i,j) ! layer soil wetness
4875  stcfcs(i,j) = stcanl(i,j) ! soil temperature
4876  enddo
4877  enddo
4878 !
4879  return
4880  end
4881 
4890  subroutine rof01(aisfld,len,op,crit)
4891  use machine , only : kind_io8,kind_io4
4892  implicit none
4893  integer i,len
4894  real (kind=kind_io8) aisfld(len),crit
4895  character*2 op
4896 !
4897  if(op.eq.'ge') then
4898  do i=1,len
4899  if(aisfld(i).ge.crit) then
4900  aisfld(i)=1.
4901  else
4902  aisfld(i)=0.
4903  endif
4904  enddo
4905  elseif(op.eq.'gt') then
4906  do i=1,len
4907  if(aisfld(i).gt.crit) then
4908  aisfld(i)=1.
4909  else
4910  aisfld(i)=0.
4911  endif
4912  enddo
4913  elseif(op.eq.'le') then
4914  do i=1,len
4915  if(aisfld(i).le.crit) then
4916  aisfld(i)=1.
4917  else
4918  aisfld(i)=0.
4919  endif
4920  enddo
4921  elseif(op.eq.'lt') then
4922  do i=1,len
4923  if(aisfld(i).lt.crit) then
4924  aisfld(i)=1.
4925  else
4926  aisfld(i)=0.
4927  endif
4928  enddo
4929  else
4930  write(6,*) 'FATAL ERROR: Illegal operator'
4931  write(6,*) 'in rof01. op=',op
4932  call abort
4933  endif
4934 !
4935  return
4936  end
4937 
4948  subroutine tsfcor(tsfc,orog,slmask,umask,len,rlapse)
4949 !
4950  use machine , only : kind_io8,kind_io4
4951  implicit none
4952  integer i,len
4953  real (kind=kind_io8) rlapse,umask
4954  real (kind=kind_io8) tsfc(len), orog(len), slmask(len)
4955 !
4956  do i=1,len
4957  if(slmask(i).eq.umask) then
4958  tsfc(i) = tsfc(i) - orog(i)*rlapse
4959  endif
4960  enddo
4961  return
4962  end
4963 
4978  subroutine snodpth(scvanl,slianl,tsfanl,snoclm,
4979  & glacir,snwmax,snwmin,landice,len,snoanl, me)
4980  use machine , only : kind_io8,kind_io4
4981  implicit none
4982  integer i,me,len
4983  logical, intent(in) :: landice
4984  real (kind=kind_io8) sno,snwmax,snwmin
4985 !
4986  real (kind=kind_io8) scvanl(len), slianl(len), tsfanl(len),
4987  & snoclm(len), snoanl(len), glacir(len)
4988 !
4989  if (me .eq. 0) write(6,*) 'snodpth'
4990 !
4991 ! use surface temperature to get snow depth estimate
4992 !
4993  do i=1,len
4994  sno = 0.0
4995 !
4996 ! over land
4997 !
4998  if(slianl(i).eq.1.) then
4999  if(scvanl(i).eq.1.0) then
5000  if(tsfanl(i).lt.243.0) then
5001  sno = snwmax
5002  elseif(tsfanl(i).lt.273.0) then
5003  sno = snwmin+(snwmax-snwmin)*(273.0-tsfanl(i))/30.0
5004  else
5005  sno = snwmin
5006  endif
5007  endif
5008 !
5009 ! if glacial points has snow in climatology, set sno to snomax
5010 !
5011  if (.not.landice) then
5012  if(glacir(i).eq.1.0) then
5013  sno = snoclm(i)
5014  if(sno.eq.0.) sno=snwmax
5015  endif
5016  endif
5017  endif
5018 !
5019 ! over sea ice
5020 !
5021 ! snow over sea ice is cycled as of 01/01/94.....hua-lu pan
5022 !
5023  if(slianl(i).eq.2.0) then
5024  sno=snoclm(i)
5025  if(sno.eq.0.) sno=snwmax
5026  endif
5027 !
5028  snoanl(i) = sno
5029  enddo
5030  return
5031  end subroutine snodpth
5032 
5192  subroutine merge(len,lsoil,iy,im,id,ih,fh,deltsfc,
5193  & sihfcs,sicfcs,
5194  & vmnfcs,vmxfcs,slpfcs,absfcs,
5195  & tsffcs,wetfcs,snofcs,zorfcs,albfcs,aisfcs,
5196  & cvfcs ,cvbfcs,cvtfcs,
5197  & cnpfcs,smcfcs,stcfcs,slifcs,vegfcs,
5198  & vetfcs,sotfcs,alffcs,
5199  & sihanl,sicanl,
5200  & vmnanl,vmxanl,slpanl,absanl,
5201  & tsfanl,tsfan2,wetanl,snoanl,zoranl,albanl,aisanl,
5202  & cvanl ,cvbanl,cvtanl,
5203  & cnpanl,smcanl,stcanl,slianl,veganl,
5204  & vetanl,sotanl,alfanl,
5205  & ctsfl,calbl,caisl,csnol,csmcl,czorl,cstcl,cvegl,
5206  & ctsfs,calbs,caiss,csnos,csmcs,czors,cstcs,cvegs,
5207  & ccv,ccvb,ccvt,ccnp,cvetl,cvets,csotl,csots,
5208  & calfl,calfs,
5209  & csihl,csihs,csicl,csics,
5210  & cvmnl,cvmns,cvmxl,cvmxs,cslpl,cslps,cabsl,cabss,
5211  & irttsf,irtwet,irtsno,irtzor,irtalb,irtais,
5212  & irttg3,irtscv,irtacn,irtsmc,irtstc,irtveg,
5213  & irtvmn,irtvmx,irtslp,irtabs,
5214  & irtvet,irtsot,irtalf, landice, me)
5215  use machine , only : kind_io8,kind_io4
5216  use sfccyc_module, only : veg_type_landice, soil_type_landice
5217  implicit none
5218  integer k,i,im,id,iy,len,lsoil,ih,irtacn,irtsmc,irtscv,irtais,
5219  & irttg3,irtstc,irtalf,me,irtsot,irtveg,irtvet, irtzor,
5220  & irtalb,irtsno,irttsf,irtwet,j
5221  &, irtvmn,irtvmx,irtslp,irtabs
5222  logical, intent(in) :: landice
5223  real (kind=kind_io8) rvegs,rvets,rzors,raiss,rsnos,rsots,rcnp,
5224  & rcvt,rcv,rcvb,rsnol,rzorl,raisl,ralbl,
5225  & ralfl,rvegl,ralbs,ralfs,rtsfs,rvetl,rsotl,
5226  & qzors,qvegs,qsnos,qalfs,qaiss,qvets,qcvt,
5227  & qcnp,qcvb,qsots,qcv,qaisl,qsnol,qalfl,
5228  & qtsfl,qalbl,qzorl,qtsfs,qalbs,qsotl,qvegl,
5229  & qvetl,rtsfl,calbs,caiss,ctsfs,czorl,cvegl,
5230  & csnos,ccvb,ccvt,ccv,czors,cvegs,caisl,csnol,
5231  & calbl,fh,ctsfl,ccnp,csots,calfl,csotl,cvetl,
5232  & cvets,calfs,deltsfc,
5233  & csihl,csihs,csicl,csics,
5234  & rsihl,rsihs,rsicl,rsics,
5235  & qsihl,qsihs,qsicl,qsics
5236  &, cvmnl,cvmns,cvmxl,cvmxs,cslpl,cslps
5237  &, cabsl,cabss,rvmnl,rvmns,rvmxl,rvmxs
5238  &, rslpl,rslps,rabsl,rabss,qvmnl,qvmns
5239  &, qvmxl,qvmxs,qslpl,qslps,qabsl,qabss
5240 !
5241  real (kind=kind_io8) tsffcs(len), wetfcs(len), snofcs(len),
5242  & zorfcs(len), albfcs(len,4), aisfcs(len),
5243  & cvfcs(len), cvbfcs(len), cvtfcs(len),
5244  & cnpfcs(len),
5245  & smcfcs(len,lsoil),stcfcs(len,lsoil),
5246  & slifcs(len), vegfcs(len),
5247  & vetfcs(len), sotfcs(len), alffcs(len,2)
5248  &, sihfcs(len), sicfcs(len)
5249  &, vmnfcs(len),vmxfcs(len),slpfcs(len),absfcs(len)
5250  real (kind=kind_io8) tsfanl(len),tsfan2(len),
5251  & wetanl(len),snoanl(len),
5252  & zoranl(len), albanl(len,4), aisanl(len),
5253  & cvanl(len), cvbanl(len), cvtanl(len),
5254  & cnpanl(len),
5255  & smcanl(len,lsoil),stcanl(len,lsoil),
5256  & slianl(len), veganl(len),
5257  & vetanl(len), sotanl(len), alfanl(len,2)
5258  &, sihanl(len),sicanl(len)
5259  &, vmnanl(len),vmxanl(len),slpanl(len),absanl(len)
5260 !
5261  real (kind=kind_io8) csmcl(lsoil), csmcs(lsoil),
5262  & cstcl(lsoil), cstcs(lsoil)
5263  real (kind=kind_io8) rsmcl(lsoil), rsmcs(lsoil),
5264  & rstcl(lsoil), rstcs(lsoil)
5265  real (kind=kind_io8) qsmcl(lsoil), qsmcs(lsoil),
5266  & qstcl(lsoil), qstcs(lsoil)
5267  logical first
5268  integer num_threads
5269  data first /.true./
5270  save num_threads, first
5271 !
5272  integer len_thread_m, i1_t, i2_t, it
5273  integer num_parthds
5274 !
5275  if (first) then
5276  num_threads = num_parthds()
5277  first = .false.
5278  endif
5279 !
5280 ! coeeficients of blending forecast and interpolated clim
5281 ! (or analyzed) fields over sea or land(l) (not for clouds)
5282 ! 1.0 = use of forecast
5283 ! 0.0 = replace with interpolated analysis
5284 !
5285 ! merging coefficients are defined by parameter statement in calling program
5286 ! and therefore they should not be modified in this program.
5287 !
5288  rtsfl = ctsfl
5289  ralbl = calbl
5290  ralfl = calfl
5291  raisl = caisl
5292  rsnol = csnol
5293 !clu rsmcl = csmcl
5294  rzorl = czorl
5295  rvegl = cvegl
5296  rvetl = cvetl
5297  rsotl = csotl
5298  rsihl = csihl
5299  rsicl = csicl
5300  rvmnl = cvmnl
5301  rvmxl = cvmxl
5302  rslpl = cslpl
5303  rabsl = cabsl
5304 !
5305  rtsfs = ctsfs
5306  ralbs = calbs
5307  ralfs = calfs
5308  raiss = caiss
5309  rsnos = csnos
5310 ! rsmcs = csmcs
5311  rzors = czors
5312  rvegs = cvegs
5313  rvets = cvets
5314  rsots = csots
5315  rsihs = csihs
5316  rsics = csics
5317  rvmns = cvmns
5318  rvmxs = cvmxs
5319  rslps = cslps
5320  rabss = cabss
5321 !
5322  rcv = ccv
5323  rcvb = ccvb
5324  rcvt = ccvt
5325  rcnp = ccnp
5326 !
5327  do k=1,lsoil
5328  rsmcl(k) = csmcl(k)
5329  rsmcs(k) = csmcs(k)
5330  rstcl(k) = cstcl(k)
5331  rstcs(k) = cstcs(k)
5332  enddo
5333  if (fh-deltsfc < -0.001 .and. irttsf == 1) then
5334  rtsfs = 1.0
5335  rtsfl = 1.0
5336 ! do k=1,lsoil
5337 ! rsmcl(k) = 1.0
5338 ! rsmcs(k) = 1.0
5339 ! rstcl(k) = 1.0
5340 ! rstcs(k) = 1.0
5341 ! enddo
5342  endif
5343 !
5344 ! if analysis file name is given but no matching analysis date found,
5345 ! use guess (these are flagged by irt???=1).
5346 !
5347  if(irttsf == -1) then
5348  rtsfl = 1.
5349  rtsfs = 1.
5350  endif
5351  if(irtalb == -1) then
5352  ralbl = 1.
5353  ralbs = 1.
5354  ralfl = 1.
5355  ralfs = 1.
5356  endif
5357  if(irtais == -1) then
5358  raisl = 1.
5359  raiss = 1.
5360  endif
5361  if(irtsno == -1 .or. irtscv == -1) then
5362  rsnol = 1.
5363  rsnos = 1.
5364  endif
5365  if(irtsmc == -1 .or. irtwet == -1) then
5366 ! rsmcl = 1.
5367 ! rsmcs = 1.
5368  do k=1,lsoil
5369  rsmcl(k) = 1.
5370  rsmcs(k) = 1.
5371  enddo
5372  endif
5373  if(irtstc.eq.-1) then
5374  do k=1,lsoil
5375  rstcl(k) = 1.
5376  rstcs(k) = 1.
5377  enddo
5378  endif
5379  if(irtzor == -1) then
5380  rzorl = 1.
5381  rzors = 1.
5382  endif
5383  if(irtveg == -1) then
5384  rvegl = 1.
5385  rvegs = 1.
5386  endif
5387  if(irtvet.eq.-1) then
5388  rvetl = 1.
5389  rvets = 1.
5390  endif
5391  if(irtsot == -1) then
5392  rsotl = 1.
5393  rsots = 1.
5394  endif
5395 
5396  if(irtacn == -1) then
5397  rsicl = 1.
5398  rsics = 1.
5399  endif
5400  if(irtvmn == -1) then
5401  rvmnl = 1.
5402  rvmns = 1.
5403  endif
5404  if(irtvmx == -1) then
5405  rvmxl = 1.
5406  rvmxs = 1.
5407  endif
5408  if(irtslp == -1) then
5409  rslpl = 1.
5410  rslps = 1.
5411  endif
5412  if(irtabs == -1) then
5413  rabsl = 1.
5414  rabss = 1.
5415  endif
5416 !
5417  if(raiss == 1. .or. irtacn == -1) then
5418  if (me == 0) print *,'use forecast land-sea-ice mask'
5419  do i = 1, len
5420  aisanl(i) = aisfcs(i)
5421  slianl(i) = slifcs(i)
5422  enddo
5423  endif
5424 !
5425  if (me == 0) then
5426  write(6,100) rtsfl,ralbl,raisl,rsnol,rsmcl,rzorl,rvegl
5427  100 format('rtsfl,ralbl,raisl,rsnol,rsmcl,rzorl,rvegl=',10f7.3)
5428  write(6,101) rtsfs,ralbs,raiss,rsnos,rsmcs,rzors,rvegs
5429  101 format('rtsfs,ralbs,raiss,rsnos,rsmcs,rzors,rvegs=',10f7.3)
5430 ! print *,' ralfl=',ralfl,' ralfs=',ralfs,' rsotl=',rsotl
5431 ! *,' rsots=',rsots,' rvetl=',rvetl,' rvets=',rvets
5432  endif
5433 !
5434  qtsfl = 1. - rtsfl
5435  qalbl = 1. - ralbl
5436  qalfl = 1. - ralfl
5437  qaisl = 1. - raisl
5438  qsnol = 1. - rsnol
5439 ! qsmcl = 1. - rsmcl
5440  qzorl = 1. - rzorl
5441  qvegl = 1. - rvegl
5442  qvetl = 1. - rvetl
5443  qsotl = 1. - rsotl
5444  qsihl = 1. - rsihl
5445  qsicl = 1. - rsicl
5446  qvmnl = 1. - rvmnl
5447  qvmxl = 1. - rvmxl
5448  qslpl = 1. - rslpl
5449  qabsl = 1. - rabsl
5450 !
5451  qtsfs = 1. - rtsfs
5452  qalbs = 1. - ralbs
5453  qalfs = 1. - ralfs
5454  qaiss = 1. - raiss
5455  qsnos = 1. - rsnos
5456 ! qsmcs = 1. - rsmcs
5457  qzors = 1. - rzors
5458  qvegs = 1. - rvegs
5459  qvets = 1. - rvets
5460  qsots = 1. - rsots
5461  qsihs = 1. - rsihs
5462  qsics = 1. - rsics
5463  qvmns = 1. - rvmns
5464  qvmxs = 1. - rvmxs
5465  qslps = 1. - rslps
5466  qabss = 1. - rabss
5467 !
5468  qcv = 1. - rcv
5469  qcvb = 1. - rcvb
5470  qcvt = 1. - rcvt
5471  qcnp = 1. - rcnp
5472 !
5473  do k=1,lsoil
5474  qsmcl(k) = 1. - rsmcl(k)
5475  qsmcs(k) = 1. - rsmcs(k)
5476  qstcl(k) = 1. - rstcl(k)
5477  qstcs(k) = 1. - rstcs(k)
5478  enddo
5479 !
5480 ! merging
5481 !
5482  if(me .eq. 0) then
5483  print *, 'dbgx-- csmcl:', (csmcl(k),k=1,lsoil)
5484  print *, 'dbgx-- rsmcl:', (rsmcl(k),k=1,lsoil)
5485  print *, 'dbgx-- csnol, csnos:',csnol,csnos
5486  print *, 'dbgx-- rsnol, rsnos:',rsnol,rsnos
5487  endif
5488 
5489 ! print *, rtsfs, qtsfs, raiss , qaiss
5490 ! *, rsnos , qsnos, rzors , qzors, rvegs , qvegs
5491 ! *, rvets , qvets, rsots , qsots
5492 ! *, rcv, rcvb, rcvt, qcv, qcvb, qcvt
5493 ! *, ralbs, qalbs, ralfs, qalfs
5494 ! print *, rtsfl, qtsfl, raisl , qaisl
5495 ! *, rsnol , qsnol, rzorl , qzorl, rvegl , qvegl
5496 ! *, rvetl , qvetl, rsotl , qsotl
5497 ! *, ralbl, qalbl, ralfl, qalfl
5498 !
5499 !
5500  len_thread_m = (len+num_threads-1) / num_threads
5501 
5502 !$omp parallel do private(i1_t,i2_t,it,i)
5503  do it=1,num_threads ! start of threaded loop ...................
5504  i1_t = (it-1)*len_thread_m+1
5505  i2_t = min(i1_t+len_thread_m-1,len)
5506  do i=i1_t,i2_t
5507  if(slianl(i).eq.0.) then
5508  vetanl(i) = vetfcs(i)*rvets + vetanl(i)*qvets
5509  sotanl(i) = sotfcs(i)*rsots + sotanl(i)*qsots
5510  else
5511  vetanl(i) = vetfcs(i)*rvetl + vetanl(i)*qvetl
5512  sotanl(i) = sotfcs(i)*rsotl + sotanl(i)*qsotl
5513  endif
5514  enddo
5515  enddo
5516 !$omp end parallel do
5517 !
5518 !$omp parallel do private(i1_t,i2_t,it,i,k)
5519 !
5520  do it=1,num_threads ! start of threaded loop ...................
5521  i1_t = (it-1)*len_thread_m+1
5522  i2_t = min(i1_t+len_thread_m-1,len)
5523 !
5524  do i=i1_t,i2_t
5525  if(slianl(i).eq.0.) then
5526 !.... tsffc2 is the previous anomaly + today's climatology
5527 ! tsffc2 = (tsffcs(i)-tsfan2(i))+tsfanl(i)
5528 ! tsfanl(i) = tsffc2 *rtsfs+tsfanl(i)*qtsfs
5529 !
5530  tsfanl(i) = tsffcs(i)*rtsfs + tsfanl(i)*qtsfs
5531 ! albanl(i) = albfcs(i)*ralbs + albanl(i)*qalbs
5532  aisanl(i) = aisfcs(i)*raiss + aisanl(i)*qaiss
5533  snoanl(i) = snofcs(i)*rsnos + snoanl(i)*qsnos
5534 
5535  zoranl(i) = zorfcs(i)*rzors + zoranl(i)*qzors
5536  veganl(i) = vegfcs(i)*rvegs + veganl(i)*qvegs
5537  sihanl(i) = sihfcs(i)*rsihs + sihanl(i)*qsihs
5538  sicanl(i) = sicfcs(i)*rsics + sicanl(i)*qsics
5539  vmnanl(i) = vmnfcs(i)*rvmns + vmnanl(i)*qvmns
5540  vmxanl(i) = vmxfcs(i)*rvmxs + vmxanl(i)*qvmxs
5541  slpanl(i) = slpfcs(i)*rslps + slpanl(i)*qslps
5542  absanl(i) = absfcs(i)*rabss + absanl(i)*qabss
5543  else
5544  tsfanl(i) = tsffcs(i)*rtsfl + tsfanl(i)*qtsfl
5545 ! albanl(i) = albfcs(i)*ralbl + albanl(i)*qalbl
5546  aisanl(i) = aisfcs(i)*raisl + aisanl(i)*qaisl
5547  if(rsnol.ge.0)then
5548  snoanl(i) = snofcs(i)*rsnol + snoanl(i)*qsnol
5549  else ! envelope method
5550  if(snoanl(i).ne.0)then
5551  snoanl(i) = max(-snoanl(i)/rsnol,
5552  & min(-snoanl(i)*rsnol, snofcs(i)))
5553  endif
5554  endif
5555  zoranl(i) = zorfcs(i)*rzorl + zoranl(i)*qzorl
5556  veganl(i) = vegfcs(i)*rvegl + veganl(i)*qvegl
5557  vmnanl(i) = vmnfcs(i)*rvmnl + vmnanl(i)*qvmnl
5558  vmxanl(i) = vmxfcs(i)*rvmxl + vmxanl(i)*qvmxl
5559  slpanl(i) = slpfcs(i)*rslpl + slpanl(i)*qslpl
5560  absanl(i) = absfcs(i)*rabsl + absanl(i)*qabsl
5561  sihanl(i) = sihfcs(i)*rsihl + sihanl(i)*qsihl
5562  sicanl(i) = sicfcs(i)*rsicl + sicanl(i)*qsicl
5563  endif
5564 
5565  cnpanl(i) = cnpfcs(i)*rcnp + cnpanl(i)*qcnp
5566 !
5567 ! snow over sea ice is cycled
5568 !
5569  if(slianl(i).eq.2.) then
5570  snoanl(i) = snofcs(i)
5571  endif
5572 !
5573  enddo
5574 
5575 ! at landice points, set the soil type, slope type and
5576 ! greenness fields to flag values.
5577 
5578  if (landice) then
5579  do i=i1_t,i2_t
5580  if (nint(slianl(i)) == 1) then
5581  if (nint(vetanl(i)) == veg_type_landice) then
5582  sotanl(i) = soil_type_landice
5583  veganl(i) = 0.0
5584  slpanl(i) = 9.0
5585  vmnanl(i) = 0.0
5586  vmxanl(i) = 0.0
5587  endif
5588  end if ! if land
5589  enddo
5590  endif
5591 
5592  do i=i1_t,i2_t
5593  cvanl(i) = cvfcs(i)*rcv + cvanl(i)*qcv
5594  cvbanl(i) = cvbfcs(i)*rcvb + cvbanl(i)*qcvb
5595  cvtanl(i) = cvtfcs(i)*rcvt + cvtanl(i)*qcvt
5596  enddo
5597 !
5598  do k = 1, 4
5599  do i=i1_t,i2_t
5600  if(slianl(i).eq.0.) then
5601  albanl(i,k) = albfcs(i,k)*ralbs + albanl(i,k)*qalbs
5602  else
5603  albanl(i,k) = albfcs(i,k)*ralbl + albanl(i,k)*qalbl
5604  endif
5605  enddo
5606  enddo
5607 !
5608  do k = 1, 2
5609  do i=i1_t,i2_t
5610  if(slianl(i).eq.0.) then
5611  alfanl(i,k) = alffcs(i,k)*ralfs + alfanl(i,k)*qalfs
5612  else
5613  alfanl(i,k) = alffcs(i,k)*ralfl + alfanl(i,k)*qalfl
5614  endif
5615  enddo
5616  enddo
5617 !
5618  do k = 1, lsoil
5619  do i=i1_t,i2_t
5620  if(slianl(i).eq.0.) then
5621  smcanl(i,k) = smcfcs(i,k)*rsmcs(k) + smcanl(i,k)*qsmcs(k)
5622  stcanl(i,k) = stcfcs(i,k)*rstcs(k) + stcanl(i,k)*qstcs(k)
5623  else
5624 ! soil moisture not used at landice points, so
5625 ! don't bother merging it. also, for now don't allow nudging
5626 ! to raise subsurface temperature above freezing.
5627  stcanl(i,k) = stcfcs(i,k)*rstcl(k) + stcanl(i,k)*qstcl(k)
5628  if (landice .and. slianl(i) == 1.0 .and.
5629  & nint(vetanl(i)) == veg_type_landice) then
5630  smcanl(i,k) = 1.0 ! use value as flag
5631  stcanl(i,k) = min(stcanl(i,k), 273.15)
5632  else
5633  smcanl(i,k) = smcfcs(i,k)*rsmcl(k) + smcanl(i,k)*qsmcl(k)
5634  end if
5635  endif
5636  enddo
5637  enddo
5638 !
5639  enddo ! end of threaded loop ...................
5640 !$omp end parallel do
5641  return
5642  end subroutine merge
5643 
5676  subroutine newice(slianl,slifcs,tsfanl,tsffcs,len,lsoil,
5677  & sihnew,sicnew,sihanl,sicanl,
5678  & albanl,snoanl,zoranl,smcanl,stcanl,
5679  & albsea,snosea,zorsea,smcsea,smcice,
5680  & tsfmin,tsfice,albice,zorice,tgice,
5681  & rla,rlo,me)
5682 !
5683  use machine , only : kind_io8,kind_io4
5684  implicit none
5685  real (kind=kind_io8), parameter :: one=1.0
5686  real (kind=kind_io8) tgice,albice,zorice,tsfice,albsea,snosea,
5687  & smcice,tsfmin,zorsea,smcsea
5688 !cwu [+1l] add sicnew,sihnew
5689  &, sicnew,sihnew
5690  integer i,me,kount1,kount2,k,len,lsoil
5691  real (kind=kind_io8) slianl(len), slifcs(len),
5692  & tsffcs(len),tsfanl(len)
5693  real (kind=kind_io8) albanl(len,4), snoanl(len), zoranl(len)
5694  real (kind=kind_io8) smcanl(len,lsoil), stcanl(len,lsoil)
5695 !cwu [+1l] add sihanl & sicanl
5696  real (kind=kind_io8) sihanl(len), sicanl(len)
5697 !
5698  real (kind=kind_io8) rla(len), rlo(len)
5699 !
5700  if (me .eq. 0) write(6,*) 'newice'
5701 !
5702  kount1 = 0
5703  kount2 = 0
5704  do i=1,len
5705  if(slifcs(i).ne.slianl(i)) then
5706  if(slifcs(i).eq.1..or.slianl(i).eq.1.) then
5707  print *,'FATAL ERROR: Inconsistency in slifcs or slianl'
5708  print 910,rla(i),rlo(i),slifcs(i),slianl(i),
5709  & tsffcs(i),tsfanl(i)
5710  910 format(2x,'at lat=',f5.1,' lon=',f5.1,' slifcs=',f4.1,
5711  & ' slimsk=',f4.1,' tsffcs=',f5.1,' set to tsfanl=',f5.1)
5712  call abort
5713  endif
5714 !
5715 ! interpolated climatology indicates melted sea ice
5716 !
5717  if(slianl(i).eq.0..and.slifcs(i).eq.2.) then
5718  tsfanl(i) = tsfmin
5719  albanl(i,1) = albsea
5720  albanl(i,2) = albsea
5721  albanl(i,3) = albsea
5722  albanl(i,4) = albsea
5723  snoanl(i) = snosea
5724  zoranl(i) = zorsea
5725  do k = 1, lsoil
5726  smcanl(i,k) = smcsea
5727 !cwu [+1l] set stcanl to tgice (over sea-ice)
5728  stcanl(i,k) = tgice
5729  enddo
5730 !cwu [+2l] set siganl and sicanl
5731  sihanl(i) = 0.
5732  sicanl(i) = 0.
5733  kount1 = kount1 + 1
5734  endif
5735 !
5736 ! interplated climatoloyg/analysis indicates new sea ice
5737 !
5738  if(slianl(i).eq.2..and.slifcs(i).eq.0.) then
5739  tsfanl(i) = tsfice
5740  albanl(i,1) = albice
5741  albanl(i,2) = albice
5742  albanl(i,3) = albice
5743  albanl(i,4) = albice
5744  snoanl(i) = 0.
5745  zoranl(i) = zorice
5746  do k = 1, lsoil
5747  smcanl(i,k) = smcice
5748  stcanl(i,k) = tgice
5749  enddo
5750 !cwu [+2l] add sihanl & sicanl
5751  sihanl(i) = sihnew
5752  sicanl(i) = min(one, max(sicnew,sicanl(i)))
5753  kount2 = kount2 + 1
5754  endif
5755  endif
5756  enddo
5757 !
5758  if (me .eq. 0) then
5759  if(kount1.gt.0) then
5760  write(6,*) 'sea ice melted. tsf,alb,zor are filled',
5761  & ' at ',kount1,' points'
5762  endif
5763  if(kount2.gt.0) then
5764  write(6,*) 'sea ice formed. tsf,alb,zor are filled',
5765  & ' at ',kount2,' points'
5766  endif
5767  endif
5768 !
5769  return
5770  end
5771 
5783  subroutine qcsnow(snoanl,slmask,aisanl,glacir,len,snoval,
5784  & landice,me)
5785  use machine , only : kind_io8,kind_io4
5786  implicit none
5787  integer kount,i,len,me
5788  logical, intent(in) :: landice
5789  real (kind=kind_io8) per,snoval
5790  real (kind=kind_io8) snoanl(len),slmask(len),
5791  & aisanl(len),glacir(len)
5792  if (me .eq. 0) then
5793  write(6,*) ' '
5794  write(6,*) 'qc of snow'
5795  endif
5796  if (.not.landice) then
5797  kount=0
5798  do i=1,len
5799  if(glacir(i).ne.0..and.snoanl(i).eq.0.) then
5800 ! if(glacir(i).ne.0..and.snoanl(i).lt.snoval*0.5) then
5801  snoanl(i) = snoval
5802  kount = kount + 1
5803  endif
5804  enddo
5805  per = float(kount) / float(len)*100.
5806  if(kount.gt.0) then
5807  if (me .eq. 0) then
5808  print *,'snow filled over glacier points at ',kount,
5809  & ' points (',per,'percent)'
5810  endif
5811  endif
5812  endif ! landice check
5813  kount = 0
5814  do i=1,len
5815  if(slmask(i).eq.0.and.aisanl(i).eq.0) then
5816  snoanl(i) = 0.
5817  kount = kount + 1
5818  endif
5819  enddo
5820  per = float(kount) / float(len)*100.
5821  if(kount.gt.0) then
5822  if (me .eq. 0) then
5823  print *,'snow set to zero over open sea at ',kount,
5824  & ' points (',per,'percent)'
5825  endif
5826  endif
5827  return
5828  end subroutine qcsnow
5829 
5844  subroutine qcsice(ais,glacir,amxice,aicice,aicsea,sllnd,slmask,
5845  & rla,rlo,len,me)
5846  use machine , only : kind_io8,kind_io4
5847  implicit none
5848  integer kount1,kount,i,me,len
5849  real (kind=kind_io8) per,aicsea,aicice,sllnd
5850 !
5851  real (kind=kind_io8) ais(len), glacir(len),
5852  & amxice(len), slmask(len)
5853  real (kind=kind_io8) rla(len), rlo(len)
5854 !
5855 ! check sea-ice cover mask against land-sea mask
5856 !
5857  if (me .eq. 0) write(6,*) 'qc of sea ice'
5858  kount = 0
5859  kount1 = 0
5860  do i=1,len
5861  if(ais(i).ne.aicice.and.ais(i).ne.aicsea) then
5862  print *,'FATAL ERROR: sea ice'
5863  print *,'mask not ',aicice,' or ',aicsea
5864  print *,'ais(i),aicice,aicsea,rla(i),rlo(i,=',
5865  & ais(i),aicice,aicsea,rla(i),rlo(i)
5866  call abort
5867  endif
5868  if(slmask(i).eq.0..and.glacir(i).eq.1..and.
5869 ! if(slmask(i).eq.0..and.glacir(i).eq.2..and.
5870  & ais(i).ne.1.) then
5871  kount1 = kount1 + 1
5872  ais(i) = 1.
5873  endif
5874  if(slmask(i).eq.sllnd.and.ais(i).eq.aicice) then
5875  kount = kount + 1
5876  ais(i) = aicsea
5877  endif
5878  enddo
5879 ! enddo
5880  per = float(kount) / float(len)*100.
5881  if(kount.gt.0) then
5882  if(me .eq. 0) then
5883  print *,' sea ice over land mask at ',kount,' points (',per,
5884  & 'percent)'
5885  endif
5886  endif
5887  per = float(kount1) / float(len)*100.
5888  if(kount1.gt.0) then
5889  if(me .eq. 0) then
5890  print *,' sea ice set over glacier points over ocean at ',
5891  & kount1,' points (',per,'percent)'
5892  endif
5893  endif
5894 ! kount=0
5895 ! do j=1,jdim
5896 ! do i=1,idim
5897 ! if(amxice(i,j).ne.0..and.ais(i,j).eq.0.) then
5898 ! ais(i,j)=0.
5899 ! kount=kount+1
5900 ! endif
5901 ! enddo
5902 ! enddo
5903 ! per=float(kount)/float(idim*jdim)*100.
5904 ! if(kount.gt.0) then
5905 ! print *,' sea ice exceeds maxice at ',kount,' points (',per,
5906 ! & 'percent)'
5907 ! endif
5908 !
5909 ! remove isolated open ocean surrounded by sea ice and/or land
5910 !
5911 ! remove isolated open ocean surrounded by sea ice and/or land
5912 !
5913 ! ij = 0
5914 ! do j=1,jdim
5915 ! do i=1,idim
5916 ! ij = ij + 1
5917 ! ip = i + 1
5918 ! im = i - 1
5919 ! jp = j + 1
5920 ! jm = j - 1
5921 ! if(jp.gt.jdim) jp = jdim - 1
5922 ! if(jm.lt.1) jm = 2
5923 ! if(ip.gt.idim) ip = 1
5924 ! if(im.lt.1) im = idim
5925 ! if(slmask(i,j).eq.0..and.ais(i,j).eq.0.) then
5926 ! if((slmask(ip,jp).eq.1..or.ais(ip,jp).eq.1.).and.
5927 ! & (slmask(i ,jp).eq.1..or.ais(i ,jp).eq.1.).and.
5928 ! & (slmask(im,jp).eq.1..or.ais(im,jp).eq.1.).and.
5929 ! & (slmask(ip,j ).eq.1..or.ais(ip,j ).eq.1.).and.
5930 ! & (slmask(im,j ).eq.1..or.ais(im,j ).eq.1.).and.
5931 ! & (slmask(ip,jm).eq.1..or.ais(ip,jm).eq.1.).and.
5932 ! & (slmask(i ,jm).eq.1..or.ais(i ,jm).eq.1.).and.
5933 ! & (slmask(im,jm).eq.1..or.ais(im,jm).eq.1.)) then
5934 ! ais(i,j) = 1.
5935 ! write(6,*) ' isolated open sea point surrounded by',
5936 ! & ' sea ice or land modified to sea ice',
5937 ! & ' at lat=',rla(i,j),' lon=',rlo(i,j)
5938 ! endif
5939 ! endif
5940 ! enddo
5941 ! enddo
5942  return
5943  end
5944 
5953  subroutine setlsi(slmask,aisfld,len,aicice,slifld)
5954 !
5955  use machine , only : kind_io8,kind_io4
5956  implicit none
5957  integer i,len
5958  real (kind=kind_io8) aicice
5959  real (kind=kind_io8) slmask(len), slifld(len), aisfld(len)
5960 !
5961 ! set surface condition indicator slimsk
5962 !
5963  do i=1,len
5964  slifld(i) = slmask(i)
5965 ! if(aisfld(i).eq.aicice) slifld(i) = 2.0
5966  if(aisfld(i).eq.aicice .and. slmask(i) .eq. 0.0)
5967  & slifld(i) = 2.0
5968  enddo
5969  return
5970  end
5971 
5978  subroutine scale(fld,len,scl)
5979 !
5980  use machine , only : kind_io8,kind_io4
5981  implicit none
5982  integer i,len
5983  real (kind=kind_io8) fld(len),scl
5984  do i=1,len
5985  fld(i) = fld(i) * scl
5986  enddo
5987  return
5988  end
5989 
6021  subroutine qcmxmn(ttl,fld,slimsk,sno,iceflg,
6022  & fldlmx,fldlmn,fldomx,fldomn,fldimx,fldimn,
6023  & fldjmx,fldjmn,fldsmx,fldsmn,epsfld,
6024  & rla,rlo,len,mode,percrit,lgchek,me)
6025 !
6026  use machine , only : kind_io8,kind_io4
6027  implicit none
6028  real (kind=kind_io8) permax,per,fldimx,fldimn,fldjmx,fldomn,
6029  & fldlmx,fldlmn,fldomx,fldjmn,percrit,
6030  & fldsmx,fldsmn,epsfld
6031  integer kmaxi,kmini,kmaxj,kmino,kmaxl,kminl,kmaxo,mmprt,kminj,
6032  & ij,nprt,kmaxs,kmins,i,me,len,mode
6033  parameter(mmprt=2)
6034 !
6035  character*8 ttl
6036  logical iceflg(len)
6037  real (kind=kind_io8) fld(len),slimsk(len),sno(len),
6038  & rla(len), rlo(len)
6039  integer iwk(len)
6040  logical lgchek
6041 !
6042  logical first
6043  integer num_threads
6044  data first /.true./
6045  save num_threads, first
6046 !
6047  integer len_thread_m, i1_t, i2_t, it
6048  integer num_parthds
6049 !
6050  if (first) then
6051  num_threads = num_parthds()
6052  first = .false.
6053  endif
6054 !
6055 ! check against land-sea mask and ice cover mask
6056 !
6057  if(me .eq. 0) then
6058 ! print *,' '
6059  print *,'performing qc of ',ttl,' mode=',mode,
6060  & '(0=count only, 1=replace)'
6061  endif
6062 !
6063  len_thread_m = (len+num_threads-1) / num_threads
6064  kmaxl = 0
6065  kminl = 0
6066  kmaxo = 0
6067  kmino = 0
6068  kmaxi = 0
6069  kmini = 0
6070  kmaxj = 0
6071  kminj = 0
6072  kmaxs = 0
6073  kmins = 0
6074 !$omp parallel do private(i1_t,i2_t,it,i)
6075 !$omp+private(nprt,ij,iwk)
6076 !$omp+reduction(+:kmaxs,kmins,kmaxl,kminl,kmaxo)
6077 !$omp+reduction(+:kmino,kmaxi,kmini,kmaxj,kminj)
6078 !$omp+shared(mode,epsfld)
6079 !$omp+shared(fldlmx,fldlmn,fldomx,fldjmn,fldsmx,fldsmn)
6080 !$omp+shared(fld,slimsk,sno,rla,rlo)
6081  do it=1,num_threads ! start of threaded loop
6082  i1_t = (it-1)*len_thread_m+1
6083  i2_t = min(i1_t+len_thread_m-1,len)
6084 !
6085 !
6086 !
6087 ! lower bound check over bare land
6088 !
6089  if (fldlmn .ne. 999.0) then
6090  do i=i1_t,i2_t
6091  if(slimsk(i).eq.1..and.sno(i).le.0..and.
6092  & fld(i).lt.fldlmn-epsfld) then
6093  kminl=kminl+1
6094  iwk(kminl) = i
6095  endif
6096  enddo
6097  if(me == 0 . and. it == 1 .and. num_threads == 1) then
6098  nprt = min(mmprt,kminl)
6099  do i=1,nprt
6100  ij = iwk(i)
6101  print 8001,rla(ij),rlo(ij),fld(ij),fldlmn
6102  8001 format(' bare land min. check. lat=',f5.1,
6103  & ' lon=',f6.1,' fld=',e13.6, ' to ',e13.6)
6104  enddo
6105  endif
6106  if (mode .eq. 1) then
6107  do i=1,kminl
6108  fld(iwk(i)) = fldlmn
6109  enddo
6110  endif
6111  endif
6112 !
6113 ! upper bound check over bare land
6114 !
6115  if (fldlmx .ne. 999.0) then
6116  do i=i1_t,i2_t
6117  if(slimsk(i).eq.1..and.sno(i).le.0..and.
6118  & fld(i).gt.fldlmx+epsfld) then
6119  kmaxl=kmaxl+1
6120  iwk(kmaxl) = i
6121  endif
6122  enddo
6123  if(me == 0 . and. it == 1 .and. num_threads == 1) then
6124  nprt = min(mmprt,kmaxl)
6125  do i=1,nprt
6126  ij = iwk(i)
6127  print 8002,rla(ij),rlo(ij),fld(ij),fldlmx
6128  8002 format(' bare land max. check. lat=',f5.1,
6129  & ' lon=',f6.1,' fld=',e13.6, ' to ',e13.6)
6130  enddo
6131  endif
6132  if (mode .eq. 1) then
6133  do i=1,kmaxl
6134  fld(iwk(i)) = fldlmx
6135  enddo
6136  endif
6137  endif
6138 !
6139 ! lower bound check over snow covered land
6140 !
6141  if (fldsmn .ne. 999.0) then
6142  do i=i1_t,i2_t
6143  if(slimsk(i).eq.1..and.sno(i).gt.0..and.
6144  & fld(i).lt.fldsmn-epsfld) then
6145  kmins=kmins+1
6146  iwk(kmins) = i
6147  endif
6148  enddo
6149  if(me == 0 . and. it == 1 .and. num_threads == 1) then
6150  nprt = min(mmprt,kmins)
6151  do i=1,nprt
6152  ij = iwk(i)
6153  print 8003,rla(ij),rlo(ij),fld(ij),fldsmn
6154  8003 format(' sno covrd land min. check. lat=',f5.1,
6155  & ' lon=',f6.1,' fld=',e11.4, ' to ',e11.4)
6156  enddo
6157  endif
6158  if (mode .eq. 1) then
6159  do i=1,kmins
6160  fld(iwk(i)) = fldsmn
6161  enddo
6162  endif
6163  endif
6164 !
6165 ! upper bound check over snow covered land
6166 !
6167  if (fldsmx .ne. 999.0) then
6168  do i=i1_t,i2_t
6169  if(slimsk(i).eq.1..and.sno(i).gt.0..and.
6170  & fld(i).gt.fldsmx+epsfld) then
6171  kmaxs=kmaxs+1
6172  iwk(kmaxs) = i
6173  endif
6174  enddo
6175  if(me == 0 . and. it == 1 .and. num_threads == 1) then
6176  nprt = min(mmprt,kmaxs)
6177  do i=1,nprt
6178  ij = iwk(i)
6179  print 8004,rla(ij),rlo(ij),fld(ij),fldsmx
6180  8004 format(' snow land max. check. lat=',f5.1,
6181  & ' lon=',f6.1,' fld=',e11.4, ' to ',e11.4)
6182  enddo
6183  endif
6184  if (mode .eq. 1) then
6185  do i=1,kmaxs
6186  fld(iwk(i)) = fldsmx
6187  enddo
6188  endif
6189  endif
6190 !
6191 ! lower bound check over open ocean
6192 !
6193  if (fldomn .ne. 999.0) then
6194  do i=i1_t,i2_t
6195  if(slimsk(i).eq.0..and.
6196  & fld(i).lt.fldomn-epsfld) then
6197  kmino=kmino+1
6198  iwk(kmino) = i
6199  endif
6200  enddo
6201  if(me == 0 . and. it == 1 .and. num_threads == 1) then
6202  nprt = min(mmprt,kmino)
6203  do i=1,nprt
6204  ij = iwk(i)
6205  print 8005,rla(ij),rlo(ij),fld(ij),fldomn
6206  8005 format(' open ocean min. check. lat=',f5.1,
6207  & ' lon=',f6.1,' fld=',e11.4,' to ',e11.4)
6208  enddo
6209  endif
6210  if (mode .eq. 1) then
6211  do i=1,kmino
6212  fld(iwk(i)) = fldomn
6213  enddo
6214  endif
6215  endif
6216 !
6217 ! upper bound check over open ocean
6218 !
6219  if (fldomx .ne. 999.0) then
6220  do i=i1_t,i2_t
6221  if(fldomx.ne.999..and.slimsk(i).eq.0..and.
6222  & fld(i).gt.fldomx+epsfld) then
6223  kmaxo=kmaxo+1
6224  iwk(kmaxo) = i
6225  endif
6226  enddo
6227  if(me == 0 . and. it == 1 .and. num_threads == 1) then
6228  nprt = min(mmprt,kmaxo)
6229  do i=1,nprt
6230  ij = iwk(i)
6231  print 8006,rla(ij),rlo(ij),fld(ij),fldomx
6232  8006 format(' open ocean max. check. lat=',f5.1,
6233  & ' lon=',f6.1,' fld=',e11.4, ' to ',e11.4)
6234  enddo
6235  endif
6236  if (mode .eq. 1) then
6237  do i=1,kmaxo
6238  fld(iwk(i)) = fldomx
6239  enddo
6240  endif
6241  endif
6242 !
6243 ! lower bound check over sea ice without snow
6244 !
6245  if (fldimn .ne. 999.0) then
6246  do i=i1_t,i2_t
6247  if(slimsk(i).eq.2..and.sno(i).le.0..and.
6248  & fld(i).lt.fldimn-epsfld) then
6249  kmini=kmini+1
6250  iwk(kmini) = i
6251  endif
6252  enddo
6253  if(me == 0 . and. it == 1 .and. num_threads == 1) then
6254  nprt = min(mmprt,kmini)
6255  do i=1,nprt
6256  ij = iwk(i)
6257  print 8007,rla(ij),rlo(ij),fld(ij),fldimn
6258  8007 format(' seaice no snow min. check lat=',f5.1,
6259  & ' lon=',f6.1,' fld=',e11.4, ' to ',e11.4)
6260  enddo
6261  endif
6262  if (mode .eq. 1) then
6263  do i=1,kmini
6264  fld(iwk(i)) = fldimn
6265  enddo
6266  endif
6267  endif
6268 !
6269 ! upper bound check over sea ice without snow
6270 !
6271  if (fldimx .ne. 999.0) then
6272  do i=i1_t,i2_t
6273  if(slimsk(i).eq.2..and.sno(i).le.0..and.
6274  & fld(i).gt.fldimx+epsfld .and. iceflg(i)) then
6275 ! & fld(i).gt.fldimx+epsfld) then
6276  kmaxi=kmaxi+1
6277  iwk(kmaxi) = i
6278  endif
6279  enddo
6280  if(me == 0 . and. it == 1 .and. num_threads == 1) then
6281  nprt = min(mmprt,kmaxi)
6282  do i=1,nprt
6283  ij = iwk(i)
6284  print 8008,rla(ij),rlo(ij),fld(ij),fldimx
6285  8008 format(' seaice no snow max. check lat=',f5.1,
6286  & ' lon=',f6.1,' fld=',e11.4, ' to ',e11.4)
6287  enddo
6288  endif
6289  if (mode .eq. 1) then
6290  do i=1,kmaxi
6291  fld(iwk(i)) = fldimx
6292  enddo
6293  endif
6294  endif
6295 !
6296 ! lower bound check over sea ice with snow
6297 !
6298  if (fldjmn .ne. 999.0) then
6299  do i=i1_t,i2_t
6300  if(slimsk(i).eq.2..and.sno(i).gt.0..and.
6301  & fld(i).lt.fldjmn-epsfld) then
6302  kminj=kminj+1
6303  iwk(kminj) = i
6304  endif
6305  enddo
6306  if(me == 0 . and. it == 1 .and. num_threads == 1) then
6307  nprt = min(mmprt,kminj)
6308  do i=1,nprt
6309  ij = iwk(i)
6310  print 8009,rla(ij),rlo(ij),fld(ij),fldjmn
6311  8009 format(' sea ice snow min. check lat=',f5.1,
6312  & ' lon=',f6.1,' fld=',e11.4, ' to ',e11.4)
6313  enddo
6314  endif
6315  if (mode .eq. 1) then
6316  do i=1,kminj
6317  fld(iwk(i)) = fldjmn
6318  enddo
6319  endif
6320  endif
6321 !
6322 ! upper bound check over sea ice with snow
6323 !
6324  if (fldjmx .ne. 999.0) then
6325  do i=i1_t,i2_t
6326  if(slimsk(i).eq.2..and.sno(i).gt.0..and.
6327  & fld(i).gt.fldjmx+epsfld .and. iceflg(i)) then
6328 ! & fld(i).gt.fldjmx+epsfld) then
6329  kmaxj=kmaxj+1
6330  iwk(kmaxj) = i
6331  endif
6332  enddo
6333  if(me == 0 .and. it == 1 .and. num_threads == 1) then
6334  nprt = min(mmprt,kmaxj)
6335  do i=1,nprt
6336  ij = iwk(i)
6337  print 8010,rla(ij),rlo(ij),fld(ij),fldjmx
6338  8010 format(' seaice snow max check lat=',f5.1,
6339  & ' lon=',f6.1,' fld=',e11.4, ' to ',e11.4)
6340  enddo
6341  endif
6342  if (mode .eq. 1) then
6343  do i=1,kmaxj
6344  fld(iwk(i)) = fldjmx
6345  enddo
6346  endif
6347  endif
6348  enddo ! end of threaded loop
6349 !$omp end parallel do
6350 !
6351 ! print results
6352 !
6353  if(me .eq. 0) then
6354 ! write(6,*) 'summary of qc'
6355  permax=0.
6356  if(kminl.gt.0) then
6357  per=float(kminl)/float(len)*100.
6358  print 9001,fldlmn,kminl,per
6359  9001 format(' bare land min check. modified to ',f8.1,
6360  & ' at ',i5,' points ',f8.1,'percent')
6361  if(per.gt.permax) permax=per
6362  endif
6363  if(kmaxl.gt.0) then
6364  per=float(kmaxl)/float(len)*100.
6365  print 9002,fldlmx,kmaxl,per
6366  9002 format(' bare land max check. modified to ',f8.1,
6367  & ' at ',i5,' points ',f4.1,'percent')
6368  if(per.gt.permax) permax=per
6369  endif
6370  if(kmino.gt.0) then
6371  per=float(kmino)/float(len)*100.
6372  print 9003,fldomn,kmino,per
6373  9003 format(' open ocean min check. modified to ',f8.1,
6374  & ' at ',i5,' points ',f4.1,'percent')
6375  if(per.gt.permax) permax=per
6376  endif
6377  if(kmaxo.gt.0) then
6378  per=float(kmaxo)/float(len)*100.
6379  print 9004,fldomx,kmaxo,per
6380  9004 format(' open sea max check. modified to ',f8.1,
6381  & ' at ',i5,' points ',f4.1,'percent')
6382  if(per.gt.permax) permax=per
6383  endif
6384  if(kmins.gt.0) then
6385  per=float(kmins)/float(len)*100.
6386  print 9009,fldsmn,kmins,per
6387  9009 format(' snow covered land min check. modified to ',f8.1,
6388  & ' at ',i5,' points ',f4.1,'percent')
6389  if(per.gt.permax) permax=per
6390  endif
6391  if(kmaxs.gt.0) then
6392  per=float(kmaxs)/float(len)*100.
6393  print 9010,fldsmx,kmaxs,per
6394  9010 format(' snow covered land max check. modified to ',f8.1,
6395  & ' at ',i5,' points ',f4.1,'percent')
6396  if(per.gt.permax) permax=per
6397  endif
6398  if(kmini.gt.0) then
6399  per=float(kmini)/float(len)*100.
6400  print 9005,fldimn,kmini,per
6401  9005 format(' bare ice min check. modified to ',f8.1,
6402  & ' at ',i5,' points ',f4.1,'percent')
6403  if(per.gt.permax) permax=per
6404  endif
6405  if(kmaxi.gt.0) then
6406  per=float(kmaxi)/float(len)*100.
6407  print 9006,fldimx,kmaxi,per
6408  9006 format(' bare ice max check. modified to ',f8.1,
6409  & ' at ',i5,' points ',f4.1,'percent')
6410  if(per.gt.permax) permax=per
6411  endif
6412  if(kminj.gt.0) then
6413  per=float(kminj)/float(len)*100.
6414  print 9007,fldjmn,kminj,per
6415  9007 format(' snow covered ice min check. modified to ',f8.1,
6416  & ' at ',i5,' points ',f4.1,'percent')
6417  if(per.gt.permax) permax=per
6418  endif
6419  if(kmaxj.gt.0) then
6420  per=float(kmaxj)/float(len)*100.
6421  print 9008,fldjmx,kmaxj,per
6422  9008 format(' snow covered ice max check. modified to ',f8.1,
6423  & ' at ',i5,' points ',f4.1,'percent')
6424  if(per.gt.permax) permax=per
6425  endif
6426 ! commented on 06/30/99 -- moorthi
6427 ! if(lgchek) then
6428 ! if(permax.gt.percrit) then
6429 ! write(6,*) ' too many bad points. aborting ....'
6430 ! call abort
6431 ! endif
6432 ! endif
6433 !
6434  endif
6435 !
6436  return
6437  end
6438 
6445  subroutine setzro(fld,eps,len)
6446 !
6447  use machine , only : kind_io8,kind_io4
6448  implicit none
6449  integer i,len
6450  real (kind=kind_io8) fld(len),eps
6451  do i=1,len
6452  if(abs(fld(i)).lt.eps) fld(i) = 0.
6453  enddo
6454  return
6455  end
6456 
6463  subroutine getscv(snofld,scvfld,len)
6464 !
6465  use machine , only : kind_io8,kind_io4
6466  implicit none
6467  integer i,len
6468  real (kind=kind_io8) snofld(len),scvfld(len)
6469 !
6470  do i=1,len
6471  scvfld(i) = 0.
6472  if(snofld(i).gt.0.) scvfld(i) = 1.
6473  enddo
6474  return
6475  end
6476 
6487  subroutine getstc(tsffld,tg3fld,slifld,len,lsoil,stcfld,tsfimx)
6488 !
6489  use machine , only : kind_io8,kind_io4
6490  implicit none
6491  integer k,i,len,lsoil
6492  real (kind=kind_io8) factor,tsfimx
6493  real (kind=kind_io8) tsffld(len), tg3fld(len), slifld(len)
6494  real (kind=kind_io8) stcfld(len,lsoil)
6495 !
6496 ! layer soil temperature
6497 !
6498  do k = 1, lsoil
6499  do i = 1, len
6500  if(slifld(i).eq.1.0) then
6501  factor = ((k-1) * 2 + 1) / (2. * lsoil)
6502  stcfld(i,k) = factor*tg3fld(i)+(1.-factor)*tsffld(i)
6503  elseif(slifld(i).eq.2.0) then
6504  factor = ((k-1) * 2 + 1) / (2. * lsoil)
6505  stcfld(i,k) = factor*tsfimx+(1.-factor)*tsffld(i)
6506  else
6507  stcfld(i,k) = tg3fld(i)
6508  endif
6509  enddo
6510  enddo
6511  if(lsoil.gt.2) then
6512  do k = 3, lsoil
6513  do i = 1, len
6514  stcfld(i,k) = stcfld(i,2)
6515  enddo
6516  enddo
6517  endif
6518  return
6519  end
6520 
6529  subroutine getsmc(wetfld,len,lsoil,smcfld,me)
6530 !
6531  use machine , only : kind_io8,kind_io4
6532  implicit none
6533  integer k,i,len,lsoil,me
6534  real (kind=kind_io8) wetfld(len), smcfld(len,lsoil)
6535 !
6536  if (me .eq. 0) write(6,*) 'getsmc'
6537 !
6538 ! layer soil wetness
6539 !
6540  do k = 1, lsoil
6541  do i = 1, len
6542  smcfld(i,k) = (wetfld(i)*1000./150.)*.37 + .1
6543  enddo
6544  enddo
6545  return
6546  end
6547 
6560  subroutine usesgt(sig1t,slianl,tg3anl,len,lsoil,tsfanl,stcanl,
6561  & tsfimx)
6562 !
6563  use machine , only : kind_io8,kind_io4
6564  implicit none
6565  integer i,len,lsoil
6566  real (kind=kind_io8) tsfimx
6567  real (kind=kind_io8) sig1t(len), slianl(len), tg3anl(len)
6568  real (kind=kind_io8) tsfanl(len), stcanl(len,lsoil)
6569 !
6570 ! soil temperature
6571 !
6572  if(sig1t(1).gt.0.) then
6573  do i=1,len
6574  if(slianl(i).ne.0.) then
6575  tsfanl(i) = sig1t(i)
6576  endif
6577  enddo
6578  endif
6579  call getstc(tsfanl,tg3anl,slianl,len,lsoil,stcanl,tsfimx)
6580 !
6581  return
6582  end
6583 
6593  subroutine snosfc(snoanl,tsfanl,tsfsmx,len,me)
6594  use machine , only : kind_io8,kind_io4
6595  implicit none
6596  integer kount,i,len,me
6597  real (kind=kind_io8) per,tsfsmx
6598  real (kind=kind_io8) snoanl(len), tsfanl(len)
6599 !
6600  if (me .eq. 0) write(6,*) 'set snow temp to tsfsmx if greater'
6601  kount=0
6602  do i=1,len
6603  if(snoanl(i).gt.0.) then
6604  if(tsfanl(i).gt.tsfsmx) tsfanl(i)=tsfsmx
6605  kount = kount + 1
6606  endif
6607  enddo
6608  if(kount.gt.0) then
6609  if(me .eq. 0) then
6610  per=float(kount)/float(len)*100.
6611  write(6,*) 'snow sfc. tsf set to ',tsfsmx,' at ',
6612  & kount, ' points ',per,'percent'
6613  endif
6614  endif
6615  return
6616  end
6617 
6625  subroutine albocn(albclm,slmask,albomx,len)
6626  use machine , only : kind_io8,kind_io4
6627  implicit none
6628  integer i,len
6629  real (kind=kind_io8) albomx
6630  real (kind=kind_io8) albclm(len,4), slmask(len)
6631  do i=1,len
6632  if(slmask(i).eq.0) then
6633  albclm(i,1) = albomx
6634  albclm(i,2) = albomx
6635  albclm(i,3) = albomx
6636  albclm(i,4) = albomx
6637  endif
6638  enddo
6639  return
6640  end
6641 
6649  subroutine qcmxice(glacir,amxice,len,me)
6650  use machine , only : kind_io8,kind_io4
6651  implicit none
6652  integer i,kount,len,me
6653  real (kind=kind_io8) glacir(len),amxice(len),per
6654  if (me .eq. 0) write(6,*) 'qc of maximum ice extent'
6655  kount=0
6656  do i=1,len
6657  if(glacir(i).eq.1..and.amxice(i).eq.0.) then
6658  amxice(i) = 0.
6659  kount = kount + 1
6660  endif
6661  enddo
6662  if(kount.gt.0) then
6663  per = float(kount) / float(len)*100.
6664  if(me .eq. 0) write(6,*) ' max ice limit less than glacier'
6665  &, ' coverage at ', kount, ' points ',per,'percent'
6666  endif
6667  return
6668  end
6669 
6678  subroutine qcsli(slianl,slifcs,len,me)
6679  use machine , only : kind_io8,kind_io4
6680  implicit none
6681  integer i,kount,len,me
6682  real (kind=kind_io8) slianl(len), slifcs(len),per
6683  if (me .eq. 0) then
6684  write(6,*) ' '
6685  write(6,*) 'qcsli'
6686  endif
6687  kount=0
6688  do i=1,len
6689  if(slianl(i).eq.1..and.slifcs(i).eq.0.) then
6690  kount = kount + 1
6691  slifcs(i) = 1.
6692  endif
6693  if(slianl(i).eq.0..and.slifcs(i).eq.1.) then
6694  kount = kount + 1
6695  slifcs(i) = 0.
6696  endif
6697  if(slianl(i).eq.2..and.slifcs(i).eq.1.) then
6698  kount = kount + 1
6699  slifcs(i) = 0.
6700  endif
6701  if(slianl(i).eq.1..and.slifcs(i).eq.2.) then
6702  kount = kount + 1
6703  slifcs(i) = 1.
6704  endif
6705  enddo
6706  if(kount.gt.0) then
6707  per=float(kount)/float(len)*100.
6708  if(me .eq. 0) then
6709  write(6,*) ' inconsistency of slmask between forecast and',
6710  & ' analysis corrected at ',kount, ' points ',per,
6711  & 'percent'
6712  endif
6713  endif
6714  return
6715  end
6716 
6717 ! subroutine nntprt(data,imax,fact)
6718 ! real (kind=kind_io8) data(imax)
6719 ! ilast=0
6720 ! i1=1
6721 ! i2=80
6722 !1112 continue
6723 ! if(i2.ge.imax) then
6724 ! ilast=1
6725 ! i2=imax
6726 ! endif
6727 ! write(6,*) ' '
6728 ! do j=1,jmax
6729 ! write(6,1111) (nint(data(imax*(j-1)+i)*fact),i=i1,i2)
6730 ! enddo
6731 ! if(ilast.eq.1) return
6732 ! i1=i1+80
6733 ! i2=i1+79
6734 ! if(i2.ge.imax) then
6735 ! ilast=1
6736 ! i2=imax
6737 ! endif
6738 ! go to 1112
6739 !1111 format(80i1)
6740 ! return
6741 ! end
6742 
6766  subroutine qcbyfc(tsffcs,snofcs,qctsfs,qcsnos,qctsfi,
6767  & len,lsoil,snoanl,aisanl,slianl,tsfanl,albanl,
6768  & zoranl,smcanl,
6769  & smcclm,tsfsmx,albomx,zoromx, me)
6770 !
6771  use machine , only : kind_io8,kind_io4
6772  implicit none
6773  integer kount,me,k,i,lsoil,len
6774  real (kind=kind_io8) zoromx,per,albomx,qctsfi,qcsnos,qctsfs,tsfsmx
6775  real (kind=kind_io8) tsffcs(len), snofcs(len)
6776  real (kind=kind_io8) snoanl(len), aisanl(len),
6777  & slianl(len), zoranl(len),
6778  & tsfanl(len), albanl(len,4),
6779  & smcanl(len,lsoil)
6780  real (kind=kind_io8) smcclm(len,lsoil)
6781 !
6782  if (me .eq. 0) write(6,*) 'qc of snow and sea-ice analysis'
6783 !
6784 ! qc of snow analysis
6785 !
6786 ! questionable snow cover
6787 !
6788  kount = 0
6789  do i=1,len
6790  if(slianl(i).gt.0..and.
6791  & tsffcs(i).gt.qctsfs.and.snoanl(i).gt.0.) then
6792  kount = kount + 1
6793  snoanl(i) = 0.
6794  tsfanl(i) = tsffcs(i)
6795  endif
6796  enddo
6797  if(kount.gt.0) then
6798  per=float(kount)/float(len)*100.
6799  if (me .eq. 0) then
6800  write(6,*) .gt.' guess surface temp ',qctsfs,
6801  & ' but snow analysis indicates snow cover'
6802  write(6,*) ' snow analysis set to zero',
6803  & ' at ',kount, ' points ',per,'percent'
6804  endif
6805  endif
6806 !
6807 ! questionable no snow cover
6808 !
6809  kount = 0
6810  do i=1,len
6811  if(slianl(i).gt.0..and.
6812  & snofcs(i).gt.qcsnos.and.snoanl(i).lt.0.) then
6813  kount = kount + 1
6814  snoanl(i) = snofcs(i)
6815  tsfanl(i) = tsffcs(i)
6816  endif
6817  enddo
6818  if(kount.gt.0) then
6819  per=float(kount)/float(len)*100.
6820  if (me .eq. 0) then
6821  write(6,*) .gt.' guess snow depth ',qcsnos,
6822  & ' but snow analysis indicates no snow cover'
6823  write(6,*) ' snow analysis set to guess value',
6824  & ' at ',kount, ' points ',per,'percent'
6825  endif
6826  endif
6827 !
6828 ! questionable sea ice cover ! this qc is disable to correct error in
6829 ! surface temparature over observed sea ice points
6830 !
6831 ! kount = 0
6832 ! do i=1,len
6833 ! if(slianl(i).eq.2..and.
6834 ! & tsffcs(i).gt.qctsfi.and.aisanl(i).eq.1.) then
6835 ! kount = kount + 1
6836 ! aisanl(i) = 0.
6837 ! slianl(i) = 0.
6838 ! tsfanl(i) = tsffcs(i)
6839 ! snoanl(i) = 0.
6840 ! zoranl(i) = zoromx
6841 ! albanl(i,1) = albomx
6842 ! albanl(i,2) = albomx
6843 ! albanl(i,3) = albomx
6844 ! albanl(i,4) = albomx
6845 ! do k=1,lsoil
6846 ! smcanl(i,k) = smcclm(i,k)
6847 ! enddo
6848 ! endif
6849 ! enddo
6850 ! if(kount.gt.0) then
6851 ! per=float(kount)/float(len)*100.
6852 ! if (me .eq. 0) then
6853 ! write(6,*) ' guess surface temp .gt. ',qctsfi,
6854 ! & ' but sea-ice analysis indicates sea-ice'
6855 ! write(6,*) ' sea-ice analysis set to zero',
6856 ! & ' at ',kount, ' points ',per,'percent'
6857 ! endif
6858 ! endif
6859 !
6860  return
6861  end
6862 
6888  subroutine setrmsk(kpds5,slmask,igaul,jgaul,wlon,rnlat,
6889  & data,imax,jmax,rlnout,rltout,lmask,rslmsk
6890  &, gaus,blno, blto, kgds1, kpds4, lbms)
6891  use machine , only : kind_io8,kind_io4
6892  use sfccyc_module
6893  implicit none
6894  real (kind=kind_io8) blno,blto,wlon,rnlat,crit,data_max
6895  integer i,j,ijmax,jgaul,igaul,kpds5,jmax,imax, kgds1, kspla
6896  integer, intent(in) :: kpds4
6897  logical*1, intent(in) :: lbms(imax,jmax)
6898  real*4 :: dummy(imax,jmax)
6899 
6900  real (kind=kind_io8) slmask(igaul,jgaul)
6901  real (kind=kind_io8) data(imax,jmax),rslmsk(imax,jmax)
6902  &, rlnout(imax), rltout(jmax)
6903  real (kind=kind_io8) a(jmax), w(jmax), radi, dlat, dlon
6904  logical lmask, gaus
6905 !
6906 ! set the longitude and latitudes for the grib file
6907 !
6908  if (kgds1 .eq. 4) then ! grib file on gaussian grid
6909  kspla=4
6910  call splat(kspla, jmax, a, w)
6911 !
6912  radi = 180.0 / (4.*atan(1.))
6913  do j=1,jmax
6914  rltout(j) = acos(a(j)) * radi
6915  enddo
6916 !
6917  if (rnlat .gt. 0.0) then
6918  do j=1,jmax
6919  rltout(j) = 90. - rltout(j)
6920  enddo
6921  else
6922  do j=1,jmax
6923  rltout(j) = -90. + rltout(j)
6924  enddo
6925  endif
6926  elseif (kgds1 .eq. 0) then ! grib file on lat/lon grid
6927  dlat = -(rnlat+rnlat) / float(jmax-1)
6928  do j=1,jmax
6929  rltout(j) = rnlat + (j-1) * dlat
6930  enddo
6931  else ! grib file on some other grid
6932  write(6,*) ' FATAL ERROR: Mask data on'
6933  write(6,*) ' unsupported grid.'
6934  call abort
6935  endif
6936  dlon = 360.0 / imax
6937  do i=1,imax
6938  rlnout(i) = wlon + (i-1)*dlon
6939  enddo
6940 !
6941 !
6942  ijmax = imax*jmax
6943  rslmsk = 0.
6944 ! TG3 MODS BEGIN
6945  if(kpds5 == kpdtsf .and. imax == 138 .and. jmax == 116
6946  & .and. kpds4 == 128) then
6947 ! print*,'turn off setrmsk for tg3'
6948  lmask = .false.
6949 
6950  elseif(kpds5 == kpdtsf) then
6951 ! TG3 MODS END
6952 !
6953 ! surface temperature
6954 !
6955  lmask = .false.
6956  call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
6957  &, rlnout, rltout, gaus, blno, blto)
6958 ! &, dlon, dlat, gaus, blno, blto)
6959  crit = 0.5
6960  call rof01(rslmsk,ijmax,'ge',crit)
6961  lmask = .true.
6962 !
6963 ! bucket soil wetness
6964 !
6965  elseif(kpds5.eq.kpdwet) then
6966  call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
6967  &, rlnout, rltout, gaus, blno, blto)
6968 ! &, dlon, dlat, gaus, blno, blto)
6969  crit = 0.5
6970  call rof01(rslmsk,ijmax,'ge',crit)
6971  lmask = .true.
6972 ! write(6,*) 'wet rslmsk'
6973 ! znnt=1.
6974 ! call nntprt(rslmsk,ijmax,znnt)
6975 !
6976 ! snow depth
6977 !
6978  elseif(kpds5 == kpdsnd) then
6979  if(kpds4 == 192) then ! use the bitmap
6980  rslmsk = 0.
6981  do j = 1, jmax
6982  do i = 1, imax
6983  if (lbms(i,j)) then
6984  rslmsk(i,j) = 1.
6985  end if
6986  enddo
6987  enddo
6988  lmask=.true.
6989  else
6990  lmask=.false.
6991  end if
6992 !
6993 ! snow liq equivalent depth
6994 !
6995  elseif(kpds5.eq.kpdsno) then
6996  call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
6997  &, rlnout, rltout, gaus, blno, blto)
6998 ! &, dlon, dlat, gaus, blno, blto)
6999  crit=0.5
7000  call rof01(rslmsk,ijmax,'ge',crit)
7001  lmask=.true.
7002 ! write(6,*) 'sno rslmsk'
7003 ! znnt=1.
7004 ! call nntprt(rslmsk,ijmax,znnt)
7005 !
7006 ! soil moisture
7007 !
7008  elseif(kpds5.eq.kpdsmc) then
7009  if(kpds4 == 192) then ! use the bitmap
7010  rslmsk = 0.
7011  do j = 1, jmax
7012  do i = 1, imax
7013  if (lbms(i,j)) then
7014  rslmsk(i,j) = 1.
7015  end if
7016  enddo
7017  enddo
7018  lmask=.true.
7019  else
7020  call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
7021  &, rlnout, rltout, gaus, blno, blto)
7022  crit=0.5
7023  call rof01(rslmsk,ijmax,'ge',crit)
7024  lmask=.true.
7025  endif
7026 !
7027 ! surface roughness
7028 !
7029  elseif(kpds5.eq.kpdzor) then
7030  do j=1,jmax
7031  do i=1,imax
7032  rslmsk(i,j)=data(i,j)
7033  enddo
7034  enddo
7035  crit=9.9
7036  call rof01(rslmsk,ijmax,'lt',crit)
7037  lmask=.true.
7038 ! write(6,*) 'zor rslmsk'
7039 ! znnt=1.
7040 ! call nntprt(rslmsk,ijmax,znnt)
7041 !
7042 ! albedo
7043 !
7044 ! elseif(kpds5.eq.kpdalb) then
7045 ! do j=1,jmax
7046 ! do i=1,imax
7047 ! rslmsk(i,j)=data(i,j)
7048 ! enddo
7049 ! enddo
7050 ! crit=99.
7051 ! call rof01(rslmsk,ijmax,'lt',crit)
7052 ! lmask=.true.
7053 ! write(6,*) 'alb rslmsk'
7054 ! znnt=1.
7055 ! call nntprt(rslmsk,ijmax,znnt)
7056 !
7057 ! albedo
7058 !
7059 !cbosu new snowfree albedo database has bitmap, use it.
7060  elseif(kpds5.eq.kpdalb(1)) then
7061  if (kpds4 == 192) then ! use the bitmap
7062  rslmsk = 0.
7063  do j = 1, jmax
7064  do i = 1, imax
7065  if (lbms(i,j)) then
7066  rslmsk(i,j) = 1.
7067  end if
7068  enddo
7069  enddo
7070  lmask = .true.
7071  else ! no bitmap. old database has no water flag.
7072  lmask=.false.
7073  end if
7074  elseif(kpds5.eq.kpdalb(2)) then
7075 !cbosu
7076  if (kpds4 == 192) then ! use the bitmap
7077  rslmsk = 0.
7078  do j = 1, jmax
7079  do i = 1, imax
7080  if (lbms(i,j)) then
7081  rslmsk(i,j) = 1.
7082  end if
7083  enddo
7084  enddo
7085  lmask = .true.
7086  else ! no bitmap. old database has no water flag.
7087  lmask=.false.
7088  end if
7089  elseif(kpds5.eq.kpdalb(3)) then
7090 !cbosu
7091  if (kpds4 == 192) then ! use the bitmap
7092  rslmsk = 0.
7093  do j = 1, jmax
7094  do i = 1, imax
7095  if (lbms(i,j)) then
7096  rslmsk(i,j) = 1.
7097  end if
7098  enddo
7099  enddo
7100  lmask = .true.
7101  else ! no bitmap. old database has no water flag.
7102  lmask=.false.
7103  end if
7104  elseif(kpds5.eq.kpdalb(4)) then
7105 !cbosu
7106  if (kpds4 == 192) then ! use the bitmap
7107  rslmsk = 0.
7108  do j = 1, jmax
7109  do i = 1, imax
7110  if (lbms(i,j)) then
7111  rslmsk(i,j) = 1.
7112  end if
7113  enddo
7114  enddo
7115  lmask = .true.
7116  else ! no bitmap. old database has no water flag.
7117  lmask=.false.
7118  end if
7119 !
7120 ! vegetation fraction for albedo
7121 !
7122  elseif(kpds5.eq.kpdalf(1)) then
7123 ! rslmsk=data
7124 ! crit=0.
7125 ! call rof01(rslmsk,ijmax,'gt',crit)
7126 ! lmask=.true.
7127  lmask=.false.
7128  elseif(kpds5.eq.kpdalf(2)) then
7129 ! rslmsk=data
7130 ! crit=0.
7131 ! call rof01(rslmsk,ijmax,'gt',crit)
7132 ! lmask=.true.
7133  lmask=.false.
7134 !
7135 ! sea ice
7136 !
7137  elseif(kpds5.eq.kpdais) then
7138  lmask=.false.
7139 ! call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
7140 ! &, dlon, dlat, gaus, blno, blto)
7141 ! crit=0.5
7142 ! call rof01(rslmsk,ijmax,'ge',crit)
7143 !
7144  data_max = 0.0
7145  do j=1,jmax
7146  do i=1,imax
7147  rslmsk(i,j) = data(i,j)
7148  data_max= max(data_max,data(i,j))
7149  enddo
7150  enddo
7151  crit=1.0
7152  if (data_max .gt. crit) then
7153  call rof01(rslmsk,ijmax,'gt',crit)
7154  lmask=.true.
7155  else
7156  lmask=.false.
7157  endif
7158 ! write(6,*) 'acn rslmsk'
7159 ! znnt=1.
7160 ! call nntprt(rslmsk,ijmax,znnt)
7161 !
7162 ! deep soil temperature
7163 !
7164  elseif(kpds5.eq.kpdtg3) then
7165  lmask=.false.
7166 ! call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
7167 ! &, rlnout, rltout, gaus, blno, blto)
7168 ! &, dlon, dlat, gaus, blno, blto)
7169 ! crit=0.5
7170 ! call rof01(rslmsk,ijmax,'ge',crit)
7171 ! lmask=.true.
7172 !
7173 ! plant resistance
7174 !
7175 ! elseif(kpds5.eq.kpdplr) then
7176 ! call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
7177 ! &, rlnout, rltout, gaus, blno, blto)
7178 ! &, dlon, dlat, gaus, blno, blto)
7179 ! crit=0.5
7180 ! call rof01(rslmsk,ijmax,'ge',crit)
7181 ! lmask=.true.
7182 !
7183 ! write(6,*) 'plr rslmsk'
7184 ! znnt=1.
7185 ! call nntprt(rslmsk,ijmax,znnt)
7186 !
7187 ! glacier points
7188 !
7189  elseif(kpds5.eq.kpdgla) then
7190  lmask=.false.
7191 !
7192 ! max ice extent
7193 !
7194  elseif(kpds5.eq.kpdmxi) then
7195  lmask=.false.
7196 !
7197 ! snow cover
7198 !
7199  elseif(kpds5.eq.kpdscv) then
7200  call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
7201  &, rlnout, rltout, gaus, blno, blto)
7202 ! &, dlon, dlat, gaus, blno, blto)
7203  crit=0.5
7204  call rof01(rslmsk,ijmax,'ge',crit)
7205  lmask=.true.
7206 ! write(6,*) 'scv rslmsk'
7207 ! znnt=1.
7208 ! call nntprt(rslmsk,ijmax,znnt)
7209 !
7210 ! sea ice concentration
7211 !
7212  elseif(kpds5.eq.kpdacn) then
7213  lmask=.false.
7214  call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
7215  &, rlnout, rltout, gaus, blno, blto)
7216 ! &, dlon, dlat, gaus, blno, blto)
7217  crit=0.5
7218  call rof01(rslmsk,ijmax,'ge',crit)
7219  lmask=.true.
7220 ! write(6,*) 'acn rslmsk'
7221 ! znnt=1.
7222 ! call nntprt(rslmsk,ijmax,znnt)
7223 !
7224 ! vegetation cover
7225 !
7226  elseif(kpds5.eq.kpdveg) then
7227 !cggg
7228  if (kpds4 == 192) then ! use the bitmap
7229  rslmsk = 0.
7230  do j = 1, jmax
7231  do i = 1, imax
7232  if (lbms(i,j)) then
7233  rslmsk(i,jmax-j+1) = 1. ! need to flip grid in n/s direction
7234  end if
7235  enddo
7236  enddo
7237  lmask = .true.
7238  else ! no bitmap, set mask the old way.
7239 
7240  call ga2la(slmask,igaul,jgaul,rslmsk,imax,jmax,wlon,rnlat
7241  &, rlnout, rltout, gaus, blno, blto)
7242  crit=0.5
7243  call rof01(rslmsk,ijmax,'ge',crit)
7244  lmask=.true.
7245 
7246  end if
7247 !
7248 ! soil type
7249 !
7250  elseif(kpds5.eq.kpdsot) then
7251 
7252  if (kpds4 == 192) then ! use the bitmap
7253  rslmsk = 0.
7254  do j = 1, jmax
7255  do i = 1, imax
7256  if (lbms(i,j)) then
7257  rslmsk(i,j) = 1.
7258  end if
7259  enddo
7260  enddo
7261 ! soil type is zero over water, use this to get a bitmap.
7262  else
7263  do j = 1, jmax
7264  do i = 1, imax
7265  rslmsk(i,j) = data(i,j)
7266  enddo
7267  enddo
7268  crit=0.1
7269  call rof01(rslmsk,ijmax,'gt',crit)
7270  endif
7271  lmask=.true.
7272 !
7273 ! vegetation type
7274 !
7275  elseif(kpds5.eq.kpdvet) then
7276 
7277  if (kpds4 == 192) then ! use the bitmap
7278  rslmsk = 0.
7279  do j = 1, jmax
7280  do i = 1, imax
7281  if (lbms(i,j)) then
7282  rslmsk(i,j) = 1.
7283  end if
7284  enddo
7285  enddo
7286 ! veg type is zero over water, use this to get a bitmap.
7287  else
7288  do j = 1, jmax
7289  do i = 1, imax
7290  rslmsk(i,j) = data(i,j)
7291  enddo
7292  enddo
7293  crit=0.1
7294  call rof01(rslmsk,ijmax,'gt',crit)
7295  endif
7296  lmask=.true.
7297 !
7298 ! these are for four new data type added by clu -- not sure its correct!
7299 !
7300  elseif(kpds5.eq.kpdvmn) then
7301 !
7302 !cggg greenness is zero over water, use this to get a bitmap.
7303 !
7304  do j = 1, jmax
7305  do i = 1, imax
7306  rslmsk(i,j) = data(i,j)
7307  enddo
7308  enddo
7309 !
7310  crit=0.1
7311  call rof01(rslmsk,ijmax,'gt',crit)
7312  lmask=.true.
7313 !cggg lmask=.false.
7314 !
7315  elseif(kpds5.eq.kpdvmx) then
7316 !
7317 !cggg greenness is zero over water, use this to get a bitmap.
7318 !
7319  do j = 1, jmax
7320  do i = 1, imax
7321  rslmsk(i,j) = data(i,j)
7322  enddo
7323  enddo
7324 !
7325  crit=0.1
7326  call rof01(rslmsk,ijmax,'gt',crit)
7327  lmask=.true.
7328 !cggg lmask=.false.
7329 !
7330  elseif(kpds5.eq.kpdslp) then
7331 !
7332 !cggg slope type is zero over water, use this to get a bitmap.
7333 !
7334  do j = 1, jmax
7335  do i = 1, imax
7336  rslmsk(i,j) = data(i,j)
7337  enddo
7338  enddo
7339 !
7340  crit=0.1
7341  call rof01(rslmsk,ijmax,'gt',crit)
7342  lmask=.true.
7343 !cggg lmask=.false.
7344 !
7345 !cbosu new maximum snow albedo database has bitmap
7346  elseif(kpds5.eq.kpdabs) then
7347  if (kpds4 == 192) then ! use the bitmap
7348  rslmsk = 0.
7349  do j = 1, jmax
7350  do i = 1, imax
7351  if (lbms(i,j)) then
7352  rslmsk(i,j) = 1.
7353  end if
7354  enddo
7355  enddo
7356  lmask = .true.
7357  else ! no bitmap. old database has zero over water
7358  do j = 1, jmax
7359  do i = 1, imax
7360  rslmsk(i,j) = data(i,j)
7361  enddo
7362  enddo
7363  crit=0.1
7364  call rof01(rslmsk,ijmax,'gt',crit)
7365  lmask=.true.
7366  end if
7367  endif
7368 !
7369  return
7370  end
7371 
7388  subroutine ga2la(gauin,imxin,jmxin,regout,imxout,jmxout,
7389  & wlon,rnlat,rlnout,rltout,gaus,blno, blto)
7390  use machine , only : kind_io8,kind_io4
7391  implicit none
7392  integer i1,i2,j2,ishft,i,jj,j1,jtem,jmxout,imxin,jmxin,imxout,
7393  & j,iret
7394  real (kind=kind_io8) alamd,dxin,aphi,x,sum1,sum2,y,dlati,wlon,
7395  & rnlat,dxout,dphi,dlat,facns,tem,blno,
7396  & blto
7397 !
7398 ! interpolation from lat/lon grid to other lat/lon grid
7399 !
7400  real (kind=kind_io8) gauin (imxin,jmxin), regout(imxout,jmxout)
7401  &, rlnout(imxout), rltout(jmxout)
7402  logical gaus
7403 !
7404  real, allocatable :: gaul(:)
7405  real (kind=kind_io8) ddx(imxout),ddy(jmxout)
7406  integer iindx1(imxout), iindx2(imxout),
7407  & jindx1(jmxout), jindx2(jmxout)
7408  integer jmxsav,n,kspla
7409  data jmxsav/0/
7410  save jmxsav, gaul, dlati
7411  real (kind=kind_io8) radi
7412  real (kind=kind_io8) a(jmxin), w(jmxin)
7413 !
7414 !
7415  logical first
7416  integer num_threads
7417  data first /.true./
7418  save num_threads, first
7419 !
7420  integer len_thread_m, j1_t, j2_t, it
7421  integer num_parthds
7422 !
7423  if (first) then
7424  num_threads = num_parthds()
7425  first = .false.
7426  endif
7427 !
7428  if (jmxin .ne. jmxsav) then
7429  if (jmxsav .gt. 0) deallocate (gaul, stat=iret)
7430  allocate (gaul(jmxin))
7431  jmxsav = jmxin
7432  if (gaus) then
7433 cjfe call gaulat(gaul,jmxin)
7434 cjfe
7435 !
7436  kspla=4
7437  call splat(kspla, jmxin, a, w)
7438 !
7439  radi = 180.0 / (4.*atan(1.))
7440  do n=1,jmxin
7441  gaul(n) = acos(a(n)) * radi
7442  enddo
7443 cjfe
7444  do j=1,jmxin
7445  gaul(j) = 90. - gaul(j)
7446  enddo
7447  else
7448  dlat = -2*blto / float(jmxin-1)
7449  dlati = 1 / dlat
7450  do j=1,jmxin
7451  gaul(j) = blto + (j-1) * dlat
7452  enddo
7453  endif
7454  endif
7455 !
7456 !
7457  dxin = 360. / float(imxin )
7458 !
7459  do i=1,imxout
7460  alamd = rlnout(i)
7461  i1 = floor((alamd-blno)/dxin) + 1
7462  ddx(i) = (alamd-blno)/dxin-(i1-1)
7463  iindx1(i) = modulo(i1-1,imxin) + 1
7464  iindx2(i) = modulo(i1 ,imxin) + 1
7465  enddo
7466 !
7467 !
7468  len_thread_m = (jmxout+num_threads-1) / num_threads
7469 !
7470  if (gaus) then
7471 !
7472 !$omp parallel do private(j1_t,j2_t,it,j1,j2,jj)
7473 !$omp+private(aphi)
7474 !$omp+shared(num_threads,len_thread_m)
7475 !$omp+shared(jmxin,jmxout,gaul,rltout,jindx1,ddy)
7476 !
7477  do it=1,num_threads ! start of threaded loop ...................
7478  j1_t = (it-1)*len_thread_m+1
7479  j2_t = min(j1_t+len_thread_m-1,jmxout)
7480 !
7481  j2=1
7482  do 40 j=j1_t,j2_t
7483  aphi=rltout(j)
7484  do 50 jj=1,jmxin
7485  if(aphi.lt.gaul(jj)) go to 50
7486  j2=jj
7487  go to 42
7488  50 continue
7489  42 continue
7490  if(j2.gt.2) go to 43
7491  j1=1
7492  j2=2
7493  go to 44
7494  43 continue
7495  if(j2.le.jmxin) go to 45
7496  j1=jmxin-1
7497  j2=jmxin
7498  go to 44
7499  45 continue
7500  j1=j2-1
7501  44 continue
7502  jindx1(j)=j1
7503  jindx2(j)=j2
7504  ddy(j)=(aphi-gaul(j1))/(gaul(j2)-gaul(j1))
7505  40 continue
7506  enddo ! end of threaded loop ...................
7507 !$omp end parallel do
7508 !
7509  else
7510 !$omp parallel do private(j1_t,j2_t,it,j1,j2,jtem)
7511 !$omp+private(aphi)
7512 !$omp+shared(num_threads,len_thread_m)
7513 !$omp+shared(jmxin,jmxout,gaul,rltout,jindx1,ddy,dlati,blto)
7514 !
7515  do it=1,num_threads ! start of threaded loop ...................
7516  j1_t = (it-1)*len_thread_m+1
7517  j2_t = min(j1_t+len_thread_m-1,jmxout)
7518 !
7519  j2=1
7520  do 400 j=j1_t,j2_t
7521  aphi=rltout(j)
7522  jtem = (aphi - blto) * dlati + 1
7523  if (jtem .ge. 1 .and. jtem .lt. jmxin) then
7524  j1 = jtem
7525  j2 = j1 + 1
7526  ddy(j)=(aphi-gaul(j1))/(gaul(j2)-gaul(j1))
7527  elseif (jtem .eq. jmxin) then
7528  j1 = jmxin
7529  j2 = jmxin
7530  ddy(j)=1.0
7531  else
7532  j1 = 1
7533  j2 = 1
7534  ddy(j)=1.0
7535  endif
7536 !
7537  jindx1(j) = j1
7538  jindx2(j) = j2
7539  400 continue
7540  enddo ! end of threaded loop ...................
7541 !$omp end parallel do
7542  endif
7543 !
7544 ! write(6,*) 'ga2la'
7545 ! write(6,*) 'iindx1'
7546 ! write(6,*) (iindx1(n),n=1,imxout)
7547 ! write(6,*) 'iindx2'
7548 ! write(6,*) (iindx2(n),n=1,imxout)
7549 ! write(6,*) 'jindx1'
7550 ! write(6,*) (jindx1(n),n=1,jmxout)
7551 ! write(6,*) 'jindx2'
7552 ! write(6,*) (jindx2(n),n=1,jmxout)
7553 ! write(6,*) 'ddy'
7554 ! write(6,*) (ddy(n),n=1,jmxout)
7555 ! write(6,*) 'ddx'
7556 ! write(6,*) (ddx(n),n=1,jmxout)
7557 !
7558 !
7559 !$omp parallel do private(j1_t,j2_t,it,i,i1,i2)
7560 !$omp+private(j,j1,j2,x,y)
7561 !$omp+shared(num_threads,len_thread_m)
7562 !$omp+shared(imxout,iindx1,jindx1,ddx,ddy,gauin,regout)
7563 !
7564  do it=1,num_threads ! start of threaded loop ...................
7565  j1_t = (it-1)*len_thread_m+1
7566  j2_t = min(j1_t+len_thread_m-1,jmxout)
7567 !
7568  do j=j1_t,j2_t
7569  y = ddy(j)
7570  j1 = jindx1(j)
7571  j2 = jindx2(j)
7572  do i=1,imxout
7573  x = ddx(i)
7574  i1 = iindx1(i)
7575  i2 = iindx2(i)
7576  regout(i,j) = (1.-x)*((1.-y)*gauin(i1,j1) + y*gauin(i1,j2))
7577  & + x *((1.-y)*gauin(i2,j1) + y*gauin(i2,j2))
7578  enddo
7579  enddo
7580  enddo ! end of threaded loop ...................
7581 !$omp end parallel do
7582 !
7583  sum1 = 0.
7584  sum2 = 0.
7585  do i=1,imxin
7586  sum1 = sum1 + gauin(i,1)
7587  sum2 = sum2 + gauin(i,jmxin)
7588  enddo
7589  sum1 = sum1 / float(imxin)
7590  sum2 = sum2 / float(imxin)
7591 !
7592  if (gaus) then
7593  if (rnlat .gt. 0.0) then
7594  do i=1,imxout
7595  regout(i, 1) = sum1
7596  regout(i,jmxout) = sum2
7597  enddo
7598  else
7599  do i=1,imxout
7600  regout(i, 1) = sum2
7601  regout(i,jmxout) = sum1
7602  enddo
7603  endif
7604  else
7605  if (blto .lt. 0.0) then
7606  if (rnlat .gt. 0.0) then
7607  do i=1,imxout
7608  regout(i, 1) = sum2
7609  regout(i,jmxout) = sum1
7610  enddo
7611  else
7612  do i=1,imxout
7613  regout(i, 1) = sum1
7614  regout(i,jmxout) = sum2
7615  enddo
7616  endif
7617  else
7618  if (rnlat .lt. 0.0) then
7619  do i=1,imxout
7620  regout(i, 1) = sum2
7621  regout(i,jmxout) = sum1
7622  enddo
7623  else
7624  do i=1,imxout
7625  regout(i, 1) = sum1
7626  regout(i,jmxout) = sum2
7627  enddo
7628  endif
7629  endif
7630  endif
7631 !
7632  return
7633  end
7634 
7643  subroutine landtyp(vegtype,soiltype,slptype,slmask,len)
7644  use machine , only : kind_io8,kind_io4
7645  implicit none
7646  integer i,len
7647  real (kind=kind_io8) vegtype(len),soiltype(len),slmask(len)
7648  +, slptype(len)
7649 !
7650 ! make sure that the soil type and veg type are non-zero over land
7651 !
7652  do i = 1, len
7653  if (slmask(i) .eq. 1) then
7654  if (vegtype(i) .eq. 0.) vegtype(i) = 7
7655  if (soiltype(i) .eq. 0.) soiltype(i) = 2
7656  if (slptype(i) .eq. 0.) slptype(i) = 1
7657  endif
7658  enddo
7659  return
7660  end subroutine landtyp
7661 
7667  subroutine gaulat(gaul,k)
7668 !
7669  use machine , only : kind_io8,kind_io4
7670  implicit none
7671  integer n,k
7672  real (kind=kind_io8) radi
7673  real (kind=kind_io8) a(k), w(k), gaul(k)
7674 !
7675  call splat(4, k, a, w)
7676 !
7677  radi = 180.0 / (4.*atan(1.))
7678  do n=1,k
7679  gaul(n) = acos(a(n)) * radi
7680  enddo
7681 !
7682 ! print *,'gaussian lat (deg) for jmax=',k
7683 ! print *,(gaul(n),n=1,k)
7684 !
7685  return
7686  70 write(6,6000)
7687  6000 format(//5x,'error in gauaw'//)
7688  stop
7689  end
7690 
7699  subroutine anomint(tsfan0,tsfclm,tsfcl0,tsfanl,len)
7700 !
7701  use machine , only : kind_io8,kind_io4
7702  implicit none
7703  integer i,len
7704  real (kind=kind_io8) tsfanl(len), tsfan0(len),
7705  & tsfclm(len), tsfcl0(len)
7706 !
7707 ! time interpolation of anomalies
7708 ! add initial anomaly to date interpolated climatology
7709 !
7710  write(6,*) 'anomint'
7711  do i=1,len
7712  tsfanl(i) = tsfan0(i) - tsfcl0(i) + tsfclm(i)
7713  enddo
7714  return
7715  end
7716 
7828  subroutine clima(lugb,iy,im,id,ih,fh,len,lsoil,
7829  & slmask,fntsfc,fnwetc,fnsnoc,fnzorc,fnalbc,fnaisc,
7830  & fntg3c,fnscvc,fnsmcc,fnstcc,fnacnc,fnvegc,
7831  & fnvetc,fnsotc,
7832  & fnvmnc,fnvmxc,fnslpc,fnabsc,
7833  & tsfclm,tsfcl2,wetclm,snoclm,zorclm,albclm,aisclm,
7834  & tg3clm,cvclm ,cvbclm,cvtclm,
7835  & cnpclm,smcclm,stcclm,sliclm,scvclm,acnclm,vegclm,
7836  & vetclm,sotclm,alfclm,
7837  & vmnclm,vmxclm,slpclm,absclm,
7838  & kpdtsf,kpdwet,kpdsno,kpdzor,kpdalb,kpdais,
7839  & kpdtg3,kpdscv,kpdacn,kpdsmc,kpdstc,kpdveg,
7840  & kpdvet,kpdsot,kpdalf,tsfcl0,
7841  & kpdvmn,kpdvmx,kpdslp,kpdabs,
7842  & deltsfc, lanom
7843  &, imsk, jmsk, slmskh, outlat, outlon
7844  &, gaus, blno, blto, me,lprnt,iprnt, fnalbc2, ialb
7845  &, tile_num_ch, i_index, j_index)
7846 !
7847  use machine , only : kind_io8,kind_io4
7848  implicit none
7849  character(len=*), intent(in) :: tile_num_ch
7850  integer, intent(in) :: i_index(len), j_index(len)
7851  real (kind=kind_io8) rjday,wei1x,wei2x,rjdayh,wei2m,wei1m,wei1s,
7852  & wei2s,fh,stcmon1s,blto,blno,deltsfc,rjdayh2
7853  real (kind=kind_io8) wei1y,wei2y
7854  integer jdoy,jday,jh,jdow,mmm,mmp,mm,iret,monend,i,k,jm,jd,iy4,
7855  & jy,mon1,is2,isx,kpd9,is1,l,nn,mon2,mon,is,kpdsno,
7856  & kpdzor,kpdtsf,kpdwet,kpdscv,kpdacn,kpdais,kpdtg3,im,id,
7857  & lugb,iy,len,lsoil,ih,kpdsmc,iprnt,me,m1,m2,k1,k2,
7858  & kpdvet,kpdsot,kpdstc,kpdveg,jmsk,imsk,j,ialb
7859  &, kpdvmn,kpdvmx,kpdslp,kpdabs,landice_cat
7860  integer kpdalb(4), kpdalf(2)
7861 !
7862  character*500 fntsfc,fnwetc,fnsnoc,fnzorc,fnalbc,fnaisc,
7863  & fntg3c,fnscvc,fnsmcc,fnstcc,fnacnc,fnvegc,
7864  & fnvetc,fnsotc,fnalbc2
7865  &, fnvmnc,fnvmxc,fnslpc,fnabsc
7866  real (kind=kind_io8) tsfclm(len),tsfcl2(len),
7867  & wetclm(len),snoclm(len),
7868  & zorclm(len),albclm(len,4),aisclm(len),
7869  & tg3clm(len),acnclm(len),
7870  & cvclm(len),cvbclm(len),cvtclm(len),
7871  & cnpclm(len),
7872  & smcclm(len,lsoil),stcclm(len,lsoil),
7873  & sliclm(len),scvclm(len),vegclm(len),
7874  & vetclm(len),sotclm(len),alfclm(len,2)
7875  &, vmnclm(len),vmxclm(len),slpclm(len),absclm(len)
7876  real (kind=kind_io8) slmskh(imsk,jmsk)
7877  real (kind=kind_io8) outlat(len), outlon(len)
7878 !
7879  real (kind=kind_io8) slmask(len), tsfcl0(len)
7880  real (kind=kind_io8), allocatable :: slmask_noice(:)
7881 !
7882  logical lanom, gaus, first
7883 !
7884 ! set z0 based on sib vegetation type
7885  real (kind=kind_io8) z0_sib(13)
7886  data z0_sib /2.653, 0.826, 0.563, 1.089, 0.854, 0.856,
7887  & 0.035, 0.238, 0.065, 0.076, 0.011, 0.125,
7888  & 0.011 /
7889 ! set z0 based on igbp vegetation type
7890  real (kind=kind_io8) z0_igbp_min(20), z0_igbp_max(20)
7891  real (kind=kind_io8) z0_season(12)
7892  data z0_igbp_min /1.089, 2.653, 0.854, 0.826, 0.800, 0.050,
7893  & 0.030, 0.856, 0.856, 0.150, 0.040, 0.130,
7894  & 1.000, 0.250, 0.011, 0.011, 0.001, 0.076,
7895  & 0.050, 0.030/
7896  data z0_igbp_max /1.089, 2.653, 0.854, 0.826, 0.800, 0.050,
7897  & 0.030, 0.856, 0.856, 0.150, 0.040, 0.130,
7898  & 1.000, 0.250, 0.011, 0.011, 0.001, 0.076,
7899  & 0.050, 0.030/
7900 !
7901 ! dayhf : julian day of the middle of each month
7902 !
7903  real (kind=kind_io8) dayhf(13)
7904  data dayhf/ 15.5, 45.0, 74.5,105.0,135.5,166.0,
7905  & 196.5,227.5,258.0,288.5,319.0,349.5,380.5/
7906 !
7907  real (kind=kind_io8) fha(5)
7908  real(4) fha4(5)
7909  integer w3kindreal,w3kindint
7910  integer ida(8),jda(8),ivtyp, kpd7
7911 !
7912  real (kind=kind_io8), allocatable :: tsf(:,:),sno(:,:),
7913  & zor(:,:),wet(:,:),
7914  & ais(:,:), acn(:,:), scv(:,:), smc(:,:,:),
7915  & tg3(:), alb(:,:,:), alf(:,:),
7916  & vet(:), sot(:), tsf2(:),
7917  & veg(:,:), stc(:,:,:)
7918  &, vmn(:), vmx(:), slp(:), absm(:)
7919 !
7920  integer mon1s, mon2s, sea1s, sea2s, sea1, sea2, hyr1, hyr2
7921  data first/.true./
7922  data mon1s/0/, mon2s/0/, sea1s/0/, sea2s/0/
7923 !
7924  save first, tsf, sno, zor, wet, ais, acn, scv, smc, tg3,
7925  & alb, alf, vet, sot, tsf2, veg, stc,
7926  & vmn, vmx, slp, absm,
7927  & mon1s, mon2s, sea1s, sea2s, dayhf, k1, k2, m1, m2,
7928  & landice_cat
7929 !
7930  logical lprnt
7931 !
7932  do i=1,len
7933  tsfclm(i) = 0.0
7934  tsfcl2(i) = 0.0
7935  snoclm(i) = 0.0
7936  wetclm(i) = 0.0
7937  zorclm(i) = 0.0
7938  aisclm(i) = 0.0
7939  tg3clm(i) = 0.0
7940  acnclm(i) = 0.0
7941  cvclm(i) = 0.0
7942  cvbclm(i) = 0.0
7943  cvtclm(i) = 0.0
7944  cnpclm(i) = 0.0
7945  sliclm(i) = 0.0
7946  scvclm(i) = 0.0
7947  vmnclm(i) = 0.0
7948  vmxclm(i) = 0.0
7949  slpclm(i) = 0.0
7950  absclm(i) = 0.0
7951  enddo
7952  do k=1,lsoil
7953  do i=1,len
7954  smcclm(i,k) = 0.0
7955  stcclm(i,k) = 0.0
7956  enddo
7957  enddo
7958  do k=1,4
7959  do i=1,len
7960  albclm(i,k) = 0.0
7961  enddo
7962  enddo
7963  do k=1,2
7964  do i=1,len
7965  alfclm(i,k) = 0.0
7966  enddo
7967  enddo
7968 !
7969  iret = 0
7970  monend = 9999
7971 !
7972  if (first) then
7973 !
7974 ! allocate variables to be saved
7975 !
7976  allocate (tsf(len,2), sno(len,2), zor(len,2),
7977  & wet(len,2), ais(len,2), acn(len,2),
7978  & scv(len,2), smc(len,lsoil,2),
7979  & tg3(len), alb(len,4,2), alf(len,2),
7980  & vet(len), sot(len), tsf2(len),
7981 !clu [+1l] add vmn, vmx, slp, abs
7982  & vmn(len), vmx(len), slp(len), absm(len),
7983  & veg(len,2), stc(len,lsoil,2))
7984 !
7985 ! get tsf climatology for the begining of the forecast
7986 !
7987  if (fh > 0.0) then
7988 !cbosu
7989  if (me == 0) print*,'bosu fh gt 0'
7990 
7991  iy4 = iy
7992  if (iy < 101) iy4 = 1900 + iy4
7993  fha = 0
7994  ida = 0
7995  jda = 0
7996 ! fha(2) = nint(fh)
7997  ida(1) = iy
7998  ida(2) = im
7999  ida(3) = id
8000  ida(5) = ih
8001  call w3kind(w3kindreal,w3kindint)
8002  if(w3kindreal == 4) then
8003  fha4 = fha
8004  call w3movdat(fha4,ida,jda)
8005  else
8006  call w3movdat(fha,ida,jda)
8007  endif
8008  jy = jda(1)
8009  jm = jda(2)
8010  jd = jda(3)
8011  jh = jda(5)
8012  if (me == 0) write(6,*) ' forecast jy,jm,jd,jh',
8013  & jy,jm,jd,jh
8014  jdow = 0
8015  jdoy = 0
8016  jday = 0
8017  call w3doxdat(jda,jdow,jdoy,jday)
8018  rjday = jdoy + jda(5) / 24.
8019  if(rjday < dayhf(1)) rjday = rjday + 365.
8020 !
8021  if (me == 0) write(6,*) 'forecast jy,jm,jd,jh=',jy,jm,jd,jh
8022 !
8023 ! for monthly mean climatology
8024 !
8025  monend = 12
8026  do mm=1,monend
8027  mmm = mm
8028  mmp = mm + 1
8029  if(rjday >= dayhf(mmm) .and. rjday < dayhf(mmp)) then
8030  mon1 = mmm
8031  mon2 = mmp
8032  go to 10
8033  endif
8034  enddo
8035  print *,'FATAL ERROR: Wrong rjday',rjday
8036  call abort
8037  10 continue
8038  wei1m = (dayhf(mon2)-rjday)/(dayhf(mon2)-dayhf(mon1))
8039  wei2m = 1.0 - wei1m
8040 ! wei2m = (rjday-dayhf(mon1))/(dayhf(mon2)-dayhf(mon1))
8041  if (mon2 == 13) mon2 = 1
8042  if (me == 0) print *,'rjday,mon1,mon2,wei1m,wei2m=',
8043  & rjday,mon1,mon2,wei1m,wei2m
8044 !
8045 ! read monthly mean climatology of tsf
8046 !
8047  kpd7 = -1
8048  do nn=1,2
8049  mon = mon1
8050  if (nn == 2) mon = mon2
8051  call fixrdc(lugb,fntsfc,kpdtsf,kpd7,mon,slmask,
8052  & tsf(1,nn),len,iret
8053  &, imsk, jmsk, slmskh, gaus,blno, blto
8054  &, outlat, outlon, me)
8055  enddo
8056 !
8057 ! tsf at the begining of forecast i.e. fh=0
8058 !
8059  do i=1,len
8060  tsfcl0(i) = wei1m * tsf(i,1) + wei2m * tsf(i,2)
8061  enddo
8062  endif
8063  endif
8064 !
8065 ! compute current jy,jm,jd,jh of forecast and the day of the year
8066 !
8067  iy4 = iy
8068  if (iy < 101) iy4=1900+iy4
8069  fha = 0
8070  ida = 0
8071  jda = 0
8072  fha(2) = nint(fh)
8073  ida(1) = iy
8074  ida(2) = im
8075  ida(3) = id
8076  ida(5) = ih
8077  call w3kind(w3kindreal,w3kindint)
8078  if(w3kindreal == 4) then
8079  fha4 = fha
8080  call w3movdat(fha4,ida,jda)
8081  else
8082  call w3movdat(fha,ida,jda)
8083  endif
8084  jy = jda(1)
8085  jm = jda(2)
8086  jd = jda(3)
8087  jh = jda(5)
8088 ! if (me .eq. 0) write(6,*) ' forecast jy,jm,jd,jh,rjday=',
8089 ! & jy,jm,jd,jh,rjday
8090  jdow = 0
8091  jdoy = 0
8092  jday = 0
8093  call w3doxdat(jda,jdow,jdoy,jday)
8094  rjday = jdoy + jda(5) / 24.
8095  if(rjday < dayhf(1)) rjday = rjday + 365.
8096 
8097  if (me == 0) write(6,*) ' forecast jy,jm,jd,jh,rjday=',
8098  & jy,jm,jd,jh,rjday
8099 !
8100  if (me == 0) write(6,*) 'forecast jy,jm,jd,jh=',jy,jm,jd,jh
8101 !
8102 ! for monthly mean climatology
8103 !
8104  monend = 12
8105  do mm=1,monend
8106  mmm = mm
8107  mmp = mm + 1
8108  if(rjday >= dayhf(mmm) .and. rjday < dayhf(mmp)) then
8109  mon1 = mmm
8110  mon2 = mmp
8111  go to 20
8112  endif
8113  enddo
8114  print *,'FATAL ERROR: Wrong rjday',rjday
8115  call abort
8116  20 continue
8117  wei1m = (dayhf(mon2)-rjday)/(dayhf(mon2)-dayhf(mon1))
8118  wei2m = 1.0 - wei1m
8119 ! wei2m = (rjday-dayhf(mon1))/(dayhf(mon2)-dayhf(mon1))
8120  if (mon2 == 13) mon2 = 1
8121  if (me == 0) print *,'rjday,mon1,mon2,wei1m,wei2m=',
8122  & rjday,mon1,mon2,wei1m,wei2m
8123 !
8124 ! for seasonal mean climatology
8125 !
8126  monend = 4
8127  is = im/3 + 1
8128  if (is == 5) is = 1
8129  do mm=1,monend
8130  mmm = mm*3 - 2
8131  mmp = (mm+1)*3 - 2
8132  if(rjday >= dayhf(mmm) .and. rjday < dayhf(mmp)) then
8133  sea1 = mmm
8134  sea2 = mmp
8135  go to 30
8136  endif
8137  enddo
8138  print *,'FATAL ERROR: Wrong rjday',rjday
8139  call abort
8140  30 continue
8141  wei1s = (dayhf(sea2)-rjday)/(dayhf(sea2)-dayhf(sea1))
8142  wei2s = 1.0 - wei1s
8143 ! wei2s = (rjday-dayhf(sea1))/(dayhf(sea2)-dayhf(sea1))
8144  if (sea2 == 13) sea2 = 1
8145  if (me == 0) print *,'rjday,sea1,sea2,wei1s,wei2s=',
8146  & rjday,sea1,sea2,wei1s,wei2s
8147 !
8148 ! for summer and winter values (maximum and minimum).
8149 !
8150  monend = 2
8151  is = im/6 + 1
8152  if (is == 3) is = 1
8153  do mm=1,monend
8154  mmm = mm*6 - 5
8155  mmp = (mm+1)*6 - 5
8156  if(rjday >= dayhf(mmm) .and. rjday < dayhf(mmp)) then
8157  hyr1 = mmm
8158  hyr2 = mmp
8159  go to 31
8160  endif
8161  enddo
8162  print *,'FATAL ERROR: Wrong rjday',rjday
8163  call abort
8164  31 continue
8165  wei1y = (dayhf(hyr2)-rjday)/(dayhf(hyr2)-dayhf(hyr1))
8166  wei2y = 1.0 - wei1y
8167 ! wei2y = (rjday-dayhf(hyr1))/(dayhf(hyr2)-dayhf(hyr1))
8168  if (hyr2 == 13) hyr2 = 1
8169  if (me == 0) print *,'rjday,hyr1,hyr2,wei1y,wei2y=',
8170  & rjday,hyr1,hyr2,wei1y,wei2y
8171 !
8172 ! start reading in climatology and interpolate to the date
8173 !
8174  first_time : if (first) then
8175 !cbosu
8176  if (me == 0) print*,'bosu first time thru'
8177 !
8178 ! annual mean climatology
8179 !
8180 ! fraction of vegetation field for albedo -- there are two
8181 ! fraction fields in this version: strong zenith angle dependent
8182 ! and weak zenith angle dependent
8183 !
8184  kpd9 = -1
8185 cjfe
8186  alf=0.
8187 cjfe
8188 
8189  kpd7=-1
8190  if (ialb == 1) then
8191 !cbosu still need facsf and facwf. read them from the production
8192 !cbosu file
8193  if ( index(fnalbc2, "tileX.nc") == 0) then ! grib file
8194  call fixrdc(lugb,fnalbc2,kpdalf(1),kpd7,kpd9,slmask
8195  &, alf,len,iret
8196  &, imsk, jmsk, slmskh, gaus,blno, blto
8197  &, outlat, outlon, me)
8198  else
8199  call fixrdc_tile(fnalbc2, tile_num_ch, i_index, j_index,
8200  & kpdalf(1), alf(:,1), 1, len, me)
8201  endif
8202  else
8203  call fixrdc(lugb,fnalbc,kpdalf(1),kpd7,kpd9,slmask
8204  &, alf,len,iret
8205  &, imsk, jmsk, slmskh, gaus,blno, blto
8206  &, outlat, outlon, me)
8207  endif
8208  do i = 1, len
8209  if(slmask(i).eq.1.) then
8210  alf(i,2) = 100. - alf(i,1)
8211  endif
8212  enddo
8213 !
8214 ! deep soil temperature
8215 !
8216  if(fntg3c(1:8).ne.' ') then
8217  if ( index(fntg3c, "tileX.nc") == 0) then ! grib file
8218  kpd7=-1
8219  call fixrdc(lugb,fntg3c,kpdtg3,kpd7,kpd9,slmask,
8220  & tg3,len,iret
8221  &, imsk, jmsk, slmskh, gaus,blno, blto
8222  &, outlat, outlon, me)
8223  else
8224  call fixrdc_tile(fntg3c, tile_num_ch, i_index, j_index,
8225  & kpdtg3, tg3, 1, len, me)
8226  endif
8227  endif
8228 !
8229 ! vegetation type
8230 !
8231 ! when using the new gldas soil moisture climatology, a veg type
8232 ! dataset must be selected.
8233 !
8234  if(fnvetc(1:8).ne.' ') then
8235  if ( index(fnvetc, "tileX.nc") == 0) then ! grib file
8236  kpd7=-1
8237  call fixrdc(lugb,fnvetc,kpdvet,kpd7,kpd9,slmask,
8238  & vet,len,iret
8239  &, imsk, jmsk, slmskh, gaus,blno, blto
8240  &, outlat, outlon, me)
8241  landice_cat=13
8242  if (maxval(vet)> 13.0) landice_cat=15
8243  else
8244  call fixrdc_tile(fnvetc, tile_num_ch, i_index, j_index,
8245  & kpdvet, vet, 1, len, me)
8246  landice_cat=15
8247  endif
8248  if (me .eq. 0) write(6,*) 'climatological vegetation',
8249  & ' type read in.'
8250  elseif(index(fnsmcc,'soilmgldas') /= 0) then ! new soil moisture climo
8251  if (me .eq. 0) write(6,*) 'FATAL ERROR: must choose'
8252  if (me .eq. 0) write(6,*) 'climatological veg type when'
8253  if (me .eq. 0) write(6,*) 'using new gldas soil moisture.'
8254  call abort
8255  endif
8256 !
8257 ! soil type
8258 !
8259  if(fnsotc(1:8).ne.' ') then
8260  if ( index(fnsotc, "tileX.nc") == 0) then ! grib file
8261  kpd7=-1
8262  call fixrdc(lugb,fnsotc,kpdsot,kpd7,kpd9,slmask,
8263  & sot,len,iret
8264  &, imsk, jmsk, slmskh, gaus,blno, blto
8265  &, outlat, outlon, me)
8266  else
8267  call fixrdc_tile(fnsotc, tile_num_ch, i_index, j_index,
8268  & kpdsot, sot, 1, len, me)
8269  endif
8270  if (me .eq. 0) write(6,*) 'climatological soil type read in.'
8271  endif
8272 
8273 !
8274 ! min vegetation cover
8275 !
8276  if(fnvmnc(1:8).ne.' ') then
8277  if ( index(fnvmnc, "tileX.nc") == 0) then ! grib file
8278  kpd7=-1
8279  call fixrdc(lugb,fnvmnc,kpdvmn,kpd7,kpd9,slmask,
8280  & vmn,len,iret
8281  &, imsk, jmsk, slmskh, gaus,blno, blto
8282  &, outlat, outlon, me)
8283  else
8284  call fixrdc_tile(fnvmnc, tile_num_ch, i_index, j_index,
8285  & 257, vmn, 99, len, me)
8286 
8287  endif
8288  if (me .eq. 0) write(6,*) 'climatological shdmin read in.'
8289  endif
8290 !
8291 ! max vegetation cover
8292 !
8293  if(fnvmxc(1:8).ne.' ') then
8294  if ( index(fnvmxc, "tileX.nc") == 0) then ! grib file
8295  kpd7=-1
8296  call fixrdc(lugb,fnvmxc,kpdvmx,kpd7,kpd9,slmask,
8297  & vmx,len,iret
8298  &, imsk, jmsk, slmskh, gaus,blno, blto
8299  &, outlat, outlon, me)
8300  else
8301  call fixrdc_tile(fnvmxc, tile_num_ch, i_index, j_index,
8302  & 256, vmx, 99, len, me)
8303  endif
8304  if (me .eq. 0) write(6,*) 'climatological shdmax read in.'
8305  endif
8306 !
8307 ! slope type
8308 !
8309  if(fnslpc(1:8).ne.' ') then
8310  if ( index(fnslpc, "tileX.nc") == 0) then ! grib file
8311  kpd7=-1
8312  call fixrdc(lugb,fnslpc,kpdslp,kpd7,kpd9,slmask,
8313  & slp,len,iret
8314  &, imsk, jmsk, slmskh, gaus,blno, blto
8315  &, outlat, outlon, me)
8316  else
8317  call fixrdc_tile(fnslpc, tile_num_ch, i_index, j_index,
8318  & kpdslp, slp, 1, len, me)
8319  endif
8320  if (me .eq. 0) write(6,*) 'climatological slope read in.'
8321  endif
8322 !
8323 ! max snow albeod
8324 !
8325  if(fnabsc(1:8).ne.' ') then
8326  if ( index(fnabsc, "tileX.nc") == 0) then ! grib file
8327  kpd7=-1
8328  call fixrdc(lugb,fnabsc,kpdabs,kpd7,kpd9,slmask,
8329  & absm,len,iret
8330  &, imsk, jmsk, slmskh, gaus,blno, blto
8331  &, outlat, outlon, me)
8332  else
8333  call fixrdc_tile(fnabsc, tile_num_ch, i_index, j_index,
8334  & kpdabs, absm, 1, len, me)
8335  endif
8336  if (me .eq. 0) write(6,*) 'climatological snoalb read in.'
8337  endif
8338 !clu ----------------------------------------------------------------------
8339 !
8340  is1 = sea1/3 + 1
8341  is2 = sea2/3 + 1
8342  if (is1 .eq. 5) is1 = 1
8343  if (is2 .eq. 5) is2 = 1
8344  do nn=1,2
8345 !
8346 ! seasonal mean climatology
8347  if(nn.eq.1) then
8348  isx=is1
8349  else
8350  isx=is2
8351  endif
8352  if(isx.eq.1) kpd9 = 12
8353  if(isx.eq.2) kpd9 = 3
8354  if(isx.eq.3) kpd9 = 6
8355  if(isx.eq.4) kpd9 = 9
8356 !
8357 ! seasonal mean climatology
8358 !
8359 ! albedo
8360 ! there are four albedo fields in this version:
8361 ! two for strong zeneith angle dependent (visible and near ir)
8362 ! and two for weak zeneith angle dependent (vis ans nir)
8363 !
8364  if (ialb == 0) then
8365  kpd7=-1
8366  do k = 1, 4
8367  call fixrdc(lugb,fnalbc,kpdalb(k),kpd7,kpd9,slmask,
8368  & alb(1,k,nn),len,iret
8369  &, imsk, jmsk, slmskh, gaus,blno, blto
8370  &, outlat, outlon, me)
8371  enddo
8372  endif
8373 !
8374 ! monthly mean climatology
8375 !
8376  mon = mon1
8377  if (nn .eq. 2) mon = mon2
8378 !cbosu
8379 !cbosu new snowfree albedo database is monthly.
8380  if (ialb == 1) then
8381  if ( index(fnalbc, "tileX.nc") == 0) then ! grib file
8382  kpd7=-1
8383  do k = 1, 4
8384  call fixrdc(lugb,fnalbc,kpdalb(k),kpd7,mon,slmask,
8385  & alb(1,k,nn),len,iret
8386  &, imsk, jmsk, slmskh, gaus,blno, blto
8387  &, outlat, outlon, me)
8388  enddo
8389  else
8390  do k = 1, 4
8391  call fixrdc_tile(fnalbc, tile_num_ch, i_index, j_index,
8392  & kpdalb(k), alb(:,k,nn), mon, len, me)
8393  enddo
8394  endif
8395  endif
8396 
8397 ! if(lprnt) print *,' mon1=',mon1,' mon2=',mon2
8398 !
8399 ! tsf at the current time t
8400 !
8401  kpd7=-1
8402  call fixrdc(lugb,fntsfc,kpdtsf,kpd7,mon,slmask,
8403  & tsf(1,nn),len,iret
8404  &, imsk, jmsk, slmskh, gaus,blno, blto
8405  &, outlat, outlon, me)
8406 ! if(lprnt) print *,' tsf=',tsf(iprnt,nn),' nn=',nn
8407 !
8408 ! tsf...at time t-deltsfc
8409 !
8410 ! fh2 = fh - deltsfc
8411 ! if (fh2 .gt. 0.0) then
8412 ! call fixrd(lugb,fntsfc,kpdtsf,lclim,slmask,
8413 ! & iy,im,id,ih,fh2,tsfcl2,len,iret
8414 ! &, imsk, jmsk, slmskh, gaus,blno, blto
8415 ! &, outlat, outlon, me)
8416 ! else
8417 ! do i=1,len
8418 ! tsfcl2(i) = tsfclm(i)
8419 ! enddo
8420 ! endif
8421 !
8422 ! soil wetness
8423 !
8424  if(fnwetc(1:8).ne.' ') then
8425  kpd7=-1
8426  call fixrdc(lugb,fnwetc,kpdwet,kpd7,mon,slmask,
8427  & wet(1,nn),len,iret
8428  &, imsk, jmsk, slmskh, gaus,blno, blto
8429  &, outlat, outlon, me)
8430  elseif(fnsmcc(1:8).ne.' ') then
8431  if (index(fnsmcc,'global_soilmcpc.1x1.grb') /= 0) then ! the old climo data
8432  kpd7=-1
8433  call fixrdc(lugb,fnsmcc,kpdsmc,kpd7,mon,slmask,
8434  & smc(1,lsoil,nn),len,iret
8435  &, imsk, jmsk, slmskh, gaus,blno, blto
8436  &, outlat, outlon, me)
8437  do l=1,lsoil-1
8438  do i = 1, len
8439  smc(i,l,nn) = smc(i,lsoil,nn)
8440  enddo
8441  enddo
8442  else ! the new gldas data. it does not have data defined at landice
8443  ! points. so for efficiency, don't have fixrdc try to
8444  ! find a value at landice points as defined by the vet type (vet).
8445  allocate(slmask_noice(len))
8446  slmask_noice=1.0
8447  do i = 1, len
8448  if (nint(vet(i)) < 1 .or.
8449  & nint(vet(i)) == landice_cat) then
8450  slmask_noice(i) = 0.0
8451  endif
8452  enddo
8453  do k = 1, lsoil
8454  if (k==1) kpd7=10 ! 0_10 cm (pds octs 11 and 12)
8455  if (k==2) kpd7=2600 ! 10_40 cm
8456  if (k==3) kpd7=10340 ! 40_100 cm
8457  if (k==4) kpd7=25800 ! 100_200 cm
8458  call fixrdc(lugb,fnsmcc,kpdsmc,kpd7,mon,slmask_noice,
8459  & smc(1,k,nn),len,iret
8460  &, imsk, jmsk, slmskh, gaus,blno, blto
8461  &, outlat, outlon, me)
8462  enddo
8463  deallocate(slmask_noice)
8464  endif
8465  else
8466  write(6,*) 'FATAL ERROR: climatological soil'
8467  write(6,*) 'wetness file not given.'
8468  call abort
8469  endif
8470 !
8471 ! soil temperature
8472 !
8473  if(fnstcc(1:8).ne.' ') then
8474  kpd7=-1
8475  call fixrdc(lugb,fnstcc,kpdstc,kpd7,mon,slmask,
8476  & stc(1,lsoil,nn),len,iret
8477  &, imsk, jmsk, slmskh, gaus,blno, blto
8478  &, outlat, outlon, me)
8479  do l=1,lsoil-1
8480  do i = 1, len
8481  stc(i,l,nn) = stc(i,lsoil,nn)
8482  enddo
8483  enddo
8484  endif
8485 !
8486 ! sea ice
8487 !
8488  kpd7=-1
8489  if(fnacnc(1:8).ne.' ') then
8490  call fixrdc(lugb,fnacnc,kpdacn,kpd7,mon,slmask,
8491  & acn(1,nn),len,iret
8492  &, imsk, jmsk, slmskh, gaus,blno, blto
8493  &, outlat, outlon, me)
8494  elseif(fnaisc(1:8).ne.' ') then
8495  call fixrdc(lugb,fnaisc,kpdais,kpd7,mon,slmask,
8496  & ais(1,nn),len,iret
8497  &, imsk, jmsk, slmskh, gaus,blno, blto
8498  &, outlat, outlon, me)
8499  else
8500  write(6,*) 'FATAL ERROR: climatological ice'
8501  write(6,*) 'cover file not given.'
8502  call abort
8503  endif
8504 !
8505 ! snow depth
8506 !
8507  kpd7=-1
8508  call fixrdc(lugb,fnsnoc,kpdsno,kpd7,mon,slmask,
8509  & sno(1,nn),len,iret
8510  &, imsk, jmsk, slmskh, gaus,blno, blto
8511  &, outlat, outlon, me)
8512 !
8513 ! snow cover
8514 !
8515  if(fnscvc(1:8).ne.' ') then
8516  kpd7=-1
8517  call fixrdc(lugb,fnscvc,kpdscv,kpd7,mon,slmask,
8518  & scv(1,nn),len,iret
8519  &, imsk, jmsk, slmskh, gaus,blno, blto
8520  &, outlat, outlon, me)
8521  write(6,*) 'climatological snow cover read in.'
8522  endif
8523 !
8524 ! surface roughness
8525 !
8526  if(fnzorc(1:3) == 'sib') then
8527  if (me == 0) then
8528  write(6,*) 'roughness length to be set from sib veg type'
8529  endif
8530  elseif(fnzorc(1:4) == 'igbp') then
8531  if (me == 0) then
8532  write(6,*) 'roughness length to be set from igbp veg type'
8533  endif
8534  else
8535  kpd7=-1
8536  call fixrdc(lugb,fnzorc,kpdzor,kpd7,mon,slmask,
8537  & zor(1,nn),len,iret
8538  &, imsk, jmsk, slmskh, gaus,blno, blto
8539  &, outlat, outlon, me)
8540  endif
8541 !
8542  do i = 1, len
8543 ! set clouds climatology to zero
8544  cvclm(i) = 0.
8545  cvbclm(i) = 0.
8546  cvtclm(i) = 0.
8547 !
8548  cnpclm(i) = 0. !set canopy water content climatology to zero
8549  enddo
8550 !
8551 ! vegetation cover
8552 !
8553  if(fnvegc(1:8).ne.' ') then
8554  if ( index(fnvegc, "tileX.nc") == 0) then ! grib file
8555  kpd7=-1
8556  call fixrdc(lugb,fnvegc,kpdveg,kpd7,mon,slmask,
8557  & veg(1,nn),len,iret
8558  &, imsk, jmsk, slmskh, gaus,blno, blto
8559  &, outlat, outlon, me)
8560  else
8561  call fixrdc_tile(fnvegc, tile_num_ch, i_index, j_index,
8562  & kpdveg, veg(:,nn), mon, len, me)
8563  endif
8564  if (me .eq. 0) write(6,*) 'climatological vegetation',
8565  & ' cover read in for mon=',mon
8566  endif
8567 
8568  enddo
8569 !
8570  mon1s = mon1 ; mon2s = mon2 ; sea1s = sea1 ; sea2s = sea2
8571 !
8572  if (me == 0) print *,' mon1s=',mon1s,' mon2s=',mon2s
8573  &,' sea1s=',sea1s,' sea2s=',sea2s
8574 !
8575  k1 = 1 ; k2 = 2
8576  m1 = 1 ; m2 = 2
8577 !
8578  first = .false.
8579  endif first_time
8580 !
8581 ! to get tsf climatology at the previous call to sfccycle
8582 !
8583 ! if (fh-deltsfc >= 0.0) then
8584  rjdayh = rjday - deltsfc/24.0
8585 ! else
8586 ! rjdayh = rjday
8587 ! endif
8588 ! if(lprnt) print *,' rjdayh=',rjdayh,' mon1=',mon1,' mon2='
8589 ! &,mon2,' mon1s=',mon1s,' mon2s=',mon2s,' k1=',k1,' k2=',k2
8590  if (rjdayh .ge. dayhf(mon1)) then
8591  if (mon2 .eq. 1) mon2 = 13
8592  wei1x = (dayhf(mon2)-rjdayh)/(dayhf(mon2)-dayhf(mon1))
8593  wei2x = 1.0 - wei1x
8594  if (mon2 .eq. 13) mon2 = 1
8595  else
8596  rjdayh2 = rjdayh
8597  if (rjdayh .lt. dayhf(1)) rjdayh2 = rjdayh2 + 365.0
8598  if (mon1s .eq. mon1) then
8599  mon1s = mon1 - 1
8600  if (mon1s .eq. 0) mon1s = 12
8601  k2 = k1
8602  k1 = mod(k2,2) + 1
8603  mon = mon1s
8604  kpd7=-1
8605  call fixrdc(lugb,fntsfc,kpdtsf,kpd7,mon,slmask,
8606  & tsf(1,k1),len,iret
8607  &, imsk, jmsk, slmskh, gaus,blno, blto
8608  &, outlat, outlon, me)
8609  endif
8610  mon2s = mon1s + 1
8611 ! if (mon2s .eq. 1) mon2s = 13
8612  wei1x = (dayhf(mon2s)-rjdayh2)/(dayhf(mon2s)-dayhf(mon1s))
8613  wei2x = 1.0 - wei1x
8614  if (mon2s .eq. 13) mon2s = 1
8615  do i=1,len
8616  tsf2(i) = wei1x * tsf(i,k1) + wei2x * tsf(i,k2)
8617  enddo
8618  endif
8619 !
8620 !cbosu new albedo is monthly
8621  if (sea1 .ne. sea1s) then
8622  sea1s = sea1
8623  sea2s = sea2
8624  m1 = mod(m1,2) + 1
8625  m2 = mod(m1,2) + 1
8626 !
8627 ! seasonal mean climatology
8628 !
8629  isx = sea2/3 + 1
8630  if (isx == 5) isx = 1
8631  if (isx == 1) kpd9 = 12
8632  if (isx == 2) kpd9 = 3
8633  if (isx == 3) kpd9 = 6
8634  if (isx == 4) kpd9 = 9
8635 !
8636 ! albedo
8637 ! there are four albedo fields in this version:
8638 ! two for strong zeneith angle dependent (visible and near ir)
8639 ! and two for weak zeneith angle dependent (vis ans nir)
8640 !
8641 !cbosu
8642  if (ialb == 0) then
8643  kpd7=-1
8644  do k = 1, 4
8645  call fixrdc(lugb,fnalbc,kpdalb(k),kpd7,kpd9,slmask
8646  &, alb(1,k,m2),len,iret
8647  &, imsk, jmsk, slmskh, gaus,blno, blto
8648  &, outlat, outlon, me)
8649  enddo
8650  endif
8651 
8652  endif
8653 
8654  if (mon1 .ne. mon1s) then
8655 
8656  mon1s = mon1
8657  mon2s = mon2
8658  k1 = mod(k1,2) + 1
8659  k2 = mod(k1,2) + 1
8660 !
8661 ! monthly mean climatology
8662 !
8663  mon = mon2
8664  nn = k2
8665 !cbosu
8666  if (ialb == 1) then
8667  if (me == 0) print*,'bosu 2nd time in clima for month ',
8668  & mon, k1,k2
8669  if ( index(fnalbc, "tileX.nc") == 0) then ! grib file
8670  kpd7 = -1
8671  do k = 1, 4
8672  call fixrdc(lugb,fnalbc,kpdalb(k),kpd7,mon,slmask,
8673  & alb(1,k,nn),len,iret
8674  &, imsk, jmsk, slmskh, gaus,blno, blto
8675  &, outlat, outlon, me)
8676  enddo
8677  else
8678  do k = 1, 4
8679  call fixrdc_tile(fnalbc, tile_num_ch, i_index, j_index,
8680  & kpdalb(k), alb(:,k,nn), mon, len, me)
8681  enddo
8682  endif
8683  endif
8684 !
8685 ! tsf at the current time t
8686 !
8687  kpd7 = -1
8688  call fixrdc(lugb,fntsfc,kpdtsf,kpd7,mon,slmask,
8689  & tsf(1,nn),len,iret
8690  &, imsk, jmsk, slmskh, gaus,blno, blto
8691  &, outlat, outlon, me)
8692 !
8693 ! soil wetness
8694 !
8695  if (fnwetc(1:8).ne.' ') then
8696  kpd7=-1
8697  call fixrdc(lugb,fnwetc,kpdwet,kpd7,mon,slmask,
8698  & wet(1,nn),len,iret
8699  &, imsk, jmsk, slmskh, gaus,blno, blto
8700  &, outlat, outlon, me)
8701  elseif (fnsmcc(1:8).ne.' ') then
8702  if (index(fnsmcc,'global_soilmcpc.1x1.grb') /= 0) then ! the old climo data
8703  kpd7=-1
8704  call fixrdc(lugb,fnsmcc,kpdsmc,kpd7,mon,slmask,
8705  & smc(1,lsoil,nn),len,iret
8706  &, imsk, jmsk, slmskh, gaus,blno, blto
8707  &, outlat, outlon, me)
8708  do l=1,lsoil-1
8709  do i = 1, len
8710  smc(i,l,nn) = smc(i,lsoil,nn)
8711  enddo
8712  enddo
8713  else ! the new gldas data. it does not have data defined at landice
8714  ! points. so for efficiency, don't have fixrdc try to
8715  ! find a value at landice points as defined by the vet type (vet).
8716  allocate(slmask_noice(len))
8717  slmask_noice=1.0
8718  do i = 1, len
8719  if (nint(vet(i)) < 1 .or.
8720  & nint(vet(i)) == landice_cat) then
8721  slmask_noice(i) = 0.0
8722  endif
8723  enddo
8724  do k = 1, lsoil
8725  if (k==1) kpd7=10 ! 0_10 cm (pds octs 11 and 12)
8726  if (k==2) kpd7=2600 ! 10_40 cm
8727  if (k==3) kpd7=10340 ! 40_100 cm
8728  if (k==4) kpd7=25800 ! 100_200 cm
8729  call fixrdc(lugb,fnsmcc,kpdsmc,kpd7,mon,slmask_noice,
8730  & smc(1,k,nn),len,iret
8731  &, imsk, jmsk, slmskh, gaus,blno, blto
8732  &, outlat, outlon, me)
8733  enddo
8734  deallocate(slmask_noice)
8735  endif
8736  else
8737  write(6,*) 'FATAL ERROR: climatological soil'
8738  write(6,*) 'wetness file not given.'
8739  call abort
8740  endif
8741 !
8742 ! sea ice
8743 !
8744  kpd7 = -1
8745  if (fnacnc(1:8).ne.' ') then
8746  call fixrdc(lugb,fnacnc,kpdacn,kpd7,mon,slmask,
8747  & acn(1,nn),len,iret
8748  &, imsk, jmsk, slmskh, gaus,blno, blto
8749  &, outlat, outlon, me)
8750  elseif (fnaisc(1:8).ne.' ') then
8751  call fixrdc(lugb,fnaisc,kpdais,kpd7,mon,slmask,
8752  & ais(1,nn),len,iret
8753  &, imsk, jmsk, slmskh, gaus,blno, blto
8754  &, outlat, outlon, me)
8755  else
8756  write(6,*) 'FATAL ERROR: climatological ice cover'
8757  write(6,*) 'file not given.'
8758  call abort
8759  endif
8760 !
8761 ! snow depth
8762 !
8763  kpd7=-1
8764  call fixrdc(lugb,fnsnoc,kpdsno,kpd7,mon,slmask,
8765  & sno(1,nn),len,iret
8766  &, imsk, jmsk, slmskh, gaus,blno, blto
8767  &, outlat, outlon, me)
8768 !
8769 ! snow cover
8770 !
8771  if (fnscvc(1:8).ne.' ') then
8772  kpd7=-1
8773  call fixrdc(lugb,fnscvc,kpdscv,kpd7,mon,slmask,
8774  & scv(1,nn),len,iret
8775  &, imsk, jmsk, slmskh, gaus,blno, blto
8776  &, outlat, outlon, me)
8777  write(6,*) 'climatological snow cover read in.'
8778  endif
8779 !
8780 ! surface roughness
8781 !
8782  if (fnzorc(1:3) == 'sib') then
8783  if (me == 0) then
8784  write(6,*) 'roughness length to be set from sib veg type'
8785  endif
8786  elseif(fnzorc(1:4) == 'igbp') then
8787  if (me == 0) then
8788  write(6,*) 'roughness length to be set from igbp veg type'
8789  endif
8790  else
8791  kpd7=-1
8792  call fixrdc(lugb,fnzorc,kpdzor,kpd7,mon,slmask,
8793  & zor(1,nn),len,iret
8794  &, imsk, jmsk, slmskh, gaus,blno, blto
8795  &, outlat, outlon, me)
8796  endif
8797 !
8798 ! vegetation cover
8799 !
8800  if (fnvegc(1:8) .ne. ' ') then
8801  if ( index(fnvegc, "tileX.nc") == 0) then ! grib file
8802  kpd7=-1
8803  call fixrdc(lugb,fnvegc,kpdveg,kpd7,mon,slmask,
8804  & veg(1,nn),len,iret
8805  &, imsk, jmsk, slmskh, gaus,blno, blto
8806  &, outlat, outlon, me)
8807  else
8808  call fixrdc_tile(fnvegc, tile_num_ch, i_index, j_index,
8809  & kpdveg, veg(:,nn), mon, len, me)
8810  endif
8811 ! if (me .eq. 0) write(6,*) 'climatological vegetation',
8812 ! & ' cover read in for mon=',mon
8813  endif
8814 !
8815  endif
8816 !
8817 ! now perform the time interpolation
8818 !
8819 ! when chosen, set the z0 based on the vegetation type.
8820 ! for this option to work, namelist variable fnvetc must be
8821 ! set to point at the proper vegetation type file.
8822  if (fnzorc(1:3) == 'sib') then
8823  if (fnvetc(1:4) == ' ') then
8824  if (me==0) then
8825  write(6,*) " FATAL ERROR: Must choose sib"
8826  write(6,*) " veg type climo file."
8827  endif
8828  call abort
8829  endif
8830  zorclm = 0.0
8831  do i=1,len
8832  ivtyp = nint(vet(i))
8833  if (ivtyp >= 1 .and. ivtyp <= 13) then
8834  zorclm(i) = z0_sib(ivtyp)
8835  endif
8836  enddo
8837  elseif(fnzorc(1:4) == 'igbp') then
8838  if (fnvetc(1:4) == ' ') then
8839  if (me == 0) then
8840  write(6,*) " FATAL ERROR: Must choose igbp"
8841  write(6,*) " veg type climo file."
8842  endif
8843  call abort
8844  endif
8845  zorclm = 0.0
8846  do i=1,len
8847  ivtyp = nint(vet(i))
8848  if (ivtyp >= 1 .and. ivtyp <= 20) then
8849  z0_season(1) = z0_igbp_min(ivtyp)
8850  z0_season(7) = z0_igbp_max(ivtyp)
8851  if (outlat(i) < 0.0) then
8852  zorclm(i) = wei1y * z0_season(hyr2) +
8853  & wei2y * z0_season(hyr1)
8854  else
8855  zorclm(i) = wei1y * z0_season(hyr1) +
8856  & wei2y * z0_season(hyr2)
8857  endif
8858  endif
8859  enddo
8860  else
8861  do i=1,len
8862  zorclm(i) = wei1m * zor(i,k1) + wei2m * zor(i,k2)
8863  enddo
8864  endif
8865 !
8866  do i=1,len
8867  tsfclm(i) = wei1m * tsf(i,k1) + wei2m * tsf(i,k2)
8868  snoclm(i) = wei1m * sno(i,k1) + wei2m * sno(i,k2)
8869  cvclm(i) = 0.0
8870  cvbclm(i) = 0.0
8871  cvtclm(i) = 0.0
8872  cnpclm(i) = 0.0
8873  tsfcl2(i) = tsf2(i)
8874  enddo
8875 ! if(lprnt) print *,' tsfclm=',tsfclm(iprnt),' wei1m=',wei1m
8876 ! &,' wei2m=',wei2m,' tsfk12=',tsf(iprnt,k1),tsf(iprnt,k2)
8877 !
8878  if (fh .eq. 0.0) then
8879  do i=1,len
8880  tsfcl0(i) = tsfclm(i)
8881  enddo
8882  endif
8883  if (rjdayh .ge. dayhf(mon1)) then
8884  do i=1,len
8885  tsf2(i) = wei1x * tsf(i,k1) + wei2x * tsf(i,k2)
8886  tsfcl2(i) = tsf2(i)
8887  enddo
8888  endif
8889 ! if(lprnt) print *,' tsf2=',tsf2(iprnt),' wei1x=',wei1x
8890 ! &,' wei2x=',wei2x,' tsfk12=',tsf(iprnt,k1),tsf(iprnt,k2)
8891 ! &,' mon1s=',mon1s,' mon2s=',mon2s
8892 ! &,' slmask=',slmask(iprnt)
8893 !
8894  if(fnacnc(1:8).ne.' ') then
8895  do i=1,len
8896  acnclm(i) = wei1m * acn(i,k1) + wei2m * acn(i,k2)
8897  enddo
8898  elseif(fnaisc(1:8).ne.' ') then
8899  do i=1,len
8900  aisclm(i) = wei1m * ais(i,k1) + wei2m * ais(i,k2)
8901  enddo
8902  endif
8903 !
8904  if(fnwetc(1:8).ne.' ') then
8905  do i=1,len
8906  wetclm(i) = wei1m * wet(i,k1) + wei2m * wet(i,k2)
8907  enddo
8908  elseif(fnsmcc(1:8).ne.' ') then
8909  do k=1,lsoil
8910  do i=1,len
8911  smcclm(i,k) = wei1m * smc(i,k,k1) + wei2m * smc(i,k,k2)
8912  enddo
8913  enddo
8914  endif
8915 !
8916  if(fnscvc(1:8).ne.' ') then
8917  do i=1,len
8918  scvclm(i) = wei1m * scv(i,k1) + wei2m * scv(i,k2)
8919  enddo
8920  endif
8921 !
8922  if(fntg3c(1:8).ne.' ') then
8923  do i=1,len
8924  tg3clm(i) = tg3(i)
8925  enddo
8926  elseif(fnstcc(1:8).ne.' ') then
8927  do k=1,lsoil
8928  do i=1,len
8929  stcclm(i,k) = wei1m * stc(i,k,k1) + wei2m * stc(i,k,k2)
8930  enddo
8931  enddo
8932  endif
8933 !
8934  if(fnvegc(1:8).ne.' ') then
8935  do i=1,len
8936  vegclm(i) = wei1m * veg(i,k1) + wei2m * veg(i,k2)
8937  enddo
8938  endif
8939 !
8940  if(fnvetc(1:8).ne.' ') then
8941  do i=1,len
8942  vetclm(i) = vet(i)
8943  enddo
8944  endif
8945 !
8946  if(fnsotc(1:8).ne.' ') then
8947  do i=1,len
8948  sotclm(i) = sot(i)
8949  enddo
8950  endif
8951 
8952 
8953 !clu ----------------------------------------------------------------------
8954 !
8955  if(fnvmnc(1:8).ne.' ') then
8956  do i=1,len
8957  vmnclm(i) = vmn(i)
8958  enddo
8959  endif
8960 !
8961  if(fnvmxc(1:8).ne.' ') then
8962  do i=1,len
8963  vmxclm(i) = vmx(i)
8964  enddo
8965  endif
8966 !
8967  if(fnslpc(1:8).ne.' ') then
8968  do i=1,len
8969  slpclm(i) = slp(i)
8970  enddo
8971  endif
8972 !
8973  if(fnabsc(1:8).ne.' ') then
8974  do i=1,len
8975  absclm(i) = absm(i)
8976  enddo
8977  endif
8978 !clu ----------------------------------------------------------------------
8979 !
8980 !cbosu diagnostic print
8981  if (me == 0) print*,'monthly albedo weights are ',
8982  & wei1m,' for k', k1, wei2m, ' for k', k2
8983 
8984  if (ialb == 1) then
8985  do k=1,4
8986  do i=1,len
8987  albclm(i,k) = wei1m * alb(i,k,k1) + wei2m * alb(i,k,k2)
8988  enddo
8989  enddo
8990  else
8991  do k=1,4
8992  do i=1,len
8993  albclm(i,k) = wei1s * alb(i,k,m1) + wei2s * alb(i,k,m2)
8994  enddo
8995  enddo
8996  endif
8997 !
8998  do k=1,2
8999  do i=1,len
9000  alfclm(i,k) = alf(i,k)
9001  enddo
9002  enddo
9003 !
9004 ! end of climatology reads
9005 !
9006  return
9007  end subroutine clima
9008 
9024  subroutine fixrdc_tile(filename_raw, tile_num_ch,
9025  & i_index, j_index, kpds,
9026  & var, mon, npts, me)
9027  use netcdf
9028  use machine , only : kind_io8
9029  implicit none
9030  character(len=*), intent(in) :: filename_raw
9031  character(len=*), intent(in) :: tile_num_ch
9032  integer, intent(in) :: npts, me, kpds, mon
9033  integer, intent(in) :: i_index(npts)
9034  integer, intent(in) :: j_index(npts)
9035  real(kind_io8), intent(out) :: var(npts)
9036  character(len=500) :: filename
9037  character(len=80) :: errmsg
9038  integer :: i, ii, ncid, t
9039  integer :: error, id_dim
9040  integer :: nx, ny, num_times
9041  integer :: id_var
9042  real(kind=4), allocatable :: dummy(:,:,:)
9043  ii=index(filename_raw,"tileX")
9044 
9045  do i = 1, len(filename)
9046  filename(i:i) = " "
9047  enddo
9048 
9049  filename = filename_raw(1:ii-1) // tile_num_ch // ".nc"
9050 
9051  if (me == 0) print*, ' in fixrdc_tile for mon=',mon,
9052  & ' filename=', trim(filename)
9053 
9054  error=nf90_open(trim(filename), nf90_nowrite, ncid)
9055  if (error /= nf90_noerr) call netcdf_err(error)
9056 
9057  error=nf90_inq_dimid(ncid, 'nx', id_dim)
9058  if (error /= nf90_noerr) call netcdf_err(error)
9059  error=nf90_inquire_dimension(ncid,id_dim,len=nx)
9060  if (error /= nf90_noerr) call netcdf_err(error)
9061 
9062  error=nf90_inq_dimid(ncid, 'ny', id_dim)
9063  if (error /= nf90_noerr) call netcdf_err(error)
9064  error=nf90_inquire_dimension(ncid,id_dim,len=ny)
9065  if (error /= nf90_noerr) call netcdf_err(error)
9066 
9067  error=nf90_inq_dimid(ncid, 'time', id_dim)
9068  if (error /= nf90_noerr) call netcdf_err(error)
9069  error=nf90_inquire_dimension(ncid,id_dim,len=num_times)
9070  if (error /= nf90_noerr) call netcdf_err(error)
9071 
9072  select case (kpds)
9073  case(11)
9074  error=nf90_inq_varid(ncid, 'substrate_temperature', id_var)
9075  case(87)
9076  error=nf90_inq_varid(ncid, 'vegetation_greenness', id_var)
9077  case(159)
9078  error=nf90_inq_varid(ncid, 'maximum_snow_albedo', id_var)
9079  case(189)
9080  error=nf90_inq_varid(ncid, 'visible_black_sky_albedo', id_var)
9081  case(190)
9082  error=nf90_inq_varid(ncid, 'visible_white_sky_albedo', id_var)
9083  case(191)
9084  error=nf90_inq_varid(ncid, 'near_IR_black_sky_albedo', id_var)
9085  case(192)
9086  error=nf90_inq_varid(ncid, 'near_IR_white_sky_albedo', id_var)
9087  case(214)
9088  error=nf90_inq_varid(ncid, 'facsf', id_var)
9089  case(224)
9090  error=nf90_inq_varid(ncid, 'soil_type', id_var)
9091  case(225)
9092  error=nf90_inq_varid(ncid, 'vegetation_type', id_var)
9093  case(236)
9094  error=nf90_inq_varid(ncid, 'slope_type', id_var)
9095  case(256:257)
9096  error=nf90_inq_varid(ncid, 'vegetation_greenness', id_var)
9097  case default
9098  print*,'FATAL ERROR in fixrdc_tile of sfcsub.F.'
9099  print*,'unknown variable.'
9100  call abort
9101  end select
9102  if (error /= nf90_noerr) call netcdf_err(error)
9103 
9104  allocate(dummy(nx,ny,1))
9105 
9106  if (kpds == 256) then ! max veg greenness
9107 
9108  var = -9999.
9109  do t = 1, num_times
9110  error=nf90_get_var(ncid, id_var, dummy, start=(/1,1,t/),
9111  & count=(/nx,ny,1/) )
9112  if (error /= nf90_noerr) call netcdf_err(error)
9113  do ii = 1,npts
9114  var(ii) = max(var(ii), dummy(i_index(ii),j_index(ii),1))
9115  enddo
9116  enddo
9117 
9118  elseif (kpds == 257) then ! min veg greenness
9119 
9120  var = 9999.
9121  do t = 1, num_times
9122  error=nf90_get_var(ncid, id_var, dummy, start=(/1,1,t/),
9123  & count=(/nx,ny,1/) )
9124  if (error /= nf90_noerr) call netcdf_err(error)
9125  do ii = 1, npts
9126  var(ii) = min(var(ii), dummy(i_index(ii),j_index(ii),1))
9127  enddo
9128  enddo
9129 
9130  else
9131 
9132  error=nf90_get_var(ncid, id_var, dummy, start=(/1,1,mon/),
9133  & count=(/nx,ny,1/) )
9134  if (error /= nf90_noerr) call netcdf_err(error)
9135 
9136  do ii = 1, npts
9137  var(ii) = dummy(i_index(ii),j_index(ii),1)
9138  enddo
9139 
9140  endif
9141 
9142  deallocate(dummy)
9143 
9144  error=nf90_close(ncid)
9145 
9146  select case (kpds)
9147  case(159) ! max snow alb
9148  var = var * 100.0
9149  case(214) ! facsf
9150  where (var < 0.0) var = 0.0
9151  var = var * 100.0
9152  case(189:192)
9153  var = var * 100.0
9154  case(256:257)
9155  var = var * 100.0
9156  end select
9157 
9158  return
9159 
9160  end subroutine fixrdc_tile
9161 
9166  subroutine netcdf_err(error)
9167 
9168  use netcdf
9169  implicit none
9170 
9171  integer,intent(in) :: error
9172  character(len=256) :: errmsg
9173 
9174  errmsg = nf90_strerror(error)
9175  print*,'FATAL ERROR in sfcsub.F: ', trim(errmsg)
9176  call abort
9177 
9178  end subroutine netcdf_err
9179 
9205  subroutine fixrdc(lugb,fngrib,kpds5,kpds7,mon,slmask,
9206  & gdata,len,iret
9207  &, imsk, jmsk, slmskh, gaus,blno, blto
9208  &, outlat, outlon, me)
9209  use machine , only : kind_io8,kind_io4
9210  use sfccyc_module, only : mdata
9211  implicit none
9212  integer imax,jmax,ijmax,i,j,n,jret,inttyp,iret,imsk,
9213  & jmsk,len,lugb,kpds5,mon,lskip,lgrib,ndata,lugi,me,kmami
9214  &, jj,w3kindreal,w3kindint
9215  real (kind=kind_io8) wlon,elon,rnlat,dlat,dlon,rslat,blno,blto
9216 !
9217  character*500 fngrib
9218 ! character*80 fngrib, asgnstr
9219 !
9220  real (kind=kind_io8) slmskh(imsk,jmsk)
9221 !
9222  real (kind=kind_io8) gdata(len), slmask(len)
9223  real (kind=kind_io8), allocatable :: data(:,:), rslmsk(:,:)
9224  real (kind=kind_io8), allocatable :: data8(:)
9225  real (kind=kind_io4), allocatable :: data4(:)
9226  real (kind=kind_io8), allocatable :: rlngrb(:), rltgrb(:)
9227 !
9228  logical lmask, yr2kc, gaus, ijordr
9229  logical*1, allocatable :: lbms(:)
9230 !
9231  integer, intent(in) :: kpds7
9232  integer kpds(1000),kgds(1000)
9233  integer jpds(1000),jgds(1000), kpds0(1000)
9234  real (kind=kind_io8) outlat(len), outlon(len)
9235 !
9236  allocate(data8(1:mdata))
9237  allocate(lbms(mdata))
9238 !
9239 ! integer imax_sv, jmax_sv, wlon_sv, rnlat_sv, kpds1_sv
9240 ! date imax_sv/0/, jmax_sv/0/, wlon_sv/999.0/, rnlat_sv/999.0/
9241 ! &, kpds1_sv/-1/
9242 ! save imax_sv, jmax_sv, wlon_sv, rnlat_sv, kpds1_sv
9243 ! &, rlngrb, rltgrb
9244 !
9245  iret = 0
9246 !
9247  if (me .eq. 0) write(6,*) ' in fixrdc for mon=',mon
9248  &,' fngrib=',trim(fngrib)
9249 !
9250  close(lugb)
9251  call baopenr(lugb,fngrib,iret)
9252  if (iret .ne. 0) then
9253  write(6,*) ' FATAL ERROR: in opening file ',trim(fngrib)
9254  print *,'FATAL ERROR: in opening file ',trim(fngrib)
9255  call abort
9256  endif
9257  if (me .eq. 0) write(6,*) ' file ',trim(fngrib),
9258  & ' opened. unit=',lugb
9259 !
9260  lugi = 0
9261 !
9262  lskip = -1
9263  jpds = -1
9264  jgds = -1
9265  jpds(5) = kpds5
9266  jpds(7) = kpds7
9267  kpds = jpds
9268  call getgbh(lugb,lugi,lskip,jpds,jgds,lgrib,ndata,
9269  & lskip,kpds,kgds,iret)
9270  if (me .eq. 0) then
9271  write(6,*) ' first grib record.'
9272  write(6,*) ' kpds( 1-10)=',(kpds(j),j= 1,10)
9273  write(6,*) ' kpds(11-20)=',(kpds(j),j=11,20)
9274  write(6,*) ' kpds(21- )=',(kpds(j),j=21,22)
9275  endif
9276  yr2kc = (kpds(8) / 100) .gt. 0
9277  kpds0 = jpds
9278  kpds0(4) = -1
9279  kpds0(18) = -1
9280  if(iret.ne.0) then
9281  write(6,*) ' FATAL ERROR: in getgbh. iret: ', iret
9282  if (iret==99) write(6,*) ' field not found.'
9283  call abort
9284  endif
9285 !
9286 ! handling climatology file
9287 !
9288  lskip = -1
9289  n = 0
9290  jpds = kpds0
9291  jpds(9) = mon
9292  if(jpds(9).eq.13) jpds(9) = 1
9293  call w3kind(w3kindreal,w3kindint)
9294  if (w3kindreal==8) then
9295  call getgb(lugb,lugi,mdata,lskip,jpds,jgds,ndata,lskip,
9296  & kpds,kgds,lbms,data8,jret)
9297  else if (w3kindreal==4) then
9298  allocate(data4(1:mdata))
9299  call getgb(lugb,lugi,mdata,lskip,jpds,jgds,ndata,lskip,
9300  & kpds,kgds,lbms,data4,jret)
9301  data8 = real(data4, kind=kind_io8)
9302  deallocate(data4)
9303  endif
9304  if (me .eq. 0) write(6,*) ' input grib file dates=',
9305  & (kpds(i),i=8,11)
9306  if(jret.eq.0) then
9307  if(ndata.eq.0) then
9308  write(6,*) ' FATAL ERROR: in getgb.'
9309  write(6,*) ' kpds=',kpds
9310  write(6,*) ' kgds=',kgds
9311  call abort
9312  endif
9313  imax=kgds(2)
9314  jmax=kgds(3)
9315  ijmax=imax*jmax
9316  allocate (data(imax,jmax))
9317  do j=1,jmax
9318  jj = (j-1)*imax
9319  do i=1,imax
9320  data(i,j) = data8(jj+i)
9321  enddo
9322  enddo
9323  if (me .eq. 0) write(6,*) 'imax,jmax,ijmax=',imax,jmax,ijmax
9324  else
9325  write(6,*) ' FATAL ERROR: in getgb - jret=', jret
9326  call abort
9327  endif
9328 !
9329 ! if (me == 0) then
9330 ! write(6,*) ' maxmin of input as is'
9331 ! kmami=1
9332 ! call maxmin(data(1,1),ijmax,kmami)
9333 ! endif
9334 !
9335  call getarea(kgds,dlat,dlon,rslat,rnlat,wlon,elon,ijordr,me)
9336  if (me == 0) then
9337  write(6,*) 'imax,jmax,ijmax,dlon,dlat,ijordr,wlon,rnlat='
9338  write(6,*) imax,jmax,ijmax,dlon,dlat,ijordr,wlon,rnlat
9339  endif
9340  call subst(data,imax,jmax,dlon,dlat,ijordr)
9341 !
9342 ! first get slmask over input grid
9343 !
9344  allocate (rlngrb(imax), rltgrb(jmax))
9345  allocate (rslmsk(imax,jmax))
9346 
9347  call setrmsk(kpds5,slmskh,imsk,jmsk,wlon,rnlat,
9348  & data,imax,jmax,rlngrb,rltgrb,lmask,rslmsk
9349  &, gaus,blno, blto, kgds(1), kpds(4), lbms)
9350 ! write(6,*) ' kpds5=',kpds5,' lmask=',lmask
9351 !
9352  inttyp = 0
9353  if(kpds5.eq.225) inttyp = 1
9354  if(kpds5.eq.230) inttyp = 1
9355  if(kpds5.eq.236) inttyp = 1
9356  if(kpds5.eq.224) inttyp = 1
9357  if (me .eq. 0) then
9358  if(inttyp.eq.1) print *, ' nearest grid point used'
9359  &, ' kpds5=',kpds5, ' lmask = ',lmask
9360  endif
9361 !
9362  call la2ga(data,imax,jmax,rlngrb,rltgrb,wlon,rnlat,inttyp,
9363  & gdata,len,lmask,rslmsk,slmask
9364  &, outlat, outlon,me)
9365 !
9366  deallocate (rlngrb, stat=iret)
9367  deallocate (rltgrb, stat=iret)
9368  deallocate (data, stat=iret)
9369  deallocate (rslmsk, stat=iret)
9370  call baclose(lugb,iret)
9371 !
9372  deallocate(data8)
9373  deallocate(lbms)
9374  return
9375  end subroutine fixrdc
9376 
9406  subroutine fixrda(lugb,fngrib,kpds5,slmask,
9407  & iy,im,id,ih,fh,gdata,len,iret
9408  &, imsk, jmsk, slmskh, gaus,blno, blto
9409  &, outlat, outlon, me)
9410  use machine , only : kind_io8,kind_io4
9411  use sfccyc_module, only : mdata
9412  implicit none
9413  integer nrepmx,nvalid,imo,iyr,idy,jret,ihr,nrept,lskip,lugi,
9414  & lgrib,j,ndata,i,inttyp,jmax,imax,ijmax,ij,jday,len,iret,
9415  & jmsk,imsk,ih,kpds5,lugb,iy,id,im,jh,jd,jdoy,jdow,jm,me,
9416  & monend,jy,iy4,kmami,iret2,jj,w3kindreal,w3kindint
9417  real (kind=kind_io8) rnlat,rslat,wlon,elon,dlon,dlat,fh,blno,
9418  & rjday,blto
9419 !
9420 ! nrepmx: max number of days for going back date search
9421 ! nvalid: analysis later than (current date - nvalid) is regarded as
9422 ! valid for current analysis
9423 !
9424  parameter(nrepmx=15, nvalid=4)
9425 !
9426  character*500 fngrib
9427 ! character*80 fngrib, asgnstr
9428 !
9429  real (kind=kind_io8) slmskh(imsk,jmsk)
9430 !
9431  real (kind=kind_io8) gdata(len), slmask(len)
9432  real (kind=kind_io8), allocatable :: data(:,:),rslmsk(:,:)
9433  real (kind=kind_io8), allocatable :: data8(:)
9434  real (kind=kind_io4), allocatable :: data4(:)
9435  real (kind=kind_io8), allocatable :: rlngrb(:), rltgrb(:)
9436 !
9437  logical lmask, yr2kc, gaus, ijordr
9438  logical*1 lbms(mdata)
9439 !
9440  integer kpds(1000),kgds(1000)
9441  integer jpds(1000),jgds(1000), kpds0(1000)
9442  real (kind=kind_io8) outlat(len), outlon(len)
9443 !
9444 ! dayhf : julian day of the middle of each month
9445 !
9446  real (kind=kind_io8) dayhf(13)
9447  data dayhf/ 15.5, 45.0, 74.5,105.0,135.5,166.0,
9448  & 196.5,227.5,258.0,288.5,319.0,349.5,380.5/
9449 !
9450 ! mjday : number of days in a month
9451 !
9452  integer mjday(12)
9453  data mjday/31,28,31,30,31,30,31,31,30,31,30,31/
9454 !
9455  real (kind=kind_io8) fha(5)
9456  real(4) fha4(5)
9457  integer ida(8),jda(8)
9458 !
9459  allocate(data8(1:mdata))
9460  iret = 0
9461  monend = 9999
9462 !
9463 ! compute jy,jm,jd,jh of forecast and the day of the year
9464 !
9465  iy4=iy
9466  if(iy.lt.101) iy4=1900+iy4
9467  fha=0
9468  ida=0
9469  jda=0
9470  fha(2)=nint(fh)
9471  ida(1)=iy
9472  ida(2)=im
9473  ida(3)=id
9474  ida(5)=ih
9475  call w3kind(w3kindreal,w3kindint)
9476  if(w3kindreal==4) then
9477  fha4=fha
9478  call w3movdat(fha4,ida,jda)
9479  else
9480  call w3movdat(fha,ida,jda)
9481  endif
9482  jy=jda(1)
9483  jm=jda(2)
9484  jd=jda(3)
9485  jh=jda(5)
9486 ! if (me .eq. 0) write(6,*) ' forecast jy,jm,jd,jh,rjday=',
9487 ! & jy,jm,jd,jh,rjday
9488  jdow = 0
9489  jdoy = 0
9490  jday = 0
9491  call w3doxdat(jda,jdow,jdoy,jday)
9492  rjday=jdoy+jda(5)/24.
9493  if(rjday.lt.dayhf(1)) rjday=rjday+365.
9494 
9495  if (me .eq. 0) write(6,*) ' forecast jy,jm,jd,jh,rjday=',
9496  & jy,jm,jd,jh,rjday
9497 !
9498  if (me .eq. 0) then
9499  write(6,*) 'forecast jy,jm,jd,jh=',jy,jm,jd,jh
9500 !
9501  write(6,*) ' '
9502  write(6,*) '************************************************'
9503  endif
9504 !
9505  close(lugb)
9506  call baopenr(lugb,fngrib,iret)
9507  if (iret .ne. 0) then
9508  write(6,*) ' FATAL ERROR: in opening file ',trim(fngrib)
9509  print *,' FATAL ERROR: in opening file ',trim(fngrib)
9510  call abort
9511  endif
9512  if (me .eq. 0) write(6,*) ' file ',trim(fngrib),
9513  & ' opened. unit=',lugb
9514 !
9515  lugi = 0
9516 !
9517  lskip=-1
9518  jpds=-1
9519  jgds=-1
9520  jpds(5)=kpds5
9521  kpds = jpds
9522  call getgbh(lugb,lugi,lskip,jpds,jgds,lgrib,ndata,
9523  & lskip,kpds,kgds,iret)
9524  if (me .eq. 0) then
9525  write(6,*) ' first grib record.'
9526  write(6,*) ' kpds( 1-10)=',(kpds(j),j= 1,10)
9527  write(6,*) ' kpds(11-20)=',(kpds(j),j=11,20)
9528  write(6,*) ' kpds(21- )=',(kpds(j),j=21,22)
9529  endif
9530  yr2kc = (kpds(8) / 100) .gt. 0
9531  kpds0=jpds
9532  kpds0(4)=-1
9533  kpds0(18)=-1
9534  if(iret.ne.0) then
9535  write(6,*) ' FATAL ERROR: in getgbh. iret: ', iret
9536  if(iret==99) write(6,*) ' field not found.'
9537  call abort
9538  endif
9539 !
9540 ! handling analysis file
9541 !
9542 ! find record for the given hour/day/month/year
9543 !
9544  nrept=0
9545  jpds=kpds0
9546  lskip = -1
9547  iyr=jy
9548  if(iyr.le.100) iyr=2050-mod(2050-iyr,100)
9549  imo=jm
9550  idy=jd
9551  ihr=jh
9552 ! year 2000 compatible data
9553  if (yr2kc) then
9554  jpds(8) = iyr
9555  else
9556  jpds(8) = mod(iyr,1900)
9557  endif
9558  50 continue
9559  jpds( 8)=mod(iyr-1,100)+1
9560  jpds( 9)=imo
9561  jpds(10)=idy
9562 ! jpds(11)=ihr
9563  jpds(21)=(iyr-1)/100+1
9564  call w3kind(w3kindreal,w3kindint)
9565  if (w3kindreal == 8) then
9566  call getgb(lugb,lugi,mdata,lskip,jpds,jgds,ndata,lskip,
9567  & kpds,kgds,lbms,data8,jret)
9568  elseif (w3kindreal == 4) then
9569  allocate (data4(1:mdata))
9570  call getgb(lugb,lugi,mdata,lskip,jpds,jgds,ndata,lskip,
9571  & kpds,kgds,lbms,data4,jret)
9572  data8 = real(data4, kind=kind_io8)
9573  deallocate(data4)
9574  endif
9575  if (me .eq. 0) write(6,*) ' input grib file dates=',
9576  & (kpds(i),i=8,11)
9577  if(jret.eq.0) then
9578  if(ndata.eq.0) then
9579  write(6,*) ' FATAL ERROR: in getgb.'
9580  write(6,*) ' kpds=',kpds
9581  write(6,*) ' kgds=',kgds
9582  call abort
9583  endif
9584  imax=kgds(2)
9585  jmax=kgds(3)
9586  ijmax=imax*jmax
9587  allocate (data(imax,jmax))
9588  do j=1,jmax
9589  jj = (j-1)*imax
9590  do i=1,imax
9591  data(i,j) = data8(jj+i)
9592  enddo
9593  enddo
9594  else
9595  if(nrept.eq.0) then
9596  if (me .eq. 0) then
9597  write(6,*) ' no matching dates found. start searching',
9598  & ' nearest matching dates (going back).'
9599  endif
9600  endif
9601 !
9602 ! no matching ih found. search nearest hour
9603 !
9604  if(ihr.eq.6) then
9605  ihr=0
9606  go to 50
9607  elseif(ihr.eq.12) then
9608  ihr=0
9609  go to 50
9610  elseif(ihr.eq.18) then
9611  ihr=12
9612  go to 50
9613  elseif(ihr.eq.0.or.ihr.eq.-1) then
9614  idy=idy-1
9615  if(idy.eq.0) then
9616  imo=imo-1
9617  if(imo.eq.0) then
9618  iyr=iyr-1
9619  if(iyr.lt.0) iyr=99
9620  imo=12
9621  endif
9622  idy=31
9623  if(imo.eq.4.or.imo.eq.6.or.imo.eq.9.or.imo.eq.11) idy=30
9624  if(imo.eq.2) then
9625  if(mod(iyr,4).eq.0) then
9626  idy=29
9627  else
9628  idy=28
9629  endif
9630  endif
9631  endif
9632  ihr=-1
9633  if (me .eq. 0) write(6,*) ' decremented dates=',
9634  & iyr,imo,idy,ihr
9635  nrept=nrept+1
9636  if(nrept.gt.nvalid) iret=-1
9637  if(nrept.gt.nrepmx) then
9638  if (me .eq. 0) then
9639  write(6,*) ' <warning:cycl> searching range exceeded.'
9640  &, ' may be wrong grib file given'
9641  write(6,*) ' <warning:cycl> fngrib=',trim(fngrib)
9642  write(6,*) ' <warning:cycl> terminating search and',
9643  & ' and setting gdata to -999'
9644  write(6,*) ' range max=',nrepmx
9645  endif
9646 ! imax=kgds(2)
9647 ! jmax=kgds(3)
9648 ! ijmax=imax*jmax
9649 ! do ij=1,ijmax
9650 ! data(ij)=0.
9651 ! enddo
9652  go to 100
9653  endif
9654  go to 50
9655  else
9656  if (me .eq. 0) then
9657  write(6,*) ' search of analysis for ihr=',ihr,' failed.'
9658  write(6,*) ' kpds=',kpds
9659  write(6,*) ' iyr,imo,idy,ihr=',iyr,imo,idy,ihr
9660  endif
9661  go to 100
9662  endif
9663  endif
9664 !
9665  80 continue
9666 ! if (me == 0) then
9667 ! write(6,*) ' maxmin of input as is'
9668 ! kmami=1
9669 ! call maxmin(data(1,1),ijmax,kmami)
9670 ! endif
9671 !
9672  call getarea(kgds,dlat,dlon,rslat,rnlat,wlon,elon,ijordr,me)
9673  if (me == 0) then
9674  write(6,*) 'imax,jmax,ijmax,dlon,dlat,ijordr,wlon,rnlat='
9675  write(6,*) imax,jmax,ijmax,dlon,dlat,ijordr,wlon,rnlat
9676  endif
9677  call subst(data,imax,jmax,dlon,dlat,ijordr)
9678 !
9679 ! first get slmask over input grid
9680 !
9681  allocate (rlngrb(imax), rltgrb(jmax))
9682  allocate (rslmsk(imax,jmax))
9683  call setrmsk(kpds5,slmskh,imsk,jmsk,wlon,rnlat,
9684  & data,imax,jmax,rlngrb,rltgrb,lmask,rslmsk
9685 ! & data,imax,jmax,abs(dlon),abs(dlat),lmask,rslmsk
9686 !cggg &, gaus,blno, blto, kgds(1))
9687  &, gaus,blno, blto, kgds(1), kpds(4), lbms)
9688 
9689 ! write(6,*) ' kpds5=',kpds5,' lmask=',lmask
9690 !
9691  inttyp = 0
9692  if(kpds5.eq.225) inttyp = 1
9693  if(kpds5.eq.230) inttyp = 1
9694  if(kpds5.eq.66) inttyp = 1
9695  if(inttyp.eq.1) print *, ' nearest grid point used'
9696 !
9697  call la2ga(data,imax,jmax,rlngrb,rltgrb,wlon,rnlat,inttyp,
9698  & gdata,len,lmask,rslmsk,slmask
9699  &, outlat, outlon, me)
9700 !
9701  deallocate (rlngrb, stat=iret)
9702  deallocate (rltgrb, stat=iret)
9703  deallocate (data, stat=iret)
9704  deallocate (rslmsk, stat=iret)
9705  call baclose(lugb,iret2)
9706 ! write(6,*) ' '
9707  deallocate(data8)
9708  return
9709 !
9710  100 continue
9711  iret=1
9712  do i=1,len
9713  gdata(i) = -999.
9714  enddo
9715 !
9716  call baclose(lugb,iret2)
9717 !
9718  deallocate(data8)
9719  return
9720  end subroutine fixrda
9721 
9730  subroutine snodpth2(glacir,snwmax,snoanl, len, me)
9731  use machine , only : kind_io8,kind_io4
9732  implicit none
9733  integer i,me,len
9734  real (kind=kind_io8) snwmax
9735 !
9736  real (kind=kind_io8) snoanl(len), glacir(len)
9737 !
9738  if (me .eq. 0) write(6,*) 'snodpth2'
9739 !
9740  do i=1,len
9741 !
9742 ! if glacial points has snow in climatology, set sno to snomax
9743 !
9744  if(glacir(i).ne.0..and.snoanl(i).lt.snwmax*0.5) then
9745  snoanl(i) = snwmax + snoanl(i)
9746  endif
9747 !
9748  enddo
9749  return
9750  end
subroutine setlsi(slmask, aisfld, len, aicice, slifld)
Set land-sea-ice mask at sea ice.
Definition: sfcsub.F:5953
integer function num_parthds()
Return the number of omp threads.
Definition: num_parthds.f90:9
subroutine snodpth(scvanl, slianl, tsfanl, snoclm, glacir, snwmax, snwmin, landice, len, snoanl, me)
Estimate snow depth at glacial, land and sea ice points.
Definition: sfcsub.F:4978
subroutine getstc(tsffld, tg3fld, slifld, len, lsoil, stcfld, tsfimx)
Set soil temperature and sea ice column temperature.
Definition: sfcsub.F:6487
subroutine hmskrd(lugb, imsk, jmsk, fnmskh, kpds5, slmskh, gausm, blnmsk, bltmsk, me)
Read a high-resolution land mask.
Definition: sfcsub.F:2792
subroutine snodpth2(glacir, snwmax, snoanl, len, me)
Ensure deep snow pack at permanent glacial points.
Definition: sfcsub.F:9730
subroutine count(slimsk, sno, ijmax)
Counts the number of model points that are snow covered land, snow-free land, open water...
Definition: sfcsub.F:2626
subroutine getscv(snofld, scvfld, len)
Set snow cover flag based on snow depth.
Definition: sfcsub.F:6463
subroutine qcbyfc(tsffcs, snofcs, qctsfs, qcsnos, qctsfi, len, lsoil, snoanl, aisanl, slianl, tsfanl, albanl, zoranl, smcanl, smcclm, tsfsmx, albomx, zoromx, me)
Quality control analysis fields using the first guess.
Definition: sfcsub.F:6766
subroutine snosfc(snoanl, tsfanl, tsfsmx, len, me)
Check skin temperature at points with snow.
Definition: sfcsub.F:6593
subroutine fixrdc_tile(filename_raw, tile_num_ch, i_index, j_index, kpds, var, mon, npts, me)
Reads in climatological data on the model grid tile for a given month.
Definition: sfcsub.F:9024
subroutine analy(lugb, iy, im, id, ih, fh, len, lsoil, slmask, fntsfa, fnweta, fnsnoa, fnzora, fnalba, fnaisa, fntg3a, fnscva, fnsmca, fnstca, fnacna, fnvega, fnveta, fnsota, fnvmna, fnvmxa, fnslpa, fnabsa, tsfanl, wetanl, snoanl, zoranl, albanl, aisanl, tg3anl, cvanl, cvbanl, cvtanl, smcanl, stcanl, slianl, scvanl, acnanl, veganl, vetanl, sotanl, alfanl, tsfan0, vmnanl, vmxanl, slpanl, absanl, kpdtsf, kpdwet, kpdsno, kpdsnd, kpdzor, kpdalb, kpdais, kpdtg3, kpdscv, kpdacn, kpdsmc, kpdstc, kpdveg, kprvet, kpdsot, kpdalf, kpdvmn, kpdvmx, kpdslp, kpdabs, irttsf, irtwet, irtsno, irtzor, irtalb, irtais, irttg3, irtscv, irtacn, irtsmc, irtstc, irtveg, irtvet, irtsot, irtalf, irtvmn, irtvmx, irtslp, irtabs, imsk, jmsk, slmskh, outlat, outlon, gaus, blno, blto, me, lanom)
Read analysis fields.
Definition: sfcsub.F:4072
subroutine anomint(tsfan0, tsfclm, tsfcl0, tsfanl, len)
Add initial SST anomaly to date interpolated climatology.
Definition: sfcsub.F:7699
subroutine qcmxice(glacir, amxice, len, me)
Quality control maximum ice extent.
Definition: sfcsub.F:6649
subroutine rof01(aisfld, len, op, crit)
Round a field up to one or down to zero.
Definition: sfcsub.F:4890
subroutine albocn(albclm, slmask, albomx, len)
Set the albedo at open water points.
Definition: sfcsub.F:6625
subroutine qcsice(ais, glacir, amxice, aicice, aicsea, sllnd, slmask, rla, rlo, len, me)
Check the sea ice cover mask against the land-sea mask.
Definition: sfcsub.F:5844
subroutine setzro(fld, eps, len)
Set a field to zero if it is less than a threshold.
Definition: sfcsub.F:6445
subroutine netcdf_err(error)
Print the error message for a given netCDF return code.
Definition: sfcsub.F:9166
Holds machine dependent constants for global_cycle.
Definition: machine.f90:7
subroutine fixrdc(lugb, fngrib, kpds5, kpds7, mon, slmask, gdata, len, iret, imsk, jmsk, slmskh, gaus, blno, blto, outlat, outlon, me)
Read in grib1 climatology data for a specific month and and horizontally interpolate to the model gri...
Definition: sfcsub.F:9205
subroutine filanl(tsfanl, tsfan2, wetanl, snoanl, zoranl, albanl, aisanl, tg3anl, cvanl, cvbanl, cvtanl, cnpanl, smcanl, stcanl, slianl, scvanl, veganl, vetanl, sotanl, alfanl, sihanl, sicanl, vmnanl, vmxanl, slpanl, absanl, tsfclm, tsfcl2, wetclm, snoclm, zorclm, albclm, aisclm, tg3clm, cvclm, cvbclm, cvtclm, cnpclm, smcclm, stcclm, sliclm, scvclm, vegclm, vetclm, sotclm, alfclm, sihclm, sicclm, vmnclm, vmxclm, slpclm, absclm, len, lsoil)
Fill in analysis arrays with climatology before reading analysis data.
Definition: sfcsub.F:3852
subroutine clima(lugb, iy, im, id, ih, fh, len, lsoil, slmask, fntsfc, fnwetc, fnsnoc, fnzorc, fnalbc, fnaisc, fntg3c, fnscvc, fnsmcc, fnstcc, fnacnc, fnvegc, fnvetc, fnsotc, fnvmnc, fnvmxc, fnslpc, fnabsc, tsfclm, tsfcl2, wetclm, snoclm, zorclm, albclm, aisclm, tg3clm, cvclm, cvbclm, cvtclm, cnpclm, smcclm, stcclm, sliclm, scvclm, acnclm, vegclm, vetclm, sotclm, alfclm, vmnclm, vmxclm, slpclm, absclm, kpdtsf, kpdwet, kpdsno, kpdzor, kpdalb, kpdais, kpdtg3, kpdscv, kpdacn, kpdsmc, kpdstc, kpdveg, kpdvet, kpdsot, kpdalf, tsfcl0, kpdvmn, kpdvmx, kpdslp, kpdabs, deltsfc, lanom, imsk, jmsk, slmskh, outlat, outlon, gaus, blno, blto, me, lprnt, iprnt, fnalbc2, ialb, tile_num_ch, i_index, j_index)
Driver routine that reads in climatological data for a given time, and, if necessary, interpolates it to the model grid.
Definition: sfcsub.F:7828
subroutine setrmsk(kpds5, slmask, igaul, jgaul, wlon, rnlat, data, imax, jmax, rlnout, rltout, lmask, rslmsk, gaus, blno, blto, kgds1, kpds4, lbms)
Set the mask for the input data.
Definition: sfcsub.F:6888
subroutine landtyp(vegtype, soiltype, slptype, slmask, len)
Set vegetation, soil and slope type at undefined model points.
Definition: sfcsub.F:7643
subroutine getarea(kgds, dlat, dlon, rslat, rnlat, wlon, elon, ijordr, me)
For a given GRIB1 grid description section array, determine some grid specifications.
Definition: sfcsub.F:2970
subroutine subst(data, imax, jmax, dlon, dlat, ijordr)
Take an array of data on a lat/lon based grid and rearrange it so the corner point is in the &#39;lower l...
Definition: sfcsub.F:3130
subroutine monitr(lfld, fld, slimsk, sno, ijmax)
Determine the maximum and minimum values of a surface field at snow-free and snow covered land...
Definition: sfcsub.F:2689
subroutine scale(fld, len, scl)
Multiply a field by a scaling factor.
Definition: sfcsub.F:5978
subroutine qcmxmn(ttl, fld, slimsk, sno, iceflg, fldlmx, fldlmn, fldomx, fldomn, fldimx, fldimn, fldjmx, fldjmn, fldsmx, fldsmn, epsfld, rla, rlo, len, mode, percrit, lgchek, me)
Range check a field.
Definition: sfcsub.F:6021
subroutine fixrda(lugb, fngrib, kpds5, slmask, iy, im, id, ih, fh, gdata, len, iret, imsk, jmsk, slmskh, gaus, blno, blto, outlat, outlon, me)
Read in grib1 analysis data for the requested date and horizontally interpolate to the model grid...
Definition: sfcsub.F:9406
subroutine dayoyr(iyr, imo, idy, ldy)
Compute day of the year based on month and day.
Definition: sfcsub.F:2761
This program runs in two different modes:
Definition: sfcsub.F:46
subroutine tsfcor(tsfc, orog, slmask, umask, len, rlapse)
Adjust skin temperature or SST for terrain.
Definition: sfcsub.F:4948
subroutine merge(len, lsoil, iy, im, id, ih, fh, deltsfc, sihfcs, sicfcs, vmnfcs, vmxfcs, slpfcs, absfcs, tsffcs, wetfcs, snofcs, zorfcs, albfcs, aisfcs, cvfcs, cvbfcs, cvtfcs, cnpfcs, smcfcs, stcfcs, slifcs, vegfcs, vetfcs, sotfcs, alffcs, sihanl, sicanl, vmnanl, vmxanl, slpanl, absanl, tsfanl, tsfan2, wetanl, snoanl, zoranl, albanl, aisanl, cvanl, cvbanl, cvtanl, cnpanl, smcanl, stcanl, slianl, veganl, vetanl, sotanl, alfanl, ctsfl, calbl, caisl, csnol, csmcl, czorl, cstcl, cvegl, ctsfs, calbs, caiss, csnos, csmcs, czors, cstcs, cvegs, ccv, ccvb, ccvt, ccnp, cvetl, cvets, csotl, csots, calfl, calfs, csihl, csihs, csicl, csics, cvmnl, cvmns, cvmxl, cvmxs, cslpl, cslps, cabsl, cabss, irttsf, irtwet, irtsno, irtzor, irtalb, irtais, irttg3, irtscv, irtacn, irtsmc, irtstc, irtveg, irtvmn, irtvmx, irtslp, irtabs, irtvet, irtsot, irtalf, landice, me)
Blend the model forecast (or first guess) fields with the analysis/climatology.
Definition: sfcsub.F:5192
subroutine getsmc(wetfld, len, lsoil, smcfld, me)
Set soil moisture from soil wetness.
Definition: sfcsub.F:6529
subroutine maxmin(f, imax, kmax)
Compute the maxmimum and minimum of a field.
Definition: sfcsub.F:3752
subroutine newice(slianl, slifcs, tsfanl, tsffcs, len, lsoil, sihnew, sicnew, sihanl, sicanl, albanl, snoanl, zoranl, smcanl, stcanl, albsea, snosea, zorsea, smcsea, smcice, tsfmin, tsfice, albice, zorice, tgice, rla, rlo, me)
Adjust surface fields when ice melts or forms.
Definition: sfcsub.F:5676
subroutine gaulat(gaul, k)
Calculate gaussian latitudes.
Definition: sfcsub.F:7667
subroutine filfcs(tsffcs, wetfcs, snofcs, zorfcs, albfcs, tg3fcs, cvfcs, cvbfcs, cvtfcs, cnpfcs, smcfcs, stcfcs, slifcs, aisfcs, vegfcs, vetfcs, sotfcs, alffcs, sihfcs, sicfcs, vmnfcs, vmxfcs, slpfcs, absfcs, tsfanl, wetanl, snoanl, zoranl, albanl, tg3anl, cvanl, cvbanl, cvtanl, cnpanl, smcanl, stcanl, slianl, aisanl, veganl, vetanl, sotanl, alfanl, sihanl, sicanl, vmnanl, vmxanl, slpanl, absanl, len, lsoil)
Fill in model grid guess arrays with analysis values if this is a dead start.
Definition: sfcsub.F:4793
subroutine usesgt(sig1t, slianl, tg3anl, len, lsoil, tsfanl, stcanl, tsfimx)
Set soil temperature and sea ice column temperature for a dead start.
Definition: sfcsub.F:6560
subroutine ga2la(gauin, imxin, jmxin, regout, imxout, jmxout, wlon, rnlat, rlnout, rltout, gaus, blno, blto)
Interpolation from lat/lon or gaussian grid to a lat/lon grid.
Definition: sfcsub.F:7388
subroutine qcsli(slianl, slifcs, len, me)
Check consistency between the forecast and analysis land-sea-ice mask.
Definition: sfcsub.F:6678
subroutine sfccycle(lugb, len, lsoil, sig1t, deltsfc, iy, im, id, ih, fh, rla, rlo, slmask, orog, orog_uf, use_ufo, nst_anl, sihfcs, sicfcs, sitfcs, swdfcs, slcfcs, vmnfcs, vmxfcs, slpfcs, absfcs, tsffcs, snofcs, zorfcs, albfcs, tg3fcs, cnpfcs, smcfcs, stcfcs, slifcs, aisfcs, vegfcs, vetfcs, sotfcs, alffcs, cvfcs, cvbfcs, cvtfcs, me, nlunit, sz_nml, input_nml_file, ialb, isot, ivegsrc, tile_num_ch, i_index, j_index)
Surface cycling driver routine.
Definition: sfcsub.F:175
subroutine la2ga(regin, imxin, jmxin, rinlon, rinlat, rlon, rlat, inttyp, gauout, len, lmask, rslmsk, slmask, outlat, outlon, me)
Interpolate data from a lat/lon grid to the model grid.
Definition: sfcsub.F:3210
subroutine fixrdg(lugb, idim, jdim, fngrib, kpds5, gdata, gaus, blno, blto, me)
Read a GRIB1 file.
Definition: sfcsub.F:2840
subroutine qcsnow(snoanl, slmask, aisanl, glacir, len, snoval, landice, me)
Quality control snow at the model points.
Definition: sfcsub.F:5783