-
Notifications
You must be signed in to change notification settings - Fork 0
/
agioncmd.ftn90
2333 lines (2041 loc) · 112 KB
/
agioncmd.ftn90
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
!/ ------------------------------------------------------------------- /
!/
!/
!/ version | date | description
!/ 1.0 | 01-nov-2006 | netcdf support in wavewatch III 2.22
!/ 1.1 | 03-dec-2009 | netcdf support in wavewatch III 3.04
!/ 1.2 | 01-sep-2012 | netcdf support in SWAN 40.91
!/ 1.3 | 01-may-2013 | overhauled to simplify maintenance
!/ 1.4 | 11-apr-2014 | bug fixes
!/ | reduced memory consumption
!/
!/ copyright 2009-2014 bmtargoss
!/ no unauthorized use without permission.
!/
! 1. purpose :
!
! agioncmd is a module that provides methods to write cf1.5 compliant
! netcdf files containing integrated parameters and spectra.
! Furthermore, some methods are available to read data from cf-1.5 and
! coads compliant files.
!
! For more information:
! http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.5
!
! 2. method :
!
! 3.a.types
! --name-------------type-------description-----------------------
! mapgrid data on a lon / lat grid
! %longitude r.a x-coordinates
! %latitude r.a y-coordinates
! %nx int number of columns
! %ny int number of rows
! %dx real horizontal cellsize
! %dy real vertical cellsize
! %lunit char likely unit (meter, degrees)
! %alpc real angle of the grid
! %mdc bool multi dimensional coordinates
!
! recordaxe derived type with fields
! %content int.a seconds since 1-jan-1970 / run
! %delta int timestep (s) / 1
! %ncontent int number of times / runs
! %nstatm bool non-stationary mode (time /run switch)
! %dimid int netcdf dimension id
! %varid int netcdf variable id
!
! pointgrid data along a point dimension
! %points int 1:npoints
! %longitude r.a x coordinates points
! %latitude r.a y coordinates points
! %lunit char likely unit (meter, degrees)
! %npoints
!
! spcgrid spectral grid
! %frequency r.a frequency axe
! %direction r.a. directional axe
! %nfreq int number of frequencies
! %ndir int number of directions
!
! nctable ncttype struct array from nctablemd.f90
! %standard_name char see remarks a
! %long_name char "
! %units char "
! %nctype integer agnc_short, agnc_byte etc.
! %datamin real minimum expected data value
! %datamax real maximum expected data value
! %varid int nc90_var_id (remark e)
!
! fill_values fvtype struct with fields
! %byte int*1
! %short int*2
! %float real
! %double real*8
!
! 3.a.variables
! ncid int identifier of opened / created file
! pi real 3.1415927
! r2d real 180. / pi
! d2r real pi / 180.
!
! 4.procedures
! ---- create new netcdf files ----
! create_ncfile(ncfile, ncid)
! agnc_define_mapgrid
! agnc_define_pntgrid
! agnc_define_spcgrid
! agnc_define_mapvariable
! agnc_define_pntvariables
! agnc_set_mapgrid(ncid, mapgrid)
! agnc_set_pntgrid(ncid, pntgrid)
! agnc_set_spcgrid(ncid, spcgrid)
! agnc_set_recordaxe (ncid, recordaxe)
! agnc_add_variable_attributes
! agnc_add_coordinates_attribute
! ------------- or ---------------
! open_ncfile
! --------- get grids -------------
! agnc_get_mapgrid
! agnc_get_pntgrid
! agnc_get_spcgrid
! agnc_get_recordaxe
! ------ insert/append data ------
! agnc_add_mapdata
! agnc_add_pntdata
! agnc_add_spcdata
! agnc_add_density
! ------------- utils -------------
! coordinate_names_units
! agnc_get_varid_by_name
! close_ncfile
! nccheck
! axe_is_regular
! compute_1d_spectra
!
! 5. used by :
!
! ww3_outnc.ftn (wavewatch)
! swoutp (swan)
!
! 6. exit codes
!
! 7. remarks :
! a. http://cf-pcmdi.llnl.gov/documents/cf-standard-names/
! b. int16 = round((data - offset) / scale)
! scale = (datamax - datamin) / (2^n -2)
! offset = (datamax - datamin)/2
! c. officially, netcdf apps should un-/pack data on request
! however, i didn't feel like writing the interface on
! http://www.lahey.com/lookat90.htm, interface to deal with
! the different data storage types.
! d. this module is fortran 95 due to the usage of allocatable
! arrays in derived data type (eg, structs)
! e. set when opening an existing file or creation of a variable in
! a new file
!
module agioncmd
use netcdf
use nctablemd
implicit none
real, parameter :: AGIONCMD_VERSION = 1.4
real, parameter :: AGNC_DUMMY = NF90_FILL_FLOAT
integer(kind=1), parameter :: AGNC_FILL_BYTE = -2**7
integer(kind=2), parameter :: AGNC_FILL_SHORT= -2**15
type mapgrid_type
real, dimension(:,:), allocatable :: longitude, latitude
character(len=10) :: lunit = 'degrees'
integer :: nx = 0, ny = 0, &
lon_dimid, lat_dimid, &
lon_varid, lat_varid
real :: dx, dy, alpc = 0.
logical :: mdc = .false.
end type mapgrid_type
type pntgrid_type
real, dimension(:), allocatable :: longitude, latitude
integer, dimension(:), allocatable :: ips
character(len=10) :: lunit = 'degrees'
integer :: pnt_dimid, &
lon_varid, lat_varid, &
npoints = 0, ips_varid = 0, &
xdimlen = 0, ydimlen = 0
end type pntgrid_type
type spcgrid_type
real, dimension(:), allocatable :: frequency, direction
logical :: relative
integer :: frq_dimid, frq_varid, &
dir_dimid, dir_varid, &
nfreq = 0, ndir = 0
end type spcgrid_type
type recordaxe_type
integer*8, dimension(:), allocatable :: content
integer :: delta
integer :: ncontent = 0, dimid, varid
logical :: nstatm = .true.
end type recordaxe_type
! Given the dimension size and/or type, select proper procedure/function
interface agnc_get_griddef
module procedure agnc_get_griddef_spc, agnc_get_griddef_map, &
agnc_get_griddef_pnt
end interface agnc_get_griddef
interface axe_is_regular
module procedure axe_is_regular_integer, &
axe_is_regular_integer64, &
axe_is_regular_float, &
axe_is_regular_float_2d, &
axe_is_regular_double
end interface axe_is_regular
interface agnc_add_mapdata
module procedure agnc_add_mapdata_float, agnc_add_mapdata_double
end interface agnc_add_mapdata
interface agnc_add_spcdata
module procedure agnc_add_spcdata_3d, agnc_add_spcdata_2d
end interface agnc_add_spcdata
interface agnc_add_pntdata
module procedure agnc_add_pntdata1d_float, agnc_add_pntdata2d_float, &
agnc_add_pntdata3d_float, &
agnc_add_pntdata1d_double, agnc_add_pntdata2d_double, &
agnc_add_pntdata3d_double
end interface agnc_add_pntdata
interface seconds_since_epoch
module procedure seconds_since_epoch_datevec, seconds_since_epoch_twoint
end interface seconds_since_epoch
interface datevec
module procedure datevec_from_twoint, datevec_from_epoch
end interface datevec
interface timeindex
module procedure timeindex32, timeindex64
end interface timeindex
contains
!
! ---- create new netcdf files ----
! name
! agnc_define_mapgrid
! agnc_define_pntgrid
! agnc_define_spcgrid
! agnc_define_mapvariable
! agnc_define_pntvariables
! agnc_set_mapgrid(ncid, mapgrid)
! agnc_set_pntgrid(ncid, pntgrid)
! agnc_set_spcgrid(ncid, spcgrid)
! agnc_set_recordaxe (ncid, recordaxe)
subroutine create_ncfile( ncfile, ncid, recordaxe, mapgrid, pntgrid, spcgrid, Escale, nautical )
character(len=*), intent(in) :: ncfile
integer, intent(out) :: ncid
type(recordaxe_type), intent(inout) :: recordaxe
type(mapgrid_type), intent(inout), optional :: mapgrid
type(spcgrid_type), intent(inout), optional :: spcgrid
type(pntgrid_type), intent(inout), optional :: pntgrid
logical ,intent( in), optional :: Escale
logical ,intent( in), optional :: nautical
character(len=100) :: history, dconv
logical :: do_scale
dconv = 'nautical'
if ( present(nautical) ) then
if ( .not.nautical ) then
dconv = 'cartesian'
! nctablemd::nautical defaults to true
call set_nctable_convention_nautical(.false.)
end if
end if
do_scale = .true.
if ( present(Escale) ) then
do_scale = Escale
end if
write(history,'(a,f3.1)') 'Created with agioncmd version ', AGIONCMD_VERSION
call nccheck ( nf90_create( ncfile, NF90_NOCLOBBER + NF90_64BIT_OFFSET, ncid) )
if ( recordaxe%nstatm ) then
call nccheck ( nf90_def_dim( ncid, 'time', NF90_UNLIMITED, recordaxe%dimid ) );
call nccheck ( nf90_def_var( ncid, 'time', NF90_INT, recordaxe%dimid, recordaxe%varid ) )
call nccheck ( nf90_put_att( ncid, recordaxe%varid, 'units', 'seconds since 1970-01-01') )
call nccheck ( nf90_put_att( ncid, recordaxe%varid, 'calendar', 'gregorian') )
call nccheck ( nf90_put_att( ncid, recordaxe%varid, 'standard_name', 'time') )
call nccheck ( nf90_put_att( ncid, recordaxe%varid, 'long_name', 'time') )
else
call nccheck ( nf90_def_dim( ncid, 'run', NF90_UNLIMITED, recordaxe%dimid ) );
call nccheck ( nf90_def_var( ncid, 'run', NF90_INT, recordaxe%dimid, recordaxe%varid ) )
call nccheck ( nf90_put_att( ncid, recordaxe%varid, 'long_name', 'run number') )
call nccheck ( nf90_put_att( ncid, recordaxe%varid, 'units', '1' ) )
end if
! global attributes
call nccheck ( nf90_put_att( ncid, NF90_GLOBAL, 'Conventions', 'CF-1.5') )
call nccheck ( nf90_put_att( ncid, NF90_GLOBAL, 'History', history) )
call nccheck ( nf90_put_att( ncid, NF90_GLOBAL, 'Directional_convention', dconv) )
if ( present(mapgrid) ) then
call agnc_define_mapgrid(ncid, mapgrid)
if ( present(spcgrid) ) call agnc_define_spcgrid(ncid, spcgrid, do_scale, dconv, mapgrid=mapgrid)
else
if ( present(pntgrid) ) then
call agnc_define_pntgrid(ncid, pntgrid)
if ( present(spcgrid) ) call agnc_define_spcgrid(ncid, spcgrid, do_scale, dconv, pntgrid=pntgrid)
else
call agnc_set_error('Provide a point grid or a map grid to create_ncfile')
end if
end if
! reserve extra space in the header for adding attributes
! later without reorganizing the file structure. 4kb should be
! enough for most attributes and negligible compared to the
! total file size.
call nccheck( nf90_enddef(ncid, h_minfree=4096) )
! Set the file back to definition mode so variables can be defined later on
call nccheck( nf90_redef(ncid) )
end subroutine create_ncfile
subroutine coordinate_names_units( lunit, xname, xunit, yname, yunit )
character(len=10), intent( in):: lunit
character(len=20), intent( out):: xname, xunit
character(len=20), intent( out):: yname, yunit
if ( trim(lunit) == 'degrees') then
xname = 'longitude'
xunit = 'degrees_east'
yname = 'latitude'
yunit = 'degrees_north'
else
xname = 'x'
xunit = lunit
yname = 'y'
yunit = lunit
end if
end subroutine coordinate_names_units
subroutine agnc_define_mapgrid( ncid, mapgrid )
integer, intent( in) :: ncid
type (mapgrid_type),intent( inout) :: mapgrid
character(len=20) :: xname, xunit
character(len=20) :: yname, yunit
call coordinate_names_units( mapgrid%lunit, xname, xunit, yname, yunit )
if ( mapgrid%mdc ) then
call nccheck ( nf90_def_dim( ncid, 'xc', mapgrid%nx, mapgrid%lon_dimid ) );
call nccheck ( nf90_def_dim( ncid, 'yc', mapgrid%ny, mapgrid%lat_dimid ) );
call nccheck ( nf90_def_var( ncid, xname, NF90_FLOAT, &
(/ mapgrid%lon_dimid, mapgrid%lat_dimid /), &
mapgrid%lon_varid ) )
call nccheck ( nf90_def_var( ncid, yname, NF90_FLOAT, &
(/ mapgrid%lon_dimid, mapgrid%lat_dimid /), &
mapgrid%lat_varid ) )
call nccheck ( nf90_put_att( ncid, mapgrid%lon_varid, '_FillValue', NF90_FILL_FLOAT) )
call nccheck ( nf90_put_att( ncid, mapgrid%lat_varid, '_FillValue', NF90_FILL_FLOAT) )
else
call nccheck ( nf90_def_dim( ncid, xname, mapgrid%nx, mapgrid%lon_dimid ) );
call nccheck ( nf90_def_var( ncid, xname, NF90_FLOAT, mapgrid%lon_dimid, mapgrid%lon_varid ) );
call nccheck ( nf90_def_dim( ncid, yname, mapgrid%ny, mapgrid%lat_dimid ) );
call nccheck ( nf90_def_var( ncid, yname, NF90_FLOAT, mapgrid%lat_dimid, mapgrid%lat_varid ) );
end if
call nccheck ( nf90_put_att( ncid, mapgrid%lon_varid, 'units', xunit) )
call nccheck ( nf90_put_att( ncid, mapgrid%lon_varid, 'long_name', xname) )
if ( trim(xname) == 'longitude' ) then
call nccheck ( nf90_put_att( ncid, mapgrid%lon_varid, 'standard_name', 'longitude') )
end if
call nccheck ( nf90_put_att( ncid, mapgrid%lat_varid, 'units', yunit) )
call nccheck ( nf90_put_att( ncid, mapgrid%lat_varid, 'long_name', yname) )
if ( trim(yname) == 'latitude' ) then
call nccheck ( nf90_put_att( ncid, mapgrid%lat_varid, 'standard_name', 'latitude') )
end if
end subroutine agnc_define_mapgrid
subroutine agnc_define_spcgrid( ncid, spcgrid, do_scale, dconv, mapgrid, pntgrid )
integer, intent(in ) :: ncid
type(spcgrid_type), intent(inout) :: spcgrid
logical, intent( in) :: do_scale
character(len=100), intent( in) :: dconv
type(mapgrid_type), intent(inout), optional :: mapgrid
type(pntgrid_type), intent(inout), optional :: pntgrid
integer :: evarid, ra_dimid, &
nf90_var_type
logical :: nautical
nautical = .false.
if ( dconv == 'nautical') nautical = .true.
if ( do_scale ) then
nf90_var_type = NF90_SHORT
else
nf90_var_type = NF90_FLOAT
end if
!
! frequency
!
call nccheck ( nf90_def_dim( ncid, 'frequency', spcgrid%nfreq, spcgrid%frq_dimid ) );
call nccheck ( nf90_def_var( ncid, 'frequency', NF90_FLOAT, spcgrid%frq_dimid, spcgrid%frq_varid ) );
call nccheck ( nf90_put_att( ncid, spcgrid%frq_varid, 'units', 's-1') )
call nccheck ( nf90_put_att( ncid, spcgrid%frq_varid, 'standard_name', 'wave_frequency') )
call nccheck ( nf90_put_att( ncid, spcgrid%frq_varid, 'flow', spcgrid%frequency(1) ) )
call nccheck ( nf90_put_att( ncid, spcgrid%frq_varid, 'fhigh', spcgrid%frequency(spcgrid%nfreq) ) )
call nccheck ( nf90_put_att( ncid, spcgrid%frq_varid, 'msc', spcgrid%nfreq-1 ) )
call agnc_get_recordaxe_ids( ncid, ra_dimid )
if ( spcgrid%ndir > 0 ) then
!
! 2D direction axe
!
call nccheck ( nf90_def_dim( ncid, 'direction', spcgrid%ndir, spcgrid%dir_dimid ) )
call nccheck ( nf90_def_var( ncid, 'direction', NF90_FLOAT, spcgrid%dir_dimid, spcgrid%dir_varid ) );
call nccheck ( nf90_put_att( ncid, spcgrid%dir_varid, 'units', 'radians') )
call nccheck ( nf90_put_att( ncid, spcgrid%dir_varid, 'long_name', 'direction') )
if ( nautical ) then
call nccheck ( nf90_put_att( ncid, spcgrid%dir_varid, 'standard_name', 'sea_surface_wave_from_direction') )
else
call nccheck ( nf90_put_att( ncid, spcgrid%dir_varid, 'standard_name', 'sea_surface_wave_to_direction') )
end if
call nccheck ( nf90_put_att( ncid, spcgrid%dir_varid, 'mdc', spcgrid%ndir ) )
!
! 2D density spectrum
!
if ( present(mapgrid) ) then
call nccheck ( nf90_def_var( ncid, 'density', nf90_var_type, &
(/ spcgrid%dir_dimid, spcgrid%frq_dimid, mapgrid%lon_dimid, mapgrid%lat_dimid, ra_dimid /), evarid ) )
else
call nccheck ( nf90_def_var( ncid, 'density', nf90_var_type, &
(/ spcgrid%dir_dimid, spcgrid%frq_dimid, pntgrid%pnt_dimid, ra_dimid /), evarid ) )
end if
if ( do_scale ) then
call nccheck ( nf90_put_att( ncid, evarid, '_FillValue',NF90_FILL_SHORT) )
else
call nccheck ( nf90_put_att( ncid, evarid, '_FillValue',NF90_FILL_FLOAT) )
end if
call nccheck ( nf90_put_att( ncid, evarid, 'long_name', 'density') )
call nccheck ( nf90_put_att( ncid, evarid, 'units', 'm2 s rad-1') )
call nccheck ( nf90_put_att( ncid, evarid, 'standard_name', &
'sea_surface_wave_directional_variance_spectral_density') )
if ( do_scale) then
call nccheck ( nf90_put_att( ncid, evarid, 'note', &
'multiply with scale_density') )
end if
if ( spcgrid%relative ) then
call nccheck ( nf90_put_att( ncid, evarid, 'relative_to_current', 'true') )
else
call nccheck ( nf90_put_att( ncid, evarid, 'relative_to_current', 'false') )
end if
!
! scale_density
!
if ( do_scale ) then
if ( present(mapgrid) ) then
call nccheck ( nf90_def_var( ncid, 'scale_density', NF90_FLOAT, &
(/ mapgrid%lon_dimid, mapgrid%lat_dimid, ra_dimid /), evarid ) )
else
call nccheck ( nf90_def_var( ncid, 'scale_density', NF90_FLOAT, &
(/ pntgrid%pnt_dimid, ra_dimid /), evarid ) )
end if
call nccheck ( nf90_put_att( ncid, evarid, 'note', &
'multiply density values with this scale factor') )
call nccheck ( nf90_put_att( ncid, evarid, 'units', '1') )
end if
else
if ( present(mapgrid) ) then
call nccheck ( nf90_def_var( ncid, 'energy_1d', nf90_var_type, &
(/ spcgrid%frq_dimid, mapgrid%lon_dimid, mapgrid%lat_dimid, ra_dimid /), evarid ) )
else
call nccheck ( nf90_def_var( ncid, 'energy_1d', nf90_var_type, &
(/ spcgrid%frq_dimid, pntgrid%pnt_dimid, ra_dimid /), evarid ) )
end if
if ( do_scale ) then
call nccheck ( nf90_put_att( ncid, evarid, '_FillValue',NF90_FILL_SHORT) )
else
call nccheck ( nf90_put_att( ncid, evarid, '_FillValue',NF90_FILL_FLOAT) )
end if
call nccheck ( nf90_put_att( ncid, evarid, 'long_name', 'energy') )
call nccheck ( nf90_put_att( ncid, evarid, 'units', 'm2 s') )
call nccheck ( nf90_put_att( ncid, evarid, 'standard_name', 'sea_surface_wave_variance_spectral_density') )
if ( spcgrid%relative ) then
call nccheck ( nf90_put_att( ncid, evarid, 'relative_to_current', 'true') )
else
call nccheck ( nf90_put_att( ncid, evarid, 'relative_to_current', 'false') )
end if
call agnc_add_coordinates_attribute(ncid, evarid)
!
! scale_density
!
if ( do_scale ) then
call nccheck ( nf90_put_att( ncid, evarid, 'note', &
'multiply with scale_energy_1d') )
if ( present(mapgrid) ) then
call nccheck ( nf90_def_var( ncid, 'scale_energy_1d', NF90_FLOAT, &
(/ mapgrid%lon_dimid, mapgrid%lat_dimid, ra_dimid /), evarid ) )
else
call nccheck ( nf90_def_var( ncid, 'scale_energy_1d', NF90_FLOAT, &
(/ pntgrid%pnt_dimid, ra_dimid /), evarid ) )
end if
call nccheck ( nf90_put_att( ncid, evarid, 'long_name', &
'Multiply energy_1d values with this scale factor') )
call nccheck ( nf90_put_att( ncid, evarid, 'units', &
'1') )
end if
!
! theta_1d
!
if ( present(mapgrid) ) then
call nccheck ( nf90_def_var( ncid, 'theta_1d', NF90_BYTE, &
(/ spcgrid%frq_dimid, mapgrid%lon_dimid, mapgrid%lat_dimid, ra_dimid /), evarid ) )
else
call nccheck ( nf90_def_var( ncid, 'theta_1d', NF90_BYTE, &
(/ spcgrid%frq_dimid, pntgrid%pnt_dimid, ra_dimid /), evarid ) )
end if
call nccheck ( nf90_put_att( ncid, evarid, 'units', 'degree') )
call nccheck ( nf90_put_att( ncid, evarid, 'long_name', 'principal wave direction') )
call nccheck ( nf90_put_att( ncid, evarid, '_FillValue', AGNC_FILL_BYTE) )
call nccheck ( nf90_put_att( ncid, evarid, 'scale_factor', 360. / (2**8 -2)) )
call nccheck ( nf90_put_att( ncid, evarid, 'add_offset', 360. / 2.) )
call agnc_add_coordinates_attribute(ncid, evarid)
!
! spread_1d
!
if ( present(mapgrid) ) then
call nccheck ( nf90_def_var( ncid, 'spread_1d', NF90_BYTE, &
(/ spcgrid%frq_dimid, mapgrid%lon_dimid, mapgrid%lat_dimid, ra_dimid /), evarid ) )
else
call nccheck ( nf90_def_var( ncid, 'spread_1d', NF90_BYTE, &
(/ spcgrid%frq_dimid, pntgrid%pnt_dimid, ra_dimid /), evarid ) )
end if
call nccheck ( nf90_put_att( ncid, evarid, 'units', 'degree') )
call nccheck ( nf90_put_att( ncid, evarid, 'long_name', &
'Longuet-Higgins short-crestedness parameter (s in cos(theta/2)^2s)') )
call nccheck ( nf90_put_att( ncid, evarid, '_FillValue', AGNC_FILL_BYTE) )
call agnc_add_coordinates_attribute(ncid, evarid)
end if
end subroutine agnc_define_spcgrid
subroutine agnc_define_pntgrid( ncid, pntgrid )
integer, intent(in ) :: ncid
type (pntgrid_type),intent(inout) :: pntgrid
character(len=20) :: xname, xunit
character(len=20) :: yname, yunit
call coordinate_names_units( pntgrid%lunit, xname, xunit, yname, yunit )
call nccheck ( nf90_def_dim( ncid, 'points', pntgrid%npoints, pntgrid%pnt_dimid ) )
call nccheck ( nf90_def_var( ncid, xname, NF90_FLOAT, pntgrid%pnt_dimid, pntgrid%lon_varid ) );
call nccheck ( nf90_put_att( ncid, pntgrid%lon_varid, 'units', xunit) )
call nccheck ( nf90_put_att( ncid, pntgrid%lon_varid, 'long_name', xname) )
if ( trim(xname) == 'longitude' ) then
call nccheck ( nf90_put_att( ncid, pntgrid%lon_varid, 'standard_name', 'longitude') )
end if
call nccheck ( nf90_def_var( ncid, yname, NF90_FLOAT, pntgrid%pnt_dimid, pntgrid%lat_varid ) );
call nccheck ( nf90_put_att( ncid, pntgrid%lat_varid, 'units', yunit) )
call nccheck ( nf90_put_att( ncid, pntgrid%lat_varid, 'long_name', yname) )
if ( trim(yname) == 'latitude' ) then
call nccheck ( nf90_put_att( ncid, pntgrid%lat_varid, 'standard_name', 'latitude') )
end if
! http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.5/ch08s02.html
! states that compression by gathering consists of having a pointlist with one dimensional
! indices in the two dimensional grid (lon, lat). In the example lon and lat are arrays sized
! nx and ny. This code delibertly breaks that rule because it is also used to write unstructured
! data and along a user defined pointlist. It makes more sense to have a longitude / latitude with the
! size of the pointlist.
if ( allocated(pntgrid%ips) ) then
call nccheck ( nf90_put_att( ncid, pntgrid%lon_varid, 'dimlen', pntgrid%xdimlen) )
call nccheck ( nf90_put_att( ncid, pntgrid%lat_varid, 'dimlen', pntgrid%ydimlen) )
call nccheck ( nf90_def_var( ncid, 'ips', NF90_INT, pntgrid%pnt_dimid, pntgrid%ips_varid ) );
call nccheck ( nf90_put_att( ncid, pntgrid%ips_varid, 'units', '1') )
call nccheck ( nf90_put_att( ncid, pntgrid%ips_varid, 'computed_as', '(i - 1) * dimlen1 + j') )
call nccheck ( nf90_put_att( ncid, pntgrid%ips_varid, 'description', 'i is index in first spatial dimension, dimlen1 its length') )
call nccheck ( nf90_put_att( ncid, pntgrid%ips_varid, 'long_name', 'one-dimensional index') )
end if
end subroutine agnc_define_pntgrid
subroutine agnc_define_mapvariable (ncid, varname)
integer, intent( in) :: ncid
character(len=*), intent( in) :: varname
type(nctable_record) :: trecord
integer :: ra_dimid, lon_dimid, lat_dimid,&
varid
call get_nctable_record(varname, trecord)
call agnc_get_recordaxe_ids(ncid, ra_dimid)
call agnc_get_ll_dimids(ncid, lon_dimid, lat_dimid)
call nccheck ( nf90_def_var( ncid, trecord%name , trecord%nctype, &
(/ lon_dimid, lat_dimid, ra_dimid /), varid ) )
call agnc_add_variable_attributes( ncid, varid, trecord )
end subroutine agnc_define_mapvariable
subroutine agnc_define_pntvariable (ncid, varname)
integer, intent( in) :: ncid
character(len=*), intent( in) :: varname
type(nctable_record) :: trecord
integer :: pnt_dimid, ra_dimid, varid
call get_nctable_record(varname, trecord)
call agnc_get_recordaxe_ids(ncid, ra_dimid)
! both unstructured as spectral grid have the "point" dimension
call nccheck ( nf90_inq_dimid(ncid, 'points', pnt_dimid) )
call nccheck ( nf90_def_var( ncid, trecord%name , trecord%nctype, &
(/ pnt_dimid, ra_dimid /), varid ) )
call agnc_add_variable_attributes( ncid, varid, trecord )
end subroutine agnc_define_pntvariable
subroutine agnc_add_variable_attributes( ncid, varid, trecord )
integer, intent(in) :: ncid, varid
type(nctable_record), intent(in) :: trecord
real(kind=4) :: add_offset, scale_factor, rng
integer :: tmpid
character(len=nf90_max_name) :: lonname
call nccheck ( nf90_put_att( ncid, varid, 'units', trecord%units) )
if ( trim(trecord%standard_name) /= 'none' ) &
call nccheck ( nf90_put_att( ncid, varid, 'standard_name', trecord%standard_name) )
call nccheck ( nf90_put_att( ncid, varid, 'long_name', trecord%long_name) )
call agnc_add_coordinates_attribute(ncid, varid)
! http://www.unidata.ucar.edu/software/netcdf/docs/BestPractices.html
!
! In either the signed or unsigned case, an alternate formula may be used for the add_offset
! and scale_factor packing parameters that reserves a packed value for a special value, such
! as an indicator of missing data. For example, to reserve the minimum packed value (-2n - 1)
! for use as a special value in the case of signed packed values:
!
! scale_factor =(dataMax - dataMin) / (2n - 2)
! add_offset = (dataMax + dataMin) / 2
!
! Note that the NF90_FILL_ values are one off in comparison with the formulea used here
!
! if datamin equals datamax in an integer kind of way, do not apply scaling
rng = (trecord%datamax - trecord%datamin)
add_offset = (trecord%datamin + trecord%datamax) * .5
if ( rng /= 0 ) then
select case ( trecord%nctype )
case (NF90_BYTE)
scale_factor = rng/(2**8-2)
case (NF90_SHORT)
scale_factor = rng/(2**16-2)
case default
call agnc_set_error( 'The nctable contains an invalid agnc_TYPE')
end select
call nccheck ( nf90_put_att( ncid, varid, 'scale_factor', scale_factor) )
call nccheck ( nf90_put_att( ncid, varid, 'add_offset', add_offset) )
end if
select case ( trecord%nctype )
case (NF90_BYTE)
call nccheck ( nf90_put_att( ncid, varid, '_FillValue', AGNC_FILL_BYTE) )
case (NF90_SHORT)
call nccheck ( nf90_put_att( ncid, varid, '_FillValue', AGNC_FILL_SHORT) )
case (NF90_FLOAT)
call nccheck ( nf90_put_att( ncid, varid, '_FillValue', NF90_FILL_FLOAT) )
end select
end subroutine agnc_add_variable_attributes
subroutine agnc_add_coordinates_attribute(ncid, varid)
integer, intent(in) :: ncid, varid
integer :: tmpid
! CF-1.2+ prescibes a "coordinates" attribute on multi-dimension fields
if ( nf90_inq_dimid(ncid, 'xc', tmpid) == nf90_noerr ) then
! multi-dimension field because of the hard coded dimension name. The coordinates
! are either in the longitude/latitude or x,y variables
if ( nf90_inq_varid(ncid, 'x', tmpid) == nf90_noerr ) then
call nccheck ( nf90_put_att( ncid, varid, 'coordinates', 'x y') )
else
call nccheck ( nf90_put_att( ncid, varid, 'coordinates', 'longitude latitude') )
end if
end if
end subroutine agnc_add_coordinates_attribute
! ------------- or ---------------
! name status
! open_ncfile( ncfile, 'read/write', &
! recordaxe, monthly ) done
!
subroutine open_ncfile( ncfile, permission, ncid, recordaxe, monthly )
!
! recordaxe, is optional. When permission is "read", the information is read from the file.
! When permission is write, a check is made whether the recordaxe and grid is set in
! the dimension variables in the file.
character(len=*), intent(in) :: ncfile, permission
integer , intent(out) :: ncid
type(recordaxe_type) , optional, intent(inout) :: recordaxe
logical , optional, intent( in) :: monthly
type(recordaxe_type) :: recordaxe_tmp
integer :: oldmode
if ( permission == "read") then
call nccheck( nf90_open(ncfile, nf90_nowrite, ncid) )
else
call nccheck( nf90_open(ncfile, nf90_write, ncid) )
end if
if ( present(recordaxe)) then
call agnc_get_recordaxe(ncid, recordaxe_tmp)
! Check if read time axe is regular and does not only contain _FillValues
if ( axe_is_regular(recordaxe_tmp%content, recordaxe_tmp%ncontent, recordaxe_tmp%delta, .true.) ) then
recordaxe = recordaxe_tmp
else
if ( axe_is_regular(recordaxe%content, recordaxe%ncontent, recordaxe%delta, .true.)) then
if ( present(monthly) ) then
call agnc_set_recordaxe(ncid, recordaxe, monthly)
else
call agnc_set_recordaxe(ncid, recordaxe)
end if
else
! If you're writing a program, you end up here if you didn't provide a
! regular grid. Check that dt and nt are set
call agnc_set_error( 'Could not read valid record axe from file')
end if
end if
end if
end subroutine open_ncfile
!
! --------- set (quasi-) dimension variables -------------
! name
! agnc_set_recordaxe
! agnc_set_mapgrid
! agnc_set_pntgrid
! agnc_set_spcgrid (ncid, spcgrid) done
subroutine agnc_set_recordaxe (ncid, recordaxe, monthly)
! When the optional monthly is set to true, the time axis in the netcdf file
! is predeclared for the month of trecordaxe%content(1)
integer, intent ( in) :: ncid
type (recordaxe_type), intent (inout) :: recordaxe
logical, optional , intent ( in) :: monthly
integer :: t1(6), t2(6), i
if ( recordaxe%varid < 1 ) call agnc_get_recordaxe_ids(ncid, recordaxe%dimid, recordaxe%varid)
! Some sanity checks
if (recordaxe%ncontent == 0) then
call agnc_set_error( 'Cannot set empty record axis')
end if
if ( present(monthly) ) then
if (monthly) then
t1 = datevec(recordaxe%content(1))
t1(3) = 1
t1(4:6) = (/0,0,0/)
t2=t1
t2(2) = t2(2)+1
recordaxe%ncontent = (seconds_since_epoch(t2) - seconds_since_epoch(t1)) / recordaxe%delta
deallocate(recordaxe%content)
allocate(recordaxe%content(recordaxe%ncontent))
recordaxe%content = (/ (i, i=seconds_since_epoch(t1), seconds_since_epoch(t2) - recordaxe%delta, recordaxe%delta) /)
end if
end if
if (.not. axe_is_regular(recordaxe%content, recordaxe%ncontent, recordaxe%delta) ) then
call close_ncfile(ncid)
call agnc_set_error( 'Failed to append data to this file. It would result in a irregular record axis');
end if
call nccheck ( nf90_put_var(ncid, recordaxe%varid, recordaxe%content) )
end subroutine agnc_set_recordaxe
subroutine agnc_set_pntgrid (ncid, pntgrid)
integer, intent ( in) :: ncid
type (pntgrid_type), intent (inout) :: pntgrid
character(len=nf90_max_name) :: name
if ( pntgrid%lon_varid < 1 ) then
call agnc_get_griddef(ncid, pntgrid)
end if
call nccheck ( nf90_put_var(ncid, pntgrid%lon_varid, pntgrid%longitude) )
call nccheck ( nf90_put_var(ncid, pntgrid%lat_varid, pntgrid%latitude) )
if ( pntgrid%ips_varid > 0 ) then
call nccheck ( nf90_put_var(ncid, pntgrid%ips_varid, pntgrid%ips) )
end if
end subroutine agnc_set_pntgrid
subroutine agnc_set_mapgrid (ncid, mapgrid)
integer, intent ( in) :: ncid
type (mapgrid_type), intent (inout) :: mapgrid
character(len=nf90_max_name) :: name
if ( mapgrid%lon_varid < 1 ) then
call agnc_get_griddef(ncid, mapgrid)
end if
call nccheck ( nf90_put_var(ncid, mapgrid%lon_varid, mapgrid%longitude) )
if ( mapgrid%mdc ) then
call nccheck ( nf90_put_var(ncid, mapgrid%lat_varid, mapgrid%latitude) )
else
call nccheck ( nf90_put_var(ncid, mapgrid%lat_varid, &
reshape(mapgrid%latitude, (/mapgrid%ny/)) ))
end if
end subroutine agnc_set_mapgrid
subroutine agnc_set_spcgrid (ncid, spcgrid)
integer, intent ( in) :: ncid
type (spcgrid_type), intent (inout) :: spcgrid
character(len=nf90_max_name) :: name
if ( spcgrid%frq_varid < 1 ) then
call agnc_get_griddef(ncid, spcgrid)
end if
call nccheck ( nf90_put_var(ncid, spcgrid%frq_varid, spcgrid%frequency) )
if (spcgrid%ndir > 0) &
call nccheck ( nf90_put_var(ncid, spcgrid%dir_varid, spcgrid%direction) )
end subroutine agnc_set_spcgrid
! --------- get data -------------
! agnc_get_mapgrid
! agnc_get_pntgrid
! agnc_get_spcgrid
! agnc_get_recordaxe
subroutine agnc_get_mapgrid(ncid, mapgrid)
! Structure:
! find dimension id of dimenstion named longitude, lon or x
! get meta information on this dimension
! assume the name of the dimension corresponds with a variable
! read variable
! set results in mapgrid struct
! check that geographic axes are regular
integer, intent ( in ) :: ncid
type (mapgrid_type), intent (out) :: mapgrid
integer :: ndims
real :: xlen, ylen
real, dimension(:), allocatable :: lattmp
call agnc_get_griddef(ncid, mapgrid)
call nccheck ( nf90_inquire_variable(ncid, mapgrid%lon_varid, ndims=ndims) )
if ( ndims == 1 ) then
allocate(mapgrid%longitude(mapgrid%nx, 1))
allocate(mapgrid%latitude (1,mapgrid%ny))
allocate(lattmp(mapgrid%ny))
call nccheck ( nf90_get_var(ncid, mapgrid%lon_varid, mapgrid%longitude) )
call nccheck ( nf90_get_var(ncid, mapgrid%lat_varid, lattmp) )
mapgrid%latitude(1,:) = lattmp
deallocate(lattmp)
xlen = mapgrid%longitude(mapgrid%nx,1) - mapgrid%longitude(1,1)
ylen = mapgrid%latitude(1,mapgrid%ny) - mapgrid%latitude(1,1)
mapgrid%dx = xlen / ( mapgrid%nx - 1 )
mapgrid%dy = ylen / ( mapgrid%ny - 1 )
mapgrid%alpc = 0.
else
allocate(mapgrid%longitude(mapgrid%nx, mapgrid%ny))
allocate(mapgrid%latitude (mapgrid%nx, mapgrid%ny))
call nccheck ( nf90_get_var(ncid, mapgrid%lat_varid, mapgrid%latitude) )
call nccheck ( nf90_get_var(ncid, mapgrid%lon_varid, mapgrid%longitude) )
xlen = mapgrid%longitude(mapgrid%nx,1) - mapgrid%longitude(1,1)
ylen = mapgrid%latitude(mapgrid%nx,1) - mapgrid%latitude(1,1)
mapgrid%alpc = atan2(xlen, ylen)
! greatcircle??
mapgrid%dx = ( xlen**2 + ylen**2 )**0.5 / ( mapgrid%nx - 1 )
xlen = mapgrid%longitude(1,mapgrid%ny) - mapgrid%longitude(1,1)
ylen = mapgrid%latitude(1,mapgrid%ny) - mapgrid%latitude(1,1)
mapgrid%dy = ( xlen**2 + ylen**2)**0.5 / ( mapgrid%ny - 1 )
end if
end subroutine agnc_get_mapgrid
subroutine agnc_get_pntgrid(ncid, pntgrid)
integer, intent ( in ) :: ncid
type (pntgrid_type), intent (out) :: pntgrid
call agnc_get_griddef(ncid, pntgrid)
allocate(pntgrid%longitude(pntgrid%npoints))
call nccheck ( nf90_get_var(ncid, pntgrid%lon_varid, pntgrid%longitude) );
allocate(pntgrid%latitude(pntgrid%npoints))
call nccheck ( nf90_get_var(ncid, pntgrid%lat_varid, pntgrid%latitude) )
if ( pntgrid%ips_varid /= -1 ) then
allocate(pntgrid%ips(pntgrid%npoints))
call nccheck ( nf90_get_var(ncid, pntgrid%ips_varid, pntgrid%ips) )
end if
end subroutine agnc_get_pntgrid
subroutine agnc_get_spcgrid(ncid, spcgrid)
integer, intent ( in ) :: ncid
type (spcgrid_type), intent (out) :: spcgrid
call agnc_get_griddef(ncid, spcgrid)
if ( spcgrid%frq_varid > 0 ) then
allocate(spcgrid%frequency(spcgrid%nfreq))
call nccheck ( nf90_get_var(ncid, spcgrid%frq_varid, spcgrid%frequency) )
if ( spcgrid%dir_varid > 0 ) then
allocate(spcgrid%direction(spcgrid%ndir))
call nccheck ( nf90_get_var(ncid, spcgrid%dir_varid, spcgrid%direction) )
end if
end if
end subroutine agnc_get_spcgrid
subroutine agnc_get_recordaxe(ncid, recordaxe)
integer, intent ( in ) :: ncid
type (recordaxe_type), intent(out) :: recordaxe
character(len=nf90_max_name) :: units, raname, emsg
integer*8, allocatable, dimension(:) :: dvalues
integer :: factor, dvec(6)
! find time dimid
call agnc_get_recordaxe_ids(ncid, recordaxe%dimid, recordaxe%varid, recordaxe%ncontent)
if ( recordaxe%varid < 0 ) then
call agnc_set_error( 'Could not find record axe variable')
end if
call nccheck ( nf90_get_att(ncid, recordaxe%varid, 'units', units) )
call nccheck ( nf90_inquire_variable(ncid, recordaxe%varid, raname) )
allocate(recordaxe%content(recordaxe%ncontent))
if (raname /= 'run') then
call agnc_scan_time(units, factor, dvec, emsg)
if ( factor < 0 ) call agnc_set_error(emsg)
allocate(dvalues(recordaxe%ncontent))
call nccheck ( nf90_get_var(ncid, recordaxe%varid, dvalues) )
recordaxe%content = nint(dvalues * dble(factor) + dble(seconds_since_epoch(dvec)),8)
deallocate(dvalues)
recordaxe%nstatm = .true.
else
call nccheck ( nf90_get_var(ncid, recordaxe%varid, recordaxe%content) )
recordaxe%nstatm = .false.
end if
if (recordaxe%ncontent > 1) then
recordaxe%delta = recordaxe%content(2) - recordaxe%content(1)
if (.not. axe_is_regular(recordaxe%content, recordaxe%ncontent, recordaxe%delta)) then
call agnc_set_error( 'record axe does not appear to be regular')
end if
else
! You'll have set dt yourself when nt is 1
recordaxe%delta = 0
end if
end subroutine agnc_get_recordaxe
subroutine agnc_get_recordaxe_ids(ncid, dimid, varid, ncontent, raname)
integer, intent( in) :: ncid
integer, intent(out) :: dimid
integer, optional, intent(out) :: varid, ncontent
character(len=nf90_max_name), optional, intent(out) :: raname
character*6 :: dnames(2), dname
integer :: i
logical :: dim_found
dim_found = .false.
dnames = (/ 'time ', 'run ' /)
! Note that the assumption is that the record axe dimension has a correspomding
! variable
do i = 1, 2
if ( nf90_inq_dimid(ncid, dnames(i), dimid) == nf90_noerr ) then
dname = dnames(i)
dim_found = .true.
exit
end if
end do
if ( .not. dim_found ) then
call agnc_set_error( 'record axe could not be found')
end if
if (present(ncontent)) then
call nccheck ( nf90_inquire_dimension(ncid, dimid, dname, ncontent) )
end if
if ( present(varid) ) call agnc_get_varid_by_name(ncid, dname, varid)
if ( present(raname)) raname = dname
end subroutine agnc_get_recordaxe_ids
subroutine agnc_get_ll_dimids(ncid, lon_dimid, lat_dimid)
integer , intent( in) :: ncid
integer , intent(out) :: lon_dimid, lat_dimid
if ( nf90_inq_dimid(ncid, "longitude", lon_dimid) /= nf90_noerr ) then
if ( nf90_inq_dimid(ncid, "x", lon_dimid) /= nf90_noerr ) then
if ( nf90_inq_dimid(ncid, "xc", lon_dimid) /= nf90_noerr ) then
call nccheck ( nf90_inq_dimid(ncid, "lon", lon_dimid) )
end if
end if
end if