-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetCDF_mod.f90
executable file
·5295 lines (4760 loc) · 219 KB
/
NetCDF_mod.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
module NetCDF_mod
!
! Routines for netCDF output
!
! Written by Peter january 2003->
!
!for details see:
!http://www.unidata.ucar.edu/software/netcdf/
!
!
!To improve: When output is onto the same file, but with different positions
!for the lower left corner, the coordinates i_EMEP j_EMEP and long lat will
!be wrong
!
use My_Outputs_mod, only : NHOURLY_OUT, & ! No. outputs
Asc2D, hr_out, & ! Required outputs
LEVELS_HOURLY, NLEVELS_HOURLY
!NML SELECT_LEVELS_HOURLY
use Chemfields_mod, only : xn_shl,xn_adv
use CheckStop_mod, only : CheckStop,StopAll,check=>CheckNC
use ChemDims_mod, only : NSPEC_TOT, NSPEC_ADV, NSPEC_SHL
use ChemSpecs_mod, only : species
use Config_module, only: KMAX_MID,KMAX_BND, runlabel1, runlabel2&
,MasterProc, NETCDF_DEFLATE_LEVEL &
,NPROC, IIFULLDOM,JJFULLDOM &
,IOU_INST,IOU_YEAR,IOU_MON,IOU_DAY &
,IOU_HOUR,IOU_HOUR_INST,IOU_HOUR_EXTRA &
,PT,Pref,NLANDUSEMAX, model&
,USE_EtaCOORDINATES,RUNDOMAIN&
,fullrun_DOMAIN,month_DOMAIN,day_DOMAIN,hour_DOMAIN&
,SurfacePressureFile &
,SELECT_LEVELS_HOURLY& ! NML
, num_lev3d,lev3d& ! 3D levels on 3D output
, startdate
use Country_mod, only : NLAND, Country
use Debug_module, only : DEBUG_NETCDF, DEBUG_NETCDF_RF
use Functions_mod, only: StandardAtmos_km_2_kPa
use GridValues_mod, only : GRIDWIDTH_M,fi,xp,yp,xp_EMEP_official&
,debug_proc, debug_li, debug_lj &
,yp_EMEP_official,fi_EMEP,GRIDWIDTH_M_EMEP&
,grid_north_pole_latitude&
,grid_north_pole_longitude,dx_rot,dx_roti,x1_rot,y1_rot&
!,GlobalPosition,glat_fdom,glon_fdom&
,ref_latitude&
,projection, sigma_mid,gb_stagg,gl_stagg,glon&
,sigma_bnd&
,glat,lb2ij,A_bnd,B_bnd&
,lb2ij,lb2ijm,ij2lb&
,ref_latitude_EMEP,xp_EMEP_old,yp_EMEP_old&
,i_local,j_local,i_fdom,j_fdom&
,Eta_bnd,Eta_mid,A_bnd,B_bnd,A_mid,B_mid&
,lon0_lambert,&!reference longitude, also called phi, at which y=0 if lat=lat0_lambert
lat0_lambert,&!reference latitude, at which x=0
lat_stand1_lambert,&!standard latitude at which mapping factor=1
lat_stand2_lambert,&!second standard latitude
x1_lambert,& !x value at i=1
y1_lambert !y value at j=1
use InterpolationRoutines_mod, only : grid2grid_coeff
use MPI_Groups_mod, only: MPI_LOGICAL, MPI_SUM,MPI_INTEGER, MPI_BYTE,MPISTATUS, &
MPI_COMM_IO, MPI_COMM_CALC, IERROR, ME_IO, ME_CALC
use netcdf
use OwnDataTypes_mod, only : Deriv, TXTLEN_NAME
use Par_mod, only : me,GIMAX,GJMAX,MAXLIMAX, MAXLJMAX, &
IRUNBEG,JRUNBEG,limax,ljmax, &
gi0,gj0,tgi0,tgi1,tgj0,tgj1,tlimax,tljmax
use PhysicalConstants_mod, only : PI, EARTH_RADIUS
use TimeDate_mod, only: nmdays,leapyear ,current_date, date,julian_date
use TimeDate_ExtraUtil_mod,only: date2nctime
use SmallUtils_mod, only: wordsplit, find_index
implicit none
integer, public, parameter :: &
max_filename_length=200 ! large enough to include paths, eg Nest_config namelist
character(len=max_filename_length), save :: &
fileName = 'NotSet',&
fileName_iou(IOU_INST:IOU_HOUR_EXTRA)=&
['out_inst.nc ','out_year.nc ','out_month.nc ','out_day.nc ',&
'out_hour.nc ','out_hourInst.nc','out_3Dhour.nc ']
character(len=125) :: period_type !TESTHH
integer,parameter ::closedID=-999 !flag for showing that a file is closed
integer :: ncFileID_new=closedID !don't save because should always be
!redefined (in case several routines are using ncFileID_new
!with different filename_given)
integer,save :: ncFileID_iou(IOU_INST:IOU_HOUR_EXTRA)=closedID
integer,save :: outCDFtag=0
!CDF types for output:
integer, public, parameter :: Int1=1,Int2=2,Int4=3,Real4=4,Real8=5
character (len=18),parameter::Default_projection_name = 'General_Projection'
public :: Out_netCDF
public :: printCDF ! minimal caller for Out_netCDF
public :: CloseNetCDF
public :: Init_new_netCDF
public :: GetCDF
public :: GetCDF_modelgrid
public :: WriteCDF !for testing purposes
public :: ReadField_CDF
public :: ReadField_CDF_FL
public :: ReadTimeCDF
public :: vertical_interpolate
public :: Out_CDF_sondes
public :: Create_CDF_sondes
public :: check
public :: IsCDFfractionFormat
public :: ReadSectorName
public :: check_lon_lat
public :: make_gridresolution
public :: create_country_emission_file
public :: output_country_emissions
private :: CreatenetCDFfile
private :: createnewvariable
contains
!_______________________________________________________________________
subroutine Out_CDF_sondes(fileName,SpecName,NSpec,Values,NLevels,g_ps,debug)
!writes out a given variable with attributes in 1D (vertical coordinates) + station dimension (and time).
!Note: defining a different variable for each station/component pair, would be very slow (almost double cpu time of run)
!Note: writing only one variable at a time would be extremely slow
character(len=*), intent(in) :: fileName,SpecName(NSpec)
integer, intent(in) :: NLevels,NSpec
real,intent(in) :: Values(Nlevels,NSpec,*),g_ps(*)
logical, intent(in), optional :: debug
logical :: debug_1D
integer :: ncFileID
integer :: varID,dimID
character(len=8) :: lastmodified_date
character(len=10) :: lastmodified_hour
integer :: i,iSpec,nrecords,nstations
real :: rdays
real,save,allocatable :: buff(:,:)
debug_1D=DEBUG_NETCDF
if(present(debug))debug_1D = debug .or. debug_1D
if(MasterProc)then
if(debug_1D)write(*,*)'writing in ',trim(fileName)
!The file must have been created already with "Create_CDF_1D"
call check(nf90_open(fileName,nf90_share+nf90_write,ncFileID))
! netCDF functions calls (optional=arg)
! nf90_open(path,cmode,ncid)
! nf90_inq_dimid(ncid,name,dimid)
! nf90_inquire_dimension(ncid,dimid,name=name,len=len)
! nf90_inq_varid(ncid,name,varid)
! nf90_put_var(ncid,varid,values,start=start,count=count)
call Date_And_Time(date=lastmodified_date,time=lastmodified_hour)
call check(nf90_inq_dimid(ncFileID,"time",dimID),"dim:time")
call check(nf90_inquire_dimension(ncFileID,dimID,len=nrecords),"len:time")
call check(nf90_inq_dimid(ncFileID,"station",dimID),"dim:station")
call check(nf90_inquire_dimension(ncFileID,dimID,len=nstations),"len:station")
if(debug_1D)write(*,*)'number of stations ',nstations
nrecords=nrecords+1
if(debug_1D)write(*,*)'writing on record ',nrecords
call check(nf90_inq_varid(ncFileID,"time",varID),"inq:time")
call date2nctime(current_date,rdays)
call check(nf90_put_var(ncFileID,varID,rdays,start=[nrecords]),"put:time")
if(.not.allocated(buff).and.NLevels>1)allocate(buff(nstations,Nlevels))
do iSpec=1,NSpec
!The variable must have been created already with "Create_CDF_1D"
call check(nf90_inq_varid(ncFileID,SpecName(iSpec),varID),&
"inq:"//trim(SpecName(iSpec)))
if(debug_1D)write(*,"(A,1X,A20,2(1X,I0))")&
'writing',trim(SpecName(iSpec)),NLevels,nstations
if(NLevels==1)then
call check(nf90_put_var(ncFileID,varID,values(1,iSpec,1:nstations),&
start=[1,nrecords],count=[nstations,1]),"put:"//trim(SpecName(iSpec)))
else
do i=1,nstations
buff(i,1:NLevels)=Values(1:NLevels,iSpec,i)!NB: indices are switched (transposed of matrix)
end do
call check(nf90_put_var(ncFileID,varID,buff,&
start=[1,1,nrecords],count=[nstations,NLevels,1]),&
"put:"//trim(SpecName(iSpec)))
! call check(nf90_put_var(ncFileID,varID,buff,start=[1,1,nrecords],count=[1,1,1]))
end if
if(NLevels>1)then
call check(nf90_inq_varid(ncFileID,'PS',varID),"inq:PS")
call check(nf90_put_var(ncFileID,varID,g_ps(1:nstations),&
start=[1,nrecords],count=[nstations,1]),"put:PS")
end if
end do
call check(nf90_put_att(ncFileID,nf90_global,"lastmodified_date",lastmodified_date))
call check(nf90_put_att(ncFileID,nf90_global,"lastmodified_hour",lastmodified_hour))
call check(nf90_close(ncFileID))
end if !Masterproc
end subroutine Out_CDF_sondes
subroutine Create_CDF_sondes(fileName,NSpec,NSpec_Att,SpecDef,&
NStations,NMetaData,MetaData,KMAXcdf,CDFtype,debug)
! Create a NetCDF file with given attributes.
! if Nvalues>1 define vertical coordinates.
integer, intent(in) :: KMAXcdf,NSpec,NStations,NSpec_Att,NMetaData
character(len=*), intent(in) :: fileName, &
SpecDef(NSpec,0:NSpec_Att),MetaData(0:NStations,NMetaData)
integer, intent(in), optional :: CDFtype
logical, intent(in), optional :: debug
logical :: debug_1D
integer :: OUTtype,ncFileID,levDimID,ilevDimID,timeDimID
integer :: varID,StationDimID,StringDimID
character (len=*), parameter :: vert_coord='atmosphere_hybrid_sigma_pressure_coordinate'
character(len=8) :: lastmodified_date
character(len=10) :: lastmodified_hour
integer :: i,k,n,iSpec,iSta
real :: kcoord(KMAXcdf+1)
real :: Acdf(KMAXcdf),Bcdf(KMAXcdf),Aicdf(KMAXcdf+1),Bicdf(KMAXcdf+1)
integer,parameter :: MAX_String_length=36
character(len=100) :: auxL(4)
character(len=MAX_String_length) :: metaName,metaType,auxC(NStations)
integer :: auxI(NStations),ierr
real :: auxR(NStations)
debug_1D=DEBUG_NETCDF
if(present(debug))debug_1D = debug .or. debug_1D
if(MasterProc)then
!Create file if it does not yet exist
if(debug_1D)write(*,*)'creating ',trim(fileName)
call check(nf90_create(fileName,nf90_clobber,ncFileID),"create:"//trim(fileName))
! netCDF functions calls (wo optional arg.)
! nf90_create(path,cmode,ncid)
! nf90_def_dim(ncid,name,len,dimid)
! nf90_def_var(ncid,name,xtype,dimids,varid)
! nf90_put_att(ncid,varid,name,values)
! nf90_inq_varid(ncid,name,varid)
! nf90_put_var(ncid,varid,values)
call check(nf90_def_dim(ncFileID,"time",nf90_unlimited,timeDimID),"dim:time")
call check(nf90_def_dim(ncFileID,"station",NStations,StationDimID),"dim:station")
call check(nf90_def_dim(ncFileID,"string_length",MAX_String_length,StringDimID),"dim:slen")
if(KMAXcdf>1)then
if(debug_1D)write(*,*)'defining vertical levels ',KMAXcdf
call check(nf90_put_att(ncFileID,nf90_global,"vert_coord",vert_coord))
call check(nf90_def_dim(ncFileID, "lev",KMAXcdf , levDimID),"dim:lev")
call check(nf90_def_dim(ncFileID,"ilev",KMAXcdf+1,ilevDimID),"dim:ilev")
call check(nf90_def_var(ncFileID,"lev",nf90_double,levDimID,varID))
call check(nf90_put_att(ncFileID,varID,"standard_name","atmosphere_hybrid_sigma_pressure_coordinate"))
call check(nf90_put_att(ncFileID,varID,"long_name","hybrid level at layer midpoints (A/P0+B)"))
call check(nf90_put_att(ncFileID,varID,"positive","up"))
call check(nf90_put_att(ncFileID,varID,"formula_terms","ap: hyam b: hybm ps: PS p0: P0"))
!p(n,k,j,i) = a(k)+ b(k)*ps(n,j,i)
call check(nf90_def_var(ncFileID,"ilev",nf90_double,ilevDimID,varID))
call check(nf90_put_att(ncFileID,varID,"standard_name","atmosphere_hybrid_sigma_pressure_coordinate"))
call check(nf90_put_att(ncFileID,varID,"long_name","hybrid level at layer interfaces (A/P0+B)"))
call check(nf90_put_att(ncFileID,varID,"positive","up"))
call check(nf90_put_att(ncFileID,varID,"formula_terms","ap: hyai b: hybi ps: PS p0: P0"))
call check(nf90_def_var(ncFileID,"P0",nf90_double,varID))
call check(nf90_put_att(ncFileID,varID,"units","hPa"))
call check(nf90_def_var(ncFileID,"PS",nf90_double,[StationDimID,timeDimID],varID))
call check(nf90_put_att(ncFileID,varID,"long_name","Surface pressure"))
call check(nf90_put_att(ncFileID,varID,"units","hPa"))
!The hybrid sigma-pressure coordinate for level k is defined as ap(k)/p0+b(k).
call check(nf90_def_var(ncFileID,"hyam",nf90_double,levDimID,varID))
call check(nf90_put_att(ncFileID,varID,"long_name","hybrid A coefficient at layer midpoints"))
call check(nf90_put_att(ncFileID,varID,"units","hPa"))
call check(nf90_def_var(ncFileID,"hybm",nf90_double,levDimID,varID))
call check(nf90_put_att(ncFileID,varID,"long_name","hybrid B coefficient at layer midpoints"))
call check(nf90_def_var(ncFileID,"hyai",nf90_double,ilevDimID,varID))
call check(nf90_put_att(ncFileID,varID,"long_name","hybrid A coefficient at layer interfaces"))
call check(nf90_put_att(ncFileID,varID,"units","hPa"))
call check(nf90_def_var(ncFileID,"hybi",nf90_double,ilevDimID,varID) )
call check(nf90_put_att(ncFileID,varID,"long_name","hybrid B coefficient at layer interfaces"))
else
if(debug_1D)write(*,*)'not defining vertical levels ',KMAXcdf
end if
call check(nf90_def_var(ncFileID,"time",nf90_double,timeDimID,varID))
call check(nf90_put_att(ncFileID,varID,"long_name", "time (instantaneous)"))
call check(nf90_put_att(ncFileID,varID,"units","days since 1900-1-1 0:0:0"))
call Date_And_Time(date=lastmodified_date,time=lastmodified_hour)
call check(nf90_put_att(ncFileID,nf90_global,"created_date",lastmodified_date))
call check(nf90_put_att(ncFileID,nf90_global,"created_hour",lastmodified_hour))
!global attributes
do n=1,NMetaData
if(MetaData(0,n)=="")cycle
if(debug_1D)write(*,*)'Global Attribute ',trim(MetaData(0,n))
call wordsplit(trim(MetaData(0,n)),3,auxL,k,ierr,strict_separator=':')
call CheckStop(3,k,&
"NetCDF_mod: too short metadata definition "//trim(MetaData(0,n)))
select case(auxL(2))
case("c","C","s","S") ! string/char attribute
call check(nf90_put_att(ncFileID,nf90_global,trim(auxL(1)),trim(auxL(3))),&
"MetaData="//trim(MetaData(0,n)))
case("i","I","n","N") ! integer attribute
read(auxL(3),*)auxI(1)
call check(nf90_put_att(ncFileID,nf90_global,trim(auxL(1)),auxI(1)),&
"MetaData="//trim(MetaData(0,n)))
case("f","F","d","D") ! float/double attribute
read(auxL(3),*)auxR(1)
call check(nf90_put_att(ncFileID,nf90_global,trim(auxL(1)),auxR(1)),&
"MetaData="//trim(MetaData(0,n)))
case default
call CheckStop("NetCDF_mod: unknown metadata-type "//trim(MetaData(0,n)))
end select
end do
OUTtype=Real4 !default value
if(present(CDFtype))OUTtype=CDFtype
select case(OUTtype)
case(Int1 );OUTtype=nf90_byte
case(Int2 );OUTtype=nf90_short
case(Int4 );OUTtype=nf90_int
case(Real4);OUTtype=nf90_float
case(Real8);OUTtype=nf90_double
case default;
call CheckStop("NetCDF_mod:undefined datatype")
end select
do iSpec=1,NSpec
!define the variables
if(debug_1D)write(*,*)'Defining new variable ',trim(SpecDef(iSpec,0)),OUTtype
if(KMAXcdf>1)then
call check(nf90_def_var(ncFileID,SpecDef(iSpec,0),OUTtype,&
[StationDimID,levDimID,timeDimID],varID))
else
call check(nf90_def_var(ncFileID,SpecDef(iSpec,0),OUTtype,&
[StationDimID,timeDimID],varID))
end if
!species attributes:
do n=1,NSpec_Att
if(SpecDef(iSpec,n)=="")cycle
call wordsplit(trim(SpecDef(iSpec,n)),3,auxL,k,ierr,strict_separator=':')
call CheckStop(3,k,&
"NetCDF_mod: too short metadata definition "//trim(SpecDef(iSpec,n)))
select case(auxL(1))
case("_FillValue") ! ensure that OUTtype/_FillValue types match
select case(OUTtype) ! even under -r8 compilation option
case(NF90_BYTE) ! _FillValue=achar(0)
call check(nf90_put_att(ncFileID,varID,trim(auxL(1)),NF90_FILL_BYTE))
case(NF90_SHORT) ! _FillValue=-32767
call check(nf90_put_att(ncFileID,varID,trim(auxL(1)),NF90_FILL_SHORT))
case(NF90_INT) ! _FillValue=-2147483647
call check(nf90_put_att(ncFileID,varID,trim(auxL(1)),NF90_FILL_INT))
case(NF90_FLOAT) ! _FillValue=9.9692099683868690e+36
call check(nf90_put_att(ncFileID,varID,trim(auxL(1)),NF90_FILL_FLOAT))
case(NF90_DOUBLE) ! _FillValue=9.9692099683868690e+36
call check(nf90_put_att(ncFileID,varID,trim(auxL(1)),NF90_FILL_DOUBLE))
end select
case default
auxC(1)=NF90_FILL_CHAR
auxI(1)=NF90_FILL_INT
auxR(1)=NF90_FILL_DOUBLE
select case(auxL(2))
case("c","C","s","S") ! string/char attribute
if(auxL(3)/="missing")auxC(1)=trim(auxL(3))
call check(nf90_put_att(ncFileID,varID,trim(auxL(1)),trim(auxC(1))),&
"SpecDef="//trim(SpecDef(iSpec,n)))
case("i","I","n","N") ! integer attribute
if(auxL(3)/="missing")read(auxL(3),*)auxI(1)
call check(nf90_put_att(ncFileID,varID,trim(auxL(1)),auxI(1)),&
"SpecDef="//trim(SpecDef(iSpec,n)))
case("f","F","d","D") ! float/double attribute
if(auxL(3)/="missing")read(auxL(3),*)auxR(1)
call check(nf90_put_att(ncFileID,varID,trim(auxL(1)),auxR(1)),&
"SpecDef="//trim(SpecDef(iSpec,n)))
case default
call CheckStop("NetCDF_mod: unknown metadata-type "//trim(SpecDef(iSpec,n)))
end select
end select
end do
end do
if(debug_1D)write(*,*)'Defining station meta-data'
do n=1,NMetaData
if(MetaData(1,n)=="")cycle
if(debug_1D)write(*,*)'Defining station ',trim(MetaData(1,n))
call wordsplit(trim(MetaData(1,n)),3,auxL,k,ierr,strict_separator=':')
call CheckStop(3,k,&
"NetCDF_mod: too short metadata definition "//trim(MetaData(1,n)))
select case(auxL(2))
case("c","C","s","S") ! string/char attribute
call check(nf90_def_var(ncFileID,auxL(1),nf90_char,&
[StringDimID,StationDimID],varID),"def:"//trim(auxL(1)))
call check(nf90_put_att(ncFileID,varID,"_FillValue",NF90_FILL_CHAR),&
"def:"//trim(auxL(1))//"@_FillValue")
case("i","I","n","N") ! integer attribute
call check(nf90_def_var(ncFileID,auxL(1),nf90_int,&
StationDimID,varID),"def:"//trim(auxL(1)))
call check(nf90_put_att(ncFileID,varID,"_FillValue",NF90_FILL_INT),&
"def:"//trim(auxL(1))//"@_FillValue")
case("f","F","d","D") ! float/double attribute
call check(nf90_def_var(ncFileID,auxL(1),nf90_double,&
StationDimID,varID),"def:"//trim(auxL(1)))
call check(nf90_put_att(ncFileID,varID,"_FillValue",NF90_FILL_DOUBLE),&
"def:"//trim(auxL(1))//"@_FillValue")
case default
call CheckStop("NetCDF_mod: unknown metadata-type "//trim(MetaData(1,n)))
end select
end do
call check(nf90_put_att(ncFileID,nf90_global,"lastmodified_date",lastmodified_date))
call check(nf90_put_att(ncFileID,nf90_global,"lastmodified_hour",lastmodified_hour))
call check(nf90_enddef(ncFileID),"define done:"//trim(fileName))
if(debug_1D)write(*,*)'Writing station meta-data'
do n=1,NMetaData
if(MetaData(1,n)=="")cycle
if(debug_1D)write(*,*)'Writing station ',trim(MetaData(1,n))
call wordsplit(trim(MetaData(1,n)),3,auxL,k,ierr,strict_separator=':')
call CheckStop(3,k,&
"NetCDF_mod: too short metadata definition "//trim(MetaData(1,n)))
metaName=trim(auxL(1))
metaType=trim(auxL(2))
auxC(:)=NF90_FILL_CHAR
auxI(:)=NF90_FILL_INT
auxR(:)=NF90_FILL_DOUBLE
do iSta=1,NStations
if(MetaData(iSta,n)=="")cycle
call wordsplit(trim(MetaData(iSta,n)),3,auxL,k,ierr,strict_separator=':')
call CheckStop(3,k,&
"NetCDF_mod: too short metadata definition "//trim(MetaData(iSta,n)))
call CheckStop(metaName,auxL(1),&
"NetCDF_mod: inconsistent metadata-name definition "//trim(MetaData(iSta,n)))
call CheckStop(metaType,auxL(2),&
"NetCDF_mod: inconsistent metadata-type definition "//trim(MetaData(iSta,n)))
if(auxL(3)=="missing")cycle
select case(metaType)
case("c","C","s","S");auxC(iSta)=trim(auxL(3)) ! string/char attribute
case("i","I","n","N");read(auxL(3),*)auxI(iSta) ! integer attribute
case("f","F","d","D");read(auxL(3),*)auxR(iSta) ! float/double attribute
end select
end do
call check(nf90_inq_varid(ncFileID,metaName,varID),"inq:"//trim(metaName))
select case(metaType)
case("c","C","s","S") ! string/char attribute
call check(nf90_put_var(ncFileID,varID,auxC),"put:"//trim(metaType))
case("i","I","n","N") ! integer attribute
call check(nf90_put_var(ncFileID,varID,auxI),"put:"//trim(metaType))
case("f","F","d","D") ! float/double attribute
call check(nf90_put_var(ncFileID,varID,auxR),"put:"//trim(metaType))
case default
call CheckStop("NetCDF_mod: wrong metadata-type definition "//trim(metaType))
end select
end do
if(KMAXcdf>1)then
call check(nf90_inq_varid(ncFileID,"P0",varID),"inq:P0")
call check(nf90_put_var(ncFileID,varID,Pref/100.0 ),"put:P0")
do k=1,KMAXcdf
!REVERSE order of k !
Acdf(k)=A_mid(KMAX_MID-k+1)
Bcdf(k)=B_mid(KMAX_MID-k+1)
Aicdf(k)=A_bnd(KMAX_BND-k+1)
Bicdf(k)=B_bnd(KMAX_BND-k+1)
if(k==KMAXcdf)then
Aicdf(k+1)=A_bnd(KMAX_BND-k)
Bicdf(k+1)=B_bnd(KMAX_BND-k)
end if
end do
call check(nf90_inq_varid(ncFileID,"hyam",varID),"inq:hyam")
call check(nf90_put_var(ncFileID,varID,Acdf(1:KMAXcdf)/1e2),"put:hyam")
call check(nf90_inq_varid(ncFileID,"hybm",varID),"inq:hybm")
call check(nf90_put_var(ncFileID,varID,Bcdf(1:KMAXcdf)),"put:hybm")
call check(nf90_inq_varid(ncFileID,"hyai",varID),"inq:hyai")
call check(nf90_put_var(ncFileID,varID,Aicdf(1:KMAXcdf+1)/1e2),"put:hyai")
call check(nf90_inq_varid(ncFileID,"hybi",varID),"inq:hybi")
call check(nf90_put_var(ncFileID,varID,Bicdf(1:KMAXcdf+1)),"put:hybi")
do i=1,KMAXcdf
kcoord(i)=Acdf(i)/Pref+Bcdf(i)
end do
call check(nf90_inq_varid(ncFileID,"lev",varID),"inq:lev")
call check(nf90_put_var(ncFileID,varID,kcoord(1:KMAXcdf)) ,"put:lev")
do i=1,KMAXcdf+1
kcoord(i)=Aicdf(i)/Pref+Bicdf(i)
end do
call check(nf90_inq_varid(ncFileID,"ilev",varID),"inq:ilev")
call check(nf90_put_var(ncFileID,varID,kcoord(1:KMAXcdf+1)),"put:ilev")
end if
call check(nf90_close(ncFileID))
end if !Masterproc
end subroutine Create_CDF_sondes
subroutine Init_new_netCDF(fileName,iotyp)
integer, intent(in) :: iotyp
character(len=*), intent(in) :: fileName
integer :: GIMAXcdf,GJMAXcdf,IBEGcdf,JBEGcdf,KMAXcdf
integer :: i1,i2,j1,j2
call CloseNetCDF !must be called by all procs, to syncronize outCDFtag
!if(MasterProc)then
if(MasterProc.and.DEBUG_NETCDF )&
write(*,*)'Init_new_netCDF ',trim(fileName),iotyp
!NB IBEGcdf and JBEGcdf are here defined relative to fulldomain
IBEGcdf=GIMAX+IRUNBEG-1; JBEGcdf=GJMAX+JRUNBEG-1 !initialisations
GIMAXcdf=0; GJMAXcdf=0 !initialisations
KMAXcdf=1
select case (iotyp)
case (IOU_YEAR)
fileName_iou(iotyp) = trim(fileName)
period_type = 'fullrun'
i1=fullrun_DOMAIN(1);i2=fullrun_DOMAIN(2);j1=fullrun_DOMAIN(3);j2=fullrun_DOMAIN(4)
IBEGcdf=min(IBEGcdf,i1); JBEGcdf=min(JBEGcdf,j1)
GIMAXcdf=max(GIMAXcdf,i2-i1+1); GJMAXcdf=max(GJMAXcdf,j2-j1+1)
case(IOU_MON)
fileName_iou(iotyp) = trim(fileName)
period_type = 'monthly'
i1=month_DOMAIN(1);i2=month_DOMAIN(2);j1=month_DOMAIN(3);j2=month_DOMAIN(4)
IBEGcdf=min(IBEGcdf,i1); JBEGcdf=min(JBEGcdf,j1)
GIMAXcdf=max(GIMAXcdf,i2-i1+1); GJMAXcdf=max(GJMAXcdf,j2-j1+1)
case(IOU_DAY)
fileName_iou(iotyp) = trim(fileName)
period_type = 'daily'
i1=day_DOMAIN(1);i2=day_DOMAIN(2);j1=day_DOMAIN(3);j2=day_DOMAIN(4)
IBEGcdf=min(IBEGcdf,i1); JBEGcdf=min(JBEGcdf,j1)
GIMAXcdf=max(GIMAXcdf,i2-i1+1); GJMAXcdf=max(GJMAXcdf,j2-j1+1)
case(IOU_HOUR)
fileName_iou(iotyp) = trim(fileName)
period_type = 'hourlyMean'
i1=hour_DOMAIN(1);i2=hour_DOMAIN(2);j1=hour_DOMAIN(3);j2=hour_DOMAIN(4)
IBEGcdf=min(IBEGcdf,i1); JBEGcdf=min(JBEGcdf,j1)
GIMAXcdf=max(GIMAXcdf,i2-i1+1); GJMAXcdf=max(GJMAXcdf,j2-j1+1)
case(IOU_HOUR_INST)
fileName_iou(iotyp) = trim(fileName)
period_type = 'hourly'
i1=hour_DOMAIN(1);i2=hour_DOMAIN(2);j1=hour_DOMAIN(3);j2=hour_DOMAIN(4)
IBEGcdf=min(IBEGcdf,i1); JBEGcdf=min(JBEGcdf,j1)
GIMAXcdf=max(GIMAXcdf,i2-i1+1); GJMAXcdf=max(GJMAXcdf,j2-j1+1)
case(IOU_HOUR_EXTRA)
fileName_iou(iotyp) = trim(fileName)
period_type = 'hourly'
i1=hour_DOMAIN(1);i2=hour_DOMAIN(2);j1=hour_DOMAIN(3);j2=hour_DOMAIN(4)
IBEGcdf=min(IBEGcdf,i1); JBEGcdf=min(JBEGcdf,j1)
GIMAXcdf=max(GIMAXcdf,i2-i1+1); GJMAXcdf=max(GJMAXcdf,j2-j1+1)
KMAXcdf =min(maxval(hr_out(1:NHOURLY_OUT)%nk),NLEVELS_HOURLY)
case(IOU_INST)
fileName_iou(iotyp) = trim(fileName)
period_type = 'instant'
case default
period_type = 'unknown'
end select
if(MasterProc.and.DEBUG_NETCDF)&
write(*,*) "Creating ", trim(fileName),' ',trim(period_type)
if(iotyp/=IOU_HOUR_EXTRA)then
call CreatenetCDFfile(fileName,GIMAXcdf,GJMAXcdf,IBEGcdf,JBEGcdf,&
num_lev3d,KLEVcdf=lev3d,KLEVcdf_from_top=.true.)
elseif(SELECT_LEVELS_HOURLY)then ! Output selected model levels
call CreatenetCDFfile(fileName,GIMAXcdf,GJMAXcdf,IBEGcdf,JBEGcdf,&
KMAXcdf,KLEVcdf=LEVELS_HOURLY)
else
call CreatenetCDFfile(fileName,GIMAXcdf,GJMAXcdf,IBEGcdf,JBEGcdf,KMAXcdf)
end if
if(MasterProc.and.DEBUG_NETCDF)&
write(*,*) "Finished Init_new_netCDF", trim(fileName),' ',trim(period_type)
!end if
end subroutine Init_new_netCDF
subroutine CreatenetCDFfile(fileName,GIMAXcdf,GJMAXcdf,IBEGcdf,JBEGcdf,&
KMAXcdf,KLEVcdf,KLEVcdf_from_top,RequiredProjection)
!IBEGcdf,JBEGcdf relative to fulldomain
integer, intent(in) :: GIMAXcdf,GJMAXcdf,IBEGcdf,JBEGcdf,KMAXcdf
character(len=*), intent(in) :: fileName
integer, intent(in), optional :: KLEVcdf(KMAXcdf)
logical, intent(in), optional :: KLEVcdf_from_top
character(len=*),optional, intent(in):: RequiredProjection
character(len=*), parameter :: author_of_run='emepctm group'
character(len=19) :: projection_params='90.0 -32.0 0.933013' !set later on
real :: xcoord(GIMAX),ycoord(GJMAX)
character(len=8) :: created_date,lastmodified_date
character(len=10) :: created_hour,lastmodified_hour
integer :: iDimID,jDimID,levDimID,ilevDimID,timeDimID,VarID,iVarID,jVarID
integer :: ncFileID,iEMEPVarID,jEMEPVarID,latVarID,longVarID
integer :: i1,j1,istart,jstart,ishift,jshift,icount,jcount
integer :: i,j,k,iproc
logical :: levels_from_top=.true.
real :: scale_at_projection_origin
character(len=80) ::UsedProjection
character (len=*), parameter :: vert_coord='atmosphere_hybrid_sigma_pressure_coordinate'
real ::Buff2D(MAXLIMAX,MAXLJMAX,2)
! fileName: Name of the new created file
! nf90_clobber: protect existing datasets
! ncFileID: netcdf ID
!Check that the dimensions are > 0
if(GIMAXcdf<=0.or.GJMAXcdf<=0.or.KMAXcdf<=0)then
write(*,*)'WARNING:'
write(*,*)trim(fileName),&
' not created. Requested area too small (or outside domain) '
write(*,*)'sizes (IMAX,JMAX,IBEG,JBEG,KMAX) ',&
GIMAXcdf,GJMAXcdf,IBEGcdf,JBEGcdf,KMAXcdf
return
end if
if(present(RequiredProjection))then
UsedProjection=trim(RequiredProjection)
else
UsedProjection=trim(projection)
end if
if(MasterProc)write(*,*)'creating ',trim(fileName)
if(MasterProc.and.DEBUG_NETCDF)write(*,*)'UsedProjection ',trim(UsedProjection)
if(MasterProc.and.DEBUG_NETCDF)write(*,fmt='(A,8I7)')'with sizes (IMAX,JMAX,IBEG,JBEG,KMAX) ',&
GIMAXcdf,GJMAXcdf,IBEGcdf,JBEGcdf,KMAXcdf
if(MasterProc)then
if(NETCDF_DEFLATE_LEVEL >= 0)then
if(MasterProc.and.DEBUG_NETCDF)write(*,*)'nf90_create'
call check(nf90_create(fileName,nf90_hdf5,ncFileID),"create:"//trim(fileName))
if(MasterProc.and.DEBUG_NETCDF)write(*,*)'nf90_created'
else
call check(nf90_create(fileName,nf90_clobber,ncFileID),"create:"//trim(fileName))
end if
! Define the dimensions
select case(UsedProjection)
case('Stereographic')
! define coordinate dimensions
call check(nf90_def_dim(ncFileID,"i",GIMAXcdf,iDimID),"dim:i")
call check(nf90_def_dim(ncFileID,"j",GJMAXcdf,jDimID),"dim:j")
scale_at_projection_origin=(1.+sin(ref_latitude*PI/180.))/2.
write(projection_params,fmt='(''90.0 '',F5.1,F9.6)')fi,scale_at_projection_origin
call check(nf90_put_att(ncFileID,nf90_global,"projection_params",projection_params))
! define coordinate variables
iVarID =define_var("i" ,nf90_float,[iDimID])
jVarID =define_var("j" ,nf90_float,[jDimID])
iEMEPVarID=define_var("i_EMEP",nf90_float,[iDimID])
jEMEPVarID=define_var("j_EMEP",nf90_float,[jDimID])
latVarID =define_var("lat" ,nf90_float,[iDimID,jDimID])
longVarID =define_var("lon" ,nf90_float,[iDimID,jDimID])
case('lon lat')
! define coordinate dimensions
call check(nf90_def_dim(ncFileID,"lon",GIMAXcdf,iDimID),"dim:lon")
call check(nf90_def_dim(ncFileID,"lat",GJMAXcdf,jDimID),"dim:lat")
! define coordinate variables
iVarID=define_var("lon",nf90_double,[iDimID])
jVarID=define_var("lat",nf90_double,[jDimID])
case('Rotated_Spherical')
! define coordinate dimensions
call check(nf90_def_dim(ncFileID,"i",GIMAXcdf,iDimID),"dim:i_RotS")
call check(nf90_def_dim(ncFileID,"j",GJMAXcdf,jDimID),"dim:j_RotS")
! define coordinate variables
iVarID =define_var("i_RotS",nf90_float,[iDimID])
jVarID =define_var("j_RotS",nf90_float,[jDimID])
latVarID =define_var("lat" ,nf90_float,[iDimID,jDimID])
longVarID =define_var("lon" ,nf90_float,[iDimID,jDimID])
case('lambert')
! define coordinate dimensions
call check(nf90_def_dim(ncFileID,"i",GIMAXcdf,iDimID))
call check(nf90_def_dim(ncFileID,"j",GJMAXcdf,jDimID))
! define coordinate variables
iVarID =define_var("i_lambert",nf90_float,[iDimID])
jVarID =define_var("j_lambert",nf90_float,[jDimID])
latVarID =define_var("lat" ,nf90_float,[iDimID,jDimID])
longVarID =define_var("lon" ,nf90_float,[iDimID,jDimID])
case default !general projection
! define coordinate dimensions
call check(nf90_def_dim(ncFileID,"i",GIMAXcdf,iDimID),"dim:i")
call check(nf90_def_dim(ncFileID,"j",GJMAXcdf,jDimID),"dim:j")
! define coordinate variables
iVarID =define_var("i" ,nf90_float,[iDimID])
jVarID =define_var("j" ,nf90_float,[jDimID])
latVarID =define_var("lat" ,nf90_float,[iDimID,jDimID])
longVarID =define_var("lon" ,nf90_float,[iDimID,jDimID])
end select
if(MasterProc.and.DEBUG_NETCDF)write(*,*)'lon lat dims defined'
call check(nf90_def_dim(ncFileID, "lev",KMAXcdf , levDimID))
call check(nf90_def_dim(ncFileID,"ilev",KMAXcdf+1,ilevDimID))
call check(nf90_put_att(ncFileID, nf90_global,"vert_coord",vert_coord))
call check(nf90_def_dim(ncFileID,"time",nf90_unlimited,timeDimID))
call Date_And_Time(date=created_date,time=created_hour)
if(DEBUG_NETCDF)write(*,"(2A)")&
'created_date: ',created_date,'created_hour: ',created_hour
! Write global attributes
call check(nf90_put_att(ncFileID,nf90_global,"Conventions", "CF-1.6" ))
!call check(nf90_put_att(ncFileID,nf90_global,"version", version ))
call check(nf90_put_att(ncFileID,nf90_global,"model", model))
call check(nf90_put_att(ncFileID,nf90_global,"author_of_run", author_of_run))
call check(nf90_put_att(ncFileID,nf90_global,"created_date", created_date))
call check(nf90_put_att(ncFileID,nf90_global,"created_hour", created_hour))
lastmodified_date = created_date
lastmodified_hour = created_hour
call check(nf90_put_att(ncFileID,nf90_global,"lastmodified_date", lastmodified_date))
call check(nf90_put_att(ncFileID,nf90_global,"lastmodified_hour", lastmodified_hour))
call check(nf90_put_att(ncFileID,nf90_global,"projection",UsedProjection))
call check(nf90_put_att(ncFileID,nf90_global,"period_type", &
trim(period_type)), "period_type:"//trim(period_type) )
call check(nf90_put_att(ncFileID,nf90_global,"run_label", trim(runlabel2)))
! The hybrid sigma-pressure coordinate for level k is defined as ap(k)/p0+b(k).
! p(n,k,j,i) = ap(k)+ b(k)*ps(n,j,i)
varID=define_var("lev" ,nf90_double,[levDimID])
varID=define_var("P0" ,nf90_double,[0])
varID=define_var("hyam",nf90_double,[levDimID])
varID=define_var("hybm",nf90_double,[levDimID])
varID=define_var("ilev",nf90_double,[ilevDimID])
varID=define_var("hyai",nf90_double,[ilevDimID])
varID=define_var("hybi",nf90_double,[ilevDimID])
varID=define_var("time",nf90_double,[timeDimID])
!CF-1.0 definitions:
select case(UsedProjection)
case('Stereographic')
varID=define_var("Polar_Stereographic",nf90_int,[0])
case('lon lat')
! no additional projection meta-data needed
case('Rotated_Spherical')
varID=define_var("Rotated_Spherical",nf90_int ,[0])
varID=define_var("rotated_pole" ,nf90_char,[0]) ! for NCL
case('lambert')
varID=define_var("projection_lambert",nf90_int ,[0])
case default
varID=define_var("Default_projection_name",nf90_int,[0])
end select
! Leave define mode
call check(nf90_enddef(ncFileID), "define_done"//trim(fileName) )
! Define horizontal distances
select case(UsedProjection)
case('Stereographic')
xcoord(1)=(IBEGcdf-xp)*GRIDWIDTH_M/1000.
do i=2,GIMAXcdf
xcoord(i)=xcoord(i-1)+GRIDWIDTH_M/1000.
end do
ycoord(1)=(JBEGcdf-yp)*GRIDWIDTH_M/1000.
do j=2,GJMAXcdf
ycoord(j)=ycoord(j-1)+GRIDWIDTH_M/1000.
end do
call check(nf90_put_var(ncFileID,iVarID,xcoord(1:GIMAXcdf)))
call check(nf90_put_var(ncFileID,jVarID,ycoord(1:GJMAXcdf)))
! Define horizontal coordinates in the official EMEP grid
!xp_EMEP_official=8.
!yp_EMEP_official=110.
!GRIDWIDTH_M_EMEP=50000.
!fi_EMEP=-32.
if(fi==fi_EMEP)then
! Implemented only if fi = fi_EMEP = -32 (Otherwise needs a 2-dimensional mapping)
! uses (i-xp)*GRIDWIDTH_M = (i_EMEP-xp_EMEP)*GRIDWIDTH_M_EMEP
do i=1,GIMAXcdf
xcoord(i)=(i+IBEGcdf-1-xp)*GRIDWIDTH_M/GRIDWIDTH_M_EMEP + xp_EMEP_official
!print *, i,xcoord(i)
end do
do j=1,GJMAXcdf
ycoord(j)=(j+JBEGcdf-1-yp)*GRIDWIDTH_M/GRIDWIDTH_M_EMEP + yp_EMEP_official
!print *, j,ycoord(j)
end do
else
xcoord(1:GIMAXcdf)=NF90_FILL_FLOAT
ycoord(1:GJMAXcdf)=NF90_FILL_FLOAT
end if
call check(nf90_put_var(ncFileID,iEMEPVarID,xcoord(1:GIMAXcdf)))
call check(nf90_put_var(ncFileID,jEMEPVarID,ycoord(1:GJMAXcdf)))
case('Rotated_Spherical')
do i=1,GIMAXcdf
xcoord(i)= (i+IBEGcdf-2)*dx_rot+x1_rot
end do
do j=1,GJMAXcdf
ycoord(j)= (j+JBEGcdf-2)*dx_rot+y1_rot
end do
call check(nf90_put_var(ncFileID,iVarID,xcoord(1:GIMAXcdf)))
call check(nf90_put_var(ncFileID,jVarID,ycoord(1:GJMAXcdf)))
case('lambert')
do i=1,GIMAXcdf
xcoord(i)= (i+IBEGcdf-2)*GRIDWIDTH_M+x1_lambert
end do
do j=1,GJMAXcdf
ycoord(j)= (j+JBEGcdf-2)*GRIDWIDTH_M+y1_lambert
end do
call check(nf90_put_var(ncFileID,iVarID,xcoord(1:GIMAXcdf)))
call check(nf90_put_var(ncFileID,jVarID,ycoord(1:GJMAXcdf)))
case('lon lat')
do i=1,GIMAXcdf
xcoord(i)= glon(1,1)+(i_local(i+IBEGcdf-1)-1)*(glon(2,1)-glon(1,1))
!force monotone values:
if(i>1)then
!must first check that i>1 before testing xcoord(i-1) (to avoid debug errors)
if(xcoord(i)<xcoord(i-1).and.xcoord(i)<0)xcoord(i)=xcoord(i)+360.0
end if
end do
do j=1,GJMAXcdf
ycoord(j)= glat(1,1)+(j_local(j+JBEGcdf-1)-1)*(glat(1,2)-glat(1,1))
end do
call check(nf90_put_var(ncFileID,iVarID,xcoord(1:GIMAXcdf)))
call check(nf90_put_var(ncFileID,jVarID,ycoord(1:GJMAXcdf)))
case default
xcoord(1)=(IBEGcdf-0.5)*GRIDWIDTH_M/1000.
do i=2,GIMAXcdf
xcoord(i)=xcoord(i-1)+GRIDWIDTH_M/1000.
!print *, i,xcoord(i)
end do
ycoord(1)=(JBEGcdf-0.5)*GRIDWIDTH_M/1000.
do j=2,GJMAXcdf
ycoord(j)=ycoord(j-1)+GRIDWIDTH_M/1000.
end do
call check(nf90_put_var(ncFileID,iVarID,xcoord(1:GIMAXcdf)))
call check(nf90_put_var(ncFileID,jVarID,ycoord(1:GJMAXcdf)))
! write(*,*)'coord written'
end select! projections
! Define vertical levels
if(present(KLEVcdf))then !order is defined in KLEVcdf
levels_from_top=.false.
if(present(KLEVcdf_from_top))levels_from_top=KLEVcdf_from_top
call write_klev(KMAXcdf,KLEVcdf,levels_from_top)
else
levels_from_top=(KMAXcdf==KMAX_MID)
call write_klev(KMAXcdf,[(k,k=1,KMAXcdf)],levels_from_top)
end if
if(DEBUG_NETCDF) write(*,*) "end serial part "
end if! MasterProc
!NB: this part is run by all processors
!make lon and lat
if(UsedProjection=='lon lat') then
! lon and lat are 1 dimensional, already done
else
!write one block at a time
Buff2D(1:limax,1:ljmax,1)=glon(1:limax,1:ljmax)
Buff2D(1:limax,1:ljmax,2)=glat(1:limax,1:ljmax)
if(MasterProc)then
do iproc=0,NPROC-1
if(iproc>0)&
CALL MPI_RECV(Buff2D,2*8*MAXLIMAX*MAXLJMAX, MPI_BYTE,iproc,&
iproc,MPI_COMM_CALC,MPISTATUS,IERROR)
!NB IBEGcdf, IRUNBEG, etc. relative to fulldomain
! tgi0,tlimax, etc. relative to rundomain
i1=1 ! buffer, rundomain
j1=1 ! buffer, rundomain
istart=tgi0(iproc) ! restricted (IBEGcdf, JBEGcdf) domain
jstart=tgj0(iproc) ! restricted domain
icount=tlimax(iproc) ! both
jcount=tljmax(iproc) ! both
! must shift from rundomain to restricted grid in case it is smaller
ishift=IBEGcdf-IRUNBEG
jshift=JBEGcdf-JRUNBEG
istart=istart-ishift
jstart=jstart-jshift
! not entire buffer may be needed
! if the buffer start at the left of the restricted domain,
! must shift start of buffer and take less elements
if(tgi0(iproc)<=ishift)then
i1=i1+ishift-tgi0(iproc)+1
istart=istart+(ishift-tgi0(iproc)+1)
icount=icount-(ishift-tgi0(iproc)+1)
end if
if(tgj0(iproc)<=jshift)then
j1=j1+jshift-tgj0(iproc)+1
jstart=jstart+(jshift-tgj0(iproc)+1)
jcount=jcount-(jshift-tgj0(iproc)+1)
end if
! if we end at the right of the buffer end. must shift end of buffer,
! i.e. take less element
if(istart+icount-1>GIMAXcdf)icount=GIMAXcdf-istart+1
if(jstart+jcount-1>GJMAXcdf)jcount=GJMAXcdf-jstart+1
if(all([istart<=GIMAXcdf+ishift,jstart<=GJMAXcdf+jshift,&
icount>0,jcount>0,i1<=tlimax(iproc),j1<=tljmax(iproc)]))then
call check(nf90_put_var(ncFileID,longVarID, &
Buff2D(i1:i1+icount-1,j1:j1+jcount-1,1), &
start=[istart,jstart],count=[icount,jcount]))
call check(nf90_put_var(ncFileID, latVarID, &
Buff2D(i1:i1+icount-1,j1:j1+jcount-1,2), &
start=[istart,jstart],count=[icount,jcount]))
end if
end do
if(DEBUG_NETCDF) write(*,*) "NetCDF: lon lat written"
else
CALL MPI_SEND(Buff2D,2*8*MAXLIMAX*MAXLJMAX,MPI_BYTE,0,&
me,MPI_COMM_CALC,IERROR)
end if
end if
if(MasterProc)then
call check(nf90_close(ncFileID))
if(DEBUG_NETCDF)write(*,*)'NetCDF: file created, end of CreatenetCDFfile ',ncFileID
end if
contains
function define_var(vname,xtype,dimIDs) result(varID)
character(len=*), intent(in) :: vname
integer, intent(in) :: xtype,dimIDs(:)
integer :: varID
select case(vname)
case("i")
call check(nf90_def_var(ncFileID,vname,xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"standard_name","projection_x_coordinate"))
call check(nf90_put_att(ncFileID,varID,"coord_axis","x"))
call check(nf90_put_att(ncFileID,varID,"long_name","EMEP grid x coordinate"))
call check(nf90_put_att(ncFileID,varID,"units","km"))
case("j")
call check(nf90_def_var(ncFileID,vname,xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"standard_name", "projection_y_coordinate"))
call check(nf90_put_att(ncFileID,varID,"coord_axis","y"))
call check(nf90_put_att(ncFileID,varID,"long_name","EMEP grid y coordinate"))
call check(nf90_put_att(ncFileID,varID,"units","km"))
case("i_EMEP")
call check(nf90_def_var(ncFileID,vname,xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"long_name","official EMEP grid coordinate i"))
call check(nf90_put_att(ncFileID,varID,"units","gridcells"))
case("j_EMEP")
call check(nf90_def_var(ncFileID,vname,xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"long_name","official EMEP grid coordinate j"))
call check(nf90_put_att(ncFileID,varID,"units","gridcells"))
case("i_RotS")
call check(nf90_def_var(ncFileID,"i",xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"standard_name","grid_longitude"))
call check(nf90_put_att(ncFileID,varID,"long_name","Rotated longitude"))
call check(nf90_put_att(ncFileID,varID,"units","degrees"))
call check(nf90_put_att(ncFileID,varID,"axis","X"))
case("j_RotS")
call check(nf90_def_var(ncFileID,"j",xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"standard_name","grid_latitude"))
call check(nf90_put_att(ncFileID,varID,"long_name","Rotated latitude"))
call check(nf90_put_att(ncFileID,varID,"units","degrees"))
call check(nf90_put_att(ncFileID,varID,"axis","Y"))
case("i_lambert")
call check(nf90_def_var(ncFileID,"i",xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"standard_name","projection_x_coordinate"))
call check(nf90_put_att(ncFileID,varID,"long_name","x-coordinate in Cartesian system"))
call check(nf90_put_att(ncFileID,varID,"units","m"))
call check(nf90_put_att(ncFileID,varID,"axis","X"))
case("j_lambert")
call check(nf90_def_var(ncFileID,"j",xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"standard_name","projection_y_coordinate"))
call check(nf90_put_att(ncFileID,varID,"long_name","y-coordinate in Cartesian system"))
call check(nf90_put_att(ncFileID,varID,"units","m"))
call check(nf90_put_att(ncFileID,varID,"axis","Y"))
case("lat")
call check(nf90_def_var(ncFileID,"lat",xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"long_name","latitude"))
call check(nf90_put_att(ncFileID,varID,"units","degrees_north"))
call check(nf90_put_att(ncFileID,varID,"standard_name","latitude"))
case("lon")
call check(nf90_def_var(ncFileID,"lon",xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"standard_name","longitude"))
call check(nf90_put_att(ncFileID,varID,"long_name","longitude"))
call check(nf90_put_att(ncFileID,varID,"units","degrees_east"))
case("lev") ! The hybrid sigma-pressure coordinate for level k is defined as ap(k)/p0+b(k).
! p(n,k,j,i) = ap(k)+ b(k)*ps(n,j,i)
call check(nf90_def_var(ncFileID,vname,xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"standard_name","atmosphere_hybrid_sigma_pressure_coordinate"))
call check(nf90_put_att(ncFileID,varID,"long_name", "hybrid level at layer midpoints (A/P0+B)"))
call check(nf90_put_att(ncFileID,varID,"positive", "down"))
call check(nf90_put_att(ncFileID,varID,"formula_terms","ap: hyam b: hybm ps: PS p0: P0"))
case("ilev")
call check(nf90_def_var(ncFileID,vname,xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"standard_name","atmosphere_hybrid_sigma_pressure_coordinate"))
call check(nf90_put_att(ncFileID,varID,"long_name", "hybrid level at layer interfaces (A/P0+B)"))
call check(nf90_put_att(ncFileID,varID,"positive", "down"))
call check(nf90_put_att(ncFileID,varID,"formula_terms","ap: hyai b: hybi ps: PS p0: P0"))
case("hyam")
call check(nf90_def_var(ncFileID,vname,xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"long_name","hybrid A coefficient at layer midpoints"))
call check(nf90_put_att(ncFileID,varID,"units","hPa"))
case("hyai")
call check(nf90_def_var(ncFileID,vname,xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID,"long_name","hybrid A coefficient at layer interfaces"))
call check(nf90_put_att(ncFileID,varID,"units","hPa"))
case("hybm")
call check(nf90_def_var(ncFileID,vname,xtype,dimIDs,varID),"def:"//trim(vname))
call check(nf90_put_att(ncFileID,varID, "long_name","hybrid B coefficient at layer midpoints"))