OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ncinfo.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004, 2009 Remik Ziemlinski <first d0t surname att n0aa d0t g0v>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2, or (at your option)
7  any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; see the file COPYING.
16  If not, write to the Free Software Foundation,
17  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 #include "ncinfo.h"
21 #include "strlist.h"
22 
23 int ncnonrecvars(int ncid, char** list, int nlist) {
24  int status;
25  int recid;
26  int varid;
27  int dimids[NC_MAX_VAR_DIMS];
28  int ndims;
29  int nvars;
30  int natts;
31  int i;
32  nc_type type;
33  char name[NC_MAX_NAME];
34 
35  /* Query the number of variables */
36  status = nc_inq_nvars(ncid, &nvars);
37  if (status != NC_NOERR) return EXIT_FATAL;
38 
39  /* record dimension id */
40  status = nc_inq_unlimdim(ncid, &recid);
41  if (status != NC_NOERR) {
42  /* no record dimension */
43  recid = -1;
44  }
45 
46  for (i = 0; i < nvars; ++i) {
47  /* Query the variable */
48  varid = i;
49  status = nc_inq_var(ncid, varid, name, &type,
50  &ndims, dimids, &natts);
51  if (status != NC_NOERR) return EXIT_FATAL;
52 
53  if (hasrecdim(dimids, ndims, recid) == 0) {
54  addstringtolist(list, name, nlist);
55  }
56  }
57 
58  return EXIT_SUCCESS;
59 }
60 
61 int ncrecvars(int ncid, char** list, int nlist) {
62  int status;
63  int recid;
64  int varid;
65  int dimids[NC_MAX_VAR_DIMS];
66  int ndims;
67  int nvars;
68  int natts;
69  int i;
70  nc_type type;
71  char name[NC_MAX_NAME];
72 
73  /* Query the number of variables */
74  status = nc_inq_nvars(ncid, &nvars);
75  if (status != NC_NOERR) return EXIT_FATAL;
76 
77  /* record dimension id */
78  status = nc_inq_unlimdim(ncid, &recid);
79 
80  for (i = 0; i < nvars; ++i) {
81  /* Query the variable */
82  varid = i;
83  status = nc_inq_var(ncid, varid, name, &type,
84  &ndims, dimids, &natts);
85  if (status != NC_NOERR) return EXIT_FATAL;
86 
87  if (hasrecdim(dimids, ndims, recid))
88  addstringtolist(list, name, nlist);
89  }
90 
91  return EXIT_SUCCESS;
92 }
93 
94 int ncallvars(int ncid, char** list, int nlist) {
95  int nrecvars;
96  int nnonrecvars;
97  char** nonrecvars = NULL;
98  char** recvars = NULL;
99  int status;
100 
102  newstringlist(&nonrecvars, &nnonrecvars, NC_MAX_VARS);
103  newstringlist(&recvars, &nrecvars, NC_MAX_VARS);
104 
105  if (ncnonrecvars(ncid, nonrecvars, nnonrecvars)) {
106  status = EXIT_FATAL;
107  goto recover;
108  }
109  if (ncrecvars(ncid, recvars, nrecvars)) {
110  status = EXIT_FATAL;
111  goto recover;
112  }
113 
114  /*printf("Nonrecvars:\n");
115  printstrlist(nonrecvars, nnonrecvars, stdout);
116 
117  printf("Recvars:\n");
118  printstrlist(recvars, nrecvars, stdout);
119 
120  printf("List (nlist=%d):\n", nlist);
121  printstrlist(list, nlist, stdout);
122  */
123 
124  if (strlistu(nonrecvars, recvars, list, nnonrecvars, nrecvars, nlist)) {
125  status = EXIT_FATAL;
126  goto recover;
127  }
128 
129  /* printf("Nonrecvars 2:\n");
130  printstrlist(nonrecvars, nnonrecvars, stderr);
131 
132  printf("Recvars 2:\n");
133  printstrlist(recvars, nrecvars, stderr);
134  */
135 
136 recover:
137  freestringlist(&recvars, nrecvars);
138  freestringlist(&nonrecvars, nnonrecvars);
139 
140  /* printf("ncallvars, nlist = %d\n", nlist); */
141 
142  return status;
143 }
144 
145 int ncrecinfo(int ncid, int* recid, char* name, size_t* size) {
146  int status;
147 
148  /* get ID of unlimited dimension */
149  status = nc_inq_unlimdim(ncid, recid);
150  if (status != NC_NOERR) return EXIT_FATAL;
151 
152  if (*recid < 0) {
153  /* no record */
154  strcpy(name, "");
155  *size = 0;
156  return EXIT_SUCCESS;
157  }
158 
159  /* Query the dimension */
160  status = nc_inq_dim(ncid, *recid, name, size);
161  if (status != NC_NOERR) return EXIT_FATAL;
162 
163  return EXIT_SUCCESS;
164 }
165 
166 int hasrecdim(int* dimids, int ndims, int recid) {
167  int i;
168 
169  if ((ndims < 1) || (recid < 0))
170  return 0;
171 
172  for (i = 0; i < ndims; ++i) {
173  if (dimids[i] == recid) {
174  return 1;
175  }
176  }
177 
178  return 0;
179 }
180 
181 int gettypelength(nc_type type) {
182  switch (type) {
183  case NC_BYTE:
184  case NC_CHAR:
185  return 1;
186  break;
187  case NC_SHORT:
188  return 2;
189  break;
190  case NC_INT:
191  case NC_FLOAT:
192  return 4;
193  break;
194  case NC_DOUBLE:
195  case NC_INT64:
196  case NC_UINT64:
197  return 8;
198  break;
199  }
200  return 0;
201 }
int addstringtolist(char **list, char *string, int nitems)
Definition: strlist.c:129
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
int status
Definition: l1_czcs_hdf.c:32
list(APPEND LIBS ${PGSTK_LIBRARIES}) add_executable(atteph_info_modis atteph_info_modis.c) target_link_libraries(atteph_info_modis $
Definition: CMakeLists.txt:7
#define NULL
Definition: decode_rs.h:63
int ncrecvars(int ncid, char **list, int nlist)
Definition: ncinfo.c:61
int newstringlist(char ***list, int *n, int size)
Definition: strlist.c:22
void freestringlist(char ***list, int nitems)
Definition: strlist.c:88
int ncnonrecvars(int ncid, char **list, int nlist)
Definition: ncinfo.c:23
int ncrecinfo(int ncid, int *recid, char *name, size_t *size)
Definition: ncinfo.c:145
int ncallvars(int ncid, char **list, int nlist)
Definition: ncinfo.c:94
int hasrecdim(int *dimids, int ndims, int recid)
Definition: ncinfo.c:166
#define EXIT_FATAL
Definition: common.h:85
int strlistu(char **list1, char **list2, char **listunion, int n1, int n2, int nu)
Definition: strlist.c:171
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
int gettypelength(nc_type type)
Definition: ncinfo.c:181