/* * attempts to move to Classes from structs * ** warnings removed from g++ compile ** time functions removed ** */ /* * Libraries. **************/ #include #include #include #include #include #include #include #include /* this added to avoid compiler error of isalpha, isdigit, isgraph */ #include /* removed if system Unix to avoid Fedora 8 error */ #include /* * Headers. ************/ //#include "/opt/cxx/netcdf.h" DLL definitions for Windows removed #include "/usr/local/include/netcdf.h" #include #include "/opt/cern/root/include/TObject.h" #include "/opt/cern/root/include/TROOT.h" #include "/opt/cern/root/include/TFile.h" #include "/opt/cern/root/include/TTree.h" #include "/opt/cern/root/include/TVersionCheck.h" /* * Macros. **************/ #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif #ifndef CALLOC #define CALLOC(n, t) (t *)calloc(n, sizeof(t)) #endif #ifndef MALLOC #define MALLOC(n, t) (t *)malloc(n * sizeof(t)) #endif #ifndef REALLOC #define REALLOC(p, n, t) (t*) realloc(p, (n) * sizeof(t)) #endif #ifndef ncx_DataCALLOC #define ncx_DataCALLOC(DATA) \ if( ( DATA = MALLOC(1, ncDataS)) \ == (ncDataP)NULL ) \ memory_fail(__FILE__,__LINE__); \ init_ncdata(DATA) #endif /* * Struct (now class) to hold the dimensions of the netcdf file. *****************************************************/ typedef class _Dimensions : public TObject { public: /* Dimension id. */ int dimid; /* Dimension name. */ char *dimname; /* Dimension length. */ size_t dimlength; /* Flag to keep this dimension. */ int keep; /* Dimension id for writing. */ int wdimid; /* Variable to hold start position to print. */ int start; _Dimensions() { dimid = -1; dimname = (char *)NULL; dimlength = -1; keep = 0; wdimid = -1; }; virtual ~_Dimensions() { dimid = -1; dimname = (char *)NULL; dimlength = -1; keep = 0; wdimid = -1; }; // ClassDef(_Dimensions, 1); } DimensionsS, *DimensionsP; /* * Struct (now class) to hold the global/field-level attributes of the netcdf file. ************************************************************************/ typedef class _Attributes : public TObject { public: /* Attribute id. */ int attid; /* Attribute type. */ nc_type atttype; /* Attribute name. */ char *attname; /* Attribute length. */ size_t attlength; /* Attribute value. */ long *long_val; char *char_val; double *double_val; float *float_val; short *short_val; unsigned char *uchar_val; /* Flag to keep this attribute. */ int keep; /* Attribute id for writing. */ int wattid; _Attributes() { attid = -1; atttype = (nc_type) -1; attname = (char *)NULL; attlength = 0; long_val = (long *)NULL; char_val = (char *)NULL; double_val = (double *)NULL; float_val = (float *)NULL; short_val = (short *)NULL; uchar_val = (unsigned char *)NULL; keep = 0; wattid = -1; }; virtual ~_Attributes() { attid = -1; atttype = (nc_type) -1; attname = (char *)NULL; attlength = 0; long_val = (long *)NULL; char_val = (char *)NULL; double_val = (double *)NULL; float_val = (float *)NULL; short_val = (short *)NULL; uchar_val = (unsigned char *)NULL; keep = 0; wattid = -1; }; // ClassDef(_Attributes, 1); } AttributesS, *AttributesP; /* * Struct (now class) to hold the variables of the netcdf file. ****************************************************/ typedef class _Variables : public TObject { public: /* Variable id. */ int varid; /* Variable name. */ char *varname; /* Variable type. */ nc_type vartype; /* Number of dimensions. */ int varndims; /* Dimension ids. */ int *vardimids; /* Number of attributes. */ int varnatts; /* Attributes. */ AttributesP *attributes; /* Number of samples. */ size_t nsamples; /* Variable data. */ long *long_data; char *char_data; double *double_data; float *float_data; short *short_data; unsigned char *uchar_data; /* Flag to keep this variable. */ int keep; /* Variable id for writing. */ int wvarid; _Variables() { varid = -1; varname = (char *)NULL; vartype = (nc_type) -1; varndims = 0; vardimids = (int *)NULL; varnatts = 0; attributes = (AttributesP *)NULL; nsamples = 0; long_data = (long *)NULL; char_data = (char *)NULL; double_data = (double *)NULL; float_data = (float *)NULL; short_data = (short *)NULL; uchar_data = (unsigned char *)NULL; keep = 0; wvarid = -1; }; virtual ~_Variables() { varid = -1; varname = (char *)NULL; vartype = (nc_type) -1; varndims = 0; vardimids = (int *)NULL; varnatts = 0; attributes = (AttributesP *)NULL; nsamples = 0; long_data = (long *)NULL; char_data = (char *)NULL; double_data = (double *)NULL; float_data = (float *)NULL; short_data = (short *)NULL; uchar_data = (unsigned char *)NULL; keep = 0; wvarid = -1; } ; // ClassDef(_Variables, 1); } VariablesS, *VariablesP; /* * Struct (now class) to hold the contents of the netcdf file. ***************************************************/ typedef class _ncData : public TObject { public: /* Stats. */ int fid, ndims, nvars, ngatts, unlimited; /* Dimensions. */ DimensionsP *dimensions; /* Global attributes. */ AttributesP *globals; /* Variables. */ VariablesP *variables; /* File names. */ char *infile, *outfile; /* Flags for reading. */ int isopen, getattvalues, getvardata; /* Flags for writing. */ int mode; /* Flag to sort data. */ int sort; /* Time flag. */ int time_flag; _ncData() { fid = -1; ndims = 0; nvars = 0; ngatts = 0; unlimited = -1; dimensions = ( DimensionsP * )NULL; globals = ( AttributesP * )NULL; variables = ( VariablesP * )NULL; infile = ( char * )NULL; outfile = ( char * )NULL; isopen = FALSE; getvardata = TRUE; getattvalues = TRUE; mode = NC_NOCLOBBER; sort = 0; time_flag = 0; }; virtual ~_ncData() { fid = -1; ndims = 0; nvars = 0; ngatts = 0; unlimited = -1; dimensions = ( DimensionsP * )NULL; globals = ( AttributesP * )NULL; variables = ( VariablesP * )NULL; infile = ( char * )NULL; outfile = ( char * )NULL; isopen = FALSE; getvardata = TRUE; getattvalues = TRUE; mode = NC_NOCLOBBER; sort = 0; time_flag = 0; }; // ClassDef(_ncData, 1); } ncDataS, *ncDataP; /* * Prototypes. ***************/ /* Read. */ int ncx_read(ncDataP); int ncx_getdimensions(ncDataP); int ncx_getdimension(ncDataP,DimensionsP); int ncx_getglobals(ncDataP); int ncx_getattributes(ncDataP,VariablesP); int ncx_getattribute(ncDataP,AttributesP,VariablesP); int ncx_getattributevalue(ncDataP,AttributesP,VariablesP); int ncx_getvariables(ncDataP); int ncx_getvariable(ncDataP,VariablesP); int ncx_getvariabledata(ncDataP, VariablesP, unsigned int*, unsigned int*); int ncx_getatt(ncDataP, int, char *); int ncx_getdimid(ncDataP, char *); int ncx_getvarid(ncDataP, char *); int ncx_getvarsize(ncDataP, int); int ncx_setFilename(ncDataP, char *, int); /* Initialize. */ void init_dimension(DimensionsP); void init_attribute(AttributesP); void init_variable(VariablesP); void init_ncdata(ncDataP); /* Print. */ void print_dimension(DimensionsP); void print_attribute(AttributesP); void print_variable(VariablesP); void print_ncdata(ncDataP); /* Free memory. */ int free_dimensions(ncDataP); int free_dimension(DimensionsP); int free_attributes(AttributesP*,int); int free_attribute(AttributesP); int free_variables(ncDataP); int free_variable(VariablesP); int free_ncdata(ncDataP); /* Memory allocation failure. */ void memory_fail(char*,int);