int main(int argc,char *argv[]) { CDFid id; CDFstatus status; int max; fprintf(stdout,"RCDFMaxRecR\n"); if (argc<3) { fprintf(stderr,"Filename and variable must be given.\n"); exit(-1); } status=CDFopen(argv[1],&id); if (status !=CDF_OK) { fprintf(stderr,"Could not open cdf file.\n"); exit(-1); } max=RCDFMaxRecR(id,argv[2]); CDFclose(id); fprintf(stdout,"Max. R Rec for %s:%d\n",argv[2],max); return 0; }
int main(int argc,char *argv[]) { CDFid id; CDFstatus status; int majority; fprintf(stdout,"RCDFMajority\n"); if (argc==1) { fprintf(stderr,"No filename.\n"); exit(-1); } status=CDFopen(argv[1],&id); if (status !=CDF_OK) { fprintf(stderr,"Could not open cdf file.\n"); exit(-1); } majority=RCDFMajority(id); CDFclose(id); fprintf(stdout,"Majority:%d\n",majority); return 0; }
int dsel(struct dirent *dp) { #else int dsel(const struct dirent *dp) { #endif if (dp->d_name[0]=='.') return 0; return 1; } int test_file_epoch(char *fname,double stime,double etime) { long yr,mo,dy,hr,mt,sc,ms; double fstime=0,fetime=0; int max=0; CDFid id; CDFstatus status; double *dptr; char *varlist[]={"Epoch",NULL}; status=CDFopen(fname,&id); if (status !=CDF_OK) return -1; max=RCDFMaxRecR(id,varlist[0]); if (max>0) { status=RCDFReadR(id,0,varlist,data); dptr=(double *) data[0].data; EPOCHbreakdown(dptr[0],&yr,&mo,&dy,&hr,&mt,&sc,&ms); fstime=TimeYMDHMSToEpoch(yr,mo,dy,hr,mt,sc+(ms/1000.0)); status=RCDFReadR(id,max-1,varlist,data); dptr=(double *) data[0].data; EPOCHbreakdown(dptr[0],&yr,&mo,&dy,&hr,&mt,&sc,&ms); fetime=TimeYMDHMSToEpoch(yr,mo,dy,hr,mt,sc+(ms/1000.0)); } max=RCDFMaxRecZ(id,varlist[0]); if (max>0) { status=RCDFReadZ(id,0,varlist,data); dptr=(double *) data[0].data; EPOCHbreakdown(dptr[0],&yr,&mo,&dy,&hr,&mt,&sc,&ms); fstime=TimeYMDHMSToEpoch(yr,mo,dy,hr,mt,sc+(ms/1000.0)); status=RCDFReadZ(id,max-1,varlist,data); dptr=(double *) data[0].data; EPOCHbreakdown(dptr[0],&yr,&mo,&dy,&hr,&mt,&sc,&ms); fetime=TimeYMDHMSToEpoch(yr,mo,dy,hr,mt,sc+(ms/1000.0)); } CDFclose(&id); if (stime>fetime) return 0; if (etime<fstime) return 0; return 1; }
/* get the starttime, endtime, and number of records for the specified CDF file */ int get_start_and_end_times(char *filename, double *starttime, double *endtime, int *nrecords) { int i; CDFstatus cstatus; CDFid cid; long version; long release; char copyRight[CDF_COPYRIGHT_LEN+1]; long numDims; long dimSizes[CDF_MAX_DIMS]; long encoding; long majority; long maxRec; long numVars; long numAttrs; long varEpoch; static long indices[10] = {0,0,0,0,0,0,0,0,0,0}; CDFstatus rstat; char pathname[500]; char *cptr; int epoch_is_rvar = -1; char error_message[100]; /* Remove any trailing ".cdf", ".CDF", ".Cdf" extensions */ (void) strncpy(pathname, filename, 256); i = strlen(pathname); cptr = pathname + i - 4; if (!strcmp (cptr, ".cdf") || !strcmp (cptr, ".CDF") || !strcmp (cptr, ".Cdf")) { *cptr = '\0'; } /* Open the file as a CDF file */ cstatus = CDFopen (pathname, &cid); if (cstatus != CDF_OK) { if (cstatus < CDF_WARN) { fprintf(stderr, "Unable to open CDF file: %s (%s)\n", filename, pathname); fprintf(stderr, "status = %ld\n", cstatus); CDFerror(cstatus, error_message); fprintf(stderr,"%s\n", error_message); return(-1); } else { /* Let's ignore non-error warning messages here for now, at Davin's request. CDFerror(cstatus, error_message); fprintf(stderr, "%s ", error_message); fprintf(stderr, "This is a warning only, continuing...\n"); */ } } /* Get the version, etc */ cstatus = CDFdoc (cid, &version, &release, copyRight); if (cstatus != CDF_OK) { fprintf(stderr, "Error in CDFdoc: filename = %s, cstatus= %ld \n", filename, cstatus); cstatus = CDFclose(cid); return(-1); } /* test whether Epoch is an r variable or z variable */ cstatus = CDFlib(CONFIRM_, rVAR_EXISTENCE_, "Epoch", NULL_); if (cstatus >= CDF_WARN) { /* In this case, Epoch is an r variable */ epoch_is_rvar = 1; varEpoch = CDFvarNum(cid, "Epoch"); if (varEpoch < 0) { fprintf(stderr, "Error: filename = %s, could not get rvar number for 'Epoch'.\n", filename); cstatus = CDFclose(cid); return(-1); } cstatus = CDFinquire (cid, &numDims, dimSizes, &encoding, &majority, &maxRec, &numVars, &numAttrs); if (cstatus != CDF_OK) { fprintf(stderr, "Error in CDFinquire: filename = %s, cstatus= %ld \n", filename, cstatus); cstatus = CDFclose(cid); return(-1); } if (maxRec < 0) { fprintf(stderr, "Number records: %d is too small in file: %s\n", maxRec,filename); cstatus = CDFclose(cid); return(-1); } *nrecords = (int) (maxRec + 1); rstat = CDFvarGet (cid, varEpoch, (long) 0, indices, starttime); rstat = CDFvarGet (cid, varEpoch, (long) maxRec, indices, endtime); } else { /* In this case, Epoch is a z variable */ cstatus = CDFlib(CONFIRM_, zVAR_EXISTENCE_, "Epoch", NULL_); if (cstatus >= CDF_WARN) { epoch_is_rvar = 0; cstatus = CDFlib(SELECT_, zVAR_NAME_, "Epoch", NULL_); if (cstatus != CDF_OK) { fprintf(stderr,"Error: filename = %s, could not find zvar 'Epoch'.\n", filename); cstatus = CDFclose(cid); return(-1); } cstatus = CDFlib(SELECT_, zVAR_RECNUMBER_, 0, GET_, zVAR_DATA_, starttime, NULL_); if (cstatus != CDF_OK) { fprintf(stderr, "Error: filename = %s,", filename); fprintf(stderr, " could not get first time record of zvariable 'Epoch'.\n"); cstatus = CDFclose(cid); return(-1); } cstatus = CDFlib(GET_, zVAR_MAXREC_, &maxRec, NULL_); if (cstatus != CDF_OK) { fprintf(stderr, "Error: filename = %s,", filename); fprintf(stderr, " could not get number of records for zvar 'Epoch'.\n"); cstatus = CDFclose(cid); return(-1); } *nrecords = (int) (maxRec + 1); cstatus = CDFlib(SELECT_, zVAR_RECNUMBER_, maxRec, GET_, zVAR_DATA_, endtime, NULL_); if (cstatus != CDF_OK) { fprintf(stderr, "Error: filename = %s,", filename); fprintf(stderr, " could not get last time record of zvariable 'Epoch'.\n"); return(-1); } } } if (epoch_is_rvar == -1) { fprintf(stderr, "Error: filename = %s,", filename); fprintf(stderr, " could not find either rvar or zvar named 'Epoch'.\n"); return(-1); } cstatus = CDFclose(cid); return(1); }
bool CdfSource::initFile() { CDFid id; CDFstatus status = CDFopen(_filename.latin1(), &id); if (status < CDF_OK) { kstdDebug() << _filename << ": failed to open in initFile()" << endl; return false; } // Query field list and store it in _fieldList (plus "INDEX") _fieldList.clear(); //_fieldList += "INDEX"; commented out as it leads to problems in countFrames(): // which value should the method return when not all variables have the same length ? long numRvars = 0, numZvars = 0, varN = 0, numDims = 0, dimSizes[CDF_MAX_DIMS], maxRec = 0; char varName[CDF_VAR_NAME_LEN + 1]; status = CDFlib(SELECT_, CDF_READONLY_MODE_, READONLYon, GET_, CDF_NUMrVARS_, &numRvars, CDF_NUMzVARS_, &numZvars, NULL_); // We accept the following types of fields: // - scalars (numDims == 0) // - vectors (numDims == 1) of size 1 (dimSizes[0] == 1) // - vectors (numDims == 1) of any size, PROVIDED THAT THEY HAVE ONLY 1 RECORD // Note that the last case is required by Matlab-generated CDFs, where vectors are stored that way // Add 0-dimensional rVariables for (varN = 0; varN < numRvars; varN++) { status = CDFlib(SELECT_, rVAR_, varN, GET_, rVAR_NAME_, varName, rVARs_NUMDIMS_, &numDims, rVARs_DIMSIZES_, dimSizes, rVAR_MAXREC_, &maxRec, NULL_); // maxRec is not exactly the number of records :-) maxRec += 1; if (status == CDF_OK && numDims < 2) { if (numDims == 1 && dimSizes[0] > 1 && maxRec > 1) { // Ignore that, this is not really a vector kstdDebug() << "Variable " << varName << " can't be handled by kst: only CDF vectors with dimensionalities 0, 1[1] or 1[n] but only one record in the latter case are supported (try cdfexport on your CDF if you have problems)" << endl; continue; } _fieldList += varName; if (numDims == 1 && dimSizes[0] > 1) { _frameCounts[QString(varName)] = dimSizes[0]; if ((int) dimSizes[0] > _maxFrameCount) { _maxFrameCount = dimSizes[0]; } } else { _frameCounts[QString(varName)] = maxRec; if ((int) maxRec > _maxFrameCount) { _maxFrameCount = maxRec; } } } } // Add 0-dimensional zVariables for (varN = 0; varN < numZvars; varN++) { status = CDFlib(SELECT_, zVAR_, varN, GET_, zVAR_NAME_, varName, zVAR_NUMDIMS_, &numDims, zVAR_DIMSIZES_, dimSizes, zVAR_MAXREC_, &maxRec, NULL_); maxRec += 1; if (status == CDF_OK && numDims < 2) { if (numDims == 1 && dimSizes[0] > 1 && maxRec > 1) { // Ignore that, this is not really a vector kstdDebug() << "Variable " << varName << " can't be handled by kst: only CDF vectors with dimensionalities 0, 1[1] or 1[n] but only one record in the latter case are supported (try cdfexport on your CDF if you have problems)" << endl; continue; } _fieldList += varName; if (numDims == 1 && dimSizes[0] > 1) { _frameCounts[QString(varName)] = dimSizes[0]; if ((int) dimSizes[0] > _maxFrameCount) { _maxFrameCount = dimSizes[0]; } } else { _frameCounts[QString(varName)] = maxRec; if ((int) maxRec > _maxFrameCount) { _maxFrameCount = maxRec; } } } } // Close the file :-) status = CDFclose(id); return status >= CDF_OK; }
int CdfSource::readField(double *v, const QString& field, int s, int n) { int i; CDFstatus status; CDFid id; long dataType = 0, maxRec = 0, numDims = 0, dimSizes[CDF_MAX_DIMS]; long recCount = 0, indices[1] = {0}, counts[1] = {0}; char varName[CDF_VAR_NAME_LEN+1]; bool isZvar = true; /* Should be the case for recent cdf files */ // kstdDebug() << "Entering CdfSource::readField with params: " << field << ", from " << s << " for " << n << " values" << endl; // Handle the special case where we query INDEX if (field.lower() == "index") { if (n < 0) { v[0] = double(s); return 1; } for (int i = 0; i < n; ++i) { v[i] = double(s + i); } return n; } // If not INDEX, look into the CDF file... status = CDFopen(_filename.latin1(), &id); if (status < CDF_OK) { kstdDebug() << _filename << ": failed to open to read from field " << field << endl; return -1; } QString ftmp = field; ftmp.truncate(CDF_VAR_NAME_LEN); // Variable selection strcpy(varName, ftmp.latin1()); status = CDFlib(SELECT_, zVAR_NAME_, varName, GET_, zVAR_DATATYPE_, &dataType, NULL_); if (status < CDF_OK) { // if not zVar, try rVar // kstdDebug() << ftmp << ": " << " not a zVAR (" << status <<")" << endl; isZvar = false; status = CDFlib(SELECT_, rVAR_NAME_, varName, GET_, rVAR_DATATYPE_, &dataType, NULL_); } // I suppose the returned int is the number of values read, <0 when there is a problem if (status < CDF_OK) { kstdDebug() << ftmp << ": " << " not a rVAR either -> exiting" << endl; CDFclose(id); return -1; } // If n<0 set it to 1 as suggested by George Staikos // (needs to be documented better I guess !) // First check for the existence of more values for this field if (n < 0) { n = 1; } void *binary = 0L; void *pt = 0L; // Cast the iteration pointer to the right type and allocate the needed space for binary switch (dataType) { case CDF_INT2: binary = malloc(n*sizeof(Int16)); pt = (Int16 *)binary; break; case CDF_INT4: binary = malloc(n*sizeof(Int32)); pt = (Int32 *)binary; break; case CDF_UINT1: binary = malloc(n*sizeof(uChar)); pt = (uChar *)binary; break; case CDF_UINT2: binary = malloc(n*sizeof(uInt16)); pt = (uInt16 *)binary; break; case CDF_UINT4: binary = malloc(n*sizeof(uInt32)); pt = (uInt32 *)binary; break; case CDF_REAL4: case CDF_FLOAT: binary = malloc(n*sizeof(float)); pt = (float *)binary; break; case CDF_REAL8: case CDF_DOUBLE: binary = malloc(n*sizeof(double)); pt = (double *)binary; break; default : binary = malloc(n*sizeof(long double)); break; } // Get some useful values status = CDFlib (GET_, BOO(isZvar, zVAR_MAXREC_, rVAR_MAXREC_), &maxRec, BOO(isZvar, zVAR_NUMDIMS_, rVARs_NUMDIMS_), &numDims, BOO(isZvar, zVAR_DIMSIZES_, rVARs_DIMSIZES_), dimSizes, NULL_); maxRec += 1; if (numDims == 0 || (numDims == 1 && dimSizes[0] < 2)) { // Vars of dimension 0, or vectors of size 1 (pseudo scalars) with records > 1 status = CDFlib (SELECT_, BOO(isZvar, zVAR_RECNUMBER_, rVAR_SEQPOS_), (long) s, BOO(isZvar, zVAR_RECCOUNT_, rVARs_RECCOUNT_), (long) n, GET_, BOO(isZvar, zVAR_HYPERDATA_, rVAR_HYPERDATA_), binary, NULL_); } else { // Vectors of size [1: n>1] with only one sample/record a la Matlab indices[0] = s; counts[0] = n; recCount = 1; status = CDFlib (SELECT_, BOO(isZvar,zVAR_RECCOUNT_,rVARs_RECCOUNT_), recCount, BOO(isZvar,zVAR_DIMINDICES_,rVARs_DIMINDICES_), indices, BOO(isZvar,zVAR_DIMCOUNTS_,rVARs_DIMCOUNTS_), counts, GET_, BOO(isZvar,zVAR_HYPERDATA_,rVAR_HYPERDATA_), binary, NULL_); maxRec = dimSizes[0]; } for (i = 0; i < n && i < maxRec; i++) { switch (dataType) { case CDF_INT2: v[i] = (double) *((Int16 *)pt); pt = (Int16 *)pt + 1; break; case CDF_INT4: v[i] = (double) *((Int32 *)pt); pt = (Int32 *)pt + 1; break; case CDF_UINT1: v[i] = (double) *((uChar *)pt); pt = (uChar *)pt + 1; break; case CDF_UINT2: v[i] = (double) *((uInt16 *)pt); pt = (uInt16 *)pt + 1; break; case CDF_UINT4: v[i] = (double) *((uInt32 *)pt); pt = (uInt32 *)pt + 1; break; case CDF_REAL4: case CDF_FLOAT: v[i] = (double) *((float *)pt); pt = (float *)pt + 1; break; case CDF_REAL8: case CDF_DOUBLE: v[i] = (double) *((double *)pt); pt = (double *)pt + 1; break; } } free(binary); status = CDFclose(id); return i; }