netcdf-c/ncgen3/main.c

230 lines
5.5 KiB
C
Raw Normal View History

2010-06-03 21:24:43 +08:00
/*********************************************************************
2018-12-07 06:42:41 +08:00
* Copyright 2018, UCAR/Unidata
2010-06-03 21:24:43 +08:00
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/ncgen3/main.c,v 1.20 2010/03/31 18:18:40 dmh Exp $
*********************************************************************/
2012-08-02 01:18:58 +08:00
#include "config.h"
2010-06-03 21:24:43 +08:00
#include <stdio.h>
2015-02-06 07:33:42 +08:00
#include <stdlib.h>
2010-06-03 21:24:43 +08:00
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
2010-06-03 21:24:43 +08:00
#if defined(_WIN32) && !defined(__MINGW32__)
2012-09-13 04:18:48 +08:00
#include "XGetopt.h"
#endif
2017-04-15 01:05:30 +08:00
#include "netcdf.h"
#include "ncpathmgr.h"
2010-06-03 21:24:43 +08:00
#include "generic.h"
#include "ncgen.h"
#include "genlib.h"
extern int ncgparse(void);
const char *progname; /* for error messages */
const char *cdlname;
int c_flag;
int fortran_flag;
int netcdf_flag;
int cmode_modifier;
int nofill_flag;
char *netcdf_name = NULL; /* name of output netCDF file to write */
extern FILE *ncgin;
extern int derror_count;
static const char* ubasename ( const char* av0 );
static void usage ( void );
int main ( int argc, char** argv );
/* strip off leading path */
static const char *
ubasename(
const char *av0)
{
const char *logident ;
#ifdef VMS
#define SEP ']'
#endif
#ifdef MSDOS
#define SEP '\\'
#endif
#ifndef SEP
#define SEP '/'
#endif
if ((logident = strrchr(av0, SEP)) == NULL)
logident = av0 ;
else
logident++ ;
return logident ;
}
static void usage(void)
{
derror("Usage: %s [ -b ] [ -c ] [ -f ] [ -k kind ] [ -x ] [ -o outfile] [ file ... ]",
progname);
derror("netcdf library version %s", nc_inq_libvers());
}
int
main(
int argc,
char *argv[])
{
int any_error;
int c;
FILE *fp;
#ifdef MDEBUG
malloc_debug(2) ; /* helps find malloc/free errors on Sun */
#endif /* MDEBUG */
progname = ubasename(argv[0]);
cdlname = "-";
c_flag = 0;
fortran_flag = 0;
netcdf_flag = 0;
cmode_modifier = 0;
nofill_flag = 0;
while ((c = getopt(argc, argv, "bcfk:l:no:v:x")) != EOF)
switch(c) {
case 'c': /* for c output, old version of "-lc" */
c_flag = 1;
break;
case 'f': /* for fortran output, old version of "-lf" */
fortran_flag = 1;
break;
case 'b': /* for binary netcdf output, ".nc" extension */
netcdf_flag = 1;
break;
case 'l': /* specify language, instead of using -c or -f */
{
char *lang_name = (char *) emalloc(strlen(optarg)+1);
if (! lang_name) {
derror ("%s: out of memory", progname);
return(1);
}
(void)strcpy(lang_name, optarg);
if (strcmp(lang_name, "c") == 0 || strcmp(lang_name, "C") == 0) {
c_flag = 1;
}
else if (strcmp(lang_name, "f77") == 0 ||
strcmp(lang_name, "fortran77") == 0 ||
strcmp(lang_name, "Fortran77") == 0) {
fortran_flag = 1;
} else { /* Fortran90, Java, C++, Perl, Python, Ruby, ... */
derror("%s: output language %s not implemented",
progname, lang_name);
return(1);
}
}
break;
case 'n': /* old version of -b, uses ".cdf" extension */
netcdf_flag = -1;
break;
case 'o': /* to explicitly specify output name */
netcdf_flag = 1;
netcdf_name = (char *) emalloc(strlen(optarg)+1);
if (! netcdf_name) {
derror ("%s: out of memory", progname);
return(1);
}
(void)strcpy(netcdf_name,optarg);
break;
case 'x': /* set nofill mode to speed up creation of large files */
nofill_flag = 1;
break;
case 'v': /* a deprecated alias for "kind" option */
/*FALLTHRU*/
case 'k': /* for specifying variant of netCDF format to be generated */
{
char *kind_name = (char *) emalloc(strlen(optarg)+1);
if (! kind_name) {
derror ("%s: out of memory", progname);
return(1);
}
(void)strcpy(kind_name, optarg);
2015-08-16 06:26:35 +08:00
/* The default kind is kind 1 (classic), with 32-bit offsets */
2010-06-03 21:24:43 +08:00
if (strcmp(kind_name, "1") == 0 ||
strcmp(kind_name, "classic") == 0) {
2015-08-16 06:26:35 +08:00
cmode_modifier |= NC_CLASSIC_MODEL;
2010-06-03 21:24:43 +08:00
}
/* The 64-bit offset kind (2) should only be used if actually needed */
else if (strcmp(kind_name, "2") == 0 ||
strcmp(kind_name, "64-bit-offset") == 0 ||
strcmp(kind_name, "64-bit offset") == 0) {
cmode_modifier |= NC_64BIT_OFFSET;
}
Add filter support to NCZarr Filter support has three goals: 1. Use the existing HDF5 filter implementations, 2. Allow filter metadata to be stored in the NumCodecs metadata format used by Zarr, 3. Allow filters to be used even when HDF5 is disabled Detailed usage directions are define in docs/filters.md. For now, the existing filter API is left in place. So filters are defined using ''nc_def_var_filter'' using the HDF5 style where the id and parameters are unsigned integers. This is a big change since filters affect many parts of the code. In the following, the terms "compressor" and "filter" and "codec" are generally used synonomously. ### Filter-Related Changes: * In order to support dynamic loading of shared filter libraries, a new library was added in the libncpoco directory; it helps to isolate dynamic loading across multiple platforms. * Provide a json parsing library for use by plugins; this is created by merging libdispatch/ncjson.c with include/ncjson.h. * Add a new _Codecs attribute to allow clients to see what codecs are being used; let ncdump -s print it out. * Provide special headers to help support compilation of HDF5 filters when HDF5 is not enabled: netcdf_filter_hdf5_build.h and netcdf_filter_build.h. * Add a number of new test to test the new nczarr filters. * Let ncgen parse _Codecs attribute, although it is ignored. ### Plugin directory changes: * Add support for the Blosc compressor; this is essential because it is the most common compressor used in Zarr datasets. This also necessitated adding a CMake FindBlosc.cmake file * Add NCZarr support for the big-four filters provided by HDF5: shuffle, fletcher32, deflate (zlib), and szip * Add a Codec defaulter (see docs/filters.md) for the big four filters. * Make plugins work with windows by properly adding __declspec declaration. ### Misc. Non-Filter Changes * Replace most uses of USE_NETCDF4 (deprecated) with USE_HDF5. * Improve support for caching * More fixes for path conversion code * Fix misc. memory leaks * Add new utility -- ncdump/ncpathcvt -- that does more or less the same thing as cygpath. * Add a number of new test to test the non-filter fixes. * Update the parsers * Convert most instances of '#ifdef _MSC_VER' to '#ifdef _WIN32'
2021-09-03 07:04:26 +08:00
#ifdef USE_HDF5
2010-06-03 21:24:43 +08:00
/* NetCDF-4 HDF5 format*/
else if (strcmp(kind_name, "3") == 0 ||
strcmp(kind_name, "hdf5") == 0 ||
strcmp(kind_name, "netCDF-4") == 0) {
cmode_modifier |= NC_NETCDF4;
}
/* NetCDF-4 HDF5 format, but using only nc3 data model */
else if (strcmp(kind_name, "4") == 0 ||
strcmp(kind_name, "hdf5-nc3") == 0 ||
strcmp(kind_name, "netCDF-4 classic model") == 0) {
cmode_modifier |= NC_NETCDF4 | NC_CLASSIC_MODEL;
}
#endif
else
{
derror("Invalid format, try classic, 64-bit offset, netCDF-4, or netCDF-4 classic model");
return 2;
}
2015-02-06 07:33:42 +08:00
free(kind_name);
2010-06-03 21:24:43 +08:00
}
break;
case '?':
usage();
return(8);
}
if (fortran_flag && c_flag) {
derror("Only one of -c or -f may be specified");
return(8);
}
argc -= optind;
argv += optind;
if (argc > 1) {
Codify cross-platform file paths The netcdf-c code has to deal with a variety of platforms: Windows, OSX, Linux, Cygwin, MSYS, etc. These platforms differ significantly in the kind of file paths that they accept. So in order to handle this, I have created a set of replacements for the most common file system operations such as _open_ or _fopen_ or _access_ to manage the file path differences correctly. A more limited version of this idea was already implemented via the ncwinpath.h and dwinpath.c code. So this can be viewed as a replacement for that code. And in path in many cases, the only change that was required was to replace '#include <ncwinpath.h>' with '#include <ncpathmgt.h>' and then replace file operation calls with the NCxxx equivalent from ncpathmgr.h Note that recently, the ncwinpath.h was renamed ncpathmgmt.h, so this pull request should not require dealing with winpath. The heart of the change is include/ncpathmgmt.h, which provides alternate operations such as NCfopen or NCaccess and which properly parse and rebuild path arguments to work for the platform on which the code is executing. This mostly matters for Windows because of the way that it uses backslash and drive letters, as compared to *nix*. One important feature is that the user can do string manipulations on a file path without having to worry too much about the platform because the path management code will properly handle most mixed cases. So one can for example concatenate a path suffix that uses forward slashes to a Windows path and have it work correctly. The conversion code is in libdispatch/dpathmgr.c, and the important function there is NCpathcvt which does the proper conversions to the local path format. As a rule, most code should just replace their file operations with the corresponding NCxxx ones defined in include/ncpathmgmt.h. These NCxxx functions all call NCpathcvt on their path arguments before executing the actual file operation. In some rare cases, the client may need to directly use NCpathcvt, but this should be avoided as much as possible. If there is a need for supporting a new file operation not already in ncpathmgmt.h, then use the code in dpathmgr.c as a template. Also please notify Unidata so we can include it as a formal part or our supported operations. Also, if you see an operation in the library that is not using the NCxxx form, then please submit an issue so we can fix it. Misc. Changes: * Clean up the utf8 testing code; it is impossible to get some tests to work under windows using shell scripts; the args do not pass as utf8 but as some other encoding. * Added an extra utf8 test case: test_unicode_path.sh * Add a true test for HDF5 1.10.6 or later because as noted in PR https://github.com/Unidata/netcdf-c/pull/1794, HDF5 changed its Windows file path handling.
2021-03-05 04:41:31 +08:00
int i;
for(i=0;i<argc;i++)
fprintf(stderr,"xarg(%d): |%s|\n",i,argv[i]);
2010-06-03 21:24:43 +08:00
derror ("%s: only one input file argument permitted",progname);
return(6);
}
fp = stdin;
if (argc > 0 && strcmp(argv[0], "-") != 0) {
2017-04-15 01:05:30 +08:00
if ((fp = NCfopen(argv[0], "r")) == NULL) {
2010-06-03 21:24:43 +08:00
derror ("can't open file %s for reading: ", argv[0]);
perror("");
return(7);
}
cdlname = argv[0];
}
ncgin = fp;
any_error = ncgparse();
nc_finalize();
2010-06-03 21:24:43 +08:00
if (any_error || derror_count > 0)
return 1;
return 0;
}