int var_exists( char *variable_name) { if (CDFlib( CONFIRM_, zVAR_EXISTENCE_, variable_name, NULL_) == CDF_OK) { /** specified variable exists in currently open CDF file **/ return 1; } else { return 0; } }
/* 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); }
int main () { CDFid id; CDFstatus status; long varnum; long variance = { VARY }; long dimSizes[1] = {0}, dimVary[1] = {VARY}; long indices[1] = {0}, counts[1]= {1}, intervals[1] = {1}; long long tt2000[8], mm[8]; double year1=2008, month1=12, day1=31, hour1=23, minute1=59, second1=57, msec1=100, usec1=200, nsec1=300; double year=2008, month=12, day=31, hour=23, minute=59, second=57, msec=100, usec=200, nsec=300; double yearOut, monthOut, dayOut, hourOut, minuteOut, secondOut, msecOut, usecOut, nsecOut; int ix; char string[TT2000_3_STRING_LEN+1]; long long int8[3][2], int8o[3][2], nn; /****************************************************************************** * Display title. ******************************************************************************/ printf ("Testing Internal/C interface for CDF_TIME_TT2000 ...\n"); tt2000[0] = computeTT2000 (2000., 1., 1., 12., 0., 0., TT2000END); breakdownTT2000 (tt2000[0], &yearOut, &monthOut, &dayOut, &hourOut, &minuteOut, &secondOut, &msecOut, &usecOut, &nsecOut); printf ("Base: 2000-01-01T12:00.00.000 => %lld nanoseconds\n", tt2000[0]); encodeTT2000 (tt2000[0], string, 3); printf (" %lld => %s\n", tt2000[0], string); /****************************************************************************** * Create CDF. ******************************************************************************/ status = CDFlib (CREATE_, CDF_, "TEST", 0, dimSizes, &id, NULL_); if (status < CDF_OK) { if (status == CDF_EXISTS) { status = CDFlib (OPEN_, CDF_, "TEST", &id, NULL_); if (status < CDF_OK) QuitCDF ("1.0", status); status = CDFlib (DELETE_, CDF_, NULL_); if (status < CDF_OK) QuitCDF ("1.1", status); status = CDFlib (CREATE_, CDF_, "TEST", 0L, dimSizes, &id, NULL_); if (status < CDF_OK) QuitCDF ("6.0", status); } } /****************************************************************************** * Create variables. ******************************************************************************/ status = CDFlib (CREATE_, zVAR_, "myTT2000", CDF_TIME_TT2000, 1L, 0L, dimSizes, variance, dimVary, &varnum, NULL_); if (status < CDF_OK) QuitCDF ("7.0", status); dimSizes[0] = 2; status = CDFlib (CREATE_, zVAR_, "myINT8", CDF_INT8, 1L, 1L, dimSizes, variance, dimVary, &varnum, NULL_); if (status < CDF_OK) QuitCDF ("7.1", status); /****************************************************************************** * PUT to variables. ******************************************************************************/ tt2000[0] = computeTT2000 (year, month, day, hour, minute, second, msec, usec, nsec); if (tt2000[0] == ILLEGAL_TT2000_VALUE) QuitCDF ("9.0", TT2000_TIME_ERROR); for (ix = 1; ix < 8; ++ix) tt2000[ix] = tt2000[0] + (long long) ix*1000000000; varnum = CDFgetVarNum (id, "myTT2000"); status = CDFlib (SELECT_, zVAR_, varnum, zVAR_RECNUMBER_, 0L, zVAR_RECCOUNT_, 8L, zVAR_RECINTERVAL_, 1L, zVAR_DIMINDICES_, indices, zVAR_DIMCOUNTS_, counts, zVAR_DIMINTERVALS_, intervals, PUT_, zVAR_HYPERDATA_, tt2000, NULL_); if (status < CDF_OK) QuitCDF ("10.0", status); /****************************************************************************** * HyperGET from variables. ******************************************************************************/ status = CDFlib (GET_, zVAR_HYPERDATA_, mm, NULL_); if (status < CDF_OK) QuitCDF ("11.0", status); for (ix= 0; ix < 8; ix++) { breakdownTT2000 (mm[ix], &yearOut, &monthOut, &dayOut, &hourOut, &minuteOut, &secondOut, &msecOut, &usecOut, &nsecOut); if (year != yearOut) QuitCDF("11.1", status); if (month != monthOut) QuitCDF("11.2", status); if (day != dayOut) QuitCDF("11.3", status); if (hour != hourOut) QuitCDF("11.4", status); if (minute != minuteOut) QuitCDF("11.5", status); if (second != secondOut) QuitCDF("11.6", status); if (msec != msecOut) QuitCDF("11.7", status); if (usec != usecOut) QuitCDF("11.8", status); if (nsec != nsecOut) QuitCDF("11.9", status); ++second; if (second > 60) { year = 2009; month = 1; day = 1; hour = 0; minute = 0; second = 0; } } varnum = CDFgetVarNum (id, "myINT8"); nn = 88888888888LL; for (ix = 0; ix < 3; ++ix) { int8[ix][0] = nn; int8[ix][1] = -nn; ++nn; } counts[0] = 2; status = CDFlib (SELECT_, zVAR_, varnum, zVAR_RECNUMBER_, 0L, zVAR_RECCOUNT_, 3L, zVAR_RECINTERVAL_, 1L, zVAR_DIMINDICES_, indices, zVAR_DIMCOUNTS_, counts, zVAR_DIMINTERVALS_, intervals, PUT_, zVAR_HYPERDATA_, int8, NULL_); if (status < CDF_OK) QuitCDF ("12.0", status); status = CDFlib (GET_, zVAR_HYPERDATA_, int8o, NULL_); if (status < CDF_OK) QuitCDF ("13.0", status); nn = 88888888888LL; for (ix = 0; ix < 3; ++ix) { if (int8o[ix][0] != nn) QuitCDF ("14.0", status);; if (int8o[ix][1] != -nn) QuitCDF ("14.1", status);; ++nn; } /****************************************************************************** * Close CDF. ******************************************************************************/ status = CDFlib (CLOSE_, CDF_, NULL_); if (status < CDF_OK) QuitCDF ("26.2", status); /****************************************************************************** * Re-open the CDF. ******************************************************************************/ status = CDFlib (OPEN_, CDF_, "TEST", &id, NULL_); if (status < CDF_OK) QuitCDF ("30.1", status); varnum = CDFvarNum (id, "myTT2000"); if (varnum < CDF_OK) QuitCDF ("31.1", status); /****************************************************************************** * HyperGET from variables. ******************************************************************************/ status = CDFlib (SELECT_, zVAR_, varnum, zVAR_RECNUMBER_, 0L, zVAR_RECCOUNT_, 8L, zVAR_RECINTERVAL_, 1L, zVAR_DIMINDICES_, indices, zVAR_DIMCOUNTS_, counts, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, mm, NULL_); if (status < CDF_OK) QuitCDF ("32.1", status); for (ix= 0; ix < 8; ix++) { breakdownTT2000 (mm[ix], &yearOut, &monthOut, &dayOut, &hourOut, &minuteOut, &secondOut, &msecOut, &usecOut, &nsecOut); encodeTT2000 (mm[ix], string, 3); printf ("%s\n",string); if (year1 != yearOut) QuitCDF("33.1", status); if (month1 != monthOut) QuitCDF("33.2", status); if (day1 != dayOut) QuitCDF("33.3", status); if (hour1 != hourOut) QuitCDF("33.4", status); if (minute1 != minuteOut) QuitCDF("33.5", status); if (second1 != secondOut) QuitCDF("33.6", status); if (msec1 != msecOut) QuitCDF("33.7", status); if (usec1 != usecOut) QuitCDF("33.8", status); if (nsec1 != nsecOut) QuitCDF("33.9", status); ++second1; if (second1 > 60) { year1 = 2009; month1 = 1; day1 = 1; hour1 = 0; minute1 = 0; second1 = 0; } } /****************************************************************************** * Close CDF. ******************************************************************************/ status = CDFlib (CLOSE_, CDF_, NULL_); if (status < CDF_OK) QuitCDF ("26.2", status); /****************************************************************************** * Successful completion. ******************************************************************************/ return EXIT_SUCCESS_; }
void get_kpvt_cdf_info( int main_memory_flag) { void allocate_defaults_kpvt( void); CDFstatus status; long recordStart = 0; long recordCount = 1; long recordInterval = 1; long counts[1] = { 0 }; long intervals[1] = { 1 }; long indices[1]; int i; counts[0] = 1; /* set values */ intervals[0] = 1; allocate_defaults_kpvt(); /* get all of the cdf attribute/variable numbers that will be needed *******/ if (CDFlib( CONFIRM_, zVAR_EXISTENCE_, x_name, NULL_) == CDF_OK) { status = CDFlib( GET_, zVAR_NUMBER_, x_name, &x_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); } if (CDFlib( CONFIRM_, zVAR_EXISTENCE_, y_name, NULL_) == CDF_OK) { status = CDFlib( GET_, zVAR_NUMBER_, y_name, &y_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); } if (CDFlib( CONFIRM_, zVAR_EXISTENCE_, net_flux_name, NULL_) == CDF_OK) { status = CDFlib( GET_, zVAR_NUMBER_, net_flux_name, &net_flux_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); } if (CDFlib( CONFIRM_, zVAR_EXISTENCE_, total_flux_name, NULL_) == CDF_OK) { status = CDFlib( GET_, zVAR_NUMBER_, total_flux_name, &total_flux_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); } if (CDFlib( CONFIRM_, zVAR_EXISTENCE_, weights_name, NULL_) == CDF_OK) { status = CDFlib( GET_, zVAR_NUMBER_, weights_name, &weights_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); } /*** NOTE THESE ARE ATTRIBUTES SO USE CONFRIM ATTR_EXISTENCE... ***/ if (CDFlib( CONFIRM_, ATTR_EXISTENCE_, NX_name, NULL_) == CDF_OK) { status = CDFlib( GET_, ATTR_NUMBER_, NX_name, &NX_cdf_num, NULL_); if (status != CDF_OK) StatusHandler(status); } if (CDFlib( CONFIRM_, ATTR_EXISTENCE_, NY_name, NULL_) == CDF_OK) { status = CDFlib( GET_, ATTR_NUMBER_, NY_name, &NY_cdf_num, NULL_); if (status != CDF_OK) StatusHandler(status); } if ( DEBUG_FLAG) printf("DEBUG\t finished getting cdf nums\n"); /***************** use cdf_varNums to get actual scalar values *************/ /********* get value for NX **********/ status = CDFlib( SELECT_, ATTR_, NX_cdf_num, SELECT_, gENTRY_, 0, GET_, gENTRY_DATA_, &NX, NULL_); if (status != CDF_OK) StatusHandler(status); /********* get value for NY **********/ status = CDFlib( SELECT_, ATTR_, NY_cdf_num, SELECT_, gENTRY_, 0, GET_, gENTRY_DATA_, &NY, NULL_); if (status != CDF_OK) StatusHandler(status); /*convert original float N* to int n* if required */ /* bats NX was float but ucla, ctip, enlil, kpvt... are interger - no conversion needed here*/ nx = NX; ny = NY; if ( DEBUG_FLAG) { printf("DEBUG\tINFO from get_kpvt_cdf_info\n"); printf( "DEBUG\tNX=%d & nx=%d\n", NX, nx); printf( "DEBUG\tNY=%d & ny=%d\n", NY, ny); printf( "DEBUG\t main_memory_flag = %d\n", main_memory_flag); } /************************ allocate space for unique x & y positions ********/ if ( (x_pos = ( float * ) calloc( nx, sizeof(float)) ) == NULL) { printf("\ncalloc failed for x_pos buffer!\n"); exit( EXIT_FAILURE); } if ( (y_pos = ( float * ) calloc( ny, sizeof(float)) ) == NULL) { printf("\ncalloc failed for y_pos buffer!\n"); exit( EXIT_FAILURE); } if ( DEBUG_FLAG) printf("DEBUG\tspace allocated for variables...\n"); /***************************** get unique x,y values **********************/ counts[0] = nx; indices[0] = 0; intervals[0] = 1; status = CDFlib( SELECT_, zVAR_, x_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, x_pos, NULL_); if (status != CDF_OK) StatusHandler(status); counts[0] = ny; status = CDFlib( SELECT_, zVAR_, y_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, y_pos, NULL_); if (status != CDF_OK) StatusHandler(status); /****** more debug repoting ******/ /** print first few values for position variables **/ if ( DEBUG_FLAG) { for (i = 0; i < 30; i++) { printf( "INDEX-------------- %d\nx_pos[%d] = %f\ny_pos[%d] = %f\n\n", i, i, x_pos[i], i, y_pos[i]); } } }
int load_kpvt_cdf_variable_into_main_memory( char *variable_to_read) { CDFstatus status; long recordStart = 0; long recordCount = 1; long recordInterval = 1; long counts[1] = { 0 }; long intervals[1] = { 1 }; long indices[1]; long variable_cdf_number; int kpvt_reserve_mem_and_set_cdf_num( char *); /*printf("DEBUG\tinside load\n");*/ variable_cdf_number = kpvt_reserve_mem_and_set_cdf_num(variable_to_read); if (variable_cdf_number == -1) { printf( "ERROR:\tcould not find cdf variable number for -->%s<--\n", variable_to_read); return EXIT_FAILURE; /*exit( EXIT_FAILURE );*/ } /*(printf("DEBUG\tback from reserve mem with cdf_varNum %d for variable %s \n", variable_cdf_number, variable_to_read );*/ counts[0] = nx * ny; indices[0] = 0; intervals[0] = 1; if ( !strcmp( variable_to_read, net_flux_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, net_flux, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, total_flux_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, total_flux, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, weights_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, weights, NULL_); if (status != CDF_OK) StatusHandler(status); } else { /* INSERT ERROR HANDLER HERE */ printf("couldn't find a variable to load...\n"); return EXIT_FAILURE; } /*printf("DEBUG\tfinished hyper read returning\n");*/ return EXIT_SUCCESS; }
void get_ucla_ggcm_cdf_info( int main_memory_flag) { void allocate_defaults_b( void); CDFstatus status; long recordStart = 0; long recordCount = 1; long recordInterval = 1; long counts[1] = { 0 }; long intervals[1] = { 1 }; long indices[1]; counts[0] = 1; /* set values */ intervals[0] = 1; allocate_defaults_b(); /******************** get all of the cdf attributes/variable numbers that will be needed ******************/ status = CDFlib( GET_, zVAR_NUMBER_, rho_name, &rho_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, p_name, &p_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, jx_name, &jx_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, jy_name, &jy_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, jz_name, &jz_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, ux_name, &ux_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, uy_name, &uy_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, uz_name, &uz_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, bx_name, &bx_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, by_name, &by_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, bz_name, &bz_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, bx1_name, &bx1_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, by1_name, &by1_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, bz1_name, &bz1_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, x_name, &x_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, y_name, &y_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, z_name, &z_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, x_bx_name, &x_bx_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, y_bx_name, &y_bx_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, z_bx_name, &z_bx_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, x_by_name, &x_by_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, y_by_name, &y_by_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, z_by_name, &z_by_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, x_bz_name, &x_bz_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, y_bz_name, &y_bz_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, z_bz_name, &z_bz_cdf_varNum, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, ATTR_NUMBER_, NX_name, &NX_cdf_num, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, ATTR_NUMBER_, NY_name, &NY_cdf_num, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, ATTR_NUMBER_, NZ_name, &NZ_cdf_num, NULL_); if (status != CDF_OK) StatusHandler(status); status = CDFlib( GET_, zVAR_NUMBER_, eta_name, &eta_cdfNum, NULL_); if (status != CDF_OK) StatusHandler(status); /*printf("DEBUG\t finished getting cdf nums\n");*/ /***************** use cdf_varNums to get actual scalar values *************/ /********* get value for NX **********/ status = CDFlib( SELECT_, ATTR_, NX_cdf_num, SELECT_, gENTRY_, 0, GET_, gENTRY_DATA_, &NX, NULL_); if (status != CDF_OK) StatusHandler(status); /********* get value for NY **********/ status = CDFlib( SELECT_, ATTR_, NY_cdf_num, SELECT_, gENTRY_, 0, GET_, gENTRY_DATA_, &NY, NULL_); if (status != CDF_OK) StatusHandler(status); /********* get value for NZ **********/ status = CDFlib( SELECT_, ATTR_, NZ_cdf_num, SELECT_, gENTRY_, 0, GET_, gENTRY_DATA_, &NZ, NULL_); if (status != CDF_OK) StatusHandler(status); /*convert original float N* to int n* */ /* bats NX was float but ucla is int no conversion needed fix latter*/ nx = NX; ny = NY; nz = NZ; /*printf("DEBUG\tNX[%d] -> nx[%d]\n", NX, nx ); printf("DEBUG\tNY[%d] -> ny[%d]\n", NY, ny ); printf("DEBUG\tNZ[%d] -> nz[%d]\n", NZ, nz ); printf("DEBUG\t main_memory_flag = %d\n", main_memory_flag);*/ /********************* allocate space for unique x, y, & z positions *******/ if ( (x_pos = ( float * ) calloc( nx, sizeof(float)) ) == NULL) { printf("\ncalloc failed for x_pos buffer!\n"); exit( EXIT_FAILURE); } if ( (y_pos = ( float * ) calloc( ny, sizeof(float)) ) == NULL) { printf("\ncalloc failed for y_pos buffer!\n"); exit( EXIT_FAILURE); } if ( (z_pos = ( float * ) calloc( nz, sizeof(float)) ) == NULL) { printf("\ncalloc failed for x_pos buffer!\n"); exit( EXIT_FAILURE); } /************ allocate space for unique x_bx, y_bx, & z_bx positions *******/ if ( (x_bx_pos = ( float * ) calloc( nx, sizeof(float)) ) == NULL) { printf("\ncalloc failed for x_bx_pos buffer!\n"); exit( EXIT_FAILURE); } if ( (y_bx_pos = ( float * ) calloc( ny, sizeof(float)) ) == NULL) { printf("\ncalloc failed for y_bx_pos buffer!\n"); exit( EXIT_FAILURE); } if ( (z_bx_pos = ( float * ) calloc( nz, sizeof(float)) ) == NULL) { printf("\ncalloc failed for x_bx_pos buffer!\n"); exit( EXIT_FAILURE); } /************ allocate space for unique x_by, y_by, & z_by positions *******/ if ( (x_by_pos = ( float * ) calloc( nx, sizeof(float)) ) == NULL) { printf("\ncalloc failed for x_by_pos buffer!\n"); exit( EXIT_FAILURE); } if ( (y_by_pos = ( float * ) calloc( ny, sizeof(float)) ) == NULL) { printf("\ncalloc failed for y_by_pos buffer!\n"); exit( EXIT_FAILURE); } if ( (z_by_pos = ( float * ) calloc( nz, sizeof(float)) ) == NULL) { printf("\ncalloc failed for x_by_pos buffer!\n"); exit( EXIT_FAILURE); } /************ allocate space for unique x_bz, y_bz, & z_bz positions *******/ if ( (x_bz_pos = ( float * ) calloc( nx, sizeof(float)) ) == NULL) { printf("\ncalloc failed for x_bz_pos buffer!\n"); exit( EXIT_FAILURE); } if ( (y_bz_pos = ( float * ) calloc( ny, sizeof(float)) ) == NULL) { printf("\ncalloc failed for y_bz_pos buffer!\n"); exit( EXIT_FAILURE); } if ( (z_bz_pos = ( float * ) calloc( nz, sizeof(float)) ) == NULL) { printf("\ncalloc failed for x_bz_pos buffer!\n"); exit( EXIT_FAILURE); } /*printf("DEBUG\tspace allocated for variables...\n"); */ /******************** get unique x,y,z values ******************************/ counts[0] = nx; indices[0] = 0; intervals[0] = 1; status = CDFlib( SELECT_, zVAR_, x_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, x_pos, NULL_); if (status != CDF_OK) StatusHandler(status); counts[0] = ny; status = CDFlib( SELECT_, zVAR_, y_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, y_pos, NULL_); if (status != CDF_OK) StatusHandler(status); counts[0] = nz; status = CDFlib( SELECT_, zVAR_, z_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, z_pos, NULL_); if (status != CDF_OK) StatusHandler(status); /*printf("DEBUG\tunique x,y,z values read in successfully\n");*/ /********* get unique x_bx, y_bx, z_bx values ******************************/ counts[0] = nx; status = CDFlib( SELECT_, zVAR_, x_bx_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, x_bx_pos, NULL_); if (status != CDF_OK) StatusHandler(status); counts[0] = ny; status = CDFlib( SELECT_, zVAR_, y_bx_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, y_bx_pos, NULL_); if (status != CDF_OK) StatusHandler(status); counts[0] = nz; status = CDFlib( SELECT_, zVAR_, z_bx_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, z_bx_pos, NULL_); if (status != CDF_OK) StatusHandler(status); /************** get unique x_by, y_by, z_by values *************************/ counts[0] = nx; status = CDFlib( SELECT_, zVAR_, x_by_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, x_by_pos, NULL_); if (status != CDF_OK) StatusHandler(status); counts[0] = ny; status = CDFlib( SELECT_, zVAR_, y_by_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, y_by_pos, NULL_); if (status != CDF_OK) StatusHandler(status); counts[0] = nz; status = CDFlib( SELECT_, zVAR_, z_by_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, z_by_pos, NULL_); if (status != CDF_OK) StatusHandler(status); /************************* get unique x_bz, y_bz, z_bz values **************/ counts[0] = nx; status = CDFlib( SELECT_, zVAR_, x_bz_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, x_bz_pos, NULL_); if (status != CDF_OK) StatusHandler(status); counts[0] = ny; status = CDFlib( SELECT_, zVAR_, y_bz_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, y_bz_pos, NULL_); if (status != CDF_OK) StatusHandler(status); counts[0] = nz; status = CDFlib( SELECT_, zVAR_, z_bz_cdf_varNum, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, z_bz_pos, NULL_); if (status != CDF_OK) StatusHandler(status); }
int load_ucla_ggcm_cdf_variable_into_main_memory( char *variable_to_read) { CDFstatus status; long recordStart = 0; long recordCount = 1; long recordInterval = 1; long counts[1] = { 0 }; long intervals[1] = { 1 }; long indices[1]; long variable_cdf_number; long ucla_ggcm_reserve_mem_and_set_cdf_num( char *); /*printf("DEBUG\tinside load\n");*/ variable_cdf_number = ucla_ggcm_reserve_mem_and_set_cdf_num(variable_to_read); /* instead of EXITING PROGRAM with exit failure - print warning and return control to calling function */ if (variable_cdf_number == -1) { printf( "ERROR:\tcould not find cdf variable number for -->%s<--\n", variable_to_read); return EXIT_FAILURE; /*exit( EXIT_FAILURE );*/ } /*(printf("DEBUG\tback from reserve mem with cdfnum %d for variable %s \n", variable_cdf_number, variable_to_read );*/ counts[0] = nx * ny * nz; indices[0] = 0; intervals[0] = 1; if ( !strcmp( variable_to_read, bx_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, bx, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, by_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, by, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, bz_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, bz, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, bx1_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, bx1, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, by1_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, by1, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, bz1_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, bz1, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, ux_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, ux, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, uy_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, uy, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, uz_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, uz, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, jx_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, jx, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, jy_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, jy, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, jz_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, jz, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, rho_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, rho, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, p_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, p, NULL_); if (status != CDF_OK) StatusHandler(status); } else if ( !strcmp( variable_to_read, eta_name) ) { status = CDFlib( SELECT_, zVAR_, variable_cdf_number, SELECT_, zVAR_RECNUMBER_, recordStart, SELECT_, zVAR_RECCOUNT_, recordCount, SELECT_, zVAR_RECINTERVAL_, recordInterval, SELECT_, zVAR_DIMINDICES_, indices, SELECT_, zVAR_DIMCOUNTS_, counts, SELECT_, zVAR_DIMINTERVALS_, intervals, GET_, zVAR_HYPERDATA_, eta, NULL_); if (status != CDF_OK) StatusHandler(status); } else { /* INSERT ERROR HANDLER HERE */ printf("couldn't find a variable to load...\n"); return EXIT_FAILURE; } /*printf("DEBUG\tfinished hyper read returning\n");*/ return EXIT_SUCCESS; }
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; }