netcdf_output_mod.F90 72 KB
Newer Older
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
!***********************************************************************
! Copyright 2015                                                       *
!   Wayne Angevine        Delia Arnold          Jerome Brioude         *
!   John Burkhart         Massimo Cassiani      Adam Dingwell          *
!   Richard C Easter      Sabine Eckhardt       Stephanie Evan         *
!   Jerome D Fast         Caroline Forster      Don Morton             *
!   Ignacio Pisso         Petra Seibert         Harald Sodemann        *
!   Andreas Stohl         Gerard Wotawa                                *
!                                                                      * 
!                                                                      *
!  This file is part of FLEXPART WRF                                   *
!                                                                      *
! FLEXPART is free software: you can redistribute it and/or modify     *
! it under the terms of the GNU General Public License as published by *
! the Free Software Foundation, either version 3 of the License, or    *
! (at your option) any later version.                                  *
!                                                                      *
! FLEXPART is distributed in the hope that it will be useful,          *
! but WITHOUT ANY WARRANTY; without even the implied warranty of       *
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
! GNU General Public License for more details.                         *
!                                                                      *
! You should have received a copy of the GNU General Public License    *
! along with FLEXPART.  If not, see <http://www.gnu.org/licenses/>.    *
!***********************************************************************

!*******************************************************************************
!                                                                              *
!   This module contains the subroutines used to generate NetCDF output files. *
!                                                                              *
!       Author: A. Dingwell                                                    *
!                                                                              *
!       05 June 2015                                                           *
!                                                                              *
!   Note that the file-extension of this file should be ".F" (upper case)      *
!   This ensure that it is sent to a preprocessor when compiling with any of   *
!   the supported compilers (PGI,Intel or GNU)                                 *
!   The preprocessor is required to determine if netcdf4 compressed output     *
!   be included in the program or not.                                         *
!                                                                              *
!*******************************************************************************

module netcdf_output_mod

  ! uses wrf_map_utils_mod
  use netcdf              ! netcdf module
  use com_mod             ! Global variables
  use point_mod           !
  use outg_mod            ! Output grid definition
  use wrf_map_utils_mod   ! Map projection variables

  
  implicit none
  
  #ifdef NETCDF4_OUTPUT
  integer, parameter :: deflate_level=4   ! compression (1-9, 9 is most intense)
  logical, parameter :: shuffle=.FALSE.   ! For compressing integer arrays
  #endif
  
  contains
  
  subroutine nc_write_global_attributes(ncid)
  !*****************************************************************************
  !                                                                            *
  !  This subroutine writes global attributes to the given output file.        *
  !  It should be called once for each netcdf file (header or data) in the     *
  !  when the respective file is open in define mode                           *
  !                                                                            *
  !      Author: A. Dingwell                                                   *
  !                                                                            *
  !      27 May 2013                                                           *
  !                                                                            *
  !*****************************************************************************
  
    integer, intent(in) :: ncid ! File handle for output file
  
    if (ldirect.eq.1) then  ! Forward simulation
      if (option_verbose.ge.10) write(*,10) 'forward simulation attributes'
      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_START_DATE',ibdate)
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_START_TIME',ibtime)
      call check_ncerror(ncret)

      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_END_DATE',iedate)
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_END_TIME',ietime)
      call check_ncerror(ncret)
    else                  ! Backward simulation
      if (option_verbose.ge.10) write(*,10) 'backward simulation attributes'
      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_START_DATE',iedate)
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_START_TIME',ietime)
      call check_ncerror(ncret)

      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_END_DATE',ibdate)
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_END_TIME',ibtime)
      call check_ncerror(ncret)
    endif

    if (option_verbose.ge.10) write(*,10) 'map projection attributes'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'MAP_PROJ',map_proj_id)
    call check_ncerror(ncret)
    if (outgrid_option .eq. 1) then ! native lon-lat grid.
      ncret = &
        NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_PROJECTION','Regular Latit/Longit')
      call check_ncerror(ncret)
    else ! Using map projection, add projection info to output:
      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'EARTH_RADIUS_M',earth_radius_m)
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'CEN_LAT',proj_clat)
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'CEN_LON',proj_clon)
      call check_ncerror(ncret)
      if (map_proj_id.eq.1) then  ! Lamber conformal projection
        !ncret = &
        !  NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_PROJECTION','Lambert conformal')
        !call check_ncerror(ncret)
        ! Attributes on WRF format:
        ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'MAP_PROJ_CHAR','Lambert Conformal')
        call check_ncerror(ncret)
        ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'STAND_LON',proj_stdlon)
        call check_ncerror(ncret)
        ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'TRUELAT1',proj_truelat1)
        call check_ncerror(ncret)
        ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'TRUELAT2',proj_truelat2)
        call check_ncerror(ncret)
      elseif (map_proj_id.eq.2) then
        ncret = &
          NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_PROJECTION','stereographic')
        call check_ncerror(ncret)
      elseif (map_proj_id.eq.3) then
        ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'MAP_PROJ_CHAR','Mercator')
        call check_ncerror(ncret)
        ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'TRUELAT1',proj_truelat1)
        call check_ncerror(ncret)
      elseif (map_proj_id.eq.4) then
        ncret = &
          NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_PROJECTION','global')
        call check_ncerror(ncret)
      endif
    endif

    ! Write info common model settings
    !*********************************
    if (option_verbose.ge.10) write(*,10) 'common model attributes'

    if (option_verbose.ge.10) write(*,10) 'OUTPUT_INTERVAL'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_INTERVAL',loutstep)
    call check_ncerror(ncret)

    if (option_verbose.ge.10) write(*,10) 'AVERAGING_TIME'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'AVERAGING_TIME',loutaver)
    call check_ncerror(ncret)

    if (option_verbose.ge.10) write(*,10) 'AVERAGE_SAMPLING'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'AVERAGE_SAMPLING',loutsample)
    call check_ncerror(ncret)

    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'NSPEC',nspec)
    call check_ncerror(ncret)
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'NUMRECEPTOR',numreceptor)
    call check_ncerror(ncret)
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'NAGECLASS',nageclass)
    call check_ncerror(ncret)

    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'NUMRELEASES',numpoint)
    call check_ncerror(ncret)

    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'DISPERSION_METHOD',method)
    call check_ncerror(ncret)

    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SUBGRID_TOPOGRAPHY',lsubgrid)
    call check_ncerror(ncret)

    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'CONVECTION_PARAM',lconvection)
    call check_ncerror(ncret)

    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SUBGRID_TOPOGRAPHY',lsubgrid)
    call check_ncerror(ncret)
    
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'LU_OPTION',lu_option)
    call check_ncerror(ncret)

    ! Write information on output grid setup
    !***************************************
    if (option_verbose.ge.10) write(*,10) 'WEST-EAST_GRID_DIMENSION'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'WEST-EAST_GRID_DIMENSION', &
      ncgrid_nx)
    call check_ncerror(ncret)

    if (option_verbose.ge.10) write(*,10) 'SOUTH-NORTH_GRID_DIMENSION'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SOUTH-NORTH_GRID_DIMENSION', &
      ncgrid_ny)
    call check_ncerror(ncret)
    
    if (option_verbose.ge.10) write(*,10) 'BOTTOM-TOP_GRID_DIMENSION'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'BOTTOM-TOP_GRID_DIMENSION', &
      numzgrid)

    if (option_verbose.ge.10) write(*,10) 'DX and DY'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'DX',ncgrid_dx)
    call check_ncerror(ncret)

    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'DY',ncgrid_dy)
    call check_ncerror(ncret)
