// /* NETCDFFileData.cpp */ #include "netcdffiledata.h" NETCDFFileData::NETCDFFileData(const string &filename, const string &mode) :FileData(filename,mode),FileDataReader(filename,mode) { this->filename=filename; if (!exists_file(filename)) { bad_file e(filename); throw e; } open_data_file(); } void NETCDFFileData::open_data_file() { int32 fileMode; if(mode=="r") fileMode=DFACC_READ; else if(mode=="rw") fileMode=DFACC_WRITE; else if(mode=="w") fileMode=DFACC_CREATE; ncfile = SDstart(filename.c_str(),fileMode); if(ncfile<0){ bad_file_opening_mode e(filename); throw e; } } NETCDFFileData::~NETCDFFileData() { SDend(ncfile); } void* NETCDFFileData::read_data(void* data, const char* sds_name, int * start, int * stride, int *edges, int rank) { int32 index=SDnametoindex(ncfile,sds_name); int32 sds_id=SDselect(ncfile,index); int size=1; bool are_limits_initialized[3]; init_read_write_null_input_param(sds_name, start, stride, edges,rank, are_limits_initialized); // check the selection limits check_read_write_limits(sds_name, start,stride,edges,rank); // throw bad_index in case of problem if (data==NULL) { vector v = get_dataset_dimension(sds_name); int data_type_size; char name[MAX_NC_NAME]; int32 rank; int32 dimsizes[MAX_VAR_DIMS]; int32 data_type; int32 num_attrs; for(uint i=0; i(malloc(data_type_size*size)); } SDreaddata(sds_id, (int32*)(start), (int32*)(stride), (int32*)(edges), data); SDendaccess(sds_id); free_read_write_allocations(are_limits_initialized, start, stride, edges); return data; } vector NETCDFFileData::get_dataset_dimension(const string &sds_name) { vector v; int l; char name[MAX_NC_NAME]; int32 rank; int32 dimsizes[MAX_VAR_DIMS]; int32 data_type; int32 num_attrs; int32 index=SDnametoindex(ncfile,sds_name.c_str()); int32 sds_id=SDselect(ncfile,index); SDgetinfo(sds_id, name, &rank, dimsizes, &data_type, &num_attrs); SDendaccess(sds_id); if(rank>=0) v.reserve(rank); for(int i=0; i dim=this->get_dataset_dimension(sds_name); for ( int i = 0 ; i (dim[i]-1)) { bad_index e(start[i],(dim[i]-1)); throw e; return false; } // test the selection length int stride_val; if ( stride!=NULL ) stride_val=stride[i]; else stride_val=1; if ( (start[i]+(edges[i]*stride_val))> dim[i] ) { bad_index e(start[i]+(edges[i]*stride_val),dim[i]-1); throw e; return false; } } return true; }; void NETCDFFileData::init_read_write_null_input_param(const char* sds_name, int *&start, int *&stride, int *&edges, int &rank, bool * initialized_values) { initialized_values[0] = false; initialized_values[1] = false; initialized_values[2] = false; char name[MAX_NC_NAME]; int32 dimsizes[MAX_VAR_DIMS]; int32 data_type; int32 num_attrs; int32 index=SDnametoindex(ncfile,sds_name); int32 sds_id=SDselect(ncfile,index); int32 id = SDgetinfo(sds_id, name, (int32*)(&rank), dimsizes, &data_type, &num_attrs); // if start is not defined, allocate it with a rank size, and set its values to 0 if(id<0) { bad_sds_name e(filename.c_str(),sds_name); throw e; } SDendaccess(sds_id); if ( start == NULL ) { start = new int[rank]; initialized_values[0] = true; for (int i = 0;i vect=this->get_dataset_dimension(sds_name); for ( int i = 0;iget_dataset(sds_name.c_str()); Hdf_attr attr = sds.get_attribute(attr_name.c_str()); char8 *values = new char8[attr.get_nvalues()]; string ret = string(values); delete values; return ret; } bool NETCDFFileData::has_attr_dataset(string sds_name,string attr_name) { Hdf_sds sds = this->get_dataset(sds_name.c_str()); return sds.has_attr(attr_name.c_str()); }