Line data Source code
1 : !> \file mo_edk_write.f90
2 : !> \brief \copybrief mo_edk_write
3 : !> \details \copydetails mo_edk_write
4 :
5 : !> \brief Module to write out edk results.
6 : !> \copyright Copyright 2005-\today, the CHS Developers, Sabine Attinger: All rights reserved.
7 : !! EDK is released under the LGPLv3+ license \license_note
8 : module mo_edk_write
9 :
10 : implicit none
11 :
12 : PRIVATE
13 :
14 : PUBLIC :: open_netcdf
15 :
16 : CONTAINS
17 :
18 : !> \brief Open the output netcdf file.
19 2 : subroutine open_netcdf(nc, var_data, var_time)
20 :
21 : use mo_kind, only: i4, sp, dp
22 : use mo_netcdf, only: NcDataset, NcDimension, NcVariable
23 : use mo_string_utils, only: num2str
24 : use mainVar, only: gridMeteo, yStart, mStart, dStart, DEMNcFlag, DataConvertFactor, nodata_sp
25 : use NetCDFVar, only: fileOut, variable_name, variable_unit, variable_long_name, &
26 : invert_y, variable_standard_name, variable_calendar_type, &
27 : ncOut_dem_Latitude, ncOut_dem_Longitude, &
28 : originator, contact, crs, title, institution, source
29 : use mo_edk_info, only: version
30 :
31 : implicit none
32 :
33 : type(NcDataset), intent(out) :: nc !< opened netcdf file
34 : type(NcVariable), intent(out) :: var_data !< netcdf data variable
35 : type(NcVariable), intent(out) :: var_time !< netcdf time variable
36 :
37 : type(NcDimension) :: dim_x, dim_y, dim_time
38 : type(NcVariable) :: var_east, var_north, var_lat, var_lon
39 : integer(i4) :: i, f
40 2 : real(dp), allocatable :: dummy(:, :)
41 2 : real(dp), allocatable :: dummy_lat(:)
42 :
43 : character(128) :: date, time, datetime
44 : ! 1.1 create a file
45 2 : nc = NcDataset(trim(fileOut), "w")
46 :
47 2 : if (DEMNcFlag == 1) then
48 0 : dim_y = nc%setDimension(ncOut_dem_Latitude, gridMeteo%ncols)
49 0 : dim_x = nc%setDimension(ncOut_dem_Longitude, gridMeteo%nrows)
50 0 : dim_time = nc%setDimension("time", -1)
51 : else
52 : ! create dimensions
53 2 : dim_x = nc%setDimension("x", gridMeteo%ncols)
54 2 : dim_y = nc%setDimension("y", gridMeteo%nrows)
55 2 : dim_time = nc%setDimension("time", -1)
56 : end if
57 :
58 : ! create variables
59 4 : var_time = nc%setVariable('time', "i32", (/dim_time/))
60 : ! add some variable attributes
61 : call var_time%setAttribute("units", "days since " // trim(num2str(yStart, form='(I4)')) // "-"// &
62 : trim(num2str(mStart, form='(I0.2)')) // "-" // &
63 2 : trim(num2str(dStart, form='(I0.2)')) // " " // "00:00:00")
64 2 : call var_time%setAttribute("calendar", variable_calendar_type)
65 :
66 2 : if (DEMNcFlag == 1) then
67 :
68 0 : if (invert_y) then
69 :
70 0 : allocate(dummy(gridMeteo%nrows, gridMeteo%ncols))
71 0 : f = 1
72 0 : do i = 1, gridMeteo%ncols
73 0 : dummy(:,gridMeteo%ncols - i + 1) = gridMeteo%northing(:,f)
74 0 : f = f + 1
75 : end do
76 0 : var_north = nc%setVariable('northing', 'f32', (/dim_x, dim_y/))
77 0 : call var_north%setAttribute("standard_name", "northing")
78 0 : call var_north%setAttribute("units", "m")
79 0 : call var_north%setData(dummy)
80 :
81 : else
82 0 : var_north = nc%setVariable('northing', 'f32', (/dim_x, dim_y/))
83 0 : call var_north%setData(gridMeteo%northing)
84 0 : call var_north%setAttribute("standard_name", "northing")
85 0 : call var_north%setAttribute("units", "m")
86 : end if
87 :
88 0 : var_east = nc%setVariable('easting', 'f32', (/dim_x, dim_y/))
89 0 : call var_east%setData(gridMeteo%easting)
90 0 : call var_east%setAttribute("standard_name", "easting")
91 0 : call var_east%setAttribute("units", "m")
92 :
93 0 : if (invert_y) then
94 0 : allocate(dummy_lat(gridMeteo%ncols))
95 0 : f = 1
96 0 : do i = 1, gridMeteo%ncols
97 0 : dummy_lat(gridMeteo%ncols - i + 1) = gridMeteo%latitude(f)
98 0 : f = f + 1
99 : end do
100 0 : var_lat = nc%setVariable(ncOut_dem_Latitude, 'f32', (/dim_y/))
101 0 : call var_lat%setAttribute("standard_name", "latitude")
102 0 : call var_lat%setAttribute("units", "degrees_north")
103 0 : call var_lat%setData(dummy_lat)
104 : else
105 0 : var_lat = nc%setVariable(ncOut_dem_Latitude, 'f32', (/dim_y/))
106 0 : call var_lat%setAttribute("standard_name", "latitude")
107 0 : call var_lat%setAttribute("units", "degrees_north")
108 0 : call var_lat%setData(gridMeteo%latitude)
109 : end if
110 0 : var_lon = nc%setVariable(ncOut_dem_Longitude, 'f32', (/dim_x/))
111 0 : call var_lon%setAttribute("standard_name", "longitude")
112 0 : call var_lon%setAttribute("units", "degrees_east")
113 0 : call var_lon%setData(gridMeteo%longitude)
114 :
115 : else
116 8 : allocate(dummy(gridMeteo%ncols, gridMeteo%nrows))
117 2 : if (invert_y) then
118 : f = 1
119 22 : do i = gridMeteo%nrows, 1, -1
120 140 : dummy(:, i) = gridMeteo%yllcorner + (real(f,dp) - 0.5_dp) * gridMeteo%cellsize
121 22 : f = f + 1
122 : end do
123 : else
124 0 : do i = 1, gridMeteo%nrows
125 0 : dummy(:, i) = gridMeteo%yllcorner + (real(i,dp) - 0.5_dp) * gridMeteo%cellsize
126 : end do
127 : end if
128 6 : var_north = nc%setVariable('northing', "f32", (/dim_x, dim_y/))
129 2 : call var_north%setAttribute("standard_name", "northing")
130 2 : call var_north%setAttribute("units", "m")
131 2 : call var_north%setData(dummy)
132 :
133 14 : do i = 1, gridMeteo%ncols
134 134 : dummy(i, :) = gridMeteo%xllcorner + (real(i,dp) - 0.5_dp) * gridMeteo%cellsize
135 : end do
136 6 : var_east = nc%setVariable('easting', "f32", (/dim_x, dim_y/))
137 2 : call var_east%setAttribute("standard_name", "easting")
138 2 : call var_east%setAttribute("units", "m")
139 2 : call var_east%setData(dummy)
140 2 : deallocate(dummy)
141 2 : if( allocated(dummy_lat) ) deallocate(dummy_lat)
142 :
143 : end if
144 :
145 8 : var_data = nc%setVariable(variable_name, "f32", (/dim_x, dim_y, dim_time/))
146 : ! add some more variable attributes
147 2 : call var_data%setFillValue(nodata_sp)
148 2 : call var_data%setAttribute("units", trim(variable_unit))
149 2 : call var_data%setAttribute("long_name", trim(variable_long_name))
150 2 : call var_data%setAttribute("standard_name",trim(variable_standard_name))
151 2 : call var_data%setAttribute("scale_factor", 1.0_sp)
152 2 : call var_data%setAttribute("missing_value", nodata_sp)
153 :
154 : ! fixed global attributes
155 2 : call nc%setAttribute("Conventions", "CF-1.8")
156 : ! call nc%setAttribute("creation_date", call date() )
157 : ! edk version...
158 2 : call nc%setAttribute("edk-version", trim(version))
159 :
160 : ! add global attributes
161 2 : call nc%setAttribute("institution", trim(institution))
162 2 : call nc%setAttribute("title", trim(title))
163 2 : call nc%setAttribute("source", trim(source))
164 2 : call nc%setAttribute("originator", trim(originator))
165 2 : call nc%setAttribute("contact", trim(contact))
166 2 : call nc%setAttribute("crs", trim(crs))
167 :
168 2 : call date_and_time(date = date, time = time)
169 2 : write(datetime, "(a4,'-',a2,'-',a2,1x,a2,':',a2,':',a2)") date(1 : 4), &
170 4 : date(5 : 6), date(7 : 8), time(1 : 2), time(3 : 4), time(5 : 6)
171 2 : call nc%setAttribute("creation_date", datetime)
172 2 : end subroutine open_netcdf
173 :
174 : end module mo_edk_write
|