1 | program reshape_XIOS_output |
---|
2 | |
---|
3 | !======================================================================= |
---|
4 | ! |
---|
5 | ! Purpose: Read XIOS files, and convert them into the correct GCM grid |
---|
6 | ! XIOS longitudes start at -180 but stop before -180 (not duplicated) |
---|
7 | ! We basically add the last point, and complete the XIOS file. Looped |
---|
8 | ! over the two GCM runs |
---|
9 | ! |
---|
10 | ! Authors: RV & LL |
---|
11 | !======================================================================= |
---|
12 | use netcdf |
---|
13 | implicit none |
---|
14 | integer :: status, ncid, ncid1, ncid2 |
---|
15 | integer :: nDims, nVars, nGlobalAtts, unlimDimID |
---|
16 | integer i,j |
---|
17 | |
---|
18 | integer :: include_parents |
---|
19 | |
---|
20 | integer, dimension(:),allocatable :: dimids |
---|
21 | integer, dimension(:),allocatable :: varids |
---|
22 | |
---|
23 | integer, dimension(:),allocatable :: dimids_2 |
---|
24 | integer, dimension(:),allocatable :: varids_2 |
---|
25 | |
---|
26 | integer, dimension(:),allocatable :: dimid_var |
---|
27 | |
---|
28 | real, dimension(:), allocatable :: tempvalues_1d |
---|
29 | real, dimension(:), allocatable :: values_1d |
---|
30 | |
---|
31 | real, dimension(:,:), allocatable :: tempvalues_2d |
---|
32 | real, dimension(:,:), allocatable :: values_2d |
---|
33 | |
---|
34 | real, dimension(:,:,:), allocatable :: tempvalues_3d |
---|
35 | real, dimension(:,:,:), allocatable :: values_3d |
---|
36 | |
---|
37 | real, dimension(:,:,:,:), allocatable :: tempvalues_4d |
---|
38 | real, dimension(:,:,:,:), allocatable :: values_4d |
---|
39 | |
---|
40 | character*1 str2 |
---|
41 | character*30 :: name_ |
---|
42 | character*30 :: namevar |
---|
43 | integer :: xtype_var |
---|
44 | integer :: len_ |
---|
45 | integer :: len_1,len_2 |
---|
46 | integer :: len_lat, len_lon, len_time, len_soil |
---|
47 | integer :: dimid_lon, dimid_lat, dimid_time, dimid_soil |
---|
48 | integer :: dimid_2 |
---|
49 | integer :: numdims |
---|
50 | integer :: numatts |
---|
51 | integer :: numyear |
---|
52 | |
---|
53 | DO numyear=1, 2 |
---|
54 | write(*,*) 'numyear',numyear |
---|
55 | write(str2(1:1),'(i1.1)') numyear |
---|
56 | !nf90_open ! open existing netCDF dataset |
---|
57 | !integer :: ncid, status |
---|
58 | !... |
---|
59 | status = nf90_open(path = "data2reshape"//str2//".nc", mode = nf90_nowrite, ncid = ncid1) |
---|
60 | if(status /= nf90_noerr) call handle_err(status) |
---|
61 | |
---|
62 | status = nf90_create(path = "datareshaped"//str2//".nc", cmode=or(nf90_noclobber,nf90_64bit_offset), ncid = ncid2) |
---|
63 | if(status /= nf90_noerr) call handle_err(status) |
---|
64 | |
---|
65 | status = nf90_inquire(ncid1, ndims, nvars, nglobalatts, unlimdimid) |
---|
66 | if(status /= nf90_noerr) call handle_err(status) |
---|
67 | |
---|
68 | allocate(dimids(ndims)) |
---|
69 | allocate(varids(nvars)) |
---|
70 | |
---|
71 | allocate(dimids_2(ndims)) |
---|
72 | allocate(varids_2(nvars)) |
---|
73 | |
---|
74 | status = nf90_inq_dimids(ncid1, ndims, dimids, include_parents) |
---|
75 | if(status /= nf90_noerr) call handle_err(status) |
---|
76 | status = nf90_inq_varids(ncid1, nvars, varids) |
---|
77 | if(status /= nf90_noerr) call handle_err(status) |
---|
78 | |
---|
79 | do i=1,ndims |
---|
80 | status = nf90_inquire_dimension(ncid1, dimids(i), name_, len_) |
---|
81 | if(status /= nf90_noerr) call handle_err(status) |
---|
82 | if(name_.eq."lon" .or. name_.eq."longitude") then |
---|
83 | dimid_lon=dimids(i) |
---|
84 | len_lon=len_ |
---|
85 | len_=len_+1 |
---|
86 | elseif(name_.eq."lat".or. name_.eq."latitude") then |
---|
87 | dimid_lat=dimids(i) |
---|
88 | len_lat=len_ |
---|
89 | elseif(name_.eq."time_counter".or. name_.eq. "Time") then |
---|
90 | dimid_time=dimids(i) |
---|
91 | len_time=len_ |
---|
92 | elseif(name_.eq."soil_layers".or. name_.eq. "subsurface_layers") then |
---|
93 | dimid_soil=dimids(i) |
---|
94 | len_soil=len_ |
---|
95 | endif |
---|
96 | status = nf90_def_dim(ncid2, name_, len_, dimid_2) |
---|
97 | if(status /= nf90_noerr) call handle_err(status) |
---|
98 | dimids_2(i)=dimid_2 |
---|
99 | enddo |
---|
100 | |
---|
101 | do i=1,nvars |
---|
102 | status = nf90_inquire_variable(ncid1, varids(i), name=namevar, xtype=xtype_var, ndims = numdims,natts = numatts) |
---|
103 | print *, "namevar00= ", namevar |
---|
104 | if(status /= nf90_noerr) call handle_err(status) |
---|
105 | allocate(dimid_var(numdims)) |
---|
106 | status = nf90_inquire_variable(ncid1, varids(i), name=namevar, xtype=xtype_var, ndims = numdims, dimids=dimid_var, natts = numatts) |
---|
107 | if(status /= nf90_noerr) call handle_err(status) |
---|
108 | if(numdims.eq.1) then |
---|
109 | if(namevar.eq."lon") then |
---|
110 | allocate(tempvalues_1d(len_lon)) |
---|
111 | allocate(values_1d(len_lon+1)) |
---|
112 | status = nf90_get_var(ncid1, varids(i), tempvalues_1d) |
---|
113 | if(status /= nf90_noerr) call handle_err(status) |
---|
114 | status = nf90_def_var(ncid2, namevar, xtype_var, dimid_var, varids_2(i)) |
---|
115 | if(status /= nf90_noerr) call handle_err(status) |
---|
116 | values_1d(1:len_lon)=tempvalues_1d(:) |
---|
117 | values_1d(len_lon+1)=values_1d(1) |
---|
118 | status = nf90_enddef(ncid2) |
---|
119 | if(status /= nf90_noerr) call handle_err(status) |
---|
120 | status = nf90_put_var(ncid2, varids_2(i), values_1d) |
---|
121 | if(status /= nf90_noerr) call handle_err(status) |
---|
122 | status = nf90_redef(ncid2) |
---|
123 | if(status /= nf90_noerr) call handle_err(status) |
---|
124 | deallocate(tempvalues_1d) |
---|
125 | deallocate(values_1d) |
---|
126 | else |
---|
127 | status = nf90_inquire_dimension(ncid1, dimid_var(1), name_, len_) |
---|
128 | if(status /= nf90_noerr) call handle_err(status) |
---|
129 | allocate(tempvalues_1d(len_)) |
---|
130 | status = nf90_get_var(ncid1, varids(i), tempvalues_1d) |
---|
131 | if(status /= nf90_noerr) call handle_err(status) |
---|
132 | status = nf90_def_var(ncid2, namevar, xtype_var, dimid_var, varids_2(i)) |
---|
133 | if(status /= nf90_noerr) call handle_err(status) |
---|
134 | status = nf90_enddef(ncid2) |
---|
135 | if(status /= nf90_noerr) call handle_err(status) |
---|
136 | status = nf90_put_var(ncid2, varids_2(i), tempvalues_1d) |
---|
137 | if(status /= nf90_noerr) call handle_err(status) |
---|
138 | status = nf90_redef(ncid2) |
---|
139 | if(status /= nf90_noerr) call handle_err(status) |
---|
140 | deallocate(tempvalues_1d) |
---|
141 | endif |
---|
142 | elseif(numdims.eq.2) then |
---|
143 | if(namevar.eq."area") then |
---|
144 | allocate(tempvalues_2d(len_lon,len_lat)) |
---|
145 | allocate(values_2d(len_lon+1,len_lat)) |
---|
146 | status = nf90_get_var(ncid1, varids(i), tempvalues_2d) |
---|
147 | if(status /= nf90_noerr) call handle_err(status) |
---|
148 | status = nf90_def_var(ncid2, namevar, xtype_var, dimid_var, varids_2(i)) |
---|
149 | if(status /= nf90_noerr) call handle_err(status) |
---|
150 | values_2d(1:len_lon,:)=tempvalues_2d(:,:) |
---|
151 | values_2d(len_lon+1,:)=values_2d(1,:) |
---|
152 | status = nf90_enddef(ncid2) |
---|
153 | if(status /= nf90_noerr) call handle_err(status) |
---|
154 | status = nf90_put_var(ncid2, varids_2(i), values_2d) |
---|
155 | if(status /= nf90_noerr) call handle_err(status) |
---|
156 | status = nf90_redef(ncid2) |
---|
157 | if(status /= nf90_noerr) call handle_err(status) |
---|
158 | deallocate(tempvalues_2d) |
---|
159 | deallocate(values_2d) |
---|
160 | else |
---|
161 | status = nf90_inquire_dimension(ncid1, dimid_var(1), name_, len_1) |
---|
162 | if(status /= nf90_noerr) call handle_err(status) |
---|
163 | status = nf90_inquire_dimension(ncid1, dimid_var(2), name_, len_2) |
---|
164 | if(status /= nf90_noerr) call handle_err(status) |
---|
165 | allocate(tempvalues_2d(len_1,len_2)) |
---|
166 | status = nf90_get_var(ncid1, varids(i), tempvalues_2d) |
---|
167 | if(status /= nf90_noerr) call handle_err(status) |
---|
168 | status = nf90_def_var(ncid2, namevar, xtype_var, dimid_var, varids_2(i)) |
---|
169 | if(status /= nf90_noerr) call handle_err(status) |
---|
170 | status = nf90_enddef(ncid2) |
---|
171 | if(status /= nf90_noerr) call handle_err(status) |
---|
172 | status = nf90_put_var(ncid2, varids_2(i), tempvalues_2d) |
---|
173 | if(status /= nf90_noerr) call handle_err(status) |
---|
174 | status = nf90_redef(ncid2) |
---|
175 | if(status /= nf90_noerr) call handle_err(status) |
---|
176 | deallocate(tempvalues_2d) |
---|
177 | endif |
---|
178 | elseif(numdims.eq.3) then |
---|
179 | allocate(tempvalues_3d(len_lon,len_lat,len_time)) |
---|
180 | allocate(values_3d(len_lon+1,len_lat,len_time)) |
---|
181 | status = nf90_get_var(ncid1, varids(i), tempvalues_3d) |
---|
182 | if(status /= nf90_noerr) call handle_err(status) |
---|
183 | status = nf90_def_var(ncid2, namevar, xtype_var, dimid_var, varids_2(i)) |
---|
184 | if(status /= nf90_noerr) call handle_err(status) |
---|
185 | values_3d(1:len_lon,:,:)=tempvalues_3d(:,:,:) |
---|
186 | values_3d(len_lon+1,:,:)=values_3d(1,:,:) |
---|
187 | status = nf90_enddef(ncid2) |
---|
188 | if(status /= nf90_noerr) call handle_err(status) |
---|
189 | status = nf90_put_var(ncid2, varids_2(i), values_3d) |
---|
190 | if(status /= nf90_noerr) call handle_err(status) |
---|
191 | status = nf90_redef(ncid2) |
---|
192 | if(status /= nf90_noerr) call handle_err(status) |
---|
193 | deallocate(tempvalues_3d) |
---|
194 | deallocate(values_3d) |
---|
195 | elseif(numdims.eq.4) then |
---|
196 | allocate(tempvalues_4d(len_lon,len_lat,len_soil,len_time)) |
---|
197 | allocate(values_4d(len_lon+1,len_lat,len_soil,len_time)) |
---|
198 | status = nf90_get_var(ncid1, varids(i), tempvalues_4d) |
---|
199 | if(status /= nf90_noerr) call handle_err(status) |
---|
200 | status = nf90_def_var(ncid2, namevar, xtype_var, dimid_var, varids_2(i)) |
---|
201 | if(status /= nf90_noerr) call handle_err(status) |
---|
202 | status = nf90_enddef(ncid2) |
---|
203 | values_4d(1:len_lon,:,:,:)=tempvalues_4d(:,:,:,:) |
---|
204 | values_4d(len_lon+1,:,:,:)=values_4d(1,:,:,:) |
---|
205 | if(status /= nf90_noerr) call handle_err(status) |
---|
206 | status = nf90_put_var(ncid2, varids_2(i), values_4d) |
---|
207 | if(status /= nf90_noerr) call handle_err(status) |
---|
208 | status = nf90_redef(ncid2) |
---|
209 | if(status /= nf90_noerr) call handle_err(status) |
---|
210 | deallocate(tempvalues_4d) |
---|
211 | deallocate(values_4d) |
---|
212 | endif |
---|
213 | |
---|
214 | deallocate(dimid_var) |
---|
215 | enddo |
---|
216 | |
---|
217 | status = nf90_enddef(ncid2) |
---|
218 | if(status /= nf90_noerr) call handle_err(status) |
---|
219 | status = nf90_close(ncid1) |
---|
220 | if(status /= nf90_noerr) call handle_err(status) |
---|
221 | status = nf90_close(ncid2) |
---|
222 | if(status /= nf90_noerr) call handle_err(status) |
---|
223 | |
---|
224 | |
---|
225 | deallocate(dimids) |
---|
226 | deallocate(varids) |
---|
227 | deallocate(dimids_2) |
---|
228 | deallocate(varids_2) |
---|
229 | |
---|
230 | enddo |
---|
231 | |
---|
232 | end program reshape_XIOS_output |
---|
233 | |
---|