207
  end subroutine nc_write_global_attributes
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
  
  subroutine nc_create_main_outfile(itime,nesting_level)
  !*****************************************************************************
  !                                                                            *
  !  This routine perdefines a netcdf ouput file with information on flexpart  *
  !  settings, releases and topography.                                        *
  !                                                                            *
  !      Author: A. Dingwell                                                   *
  !                                                                            *
  !      27 May 2013                                                           *
  !                                                                            *
  ! Modifications:                                                             *
  ! June 5 2013: J. Brioude: Create and write attributes to netcdf output only *
  !  2015-04-28: A. Dingwell: Cleaned up comments and indentation              *
  !  2015-05-04: A. Dingwell: Added receptor points to netcdf output           *
  !  2015-06-04: A. Dingwell: Updated subroutine to use NetCDF F90 module.     *
  !  2015-06-05: A. Dingwell: Moved global attributes to separate subroutine   *
  !*****************************************************************************
  
    integer, intent(in):: itime,stat      ! seconds since simulation start
    integer, intent(in) :: nesting_level  ! 0=main grid (mother) 1=nest (child)
                            ! this is written to be easy to expand if additional 
                            ! nests are desired in the future

    real(kind=dp) :: jul          ! Julian date
    integer   :: jjjjmmdd,ihmmss  ! date & time as integer
    character :: adate*8,atime*6  ! date and time strings, used for filename

    ! Grid related variables
    real    :: xp1,yp1,xp2,yp2  ! temporary coordinates
    real    :: xsw,xne,ysw,yne,tmpx,tmpy,tmplon,tmplat,xl2,yl2
    integer :: ncgrid_nx,ncgrid_ny        ! nx,ny of current grid
    integer :: ncgrid_dx,ncgrid_dy        ! dx,dy of current grid in m or latlon
    real    :: ncgrid_swlon,ncgrid_swlat  ! SW corner of current grid in latlon
    real    :: ncgrid_nelon,ncgrid_nelat  ! NE corner of current grid in latlon
    real    :: ncgrid_xm0,ncgrid_ym0      ! lower-left grid coord in metres
    real    :: ncgrid_lon0,ncgrid_lat0    ! lower-left grid coord in latlon

    ! Iterators
    integer i,j,ix,jy

    ! NETCDF file related variables
    integer nclvlid,nclonid,nclatid,ncrecid,ncspcid,ncageid !outgrid dimension ids
    integer ncrelid,ncrseid                                 ! release points dimension ids
    integer ncrepid                             ! receptor points dimension ids
    integer ncrnvid,ncrmvid,ncspvid             ! release points: number,mass,species ids
    integer ncrtvid,ncrxvid,ncryvid,ncrzvid     ! release points: t,x,y,z min/max limits
    integer nctovid,ncarvid                     ! Topography and grid area variable-ids
    integer ncstr1id,ncstr2id,ncstr3id          ! description string length dimid
    integer nclvlvid,nclonvid,nclatvid,ncspcvid,ncagevid  ! outgrid dimension variables
    integer ncdimsid3(6),ncdimsid2(5) ! arrays of dimension ids for outgrid 3D & 2D
    integer ncdimsid32(7),ncdimsid22(6) ! arrays of dimension ids for outgrid 3D & 2D

    ! NETCDF filename & attribute related variables
    character descr*11,units*5,ncname*29,coord*11,coordxy*10
    !integer coordxylen
    character unit2d*10   ! unit for deposition fields
    !integer   unit2dlen   ! length of character string

    ! NETCDF misc variables
    integer ncid    ! local container for netcdf file-id (either ncout or ncoutn)
    integer ncret   ! Return-value of calls to nf_* utils
    
    ! integer :: chunks(2) ! shuffle
    ! Attribute notation:
    descr = 'description'
    units = 'units'
    coord = 'coordinates'
    coordxy = 'XLONG XLAT'
    !coordxylen = 10

    ! Determine current calendar date, needed for the file name
    !**********************************************************
    jul=bdate+real(itime,kind=dp)/86400._dp
    call caldate(jul,jjjjmmdd,ihmmss)
    write(adate,'(i8.8)') jjjjmmdd
    write(atime,'(i6.6)') ihmmss

    !************************
    ! Open header output file
    !************************
    write(ncname,'(A8,I2.2,A1,I8.8,A1,I6.6,A3)') &
      'flxout_d',nesting_level+1,'_',jjjjmmdd,'_',ihmmss,'.nc' ! filename

  ! print*,'step0',itime,jjjjmmdd,ihmmss

  !  call nf_set_log_level(3)
    if (option_verbose.ge.1) write(*,*) &
      'write_ncinfo: creating file: ',path(1)(1:length(1))//ncname
  ! call nf_set_chunk_cache(32000000)
  ! ncret = nf_create(path(1)(1:length(1))//ncname, nf_clobber,ncid)
    ncret = NF90_CREATE(path=path(1)(1:length(1))//ncname, cmode=NF90_NETCDF4,ncid=ncid)
    call check_ncerror(ncret)
    

    ! Determine which nest/outfile we just created so we can set up the grid
    !***********************************************************************
    if (nesting_level.eq.0) then  ! current grid is main grid
      ncout   = ncid  ! copy current file handle to ncout
      ncgrid_nx = numxgrid
      ncgrid_ny = numygrid
      ncgrid_nelon = outgrid_nelon
      ncgrid_nelat = outgrid_nelat
      ncgrid_swlon = outgrid_swlon
      ncgrid_swlat = outgrid_swlat
  !   allocate(ncgrid_oro(ncgrid_nx,ncgrid_ny),stat=stat)
  !   allocate(ncgrid_area(ncgrid_nx,ncgrid_ny),stat=stat)
  !   ncgrid_oro   = oroout(0:ncgrid_nx-1,0:ncgrid_ny-1)
  !   ncgrid_area  = area(0:ncgrid_nx-1,0:ncgrid_ny-1)
  !   print*,'step2'
      if (outgrid_option.eq.1) then ! input was in latlon
        ncgrid_dx = dxoutl
        ncgrid_dy = dyoutl
        ncgrid_lon0 = outlon0
        ncgrid_lat0 = outlat0
      else  ! input was in metres
        ncgrid_dx = dxout
        ncgrid_dy = dyout
        ncgrid_xm0  = out_xm0
        ncgrid_ym0  = out_ym0
      endif
    elseif (nesting_level.eq.1) then  ! current grid is nested
      ncoutn  = ncid  ! copy current file handle to ncoutn
      ncgrid_nx = numxgridn
      ncgrid_ny = numygridn
      ncgrid_nelon = outgridn_nelon
      ncgrid_nelat = outgridn_nelat
      ncgrid_swlon = outgridn_swlon
      ncgrid_swlat = outgridn_swlat
  !   allocate(ncgrid_oro(ncgrid_nx,ncgrid_ny),stat=stat)
  !   allocate(ncgrid_area(ncgrid_nx,ncgrid_ny),stat=stat)
  !   ncgrid_oro   = orooutn(0:ncgrid_nx-1,0:ncgrid_ny-1)
  !   ncgrid_area  = arean(0:ncgrid_nx-1,0:ncgrid_ny-1)
      if (outgrid_option.eq.1) then ! input was in latlon
        ncgrid_dx = dxoutln
        ncgrid_dy = dyoutln
        ncgrid_lon0 = outlon0n
        ncgrid_lat0 = outlat0n
      else  ! input was in metres
        ncgrid_dx = dxoutn
        ncgrid_dy = dyoutn
        ncgrid_xm0  = out_xm0n
        ncgrid_ym0  = out_ym0n
      endif
    endif
  !   print*,'step3'

    if (option_verbose.ge.10) &
      write(*,*) 'write_ncinfo: ncout,ncoutn=',ncout,ncoutn

    ! Write global attributes
    !*****************************
    call write_nc_global_attributes(ncid)

    ! Set up netcdf dimensions
    !*************************
    if (option_verbose.ge.10) write(*,10) 'main grid dimensions'
    
    ncret = NF90_DEF_DIM(ncid,'Time',NF90_UNLIMITED,ncrecid)
    call check_ncerror(ncret)

    ncret = NF90_DEF_DIM(ncid,'DateStrLen',15,ncstr3id) !TODO: WRF format
    call check_ncerror(ncret)

    ncret = NF90_DEF_DIM(ncid,'west_east',ncgrid_nx,nclonid)
    call check_ncerror(ncret)

    ncret = NF90_DEF_DIM(ncid,'south_north',ncgrid_ny,nclatid)
    call check_ncerror(ncret)

    ncret = NF90_DEF_DIM(ncid,'bottom_top',numzgrid,nclvlid)
    call check_ncerror(ncret)

    ncret = NF90_DEF_DIM(ncid,'species',nspec,ncspcid)
    call check_ncerror(ncret)

    ncret = NF90_DEF_DIM(ncid,'SpeciesStrLen',10,ncstr1id)
    call check_ncerror(ncret)

    ncret = NF90_DEF_DIM(ncid,'ageclass',nageclass,ncageid)
    call check_ncerror(ncret)

    if (option_verbose.ge.10) write(*,10) 'release point dimensions'
    ncret = NF90_DEF_DIM(ncid,'releases',numpoint,ncrelid)
    call check_ncerror(ncret)
    
    ncret = NF90_DEF_DIM(ncid,'ReleaseStrLen',45,ncstr2id)
    call check_ncerror(ncret)
    
    ncret = NF90_DEF_DIM(ncid,'ReleaseStartEnd',2,ncrseid)
    call check_ncerror(ncret)
    
    if(option_verbose.ge.10) write(*,10) 'receptor point dimensions'
    ncret = NF90_DEF_DIM(ncid,'receptors',numreceptor,ncrepid)
    call check_ncerror(ncret)

    ! Select which dimensions to use for main output grids
    if ((ldirect.eq.1).and.(maxpointspec_act.gt.1)) then
      ncdimsid32(1) = nclonid ! X
      ncdimsid32(2) = nclatid ! Y
      ncdimsid32(3) = nclvlid ! Z
      ncdimsid32(4) = ncrelid ! points
      ncdimsid32(5) = ncspcid ! species
      ncdimsid32(6) = ncageid ! ageclass
      ncdimsid32(7) = ncrecid ! t

      ncdimsid22(1) = nclonid ! X
      ncdimsid22(2) = nclatid ! Y
      ncdimsid22(3) = ncrelid ! points
      ncdimsid22(4) = ncspcid ! species
      ncdimsid22(5) = ncageid ! ageclass
      ncdimsid22(6) = ncrecid ! t
    else
      ncdimsid3(1) = nclonid ! X
      ncdimsid3(2) = nclatid ! Y
      ncdimsid3(3) = nclvlid ! Z
      if (ldirect.eq.1) ncdimsid3(4) = ncspcid ! species
      if (ldirect.eq.-1) ncdimsid3(4) = ncrelid ! points
      ncdimsid3(5) = ncageid ! ageclass
      ncdimsid3(6) = ncrecid ! t

      ncdimsid2(1) = nclonid ! X
      ncdimsid2(2) = nclatid ! Y
      if (ldirect.eq.1) ncdimsid2(3) = ncspcid ! species
      if (ldirect.eq.-1) ncdimsid2(3) = ncrelid ! points
      ncdimsid2(4) = ncageid ! ageclass
      ncdimsid2(5) = ncrecid ! t
    endif

    ! TIMES
    if (option_verbose.ge.10) write(*,10) 'TIMES dimension variable'
    #ifdef NETCDF4_OUTPUT
      ncret = NF90_DEF_VAR(ncid,'Times',NF90_CHAR,(/ncstr3id,ncrecid/),ncrecvid,&
          shuffle = shuffle, deflate_level=deflate_level))
    #else
      ncret = NF90_DEF_VAR(ncid,'Times',NF90_CHAR,(/ncstr3id,ncrecid/),ncrecvid)
    #endif
    call check_ncerror(ncret)
    ncret = NF90_PUT_ATT(ncid,ncrecvid,descr, &
        'TIME OF OUTPUT (END OF AVERAGING INTERVAL)')

    ! RECEPTOR POINTS
    ! only write receptors to domain 1 (maybe change this)
    if (nesting_level.eq.0 .and. numreceptor.gt.0) then  
      if (ldirect.eq.1) then  ! Forward run
        if (option_verbose.ge.10) write(*,10) 'RECEPTORS variable'
        if ((iout.eq.1).or.(iout.eq.3).or.(iout.eq.5)) then ! CONCENTRATION OUTPUT?
          #ifdef NETCDF4_OUTPUT
          ncret = NF90_DEF_VAR(ncid,'RECEPTOR_CONCENTRATION', &
              NF90_REAL,(/ncrepid,ncspcid,ncrecid/),ncrcovid, &
              shuffle = shuffle, deflate_level=deflate_level)
          #else
          ncret = NF90_DEF_VAR(ncid,'RECEPTOR_CONCENTRATION', &
            NF90_REAL,(/ncrepid,ncspcid,ncrecid/),ncrcovid)
          #endif
          call check_ncerror(ncret)
          ncret = NF90_PUT_ATT(ncid,ncrcovid,descr, &
              'CONCENTRATION AT RECEPTOR POINTS')
          call check_ncerror(ncret)
          ncret = NF90_PUT_ATT(ncid,ncrcovid,units,'ng m-3')
          call check_ncerror(ncret)
        endif
        
        if ((iout.eq.2).or.(iout.eq.3)) then  ! MIXING RATIO OUTPUT? 
          #ifdef NETCDF4_OUTPUT
          ncret = NF90_DEF_VAR(ncid,'RECEPTOR_MIXINGRATIO', &
              NF90_REAL,(/ncrepid,ncspcid,ncrecid/),ncrmivid,&
              shuffle = shuffle, deflate_level=deflate_level)
          #else
          ncret = NF90_DEF_VAR(ncid,'RECEPTOR_MIXINGRATIO', &
              NF90_REAL,(/ncrepid,ncspcid,ncrecid/),ncrmivid)
          call check_ncerror(ncret)
          #endif
          ncret = NF90_PUT_ATT(ncid,ncrmivid,descr, &
              'MASS MIXING RATIO AT RECEPTOR POINTS')
          call check_ncerror(ncret)
          ncret = NF90_PUT_ATT(ncid,ncrmivid,units,'ppt by mass')
          call check_ncerror(ncret)
        endif

      endif ! ldirect.eq.1
    end if ! nesting_level.eq.0

    ! MAIN OUTPUT VARIABLES
    if ((iout.eq.1).or.(iout.eq.3).or.(iout.eq.5)) then ! OUTPUT CONCENTRATION
      if (option_verbose.ge.10) write(*,10) 'CONC variable'
  !   print*,ncdimsid3
      if ((ldirect.eq.1).and.(maxpointspec_act.gt.1)) then
        #ifdef NETCDF4_OUTPUT
        ncret = NF90_DEF_VAR(ncid,'CONC',NF90_REAL,ncdimsid32,nccovid,&
            shuffle = shuffle, deflate_level=deflate_level)
        #else
        ncret = NF90_DEF_VAR(ncid,'CONC',NF90_REAL,ncdimsid32,nccovid)
        #endif
      else
        #ifdef NETCDF4_OUTPUT
        ncret = NF90_DEF_VAR(ncid,'CONC',NF90_REAL,ncdimsid3,nccovid,&
            shuffle = shuffle, deflate_level=deflate_level)
        #else
        ncret = NF90_DEF_VAR(ncid,'CONC',NF90_REAL,ncdimsid3,nccovid)
        #endif
      endif
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,nccovid,descr, &
        'CONCENTRATION OF AIRBORNE SPECIES')
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,nccovid,coord,coordxy)  ! Coordinate string
      call check_ncerror(ncret)
    endif

    if ((iout.eq.2).or.(iout.eq.3)) then  ! MIXING RATIO
      if (option_verbose.ge.10) write(*,10) 'MIXINGRATIO variable'
    if ((ldirect.eq.1).and.(maxpointspec_act.gt.1)) then
      #ifdef NETCDF4_OUTPUT
      ncret = NF90_DEF_VAR(ncid,'MIXINGRATIO',NF90_REAL,ncdimsid32,ncravid,&
            shuffle = shuffle, deflate_level=deflate_level)
      #else
      ncret = NF90_DEF_VAR(ncid,'MIXINGRATIO',NF90_REAL,ncdimsid32,ncravid)
      #endif
    else
      #ifdef NETCDF4_OUTPUT
      ncret = NF90_DEF_VAR(ncid,'MIXINGRATIO',NF90_REAL,ncdimsid3,ncravid,&
            shuffle = shuffle, deflate_level=deflate_level)
      #else
      ncret = NF90_DEF_VAR(ncid,'MIXINGRATIO',NF90_REAL,ncdimsid3,ncravid)
      #endif
    endif
    ncret = NF90_PUT_ATT(ncid,ncravid,descr, &
        'MASS MIXING RATIO OF AIRBORNE SPECIES')
    call check_ncerror(ncret)
    ncret = NF90_PUT_ATT(ncid,ncravid,coord,coordxy)  ! Coordinate string
    call check_ncerror(ncret)
    endif

    if (ldirect.eq.1) then  ! Forward run
      unit2d = 'pg m-2'
      !unit2dlen = 6
      if (option_verbose.ge.10) write(*,10) 'DRYDEP variable'
  !   write(*,*) ncdimsid2
      if ((ldirect.eq.1).and.(maxpointspec_act.gt.1)) then
        #ifdef NETCDF4_OUTPUT
        ncret = NF90_DEF_VAR(ncid,'DRYDEP',NF90_REAL,ncdimsid2,ncddvid,&
            shuffle = shuffle, deflate_level=deflate_level)
        #else
        ncret = NF90_DEF_VAR(ncid,'DRYDEP',NF90_REAL,ncdimsid22,ncddvid)
        #endif
      else
        #ifdef NETCDF4_OUTPUT
        ncret = NF90_DEF_VAR(ncid,'DRYDEP',NF90_REAL,ncdimsid2,ncddvid,&
            shuffle = shuffle, deflate_level=deflate_level)
        #else
        ncret = NF90_DEF_VAR(ncid,'DRYDEP',NF90_REAL,ncdimsid2,ncddvid)
        #endif
      endif
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,ncddvid,descr, &
          'ACCUMULATED TOTAL DRY DEPOSITION')
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,ncddvid,units,unit2d)   ! unit string
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,ncddvid,coord,coordxy)  ! coordinate string
      call check_ncerror(ncret)

      if (option_verbose.ge.10) write(*,10) 'WETDEP variable'
      if ((ldirect.eq.1).and.(maxpointspec_act.gt.1)) then
        #ifdef NETCDF4_OUTPUT
        ncret = NF90_DEF_VAR(ncid,'WETDEP',NF90_REAL,ncdimsid22,ncwdvid,&
            shuffle = shuffle, deflate_level=deflate_level)
        #else
        ncret = NF90_DEF_VAR(ncid,'WETDEP',NF90_REAL,ncdimsid22,ncwdvid)
        #endif
      else
        #ifdef NETCDF4_OUTPUT
        ncret = NF90_DEF_VAR(ncid,'WETDEP',NF90_REAL,ncdimsid2,ncwdvid,&
            shuffle = shuffle, deflate_level=deflate_level)
        #else
        ncret = NF90_DEF_VAR(ncid,'WETDEP',NF90_REAL,ncdimsid2,ncwdvid)
        #endif
      endif
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,ncwdvid,descr, &
          'ACCUMULATED TOTAL WET DEPOSITION')
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,ncwdvid,units,unit2d)   ! unit string
      call check_ncerror(ncret)
      ncret = NF90_PUT_ATT(ncid,ncwdvid,coord,coordxy)  ! coordinate string
      call check_ncerror(ncret)
      
      ! Add unit attr to mixing ratio and concentration fields
      if (ind_samp.eq.0) then   ! AD: Not sure if this should be done like this in forward runs...
        ncret = NF90_PUT_ATT(ncid,nccovid,units,'ng m-3') !CONC
        call check_ncerror(ncret)
        ncret = NF90_PUT_ATT(ncid,ncravid,units,'ppt by mass')  !MIX
        call check_ncerror(ncret)
      else
        ncret = NF90_PUT_ATT(ncid,nccovid,units,'ppt by mass') !CONC
        call check_ncerror(ncret)
        !    ncret = nf_put_att_text(ncid,ncravid,units,3,'???')  !MIX
        !    call check_ncerror(ncret)
      endif
    else                    ! Backward run
      if ((ind_rel.eq.0).and.(ind_samp.eq.0)) then
        ncret = NF90_PUT_ATT(ncid,nccovid,units,'s') !CONC
      else if ((ind_rel.eq.0).and.(ind_samp.eq.-1)) then
        ncret = NF90_PUT_ATT(ncid,nccovid,units,'s m3 kg-1') !CONC
      else if ((ind_rel.eq.1).and.(ind_samp.eq.0)) then
        ncret = NF90_PUT_ATT(ncid,nccovid,units,'s kg m-3') !CONC
      else if ((ind_rel.eq.1).and.(ind_samp.eq.-1)) then
        ncret = NF90_PUT_ATT(ncid,nccovid,units,'s') !CONC
      end if
      call check_ncerror(ncret)
    endif ! Backward/Forward run

    ! EXIT DEFINE MODE, ENTER DATA MODE
    ncret = NF90_ENDDEF(ncid)
    call check_ncerror(ncret)
    
    return

  10 format('write_ncheader: Setting up ',A)
  end subroutine nc_create_main_outfile

  subroutine nc_create_header_outfile(itime,nesting_level)
  !*****************************************************************************
  !                                                                            *
  !  This routine create the netcdf header file for FLEXPART-WRF               *
  !                                                                            *
  !      Author: A. Dingwell                                                   *
  !                                                                            *
  !      27 May 2013                                                           *
  !                                                                            *
  !  Modifications                                                             *
  !  June 5 2013: J. Brioude: generate a header*nc                             * 
  !  2015-05-04: A. Dingwell: Added receptor points to netcdf output           *
  !  2015-05-07: H.M.J. Barbosa: Receptor coordinates should be in latlon      *
  !                 AD: Use method fix regardless of outgrid_option            *
  !  2015-06-04: A. Dingwell: Updated subroutine to use NetCDF F90 module.     *
  !  2015-06-05: A. Dingwell: Moved global attributes to separate subroutine   *
  !*****************************************************************************
  integer, intent(in) :: itime,stat     ! seconds since simulation start
  integer, intent(in) :: nesting_level  ! 0=main grid (mother) 1=nest (child)
                            ! this is written to be easy to expand if additional 
                            ! are desired in the future

  real(kind=dp) :: jul          ! Julian date
  integer   :: jjjjmmdd,ihmmss  ! date & time as integer
  character :: adate*8,atime*6  ! date and time strings, used for filename

  ! Grid related variables
  real    :: xp1,yp1,xp2,yp2  ! temporary coordinates
  real    :: xsw,xne,ysw,yne,tmpx,tmpy,tmplon,tmplat,xl2,yl2
  integer :: ncgrid_nx,ncgrid_ny        ! nx,ny of current grid
  integer :: ncgrid_dx,ncgrid_dy        ! dx,dy of current grid in m or latlon
  real    :: ncgrid_swlon,ncgrid_swlat  ! SW corner of current grid in latlon
  real    :: ncgrid_nelon,ncgrid_nelat  ! NE corner of current grid in latlon
  real    :: ncgrid_xm0,ncgrid_ym0      ! lower-left grid coord in metres
  real    :: ncgrid_lon0,ncgrid_lat0    ! lower-left grid coord in latlon

  ! Grid related 2D-variables (reassigning these here is a bit inefficient but
  ! it lets us keep a consistent structure of the code, besides it's only once
  ! per output
  real,allocatable,dimension (:,:)  :: ncgrid_oro,ncgrid_area ! of current grid

  ! Iterators
  integer i,j,ix,jy

  ! NETCDF file related variables
  integer nclvlid,nclonid,nclatid,ncrecid,ncspcid,ncageid !outgrid dimension ids
  integer ncrelid,ncrseid                                 ! release points dimension ids
  integer ncrnvid,ncrmvid,ncspvid             ! release points: number,mass,species ids
  integer ncrtvid,ncrxvid,ncryvid,ncrzvid     ! release points: t,x,y,z min/max limits
  integer ncrexvid,ncreyvid,ncrenvid,ncrepid,ncresid   ! Receptor variable ids (move to header?)
  integer nctovid,ncarvid                     ! Topography and grid area variable-ids
  integer ncstr1id,ncstr2id,ncstr3id          ! description string length dimid
  integer nclvlvid,nclonvid,nclatvid,ncspcvid,ncagevid  ! outgrid dimension variables
  integer nclonvid2,nclatvid2
  integer ncdimsid3(6),ncdimsid2(5) ! arrays of dimension ids for outgrid 3D & 2D

  ! NETCDF filename & attribute related variables
  character descr*11,units*5,ncname*29,coord*11,coordxy*10

  ! NETCDF misc variables
  integer ncid    ! local container for netcdf file-id (either ncout or ncoutn)
  integer ncret   ! Return-value of calls to nf_* utils
  
! integer :: chunks(2) ! shuffle

  !hmjb: avoid creation of temporary vectors while calling netcdf
  integer :: ones2(2) = (/1, 1/)
  integer :: xyvec(2)

  !Receptor coordinates
  real :: x_rep_tmp(maxreceptor),y_rep_tmp(maxreceptor)
  real :: x_rep_tmpb,y_rep_tmpb

  ! Attribute notation:
  descr = 'description'
  units = 'units'
  coord = 'coordinates'
  coordxy = 'XLONG XLAT'

  ! Determine current calendar date, needed for the file name
  !**********************************************************
  jul=bdate+real(itime,kind=dp)/86400._dp
  call caldate(jul,jjjjmmdd,ihmmss)
  write(adate,'(i8.8)') jjjjmmdd
  write(atime,'(i6.6)') ihmmss

  !************************
  ! Open header output file
  !************************
! write(ncname,'(A8,I2.2,A1,I8.8,A1,I6.6,A3)') &
!   'flxout_d',nesting_level+1,'_',jjjjmmdd,'_',ihmmss,'.nc' ! filename
  write(ncname,'(A8,I2.2,A3)') &
    'header_d',nesting_level+1,'.nc' ! filename


!  call nf_set_log_level(3)
  if (option_verbose.ge.1) write(*,*) &
    'write_ncheader: creating file: ',path(1)(1:length(1))//ncname
! call nf_set_chunk_cache(32000000)
! ncret = nf_create(path(1)(1:length(1))//ncname, nf_clobber,ncid)
  ncret = NF90_create(path(1)(1:length(1))//ncname, cmode=NF90_NETCDF4,ncid=ncid)
  call check_ncerror(ncret)

  ! Determine which nest/outfile we just created so we can set up the grid
  !***********************************************************************
  if (nesting_level.eq.0) then  ! current grid is main grid
    ncout   = ncid  ! copy current file handle to ncout
    ncgrid_nx = numxgrid
    ncgrid_ny = numygrid
    ncgrid_nelon = outgrid_nelon
    ncgrid_nelat = outgrid_nelat
    ncgrid_swlon = outgrid_swlon
    ncgrid_swlat = outgrid_swlat
    allocate(ncgrid_oro(ncgrid_nx,ncgrid_ny),stat=stat)
    allocate(ncgrid_area(ncgrid_nx,ncgrid_ny),stat=stat)
    ncgrid_oro   = oroout(0:ncgrid_nx-1,0:ncgrid_ny-1)
    ncgrid_area  = area(0:ncgrid_nx-1,0:ncgrid_ny-1)
    if (outgrid_option.eq.1) then ! input was in latlon
      ncgrid_dx = dxoutl
      ncgrid_dy = dyoutl
      ncgrid_lon0 = outlon0
      ncgrid_lat0 = outlat0
    else  ! input was in metres
      ncgrid_dx = dxout
      ncgrid_dy = dyout
      ncgrid_xm0  = out_xm0
      ncgrid_ym0  = out_ym0
    endif
  elseif (nesting_level.eq.1) then  ! current grid is nested
    ncoutn  = ncid  ! copy current file handle to ncoutn
    ncgrid_nx = numxgridn
    ncgrid_ny = numygridn
    ncgrid_nelon = outgridn_nelon
    ncgrid_nelat = outgridn_nelat
    ncgrid_swlon = outgridn_swlon
    ncgrid_swlat = outgridn_swlat
    allocate(ncgrid_oro(ncgrid_nx,ncgrid_ny),stat=stat)
    allocate(ncgrid_area(ncgrid_nx,ncgrid_ny),stat=stat)
    ncgrid_oro   = orooutn(0:ncgrid_nx-1,0:ncgrid_ny-1)
    ncgrid_area  = arean(0:ncgrid_nx-1,0:ncgrid_ny-1)
    if (outgrid_option.eq.1) then ! input was in latlon
      ncgrid_dx = dxoutln
      ncgrid_dy = dyoutln
      ncgrid_lon0 = outlon0n
      ncgrid_lat0 = outlat0n
    else  ! input was in metres
      ncgrid_dx = dxoutn
      ncgrid_dy = dyoutn
      ncgrid_xm0  = out_xm0n
      ncgrid_ym0  = out_ym0n
    endif
  endif

  if (option_verbose.ge.10) &
    write(*,*) 'write_ncheader: ncout,ncoutn=',ncout,ncoutn

  ! Write the header information
  !*****************************

  !ncret = nf_put_att_text(ncout,NF90_GLOBAL,'TITLE',20,version)
  !call check_ncerror(ncret)
  if (ldirect.eq.1) then  ! Forward simulation
    if (option_verbose.ge.10) write(*,10) 'forward simulation attributes'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_START_DATE',ibdate)
    call check_ncerror(ncret)
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_START_TIME',ibtime)
    call check_ncerror(ncret)

    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_END_DATE',iedate)
    call check_ncerror(ncret)
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_END_TIME',ietime)
    call check_ncerror(ncret)
  else                  ! Backward simulation
    if (option_verbose.ge.10) write(*,10) 'backward simulation attributes'
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_START_DATE',iedate)
    call check_ncerror(ncret)
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_START_TIME',ietime)
    call check_ncerror(ncret)

    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_END_DATE',ibdate)
    call check_ncerror(ncret)
    ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SIMULATION_END_TIME',ibtime)
    call check_ncerror(ncret)
  endif

  if (option_verbose.ge.10) write(*,10) 'map projection attributes'
  if (outgrid_option .eq. 1) then
    ncret = &
      NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_PROJECTION','Regular Latit/Longit')
    call check_ncerror(ncret)
  else
    if (map_proj_id.eq.1) then
      ncret = &
        NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_PROJECTION','Lambert conformal')
      call check_ncerror(ncret)
    elseif (map_proj_id.eq.2) then
      ncret = &
        NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_PROJECTION','stereographic')
      call check_ncerror(ncret)
    elseif (map_proj_id.eq.3) then
      ncret = &
        NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_PROJECTION','mercator')
      call check_ncerror(ncret)
    elseif (map_proj_id.eq.4) then
      ncret = &
        NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_PROJECTION','global')
      call check_ncerror(ncret)
    endif
  endif

  ! Write info common model settings
  !*********************************
  if (option_verbose.ge.10) write(*,10) 'common model attributes'

  if (option_verbose.ge.10) write(*,10) 'OUTPUT_INTERVAL'
  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'OUTPUT_INTERVAL',loutstep)
  call check_ncerror(ncret)

  if (option_verbose.ge.10) write(*,10) 'AVERAGING_TIME'
  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'AVERAGING_TIME',loutaver)
  call check_ncerror(ncret)

  if (option_verbose.ge.10) write(*,10) 'AVERAGE_SAMPLING'
  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'AVERAGE_SAMPLING',loutsample)
  call check_ncerror(ncret)

  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'NSPEC',nspec)
  call check_ncerror(ncret)
  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'NUMRECEPTOR',numreceptor)
  call check_ncerror(ncret)
  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'NAGECLASS',nageclass)
  call check_ncerror(ncret)

  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'NUMRELEASES',numpoint)
  call check_ncerror(ncret)

  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'DISPERSION_METHOD',method)
  call check_ncerror(ncret)

  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SUBGRID_TOPOGRAPHY',lsubgrid)
  call check_ncerror(ncret)

  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'CONVECTION_PARAM',lconvection)
  call check_ncerror(ncret)

  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SUBGRID_TOPOGRAPHY',lsubgrid)
  call check_ncerror(ncret)

  ! Write information on output grid setup
  !***************************************
  if (option_verbose.ge.10) write(*,10) 'WEST-EAST_GRID_DIMENSION'
  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'WEST-EAST_GRID_DIMENSION', ncgrid_nx)
  call check_ncerror(ncret)

  if (option_verbose.ge.10) write(*,10) 'SOUTH-NORTH_GRID_DIMENSION'
  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'SOUTH-NORTH_GRID_DIMENSION', ncgrid_ny)
  call check_ncerror(ncret)
  
  if (option_verbose.ge.10) write(*,10) 'BOTTOM-TOP_GRID_DIMENSION'
  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'BOTTOM-TOP_GRID_DIMENSION',numzgrid)

  if (option_verbose.ge.10) write(*,10) 'DX and DY'
  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'DX',ncgrid_dx)
  call check_ncerror(ncret)

  ncret = NF90_PUT_ATT(ncid,NF90_GLOBAL,'DY',ncgrid_dy)
  call check_ncerror(ncret)

  ! Set up netcdf dimensions
  !*************************
  if (option_verbose.ge.10) write(*,10) 'main grid dimensions'

  ncret = NF90_DEF_DIM(ncid,'Time',NF90_UNLIMITED,ncrecid)
  call check_ncerror(ncret)

  ncret = NF90_DEF_DIM(ncid,'DateStrLen',15,ncstr3id) !TODO: WRF format
  call check_ncerror(ncret)

  ncret = NF90_DEF_DIM(ncid,'west_east',ncgrid_nx,nclonid)
  call check_ncerror(ncret)

  ncret = NF90_DEF_DIM(ncid,'south_north',ncgrid_ny,nclatid)
  call check_ncerror(ncret)

  ncret = NF90_DEF_DIM(ncid,'bottom_top',numzgrid,nclvlid)
  call check_ncerror(ncret)

  ncret = NF90_DEF_DIM(ncid,'species',nspec,ncspcid)
  call check_ncerror(ncret)

  ncret = NF90_DEF_DIM(ncid,'SpeciesStrLen',10,ncstr1id)
  call check_ncerror(ncret)

  ncret = NF90_DEF_DIM(ncid,'ageclass',nageclass,ncageid)
  call check_ncerror(ncret)

  if (option_verbose.ge.10) write(*,10) 'release point dimensions'
  ncret = NF90_DEF_DIM(ncid,'releases',numpoint,ncrelid)
  call check_ncerror(ncret)
  
  ncret = NF90_DEF_DIM(ncid,'ReleaseStrLen',45,ncstr2id)
  call check_ncerror(ncret)

  ncret = NF90_DEF_DIM(ncid,'ReleaseStartEnd',2,ncrseid)
  call check_ncerror(ncret)

  if(option_verbose.ge.10) write(*,10) 'receptor point dimensions'
  ncret = NF90_DEF_DIM(ncid,'receptors',numreceptor,ncrepid)
  call check_ncerror(ncret)
  
  ncret = NF90_DEF_DIM(ncid,'ReceptorStrLen',16,ncresid)
  call check_ncerror(ncret)

  ! Select which dimensions to use for main output grids
  ncdimsid3(1) = nclonid ! X
  ncdimsid3(2) = nclatid ! Y
  ncdimsid3(3) = nclvlid ! Z
  if (ldirect.eq.1) ncdimsid3(4) = ncspcid ! species
  if (ldirect.eq.-1) ncdimsid3(4) = ncrelid ! points
  ncdimsid3(5) = ncageid ! ageclass
  ncdimsid3(6) = ncrecid ! t

  ncdimsid2(1) = nclonid ! X
  ncdimsid2(2) = nclatid ! Y
  if (ldirect.eq.1) ncdimsid2(3) = ncspcid ! species
  if (ldirect.eq.-1) ncdimsid2(3) = ncrelid ! points
  ncdimsid2(4) = ncageid ! ageclass
  ncdimsid2(5) = ncrecid ! t

  ! Set up dimension variables
  !***************************

  ! XLONG
  if (option_verbose.ge.10) write(*,10) 'XLONG dimension variable'
  #ifdef NETCDF4_OUTPUT
    ncret = NF90_DEF_VAR(ncid,'XLONG',NF90_REAL,ncdimsid2(1:2),nclonvid, &
        shuffle = shuffle, deflate_level=deflate_level)
  #else
    ncret = NF90_DEF_VAR(ncid,'XLONG',NF90_REAL,ncdimsid2(1:2),nclonvid)
  #endif
  call check_ncerror(ncret)
  ncret = NF90_PUT_ATT(ncid,nclonvid,descr, &
      'Longitude of center grid, west is negative')
  call check_ncerror(ncret)
  ncret = NF90_PUT_ATT(ncid,nclonvid,units,'degree_east')
  call check_ncerror(ncret)
  
  #ifdef NETCDF4_OUTPUT
    ncret = NF90_DEF_VAR(ncid,'XLONG_CORNER',NF90_REAL,ncdimsid2(1:2),nclonvid2, &
        shuffle = shuffle, deflate_level=deflate_level)
  #else
    ncret = NF90_DEF_VAR(ncid,'XLONG_CORNER',NF90_REAL,ncdimsid2(1:2),nclonvid2)
  #endif
  call check_ncerror(ncret)
  ncret = NF90_PUT_ATT(ncid,nclonvid2,descr, &
      'Longitude of lower left corner of grids, west is negative')
  call check_ncerror(ncret)
  ncret = NF90_PUT_ATT(ncid,nclonvid2,units,'degree_east')
  call check_ncerror(ncret)

  ! XLAT
  if (option_verbose.ge.10) write(*,10) 'XLAT dimension variable'
  #ifdef NETCDF4_OUTPUT
    ncret = NF90_DEF_VAR(ncid,'XLAT',NF90_REAL,ncdimsid2(1:2),nclatvid, &
        shuffle = shuffle, deflate_level=deflate_level)
  #else
    ncret = NF90_DEF_VAR(ncid,'XLAT',NF90_REAL,ncdimsid2(1:2),nclatvid)
  #endif
  call check_ncerror(ncret)
  ncret = NF90_PUT_ATT(ncid,nclatvid,descr,'Latitude of center grid, south is negative')
  call check_ncerror(ncret)
For faster browsing, not all history is shown. View entire blame