34 type(GD_NC_VARIABLE), intent(out):: var
35 type(GD_NC_VARIABLE_SEARCH), intent(in):: entry
36 type(GD_NC_VARIABLE_ENTRY), allocatable:: tmp_table(:)
37 integer:: i, n
38
39
40 if (.not. allocated(gdnctab)) then
41 allocate(gdnctab(gdnctab_init_size), stat=result)
42 if (result /= 0) goto 999
43 do, i = 1, gdnctab_init_size
44 gdnctab(i)%fileid = 0
45 gdnctab(i)%varid = 0
46 gdnctab(i)%dimid = 0
47 gdnctab(i)%attrid = 0
48 nullify(gdnctab(i)%dimids)
49 enddo
50 endif
51
52 do, i = 1, size(gdnctab)
53 if (gdnctab(i)%fileid == entry%fileid &
54 & .and. gdnctab(i)%varid == entry%varid &
55 & .and. gdnctab(i)%dimid == entry%dimid) then
56 var = gd_nc_variable(i)
57 result = nf90_noerr
58 call dbgmessage('gtdata_netcdf_internal.add: found %d', i=(/i/))
59 return
60 endif
61 enddo
62
63
64 var = gd_nc_variable(-1)
65 do, i = 1, size(gdnctab)
66 if (gdnctab(i)%fileid == 0) then
67 var = gd_nc_variable(i)
68 exit
69 endif
70 enddo
71 if (var%id == -1) then
72
73 n = size(gdnctab)
74 allocate(tmp_table(n), stat=result)
75 if (result /= 0) goto 999
76 tmp_table(:) = gdnctab(:)
77 deallocate(gdnctab, stat=result)
78 if (result /= 0) goto 999
79 allocate(gdnctab(n * 2), stat=result)
80 if (result /= 0) goto 999
81 gdnctab(1:n) = tmp_table(1:n)
82 deallocate(tmp_table, stat=result)
83 if (result /= 0) goto 999
84
85 gdnctab(n+2)%fileid = 0
86 gdnctab(n+2)%varid = 0
87 gdnctab(n+2)%dimid = 0
88 gdnctab(n+2)%attrid = 0
89 nullify(gdnctab(n+2)%dimids)
90 gdnctab(n+3: n*2) = gdnctab(n+2)
91
92 var = gd_nc_variable(n + 1)
93 endif
94 gdnctab(var%id)%fileid = entry%fileid
95 gdnctab(var%id)%varid = entry%varid
96 gdnctab(var%id)%dimid = entry%dimid
97
98
99 call internal_build_dimids(gdnctab(var%id), result)
100 if (result /= nf90_noerr) goto 999
101
102 result = nf90_noerr
103 call dbgmessage('gtdata_netcdf_internal.add: added %d', i=(/var%id/))
104 return
105
106999 continue
107 var = gd_nc_variable(-1)
108 result = nf90_enomem
109 return
110
111 contains
112
113 subroutine internal_build_dimids(ent, stat)
114
115
116 type(GD_NC_VARIABLE_ENTRY), intent(inout):: ent
117 integer, intent(out):: stat
118 integer:: ndims
119 if (ent%varid > 0) then
120 stat = nf90_inquire_variable(ent%fileid, ent%varid, ndims = ndims)
121 if (stat /= nf90_noerr) return
122 if ((ent%dimid > 0) .and. (ndims /= 1)) goto 100
123 if (ndims == 0) then
124 nullify(ent%dimids)
125 stat = nf90_noerr
126 return
127 endif
128 allocate(ent%dimids(ndims), stat=stat)
129 if (stat /= 0) then
130 stat = nf90_enomem
131 return
132 endif
133 stat = nf90_inquire_variable(ent%fileid, ent%varid, dimids = ent%dimids)
134 if (stat /= nf90_noerr) return
135 if ((ent%dimid > 0) .and. (ent%dimids(1) /= ent%dimid)) then
136 deallocate(ent%dimids)
137 goto 100
138 endif
139 else
140 allocate(ent%dimids(1), stat=stat)
141 if (stat /= 0) then
142 stat = nf90_enomem
143 return
144 endif
145 ent%dimids(1) = ent%dimid
146 endif
147 stat = nf90_noerr
148 return
149
150100 continue
151 ent%varid = 0
152 allocate(ent%dimids(1))
153 ent%dimids(1) = ent%dimid
154 end subroutine internal_build_dimids
155