WAVEWATCH III  beta 0.0.1
ww3_uprstr.F90
Go to the documentation of this file.
1 
5 
6 #include "w3macros.h"
7 !/ ------------------------------------------------------------------- /
22 PROGRAM w3uprstr
23  !/
24  !/ +-----------------------------------+
25  !/ | WAVEWATCH III NOAA/NCEP |
26  !/ | Stelios Flampouris |
27  !/ | FORTRAN 90 |
28  !/ | First version: 16-Feb-2017 |
29  !/ +-----------------------------------+
30  !/
31  !/ 16-Feb-2017 : Original Code ( version 6.03 )
32  !/ 07-Jun-2017 : Change of the Core
33  !/ 07-Jul-2017 : Clean the code, add some flexibility, etc
34  !/ 04-Sep-2017 : Simplified the code, take out a significant part of the
35  !/ flexibility (The code is still available at SVN/UpRest)
36  !/ 15-Sep-2017 : Version 0.65 ( version 6.03 )
37  !/ 01-Oct-2018 : Fixes to preserve spectral energy correctly
38  !/ (Andy Saulter) ( version 6.06 )
39  !/ 17-Oct-2018 : Version 0.95 ( version 6.06 )
40  !/ Simplified the code, remove some user unfriendly
41  !/ options, add reg test ta1, add logical checks,
42  !/ unified the operator, add/update the documentation.
43  !/ 05-Oct-2019 : Added UPD5 and UPD6 options, plus logic for running
44  !/ with SMC grids (Andy Saulter) ( version 6.07 )
45  !/ 01-Nov-2019 : UPD5 and UPD6 use wind data either from anl.XXX file
46  !/ or from restart under WRST switch (Andy Saulter)
47  !/ 06-Oct-2020 : Added namelist input options ( version 7.11 )
48  !/ 06-May-2021 : Use SMCTYPE and FSWND for SMC grid. ( version 7.13 )
49  !/
50  !/ Copyright 2010 National Weather Service (NWS),
51  !/ National Oceanic and Atmospheric Administration. All rights
52  !/ reserved. WAVEWATCH III is a trademark of the NWS.
53  !/ No unauthorized use without permission.
54  !/
55  ! 1. Purpose :
56  !
57  ! Update the WAVEWATCH III restart files based on the significant
58  ! wave height analysis from any data assimilation system.
59  !
60  ! 2. Method :
61  !
62  ! 2.1. General:
63  ! The W3UPRSTR is the intermediator between the background WW3
64  ! and the analysis of the wave field, it modifies the original restart
65  ! file according to the analysis.
66  ! For the wave modeling and DA, the ww3_uprstr program applies the
67  ! operator from the diagnostic to the prognostic variable.
68  !
69  ! See the chart below:
70  !
71  ! +-------------------+
72  ! | WW3 Background Run|
73  ! +-------------------+
74  ! +--------------+ | +-----+
75  ! | Restart File | <------------|-----------------> | Hs |
76  ! +--------------+ +-----+
77  ! | |
78  ! | |
79  ! | +---------------+ +-----+
80  ! | |Hs Observations|-------> | D.A.|
81  ! | +---------------+ +-----+
82  ! | |
83  ! | +----------+ |
84  ! +----------------> | W3UPRSTR |<-/Analysis/--+
85  ! +----------+
86  ! |
87  ! +----------------------+
88  ! | Updated Restart File |
89  ! +----------------------+
90  !
91  ! A. The WW3 Background Run has to provide two files:
92  ! i. The field of Hs (for NCEP at grib2 format) and
93  ! ii. the restart.ww3, at the WW3 format for restart files.
94  ! Both of them, at the moment of the assimilation (Nevertheless, the WW3
95  ! restart reader will fail when the timestamps are not identical).
96  !
97  ! B. The DA module produces the analysis and/or the difference (%) of
98  ! the analysis from the first guess of Hs in the space of model and
99  ! exports the results.
100  !
101  ! C. The algorithm
102  ! The Hs correction is redistributed to each frequency and direction.
103  !
104  ! 1. The W3UPRSTR imports: i. the restart.ww3,
105  ! ii. the analysis file and
106  ! iii. the input file: ww3_uprstr.inp, details at 2.2.2.i.
107  !
108  ! 2. The W3UPRSTR updates the restart file according to the option at
109  ! ww3_uprstr UPD[N]
110  ! Note: With the version 6.06 some options have been removed, but the naming
111  ! is consistent with the original version.
112  !
113  ! 3. W3UPRSTR exports the updated spectrum in the same format as the
114  ! restart.ww3. The name of the output file is: restart001.ww3 and it has to
115  ! be renamed "restart.ww3" for the initialization of the next prediction
116  ! cycle.
117  !
118  ! E. The user runs WW3 with the analysis restart file.
119  !
120  ! 2.2. How to use ww3_uprstr
121  ! The ww3_uperstr is one of the WW3 auxilary programs, therefore it works in
122  ! a very similar way as the other auxilary programs.
123  !
124  ! A. To compile:
125  !
126  ! ww3_uprstr is included in the make_makefile.sh, to compile:
127  ! $ ./w3_make ww3_uprstr
128  ! or
129  ! $ ./w3_make
130  !
131  ! And the executable "ww3_uprstr" will appear at [...]/model/exe/
132  !
133  ! B. To run:
134  ! At the computational path:
135  ! > ${EXE}/ww3_uprstr
136  ! And it should run if the input files are at ./
137  !
138  ! C. Input Files:
139  !
140  ! i. ww3_uprstr.inp
141  ! It includes some limited information for running the program:
142  !
143  ! -------------------------------------------------------------------- $
144  ! WAVEWATCH III Update Restart input file $
145  ! -------------------------------------------------------------------- $
146  !
147  ! Time of Assimilation ----------------------------------------------- $
148  ! - Starting time in yyyymmdd hhmmss format.
149  !
150  ! This is the assimilation starting time and has to be the same with
151  ! the time at the restart.ww3.
152  !
153  ! 19680607 120000
154  !
155  ! Choose algorithm to update restart file
156  ! UPDN for the Nth approach
157  ! The UPDN*, with N<2 the same correction factor is applied at all the grid points
158  ! UPD0C:: ELIMINATED
159  ! UPDOF:: Option 0F All the spectra are updated with a constant
160  ! fac=HsAnl/HsBckg.
161  ! Expected input: PRCNTG, as defined at fac
162  ! UPD1 :: ELIMINATED
163  ! UPDN, with N>1 each gridpoint has its own update factor.
164  ! UPD2 :: Option 2 The fac(x,y,frq,theta), is calculated at each grid point
165  ! according to the ratio of HsBckg and HsAnl (squared to preseve energy)
166  ! Expected input: the Analysis field, grbtxt format
167  ! UPD3 :: Option 3 The update factor is a surface with the shape of
168  ! the background spectrum.
169  ! Expected input: the Analysis field, grbtxt format
170  ! UPD4 :: [NOT INCLUDED in this Version, Just keeping the spot]
171  ! Option 4 The generalization of the UPD3. The update factor
172  ! is the sum of surfaces which are applied on the background
173  ! spectrum.
174  ! The algorithm requires the mapping of each partition on the
175  ! individual spectra; the map is used to determine the weighting
176  ! surfaces.
177  ! Expected input: the Analysis field, grbtxt format and the
178  ! functions(frq,theta) of the update to be applied.
179  ! UPD5 :: Option 5 Corrections are calculated as per UPD2 but are
180  ! applied to wind-sea parts of the spectrum only when wind-sea
181  ! is the dominant component, otherwise the whole spectrum is
182  ! corrected
183  ! Expected input: the Analysis Hs field plus background wind speed
184  ! and direction
185  ! UPD6 :: Option 6 Corrections are calculated as per UPD5 but wind-sea
186  ! components are also shifted in frequency space using Toba (1973)
187  ! Expected input: the Analysis Hs field plus background wind speed
188  ! and direction
189  !
190  ! PRCNTG is input for option UPD0F and is the correction factor
191  ! applied to all the gridpoints (e.g. 1.)
192  !
193  ! 0.475
194  !
195  ! PRCNTG_CAP is global input for option UPD2 and UPD3 and it is a cap on
196  ! the maximum SWH correction factor applied to all the gridpoints, as
197  ! both a multiple or divisor (e.g. cap at 5.0 means SWHANL/SWHBKG<=5.0
198  ! and SWHANL/SWHBKG>=0.2). The value given should not be less than 1.0
199  !
200  ! 5.0
201  !
202  ! Name of the file with the SWH analysis from the DA system $
203  ! suffix .grbtxt for text out of grib2 file. $
204  !
205  ! anl.grbtxt
206  !
207  ! -------------------------------------------------------------------- $
208  ! WAVEWATCH III EoF ww3_uprstr.inp
209  ! -------------------------------------------------------------------- $
210  !
211  ! ii. Data files anl.XXX
212  !
213  ! FOR UPD2,3 and UPD5,6 with WRST switch
214  ! USE THE grbtxt FORMAT, See Format E.
215  !
216  ! Format E.
217  ! Text file created by wgrib2. This format is tested more extensively
218  ! and currently the only format supported for anl.grbtxt.
219  !
220  ! NX NY
221  ! VAL0001
222  ! VAL0002
223  ! ...
224  ! VALNX*NY
225  !
226  ! IMPORTANT : All the regtests are with the format E. strongly recommended.
227  ! The order of the values in .grbtxt, is assumed the same by
228  ! default as the order of spectral data in the restart file.
229  !
230  ! NOTE: It is recommended to use UPD5,6 with the WRST switch enabled and
231  ! using SWH analysis data only as per Format E. However, the code includes
232  ! an option to run using a text file in which case:
233  ! USE THE grbtxtws format below
234  !
235  ! Text file with following lines:
236  ! NX NY
237  ! SWH0001 WSPD0001 WDIR0001
238  ! SWH0002 WSPD0002 WDIR0002
239  ! ...
240  ! SWHNX*NY WSPDNX*NY WDIRNX*NY
241  !
242  ! The order of the values in .grbtxt, is assumed the same by
243  ! default as the order of spectral data in the restart file.
244  ! Wind speeds and directions in the anl.XXX file are assumed to be
245  ! in CARTESIAN (GRID U,V) CONVENTION
246  !
247  ! NOTE About Format: if you prefer a different format; there are several
248  ! I/O subroutines ready, not included in the current version of the code,
249  ! contact the prgmr to get access to the source code.
250  !
251  ! iii. restart.ww3
252  ! The restart file as came out of the background run, the name has to be
253  ! restart.ww3, but the name of the output depends on the mod_def.ww3, the
254  ! ww3_uprstr follows its content (be careful with ovewriting).
255  !
256  ! 3. Example
257  ! Use the regression tests ww3_ta1
258  !
259  ! 4. Parameters :
260  !
261  ! Local parameters.
262  ! ----------------------------------------------------------------
263  !
264  ! ----------------------------------------------------------------
265  !
266  ! 5. Subroutines used :
267  !
268  ! Name Type Module Description
269  ! ----------------------------------------------------------------
270  ! W3NMOD Subr. W3GDATMD Set number of model.
271  ! W3SETG Subr. Id. Point to selected model.
272  ! W3NDAT Subr. W3WDATMD Set number of model for wave data.
273  ! W3SETW Subr. Id. Point to selected model for wave data.
274  ! W3NINP Subr. W3IDATMD Set number of grids/models.
275  ! W3SETI Subr. Id. Point to data structure.
276  ! W3DIMI Subr. Id. Set array sizes in data structure.
277  ! W2NAUX Subr. W3ADATMD Set number of model for aux data.
278  ! W3SETA Subr. Id. Point to selected model for aux data.
279  ! ITRACE Subr. W3SERVMD Subroutine tracing initialization.
280  ! NEXTLN Subr. Id. Get next line from input file.
281  ! EXTCDE Subr. Id. Abort program as graceful as possible.
282  ! W3IOGR Subr. W3IOGRMD Reading/writing model definition file.
283  ! WAVNU1 Subr. W3DISPMD
284 
285  ! ----------------------------------------------------------------
286  ! Internal Subroutines:
287  !
288  ! READ_GRBTXT
289  ! WORKER
290  ! SWH_RSRT_1p
291  ! WRITEMATRIX
292  !
293  ! 6. Called by :
294  !
295  ! None, stand-alone program.
296  !
297  ! 7. Error messages :
298  !
299  ! 8. Remarks:
300  !
301  ! 7.1 Use the grbtxt format for correction and analysis files.
302  !
303  ! 7.2 There are some variables not used but declared, it's for future
304  ! development.
305  !
306  ! 9. Structure :
307  !
308  ! ----------------------------------------------------
309  ! 1. Set up data structures.
310  ! 2. Read model defintion file with base model ( W3IOGR )
311  ! 3. Import restart file ( W3IORS )
312  ! 4. Import correction percentage ( )
313  ! OR Import the analysis field ( )
314  ! 5. Apply correction to the restart file ( )
315  ! 6. Export the updated restart file ( W3IORS )
316  ! ----------------------------------------------------
317  !
318  ! 10. Switches :
319  !
320  ! !/SHRD Switch for shared / distributed memory architecture.
321  ! !/T
322  ! !/S Enable subroutine tracing.
323  !
324  ! 11. Known Bugs
325  !
326  ! 1. Fix the format for the output (NSDO) of non strings, e.g. for
327  ! TIME.
328  !
329  ! 12. Source code :
330  !
331  !/
332  USE w3gdatmd, ONLY: w3nmod, w3setg
333  USE w3wdatmd, ONLY: w3ndat, w3setw
334  USE w3adatmd, ONLY: w3naux, w3seta
335  USE w3odatmd, ONLY: w3nout, w3seto
336  USE w3iorsmd, ONLY: w3iors
337  USE w3servmd, ONLY: itrace, nextln, extcde
338  USE w3iogrmd, ONLY: w3iogr
339  USE w3dispmd, ONLY: wavnu1
340  !
341  USE w3gdatmd, ONLY: gname, nx, ny, mapsta, sig, nk, nth, nsea, &
342  nseal, mapsf, dmin, zb, dsip, dth, rstype, &
343  gtype, smctype
344 #ifdef W3_SMC
345  USE w3gdatmd, ONLY: fswnd
346 #endif
347  USE w3wdatmd, ONLY: va, time
348  USE w3adatmd, ONLY: nsealm
349  USE w3odatmd, ONLY: iaproc, naperr, naplog, nds, napout
350  USE w3odatmd, ONLY: ndse, ndso, ndst, idout, fnmpre
351 #ifdef W3_WRST
352  USE w3idatmd
353 #endif
354  !
355  USE w3nmluprstrmd
356  !
357  IMPLICIT NONE
358  !/
359  !/ ------------------------------------------------------------------- /
360  ! Local variables
361  !/
362  INTEGER :: ndsi, ndsm, ndstrc, ntrace, ierr, i, j
363  CHARACTER :: comstr*1
364  !
365  TYPE(nml_restart_t) :: nml_restart
366  TYPE(nml_update_t) :: nml_update
367  !
368  ! REAL, ALLOCATABLE :: BETAW(:)
369  ! LOGICAL, ALLOCATABLE :: MASK(:)
370  LOGICAL :: anl_exists, corwsea, flgnml
371  INTEGER :: imod, ndsen, ix, iy, ik, ith, &
372  ixw, iyw
373  REAL, ALLOCATABLE :: updprcnt(:,:),vatmp(:), hsig(:,:), &
374  a(:), hs_anal(:,:), gues(:,:), &
375  hs_dif(:,:),swhanl(:,:), swhbckg(:,:), &
376  swhuprstr(:,:),vatmp_norm(:), &
377  wsbckg(:,:),wdrbckg(:,:)
378  INTEGER, ALLOCATABLE :: vamapws(:)
379  REAL :: prcntg, prcntg_cap, thrwsea
380  INTEGER :: rows, cols, isea
381  CHARACTER(128) :: flnmcor, flnmanl
382  CHARACTER(16) :: updproc
383  ! for howv
384  REAL :: swhtmp,swhbckg_1, swhanl_1, &
385  depth, wn, cg, etot, e1i, &
386  swhtmp1,sumvatmp, swhbckg_w, swhbckg_s
387  REAL :: k
388  CHARACTER(8), PARAMETER :: myname='W3UPRSTR'
389  LOGICAL :: smcgrd = .false.
390  LOGICAL :: smcwnd = .false.
391  LOGICAL :: wrston = .false.
392  !/
393  !/ ------------------------------------------------------------------- /
394  !/
395  ! 1. IO set-up.
396  CALL w3nmod ( 1, 6, 6 )
397  CALL w3setg ( 1, 6, 6 )
398  CALL w3ndat ( 6, 6 )
399  CALL w3setw ( 1, 6, 6 )
400  CALL w3naux ( 6, 6 )
401  CALL w3seta ( 1, 6, 6 )
402  CALL w3nout ( 6, 6 )
403  CALL w3seto ( 1, 6, 6 )
404 #ifdef W3_WRST
405  CALL w3ninp ( 6, 6 )
406  CALL w3seti ( 1, 6, 6 )
407 #endif
408  !
409  ndse = 6
410  ndsi = 10
411  ndsm = 20
412  !
413  iaproc = 1
414  napout = 1
415  naperr = 1
416  imod = 1
417  naplog = 1
418  !
419  ndstrc = 6
420  ntrace = 10
421  CALL itrace ( ndstrc, ntrace )
422  !
423  IF ( iaproc .EQ. naperr ) THEN
424  ndsen = ndse
425  ELSE
426  ndsen = -1
427  END IF
428  !
429  WRITE (ndso,900)
430  !
431 #ifdef W3_WRST
432  !Compiling with WRST will allow access to options UPD5/6
433  wrston = .true.
434  WRITE (ndso,*) '*** UPRSTR will read wind from restart files'
435 #endif
436  !/
437  !/ ------------------------------------------------------------------- /
438  ! 2. Read the ww3_uprstr input data
439  !
440  ! process ww3_uprstr namelist
441  !
442  INQUIRE(file=trim(fnmpre)//"ww3_uprstr.nml", exist=flgnml)
443  IF (flgnml) THEN
444  ! Read namelist
445  CALL w3nmluprstr (ndsi, trim(fnmpre)//'ww3_uprstr.nml', nml_restart, &
446  nml_update, ierr)
447  READ(nml_restart%RESTARTTIME, *) time
448  updproc = nml_update%UPDPROC
449  prcntg = nml_update%PRCNTG
450  prcntg_cap = nml_update%PRCNTGCAP
451  thrwsea = nml_update%THRWSEA
452  flnmanl = nml_update%FILE
453  END IF
454  !/
455  ! otherwise read from the .inp file
456  IF (.NOT. flgnml) THEN
457  j = len_trim(fnmpre)
458  OPEN (ndsi,file=fnmpre(:j)//'ww3_uprstr.inp',status='OLD', &
459  err=800,iostat=ierr)
460  READ (ndsi,'(A)',END=801,ERR=802) comstr
461  IF (comstr.EQ.' ') comstr = '$'
462  WRITE (ndso,901) comstr
463  !
464  CALL nextln ( comstr , ndsi , ndsen )
465  READ (ndsi,*,END=2001,ERR=2002) time
466  CALL nextln ( comstr , ndsi , ndsen )
467  READ (ndsi,*,END=2001,ERR=2002) updproc
468  CALL nextln ( comstr , ndsi , ndsen )
469  IF (updproc .EQ. 'UPD0F') THEN
470  READ (ndsi,*,END=2001,ERR=2002) prcntg
471  ELSE
472  IF ((updproc .EQ. 'UPD2') .OR. (updproc .EQ. 'UPD3')) THEN
473  ! CALL NEXTLN ( COMSTR , NDSI , NDSEN )
474  READ (ndsi,*,END=2001,ERR=2002) prcntg_cap
475 #ifdef W3_F
476  CALL nextln ( comstr , ndsi , ndsen )
477  READ (ndsi,*,END=2001,ERR=2002) flnmcor
478 #endif
479  ELSE
480  READ (ndsi,*,END=2001,ERR=2002) PRCNTG_CAP, thrwsea
481  END IF
482  CALL nextln ( comstr , ndsi , ndsen )
483  READ (ndsi,*,END=2001,ERR=2002) flnmanl
484  END IF
485  ENDIF
486 #ifdef W3_T
487  WRITE (ndso,*)' TIME: ',time
488 #endif
489  !/
490  !/ ------------------------------------------------------------------- /
491  ! 3. Read model definition file.
492  !/
493  CALL w3iogr ( 'READ', ndsm )
494  nseal = nsea
495  WRITE (ndso,920) gname
496  !/
497 #ifdef W3_SMC
498  !! SMC grid option is activated if GTYPE .EQ. SMCTYPE. JGLi06May2021
499  IF( gtype .EQ. smctype ) smcgrd = .true.
500  !! SMC sea-point wind option is activated if FSWND=.TRUE. JGLi06May2021
501  IF( fswnd ) smcwnd = .true.
502 #endif
503 #ifdef W3_WRST
504  ! Override SMCWND - at present restarts only store wind on
505  ! a regular grid
506  smcwnd = .false.
507 #endif
508 #ifdef W3_SMC
509  WRITE (ndso,*) '*** UPRSTR set to work with SMC grid model'
510 #endif
511  !/
512  !/ ------------------------------------------------------------------- /
513  ! 4. Read restart file
514  !/
515 #ifdef W3_WRST
516  ! Set the wind flag to true when reading restart wind
517  inflags1(3) = .true.
518  CALL w3dimi ( 1, 6, 6 ) !Needs to be called after w3iogr to have correct dimensions?
519 #endif
520  CALL w3iors ( 'READ', nds(6), sig(nk), imod )!
521  IF ( iaproc .EQ. naplog ) THEN
522  IF (rstype.EQ.0.OR.rstype.EQ.1.OR.rstype.EQ.4) THEN
523  WRITE (ndso,1004) 'Terminating ww3_uprstr: The restart ' // &
524  'file is not read'
525  CALL extcde ( 1 )
526  ELSE
527  WRITE (ndso,1004) ' Updating Restart File'
528  WRITE (ndso,*) ' TIME: ',time
529  END IF
530  END IF
531 #ifdef W3_T
532  WRITE (ndst,*), myname,' : Exporting VA as imported to VA01.txt'
533  CALL writematrix('VA01.txt', real(va))
534 #endif
535  !/
536  !/ ------------------------------------------------------------------- /
537  ! 5. Update restart spectra array according to the selected option
538  !/
539  SELECT CASE (updproc)
540  !/
541  !/ ------------------------------------------------------------------- /
542  ! UPD0F
543  !/
544  CASE ('UPD0F')
545  WRITE (ndso,902) 'UPD0F'
546  WRITE (ndso,1005) ' PRCNTG = ',prcntg
547 #ifdef W3_T
548  ALLOCATE( vatmp(SIZE(va ,1) ))
549  ALLOCATE( swhanl(SIZE(mapsta,1), SIZE(mapsta,2)))
550  ALLOCATE( swhbckg(SIZE(mapsta,1), SIZE(mapsta,2)))
551 #endif
552  DO isea=1, nsea, 1
553 #ifdef W3_T
554  ix = mapsf(isea,1)
555  iy = mapsf(isea,2)
556  vatmp = va(:,isea)
557  CALL swh_rsrt_1p (vatmp, isea, swhbckg_1)
558  swhbckg(iy,ix)=swhbckg_1
559 #endif
560  CALL update_va(prcntg, va(:,isea))
561 #ifdef W3_T
562  vatmp = va(:,isea)
563  CALL swh_rsrt_1p (vatmp, isea, swhanl_1)
564  swhanl(iy,ix)=swhanl_1
565  WRITE (ndso,*) ' =========== UPD0F Output ==========='
566  WRITE (ndso,*)'ISEA = ', isea,' PRCNTG = ',prcntg, &
567  ' SWHBCKG = ',swhbckg(iy,ix), &
568  ' SWHANL= ', swhanl(iy,ix)
569 #endif
570  END DO
571 #ifdef W3_T
572  CALL writematrix('SWHBCKG_UPD0F.txt', real(swhbckg))
573  CALL writematrix('SWHANL_UPD0F.txt' , real(swhanl ))
574  CALL writematrix('SWHRSTR_UPD0F.txt', real(swhanl ))
575 
576  DEALLOCATE ( vatmp, swhbckg, swhanl )
577 #endif
578  !/
579  !/ ------------------------------------------------------------------- /
580  ! UPD2
581  ! Apply a bulk correction to the wave spectrum at each grid cell based
582  ! on the ratio of HsBckg and HsAnl
583  !/
584  CASE ('UPD2')
585  WRITE (ndso,902) 'UPD2'
586  WRITE (ndso,1005) ' PRCNTG_CAP = ',prcntg_cap
587  WRITE (ndso,1006) ' Reading updated SWH from: ',trim(flnmanl)
588  !
589  ! Array allocation
590  ALLOCATE ( vatmp(SIZE(va,1)))
591  IF (.NOT. smcgrd) THEN
592  ALLOCATE( swhbckg(SIZE(mapsta,1), SIZE(mapsta,2)) )
593  ALLOCATE( swhanl(SIZE(mapsta,1), SIZE(mapsta,2)) )
594 #ifdef W3_SMC
595  ELSE
596  ALLOCATE( swhbckg(nsea,1) )
597  ALLOCATE( swhanl(nsea,1) )
598 #endif
599  ENDIF
600 #ifdef W3_T
601  IF (.NOT. smcgrd) THEN
602  ALLOCATE( swhuprstr(SIZE(mapsta,1), SIZE(mapsta,2)) )
603  ELSE
604  ALLOCATE( swhuprstr(nsea,1) )
605  ENDIF
606 #endif
607  !
608  ! Read additional Input: Analysis Field
609  INQUIRE(file=flnmanl, exist=anl_exists)
610  IF (anl_exists) THEN
611 #ifdef W3_T
612  WRITE (ndso,*) 'shape(SWHANL)', shape(swhanl)
613 #endif
614  CALL read_grbtxt(swhanl, flnmanl, smcgrd)
615 #ifdef W3_T
616  CALL writematrix('SWHANL_IN.txt',swhanl)
617 #endif
618  ELSE
619  WRITE (ndso,*) trim(flnmanl), ' does not exist, stopping...'
620  DEALLOCATE( swhanl,vatmp,swhbckg )
621 #ifdef W3_T
622  DEALLOCATE( swhuprstr )
623 #endif
624  stop
625  END IF
626  !
627  ! Calculation
628  DO isea=1, nsea, 1
629  IF (.NOT. smcgrd) THEN
630  ix = mapsf(isea,1)
631  iy = mapsf(isea,2)
632 #ifdef W3_SMC
633  ELSE
634  ix = 1
635  iy = isea
636 #endif
637  ENDIF
638  vatmp = va(:,isea)
639  CALL swh_rsrt_1p (vatmp, isea, swhbckg_1)
640  swhbckg(iy,ix)=swhbckg_1
641  !
642  IF ( swhbckg(iy,ix) > 0.01 .AND. swhanl(iy,ix) > 0.01 ) THEN
643  prcntg=(swhanl(iy,ix)/swhbckg_1)
644 #ifdef W3_T
645  WRITE (ndso,*) 'ISEA = ', isea,' IX = ',ix,' IY = ', iy, &
646  ' PRCNTG = ',prcntg,' SWHBCKG = ',swhbckg(iy,ix), &
647  ' SWHANL = ', swhanl(iy,ix)
648 #endif
649  CALL check_prcntg (prcntg,prcntg_cap)
650  CALL update_va(prcntg, va(:,isea))
651 #ifdef W3_T
652  CALL swh_rsrt_1p (va(:,isea), isea, swhuprstr(iy,ix))
653  WRITE (ndso,*) ' =========== UPD2 Output ==========='
654  WRITE (ndso,*)'ISEA = ',isea, &
655  'SWH_BCKG = ', swhbckg(iy,ix), &
656  'SWH_ANL = ', swhanl(iy,ix), &
657  'PRCNTG = ', prcntg, &
658  'SWH_RSTR = ',swhuprstr(iy,ix)
659 #endif
660  END IF
661  END DO
662 #ifdef W3_T
663  CALL writematrix('SWHBCKG_UPD2.txt', real(swhbckg ))
664  CALL writematrix('SWHANL_UPD2.txt' , real(swhanl ))
665  CALL writematrix('SWHRSTR_UPD2.txt', real(swhuprstr))
666 #endif
667  !
668  DEALLOCATE( swhanl,vatmp,swhbckg )
669 #ifdef W3_T
670  DEALLOCATE( swhuprstr )
671 #endif
672  !/
673  !/ ------------------------------------------------------------------- /
674  ! UPD3
675  ! As per UPD2, but the update factor is a surface with the shape of the
676  ! background spectrum
677  !/
678  CASE ('UPD3')
679  WRITE (ndso,902) 'UPD3'
680  WRITE (ndso,1005) ' PRCNTG_CAP = ',prcntg_cap
681  WRITE (ndso,1006) ' Reading updated SWH from: ',trim(flnmanl)
682  !
683  ! Array allocation
684  ALLOCATE ( vatmp(SIZE(va,1)))
685  ALLOCATE ( vatmp_norm(SIZE(va,1)))
686  ALLOCATE ( a(SIZE(va,1)))
687  IF (.NOT. smcgrd) THEN
688  ALLOCATE( swhbckg(SIZE(mapsta,1), SIZE(mapsta,2)) )
689  ALLOCATE( swhanl(SIZE(mapsta,1), SIZE(mapsta,2)) )
690 #ifdef W3_SMC
691  ELSE
692  ALLOCATE( swhbckg(nsea,1) )
693  ALLOCATE( swhanl(nsea,1) )
694 #endif
695  ENDIF
696 #ifdef W3_T
697  IF (.NOT. smcgrd) THEN
698  ALLOCATE( swhuprstr(SIZE(mapsta,1), SIZE(mapsta,2)) )
699  ELSE
700  ALLOCATE( swhuprstr(nsea,1) )
701  ENDIF
702 #endif
703  !
704  ! Read additional Input: Analysis Field
705  INQUIRE(file=flnmanl, exist=anl_exists)
706  IF (anl_exists) THEN
707 #ifdef W3_T
708  WRITE (ndso,*) 'shape(SWHANL)', shape(swhanl)
709 #endif
710  CALL read_grbtxt(swhanl, flnmanl, smcgrd)
711 #ifdef W3_T
712  CALL writematrix('SWHANL_IN.txt',swhanl)
713 #endif
714  ELSE
715  WRITE (ndso,*) trim(flnmanl), ' does not exist, stopping...'
716  DEALLOCATE( swhanl,vatmp,swhbckg,vatmp_norm,a )
717 #ifdef W3_T
718  DEALLOCATE( swhuprstr )
719 #endif
720  stop
721  END IF
722  !
723  ! Calculation
724  DO isea=1, nsea, 1
725  IF (.NOT. smcgrd) THEN
726  ix = mapsf(isea,1)
727  iy = mapsf(isea,2)
728 #ifdef W3_SMC
729  ELSE
730  ix = 1
731  iy = isea
732 #endif
733  ENDIF
734  vatmp = va(:,isea)
735  CALL swh_rsrt_1p (vatmp, isea, swhbckg_1)
736  swhbckg(iy,ix)=swhbckg_1
737  !
738  IF ( swhbckg(iy,ix) > 0.01 .AND. swhanl(iy,ix) > 0.01 ) THEN
739  !Step 1.
740  prcntg=(swhanl(iy,ix)/swhbckg_1)
741 #ifdef W3_T
742  WRITE (ndso,*) ' =========== Step 1. ==========='
743  WRITE (ndso,*) ' ISEA = ', isea,' IX = ',ix,' IY = ', iy, &
744  ' PRCNTG = ',prcntg,' SWHBCKG = ',swhbckg(iy,ix), &
745  ' SWHANL = ', swhanl(iy,ix)
746 #endif
747  CALL check_prcntg(prcntg,prcntg_cap)
748  vatmp_norm=vatmp/sum(vatmp)
749 #ifdef W3_T
750  WRITE (ndso,*)' ISEA =', isea,' IX = ',ix,' IY = ', iy, &
751  ' PRCNTG = ',prcntg, &
752  ' SWHBCKG = ',swhbckg(iy,ix), ' SWHANL = ', swhanl(iy,ix)
753 #endif
754  IF (prcntg > 1.) THEN
755  a=prcntg**2*(1 + vatmp_norm)
756  ELSE
757  a=prcntg**2*(1 - vatmp_norm)
758  END IF
759  vatmp=a*vatmp
760  CALL swh_rsrt_1p (vatmp, isea, swhtmp)
761  prcntg=(swhanl(iy,ix)/swhtmp)
762 #ifdef W3_T
763  swhuprstr(iy,ix)=swhtmp
764  WRITE (ndso,*) ' =========== Step 2. ==========='
765  WRITE (ndso,*)'ISEA = ', isea, ' PRCNTG = ',prcntg, &
766  ' SWHANL= ', swhanl(iy,ix), &
767  ' SWHUPRSTR(IY,IX) = ', swhuprstr(iy,ix)
768 #endif
769  CALL check_prcntg (prcntg,prcntg_cap)
770  CALL update_va(prcntg, vatmp)
771  va(:,isea)=vatmp
772 #ifdef W3_T
773  CALL swh_rsrt_1p (vatmp, isea, swhtmp)
774  swhuprstr(iy,ix)=swhtmp
775  WRITE (ndso,*) ' =========== UPD3 Output ==========='
776  WRITE (ndso,*)'ISEA = ',isea,'SWH_BCKG = ', swhbckg(iy,ix), &
777  'SWH_ANL = ', swhanl(iy,ix), &
778  'SWH_RSTR = ',swhuprstr(iy,ix)
779 #endif
780  END IF
781  END DO
782 #ifdef W3_T
783  CALL writematrix('SWHBCKG_UPD3.txt', real(swhbckg))
784  CALL writematrix('SWHANL_UPD3.txt' , real(swhanl ))
785  CALL writematrix('SWHRSTR_UPD3.txt', real(swhuprstr))
786 #endif
787  !
788  DEALLOCATE( swhanl,vatmp,swhbckg,vatmp_norm,a )
789 #ifdef W3_T
790  DEALLOCATE( swhuprstr )
791 #endif
792  !/
793  !/ ------------------------------------------------------------------- /
794  ! UPD5
795  ! Corrects wind-sea only in wind dominated conditions - bulk correction
796  ! The fac(x,y,frq,theta), is calculated at each grid point according to
797  ! HsBckg and HsAnl
798  !/
799  CASE ('UPD5')
800  WRITE (ndso,902) 'UPD5'
801  WRITE (ndso,1005) ' PRCNTG_CAP = ',prcntg_cap
802  WRITE (ndso,1005) ' THRWSEA = ',thrwsea
803  WRITE (ndso,1006) ' Reading updated SWH from: ',trim(flnmanl)
804  ! Presently set hardwired THRWSEA energy threshold here
805  ! not user defined in input file
806  ! THRWSEA = 0.7
807  !
808  ! Array allocation
809  ALLOCATE ( vatmp(SIZE(va,1)))
810  ALLOCATE ( vamapws(SIZE(va,1)))
811  IF (.NOT. smcgrd) THEN
812  ! SWH arrays allocated using Y,X convention as per wgrib write
813  ALLOCATE( swhbckg(SIZE(mapsta,1), SIZE(mapsta,2)) )
814  ALLOCATE( swhanl(SIZE(mapsta,1), SIZE(mapsta,2)) )
815  ! Wind arrays allocated using X,Y convention as in w3idatmd
816  ALLOCATE( wsbckg(SIZE(mapsta,2), SIZE(mapsta,1)) )
817  ALLOCATE( wdrbckg(SIZE(mapsta,2), SIZE(mapsta,1)) )
818 #ifdef W3_SMC
819  ELSE
820  ALLOCATE( swhbckg(nsea,1) )
821  ALLOCATE( swhanl(nsea,1) )
822  ! Use SMCWND to determine if reading a seapoint aray for wind
823  IF( smcwnd ) THEN
824  ALLOCATE( wsbckg(nsea,1) )
825  ALLOCATE( wdrbckg(nsea,1) )
826  ELSE
827  ALLOCATE(wsbckg(SIZE(mapsta,2), SIZE(mapsta,1)))
828  ALLOCATE(wdrbckg(SIZE(mapsta,2), SIZE(mapsta,1)))
829  ENDIF
830 #endif
831  ENDIF
832 #ifdef W3_T
833  IF (.NOT. smcgrd) THEN
834  ALLOCATE( swhuprstr(SIZE(mapsta,1), SIZE(mapsta,2)) )
835  ELSE
836  ALLOCATE( swhuprstr(nsea,1) )
837  ENDIF
838 #endif
839  !
840  ! Read additional Input: Analysis Field
841  INQUIRE(file=flnmanl, exist=anl_exists)
842  IF (anl_exists) THEN
843 #ifdef W3_T
844  WRITE (ndso,*) 'shape(SWHANL)', shape(swhanl)
845 #endif
846 #ifdef W3_WRST
847  ! For WRST switch read only corrected SWH
848  ! Wind will have been read from the restart
849  IF (wrston) THEN
850  CALL read_grbtxt(swhanl, flnmanl, smcgrd)
851  ELSE
852 #endif
853  CALL read_grbtxtws(swhanl,wsbckg,wdrbckg,flnmanl,smcgrd)
854 #ifdef W3_WRST
855  ENDIF
856 #endif
857 #ifdef W3_T
858  CALL writematrix('SWHANL_IN.txt',swhanl)
859 #endif
860  ELSE
861  WRITE (ndso,*) trim(flnmanl), ' does not exist, stopping...'
862  DEALLOCATE( swhanl,vatmp,swhbckg,vamapws,wsbckg,wdrbckg )
863 #ifdef W3_T
864  DEALLOCATE( swhuprstr )
865 #endif
866  stop
867  END IF
868  !
869 #ifdef W3_WRST
870  !Calculate wind speed and direction values from u,v..
871  !..using cartesian direction convention
872  !At present assume only needed for data read from restart
873  CALL uvtocart(wxnwrst,wynwrst,wsbckg,wdrbckg,smcwnd)
874 #endif
875  !
876  ! Calculation
877  DO isea=1, nsea, 1
878  IF (.NOT. smcgrd) THEN
879  ix = mapsf(isea,1)
880  iy = mapsf(isea,2)
881  ixw = ix
882  iyw = iy
883 #ifdef W3_SMC
884  ELSE
885  ix = 1
886  iy = isea
887  IF( smcwnd ) THEN
888  ! Wind arrays allocated using (X,Y) convention for regular grids
889  ! but overriding here for the SMC grid which are always defined
890  ! as (NSEA,1) by switching the IY and IX dimension values around
891  ixw = iy
892  iyw = ix
893  ELSE
894  ixw = mapsf(isea,1)
895  iyw= mapsf(isea,2)
896  ENDIF
897 #endif
898  ENDIF
899  vatmp = va(:,isea)
900  CALL swh_rsrt_1pw (vatmp, wsbckg(ixw,iyw), wdrbckg(ixw,iyw), isea, &
901  swhbckg_1, swhbckg_w, swhbckg_s, vamapws)
902  swhbckg(iy,ix)=swhbckg_1
903  !
904  IF ( swhbckg(iy,ix) > 0.01 .AND. swhanl(iy,ix) > 0.01 ) THEN
905  ! If wind-sea is dominant energy component apply correction to
906  ! wind-sea part only
907  IF ( (swhbckg_w / swhbckg_1)**2.0 > thrwsea ) THEN
908  ! Apply spectrum updates to wind-sea bins only
909  prcntg=sqrt((swhanl(iy,ix)**2.0-swhbckg_s**2.0)/swhbckg_w**2.0)
910  CALL check_prcntg(prcntg,prcntg_cap)
911  CALL updtwspec(vatmp, prcntg, vamapws)
912  ! else correct the whole spectrum as for UPD2
913  ELSE
914  prcntg=(swhanl(iy,ix)/swhbckg_1)
915  CALL check_prcntg(prcntg,prcntg_cap)
916  CALL update_va(prcntg,vatmp)
917  END IF
918 #ifdef W3_T
919  WRITE (ndso,*) 'ISEA = ', isea,' IX = ',ix,' IY = ', iy, &
920  ' PRCNTG = ',prcntg,' SWHBCKG = ',swhbckg(iy,ix), &
921  ' SWHANL = ', swhanl(iy,ix)
922 #endif
923  va(:,isea)=vatmp
924 #ifdef W3_T
925  CALL swh_rsrt_1p (vatmp, isea, swhtmp)
926  swhuprstr(iy,ix)=swhtmp
927  WRITE (ndso,*) ' =========== UPD5 Output ==========='
928  WRITE (ndso,*)'ISEA = ',isea,'SWH_BCKG = ', swhbckg(iy,ix), &
929  'SWH_ANL = ', swhanl(iy,ix), &
930  'SWH_RSTR = ',swhuprstr(iy,ix)
931 #endif
932  END IF
933  END DO
934 #ifdef W3_T
935  CALL writematrix('SWHBCKG_UPD5.txt', real(swhbckg ))
936  CALL writematrix('SWHANL_UPD5.txt' , real(swhanl ))
937  CALL writematrix('SWHRSTR_UPD5.txt', real(swhuprstr))
938 #endif
939  !
940  DEALLOCATE( swhanl,vatmp,swhbckg,vamapws,wsbckg,wdrbckg )
941 #ifdef W3_T
942  DEALLOCATE( swhuprstr )
943 #endif
944  !/
945  !/ ------------------------------------------------------------------- /
946  ! UPD6
947  ! Hybrid of Lionello et al. and Kohno methods
948  ! Corrects wind-sea only in wind dominated conditions - including fp shift
949  ! The fac(x,y,frq,theta), is calculated at each grid point according to
950  ! HsBckg and HsAnl
951  !/
952  CASE ('UPD6')
953  WRITE (ndso,902) 'UPD6'
954  WRITE (ndso,1005) ' PRCNTG_CAP = ',prcntg_cap
955  WRITE (ndso,1005) ' THRWSEA = ',thrwsea
956  WRITE (ndso,1006) ' Reading updated SWH from: ',trim(flnmanl)
957  ! Presently set hardwired CORWSEA logical and THRWSEA energy
958  ! thresholds here, not user defined in input file
959  corwsea = .false.
960  !THRWSEA = 0.7
961  !
962  ! Array allocation
963  ALLOCATE ( vatmp(SIZE(va,1)))
964  ALLOCATE ( vamapws(SIZE(va,1)))
965  IF (.NOT. smcgrd) THEN
966  ! SWH arrays allocated using Y,X convention as per wgrib write
967  ALLOCATE( swhbckg(SIZE(mapsta,1), SIZE(mapsta,2)) )
968  ALLOCATE( swhanl(SIZE(mapsta,1), SIZE(mapsta,2)) )
969  ! Wind arrays allocated using X,Y convention as in w3idatmd
970  ALLOCATE( wsbckg(SIZE(mapsta,2), SIZE(mapsta,1)) )
971  ALLOCATE( wdrbckg(SIZE(mapsta,2), SIZE(mapsta,1)) )
972 #ifdef W3_SMC
973  ELSE
974  ALLOCATE( swhbckg(nsea,1) )
975  ALLOCATE( swhanl(nsea,1) )
976  ! Use SMCWND to determine if reading a seapoint aray for wind
977  IF( smcwnd ) THEN
978  ALLOCATE( wsbckg(nsea,1) )
979  ALLOCATE( wdrbckg(nsea,1) )
980  ELSE
981  ALLOCATE(wsbckg(SIZE(mapsta,2), SIZE(mapsta,1)))
982  ALLOCATE(wdrbckg(SIZE(mapsta,2), SIZE(mapsta,1)))
983  ENDIF
984 #endif
985  ENDIF
986 #ifdef W3_T
987  IF (.NOT. smcgrd) THEN
988  ALLOCATE( swhuprstr(SIZE(mapsta,1), SIZE(mapsta,2)) )
989  ELSE
990  ALLOCATE( swhuprstr(nsea,1) )
991  ENDIF
992 #endif
993  !
994  ! Read additional Input: Analysis Field
995  INQUIRE(file=flnmanl, exist=anl_exists)
996  IF (anl_exists) THEN
997 #ifdef W3_T
998  WRITE (ndso,*) 'shape(SWHANL)', shape(swhanl)
999 #endif
1000 #ifdef W3_WRST
1001  ! For WRST switch read only corrected SWH
1002  ! Wind will have been read from the restart
1003  IF (wrston) THEN
1004  CALL read_grbtxt(swhanl, flnmanl, smcgrd)
1005  ELSE
1006 #endif
1007  CALL read_grbtxtws(swhanl,wsbckg,wdrbckg,flnmanl,smcgrd)
1008 #ifdef W3_WRST
1009  ENDIF
1010 #endif
1011 #ifdef W3_T
1012  CALL writematrix('SWHANL_IN.txt',swhanl)
1013 #endif
1014  ELSE
1015  WRITE (ndso,*) trim(flnmanl), ' does not exist, stopping...'
1016  DEALLOCATE( swhanl,vatmp,swhbckg,vamapws,wsbckg,wdrbckg )
1017 #ifdef W3_T
1018  DEALLOCATE( swhuprstr )
1019 #endif
1020  stop
1021  END IF
1022  !
1023 #ifdef W3_WRST
1024  !Calculate wind speed and direction values from u,v..
1025  !..using cartesian direction convention
1026  !At present assume only needed for data read from restart
1027  CALL uvtocart(wxnwrst,wynwrst,wsbckg,wdrbckg,smcwnd)
1028 #endif
1029  !
1030  ! Calculation
1031  DO isea=1, nsea, 1
1032  IF (.NOT. smcgrd) THEN
1033  ix = mapsf(isea,1)
1034  iy = mapsf(isea,2)
1035  ixw = ix
1036  iyw = iy
1037 #ifdef W3_SMC
1038  ELSE
1039  ix = 1
1040  iy = isea
1041  IF( smcwnd ) THEN
1042  ! Wind arrays allocated using (X,Y) convention for regular grids
1043  ! but overriding here for the SMC grid which are always defined
1044  ! as (NSEA,1) by switching the IY and IX dimension values around
1045  ixw = iy
1046  iyw = ix
1047  ELSE
1048  ixw = mapsf(isea,1)
1049  iyw = mapsf(isea,2)
1050  ENDIF
1051 #endif
1052  ENDIF
1053  vatmp = va(:,isea)
1054  CALL swh_rsrt_1pw (vatmp, wsbckg(ixw,iyw), wdrbckg(ixw,iyw), isea, &
1055  swhbckg_1, swhbckg_w, swhbckg_s, vamapws)
1056  swhbckg(iy,ix)=swhbckg_1
1057  !/
1058  IF ( swhbckg(iy,ix) > 0.01 .AND. swhanl(iy,ix) > 0.01 ) THEN
1059  ! If wind-sea is dominant energy component apply correction to
1060  ! wind-sea part only
1061  IF ( (swhbckg_w / swhbckg_1)**2.0 > thrwsea ) THEN
1062  ! Apply spectrum updates to wind-sea bins only
1063  prcntg=sqrt((swhanl(iy,ix)**2.0-swhbckg_s**2.0)/swhbckg_w**2.0)
1064  CALL check_prcntg(prcntg,prcntg_cap)
1065  CALL updtwspecf(vatmp, prcntg, vamapws, isea, .false.)
1066  ! else correct the whole spectrum
1067  ELSE
1068  prcntg=(swhanl(iy,ix)/swhbckg_1)
1069  CALL check_prcntg(prcntg,prcntg_cap)
1070  IF (corwsea) THEN
1071  ! Include frequency shifts in wind-sea update
1072  CALL updtwspecf(vatmp, prcntg, vamapws, isea, .true.)
1073  ELSE
1074  ! bulk correction only, as per UPD2
1075  CALL update_va(prcntg,vatmp)
1076  END IF
1077  END IF
1078 #ifdef W3_T
1079  WRITE (ndso,*) 'ISEA = ', isea,' IX = ',ix,' IY = ', iy, &
1080  ' PRCNTG = ',prcntg,' SWHBCKG = ',swhbckg(iy,ix), &
1081  ' SWHANL = ', swhanl(iy,ix)
1082 #endif
1083  va(:,isea)=vatmp
1084 #ifdef W3_T
1085  CALL swh_rsrt_1p (vatmp, isea, swhtmp)
1086  swhuprstr(iy,ix)=swhtmp
1087  WRITE (ndso,*) ' =========== UPD6 Output ==========='
1088  WRITE (ndso,*)'ISEA = ',isea,'SWH_BCKG = ', swhbckg(iy,ix), &
1089  'SWH_ANL = ', swhanl(iy,ix), &
1090  'SWH_RSTR = ',swhuprstr(iy,ix)
1091 #endif
1092  END IF
1093  END DO
1094 #ifdef W3_T
1095  CALL writematrix('SWHBCKG_UPD6.txt', real(swhbckg ))
1096  CALL writematrix('SWHANL_UPD6.txt' , real(swhanl ))
1097  CALL writematrix('SWHRSTR_UPD6.txt', real(swhuprstr))
1098 #endif
1099  !
1100  DEALLOCATE( swhanl,vatmp,swhbckg,vamapws,wsbckg,wdrbckg )
1101 #ifdef W3_T
1102  DEALLOCATE( swhuprstr )
1103 #endif
1104  !/
1105  !/ ------------------------------------------------------------------- /
1106  ! End of update options
1107  !/
1108  END SELECT
1109  !/
1110  !/ ------------------------------------------------------------------- /
1111  ! 6. Write updated restart file
1112  !/
1113 #ifdef W3_WRST
1114  ! Copy read wind values from restart for write out
1115  wxn = wxnwrst
1116  wyn = wynwrst
1117 #endif
1118  WRITE (ndso,903)
1119  rstype = 3
1120  CALL w3iors ( 'HOT', nds(6), sig(nk), 1 )
1121 #ifdef W3_T
1122  WRITE (ndst,*), myname,' : Exporting VA at the end of the re-analysis'
1123  CALL writematrix('VA02.txt', real(va))
1124 #endif
1125  !
1126  !/
1127  !/ ------------------------------------------------------------------- /
1128  ! Escape locations read errors 08k:
1129  !/
1130  GOTO 888
1131  !
1132 800 CONTINUE
1133  WRITE (ndse,1000) ierr
1134  CALL extcde ( 10 )
1135  !
1136 801 CONTINUE
1137  WRITE (ndse,1001)
1138  CALL extcde ( 11 )
1139  !
1140 802 CONTINUE
1141  WRITE (ndse,1002) ierr
1142  CALL extcde ( 12 )
1143  !
1144 888 CONTINUE
1145  WRITE (ndso,999)
1146  !/
1147  !/ ------------------------------------------------------------------- /
1148  ! Escape locations read errors 2k:
1149  !/
1150  GOTO 2222
1151  !
1152 2001 CONTINUE
1153  IF ( iaproc .EQ. naperr ) WRITE (ndse,1001)
1154  GOTO 2222
1155  !
1156 2002 CONTINUE
1157  IF ( iaproc .EQ. naperr ) WRITE (ndse,1002) ierr
1158  GOTO 2222
1159  !
1160 2222 CONTINUE
1161  !/
1162  !/ ------------------------------------------------------------------- /
1163  ! Formats
1164  !/
1165 900 FORMAT (/15x,' *** WAVEWATCH III ww3_uprstr Initializing *** '/ &
1166  15x,' ==============================================='/)
1167 901 FORMAT ( ' Comment character is ''',a,''''/)
1168  !
1169 902 FORMAT ( ' The Option ''',a,''' is used.'/)
1170  !
1171 903 FORMAT ( ' Exporting the Updated Restart file to "restart001.ww3"'/)
1172  !
1173 920 FORMAT ( ' Grid name : ',a/)
1174  !
1175 930 FORMAT (/' Time interval : '/ &
1176  ' --------------------------------------------------')
1177  !
1178 931 FORMAT ( ' Starting time : ',a)
1179  !
1180 932 FORMAT ( ' Ending time : ',a/)
1181  !
1182 999 FORMAT (/' End of program '/ &
1183  ' ========================================='/ &
1184  ' WAVEWATCH III ww3_uprstr '/)
1185  !
1186 1000 FORMAT (/' *** WAVEWATCH III ERROR IN W3UPRSTR : '/ &
1187  ' ERROR IN OPENING INPUT FILE'/ &
1188  ' IOSTAT =',i5/)
1189  !
1190 1001 FORMAT (/' *** WAVEWATCH III ERROR IN W3UPRSTR : '/ &
1191  ' PREMATURE END OF INPUT FILE'/)
1192 
1193 1002 FORMAT (/' *** WAVEWATCH III ERROR IN W3UPRSTR : '/ &
1194  ' ERROR IN READING FROM INPUT FILE'/ &
1195  ' IOSTAT =',i5/)
1196 1004 FORMAT (/' '/,a/)
1197 1005 FORMAT (' ',a, f6.3/)
1198 1006 FORMAT (' ',a, a/)
1199  !
1200  !/
1201 CONTAINS
1202  !/
1203  !/ ------------------------------------------------------------------- /
1213  SUBROUTINE update_va (PRCNTG, VATMP)
1214  !/
1215  !/ +-----------------------------------+
1216  !/ | WAVEWATCH III NOAA/NCEP |
1217  !/ | Stelios Flampouris |
1218  !/ | FORTRAN 90 |
1219  !/ | Created : 16-Oct-2018 |
1220  !/ +-----------------------------------+
1221  !/
1222  !/ 16-Oct-2018 : Original Code ( version 6.06 )
1223  !/
1224  !/ Copyright 2010 National Weather Service (NWS),
1225  !/ National Oceanic and Atmospheric Administration. All rights
1226  !/ reserved. WAVEWATCH III is a trademark of the NWS.
1227  !/ No unauthorized use without permission.
1228  !/
1229  ! 1. Purpose :
1230  ! Apply correction to the spectrum
1231  !
1232  ! 2. Method :
1233  ! The factor is (swh_anal/swh_bkg)**2 as applying to wave energy
1234  ! 3. Parameters :
1235  !
1236  ! Local parameters.
1237  ! ----------------------------------------------------------------
1238  !
1239  ! 4. Subroutines used :
1240  !
1241  ! ----------------------------------------------------------------
1242  ! Internal Subroutines:
1243  !
1244  ! 5. Called by :
1245  !
1246  ! 6. Error messages :
1247  !
1248  ! 7. Remarks :
1249  !
1250  ! 8. Structure :
1251  !
1252  ! 9. Switches :
1253  !
1254  ! !/T
1255  !
1256  ! 10. Source code :
1257  !
1258  !/
1259  REAL, INTENT(IN) :: PRCNTG
1260  REAL, DIMENSION(:), INTENT(INOUT) :: VATMP
1261  !
1262  vatmp = (prcntg**2)*vatmp
1263  !
1264  END SUBROUTINE update_va
1265  !/
1266  !/ ---------------------------------------------------------------------
1267  !/
1277  SUBROUTINE check_prcntg (PRCNTG,PRCNTG_CAP)
1278  !/
1279  !/ +-----------------------------------+
1280  !/ | WAVEWATCH III NOAA/NCEP |
1281  !/ | Stelios Flampouris |
1282  !/ | FORTRAN 90 |
1283  !/ | Created : 16-Oct-2018 |
1284  !/ +-----------------------------------+
1285  !/
1286  !/ 16-Oct-2018 : Original Code ( version 6.06 )
1287  !/ 24-Oct-2018 : Update by Andy Saulter ( version 7.14 )
1288  !/
1289  !/ Copyright 2010 National Weather Service (NWS),
1290  !/ National Oceanic and Atmospheric Administration. All rights
1291  !/ reserved. WAVEWATCH III is a trademark of the NWS.
1292  !/ No unauthorized use without permission.
1293  !/
1294  ! 1. Purpose :
1295  ! Last sanity check before the update of the spectrum
1296  ! 2. Method :
1297  ! The percentage of change is compared against a user defined cap.
1298  ! 3. Parameters :
1299  !
1300  ! Local parameters.
1301  ! ----------------------------------------------------------------
1302  !
1303  ! 4. Subroutines used :
1304  !
1305  ! ----------------------------------------------------------------
1306  ! Internal Subroutines:
1307  !
1308  ! 5. Called by :
1309  !
1310  ! 6. Error messages :
1311  !
1312  ! 7. Remarks :
1313  !
1314  ! 8. Structure :
1315  !
1316  ! 9. Switches :
1317  !
1318  ! !/T
1319  !
1320  ! 10. Source code :
1321  !
1322  !/
1323  REAL, INTENT(INOUT) :: PRCNTG
1324  REAL, INTENT(IN ) :: PRCNTG_CAP
1325  ! local
1326  CHARACTER(12), PARAMETER :: MYNAME='CHECK_PRCNTG'
1327 #ifdef W3_T
1328 
1329  WRITE (ndso,*) trim(myname)," The original correction is ",prcntg
1330  WRITE (ndso,*) trim(myname)," The cap is ",prcntg_cap
1331 #endif
1332  IF ( prcntg_cap < 1. ) THEN
1333  WRITE (ndso,*) trim(myname)," WARNING: PRCNTG_CAP set < 1."
1334  WRITE (ndso,*) trim(myname)," This may introduce spurious corrections"
1335  END IF
1336 #ifdef W3_T
1337  WRITE (ndso,*) trim(myname)," The cap is ",prcntg_cap
1338 #endif
1339  IF ( prcntg > 1. ) THEN
1340 #ifdef W3_T
1341  WRITE (ndso,*) trim(myname)," PRCNTG > 1."
1342 #endif
1343  prcntg = min(prcntg, 1. * prcntg_cap)
1344  ELSE IF ( prcntg < 1. ) THEN
1345 #ifdef W3_T
1346  WRITE (ndso,*) trim(myname)," PRCNTG < 1."
1347 #endif
1348  prcntg = max(prcntg, 1. / prcntg_cap)
1349 #ifdef W3_T
1350 
1351 #endif
1352  END IF
1353 #ifdef W3_T
1354  WRITE (ndso,*) trim(myname)," The updated correction is ",prcntg
1355 #endif
1356  !
1357  END SUBROUTINE check_prcntg
1358  !/
1359  !/ ------------------------------------------------------------------- /
1360  !/
1368  SUBROUTINE read_grbtxt(UPDPRCNT,FLNMCOR,SMCGRD)
1369  !/
1370  !/ +-----------------------------------+
1371  !/ | WAVEWATCH III NOAA/NCEP |
1372  !/ | Stelios Flampouris |
1373  !/ | FORTRAN 90 |
1374  !/ | Created : 15-Mar-2017 |
1375  !/ | Last Update : 16-Oct-2018 |
1376  !/ +-----------------------------------+
1377  !/
1378  !/ 15-Mar-2017 : Original Code ( version 6.04 )
1379  !/ 16-Oct-2018 : Generalization of the reader ( version 6.06 )
1380  !/
1381  !/ Copyright 2010 National Weather Service (NWS),
1382  !/ National Oceanic and Atmospheric Administration. All rights
1383  !/ reserved. WAVEWATCH III is a trademark of the NWS.
1384  !/ No unauthorized use without permission.
1385  !/
1386  ! 1. Purpose :
1387  ! Read gribtxt files
1388  ! 2. Method :
1389  !
1390  ! 3. Parameters :
1391  !
1392  ! Local parameters.
1393  ! ----------------------------------------------------------------
1394  !
1395  ! 4. Subroutines used :
1396  !
1397  ! ----------------------------------------------------------------
1398  ! Internal Subroutines:
1399  !
1400  ! 5. Called by :
1401  !
1402  ! 6. Error messages :
1403  !
1404  ! 7. Remarks :
1405  !
1406  ! 8. Structure :
1407  !
1408  ! 9. Switches :
1409  !
1410  ! !/T
1411  !
1412  ! 10. Source code :
1413  !
1414  !/
1415  REAL, DIMENSION(:,:), INTENT(OUT) :: UPDPRCNT
1416  CHARACTER(*), INTENT(IN) :: FLNMCOR
1417  LOGICAL, INTENT(IN) :: SMCGRD
1418  ! Local Variables
1419  INTEGER :: I, J, IERR
1420  INTEGER :: K, L, M, N
1421  REAL :: A
1422  INTEGER, PARAMETER :: IP_FID = 123
1423  CHARACTER(25), PARAMETER::myname='read_grbtxt'
1424  !
1425 #ifdef W3_T
1426  WRITE (ndso,*) trim(myname), ' starts'
1427 #endif
1428  j = len_trim(fnmpre)
1429  OPEN (ip_fid,file=fnmpre(:j)//trim(flnmcor),status='OLD' &
1430  ,action='read',iostat=ierr)
1431  !
1432  ! Read text header and check dimensions match expected values
1433  IF (.NOT. smcgrd) THEN
1434  READ( ip_fid, *) m,n
1435  IF (( SIZE(updprcnt,1) /= n) .OR. ( SIZE(updprcnt,2) /= m )) THEN
1436  WRITE (ndso,*) trim(myname),': These are not the grid ' // &
1437  'dimensions: M=',m,' N=',n
1438  stop
1439  END IF
1440 #ifdef W3_SMC
1441  ELSE
1442  READ( ip_fid, *) n
1443  IF ( SIZE(updprcnt,1) /= n ) THEN
1444  WRITE (ndso,*) trim(myname),': These are not the grid ' // &
1445  'dimensions: N=',n
1446  stop
1447  END IF
1448 #endif
1449  END IF
1450  updprcnt=0
1451  !
1452  ! Read the data into its allocated array
1453  IF (.NOT. smcgrd) THEN
1454  DO l=1,n
1455  DO k=1,m
1456  a=0.
1457  READ(ip_fid,*)a
1458  updprcnt(n+1-l,k)=a
1459  END DO
1460  END DO
1461 #ifdef W3_SMC
1462  ELSE
1463  DO l=1,n
1464  a=0.
1465  READ(ip_fid,*)a
1466  updprcnt(l,1)=a
1467  END DO
1468 #endif
1469  END IF
1470  !
1471  CLOSE(ip_fid)
1472  !
1473 #ifdef W3_T
1474  WRITE (ndso,*) trim(myname), ' ends'
1475 #endif
1476  END SUBROUTINE read_grbtxt
1477  !/
1478  !/ ------------------------------------------------------------------- /
1479  !/
1491  SUBROUTINE read_grbtxtws(UPDPRCNT,WSPD,WDIR,FLNMCOR,SMCGRD)
1492  !/
1493  !/ +-----------------------------------+
1494  !/ | WAVEWATCH III NOAA/NCEP |
1495  !/ | Andy Saulter |
1496  !/ | FORTRAN 90 |
1497  !/ | Original code : 24-Oct-2018 |
1498  !/ | Last update : 05-Oct-2019 |
1499  !/ +-----------------------------------+
1500  !/
1501  !/ 24-Oct-2018 : Original Code ( version 6.07 )
1502  !/
1503  !/ Copyright 2010 National Weather Service (NWS),
1504  !/ National Oceanic and Atmospheric Administration. All rights
1505  !/ reserved. WAVEWATCH III is a trademark of the NWS.
1506  !/ No unauthorized use without permission.
1507  !/
1508  ! 1. Purpose :
1509  ! Read txt files that include wind data
1510  ! 2. Method :
1511  !
1512  ! 3. Parameters :
1513  !
1514  ! Local parameters.
1515  ! ----------------------------------------------------------------
1516  !
1517  ! 4. Subroutines used :
1518  !
1519  ! ----------------------------------------------------------------
1520  ! Internal Subroutines:
1521  !
1522  ! 5. Called by :
1523  !
1524  ! 6. Error messages :
1525  !
1526  ! 7. Remarks :
1527  !
1528  ! 8. Structure :
1529  !
1530  ! 9. Switches :
1531  !
1532  ! !/T
1533  !
1534  ! 10. Source code :
1535  !
1536  !/
1537  REAL, DIMENSION(:,:), INTENT(OUT) :: UPDPRCNT, WSPD, WDIR
1538  CHARACTER(*), INTENT(IN) :: FLNMCOR
1539  LOGICAL, INTENT(IN) :: SMCGRD
1540  ! Local Variables
1541  INTEGER :: I, J, IERR
1542  INTEGER :: K, L, M, N
1543  REAL :: A, WS, WD
1544  INTEGER, PARAMETER :: IP_FID = 123
1545  CHARACTER(25), PARAMETER::myname='read_grbtxt'
1546  !
1547 #ifdef W3_T
1548  WRITE (ndso,*) trim(myname), ' starts'
1549 #endif
1550  j = len_trim(fnmpre)
1551  OPEN (ip_fid,file=fnmpre(:j)//trim(flnmcor),status='OLD' &
1552  ,action='read',iostat=ierr)
1553  !
1554  ! Read text header and check dimensions match expected values
1555  IF (.NOT. smcgrd) THEN
1556  READ( ip_fid, *) m,n
1557  IF (( SIZE(updprcnt,1) /= n) .OR. ( SIZE(updprcnt,2) /= m )) THEN
1558  WRITE (ndso,*) trim(myname),': These are not the grid ' // &
1559  'dimensions: M=',m,' N=',n
1560  stop
1561  END IF
1562 #ifdef W3_SMC
1563  ELSE
1564  READ( ip_fid, *) n
1565  IF ( SIZE(updprcnt,1) /= n ) THEN
1566  WRITE (ndso,*) trim(myname),': These are not the grid ' // &
1567  'dimensions: N=',n
1568  stop
1569  END IF
1570 #endif
1571  END IF
1572  updprcnt=0
1573  wspd=0.
1574  wdir=0.
1575  !
1576  ! Read the data into allocated arrays
1577  IF (.NOT. smcgrd) THEN
1578  DO l=1,n
1579  DO k=1,m
1580  a=0.
1581  ws=0.
1582  wd=0.
1583  READ(ip_fid,*)a, ws, wd
1584  !SWH data read onto Y,X grid
1585  updprcnt(n+1-l,k)=a
1586  !Wind data read onto X,Y grid
1587  wspd(k,n+1-l)=ws
1588  wdir(k,n+1-l)=wd
1589  END DO
1590  END DO
1591 #ifdef W3_SMC
1592  ELSE
1593  DO l=1,n
1594  a=0.
1595  READ(ip_fid,*)a, ws, wd
1596  updprcnt(l,1)=a
1597  wspd(l,1)=ws
1598  wdir(l,1)=wd
1599  END DO
1600 #endif
1601  ENDIF
1602  !
1603  CLOSE(ip_fid)
1604  !
1605  !
1606 #ifdef W3_T
1607  WRITE (ndso,*) trim(myname), ' ends'
1608 #endif
1609  END SUBROUTINE read_grbtxtws
1610  !/
1611  !/ ------------------------------------------------------------------- /
1612  !/
1620  SUBROUTINE swh_rsrt_1p (VA1p, ISEA1p, HSIG1p )
1621  !/
1622  !/ +-----------------------------------+
1623  !/ | WAVEWATCH III NOAA/NCEP |
1624  !/ | Stelios Flampouris |
1625  !/ | FORTRAN 90 |
1626  !/ | Last update : 15-Mar-2017 |
1627  !/ +-----------------------------------+
1628  !/
1629  !/ 15-Mar-2017 : Original Code ( version 6.04 )
1630  !/
1631  !/ Copyright 2010 National Weather Service (NWS),
1632  !/ National Oceanic and Atmospheric Administration. All rights
1633  !/ reserved. WAVEWATCH III is a trademark of the NWS.
1634  !/ No unauthorized use without permission.
1635  !/
1636  ! 1. Purpose :
1637  ! Calculate the significant wave height from the restart file for 1 point
1638  ! 2. Method :
1639  !
1640  ! 3. Parameters :
1641  !
1642  ! Local parameters.
1643  ! ----------------------------------------------------------------
1644  !
1645  ! 4. Subroutines used :
1646  !
1647  ! ----------------------------------------------------------------
1648  ! Internal Subroutines:
1649  !
1650  ! 5. Called by :
1651  !
1652  ! 6. Error messages :
1653  !
1654  ! 7. Remarks :
1655  !
1656  ! 8. Structure :
1657  !
1658  ! 9. Switches :
1659  !
1660  ! !/T
1661  !
1662  ! 10. Source code :
1663  !
1664  !/
1665  REAL, INTENT(OUT) :: HSIG1p
1666  INTEGER, INTENT(IN) :: ISEA1p
1667  REAL, DIMENSION(:), INTENT(IN) :: VA1p
1668  CHARACTER(25),PARAMETER :: myname='SWH_RSRT_1p'
1669  !
1670 #ifdef W3_FT
1671  WRITE (ndso,*)' '
1672  WRITE (ndso,*) trim(myname), ' starts'
1673 #endif
1674  hsig1p = 0.
1675  depth = max( dmin , -zb(isea1p) )
1676  etot = 0.
1677  !
1678  DO ik=1, nk
1679  CALL wavnu1 ( sig(ik), depth, wn, cg )
1680  e1i = 0.
1681  DO ith=1, nth
1682  e1i = e1i + va1p(ith+(ik-1)*nth) * sig(ik) / cg
1683  END DO
1684  etot = etot + e1i*dsip(ik)
1685  END DO
1686  !
1687  hsig1p = 4. * sqrt( etot * dth )
1688  !
1689 #ifdef W3_FT
1690  WRITE (ndso,*) ' ', trim(myname), ' ends'
1691  WRITE (ndso,*)' '
1692 #endif
1693  END SUBROUTINE swh_rsrt_1p
1694  !/
1695  !/ ------------------------------------------------------------------- /
1696  !/
1713  SUBROUTINE swh_rsrt_1pw (VA1p, WS, WD, ISEA1p, HSIG1p, HSIGwp, HSIGsp, VAMAPWS )
1714  !/
1715  !/ +-----------------------------------+
1716  !/ | WAVEWATCH III NOAA/NCEP |
1717  !/ | Andy Saulter |
1718  !/ | FORTRAN 90 |
1719  !/ | Original code : 24-Oct-2018 |
1720  !/ | Last update : 05-Oct-2019 |
1721  !/ +-----------------------------------+
1722  !/
1723  !/ 24-Oct-2018 : Original Code ( version 6.07 )
1724  !/
1725  !/ Copyright 2010 National Weather Service (NWS),
1726  !/ National Oceanic and Atmospheric Administration. All rights
1727  !/ reserved. WAVEWATCH III is a trademark of the NWS.
1728  !/ No unauthorized use without permission.
1729  !/
1730  ! 1. Purpose :
1731  ! Calculate the significant wave height for total, wind sea and
1732  ! swell components from the restart file for 1 point
1733  ! 2. Method :
1734  !
1735  ! 3. Parameters :
1736  !
1737  ! Local parameters.
1738  ! ----------------------------------------------------------------
1739  !
1740  ! 4. Subroutines used :
1741  !
1742  ! ----------------------------------------------------------------
1743  ! Internal Subroutines:
1744  !
1745  ! 5. Called by :
1746  !
1747  ! 6. Error messages :
1748  !
1749  ! 7. Remarks :
1750  !
1751  ! 8. Structure :
1752  !
1753  ! 9. Switches :
1754  !
1755  ! !/T
1756  !
1757  ! 10. Source code :
1758  !
1759  !/
1760  USE w3gdatmd, ONLY: th
1761  USE w3odatmd, ONLY: wsmult !same wind sea cut-off factor for sea/swell outputs
1762  !
1763  REAL, INTENT(OUT) :: HSIG1p, HSIGwp, HSIGsp
1764  INTEGER, INTENT(IN) :: ISEA1p
1765  REAL, INTENT(IN) :: WS, WD
1766  REAL, DIMENSION(:), INTENT(IN) :: VA1p
1767  INTEGER, DIMENSION(:), INTENT(OUT) :: VAMAPWS ! Wind-sea id for spectral bins
1768  REAL :: RELWS, ETOTw, ETOTs, EwI, EsI
1769  CHARACTER(25),PARAMETER :: myname='SWH_RSRT_1pw'
1770  !
1771 #ifdef W3_T
1772  WRITE (ndso,*) trim(myname), ' starts'
1773 #endif
1774  hsig1p = 0.
1775  hsigwp = 0.
1776  hsigsp = 0.
1777  depth = max( dmin , -zb(isea1p) )
1778  etot = 0.
1779  etotw = 0.
1780  etots = 0.
1781  !
1782  DO ik=1, nk
1783  CALL wavnu1 ( sig(ik), depth, wn, cg )
1784  e1i = 0.
1785  ewi = 0.
1786  esi = 0.
1787  DO ith=1, nth
1788  ! Relative wind-sea calc assumes input with in direction toward
1789  ! i.e. same as for the wave spectrum
1790  relws = wsmult * ws * max(0.0, cos(wd - th(ith)))
1791  e1i = e1i + va1p(ith+(ik-1)*nth) * sig(ik) / cg
1792  IF ( relws > (sig(ik)/wn) ) THEN
1793  ewi = ewi + va1p(ith+(ik-1)*nth) * sig(ik) / cg
1794  vamapws(ith+(ik-1)*nth) = 1
1795  ELSE
1796  esi = esi + va1p(ith+(ik-1)*nth) * sig(ik) / cg
1797  vamapws(ith+(ik-1)*nth) = 0
1798  END IF
1799  END DO
1800  etot = etot + e1i*dsip(ik)
1801  etotw = etotw + ewi*dsip(ik)
1802  etots = etots + esi*dsip(ik)
1803  END DO
1804  !
1805  hsig1p = 4. * sqrt( etot * dth )
1806  hsigwp = 4. * sqrt( etotw * dth )
1807  hsigsp = 4. * sqrt( etots * dth )
1808  !
1809 #ifdef W3_T
1810  WRITE (ndso,*) trim(myname), ' ends'
1811 #endif
1812  END SUBROUTINE swh_rsrt_1pw
1813  !/
1814  !/ ------------------------------------------------------------------- /
1815  !/
1827  SUBROUTINE uvtocart (UVEC, VVEC, SPD, DCART, SMCGRD)
1828  !/
1829  !/ +-----------------------------------+
1830  !/ | WAVEWATCH III NOAA/NCEP |
1831  !/ | Andy Saulter |
1832  !/ | FORTRAN 90 |
1833  !/ | Original code : 05-Oct-2019 |
1834  !/ +-----------------------------------+
1835  !/
1836  !/ 05-Oct-2019 : Original Code ( version 6.07 )
1837  !/
1838  !/ Copyright 2010 National Weather Service (NWS),
1839  !/ National Oceanic and Atmospheric Administration. All rights
1840  !/ reserved. WAVEWATCH III is a trademark of the NWS.
1841  !/ No unauthorized use without permission.
1842  !/
1843  ! 1. Purpose :
1844  ! Calculate speed and cartesian convention directions from u,v
1845  ! input vectors
1846  ! 2. Method :
1847  !
1848  ! 3. Parameters :
1849  !
1850  ! Local parameters.
1851  ! ----------------------------------------------------------------
1852  !
1853  ! 4. Subroutines used :
1854  !
1855  ! ----------------------------------------------------------------
1856  ! Internal Subroutines:
1857  !
1858  ! 5. Called by :
1859  !
1860  ! 6. Error messages :
1861  !
1862  ! 7. Remarks :
1863  !
1864  ! 8. Structure :
1865  !
1866  ! 9. Switches :
1867  !
1868  ! !/T
1869  !
1870  ! 10. Source code :
1871  !
1872  !/
1873  USE constants, ONLY: tpi
1874  !
1875  REAL, DIMENSION(:,:), INTENT(OUT) :: SPD, DCART
1876  REAL, DIMENSION(:,:), INTENT(IN) :: UVEC, VVEC
1877  LOGICAL, INTENT(IN) :: SMCGRD
1878  !
1879 #ifdef W3_T
1880  WRITE (ndso,*) trim(myname), ' starts'
1881 #endif
1882  !
1883  DO isea=1, nsea, 1
1884  IF (.NOT. smcgrd) THEN
1885  ix = mapsf(isea,1)
1886  iy = mapsf(isea,2)
1887 #ifdef W3_SMC
1888  ELSE
1889  ix = 1
1890  iy = isea
1891 #endif
1892  ENDIF
1893  !
1894  spd(iy,ix) = sqrt( uvec(iy,ix)**2 + vvec(iy,ix)**2 )
1895  IF( spd(iy,ix) .GT. 1.e-7) THEN
1896  dcart = mod( tpi+atan2(uvec(iy,ix),vvec(iy,ix)) , tpi )
1897  ELSE
1898  dcart = 0
1899  END IF
1900  spd(iy,ix) = max( spd(iy,ix) , 0.001 )
1901  END DO
1902  !
1903 #ifdef W3_T
1904  WRITE (ndso,*) trim(myname), ' ends'
1905 #endif
1906  END SUBROUTINE uvtocart
1907  !/
1908  !/ ------------------------------------------------------------------- /
1909  !/
1919  SUBROUTINE updtwspec(VATMP, PRCNTG, VAMAPWS)
1920  !/
1921  !/ +-----------------------------------+
1922  !/ | WAVEWATCH III NOAA/NCEP |
1923  !/ | Andy Saulter |
1924  !/ | FORTRAN 90 |
1925  !/ | Original code : 24-Oct-2018 |
1926  !/ | Last update : 05-Oct-2019 |
1927  !/ +-----------------------------------+
1928  !/
1929  !/ 24-Oct-2018 : Original Code ( version 6.07 )
1930  !/
1931  !/ Copyright 2010 National Weather Service (NWS),
1932  !/ National Oceanic and Atmospheric Administration. All rights
1933  !/ reserved. WAVEWATCH III is a trademark of the NWS.
1934  !/ No unauthorized use without permission.
1935  !/
1936  ! 1. Purpose :
1937  ! Updates the wind-sea part of the wave spectrum only
1938  ! 2. Method :
1939  !
1940  ! 3. Parameters :
1941  !
1942  ! Local parameters.
1943  ! ----------------------------------------------------------------
1944  !
1945  ! 4. Subroutines used :
1946  !
1947  ! ----------------------------------------------------------------
1948  ! Internal Subroutines:
1949  !
1950  ! 5. Called by :
1951  !
1952  ! 6. Error messages :
1953  !
1954  ! 7. Remarks :
1955  !
1956  ! 8. Structure :
1957  !
1958  ! 9. Switches :
1959  !
1960  ! !/T
1961  !
1962  ! 10. Source code :
1963  !
1964  !/
1965  REAL, DIMENSION(:), INTENT(INOUT) :: VATMP
1966  INTEGER, DIMENSION(:), INTENT(IN) :: VAMAPWS
1967  REAL, INTENT(IN) :: PRCNTG
1968  CHARACTER(25),PARAMETER :: myname='UPDTWSPEC'
1969  !
1970 #ifdef W3_T
1971  WRITE (ndso,*) trim(myname), ' starts'
1972 #endif
1973  DO ik=1, nk
1974  DO ith=1, nth
1975  IF ( vamapws(ith+(ik-1)*nth) .EQ. 1 ) THEN
1976  vatmp(ith+(ik-1)*nth) = vatmp(ith+(ik-1)*nth) * prcntg**2
1977  END IF
1978  END DO
1979  END DO
1980  !
1981 #ifdef W3_T
1982  WRITE (ndso,*) trim(myname), ' ends'
1983 #endif
1984  END SUBROUTINE updtwspec
1985  !/
1986  !/ ------------------------------------------------------------------- /
1987  !/
1999  SUBROUTINE updtwspecf(VATMP, PRCNTG, VAMAPWS, ISEA1p, ADJALL)
2000  !/
2001  !/ +-----------------------------------+
2002  !/ | WAVEWATCH III NOAA/NCEP |
2003  !/ | Andy Saulter |
2004  !/ | FORTRAN 90 |
2005  !/ | Original code : 24-Oct-2018 |
2006  !/ | Last update : 05-Oct-2019 |
2007  !/ +-----------------------------------+
2008  !/
2009  !/ 24-Oct-2018 : Original Code ( version 6.07 )
2010  !/
2011  !/ Copyright 2010 National Weather Service (NWS),
2012  !/ National Oceanic and Atmospheric Administration. All rights
2013  !/ reserved. WAVEWATCH III is a trademark of the NWS.
2014  !/ No unauthorized use without permission.
2015  !/
2016  ! 1. Purpose :
2017  ! Updates the wind-sea part of the wave spectrum and shifts in frequency
2018  ! space
2019  ! 2. Method :
2020  !
2021  ! 3. Parameters :
2022  !
2023  ! Local parameters.
2024  ! ----------------------------------------------------------------
2025  !
2026  ! 4. Subroutines used :
2027  !
2028  ! ----------------------------------------------------------------
2029  ! Internal Subroutines:
2030  !
2031  ! 5. Called by :
2032  !
2033  ! 6. Error messages :
2034  !
2035  ! 7. Remarks :
2036  !
2037  ! 8. Structure :
2038  !
2039  ! 9. Switches :
2040  !
2041  ! !/T
2042  !
2043  ! 10. Source code :
2044  !
2045  !/
2046  REAL, DIMENSION(:), INTENT(INOUT) :: VATMP
2047  INTEGER, DIMENSION(:), INTENT(IN) :: VAMAPWS
2048  REAL, INTENT(IN) :: PRCNTG
2049  INTEGER, INTENT(IN) :: ISEA1p
2050  LOGICAL, INTENT(IN) :: ADJALL
2051  CHARACTER(25),PARAMETER :: myname='UPDTWSPECF'
2052  REAL :: FFAC, SIGSHFT, FDM1, FDM2, WN1, CG1, WN2, CG2
2053  INTEGER :: LPF, M1, M2
2054  REAL, ALLOCATABLE :: VASHFT(:)
2055  !
2056 #ifdef W3_T
2057  WRITE (ndso,*) trim(myname), ' starts'
2058 #endif
2059  depth = max( dmin , -zb(isea1p))
2060  ALLOCATE(vashft(SIZE(vatmp)))
2061  vashft(:) = 0.0
2062  !
2063  ! 1st iteration shifts wind-sea energy in freq space
2064  ffac = (1. / prcntg**2)**(1.0/3.0) ! uses Toba's relationship
2065  DO ik=1, nk
2066  CALL wavnu1(sig(ik), depth, wn, cg)
2067  sigshft = ffac * sig(ik)
2068  DO ith=1, nth
2069  IF ( vamapws(ith+(ik-1)*nth) .EQ. 1 ) THEN
2070  ! Interpolate frequency bin according to f-shift
2071  lpf = 1
2072  DO WHILE (lpf < nk)
2073  IF (sig(lpf) >= sigshft) THEN
2074  IF (lpf .EQ. 1) THEN
2075  CALL wavnu1(sig(lpf), depth, wn1, cg1)
2076  vashft(ith+(lpf-1)*nth) = vashft(ith+(lpf-1)*nth) + &
2077  vatmp(ith+(ik-1)*nth) * &
2078  (dsip(ik)*sig(ik)/cg) / &
2079  (dsip(lpf)*sig(lpf)/cg1)
2080  ELSE
2081  m2 = lpf
2082  m1 = lpf - 1
2083  fdm1 = sigshft - sig(m1)
2084  fdm2 = sig(m2) - sig(m1)
2085  CALL wavnu1(sig(m1), depth, wn1, cg1)
2086  CALL wavnu1(sig(m2), depth, wn2, cg2)
2087  vashft(ith+(m1-1)*nth) = vashft(ith+(m1-1)*nth) + &
2088  (fdm1 / fdm2) * &
2089  vatmp(ith+(ik-1)*nth) * &
2090  (dsip(ik)*sig(ik)/cg) / &
2091  (dsip(m1)*sig(m1)/cg1)
2092  vashft(ith+(m2-1)*nth) = vashft(ith+(m2-1)*nth) + &
2093  (1.0 - fdm1 / fdm2) * &
2094  vatmp(ith+(ik-1)*nth) * &
2095  (dsip(ik)*sig(ik)/cg) / &
2096  (dsip(m2)*sig(m2)/cg2)
2097  END IF
2098  lpf = nk + 1
2099  ENDIF
2100  lpf = lpf + 1
2101  END DO
2102  IF (lpf .EQ. nk) THEN
2103  CALL wavnu1(sig(lpf), depth, wn1, cg1)
2104  vashft(ith+(lpf-1)*nth) = vashft(ith+(lpf-1)*nth) + &
2105  vatmp(ith+(ik-1)*nth) * &
2106  (dsip(ik)*sig(ik)/cg) / &
2107  (dsip(lpf)*sig(lpf)/cg1)
2108  END IF
2109  END IF
2110  END DO
2111  END DO
2112  ! 2nd iteration scales wind-sea energy
2113  DO ik=1, nk
2114  DO ith=1, nth
2115  IF ( vamapws(ith+(ik-1)*nth) .EQ. 1 ) THEN
2116  vashft(ith+(ik-1)*nth) = vashft(ith+(ik-1)*nth) * prcntg**2
2117  END IF
2118  END DO
2119  END DO
2120  ! 3rd iteration combines wind-sea and swell energy
2121  DO ik=1, nk
2122  DO ith=1, nth
2123  IF ( vamapws(ith+(ik-1)*nth) .EQ. 1 ) THEN
2124  vatmp(ith+(ik-1)*nth) = vashft(ith+(ik-1)*nth)
2125  ELSE
2126  IF ( adjall ) THEN
2127  ! Swell components are also re-scaled
2128  vatmp(ith+(ik-1)*nth) = vatmp(ith+(ik-1)*nth) * &
2129  prcntg**2 + &
2130  vashft(ith+(ik-1)*nth)
2131  ELSE
2132  ! Re-scaling wind-sea only
2133  vatmp(ith+(ik-1)*nth) = vatmp(ith+(ik-1)*nth) + &
2134  vashft(ith+(ik-1)*nth)
2135  END IF
2136  END IF
2137  END DO
2138  END DO
2139  !
2140  DEALLOCATE(vashft)
2141  !
2142 #ifdef W3_T
2143  WRITE (ndso,*) trim(myname), ' ends'
2144 #endif
2145  END SUBROUTINE updtwspecf
2146  !/
2147  !/ ------------------------------------------------------------------- /
2148  !/
2156  SUBROUTINE writematrix(FILENAME, RDA_A)
2157  !/
2158  !/ +-----------------------------------+
2159  !/ | WAVEWATCH III NOAA/NCEP |
2160  !/ | Stelios Flampouris |
2161  !/ | FORTRAN 90 |
2162  !/ | Last update : 15-Mar-2017 |
2163  !/ +-----------------------------------+
2164  !/
2165  !/ 15-Mar-2017 : Original Code ( version 6.04 )
2166  !/
2167  !/ Copyright 2010 National Weather Service (NWS),
2168  !/ National Oceanic and Atmospheric Administration. All rights
2169  !/ reserved. WAVEWATCH III is a trademark of the NWS.
2170  !/ No unauthorized use without permission.
2171  !/
2172  ! 1. Purpose :
2173  ! Writes a 2D array to text file, column by column
2174  ! 2. Method :
2175  !
2176  ! 3. Parameters :
2177  ! fileName path to the output file
2178  ! rda_A 2D array to write
2179  !
2180  ! Local parameters.
2181  ! ----------------------------------------------------------------
2182  !
2183  ! 4. Subroutines used :
2184  !
2185  ! ----------------------------------------------------------------
2186  ! Internal Subroutines:
2187  !
2188  ! 5. Called by :
2189  ! Any routine that has to write 2d arrays !?!
2190  !
2191  ! 6. Error messages :
2192  !
2193  ! 7. Remarks :
2194  !
2195  ! 8. Structure :
2196  !
2197  ! 9. Switches :
2198  !
2199  ! !/T
2200  !
2201  ! 10. Source code :
2202  !
2203  !/
2204  REAL, DIMENSION(:, :), INTENT(IN) :: RDA_A
2205  CHARACTER(*) , INTENT(IN) :: FILENAME
2206  INTEGER IB_I, IB_J, IL_IOS
2207  INTEGER, PARAMETER :: IP_FID = 123
2208  !
2209  OPEN( unit = ip_fid, file = filename, status = 'REPLACE', &
2210  form = 'FORMATTED', iostat = il_ios)
2211  IF (il_ios /= 0) print*,'In writeMatrix : Error creating file'//filename
2212  DO ib_j = 1, SIZE(rda_a,2)
2213  DO ib_i = 1, SIZE(rda_a,1)
2214  ! write(unit=ip_fid, fmt='(I, $)') rda_A(ib_i,ib_j)
2215  WRITE(unit=ip_fid, fmt='(E18.8, $)') rda_a(ib_i,ib_j)
2216  END DO
2217  WRITE(unit=ip_fid, fmt=*)''
2218  END DO
2219  CLOSE(ip_fid)
2220  !
2221  END SUBROUTINE writematrix
2222  !/
2223  !/ ------------------------------------------------------------------- /
2224  !/
2225 END PROGRAM w3uprstr
w3nmluprstrmd::nml_update_t
Definition: w3nmluprstrmd.F90:32
w3gdatmd::nk
integer, pointer nk
Definition: w3gdatmd.F90:1230
check_prcntg
subroutine check_prcntg(PRCNTG, PRCNTG_CAP)
Last sanity check before the update of the spectrum.
Definition: ww3_uprstr.F90:1278
w3gdatmd::nseal
integer, pointer nseal
Definition: w3gdatmd.F90:1097
w3servmd::nextln
subroutine nextln(CHCKC, NDSI, NDSE)
Definition: w3servmd.F90:222
w3gdatmd::dth
real, pointer dth
Definition: w3gdatmd.F90:1232
w3adatmd::nsealm
integer, pointer nsealm
Definition: w3adatmd.F90:686
read_grbtxt
subroutine read_grbtxt(UPDPRCNT, FLNMCOR, SMCGRD)
Read gribtxt files.
Definition: ww3_uprstr.F90:1369
w3adatmd
Define data structures to set up wave model auxiliary data for several models simultaneously.
Definition: w3adatmd.F90:26
w3gdatmd::zb
real, dimension(:), pointer zb
Definition: w3gdatmd.F90:1195
w3gdatmd::fswnd
logical, pointer fswnd
Definition: w3gdatmd.F90:1264
w3gdatmd::dmin
real, pointer dmin
Definition: w3gdatmd.F90:1183
w3wdatmd
Define data structures to set up wave model dynamic data for several models simultaneously.
Definition: w3wdatmd.F90:18
w3iorsmd::w3iors
subroutine w3iors(INXOUT, NDSR, DUMFPI, IMOD, FLRSTRT)
Reads/writes restart files.
Definition: w3iorsmd.F90:113
w3gdatmd::sig
real, dimension(:), pointer sig
Definition: w3gdatmd.F90:1234
w3odatmd::iaproc
integer, pointer iaproc
Definition: w3odatmd.F90:457
w3wdatmd::time
integer, dimension(:), pointer time
Definition: w3wdatmd.F90:172
updtwspecf
subroutine updtwspecf(VATMP, PRCNTG, VAMAPWS, ISEA1p, ADJALL)
Updates the wind-sea part of the wave spectrum and shifts in frequency space.
Definition: ww3_uprstr.F90:2000
w3gdatmd::gname
character(len=30), pointer gname
Definition: w3gdatmd.F90:1223
w3gdatmd::ny
integer, pointer ny
Definition: w3gdatmd.F90:1097
w3odatmd::fnmpre
character(len=80) fnmpre
Definition: w3odatmd.F90:330
w3nmluprstrmd
Definition: w3nmluprstrmd.F90:3
writematrix
subroutine writematrix(FILENAME, RDA_A)
Writes a 2D array to text file, column by column.
Definition: ww3_uprstr.F90:2157
w3gdatmd::dsip
real, dimension(:), pointer dsip
Definition: w3gdatmd.F90:1234
w3wdatmd::va
real, dimension(:,:), pointer va
Definition: w3wdatmd.F90:183
w3gdatmd::th
real, dimension(:), pointer th
Definition: w3gdatmd.F90:1234
swh_rsrt_1pw
subroutine swh_rsrt_1pw(VA1p, WS, WD, ISEA1p, HSIG1p, HSIGwp, HSIGsp, VAMAPWS)
Calculate Hs from restart for 1 point.
Definition: ww3_uprstr.F90:1714
w3gdatmd::w3setg
subroutine w3setg(IMOD, NDSE, NDST)
Definition: w3gdatmd.F90:2152
read_grbtxtws
subroutine read_grbtxtws(UPDPRCNT, WSPD, WDIR, FLNMCOR, SMCGRD)
Read txt files that include wind data.
Definition: ww3_uprstr.F90:1492
w3odatmd::ndse
integer, pointer ndse
Definition: w3odatmd.F90:456
w3adatmd::w3seta
subroutine w3seta(IMOD, NDSE, NDST)
Select one of the WAVEWATCH III grids / models.
Definition: w3adatmd.F90:2645
w3gdatmd::rstype
integer, pointer rstype
Definition: w3gdatmd.F90:1095
w3wdatmd::w3ndat
subroutine w3ndat(NDSE, NDST)
Set up the number of grids to be used.
Definition: w3wdatmd.F90:210
w3odatmd::naperr
integer, pointer naperr
Definition: w3odatmd.F90:457
swh_rsrt_1p
subroutine swh_rsrt_1p(VA1p, ISEA1p, HSIG1p)
Calculate the significant wave height from the restart file for 1 point.
Definition: ww3_uprstr.F90:1621
w3gdatmd::nsea
integer, pointer nsea
Definition: w3gdatmd.F90:1097
uvtocart
subroutine uvtocart(UVEC, VVEC, SPD, DCART, SMCGRD)
Calculate speed and cartesian convention directions from u,v input vectors.
Definition: ww3_uprstr.F90:1828
w3servmd
Definition: w3servmd.F90:3
w3odatmd::naplog
integer, pointer naplog
Definition: w3odatmd.F90:457
w3wdatmd::w3setw
subroutine w3setw(IMOD, NDSE, NDST)
Select one of the WAVEWATCH III grids / models.
Definition: w3wdatmd.F90:660
w3odatmd::w3seto
subroutine w3seto(IMOD, NDSERR, NDSTST)
Definition: w3odatmd.F90:1523
w3gdatmd::nth
integer, pointer nth
Definition: w3gdatmd.F90:1230
w3odatmd
Definition: w3odatmd.F90:3
w3adatmd::w3naux
subroutine w3naux(NDSE, NDST)
Set up the number of grids to be used.
Definition: w3adatmd.F90:704
w3odatmd::nds
integer, dimension(:), pointer nds
Definition: w3odatmd.F90:464
w3gdatmd::mapsf
integer, dimension(:,:), pointer mapsf
Definition: w3gdatmd.F90:1163
w3iogrmd::w3iogr
subroutine w3iogr(INXOUT, NDSM, IMOD, FEXT ifdef W3_ASCII
Reading and writing of the model definition file.
Definition: w3iogrmd.F90:117
w3idatmd::w3seti
subroutine w3seti(IMOD, NDSE, NDST)
Select one of the WAVEWATCH III grids / models.
Definition: w3idatmd.F90:819
w3gdatmd::smctype
integer, parameter smctype
Definition: w3gdatmd.F90:627
file
file(STRINGS ${CMAKE_BINARY_DIR}/switch switch_strings) separate_arguments(switches UNIX_COMMAND $
Definition: CMakeLists.txt:3
w3iogrmd
Reading/writing of model definition file.
Definition: w3iogrmd.F90:20
w3odatmd::wsmult
real, pointer wsmult
Definition: w3odatmd.F90:553
constants::tpi
real, parameter tpi
TPI 2*Pi.
Definition: constants.F90:72
w3gdatmd::gtype
integer, pointer gtype
Definition: w3gdatmd.F90:1094
w3idatmd
Define data structures to set up wave model input data for several models simultaneously.
Definition: w3idatmd.F90:16
w3odatmd::ndso
integer, pointer ndso
Definition: w3odatmd.F90:456
w3gdatmd::w3nmod
subroutine w3nmod(NUMBER, NDSE, NDST, NAUX)
Definition: w3gdatmd.F90:1433
w3odatmd::napout
integer, pointer napout
Definition: w3odatmd.F90:457
w3nmluprstrmd::w3nmluprstr
subroutine w3nmluprstr(NDSI, INFILE, NML_RESTART, NML_UPDATE, IERR)
Definition: w3nmluprstrmd.F90:50
w3odatmd::idout
character(len=20), dimension(nogrp, ngrpp) idout
Definition: w3odatmd.F90:329
w3odatmd::ndst
integer, pointer ndst
Definition: w3odatmd.F90:456
w3nmluprstrmd::nml_restart_t
Definition: w3nmluprstrmd.F90:27
constants
Define some much-used constants for global use (all defined as PARAMETER).
Definition: constants.F90:20
w3gdatmd
Definition: w3gdatmd.F90:16
w3dispmd::wavnu1
subroutine wavnu1(SI, H, K, CG)
Definition: w3dispmd.F90:85
w3servmd::extcde
subroutine extcde(IEXIT, UNIT, MSG, FILE, LINE, COMM)
Definition: w3servmd.F90:736
w3odatmd::w3nout
subroutine w3nout(NDSERR, NDSTST)
Definition: w3odatmd.F90:561
w3servmd::itrace
subroutine itrace(NDS, NMAX)
Definition: w3servmd.F90:91
w3gdatmd::nx
integer, pointer nx
Definition: w3gdatmd.F90:1097
updtwspec
subroutine updtwspec(VATMP, PRCNTG, VAMAPWS)
Updates the wind-sea part of the wave spectrum only.
Definition: ww3_uprstr.F90:1920
update_va
subroutine update_va(PRCNTG, VATMP)
Apply correction to the spectrum.
Definition: ww3_uprstr.F90:1214
w3idatmd::w3ninp
subroutine w3ninp(NDSE, NDST)
Set up the number of grids to be used.
Definition: w3idatmd.F90:283
w3dispmd
Definition: w3dispmd.F90:3
w3uprstr
program w3uprstr
Update restart files based on Hs from DA.
Definition: ww3_uprstr.F90:22
w3gdatmd::mapsta
integer, dimension(:,:), pointer mapsta
Definition: w3gdatmd.F90:1163
w3iorsmd
Read/write restart files.
Definition: w3iorsmd.F90:14