NetCDF-Fortran  4.4.3
nf_lib.c
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 
17  modified by: Richard Weed Ph.D.
18  Center for Advanced Vehicular Systems
19  Mississippi State University
20  rweed@cavs.msstate.edu
21 */
22 
23 #include <config.h>
24 #include <stddef.h> /* for NULL */
25 #include <errno.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include "netcdf.h"
30 
31 #ifdef USE_NETCDF4
32 /* These appear to only be defined in netcdf-4*/
33 
34 /* Get the varids for a fortran function (i.e. add 1 to each
35  * varid.) */
36 extern int
37 nc_inq_varids_f(int ncid, int *nvars, int *fvarids)
38 {
39  int *varids, nvars1;
40  int i, ret = NC_NOERR;
41 
42  /* Get the information from the C library. */
43  if ((ret = nc_inq_varids(ncid, &nvars1, NULL)))
44  return ret;
45  if (!(varids = malloc(nvars1 * sizeof(int))))
46  return NC_ENOMEM;
47  if ((ret = nc_inq_varids(ncid, NULL, varids)))
48  goto exit;
49 
50  /* Add one to each, for fortran. */
51  for (i = 0; i < nvars1; i++)
52  fvarids[i] = varids[i] + 1;
53 
54  /* Tell the user how many there are. */
55  if (nvars)
56  *nvars = nvars1;
57 
58  exit:
59  free(varids);
60  return ret;
61 }
62 /* Get the dimids for a fortran function (i.e. add 1 to each
63  * dimid.) */
64 extern int
65 nc_inq_dimids_f(int ncid, int *ndims, int *fdimids, int parent)
66 {
67  int *dimids, ndims1;
68  int i, ret = NC_NOERR;
69 
70  /* Get the information from the C library. */
71  if ((ret = nc_inq_dimids(ncid, &ndims1, NULL, parent)))
72  return ret;
73  if (!(dimids = malloc(ndims1 * sizeof(int))))
74  return NC_ENOMEM;
75  if ((ret = nc_inq_dimids(ncid, NULL, dimids, parent)))
76  goto exit;
77 
78  /* Add one to each, for fortran. */
79  for (i = 0; i < ndims1; i++)
80  fdimids[i] = dimids[i] + 1;
81 
82  /* Tell the user how many there are. */
83  if (ndims)
84  *ndims = ndims1;
85 
86  exit:
87  free(dimids);
88  return ret;
89 }
90 
91 /* Swap the dim sizes for fortran. */
92 extern int
93 nc_insert_array_compound_f(int ncid, int typeid, char *name,
94  size_t offset, nc_type field_typeid,
95  int ndims, int *dim_sizesp)
96 {
97  int *dim_sizes_f;
98  int i, ret;
99 
100  if (ndims <= 0)
101  return NC_EINVAL;
102 
103  /* Allocate some storage to hold ids. */
104  if (!(dim_sizes_f = malloc(ndims * sizeof(int))))
105  return NC_ENOMEM;
106 
107  /* Create a backwards list of dimension sizes. */
108  for (i = 0; i < ndims; i++)
109  dim_sizes_f[i] = dim_sizesp[ndims - i - 1];
110 
111  /* Call with backwards list. */
112  ret = nc_insert_array_compound(ncid, typeid, name, offset, field_typeid,
113  ndims, dim_sizes_f);
114 
115  /* Clean up. */
116  free(dim_sizes_f);
117  return ret;
118 }
119 
120 extern int
121 nc_inq_compound_field_f(int ncid, nc_type xtype, int fieldid, char *name,
122  size_t *offsetp, nc_type *field_typeidp, int *ndimsp,
123  int *dim_sizesp)
124 {
125  int ndims;
126  int ret;
127 
128  /* Find out how many dims. */
129  if ((ret = nc_inq_compound_field(ncid, xtype, fieldid, NULL, NULL,
130  NULL, &ndims, NULL)))
131  return ret;
132 
133  /* Call the function. */
134  if ((ret = nc_inq_compound_field(ncid, xtype, fieldid, name, offsetp,
135  field_typeidp, ndimsp, dim_sizesp)))
136  return ret;
137 
138  /* Swap the order of the dimsizes. */
139  if (ndims)
140  {
141  int *f, *b, temp;
142  for (f = dim_sizesp, b = &dim_sizesp[ndims - 1]; f < b; f++, b--)
143  {
144  temp = *f;
145  *f = *b;
146  *b = temp;
147  }
148  }
149 
150  return NC_NOERR;
151 }
152 
153 #endif /*USE_NETCDF4*/
154 
155 /*
156  add a dummy nc_rename_grp function if it is not supported. This is include
157  here so we can build/test with netCDF < version 4.3.1 without
158 */
159 
160 #ifndef NC_HAVE_RENAME_GRP
161 extern int
162 nc_rename_grp(int ncid, const char *name)
163 {
164  printf("\n*** Warning - nc_rename_grp not supported in this netCDF version\n");
165  printf("*** Update your netCDF C libraries to version 4.3.1 or higher\n");
166 
167  return NC_ENOGRP;
168 
169 }
170 #endif
171 

Return to the Main Unidata NetCDF page.
Generated on Sun Mar 27 2016 13:46:12 for NetCDF-Fortran. NetCDF is a Unidata library.