-
Notifications
You must be signed in to change notification settings - Fork 559
/
Copy pathww3_uprstr.F90
2225 lines (2219 loc) · 69.9 KB
/
ww3_uprstr.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!> @file
!> @brief Contains the program W3UPRSTR.
!>
!> @author Stelios Flampouris @date 16-Feb-2017
#include "w3macros.h"
!/ ------------------------------------------------------------------- /
!>
!> @brief Update restart files based on Hs from DA.
!>
!> @details Update the WAVEWATCH III restart files based on the significant
!> wave height analysis from any data assimilation system.
!>
!> The W3UPRSTR is the intermediator between the background WW3
!> and the analysis of the wave field, it modifies the original restart
!> file according to the analysis.
!> For the wave modeling and DA, the ww3_uprstr program applies the
!> operator from the diagnostic to the prognostic variable.
!>
!> @author Stelios Flampouris @date 16-Feb-2017
!>
PROGRAM W3UPRSTR
!/
!/ +-----------------------------------+
!/ | WAVEWATCH III NOAA/NCEP |
!/ | Stelios Flampouris |
!/ | FORTRAN 90 |
!/ | First version: 16-Feb-2017 |
!/ +-----------------------------------+
!/
!/ 16-Feb-2017 : Original Code ( version 6.03 )
!/ 07-Jun-2017 : Change of the Core
!/ 07-Jul-2017 : Clean the code, add some flexibility, etc
!/ 04-Sep-2017 : Simplified the code, take out a significant part of the
!/ flexibility (The code is still available at SVN/UpRest)
!/ 15-Sep-2017 : Version 0.65 ( version 6.03 )
!/ 01-Oct-2018 : Fixes to preserve spectral energy correctly
!/ (Andy Saulter) ( version 6.06 )
!/ 17-Oct-2018 : Version 0.95 ( version 6.06 )
!/ Simplified the code, remove some user unfriendly
!/ options, add reg test ta1, add logical checks,
!/ unified the operator, add/update the documentation.
!/ 05-Oct-2019 : Added UPD5 and UPD6 options, plus logic for running
!/ with SMC grids (Andy Saulter) ( version 6.07 )
!/ 01-Nov-2019 : UPD5 and UPD6 use wind data either from anl.XXX file
!/ or from restart under WRST switch (Andy Saulter)
!/ 06-Oct-2020 : Added namelist input options ( version 7.11 )
!/ 06-May-2021 : Use SMCTYPE and FSWND for SMC grid. ( version 7.13 )
!/
!/ Copyright 2010 National Weather Service (NWS),
!/ National Oceanic and Atmospheric Administration. All rights
!/ reserved. WAVEWATCH III is a trademark of the NWS.
!/ No unauthorized use without permission.
!/
! 1. Purpose :
!
! Update the WAVEWATCH III restart files based on the significant
! wave height analysis from any data assimilation system.
!
! 2. Method :
!
! 2.1. General:
! The W3UPRSTR is the intermediator between the background WW3
! and the analysis of the wave field, it modifies the original restart
! file according to the analysis.
! For the wave modeling and DA, the ww3_uprstr program applies the
! operator from the diagnostic to the prognostic variable.
!
! See the chart below:
!
! +-------------------+
! | WW3 Background Run|
! +-------------------+
! +--------------+ | +-----+
! | Restart File | <------------|-----------------> | Hs |
! +--------------+ +-----+
! | |
! | |
! | +---------------+ +-----+
! | |Hs Observations|-------> | D.A.|
! | +---------------+ +-----+
! | |
! | +----------+ |
! +----------------> | W3UPRSTR |<-/Analysis/--+
! +----------+
! |
! +----------------------+
! | Updated Restart File |
! +----------------------+
!
! A. The WW3 Background Run has to provide two files:
! i. The field of Hs (for NCEP at grib2 format) and
! ii. the restart.ww3, at the WW3 format for restart files.
! Both of them, at the moment of the assimilation (Nevertheless, the WW3
! restart reader will fail when the timestamps are not identical).
!
! B. The DA module produces the analysis and/or the difference (%) of
! the analysis from the first guess of Hs in the space of model and
! exports the results.
!
! C. The algorithm
! The Hs correction is redistributed to each frequency and direction.
!
! 1. The W3UPRSTR imports: i. the restart.ww3,
! ii. the analysis file and
! iii. the input file: ww3_uprstr.inp, details at 2.2.2.i.
!
! 2. The W3UPRSTR updates the restart file according to the option at
! ww3_uprstr UPD[N]
! Note: With the version 6.06 some options have been removed, but the naming
! is consistent with the original version.
!
! 3. W3UPRSTR exports the updated spectrum in the same format as the
! restart.ww3. The name of the output file is: restart001.ww3 and it has to
! be renamed "restart.ww3" for the initialization of the next prediction
! cycle.
!
! E. The user runs WW3 with the analysis restart file.
!
! 2.2. How to use ww3_uprstr
! The ww3_uperstr is one of the WW3 auxilary programs, therefore it works in
! a very similar way as the other auxilary programs.
!
! A. To compile:
!
! ww3_uprstr is included in the make_makefile.sh, to compile:
! $ ./w3_make ww3_uprstr
! or
! $ ./w3_make
!
! And the executable "ww3_uprstr" will appear at [...]/model/exe/
!
! B. To run:
! At the computational path:
! > ${EXE}/ww3_uprstr
! And it should run if the input files are at ./
!
! C. Input Files:
!
! i. ww3_uprstr.inp
! It includes some limited information for running the program:
!
! -------------------------------------------------------------------- $
! WAVEWATCH III Update Restart input file $
! -------------------------------------------------------------------- $
!
! Time of Assimilation ----------------------------------------------- $
! - Starting time in yyyymmdd hhmmss format.
!
! This is the assimilation starting time and has to be the same with
! the time at the restart.ww3.
!
! 19680607 120000
!
! Choose algorithm to update restart file
! UPDN for the Nth approach
! The UPDN*, with N<2 the same correction factor is applied at all the grid points
! UPD0C:: ELIMINATED
! UPDOF:: Option 0F All the spectra are updated with a constant
! fac=HsAnl/HsBckg.
! Expected input: PRCNTG, as defined at fac
! UPD1 :: ELIMINATED
! UPDN, with N>1 each gridpoint has its own update factor.
! UPD2 :: Option 2 The fac(x,y,frq,theta), is calculated at each grid point
! according to the ratio of HsBckg and HsAnl (squared to preseve energy)
! Expected input: the Analysis field, grbtxt format
! UPD3 :: Option 3 The update factor is a surface with the shape of
! the background spectrum.
! Expected input: the Analysis field, grbtxt format
! UPD4 :: [NOT INCLUDED in this Version, Just keeping the spot]
! Option 4 The generalization of the UPD3. The update factor
! is the sum of surfaces which are applied on the background
! spectrum.
! The algorithm requires the mapping of each partition on the
! individual spectra; the map is used to determine the weighting
! surfaces.
! Expected input: the Analysis field, grbtxt format and the
! functions(frq,theta) of the update to be applied.
! UPD5 :: Option 5 Corrections are calculated as per UPD2 but are
! applied to wind-sea parts of the spectrum only when wind-sea
! is the dominant component, otherwise the whole spectrum is
! corrected
! Expected input: the Analysis Hs field plus background wind speed
! and direction
! UPD6 :: Option 6 Corrections are calculated as per UPD5 but wind-sea
! components are also shifted in frequency space using Toba (1973)
! Expected input: the Analysis Hs field plus background wind speed
! and direction
!
! PRCNTG is input for option UPD0F and is the correction factor
! applied to all the gridpoints (e.g. 1.)
!
! 0.475
!
! PRCNTG_CAP is global input for option UPD2 and UPD3 and it is a cap on
! the maximum SWH correction factor applied to all the gridpoints, as
! both a multiple or divisor (e.g. cap at 5.0 means SWHANL/SWHBKG<=5.0
! and SWHANL/SWHBKG>=0.2). The value given should not be less than 1.0
!
! 5.0
!
! Name of the file with the SWH analysis from the DA system $
! suffix .grbtxt for text out of grib2 file. $
!
! anl.grbtxt
!
! -------------------------------------------------------------------- $
! WAVEWATCH III EoF ww3_uprstr.inp
! -------------------------------------------------------------------- $
!
! ii. Data files anl.XXX
!
! FOR UPD2,3 and UPD5,6 with WRST switch
! USE THE grbtxt FORMAT, See Format E.
!
! Format E.
! Text file created by wgrib2. This format is tested more extensively
! and currently the only format supported for anl.grbtxt.
!
! NX NY
! VAL0001
! VAL0002
! ...
! VALNX*NY
!
! IMPORTANT : All the regtests are with the format E. strongly recommended.
! The order of the values in .grbtxt, is assumed the same by
! default as the order of spectral data in the restart file.
!
! NOTE: It is recommended to use UPD5,6 with the WRST switch enabled and
! using SWH analysis data only as per Format E. However, the code includes
! an option to run using a text file in which case:
! USE THE grbtxtws format below
!
! Text file with following lines:
! NX NY
! SWH0001 WSPD0001 WDIR0001
! SWH0002 WSPD0002 WDIR0002
! ...
! SWHNX*NY WSPDNX*NY WDIRNX*NY
!
! The order of the values in .grbtxt, is assumed the same by
! default as the order of spectral data in the restart file.
! Wind speeds and directions in the anl.XXX file are assumed to be
! in CARTESIAN (GRID U,V) CONVENTION
!
! NOTE About Format: if you prefer a different format; there are several
! I/O subroutines ready, not included in the current version of the code,
! contact the prgmr to get access to the source code.
!
! iii. restart.ww3
! The restart file as came out of the background run, the name has to be
! restart.ww3, but the name of the output depends on the mod_def.ww3, the
! ww3_uprstr follows its content (be careful with ovewriting).
!
! 3. Example
! Use the regression tests ww3_ta1
!
! 4. Parameters :
!
! Local parameters.
! ----------------------------------------------------------------
!
! ----------------------------------------------------------------
!
! 5. Subroutines used :
!
! Name Type Module Description
! ----------------------------------------------------------------
! W3NMOD Subr. W3GDATMD Set number of model.
! W3SETG Subr. Id. Point to selected model.
! W3NDAT Subr. W3WDATMD Set number of model for wave data.
! W3SETW Subr. Id. Point to selected model for wave data.
! W3NINP Subr. W3IDATMD Set number of grids/models.
! W3SETI Subr. Id. Point to data structure.
! W3DIMI Subr. Id. Set array sizes in data structure.
! W2NAUX Subr. W3ADATMD Set number of model for aux data.
! W3SETA Subr. Id. Point to selected model for aux data.
! ITRACE Subr. W3SERVMD Subroutine tracing initialization.
! NEXTLN Subr. Id. Get next line from input file.
! EXTCDE Subr. Id. Abort program as graceful as possible.
! W3IOGR Subr. W3IOGRMD Reading/writing model definition file.
! WAVNU1 Subr. W3DISPMD
! ----------------------------------------------------------------
! Internal Subroutines:
!
! READ_GRBTXT
! WORKER
! SWH_RSRT_1p
! WRITEMATRIX
!
! 6. Called by :
!
! None, stand-alone program.
!
! 7. Error messages :
!
! 8. Remarks:
!
! 7.1 Use the grbtxt format for correction and analysis files.
!
! 7.2 There are some variables not used but declared, it's for future
! development.
!
! 9. Structure :
!
! ----------------------------------------------------
! 1. Set up data structures.
! 2. Read model defintion file with base model ( W3IOGR )
! 3. Import restart file ( W3IORS )
! 4. Import correction percentage ( )
! OR Import the analysis field ( )
! 5. Apply correction to the restart file ( )
! 6. Export the updated restart file ( W3IORS )
! ----------------------------------------------------
!
! 10. Switches :
!
! !/SHRD Switch for shared / distributed memory architecture.
! !/T
! !/S Enable subroutine tracing.
!
! 11. Known Bugs
!
! 1. Fix the format for the output (NSDO) of non strings, e.g. for
! TIME.
!
! 12. Source code :
!
!/
USE W3GDATMD, ONLY: W3NMOD, W3SETG
USE W3WDATMD, ONLY: W3NDAT, W3SETW
USE W3ADATMD, ONLY: W3NAUX, W3SETA
USE W3ODATMD, ONLY: W3NOUT, W3SETO
USE W3IORSMD, ONLY: W3IORS
USE W3SERVMD, ONLY: ITRACE, NEXTLN, EXTCDE
USE W3IOGRMD, ONLY: W3IOGR
USE W3DISPMD, ONLY: WAVNU1
!
USE W3GDATMD, ONLY: GNAME, NX, NY, MAPSTA, SIG, NK, NTH, NSEA, &
NSEAL, MAPSF, DMIN, ZB, DSIP, DTH, RSTYPE, &
GTYPE, SMCTYPE
#ifdef W3_SMC
USE W3GDATMD, ONLY: FSWND
#endif
USE W3WDATMD, ONLY: VA, TIME
USE W3ADATMD, ONLY: NSEALM
USE W3ODATMD, ONLY: IAPROC, NAPERR, NAPLOG, NDS, NAPOUT
USE W3ODATMD, ONLY: NDSE, NDSO, NDST, IDOUT, FNMPRE
#ifdef W3_WRST
USE W3IDATMD
#endif
!
USE W3NMLUPRSTRMD
!
IMPLICIT NONE
!/
!/ ------------------------------------------------------------------- /
! Local variables
!/
INTEGER :: NDSI, NDSM, NDSTRC, NTRACE, IERR, I, J
CHARACTER :: COMSTR*1
!
TYPE(NML_RESTART_T) :: NML_RESTART
TYPE(NML_UPDATE_T) :: NML_UPDATE
!
! REAL, ALLOCATABLE :: BETAW(:)
! LOGICAL, ALLOCATABLE :: MASK(:)
LOGICAL :: anl_exists, CORWSEA, FLGNML
INTEGER :: IMOD, NDSEN, IX, IY, IK, ITH, &
IXW, IYW
REAL, ALLOCATABLE :: UPDPRCNT(:,:),VATMP(:), HSIG(:,:), &
A(:), HS_ANAL(:,:), gues(:,:), &
HS_DIF(:,:),SWHANL(:,:), SWHBCKG(:,:), &
SWHUPRSTR(:,:),VATMP_NORM(:), &
WSBCKG(:,:),WDRBCKG(:,:)
INTEGER, ALLOCATABLE :: VAMAPWS(:)
REAL :: PRCNTG, PRCNTG_CAP, THRWSEA
INTEGER :: ROWS, COLS, ISEA
CHARACTER(128) :: FLNMCOR, FLNMANL
CHARACTER(16) :: UPDPROC
! for howv
REAL :: SWHTMP,SWHBCKG_1, SWHANL_1, &
DEPTH, WN, CG, ETOT, E1I, &
SWHTMP1,SUMVATMP, SWHBCKG_W, SWHBCKG_S
REAL :: K
CHARACTER(8), PARAMETER :: MYNAME='W3UPRSTR'
LOGICAL :: SMCGRD = .FALSE.
LOGICAL :: SMCWND = .FALSE.
LOGICAL :: WRSTON = .FALSE.
!/
!/ ------------------------------------------------------------------- /
!/
! 1. IO set-up.
CALL W3NMOD ( 1, 6, 6 )
CALL W3SETG ( 1, 6, 6 )
CALL W3NDAT ( 6, 6 )
CALL W3SETW ( 1, 6, 6 )
CALL W3NAUX ( 6, 6 )
CALL W3SETA ( 1, 6, 6 )
CALL W3NOUT ( 6, 6 )
CALL W3SETO ( 1, 6, 6 )
#ifdef W3_WRST
CALL W3NINP ( 6, 6 )
CALL W3SETI ( 1, 6, 6 )
#endif
!
NDSE = 6
NDSI = 10
NDSM = 20
!
IAPROC = 1
NAPOUT = 1
NAPERR = 1
IMOD = 1
NAPLOG = 1
!
NDSTRC = 6
NTRACE = 10
CALL ITRACE ( NDSTRC, NTRACE )
!
IF ( IAPROC .EQ. NAPERR ) THEN
NDSEN = NDSE
ELSE
NDSEN = -1
END IF
!
WRITE (NDSO,900)
!
#ifdef W3_WRST
!Compiling with WRST will allow access to options UPD5/6
WRSTON = .TRUE.
WRITE (NDSO,*) '*** UPRSTR will read wind from restart files'
#endif
!/
!/ ------------------------------------------------------------------- /
! 2. Read the ww3_uprstr input data
!
! process ww3_uprstr namelist
!
INQUIRE(FILE=TRIM(FNMPRE)//"ww3_uprstr.nml", EXIST=FLGNML)
IF (FLGNML) THEN
! Read namelist
CALL W3NMLUPRSTR (NDSI, TRIM(FNMPRE)//'ww3_uprstr.nml', NML_RESTART, &
NML_UPDATE, IERR)
READ(NML_RESTART%RESTARTTIME, *) TIME
UPDPROC = NML_UPDATE%UPDPROC
PRCNTG = NML_UPDATE%PRCNTG
PRCNTG_CAP = NML_UPDATE%PRCNTGCAP
THRWSEA = NML_UPDATE%THRWSEA
FLNMANL = NML_UPDATE%FILE
END IF
!/
! otherwise read from the .inp file
IF (.NOT. FLGNML) THEN
J = LEN_TRIM(FNMPRE)
OPEN (NDSI,FILE=FNMPRE(:J)//'ww3_uprstr.inp',STATUS='OLD', &
ERR=800,IOSTAT=IERR)
READ (NDSI,'(A)',END=801,ERR=802) COMSTR
IF (COMSTR.EQ.' ') COMSTR = '$'
WRITE (NDSO,901) COMSTR
!
CALL NEXTLN ( COMSTR , NDSI , NDSEN )
READ (NDSI,*,END=2001,ERR=2002) TIME
CALL NEXTLN ( COMSTR , NDSI , NDSEN )
READ (NDSI,*,END=2001,ERR=2002) UPDPROC
CALL NEXTLN ( COMSTR , NDSI , NDSEN )
IF (UPDPROC .EQ. 'UPD0F') THEN
READ (NDSI,*,END=2001,ERR=2002) PRCNTG
ELSE
IF ((UPDPROC .EQ. 'UPD2') .OR. (UPDPROC .EQ. 'UPD3')) THEN
! CALL NEXTLN ( COMSTR , NDSI , NDSEN )
READ (NDSI,*,END=2001,ERR=2002) PRCNTG_CAP
#ifdef W3_F
CALL NEXTLN ( COMSTR , NDSI , NDSEN )
READ (NDSI,*,END=2001,ERR=2002) FLNMCOR
#endif
ELSE
READ (NDSI,*,END=2001,ERR=2002) PRCNTG_CAP, THRWSEA
END IF
CALL NEXTLN ( COMSTR , NDSI , NDSEN )
READ (NDSI,*,END=2001,ERR=2002) FLNMANL
END IF
ENDIF
#ifdef W3_T
WRITE (NDSO,*)' TIME: ',TIME
#endif
!/
!/ ------------------------------------------------------------------- /
! 3. Read model definition file.
!/
CALL W3IOGR ( 'READ', NDSM )
NSEAL = NSEA
WRITE (NDSO,920) GNAME
!/
#ifdef W3_SMC
!! SMC grid option is activated if GTYPE .EQ. SMCTYPE. JGLi06May2021
IF( GTYPE .EQ. SMCTYPE ) SMCGRD = .TRUE.
!! SMC sea-point wind option is activated if FSWND=.TRUE. JGLi06May2021
IF( FSWND ) SMCWND = .TRUE.
#endif
#ifdef W3_WRST
! Override SMCWND - at present restarts only store wind on
! a regular grid
SMCWND = .FALSE.
#endif
#ifdef W3_SMC
WRITE (NDSO,*) '*** UPRSTR set to work with SMC grid model'
#endif
!/
!/ ------------------------------------------------------------------- /
! 4. Read restart file
!/
#ifdef W3_WRST
! Set the wind flag to true when reading restart wind
INFLAGS1(3) = .TRUE.
CALL W3DIMI ( 1, 6, 6 ) !Needs to be called after w3iogr to have correct dimensions?
#endif
CALL W3IORS ( 'READ', NDS(6), SIG(NK), IMOD )!
IF ( IAPROC .EQ. NAPLOG ) THEN
IF (RSTYPE.EQ.0.OR.RSTYPE.EQ.1.OR.RSTYPE.EQ.4) THEN
WRITE (NDSO,1004) 'Terminating ww3_uprstr: The restart ' // &
'file is not read'
CALL EXTCDE ( 1 )
ELSE
WRITE (NDSO,1004) ' Updating Restart File'
WRITE (NDSO,*) ' TIME: ',TIME
END IF
END IF
#ifdef W3_T
WRITE (NDST,*), MYNAME,' : Exporting VA as imported to VA01.txt'
CALL writeMatrix('VA01.txt', REAL(VA))
#endif
!/
!/ ------------------------------------------------------------------- /
! 5. Update restart spectra array according to the selected option
!/
SELECT CASE (UPDPROC)
!/
!/ ------------------------------------------------------------------- /
! UPD0F
!/
CASE ('UPD0F')
WRITE (NDSO,902) 'UPD0F'
WRITE (NDSO,1005) ' PRCNTG = ',PRCNTG
#ifdef W3_T
ALLOCATE( VATMP (SIZE(VA ,1) ))
ALLOCATE( SWHANL (SIZE(MAPSTA,1), SIZE(MAPSTA,2)))
ALLOCATE( SWHBCKG(SIZE(MAPSTA,1), SIZE(MAPSTA,2)))
#endif
DO ISEA=1, NSEA, 1
#ifdef W3_T
IX = MAPSF(ISEA,1)
IY = MAPSF(ISEA,2)
VATMP = VA(:,ISEA)
CALL SWH_RSRT_1p (VATMP, ISEA, SWHBCKG_1)
SWHBCKG(IY,IX)=SWHBCKG_1
#endif
CALL UPDATE_VA(PRCNTG, VA(:,ISEA))
#ifdef W3_T
VATMP = VA(:,ISEA)
CALL SWH_RSRT_1p (VATMP, ISEA, SWHANL_1)
SWHANL(IY,IX)=SWHANL_1
WRITE (NDSO,*) ' =========== UPD0F Output ==========='
WRITE (NDSO,*)'ISEA = ', ISEA,' PRCNTG = ',PRCNTG, &
' SWHBCKG = ',SWHBCKG(IY,IX), &
' SWHANL= ', SWHANL(IY,IX)
#endif
END DO
#ifdef W3_T
CALL writeMatrix('SWHBCKG_UPD0F.txt', REAL(SWHBCKG))
CALL writeMatrix('SWHANL_UPD0F.txt' , REAL(SWHANL ))
CALL writeMatrix('SWHRSTR_UPD0F.txt', REAL(SWHANL ))
DEALLOCATE ( VATMP, SWHBCKG, SWHANL )
#endif
!/
!/ ------------------------------------------------------------------- /
! UPD2
! Apply a bulk correction to the wave spectrum at each grid cell based
! on the ratio of HsBckg and HsAnl
!/
CASE ('UPD2')
WRITE (NDSO,902) 'UPD2'
WRITE (NDSO,1005) ' PRCNTG_CAP = ',PRCNTG_CAP
WRITE (NDSO,1006) ' Reading updated SWH from: ',trim(FLNMANL)
!
! Array allocation
ALLOCATE ( VATMP(SIZE(VA,1)))
IF (.NOT. SMCGRD) THEN
ALLOCATE( SWHBCKG(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
ALLOCATE( SWHANL(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
#ifdef W3_SMC
ELSE
ALLOCATE( SWHBCKG(NSEA,1) )
ALLOCATE( SWHANL(NSEA,1) )
#endif
ENDIF
#ifdef W3_T
IF (.NOT. SMCGRD) THEN
ALLOCATE( SWHUPRSTR(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
ELSE
ALLOCATE( SWHUPRSTR(NSEA,1) )
ENDIF
#endif
!
! Read additional Input: Analysis Field
INQUIRE(FILE=FLNMANL, EXIST=anl_exists)
IF (anl_exists) THEN
#ifdef W3_T
WRITE (NDSO,*) 'shape(SWHANL)', shape(SWHANL)
#endif
CALL READ_GRBTXT(SWHANL, FLNMANL, SMCGRD)
#ifdef W3_T
CALL writeMatrix('SWHANL_IN.txt',SWHANL)
#endif
ELSE
WRITE (NDSO,*) trim(FLNMANL), ' does not exist, stopping...'
DEALLOCATE( SWHANL,VATMP,SWHBCKG )
#ifdef W3_T
DEALLOCATE( SWHUPRSTR )
#endif
STOP
END IF
!
! Calculation
DO ISEA=1, NSEA, 1
IF (.NOT. SMCGRD) THEN
IX = MAPSF(ISEA,1)
IY = MAPSF(ISEA,2)
#ifdef W3_SMC
ELSE
IX = 1
IY = ISEA
#endif
ENDIF
VATMP = VA(:,ISEA)
CALL SWH_RSRT_1p (VATMP, ISEA, SWHBCKG_1)
SWHBCKG(IY,IX)=SWHBCKG_1
!
IF ( SWHBCKG(IY,IX) > 0.01 .AND. SWHANL(IY,IX) > 0.01 ) THEN
PRCNTG=(SWHANL(IY,IX)/SWHBCKG_1)
#ifdef W3_T
WRITE (NDSO,*) 'ISEA = ', ISEA,' IX = ',IX,' IY = ', IY, &
' PRCNTG = ',PRCNTG,' SWHBCKG = ',SWHBCKG(IY,IX), &
' SWHANL = ', SWHANL(IY,IX)
#endif
CALL CHECK_PRCNTG (PRCNTG,PRCNTG_CAP)
CALL UPDATE_VA(PRCNTG, VA(:,ISEA))
#ifdef W3_T
CALL SWH_RSRT_1p (VA(:,ISEA), ISEA, SWHUPRSTR(IY,IX))
WRITE (NDSO,*) ' =========== UPD2 Output ==========='
WRITE (NDSO,*)'ISEA = ',ISEA, &
'SWH_BCKG = ', SWHBCKG(IY,IX), &
'SWH_ANL = ', SWHANL(IY,IX), &
'PRCNTG = ', PRCNTG, &
'SWH_RSTR = ',SWHUPRSTR(IY,IX)
#endif
END IF
END DO
#ifdef W3_T
CALL writeMatrix('SWHBCKG_UPD2.txt', REAL(SWHBCKG ))
CALL writeMatrix('SWHANL_UPD2.txt' , REAL(SWHANL ))
CALL writeMatrix('SWHRSTR_UPD2.txt', REAL(SWHUPRSTR))
#endif
!
DEALLOCATE( SWHANL,VATMP,SWHBCKG )
#ifdef W3_T
DEALLOCATE( SWHUPRSTR )
#endif
!/
!/ ------------------------------------------------------------------- /
! UPD3
! As per UPD2, but the update factor is a surface with the shape of the
! background spectrum
!/
CASE ('UPD3')
WRITE (NDSO,902) 'UPD3'
WRITE (NDSO,1005) ' PRCNTG_CAP = ',PRCNTG_CAP
WRITE (NDSO,1006) ' Reading updated SWH from: ',trim(FLNMANL)
!
! Array allocation
ALLOCATE ( VATMP(SIZE(VA,1)))
ALLOCATE ( VATMP_NORM(SIZE(VA,1)))
ALLOCATE ( A(SIZE(VA,1)))
IF (.NOT. SMCGRD) THEN
ALLOCATE( SWHBCKG(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
ALLOCATE( SWHANL(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
#ifdef W3_SMC
ELSE
ALLOCATE( SWHBCKG(NSEA,1) )
ALLOCATE( SWHANL(NSEA,1) )
#endif
ENDIF
#ifdef W3_T
IF (.NOT. SMCGRD) THEN
ALLOCATE( SWHUPRSTR(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
ELSE
ALLOCATE( SWHUPRSTR(NSEA,1) )
ENDIF
#endif
!
! Read additional Input: Analysis Field
INQUIRE(FILE=FLNMANL, EXIST=anl_exists)
IF (anl_exists) THEN
#ifdef W3_T
WRITE (NDSO,*) 'shape(SWHANL)', shape(SWHANL)
#endif
CALL READ_GRBTXT(SWHANL, FLNMANL, SMCGRD)
#ifdef W3_T
CALL writeMatrix('SWHANL_IN.txt',SWHANL)
#endif
ELSE
WRITE (NDSO,*) trim(FLNMANL), ' does not exist, stopping...'
DEALLOCATE( SWHANL,VATMP,SWHBCKG,VATMP_NORM,A )
#ifdef W3_T
DEALLOCATE( SWHUPRSTR )
#endif
STOP
END IF
!
! Calculation
DO ISEA=1, NSEA, 1
IF (.NOT. SMCGRD) THEN
IX = MAPSF(ISEA,1)
IY = MAPSF(ISEA,2)
#ifdef W3_SMC
ELSE
IX = 1
IY = ISEA
#endif
ENDIF
VATMP = VA(:,ISEA)
CALL SWH_RSRT_1p (VATMP, ISEA, SWHBCKG_1)
SWHBCKG(IY,IX)=SWHBCKG_1
!
IF ( SWHBCKG(IY,IX) > 0.01 .AND. SWHANL(IY,IX) > 0.01 ) THEN
!Step 1.
PRCNTG=(SWHANL(IY,IX)/SWHBCKG_1)
#ifdef W3_T
WRITE (NDSO,*) ' =========== Step 1. ==========='
WRITE (NDSO,*) ' ISEA = ', ISEA,' IX = ',IX,' IY = ', IY, &
' PRCNTG = ',PRCNTG,' SWHBCKG = ',SWHBCKG(IY,IX), &
' SWHANL = ', SWHANL(IY,IX)
#endif
CALL CHECK_PRCNTG(PRCNTG,PRCNTG_CAP)
VATMP_NORM=VATMP/SUM(VATMP)
#ifdef W3_T
WRITE (NDSO,*)' ISEA =', ISEA,' IX = ',IX,' IY = ', IY, &
' PRCNTG = ',PRCNTG, &
' SWHBCKG = ',SWHBCKG(IY,IX), ' SWHANL = ', SWHANL(IY,IX)
#endif
IF (PRCNTG > 1.) THEN
A=PRCNTG**2*(1 + VATMP_NORM)
ELSE
A=PRCNTG**2*(1 - VATMP_NORM)
END IF
VATMP=A*VATMP
CALL SWH_RSRT_1p (VATMP, ISEA, SWHTMP)
PRCNTG=(SWHANL(IY,IX)/SWHTMP)
#ifdef W3_T
SWHUPRSTR(IY,IX)=SWHTMP
WRITE (NDSO,*) ' =========== Step 2. ==========='
WRITE (NDSO,*)'ISEA = ', ISEA, ' PRCNTG = ',PRCNTG, &
' SWHANL= ', SWHANL(IY,IX), &
' SWHUPRSTR(IY,IX) = ', SWHUPRSTR(IY,IX)
#endif
CALL CHECK_PRCNTG (PRCNTG,PRCNTG_CAP)
CALL UPDATE_VA(PRCNTG, VATMP)
VA(:,ISEA)=VATMP
#ifdef W3_T
CALL SWH_RSRT_1p (VATMP, ISEA, SWHTMP)
SWHUPRSTR(IY,IX)=SWHTMP
WRITE (NDSO,*) ' =========== UPD3 Output ==========='
WRITE (NDSO,*)'ISEA = ',ISEA,'SWH_BCKG = ', SWHBCKG(IY,IX), &
'SWH_ANL = ', SWHANL(IY,IX), &
'SWH_RSTR = ',SWHUPRSTR(IY,IX)
#endif
END IF
END DO
#ifdef W3_T
CALL writeMatrix('SWHBCKG_UPD3.txt', REAL(SWHBCKG))
CALL writeMatrix('SWHANL_UPD3.txt' , REAL(SWHANL ))
CALL writeMatrix('SWHRSTR_UPD3.txt', REAL(SWHUPRSTR))
#endif
!
DEALLOCATE( SWHANL,VATMP,SWHBCKG,VATMP_NORM,A )
#ifdef W3_T
DEALLOCATE( SWHUPRSTR )
#endif
!/
!/ ------------------------------------------------------------------- /
! UPD5
! Corrects wind-sea only in wind dominated conditions - bulk correction
! The fac(x,y,frq,theta), is calculated at each grid point according to
! HsBckg and HsAnl
!/
CASE ('UPD5')
WRITE (NDSO,902) 'UPD5'
WRITE (NDSO,1005) ' PRCNTG_CAP = ',PRCNTG_CAP
WRITE (NDSO,1005) ' THRWSEA = ',THRWSEA
WRITE (NDSO,1006) ' Reading updated SWH from: ',trim(FLNMANL)
! Presently set hardwired THRWSEA energy threshold here
! not user defined in input file
! THRWSEA = 0.7
!
! Array allocation
ALLOCATE ( VATMP(SIZE(VA,1)))
ALLOCATE ( VAMAPWS(SIZE(VA,1)))
IF (.NOT. SMCGRD) THEN
! SWH arrays allocated using Y,X convention as per wgrib write
ALLOCATE( SWHBCKG(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
ALLOCATE( SWHANL(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
! Wind arrays allocated using X,Y convention as in w3idatmd
ALLOCATE( WSBCKG(SIZE(MAPSTA,2), SIZE(MAPSTA,1)) )
ALLOCATE( WDRBCKG(SIZE(MAPSTA,2), SIZE(MAPSTA,1)) )
#ifdef W3_SMC
ELSE
ALLOCATE( SWHBCKG(NSEA,1) )
ALLOCATE( SWHANL(NSEA,1) )
! Use SMCWND to determine if reading a seapoint aray for wind
IF( SMCWND ) THEN
ALLOCATE( WSBCKG(NSEA,1) )
ALLOCATE( WDRBCKG(NSEA,1) )
ELSE
ALLOCATE(WSBCKG(SIZE(MAPSTA,2), SIZE(MAPSTA,1)))
ALLOCATE(WDRBCKG(SIZE(MAPSTA,2), SIZE(MAPSTA,1)))
ENDIF
#endif
ENDIF
#ifdef W3_T
IF (.NOT. SMCGRD) THEN
ALLOCATE( SWHUPRSTR(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
ELSE
ALLOCATE( SWHUPRSTR(NSEA,1) )
ENDIF
#endif
!
! Read additional Input: Analysis Field
INQUIRE(FILE=FLNMANL, EXIST=anl_exists)
IF (anl_exists) THEN
#ifdef W3_T
WRITE (NDSO,*) 'shape(SWHANL)', shape(SWHANL)
#endif
#ifdef W3_WRST
! For WRST switch read only corrected SWH
! Wind will have been read from the restart
IF (WRSTON) THEN
CALL READ_GRBTXT(SWHANL, FLNMANL, SMCGRD)
ELSE
#endif
CALL READ_GRBTXTWS(SWHANL,WSBCKG,WDRBCKG,FLNMANL,SMCGRD)
#ifdef W3_WRST
ENDIF
#endif
#ifdef W3_T
CALL writeMatrix('SWHANL_IN.txt',SWHANL)
#endif
ELSE
WRITE (NDSO,*) trim(FLNMANL), ' does not exist, stopping...'
DEALLOCATE( SWHANL,VATMP,SWHBCKG,VAMAPWS,WSBCKG,WDRBCKG )
#ifdef W3_T
DEALLOCATE( SWHUPRSTR )
#endif
STOP
END IF
!
#ifdef W3_WRST
!Calculate wind speed and direction values from u,v..
!..using cartesian direction convention
!At present assume only needed for data read from restart
CALL UVTOCART(WXNwrst,WYNwrst,WSBCKG,WDRBCKG,SMCWND)
#endif
!
! Calculation
DO ISEA=1, NSEA, 1
IF (.NOT. SMCGRD) THEN
IX = MAPSF(ISEA,1)
IY = MAPSF(ISEA,2)
IXW = IX
IYW = IY
#ifdef W3_SMC
ELSE
IX = 1
IY = ISEA
IF( SMCWND ) THEN
! Wind arrays allocated using (X,Y) convention for regular grids
! but overriding here for the SMC grid which are always defined
! as (NSEA,1) by switching the IY and IX dimension values around
IXW = IY
IYW = IX
ELSE
IXW = MAPSF(ISEA,1)
IYW= MAPSF(ISEA,2)
ENDIF
#endif
ENDIF
VATMP = VA(:,ISEA)
CALL SWH_RSRT_1pw (VATMP, WSBCKG(IXW,IYW), WDRBCKG(IXW,IYW), ISEA, &
SWHBCKG_1, SWHBCKG_W, SWHBCKG_S, VAMAPWS)
SWHBCKG(IY,IX)=SWHBCKG_1
!
IF ( SWHBCKG(IY,IX) > 0.01 .AND. SWHANL(IY,IX) > 0.01 ) THEN
! If wind-sea is dominant energy component apply correction to
! wind-sea part only
IF ( (SWHBCKG_W / SWHBCKG_1)**2.0 > THRWSEA ) THEN
! Apply spectrum updates to wind-sea bins only
PRCNTG=SQRT((SWHANL(IY,IX)**2.0-SWHBCKG_S**2.0)/SWHBCKG_W**2.0)
CALL CHECK_PRCNTG(PRCNTG,PRCNTG_CAP)
CALL UPDTWSPEC(VATMP, PRCNTG, VAMAPWS)
! else correct the whole spectrum as for UPD2
ELSE
PRCNTG=(SWHANL(IY,IX)/SWHBCKG_1)
CALL CHECK_PRCNTG(PRCNTG,PRCNTG_CAP)
CALL UPDATE_VA(PRCNTG,VATMP)
END IF
#ifdef W3_T
WRITE (NDSO,*) 'ISEA = ', ISEA,' IX = ',IX,' IY = ', IY, &
' PRCNTG = ',PRCNTG,' SWHBCKG = ',SWHBCKG(IY,IX), &
' SWHANL = ', SWHANL(IY,IX)
#endif
VA(:,ISEA)=VATMP
#ifdef W3_T
CALL SWH_RSRT_1p (VATMP, ISEA, SWHTMP)
SWHUPRSTR(IY,IX)=SWHTMP
WRITE (NDSO,*) ' =========== UPD5 Output ==========='
WRITE (NDSO,*)'ISEA = ',ISEA,'SWH_BCKG = ', SWHBCKG(IY,IX), &
'SWH_ANL = ', SWHANL(IY,IX), &
'SWH_RSTR = ',SWHUPRSTR(IY,IX)
#endif
END IF
END DO
#ifdef W3_T
CALL writeMatrix('SWHBCKG_UPD5.txt', REAL(SWHBCKG ))
CALL writeMatrix('SWHANL_UPD5.txt' , REAL(SWHANL ))
CALL writeMatrix('SWHRSTR_UPD5.txt', REAL(SWHUPRSTR))
#endif
!
DEALLOCATE( SWHANL,VATMP,SWHBCKG,VAMAPWS,WSBCKG,WDRBCKG )
#ifdef W3_T
DEALLOCATE( SWHUPRSTR )
#endif
!/
!/ ------------------------------------------------------------------- /
! UPD6
! Hybrid of Lionello et al. and Kohno methods
! Corrects wind-sea only in wind dominated conditions - including fp shift
! The fac(x,y,frq,theta), is calculated at each grid point according to
! HsBckg and HsAnl
!/
CASE ('UPD6')
WRITE (NDSO,902) 'UPD6'
WRITE (NDSO,1005) ' PRCNTG_CAP = ',PRCNTG_CAP
WRITE (NDSO,1005) ' THRWSEA = ',THRWSEA
WRITE (NDSO,1006) ' Reading updated SWH from: ',trim(FLNMANL)
! Presently set hardwired CORWSEA logical and THRWSEA energy
! thresholds here, not user defined in input file
CORWSEA = .FALSE.
!THRWSEA = 0.7
!
! Array allocation
ALLOCATE ( VATMP(SIZE(VA,1)))
ALLOCATE ( VAMAPWS(SIZE(VA,1)))
IF (.NOT. SMCGRD) THEN
! SWH arrays allocated using Y,X convention as per wgrib write
ALLOCATE( SWHBCKG(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
ALLOCATE( SWHANL(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
! Wind arrays allocated using X,Y convention as in w3idatmd
ALLOCATE( WSBCKG(SIZE(MAPSTA,2), SIZE(MAPSTA,1)) )
ALLOCATE( WDRBCKG(SIZE(MAPSTA,2), SIZE(MAPSTA,1)) )
#ifdef W3_SMC
ELSE
ALLOCATE( SWHBCKG(NSEA,1) )
ALLOCATE( SWHANL(NSEA,1) )
! Use SMCWND to determine if reading a seapoint aray for wind
IF( SMCWND ) THEN
ALLOCATE( WSBCKG(NSEA,1) )
ALLOCATE( WDRBCKG(NSEA,1) )
ELSE
ALLOCATE(WSBCKG(SIZE(MAPSTA,2), SIZE(MAPSTA,1)))
ALLOCATE(WDRBCKG(SIZE(MAPSTA,2), SIZE(MAPSTA,1)))
ENDIF
#endif
ENDIF
#ifdef W3_T
IF (.NOT. SMCGRD) THEN
ALLOCATE( SWHUPRSTR(SIZE(MAPSTA,1), SIZE(MAPSTA,2)) )
ELSE
ALLOCATE( SWHUPRSTR(NSEA,1) )
ENDIF
#endif
!
! Read additional Input: Analysis Field
INQUIRE(FILE=FLNMANL, EXIST=anl_exists)
IF (anl_exists) THEN
#ifdef W3_T
WRITE (NDSO,*) 'shape(SWHANL)', shape(SWHANL)
#endif
#ifdef W3_WRST