NetCDF-Fortran  4.4.4
nf_lib.c
Go to the documentation of this file.
1 /*
2 
3 Copyright 2006, University Corporation for Atmospheric Research. See
4 the COPYRIGHT file for copying and redistribution conditions.
5 
6 $Id: fort-lib.c,v 1.15 2009/02/13 15:58:00 ed Exp $
7 */
8 
9 /*
10  Extracted from of fort-lib.c. Used to supply four required netcdf4
11  functions used in Fortran2003 interfaces. These need to be external
12  and I don't want to mangle fort-lib.c just to define these four functions
13 
14  Version 1.0, April, 2009 - based on netcdf-4.0.1 source
15  Version 2.0, April, 2010 - based on netcdf-4.1.1 source
16  Version 3.0, Jan. 2016 - added functions to return number of
17  groups, types, and compound field
18  dim_sizes. Needed to support move to
19  allocatable arrays for starts, strides, etc
20 
21  modified by: Richard Weed Ph.D.
22  Center for Advanced Vehicular Systems
23  Mississippi State University
24  rweed@cavs.msstate.edu
25 */
26 
27 #include <config.h>
28 #include <stddef.h> /* for NULL */
29 #include <errno.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include "netcdf.h"
34 
35 #ifdef USE_NETCDF4
36 /* These appear to only be defined in netcdf-4*/
37 
38 /* Get the varids for a fortran function (i.e. add 1 to each
39  * varid.) */
40 extern int
41 nc_inq_varids_f(int ncid, int *nvars, int *fvarids)
42 {
43  int *varids, nvars1;
44  int i, ret = NC_NOERR;
45 
46  /* Get the information from the C library. */
47  if ((ret = nc_inq_varids(ncid, &nvars1, NULL)))
48  return ret;
49  if (!(varids = malloc(nvars1 * sizeof(int))))
50  return NC_ENOMEM;
51  if ((ret = nc_inq_varids(ncid, NULL, varids)))
52  goto exit;
53 
54  /* Add one to each, for fortran. */
55  for (i = 0; i < nvars1; i++)
56  fvarids[i] = varids[i] + 1;
57 
58  /* Tell the user how many there are. */
59  if (nvars)
60  *nvars = nvars1;
61 
62  exit:
63  free(varids);
64  return ret;
65 }
66 /* Get the dimids for a fortran function (i.e. add 1 to each
67  * dimid.) */
68 extern int
69 nc_inq_dimids_f(int ncid, int *ndims, int *fdimids, int parent)
70 {
71  int *dimids, ndims1;
72  int i, ret = NC_NOERR;
73 
74  /* Get the information from the C library. */
75  if ((ret = nc_inq_dimids(ncid, &ndims1, NULL, parent)))
76  return ret;
77  if (!(dimids = malloc(ndims1 * sizeof(int))))
78  return NC_ENOMEM;
79  if ((ret = nc_inq_dimids(ncid, NULL, dimids, parent)))
80  goto exit;
81 
82  /* Add one to each, for fortran. */
83  for (i = 0; i < ndims1; i++)
84  fdimids[i] = dimids[i] + 1;
85 
86  /* Tell the user how many there are. */
87  if (ndims)
88  *ndims = ndims1;
89 
90  exit:
91  free(dimids);
92  return ret;
93 }
94 
95 /* Swap the dim sizes for fortran. */
96 extern int
97 nc_insert_array_compound_f(int ncid, int typeid, char *name,
98  size_t offset, nc_type field_typeid,
99  int ndims, int *dim_sizesp)
100 {
101  int *dim_sizes_f;
102  int i, ret;
103 
104  if (ndims <= 0)
105  return NC_EINVAL;
106 
107  /* Allocate some storage to hold ids. */
108  if (!(dim_sizes_f = malloc(ndims * sizeof(int))))
109  return NC_ENOMEM;
110 
111  /* Create a backwards list of dimension sizes. */
112  for (i = 0; i < ndims; i++)
113  dim_sizes_f[i] = dim_sizesp[ndims - i - 1];
114 
115  /* Call with backwards list. */
116  ret = nc_insert_array_compound(ncid, typeid, name, offset, field_typeid,
117  ndims, dim_sizes_f);
118 
119  /* Clean up. */
120  free(dim_sizes_f);
121  return ret;
122 }
123 
124 extern int
125 nc_inq_compound_field_ndims(int ncid, nc_type xtype, int fieldid, int *ndims)
126 {
127  int ret;
128 
129  /* Find out how many dims. */
130  if ((ret = nc_inq_compound_field(ncid, xtype, fieldid, NULL, NULL,
131  NULL, ndims, NULL)))
132  return ret;
133  return NC_NOERR;
134 }
135 
136 extern int
137 nc_inq_compound_field_f(int ncid, nc_type xtype, int fieldid, char *name,
138  size_t *offsetp, nc_type *field_typeidp, int *ndimsp,
139  int *dim_sizesp)
140 {
141  int ndims;
142  int ret;
143 
144  /* Find out how many dims. */
145  if ((ret = nc_inq_compound_field(ncid, xtype, fieldid, NULL, NULL,
146  NULL, &ndims, NULL)))
147  return ret;
148 
149  /* Call the function. */
150  if ((ret = nc_inq_compound_field(ncid, xtype, fieldid, name, offsetp,
151  field_typeidp, ndimsp, dim_sizesp)))
152  return ret;
153 
154  /* Swap the order of the dimsizes. */
155  if (ndims)
156  {
157  int *f, *b, temp;
158  for (f = dim_sizesp, b = &dim_sizesp[ndims - 1]; f < b; f++, b--)
159  {
160  temp = *f;
161  *f = *b;
162  *b = temp;
163  }
164  }
165 
166  return NC_NOERR;
167 }
168 
169 extern int
170 nc_inq_numgrps(int ncid, int *numgrps)
171 {
172  int ret;
173 
174  if ((ret = nc_inq_grps(ncid, numgrps, NULL)))
175 
176  return ret;
177 }
178 
179 extern int
180 nc_inq_numtypes(int ncid, int *numtypes)
181 {
182  int ret;
183 
184  if ((ret = nc_inq_typeids(ncid, numtypes, NULL)))
185 
186  return ret;
187 }
188 
189 #endif /*USE_NETCDF4*/
190 
191 /*
192  add a dummy nc_rename_grp function if it is not supported. This is include
193  here so we can build/test with netCDF < version 4.3.1 without
194 */
195 
196 #ifndef NC_HAVE_RENAME_GRP
197 extern int
198 nc_rename_grp(int ncid, const char *name)
199 {
200  printf("\n*** Warning - nc_rename_grp not supported in this netCDF version\n");
201  printf("*** Update your netCDF C libraries to version 4.3.1 or higher\n");
202 
203  return NC_ENOGRP;
204 
205 }
206 #endif
207 
EXTERNL int nc_insert_array_compound_f(int ncid, int typeid, char *name, size_t offset, nc_type field_typeid, int ndims, int *dim_sizesp)
Definition: fort-lib.c:216
EXTERNL int nc_inq_compound_field_f(int ncid, nc_type xtype, int fieldid, char *name, size_t *offsetp, nc_type *field_typeidp, int *ndimsp, int *dim_sizesp)
Definition: fort-lib.c:244
EXTERNL int nc_inq_varids_f(int ncid, int *nvars, int *fvarids)
Definition: fort-lib.c:159
int nc_rename_grp(int ncid, const char *name)
Definition: nf_lib.c:198
EXTERNL int nc_inq_dimids_f(int ncid, int *ndims, int *fdimids, int parent)
Definition: fort-lib.c:188

Return to the Main Unidata NetCDF page.
Generated on Tue Jul 17 2018 12:21:03 for NetCDF-Fortran. NetCDF is a Unidata library.