forked from DOI-USGS/volcano-ash3d-metreader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
MetReader_NetCDF.F90
4469 lines (4177 loc) · 209 KB
/
MetReader_NetCDF.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
!##############################################################################
!
! MR_Read_Met_DimVars_netcdf
! MR_Get_WRF_grid
! MR_Read_Met_Times_netcdf
! MR_Set_iwind5_filenames
! MR_Set_Met_Dims_Template_netcdf
! MR_Read_MetP_Variable_netcdf
! MR_NC_check_status
! MR_NC_check_var_synonyms
!
!##############################################################################
!
! MR_Read_Met_DimVars_netcdf
!
! Called once from MR_Read_Met_DimVars
!
! This subroutine reads the variable and dimension IDs, and fills the
! coordinate dimension variables
!
! After this subroutine completes, the following variables will be set:
! All the projection parameters of NWP grid
! Met_dim_names, Met_var_GRIB_names, Met_var_conversion_factor,
! Met_var_IsAvailable
! The lengths of all the dimensions of the file
! p_fullmet_sp (converted to Pa)
! x_fullmet_sp, y_fullmet_sp
! IsLatLon_MetGrid, IsGlobal_MetGrid, IsRegular_MetGrid
!
!##############################################################################
subroutine MR_Read_Met_DimVars_netcdf
use MetReader, only : &
MR_nio,VB,outlog,errlog,verbosity_error,verbosity_info,verbosity_production,&
nlevs_fullmet,nlevs_fullmet,levs_code,nlev_coords_detected,levs_fullmet_sp,&
p_fullmet_sp,x_fullmet_sp,y_fullmet_sp,MR_dx_met,MR_dy_met,&
z_approx,dx_met_const,dy_met_const,&
IsGlobal_MetGrid,IsLatLon_MetGrid,IsRegular_MetGrid,MR_EPS_SMALL,MR_iversion,&
MR_iwind,MR_iwindformat,MR_Max_geoH_metP_predicted,MR_MAXVARS,MR_Use_RDA,&
np_fullmet,nt_fullmet,nx_fullmet,ny_fullmet,Pressure_Conv_Fac,x_inverted,&
y_inverted,z_inverted,Met_var_zdim_idx,MR_windfiles,Met_var_IsAvailable,&
Met_var_ndim,Met_var_NC_names,Met_dim_names,Met_var_zdim_ncid,&
temp1d_byte,temp1d_char,temp1d_intS,temp1d_intL,temp1d_sp,temp1d_dp,&
MR_Z_US_StdAtm
use projection, only : &
PJ_Set_Proj_Params
use netcdf
implicit none
integer, parameter :: sp = 4 ! single precision
integer, parameter :: dp = 8 ! double precision
integer :: i, k
real(kind=sp) :: xLL_fullmet
real(kind=sp) :: yLL_fullmet
real(kind=sp) :: xUR_fullmet
real(kind=sp) :: yUR_fullmet
integer :: ncid
integer :: ivar,in_var_id,var_ndims
integer :: i_dim,iivar
integer :: xdim_id, ydim_id, tdim_id
integer :: dimlen,maxdimlen
integer :: nSTAT
character(len=NF90_MAX_NAME) :: invar,dimname
integer :: var_xtype,var_id,idx
integer :: xtype, length, attnum
real(kind=sp):: dum_sp
real(kind=dp), parameter :: tol = 1.0e-3_dp
real(kind=dp) :: x_start,y_start
integer,dimension(:),allocatable :: var_dimIDs
logical :: FoundOldDim
logical :: IsPressureDimension
real(kind=sp) :: var_scale_fac
real(kind=sp) :: var_offset
character(len=31) :: ustring
logical :: IsTruncatedDim
character(len=130) :: infile
integer :: io ! Index for output streams
INTERFACE
subroutine MR_NC_check_status(nSTAT, errcode, operation)
integer, intent(in) :: nSTAT
integer, intent(in) :: errcode
character(len=*), intent(in) :: operation
end subroutine MR_NC_check_status
subroutine MR_NC_check_var_synonyms(ivar,ncid)
integer, intent(in) :: ivar
integer, intent(in) :: ncid
end subroutine MR_NC_check_var_synonyms
subroutine MR_Get_WRF_grid
end subroutine MR_Get_WRF_grid
subroutine MR_Set_Met_Dims_Template_netcdf
end subroutine MR_Set_Met_Dims_Template_netcdf
END INTERFACE
do io=1,MR_nio;if(VB(io).le.verbosity_production)then
write(outlog(io),*)"-----------------------------------------------------------------------"
write(outlog(io),*)"---------- MR_Read_Met_DimVars_netcdf ----------"
write(outlog(io),*)"-----------------------------------------------------------------------"
endif;enddo
if(MR_iwind.eq.5)then
! For the case where variables are in different files, we will just hard-code the
! grid geometry
if (MR_iwindformat.eq.25) then
! NCEP/NCAR reanalysis 2.5 degree files
! https://rda.ucar.edu/datasets/ds090.0
maxdimlen = 17
nlev_coords_detected = 3
allocate(nlevs_fullmet(nlev_coords_detected));nlevs_fullmet(:)=0
nlevs_fullmet(1) = 17
nlevs_fullmet(2) = 12
nlevs_fullmet(3) = 8
allocate(levs_code(nlev_coords_detected));levs_code(:)=0
levs_code(1) = 1
levs_code(2) = 2
levs_code(3) = 2
allocate(levs_fullmet_sp(nlev_coords_detected,maxdimlen));levs_fullmet_sp(:,:)=0
np_fullmet = 17
allocate(p_fullmet_sp(np_fullmet))
levs_fullmet_sp(:,:) = 0.0_sp
p_fullmet_sp(1:np_fullmet) = &
(/1000.0_sp, 925.0_sp, 850.0_sp, 700.0_sp, 600.0_sp, &
500.0_sp, 400.0_sp, 300.0_sp, 250.0_sp, 200.0_sp, &
150.0_sp, 100.0_sp, 70.0_sp, 50.0_sp, 30.0_sp, &
20.0_sp, 10.0_sp /)
z_inverted = .false.
levs_fullmet_sp(1,1:17) = p_fullmet_sp(1:17)*Pressure_Conv_Fac
levs_fullmet_sp(2,1:12) = p_fullmet_sp(1:12)*Pressure_Conv_Fac
levs_fullmet_sp(3,1:8 ) = p_fullmet_sp(1:8 )*Pressure_Conv_Fac
Met_var_zdim_idx( 1) = 1
Met_var_zdim_idx( 2) = 1
Met_var_zdim_idx( 3) = 1
Met_var_zdim_idx( 4) = 2
Met_var_zdim_idx( 5) = 1
Met_var_zdim_idx( 7) = 2
Met_var_zdim_idx(30) = 3
IsLatLon_MetGrid = .true.
IsGlobal_MetGrid = .true.
IsRegular_MetGrid = .true.
nx_fullmet = 144
ny_fullmet = 73
allocate(x_fullmet_sp(0:nx_fullmet+1))
allocate(y_fullmet_sp(ny_fullmet))
allocate(MR_dx_met(nx_fullmet))
allocate(MR_dy_met(ny_fullmet))
dx_met_const = 2.5_sp
dy_met_const = 2.5_sp
x_start = 0.0_dp
y_start = 90.0_dp
do i = 0,nx_fullmet+1
x_fullmet_sp(i) = real(x_start + (i-1)*dx_met_const,kind=sp)
enddo
x_inverted = .false.
do i = 1,ny_fullmet
y_fullmet_sp(i) = real(y_start - (i-1)*dy_met_const,kind=sp)
enddo
y_inverted = .true.
do i = 1,nx_fullmet
MR_dx_met(i) = x_fullmet_sp(i+1)-x_fullmet_sp(i)
enddo
do i = 1,ny_fullmet-1
MR_dy_met(i) = y_fullmet_sp(i+1)-y_fullmet_sp(i)
enddo
MR_dy_met(ny_fullmet) = MR_dy_met(ny_fullmet-1)
elseif(MR_iwindformat.eq.26)then
! JRA-55 reanalysis 1.25 degree files
! https://rda.ucar.edu/datasets/ds628.0/
maxdimlen = 37
nlev_coords_detected = 2
allocate(nlevs_fullmet(nlev_coords_detected))
nlevs_fullmet(1) = 37
nlevs_fullmet(2) = 27
allocate(levs_code(nlev_coords_detected))
levs_code(1) = 1
levs_code(2) = 2
allocate(levs_fullmet_sp(nlev_coords_detected,maxdimlen))
np_fullmet = 37
allocate(p_fullmet_sp(np_fullmet))
levs_fullmet_sp(:,:) = 0.0_sp
p_fullmet_sp(1:np_fullmet) = &
(/1000.0_sp, 975.0_sp, 950.0_sp, 925.0_sp, 900.0_sp, &
875.0_sp, 850.0_sp, 825.0_sp, 800.0_sp, 775.0_sp, &
750.0_sp, 700.0_sp, 650.0_sp, 600.0_sp, 550.0_sp, &
500.0_sp, 450.0_sp, 400.0_sp, 350.0_sp, 300.0_sp, &
250.0_sp, 225.0_sp, 200.0_sp, 175.0_sp, 150.0_sp, &
125.0_sp, 100.0_sp, 70.0_sp, 50.0_sp, 30.0_sp, &
20.0_sp, 10.0_sp, 7.0_sp, 5.0_sp, 3.0_sp, &
2.0_sp, 1.0_sp /)
z_inverted = .true.
levs_fullmet_sp(1,1:37) = p_fullmet_sp(1:37)*Pressure_Conv_Fac
levs_fullmet_sp(2,1:27) = p_fullmet_sp(1:27)*Pressure_Conv_Fac
Met_var_zdim_idx( 1) = 1
Met_var_zdim_idx( 2) = 1
Met_var_zdim_idx( 3) = 1
Met_var_zdim_idx( 4) = 1
Met_var_zdim_idx( 5) = 1
Met_var_zdim_idx( 7) = 1
Met_var_zdim_idx(30) = 2
IsLatLon_MetGrid = .true.
IsGlobal_MetGrid = .true.
IsRegular_MetGrid = .true.
nx_fullmet = 288
ny_fullmet = 145
allocate(x_fullmet_sp(0:nx_fullmet+1))
allocate(y_fullmet_sp(ny_fullmet))
allocate(MR_dx_met(nx_fullmet))
allocate(MR_dy_met(ny_fullmet))
dx_met_const = 1.25_sp
dy_met_const = 1.25_sp
x_start = 0.0_dp
y_start = 90.0_dp
do i = 0,nx_fullmet+1
x_fullmet_sp(i) = real(x_start + (i-1)*dx_met_const,kind=sp)
enddo
x_inverted = .false.
do i = 1,ny_fullmet
y_fullmet_sp(i) = real(y_start - (i-1)*dy_met_const,kind=sp)
enddo
y_inverted = .true.
do i = 1,nx_fullmet
MR_dx_met(i) = x_fullmet_sp(i+1)-x_fullmet_sp(i)
enddo
do i = 1,ny_fullmet-1
MR_dy_met(i) = y_fullmet_sp(i+1)-y_fullmet_sp(i)
enddo
MR_dy_met(ny_fullmet) = MR_dy_met(ny_fullmet-1)
elseif(MR_iwindformat.eq.27)then
if(MR_iversion.eq.2)then
! NOAA-CIRES reanalysis 2.0 degree files :: ds131.2
! Note that v.2 is an older version.
! Since v.2 has a regular grid, we just specify it here
maxdimlen = 24
nlev_coords_detected = 2
allocate(nlevs_fullmet(nlev_coords_detected))
nlevs_fullmet(1) = 24
nlevs_fullmet(2) = 19
allocate(levs_code(nlev_coords_detected))
levs_code(1) = 1
levs_code(2) = 2
allocate(levs_fullmet_sp(nlev_coords_detected,maxdimlen))
np_fullmet = 24
allocate(p_fullmet_sp(np_fullmet))
levs_fullmet_sp(:,:) = 0.0_sp
p_fullmet_sp(1:np_fullmet) = &
(/1000.0_sp, 950.0_sp, 900.0_sp, 850.0_sp, 800.0_sp, &
750.0_sp, 700.0_sp, 650.0_sp, 600.0_sp, 550.0_sp, &
500.0_sp, 450.0_sp, 400.0_sp, 350.0_sp, 300.0_sp, &
250.0_sp, 200.0_sp, 150.0_sp, 100.0_sp, 70.0_sp, &
50.0_sp, 30.0_sp, 20.0_sp, 10.0_sp /)
if(MR_Use_RDA)then
! Files from rda.ucar.edu/datasets/ds131.2/ converted from GRIB with ncl_convert2nc
! are top down
z_inverted = .true.
else
! Files from www.esrl.noaa.gov are bottom up
z_inverted = .false.
endif
levs_fullmet_sp(1,1:24) = p_fullmet_sp(1:24)*Pressure_Conv_Fac
levs_fullmet_sp(2,1:19) = p_fullmet_sp(1:19)*Pressure_Conv_Fac
Met_var_zdim_idx( 1) = 1
Met_var_zdim_idx( 2) = 1
Met_var_zdim_idx( 3) = 1
Met_var_zdim_idx( 4) = 2
Met_var_zdim_idx( 5) = 1
Met_var_zdim_idx( 7) = 2
IsLatLon_MetGrid = .true.
IsGlobal_MetGrid = .true.
IsRegular_MetGrid = .true.
nx_fullmet = 180
ny_fullmet = 91
allocate(x_fullmet_sp(0:nx_fullmet+1))
allocate(y_fullmet_sp(ny_fullmet))
allocate(MR_dx_met(nx_fullmet))
allocate(MR_dy_met(ny_fullmet))
dx_met_const = 2.0_sp
dy_met_const = 2.0_sp
x_start = 0.0_dp
y_start = 90.0_dp
do i = 0,nx_fullmet+1
x_fullmet_sp(i) = real(x_start + (i-1)*dx_met_const,kind=sp)
enddo
x_inverted = .false.
do i = 1,ny_fullmet
y_fullmet_sp(i) = real(y_start - (i-1)*dy_met_const,kind=sp)
enddo
y_inverted = .true.
do i = 1,nx_fullmet
MR_dx_met(i) = x_fullmet_sp(i+1)-x_fullmet_sp(i)
enddo
do i = 1,ny_fullmet-1
MR_dy_met(i) = y_fullmet_sp(i+1)-y_fullmet_sp(i)
enddo
MR_dy_met(ny_fullmet) = MR_dy_met(ny_fullmet-1)
elseif(MR_iversion.eq.3)then
! NOAA-CIRES reanalysis :: ds131.3
! In v.3, we have a Gaussian grid so we read in the values in
! MR_Read_Met_Times_netcdf
maxdimlen = 28
nlev_coords_detected = 2
allocate(nlevs_fullmet(nlev_coords_detected))
nlevs_fullmet(1) = 28
nlevs_fullmet(2) = 21
allocate(levs_code(nlev_coords_detected))
levs_code(1) = 1
levs_code(2) = 2
allocate(levs_fullmet_sp(nlev_coords_detected,maxdimlen))
np_fullmet = 28
allocate(p_fullmet_sp(np_fullmet))
levs_fullmet_sp(:,:) = 0.0_sp
p_fullmet_sp(1:np_fullmet) = &
(/1000.0_sp, 975.0_sp, 950.0_sp, 925.0_sp, 900.0_sp, &
850.0_sp, 800.0_sp, 750.0_sp, 700.0_sp, 650.0_sp, &
600.0_sp, 550.0_sp, 500.0_sp, 450.0_sp, 400.0_sp, &
350.0_sp, 300.0_sp, 250.0_sp, 200.0_sp, 150.0_sp, &
100.0_sp, 70.0_sp, 50.0_sp, 30.0_sp, 20.0_sp, &
10.0_sp, 5.0_sp, 1.0_sp /)
! Files from www.esrl.noaa.gov are bottom up
z_inverted = .false.
levs_fullmet_sp(1,1:28) = p_fullmet_sp(1:28)*Pressure_Conv_Fac
levs_fullmet_sp(2,1:21) = p_fullmet_sp(1:21)*Pressure_Conv_Fac
Met_var_zdim_idx( 1) = 1
Met_var_zdim_idx( 2) = 1
Met_var_zdim_idx( 3) = 1
Met_var_zdim_idx( 4) = 2
Met_var_zdim_idx( 5) = 1
Met_var_zdim_idx( 7) = 2
IsLatLon_MetGrid = .true.
IsGlobal_MetGrid = .true.
IsRegular_MetGrid = .false.
allocate(MR_dx_met(nx_fullmet))
allocate(MR_dy_met(ny_fullmet))
do i = 1,nx_fullmet
MR_dx_met(i) = x_fullmet_sp(i+1)-x_fullmet_sp(i)
enddo
do i = 1,ny_fullmet-1
MR_dy_met(i) = y_fullmet_sp(i+1)-y_fullmet_sp(i)
enddo
MR_dy_met(ny_fullmet) = MR_dy_met(ny_fullmet-1)
endif
elseif(MR_iwindformat.eq.29)then
! ECMWF ERA5
! RDA provides the data as 1-variable/file so iwf=5
! https://rda.ucar.edu/datasets/ds633.0
! If data are from CDS, use iwf=4
maxdimlen = 37
nlev_coords_detected = 1
allocate(nlevs_fullmet(nlev_coords_detected))
nlevs_fullmet(1) = 37
allocate(levs_code(nlev_coords_detected))
levs_code(1) = 1
allocate(levs_fullmet_sp(nlev_coords_detected,maxdimlen))
np_fullmet = 37
allocate(p_fullmet_sp(np_fullmet))
levs_fullmet_sp(:,:) = 0.0_sp
p_fullmet_sp(1:np_fullmet) = &
(/1000.0_sp, 975.0_sp, 950.0_sp, 925.0_sp, 900.0_sp, &
875.0_sp, 850.0_sp, 825.0_sp, 800.0_sp, 775.0_sp, &
750.0_sp, 700.0_sp, 650.0_sp, 600.0_sp, 550.0_sp, &
500.0_sp, 450.0_sp, 400.0_sp, 350.0_sp, 300.0_sp, &
250.0_sp, 225.0_sp, 200.0_sp, 175.0_sp, 150.0_sp, &
125.0_sp, 100.0_sp, 70.0_sp, 50.0_sp, 30.0_sp, &
20.0_sp, 10.0_sp, 7.0_sp, 5.0_sp, 3.0_sp, &
2.0_sp, 1.0_sp /)
z_inverted = .true.
levs_fullmet_sp(1,1:37) = p_fullmet_sp(1:37)*Pressure_Conv_Fac
Met_var_zdim_idx( 1) = 1
Met_var_zdim_idx( 2) = 1
Met_var_zdim_idx( 3) = 1
Met_var_zdim_idx( 4) = 1
Met_var_zdim_idx( 5) = 1
Met_var_zdim_idx( 7) = 1
IsLatLon_MetGrid = .true.
IsGlobal_MetGrid = .true.
IsRegular_MetGrid = .false.
allocate(MR_dx_met(nx_fullmet))
allocate(MR_dy_met(ny_fullmet))
do i = 1,nx_fullmet
MR_dx_met(i) = x_fullmet_sp(i+1)-x_fullmet_sp(i)
enddo
do i = 1,ny_fullmet-1
MR_dy_met(i) = y_fullmet_sp(i+1)-y_fullmet_sp(i)
enddo
MR_dy_met(ny_fullmet) = MR_dy_met(ny_fullmet-1)
elseif(MR_iwindformat.eq.30)then
! ECMWF ERA-20c
maxdimlen = 37
nlev_coords_detected = 1
allocate(nlevs_fullmet(nlev_coords_detected))
nlevs_fullmet(1) = 37
allocate(levs_code(nlev_coords_detected))
levs_code(1) = 1
allocate(levs_fullmet_sp(nlev_coords_detected,maxdimlen))
np_fullmet = 37
allocate(p_fullmet_sp(np_fullmet))
levs_fullmet_sp(:,:) = 0.0_sp
p_fullmet_sp(1:np_fullmet) = &
(/1000.0_sp, 975.0_sp, 950.0_sp, 925.0_sp, 900.0_sp, &
875.0_sp, 850.0_sp, 825.0_sp, 800.0_sp, 775.0_sp, &
750.0_sp, 700.0_sp, 650.0_sp, 600.0_sp, 550.0_sp, &
500.0_sp, 450.0_sp, 400.0_sp, 350.0_sp, 300.0_sp, &
250.0_sp, 225.0_sp, 200.0_sp, 175.0_sp, 150.0_sp, &
125.0_sp, 100.0_sp, 70.0_sp, 50.0_sp, 30.0_sp, &
20.0_sp, 10.0_sp, 7.0_sp, 5.0_sp, 3.0_sp, &
2.0_sp, 1.0_sp /)
z_inverted = .true.
levs_fullmet_sp(1,1:37) = p_fullmet_sp(1:37)*Pressure_Conv_Fac
Met_var_zdim_idx( 1) = 1
Met_var_zdim_idx( 2) = 1
Met_var_zdim_idx( 3) = 1
Met_var_zdim_idx( 4) = 1
Met_var_zdim_idx( 5) = 1
Met_var_zdim_idx( 7) = 1
IsLatLon_MetGrid = .true.
IsGlobal_MetGrid = .true.
IsRegular_MetGrid = .false.
allocate(MR_dx_met(nx_fullmet))
allocate(MR_dy_met(ny_fullmet))
do i = 1,nx_fullmet
MR_dx_met(i) = x_fullmet_sp(i+1)-x_fullmet_sp(i)
enddo
do i = 1,ny_fullmet-1
MR_dy_met(i) = y_fullmet_sp(i+1)-y_fullmet_sp(i)
enddo
MR_dy_met(ny_fullmet) = MR_dy_met(ny_fullmet-1)
else
stop 1
endif
idx = Met_var_zdim_idx(1)
p_fullmet_sp(1:nlevs_fullmet(idx)) = levs_fullmet_sp(idx,1:nlevs_fullmet(idx))
! This is where we should put the check on variable names, but for iwind=5, we
! need to open each file for each variable individually
else ! MR_iwind not equal to 5
!--------------------------------------------------------------------------
! Start of all non-iwind=5 cases
if(MR_iwindformat.eq.50)then
! WRF files have a special reader, but we still need to set up
call MR_Get_WRF_grid
else ! MR_iwindformat .ne. 50
!------------------------------------------------------------------------
! Start of block for all non-iwind=5 and non-iwf=50
! This is where the Netcdf and Grib subroutines can be compared
!
! Checking for dimension length and values for x,y,t,p
! Assume all files have the same format so we just work with MR_windfiles(1)
maxdimlen = 0
infile = trim(adjustl(MR_windfiles(1)))
nSTAT=nf90_open(trim(adjustl(infile)),NF90_NOWRITE, ncid)
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"nf90_open MR_windfiles(1)")
do ivar = 1,MR_MAXVARS
if(.not.Met_var_IsAvailable(ivar)) cycle ! Only look at variables that are available
if(Met_var_ndim(ivar).ne.4) cycle ! and only ones with a 'level' dimension (i.e. with 4 dimensions)
invar = Met_var_NC_names(ivar)
nSTAT = nf90_inq_varid(ncid,invar,in_var_id) ! get the var_id for this named variable
if(nSTAT.ne.NF90_NOERR)then
call MR_NC_check_status(nSTAT,0,"inq_varid")
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)' Cannot find variable ',trim(adjustl(invar))
write(outlog(io),*)' Testing for known synonyms'
endif;enddo
! Checking other name options.
call MR_NC_check_var_synonyms(ivar,ncid)
! Now try again
invar = Met_var_NC_names(ivar)
nSTAT = nf90_inq_varid(ncid,invar,in_var_id) ! get the var_id for this named variable
if(nSTAT.ne.NF90_NOERR)then
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)' Cannot find variable ',trim(adjustl(invar))
endif;enddo
Met_var_IsAvailable(ivar) = .false.
cycle
endif
endif
nSTAT = nf90_inquire_variable(ncid, in_var_id, invar, &
xtype = var_xtype, &
ndims = var_ndims) ! get the number of dimensions
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"inq_variable")
if(var_ndims.ne.Met_var_ndim(ivar))then
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR ERROR: The actual number of dimensions differs from'
write(errlog(io),*)' what is expected'
write(errlog(io),*)' Variable : ',ivar,Met_var_NC_names(ivar)
write(errlog(io),*)' Expected : ',Met_var_ndim(ivar)
write(errlog(io),*)' Found : ',var_ndims
endif;enddo
stop 1
endif
if(.not.allocated(var_dimIDs))allocate(var_dimIDs(var_ndims))
nSTAT = nf90_inquire_variable(ncid, in_var_id, invar, &
dimids = var_dimIDs(:var_ndims))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"inq_variable")
! 3-d transient variables should be in the COORDS convention (time, level, y, x)
! 4 3 2 1
! if ivar = 1 (Geopotential Height), then take this opportunity to get all
! the info on the dimensions for x,y and time. We will assume these
! are the same for all variables. Pressure levels will be read
! separately since they do differ among variables.
if(ivar.eq.1)then
i_dim = 1 ! get x info
nSTAT = nf90_inquire_dimension(ncid,var_dimIDs(i_dim), &
name = dimname, &
len = dimlen)
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"nf90_inquire_dimension X")
nx_fullmet = dimlen
xdim_id = var_dimIDs(i_dim)
if(index(dimname,'x') .eq.0.and.& ! Check this dimension against known names
index(dimname,'lon') .eq.0.and.&
index(dimname,'g0_lon_3') .eq.0.and.&
index(dimname,'longitude').eq.0.and.&
index(dimname,'g4_lon_3') .eq.0.and.&
index(dimname,'west_east').eq.0.and.&
index(dimname,trim(adjustl(Met_dim_names(4)))).eq.0)then
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR WARNING: Name of assumed x dimension does not'
write(errlog(io),*)' match any expected names. Please verify'
write(errlog(io),*)' that this file is COORDS compliant.'
write(errlog(io),*)' Dimension name: ',dimname
endif;enddo
endif
Met_dim_names(4) = trim(adjustl(dimname))
nSTAT = nf90_inq_varid(ncid,dimname,var_id) ! get the variable associated with this dim
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"inq_variable X")
! Check what temporary array to use
nSTAT = nf90_inquire_variable(ncid, var_id, dimname, xtype = var_xtype)
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"nf90_inquire_variable X")
allocate(x_fullmet_sp(0:nx_fullmet+1))
if(var_xtype.eq.NF90_BYTE)then
allocate(temp1d_byte(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_byte, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var X byte")
! copy to local variable
!x_fullmet_sp(1:nx_fullmet) = real(temp1d_byte(1:nx_fullmet),kind=sp)
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR variable x is of type BYTE, but real value expected.'
endif;enddo
stop 1
deallocate(temp1d_byte)
elseif(var_xtype.eq.NF90_CHAR)then
allocate(temp1d_char(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_char, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var X char")
! copy to local variable
!x_fullmet_sp(1:nx_fullmet) = real(temp1d_char(1:nx_fullmet),kind=sp)
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR variable x is of type CHAR, but real value expected.'
endif;enddo
stop 1
deallocate(temp1d_char)
elseif(var_xtype.eq.NF90_SHORT)then
! For SHORT values, expect that values use scale/offset
nSTAT = nf90_get_att(ncid, var_id,"scale_factor",dum_sp)
var_scale_fac = dum_sp
nSTAT = nf90_get_att(ncid, var_id,"add_offset",dum_sp)
var_offset = dum_sp
allocate(temp1d_intS(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_intS, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var X short")
! copy to local variable
x_fullmet_sp(1:nx_fullmet) = real(temp1d_intS(1:nx_fullmet),kind=sp) * &
var_scale_fac + var_offset
deallocate(temp1d_intS)
elseif(var_xtype.eq.NF90_INT)then
allocate(temp1d_intL(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_intL, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var X int")
! copy to local variable
x_fullmet_sp(1:nx_fullmet) = real(temp1d_intL(1:nx_fullmet),kind=sp)
deallocate(temp1d_intL)
elseif(var_xtype.eq.NF90_FLOAT)then
allocate(temp1d_sp(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_sp, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var X flt")
! copy to local variable
x_fullmet_sp(1:nx_fullmet) = temp1d_sp(1:nx_fullmet)
deallocate(temp1d_sp)
elseif(var_xtype.eq.NF90_DOUBLE)then
allocate(temp1d_dp(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_dp, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var X dbl")
! copy to local variable
x_fullmet_sp(1:nx_fullmet) = real(temp1d_dp(1:nx_fullmet),kind=sp)
deallocate(temp1d_dp)
else
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR ERROR: Cannot recognize variable type for x'
if(var_xtype.eq.NF90_BYTE) write(errlog(io),*)"NF90_BYTE = " ,NF90_BYTE
if(var_xtype.eq.NF90_CHAR) write(errlog(io),*)"NF90_CHAR = " ,NF90_CHAR
if(var_xtype.eq.NF90_SHORT) write(errlog(io),*)"NF90_SHORT = " ,NF90_SHORT
if(var_xtype.eq.NF90_INT) write(errlog(io),*)"NF90_INT = " ,NF90_INT
if(var_xtype.eq.NF90_FLOAT) write(errlog(io),*)"NF90_FLOAT = " ,NF90_FLOAT
if(var_xtype.eq.NF90_DOUBLE)write(errlog(io),*)"NF90_DOUBLE = ",NF90_DOUBLE
if(var_xtype.eq.NF90_UBYTE) write(errlog(io),*)"NF90_UBYTE = " ,NF90_UBYTE
if(var_xtype.eq.NF90_USHORT)write(errlog(io),*)"NF90_USHORT = ",NF90_USHORT
if(var_xtype.eq.NF90_UINT) write(errlog(io),*)"NF90_UINT = " ,NF90_UINT
if(var_xtype.eq.NF90_INT64) write(errlog(io),*)"NF90_INT64 = " ,NF90_INT64
if(var_xtype.eq.NF90_UINT64)write(errlog(io),*)"NF90_UINT64 = ",NF90_UINT64
if(var_xtype.eq.NF90_STRING)write(errlog(io),*)"NF90_STRING = ",NF90_STRING
endif;enddo
stop 1
endif
! Check the units
nSTAT = nf90_Inquire_Attribute(ncid, var_id,&
"units",xtype, length, attnum)
if(nSTAT.ne.NF90_NOERR)then
call MR_NC_check_status(nSTAT,0,"nf90_Inquire_Attribute")
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)'MR WARNING: Cannot find units ',dimname,nf90_strerror(nSTAT)
endif;enddo
else
nSTAT = nf90_get_att(ncid, var_id,"units",ustring)
call MR_NC_check_status(nSTAT,1,"nf90_get_att X units")
if(index(ustring,'km').gt.0.or.&
index(ustring,'kilo').gt.0)then
! This is a projected grid
IsLatLon_MetGrid = .false.
elseif(index(ustring,'deg').gt.0)then
! This is a lon/lat grid
IsLatLon_MetGrid = .true.
else
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)"MR ERROR: Cannot determine if the grid is lon/lat or projected"
endif;enddo
stop 1
endif
endif
! Finally, check for orientation
if(x_fullmet_sp(1).lt.x_fullmet_sp(2))then
x_inverted = .false.
else
x_inverted = .true.
endif
allocate(MR_dx_met(nx_fullmet))
x_start = x_fullmet_sp(1)
dx_met_const = x_fullmet_sp(2)-x_fullmet_sp(1)
x_fullmet_sp(0) = x_fullmet_sp(1) - dx_met_const
x_fullmet_sp(nx_fullmet+1) = x_fullmet_sp(nx_fullmet) + dx_met_const
if(abs(x_fullmet_sp(nx_fullmet+1)-360.0_sp-x_fullmet_sp(1)).lt.0.1_sp*dx_met_const)then
IsGlobal_MetGrid = .true.
else
IsGlobal_MetGrid = .false.
endif
do i = 1,nx_fullmet
MR_dx_met(i) = x_fullmet_sp(i+1)-x_fullmet_sp(i)
enddo
i_dim = 2 ! get y info
nSTAT = nf90_inquire_dimension(ncid,var_dimIDs(i_dim), &
name = dimname, &
len = dimlen)
call MR_NC_check_status(nSTAT,1,"nf90_inquire_dimension Y")
!if(index(dimname,Met_dim_names(3)).ne.0)then
ny_fullmet = dimlen
ydim_id = var_dimIDs(i_dim)
!endif
if(index(dimname,'y') .eq.0.and.& ! Check this dimension against known names
index(dimname,'lat') .eq.0.and.&
index(dimname,'g0_lat_2') .eq.0.and.&
index(dimname,'latitude') .eq.0.and.&
index(dimname,'g4_lat_2') .eq.0.and.&
index(dimname,'south_north').eq.0.and.&
index(dimname,trim(adjustl(Met_dim_names(3)))).eq.0)then
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR WARNING: Name of assumed y dimension does not'
write(errlog(io),*)' match any expected names. Please verify'
write(errlog(io),*)' that this file is COORDS compliant.'
write(errlog(io),*)' Dimension name: ',dimname
endif;enddo
endif
Met_dim_names(3) = trim(adjustl(dimname))
nSTAT = nf90_inq_varid(ncid,dimname,var_id) ! get the variable associated with this dim
call MR_NC_check_status(nSTAT,1,"nf90_inq_varid Y")
! Check what temporary array to use
nSTAT = nf90_inquire_variable(ncid, var_id, dimname, xtype = var_xtype)
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"nf90_inquire_variable Y")
allocate(y_fullmet_sp(0:ny_fullmet+1))
if(var_xtype.eq.NF90_BYTE)then
allocate(temp1d_byte(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_byte, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var Y byte")
! copy to local variable
!y_fullmet_sp(1:ny_fullmet) = real(temp1d_byte(1:ny_fullmet),kind=sp)
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR variable x is of type BYTE, but real value expected.'
endif;enddo
stop 1
deallocate(temp1d_byte)
elseif(var_xtype.eq.NF90_CHAR)then
allocate(temp1d_char(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_char, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var Y char")
! copy to local variable
!y_fullmet_sp(1:ny_fullmet) = real(temp1d_char(1:ny_fullmet),kind=sp)
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR variable x is of type CHAR, but real value expected.'
endif;enddo
stop 1
deallocate(temp1d_char)
elseif(var_xtype.eq.NF90_SHORT)then
! For SHORT values, expect that values use scale/offset
nSTAT = nf90_get_att(ncid, var_id,"scale_factor",dum_sp)
var_scale_fac = dum_sp
nSTAT = nf90_get_att(ncid, var_id,"add_offset",dum_sp)
var_offset = dum_sp
allocate(temp1d_intS(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_intS, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var Y short")
! copy to local variable
y_fullmet_sp(1:ny_fullmet) = real(temp1d_intS(1:ny_fullmet),kind=sp) * &
var_scale_fac + var_offset
deallocate(temp1d_intS)
elseif(var_xtype.eq.NF90_INT)then
allocate(temp1d_intL(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_intL, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var Y int")
! copy to local variable
y_fullmet_sp(1:ny_fullmet) = real(temp1d_intL(1:ny_fullmet),kind=sp)
deallocate(temp1d_intL)
elseif(var_xtype.eq.NF90_FLOAT)then
allocate(temp1d_sp(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_sp, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var Y flt")
! copy to local variable
y_fullmet_sp(1:ny_fullmet) = temp1d_sp(1:ny_fullmet)
deallocate(temp1d_sp)
elseif(var_xtype.eq.NF90_DOUBLE)then
allocate(temp1d_dp(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_dp, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var Y dbl")
! copy to local variable
y_fullmet_sp(1:ny_fullmet) = real(temp1d_dp(1:ny_fullmet),kind=sp)
deallocate(temp1d_dp)
else
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR ERROR: Cannot recognize variable type for y'
if(var_xtype.eq.NF90_BYTE) write(errlog(io),*)"NF90_BYTE = " ,NF90_BYTE
if(var_xtype.eq.NF90_CHAR) write(errlog(io),*)"NF90_CHAR = " ,NF90_CHAR
if(var_xtype.eq.NF90_SHORT) write(errlog(io),*)"NF90_SHORT = " ,NF90_SHORT
if(var_xtype.eq.NF90_INT) write(errlog(io),*)"NF90_INT = " ,NF90_INT
if(var_xtype.eq.NF90_FLOAT) write(errlog(io),*)"NF90_FLOAT = " ,NF90_FLOAT
if(var_xtype.eq.NF90_DOUBLE)write(errlog(io),*)"NF90_DOUBLE = ",NF90_DOUBLE
if(var_xtype.eq.NF90_UBYTE) write(errlog(io),*)"NF90_UBYTE = " ,NF90_UBYTE
if(var_xtype.eq.NF90_USHORT)write(errlog(io),*)"NF90_USHORT = ",NF90_USHORT
if(var_xtype.eq.NF90_UINT) write(errlog(io),*)"NF90_UINT = " ,NF90_UINT
if(var_xtype.eq.NF90_INT64) write(errlog(io),*)"NF90_INT64 = " ,NF90_INT64
if(var_xtype.eq.NF90_UINT64)write(errlog(io),*)"NF90_UINT64 = ",NF90_UINT64
if(var_xtype.eq.NF90_STRING)write(errlog(io),*)"NF90_STRING = ",NF90_STRING
endif;enddo
stop 1
endif
! Check the units
nSTAT = nf90_Inquire_Attribute(ncid, var_id,&
"units",xtype, length, attnum)
if(nSTAT.ne.NF90_NOERR)then
call MR_NC_check_status(nSTAT,0,"nf90_Inquire_Attribute Y units")
do io=1,MR_nio;if(VB(io).le.verbosity_info)then
write(outlog(io),*)'MR WARNING: Cannot find units ',dimname,nf90_strerror(nSTAT)
endif;enddo
else
nSTAT = nf90_get_att(ncid, var_id,"units",ustring)
call MR_NC_check_status(nSTAT,1,"nf90_get_att Y units")
if(index(ustring,'km').gt.0.or.&
index(ustring,'kilo').gt.0)then
! This is a projected grid
IsLatLon_MetGrid = .false.
elseif(index(ustring,'deg').gt.0)then
! This is a lon/lat grid
IsLatLon_MetGrid = .true.
else
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)"MR ERROR: Cannot determine if the grid is lon/lat or projected"
endif;enddo
stop 1
endif
endif
! check for orientation
if(y_fullmet_sp(1).lt.y_fullmet_sp(2))then
y_inverted = .false.
else
y_inverted = .true.
endif
allocate(MR_dy_met(ny_fullmet))
y_start = y_fullmet_sp(1)
dy_met_const = y_fullmet_sp(2)-y_fullmet_sp(1)
do i = 1,ny_fullmet-1
MR_dy_met(i) = y_fullmet_sp(i+1)-y_fullmet_sp(i)
enddo
MR_dy_met(ny_fullmet) = MR_dy_met(ny_fullmet-1)
! We need to check if this is a regular grid
IsRegular_MetGrid = .true.
do i = 1,nx_fullmet-1
if(abs(MR_dx_met(i+1)-MR_dx_met(i)).gt.tol*MR_dx_met(i))then
IsRegular_MetGrid = .false.
endif
enddo
do i = 1,ny_fullmet-1
if(abs(MR_dy_met(i+1)-MR_dy_met(i)).gt.tol*MR_dy_met(i))then
IsRegular_MetGrid = .false.
endif
enddo
i_dim = 4 ! get t info
nSTAT = nf90_inquire_dimension(ncid,var_dimIDs(i_dim), &
name = dimname, &
len = dimlen)
call MR_NC_check_status(nSTAT,1,"nf90_inquire_dimension time")
!if(index(dimname,Met_dim_names(1)).ne.0)then
nt_fullmet = dimlen
tdim_id = var_dimIDs(i_dim)
!endif
if(index(dimname,'time') .eq.0.and.& ! Check this dimension against known names
index(dimname,'Time') .eq.0.and.&
index(dimname,trim(adjustl(Met_dim_names(1)))).eq.0)then
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR WARNING: Name of assumed t dimension does not'
write(errlog(io),*)' match any expected names. Please verify'
write(errlog(io),*)' that this file is COORDS compliant.'
write(errlog(io),*)' Dimension name: ',dimname
endif;enddo
endif
Met_dim_names(1) = trim(adjustl(dimname))
endif ! ivar.eq.1
! Dimension variables for x,y,time were read while reading GPH immediately
! above, but we need the level information for all variables, which may be
! different than GPH.
! Now checking level coordinates (pressure, height, depth);
! This is the third dimension for COORD compliant NetCDF files.
i_dim = 3
nSTAT = nf90_inquire_dimension(ncid,var_dimIDs(i_dim), &
name = dimname, &
len = dimlen)
call MR_NC_check_status(nSTAT,1,"nf90_inquire_dimension P")
! Here is the master list of the known level names. For each
! variable, we check if the 'level' coordinate is new or already
! noted in this file, building an indexed list
nSTAT = nf90_inq_varid(ncid,dimname,var_id)
if(nSTAT.eq.NF90_NOERR.and. & ! This first condition excludes dims with no vars
(index(dimname,'lev') .ne.0.or.& ! This one catches both 'lev' and 'level'
index(dimname,'isobaric') .ne.0.or.&
index(dimname,'isobaricInhPa') .ne.0.or.&
index(dimname,'pressure') .ne.0.or.&
index(dimname,'height') .ne.0.or.&
index(dimname,'depth') .ne.0.or.&
index(dimname,'lv_ISBL1') .ne.0.or.&
index(dimname,'bottom_top') .ne.0.or.&
index(dimname,'bottom_top_stag') .ne.0.or.&
index(dimname,'soil_layers_stag').ne.0).or.&
index(dimname,trim(adjustl(Met_dim_names(4)))).ne.0)then ! This last check is in case
! the template file has something
! non-standard
! Log this level coordinate if it is the first
if (nlev_coords_detected.eq.0)then
nlev_coords_detected = nlev_coords_detected + 1
Met_var_zdim_idx(ivar) = nlev_coords_detected
Met_var_zdim_ncid(ivar) = var_dimIDs(i_dim)
maxdimlen = dimlen
else
! Otherwise, check if this level coordinate has already been logged
FoundOldDim = .false.
do iivar = 1,ivar-1
if (Met_var_zdim_ncid(iivar).eq.var_dimIDs(i_dim))then
FoundOldDim = .true.
Met_var_zdim_idx(ivar) = Met_var_zdim_idx(iivar)
Met_var_zdim_ncid(ivar) = var_dimIDs(i_dim)
exit
endif
enddo
if(.not.FoundOldDim)then
nlev_coords_detected = nlev_coords_detected + 1
Met_var_zdim_idx(ivar) = nlev_coords_detected
Met_var_zdim_ncid(ivar) = var_dimIDs(i_dim)
if (maxdimlen.lt.dimlen) maxdimlen = dimlen
endif
endif
else
call MR_NC_check_status(nSTAT,0,"nf90_inq_varid")
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR ERROR: level coordinate is not in pos. 3 for ',invar
write(errlog(io),*)' Expected one of: lev, level, isobaric, pressure,'
write(errlog(io),*)' height, depth, lv_ISBL1, bottom_top,'
write(errlog(io),*)' bottom_top_stag, soil_layers_stag'
write(errlog(io),*)' Instead, found: ',dimname
endif;enddo
stop 1
endif
! tidy up
deallocate(var_dimIDs)
enddo ! ivar
! We have all the level dimension names and dim_ids; now we need to get the sizes
allocate(nlevs_fullmet(nlev_coords_detected))
allocate(levs_code(nlev_coords_detected))
allocate(levs_fullmet_sp(nlev_coords_detected,maxdimlen))
do ivar = 1,MR_MAXVARS
! Check if this variable has a z-dimension (pressure, height, depth, etc.)
if(Met_var_zdim_ncid(ivar).gt.0)then
! log the length of the dimension for this level coordinate
nSTAT = nf90_inquire_dimension(ncid,Met_var_zdim_ncid(ivar), &
name = dimname, &
len = dimlen)
call MR_NC_check_status(nSTAT,1,"nf90_inquire_dimension level")
idx = Met_var_zdim_idx(ivar)
nlevs_fullmet(idx) = dimlen
! Now inquire and populate the dimension variable info
nSTAT = nf90_inq_varid(ncid,dimname,var_id)
call MR_NC_check_status(nSTAT,1,"nf90_inq_varid level")
! Check what temporary array to use
nSTAT = nf90_inquire_variable(ncid, var_id, dimname, xtype = var_xtype)
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"nf90_inquire_variable level")
if(var_xtype.eq.NF90_BYTE)then
allocate(temp1d_byte(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_byte, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var level byte")
! copy to local variable
!levs_fullmet_sp(idx,1:nlevs_fullmet(idx)) = real(temp1d_byte(1:nlevs_fullmet(idx)),kind=sp)
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR variable level is of type BYTE, but real value expected.'
endif;enddo
stop 1
deallocate(temp1d_byte)
elseif(var_xtype.eq.NF90_CHAR)then
allocate(temp1d_char(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_char, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var level char")
! copy to local variable
!levs_fullmet_sp(idx,1:nlevs_fullmet(idx)) = real(temp1d_char(1:nlevs_fullmet(idx)),kind=sp)
do io=1,MR_nio;if(VB(io).le.verbosity_error)then
write(errlog(io),*)'MR variable level is of type CHAR, but real value expected.'
endif;enddo
stop 1
deallocate(temp1d_char)
elseif(var_xtype.eq.NF90_SHORT)then
! For SHORT values, expect that values use scale/offset
nSTAT = nf90_get_att(ncid, var_id,"scale_factor",dum_sp)
var_scale_fac = dum_sp
nSTAT = nf90_get_att(ncid, var_id,"add_offset",dum_sp)
var_offset = dum_sp
allocate(temp1d_intS(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_intS, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var level short")
! copy to local variable
levs_fullmet_sp(idx,1:nlevs_fullmet(idx)) = real(temp1d_intS(1:nlevs_fullmet(idx)),kind=sp)* &
var_scale_fac + var_offset
deallocate(temp1d_intS)
elseif(var_xtype.eq.NF90_INT)then
allocate(temp1d_intL(dimlen))
nSTAT = nf90_get_var(ncid,var_id,temp1d_intL, &
start = (/1/),count = (/dimlen/))
if(nSTAT.ne.NF90_NOERR)call MR_NC_check_status(nSTAT,1,"get_var level int")
! copy to local variable
levs_fullmet_sp(idx,1:nlevs_fullmet(idx)) = temp1d_intL(1:nlevs_fullmet(idx))
deallocate(temp1d_intL)