#ifndef CVARCONTAINER_H
#define CVARCONTAINER_H

#include "cpp/cvarcontainer.h"
#include "varcontainer_io.h"
#include <iostream>


void* cvc_holdValue=0 ;  // temporary global holding area for fortran variable pointers (stupid fortran compilers)

cVarContainer::cVarContainer(string name) : names_size(0), names(), names_citer(), 
					    att_names_size(0), att_names(), att_names_citer(), 
					    local_i32(),local_f32(),local_f64(), local_l32(),
					    local_char(), local_bytes(),
					    container(0){
  container = new VarContainer(name) ;
} ;

cVarContainer::cVarContainer(string name, int nsize) : names_size(0), names(), names_citer(), 
                        att_names_size(0), att_names(), att_names_citer(), 					    
                        local_i32(),local_f32(),local_f64(), local_l32(),
					    local_char(), local_bytes(),
                        container(0){
  if (nsize<=0) nsize=1000 ;
  //cout << "building new_size container " << name << " with size " << nsize << endl ;
  container = new VarContainer(name,static_cast<size_t>(nsize)) ;
} ;

cVarContainer::~cVarContainer() {
  //if (container!=0) cout << "cVarContainer::~cVarContainer() deleteing container " << container->name() << endl ;
  delete container ;
  container=0 ;
} ;

cVarContainerSingle::cVarContainerSingle(string name) : sname(name),ssize(0),single(0) {
} ;

cVarContainerSingle::cVarContainerSingle(string name, int nsize) : sname(name),ssize(nsize),single(0) {
} ;

cVarContainerSingle::~cVarContainerSingle() {
  delete single ;
} ;

cVarContainer* cVarContainerSingle::cobject() {
  if (single==0) {
    if (ssize<=0) {
      single = new cVarContainer(sname) ;
    }
    else {
      single = new cVarContainer(sname, ssize) ;
    }
  }
  return single ;
}

//
// ------------------ object stuff --------------------
//
// build a new instance
//   name = name of cVarContainer to build
//   self = returned new instance
//
extern "C" void F77NAME(cvc_new)(const char* name, cVarContainer** self, int* ier) {
  *ier=0 ;
  try {
    (*self) = new cVarContainer(name) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

//
// build a new instance specifying the hash set size
//   name = name of cVarContainer to build
//   nsize = number of quantities expected in the container
//   self = returned new instance
//
extern "C" void F77NAME(cvc_new_size)(const char* name, int* nsize, cVarContainer** self, int* ier) {
  *ier=0 ;
  try {
    (*self) = new cVarContainer(name,*nsize) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

//
// free the instance
//   self = instance
//
extern "C" void F77NAME(cvc_delete)(cVarContainer** self, int* ier) {
  *ier=0 ;
  try {
    delete (*self) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_copy)(cVarContainer** self, cVarContainer** you) {
  (*you) = (*self) ;
} ;

//
// hold onto a pointer for me
//   value = fortran pointer
//
extern "C" void F77NAME(cvc_hold)(void* value) {
  cvc_holdValue=value ;
} ;

//
// return nonzero if there is a held value
//
extern "C" int F77NAME(cvc_isheld)() {
  return (cvc_holdValue==0) ? 0 : 1 ;
} ;

//
// ----------- management ----------
//
extern "C" void F77NAME(cvc_show)(cVarContainer** self, int* ier) {
  *ier=0 ;
  try {
    cout << *((*self)->object()) << endl ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_name)(cVarContainer** self, int* clen, char* cname, int* ier) {
  *ier=0 ;
  string s = (*self)->object()->name() ;
  int ii = static_cast<int>(s.length()) ;
  if (*clen < (ii+1)) {
    cerr << "string argument is not large enough for the container name " << s << endl ;
    *ier=1 ;  
    return ;
  } ;
  s.copy(cname,string::npos) ;
  cname[ii]=0 ;
} ;

extern "C" void F77NAME(cvc_size)(cVarContainer** self, int* type, int* isize, int* ier) {
  *ier=0 ;
  try {
    *isize = (*self)->object()->size(static_cast<VarContainer::varType>(*type)) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_names)(cVarContainer** self, int* type, int* isize, int* ier) {
  *ier=0 ;
  (*self)->names_size=0 ;
  try {
    (*self)->names = (*self)->object()->names(static_cast<VarContainer::varType>(*type));
    (*self)->names_size = static_cast<int>((*self)->names.size()) ;
    (*self)->names_citer = (*self)->names.begin() ;
    *isize = (*self)->names_size ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_names_fetch)(cVarContainer** self, int* idone, int* clen, char* cname, int* ier) {
  *ier=0 ;
  *idone=0 ;
  if ((*self)->names_size<=0) {
    *idone=1 ;
    return ;
  } ;
  if ((*self)->names_citer == (*self)->names.end()) {
    *idone=1 ;
    (*self)->names_size=0 ;
    return ;
  } ;
  string s = *(*self)->names_citer ;
  int ii = static_cast<int>(s.length()) ;
  if (*clen < (ii+1)) {
    cerr << "string argument is not large enough for the variable name " << s << endl ;
    *ier=1 ;    // no advancement of iterator
    return ;
  } ;
  s.copy(cname,string::npos) ;
  cname[ii]=0 ;
  (*self)->names_citer++ ;  // advance when successful copy
} ;

extern "C" void F77NAME(cvc_has_key)(cVarContainer** self, const char* name, int* result, int* ier) {
  *ier=0 ;
  try {
    *result = (*self)->object()->has_key(name) ? 1 : 0 ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_remove)(cVarContainer** self, const char* name, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->remove(name) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_clear)(cVarContainer** self, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->clear() ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_gettype)(cVarContainer** self, const char* name, int* type, int* ier) {
  *ier=0 ;
  try {
    *type = static_cast<int>((*self)->object()->getType(name)) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getrank)(cVarContainer** self, const char* name, int* rank, int* ier) {
  *ier=0 ;
  try {
    *rank = static_cast<int>((*self)->object()->getRank(name)) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getshape)(cVarContainer** self, const char* name, const int* isize, int* shape, int* ier) {
  *ier=0 ;
  try {
    size_t rank = (*self)->object()->getRank(name) ;
    if (*isize < static_cast<int>(rank)) {
      cerr << "cvc_getShape: array argument is too small for rank of array" << endl ;
      cerr << "  expected: " << rank   << endl ;
      cerr << "  got:      " << *isize << endl ;
      *ier=1 ;
      return ;
    } ;
    const Shape& s = (*self)->object()->getShape(name) ;
    for (size_t i=0 ; i<rank ; i++) shape[i] = static_cast<int>(s[i]) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getinfo)(cVarContainer** self, const char* name, int* has_key, int* type, int* rank,
				     const int* isize, int* shape, int* ier) {
  *ier=0 ;
  try {
    *has_key   = 0 ;
    bool iskey = false ;
    VarContainer::varType t = VarContainer::NONE ;
    size_t k = (*isize>=0 && *isize<20) ? static_cast<size_t>(*isize) : 20 ;
    Shape s(k) ;
    size_t irank = 0 ;
    
    (*self)->object()->getInfo(name,iskey,t,irank,s) ;

    if (!iskey) return ;     // not in list
    
    *has_key = 1 ;
    if (*isize < static_cast<int>(irank)) {
      cerr << "cvc_getInfo: array argument is too small for rank of array" << endl ;
      cerr << "  expected: " << irank  << endl ;
      cerr << "  got:      " << *isize << endl ;
      *ier=1 ;
      return ;
    } ;
    *rank = static_cast<int>(irank) ;
    *type = static_cast<int>(t) ;
    for (size_t i=0 ; i<irank ; i++) shape[i] = static_cast<int>(s[i]) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_local)(cVarContainer** self, const char* name, int* iout, int* ier) {
  *ier=0 ;
  try {
    if (! (*self)->local_i32.isnull())   (*self)->local_i32   = aipPtr_32() ;
    if (! (*self)->local_f32.isnull())   (*self)->local_f32   = afpPtr_32() ;
    if (! (*self)->local_f64.isnull())   (*self)->local_f64   = afpPtr_64() ;
    if (! (*self)->local_l32.isnull())   (*self)->local_l32   = alpPtr_32() ;
    if (! (*self)->local_char.isnull())  (*self)->local_char  = acpPtr_char() ;
    if (! (*self)->local_bytes.isnull()) (*self)->local_bytes = abpPtr_bytes() ;

    if (name==0 || *name==0) return ;   // empty name

    if (! (*self)->object()->has_key(name)) {
      *ier=1 ;
      cerr << name << " is not in the container" << endl ;
      return ;
    } ;
    VarContainer::varType t = (*self)->object()->getType(name) ;

    switch(t) {
    case VarContainer::I32:
      cout << name << " is I32 (Integer*4)" << endl ; 
      (*self)->local_i32 = (*self)->object()->getInt32(name) ;
      if (*iout!=0) cout << (*self)->local_i32 << endl ;
      break ;
    case VarContainer::F32:
      cout << name << " is F32 (Real*4)" << endl ;
      (*self)->local_f32 = (*self)->object()->getFloat32(name) ;
      if (*iout!=0) cout << (*self)->local_f32 << endl ;
      break ;
    case VarContainer::F64:
      cout << name << " is F64 (Real*8)" << endl ;
      (*self)->local_f64 = (*self)->object()->getFloat64(name) ;
      if (*iout!=0) cout << (*self)->local_f64 << endl ;
      break ;
    case VarContainer::L32:
      cout << name << " is L32 (Logical*4)" << endl;
      (*self)->local_l32 = (*self)->object()->getLogical32(name) ;
      if (*iout!=0) cout << (*self)->local_l32 << endl ;
      break ;
    case VarContainer::CHAR:
      cout << name << " is CHAR (Character*(*))" << endl;
      (*self)->local_char = (*self)->object()->getChar(name) ;
      if (*iout!=0) cout << (*self)->local_char << endl ;
      break ;
    case VarContainer::BYTES:
      cout << name << " is BYTES (vector<Byte>)" << endl;
      (*self)->local_bytes = (*self)->object()->getBytes(name) ;
      if (*iout!=0) cout << (*self)->local_bytes << endl ;
      break ;
    default:
      cerr << name << " has an unknown type = " << t << endl ;
      *ier=1 ;
    } ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

//
// ---------------- attributes ---------------
//
extern "C" void F77NAME(cvc_att)(cVarContainer** self, const char* name, int* ier) {
  *ier=0 ;
  try {
    string s = (name==0) ? string() : string(name) ;
    (*self)->object()->att(s) ;
    if ((*self)->object()->temp_att()==0) {
      cerr << "no attributes -- variable called " << s << " does not exist" << endl ;
      *ier=1 ;
    } ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;
  
// get temporary attribute and set error flag if zero
fwrap_map_att* cvc_test_att_temp(cVarContainer** self, int* ier) {
  fwrap_map_att* p = (*self)->object()->temp_att() ;
  if (p==0) {
    cerr << "no attribute set has been selected (selection is cleared when the container is modified)" << endl ;
    *ier=1 ;
  } ;
  return p ;
} ;

extern "C" void F77NAME(cvc_att_copy)(cVarContainer** self, const char* name_src, const char* name_dest, int* ier) {
  *ier=0 ;
  try {
    string s = (name_src==0) ? string() : string(name_src) ;
    fwrap_map_att* fs = (*self)->object()->att(s) ;
    if (fs==0) {
      cerr << "no attributes -- variable called " << s << " does not exist" << endl ;
      *ier=1 ; return ;
    } ;

    string t = (name_dest==0) ? string() : string(name_dest) ;
    fwrap_map_att* ft = (*self)->object()->att(t) ;
    if (ft==0) {
      cerr << "no attributes -- variable called " << t << " does not exist" << endl ;
      *ier=1 ; return ;
    } ;

    if (ft!=fs) (*ft) = *fs ;  // copy over
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_att_size)(cVarContainer** self, int* inum, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    *inum = static_cast<int>(p->size()) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_clear)(cVarContainer** self, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    p->clear() ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_type)(cVarContainer** self, const char* name, int* itype, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    fwrap_att* a = p->get(name) ;
    fwrap_att::att_type t = (a==0) ? fwrap_att::none : a->get_type() ;
    *itype = static_cast<int>(t) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_vsize)(cVarContainer** self, const char* name, int* isize, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    fwrap_att* a = p->get(name) ;
    size_t t = (a==0) ? 0 : a->get_size() ;
    *isize = static_cast<int>(t) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_info)(cVarContainer** self, const char* name, int* itype, int* isize, int* maxLength,
                                      fwrap_att** ptr, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    fwrap_att* a = p->get(name) ;
    *ptr = a ;
    if (a==0) {
      *itype = static_cast<int>(fwrap_att::none) ;
      *isize = 0 ;
      *maxLength = 0 ;
    }
    else {
      fwrap_att::att_type t = a->get_type() ;
      *itype = static_cast<int>(t) ;
      *isize = static_cast<int>(a->get_size()) ;
      fwrap_string_att* s = dynamic_cast<fwrap_string_att*>(a) ;
      if ((t == fwrap_att::stype && s==0) || (t!=fwrap_att::stype && s!=0)) {
        cerr << "logic error, inconsistent type for attribute " << name << endl ;
        *ier=1 ; return ;
      }
      *maxLength = (s==0) ? 0 : static_cast<int>(s->get_maxLength()) ;
    }
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_get_int)(cVarContainer** self, const char* name, int* ival, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    *ival = p->get_int(name) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_get_vint)(fwrap_att** ptr, int* ilen, int* ival, int* ier)  {
  *ier=0 ;
  try {
    fwrap_int_att* itr = dynamic_cast<fwrap_int_att*>(*ptr) ;
    if (itr==0) {
      cerr << "?cvc_att_get_vint: non integer attribute passed to procedure" << endl ;
      *ier=1 ; return ;
    } ;
    itr->fill_int(ival, static_cast<size_t>(*ilen)) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;


extern "C" void F77NAME(cvc_att_get_double)(cVarContainer** self, const char* name, double* dval, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    *dval = p->get_double(name) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;


extern "C" void F77NAME(cvc_att_get_vdouble)(fwrap_att** ptr, int* dlen, double* dval, int* ier)  {
  *ier=0 ;
  try {
    fwrap_double_att* dtr = dynamic_cast<fwrap_double_att*>(*ptr) ;
    if (dtr==0) {
      cerr << "?cvc_att_get_vdouble: non double attribute passed to procedure" << endl ;
      *ier=1 ; return ;
    } ;
    dtr->fill_double(dval, static_cast<size_t>(*dlen)) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;


extern "C" void F77NAME(cvc_att_get_fp64)(cVarContainer** self, const char* name, fp_64* dval, int* ier) {
  double d ;
  F77NAME(cvc_att_get_double)(self, name, &d, ier) ;
  if (*ier!=0) return ;
  *dval = d ;
} ;

extern "C" void F77NAME(cvc_att_get_vfp64)(fwrap_att** ptr, int* dlen, fp_64* dval, int* ier)  {
  size_t sz = static_cast<size_t>(*dlen) ;
  double d[sz] ;
  F77NAME(cvc_att_get_vdouble)(ptr,dlen,d,ier) ;
  if (*ier==0) {
    for (size_t i=0 ; i<sz ; i++) dval[i]=d[i] ;
  }
}

extern "C" void F77NAME(cvc_att_get_slength)(cVarContainer** self, const char* name, int* ilen, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    string s = p->get_string(name) ;
    *ilen = s.length() ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_get_string)(cVarContainer** self, const char* name, int* clen, char* cname, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    string s = p->get_string(name) ;
    if ((*clen)<(s.length()+1)) {
      cerr << "need a larger string variable, got " << *clen << " needed " << (s.length()+1) 
	   << " for attribute " << name << endl ;
      if ((*clen)>0) cname[0]=0 ;  // terminate
      *ier=1 ;
    } 
    else {
      s.copy(cname,string::npos) ;
      cname[s.length()]=0 ;
    } ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_get_vxstring)(fwrap_att** ptr, int* ix, int* clen, char* cval, int* slen, int* ier)  {
  *ier=0 ;
  try {
    fwrap_string_att* str = dynamic_cast<fwrap_string_att*>(*ptr) ;
    if (str==0) {
      cerr << "?cvc_att_get_vxstring: non string attribute passed to procedure" << endl ;
      *ier=1 ; return ;
    } ;
    string s = str->getx_string(static_cast<size_t>(*ix)) ;
    size_t sz = s.length() ;
    size_t cz = static_cast<size_t>(*clen);
    *slen = static_cast<int>(sz) ;
    if (sz>cz) {
      cerr << "not enough space in char array argument when fetching string attribute, arg size=" << cz 
           << ", att size=" << sz << endl ;
    }
    s.copy(cval,cz) ;
    if (sz<cz) cval[sz]=0 ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}


extern "C" void F77NAME(cvc_att_get_intdef)(cVarContainer** self, const char* name, int* ival, int* ier, int* def) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    *ival = p->get_int(name,*def) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_get_vintdef)(fwrap_att** ptr, int* ilen, int* ival, int* ier, int* dlen, int* dval, int* iout) {
  *ier=0 ;
  try {
    fwrap_int_att* itr = dynamic_cast<fwrap_int_att*>(*ptr) ;
    if (itr!=0) {
      size_t sz = itr->get_size() ;
      size_t iz = static_cast<size_t>(*ilen) ;
      if (iz>=sz) {
        itr->fill_int(ival,iz) ;
        *iout = static_cast<int>(sz);    // normal path
        return ;
      }
    }
    if (*ilen<*dlen) {
      cerr << "default array is too large for argument array" << endl ;
      *ier=1 ; return ;
    }
    *iout = *dlen ;
    size_t dz = static_cast<size_t>(*dlen) ;
    for (size_t i=0 ; i<dz ; i++) ival[i]=dval[i] ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}


extern "C" void F77NAME(cvc_att_get_doubledef)(cVarContainer** self, const char* name, double* dval, int* ier, double* def) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    *dval = p->get_double(name,*def) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;


extern "C" void F77NAME(cvc_att_get_vdoubledef)(fwrap_att** ptr, int* ilen, double* ival, int* ier, int* dlen, double* dval, int* iout) {
  *ier=0 ;
  try {
    fwrap_double_att* itr = dynamic_cast<fwrap_double_att*>(*ptr) ;
    if (itr!=0) {
      size_t sz = itr->get_size() ;
      size_t iz = static_cast<size_t>(*ilen) ;
      if (iz>=sz) {
        itr->fill_double(ival,iz) ;
        *iout = static_cast<int>(sz);    // normal path
        return ;
      }
    }
    if (*ilen<*dlen) {
      cerr << "default array is too large for argument array" << endl ;
      *ier=1 ; return ;
    }
    *iout = *dlen ;
    size_t dz = static_cast<size_t>(*dlen) ;
    for (size_t i=0 ; i<dz ; i++) ival[i]=dval[i] ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}



extern "C" void F77NAME(cvc_att_get_fp64def)(cVarContainer** self, const char* name, fp_64* dval, int* ier, fp_64* def) {
  double d ;
  double ddef = *def ;
  F77NAME(cvc_att_get_doubledef)(self, name, &d, ier, &ddef) ;
  if (*ier!=0) return ;
  *dval = d ;
} ;

extern "C" void F77NAME(cvc_att_get_vfp64def)(fwrap_att** ptr, int* ilen, fp_64* ival, int* ier, int* dlen, fp_64* dval, int* iout) {
  size_t sz = static_cast<size_t>(*ilen) ;
  double d[sz] ;
  size_t dz = static_cast<size_t>(*dlen) ;
  double dd[dz] ;
  for (int i=0 ; i<dz ; i++) dd[i]=dval[i] ;
  F77NAME(cvc_att_get_vdoubledef)(ptr,ilen,d,ier,dlen,dd,iout) ;
  dz = static_cast<size_t>(*iout);
  if (*ier==0) {
    for (size_t i=0 ; i<dz ; i++) ival[i]=d[i] ;
  }
}


extern "C" void F77NAME(cvc_att_get_slengthdef)(cVarContainer** self, const char* name, int* ilen, int* ier, const char* def) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    string s = p->get_string(name,def) ;
    *ilen = s.length() ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_get_stringdef)(cVarContainer** self, const char* name, int* clen, char* cname, int* ier,
					       const char* def) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    string s = p->get_string(name,def) ;
    if ((*clen)<(s.length()+1)) {
      cerr << "need a larger string variable, got " << *clen << " needed " << (s.length()+1) 
	   << " for attribute " << name << " with value '" << s << "'" << endl ;
      if ((*clen)>0) cname[0]=0 ;
      *ier=1 ;
    } 
    else {
      s.copy(cname,string::npos) ;
      cname[s.length()]=0 ;
    } ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;


extern "C" void F77NAME(cvc_att_remove)(cVarContainer** self, const char* name, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    p->remove(name) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_set_int)(cVarContainer** self, const char* name, int* ival, int* ier){
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    p->set(name,*ival) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}  ;

extern "C" void F77NAME(cvc_att_set_vint)(cVarContainer** self, const char* name, int* ilen, int* ival, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    p->set(name,ival,static_cast<size_t>(*ilen)) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}


extern "C" void F77NAME(cvc_att_set_double)(cVarContainer** self, const char* name, double* dval, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    p->set(name,*dval) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_set_vdouble)(cVarContainer** self, const char* name, int* ilen, double* ival, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    p->set(name,ival,static_cast<size_t>(*ilen)) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_att_set_fp64)(cVarContainer** self, const char* name, fp_64* dval, int* ier) {
  double d = *dval ;
  F77NAME(cvc_att_set_double)(self, name,&d,ier) ;
} ;

extern "C" void F77NAME(cvc_att_set_vfp64)(cVarContainer** self, const char* name, int* ilen, fp_64* ival, int* ier) {
  size_t sz = static_cast<size_t>(*ilen) ;
  double d[sz] ;
  for (size_t i=0 ; i<sz ; i++) d[i]=ival[i] ;
  F77NAME(cvc_att_set_vdouble)(self,name,ilen,d,ier) ;
}

extern "C" void F77NAME(cvc_att_set_string)(cVarContainer** self, const char* name, const char* cval, int* ier) {
  *ier=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    string s = cval ;
    p->set(name,s) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_att_create_vstring)(cVarContainer** self, const char* name, int* isize, fwrap_att** ptr, int* ier) {
  *ier=0 ;
  string* sa = 0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    size_t sz = static_cast<size_t>(*isize) ;
    sa = new string[sz] ;
    p->set(name,sa,sz) ;
    delete[] sa ;
    sa = 0 ;
    *ptr = p->get(name) ;
  }
  catch (std::exception& e) {
    if (sa!=0) delete[] sa ;
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_att_set_vxstring)(fwrap_att** ptr, int* ix, int* clen, const char* cval, int* ier)  {
  *ier=0 ;
  try {
    fwrap_string_att* str = dynamic_cast<fwrap_string_att*>(*ptr) ;
    if (str==0) {
      cerr << "?cvc_att_set_vxstring: non string attribute passed to procedure" << endl ;
      *ier=1 ; return ;
    } ;
    string s(cval, static_cast<size_t>(*clen)) ;
    str->setx_string(static_cast<size_t>(*ix), s) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}


extern "C" void F77NAME(cvc_att_names)(cVarContainer** self, int* isize, int* ier){
  *ier=0 ;
  (*self)->att_names_size=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    (*self)->att_names = p->keys() ;
    (*self)->att_names_size = static_cast<int>((*self)->att_names.size()) ;
    (*self)->att_names_citer = (*self)->att_names.begin() ;
    *isize = (*self)->att_names_size ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}  ;

extern "C" void F77NAME(cvc_att_name_fetch)(cVarContainer** self, int* idone, int* clen, char* cname, int* ier){
  *ier=0 ;
  *idone=0 ;
  try {
    fwrap_map_att* p = cvc_test_att_temp(self,ier) ; if (p==0) return ;
    if ((*self)->att_names_size<=0) {
      *idone=1 ;
      return ;
    } ;
    if ((*self)->att_names_citer == (*self)->att_names.end()) {
      *idone=1 ;
      (*self)->att_names_size=0 ;
      return ;
    } ;
    string s = *(*self)->att_names_citer ;
    int ii = static_cast<int>(s.length()) ;
    if (*clen < (ii+1)) {
      cerr << "string argument is not large enough for the attribute name " << s << endl ;
      *ier=1 ;    // no advancement of iterator
      return ;
    } ;
    s.copy(cname,string::npos) ;
    cname[ii]=0 ;
    (*self)->att_names_citer++ ;  // advance when successful copy
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}  ;


//
// ---------------- netcdf -----------------
//
extern "C" void F77NAME(cvc_ncsave)(cVarContainer** self, const char* filename, int* option, int* ier,
				    const int* nyes, const int* kyes, const char* syes,
				    const int* nno,  const int* kno,  const char* sno) {
  *ier=0 ;
  list<string> yes ;
  list<string> no  ;

  for (size_t k=0 ; k<*nyes ; k++) yes.push_back(string(syes+kyes[k])) ;   // segfault potential
  for (size_t k=0 ; k<*nno  ; k++) no.push_back(string(sno+kno[k])) ;

  try {
    (*self)->object()->ncsave(filename,*option,yes,no) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_ncload)(cVarContainer** self, const char* filename, int* option, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->ncload(filename,*option) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

//
// ------------- update ----------------
//
extern "C" void F77NAME(cvc_update)(cVarContainer** self, cVarContainer** you, int* option, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->update(*((*you)->object()),*option) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

//
// ---------------- share -----------------
//
extern "C" void F77NAME(cvc_share)(cVarContainer** self, cVarContainer** you, 
				    const int* nnames, const int* knames, const char* snames,
				    const int* option, int* ier) {
  *ier=0 ;
  list<string> names ;

  for (size_t k=0 ; k<*nnames ; k++) names.push_back(string(snames+knames[k])) ;   // segfault potential

  try {
    (*self)->object()->share(*((*you)->object()),names,*option) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

//
// ---------------- send,receive -----------------
//
extern "C" void F77NAME(cvc_sendfile)(cVarContainer** self, const char* filename, int* ier,
				    const int* nnames, const int* knames, const char* snames,
				    const int* nyes,   const int* kyes,   const char* syes,
				    const int* nno,    const int* kno,    const char* sno) {
  *ier=0 ;
  list<string> names ;
  list<string> yes ;
  list<string> no  ;

  for (size_t k=0 ; k<*nnames ; k++) names.push_back(string(snames+knames[k])) ;   // segfault potential
  for (size_t k=0 ; k<*nyes   ; k++) yes.push_back(string(syes+kyes[k])) ; 
  for (size_t k=0 ; k<*nno    ; k++) no.push_back(string(sno+kno[k])) ;

  try {
    VarFileWriter writer(filename) ;
    (*self)->object()->send(&writer,names,yes,no) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_receivefile)(cVarContainer** self, const char* filename, int* option, int* ier,
				    const int* nkeep,    const int* kkeep,    const char* skeep,
				    const int* nprotect, const int* kprotect, const char* sprotect) {
  *ier=0 ;
  list<string> keep ;
  list<string> protect  ;

  for (size_t k=0 ; k<*nkeep    ; k++) keep.push_back(string(skeep+kkeep[k])) ; 
  for (size_t k=0 ; k<*nprotect ; k++) protect.push_back(string(sprotect+kprotect[k])) ;

  try {
    VarFileReader reader(filename) ;
    (*self)->object()->receive(&reader,*option,keep,protect) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;


//
// ----------------- Integer*4 ---------------
//
extern "C" void F77NAME(cvc_getscalarint32)(cVarContainer** self, const char* name, ip_32* value, int* ier) {
  *ier=0 ;
  try {
    *value = (*self)->object()->getScalarInt32(name) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getdefscalarint32)(cVarContainer** self, const char* name, ip_32* value, const ip_32* def, int* ier) {
  *ier=0 ;
  try {
    *value = (def==0) ? (*self)->object()->getScalarInt32(name) : (*self)->object()->getDefScalarInt32(name,*def) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setscalarint32)(cVarContainer** self, const char* name, const ip_32* value, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->set(name,*value) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_addscalarint32)(cVarContainer** self, const char* name, const ip_32* value, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->add(name,*value) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getint32)(cVarContainer** self, const char* name, const int* rank, const int* shape, ip_32* value, 
				      int* ier) {
  F77NAME(cvc_getdefint32)(self,name,rank,shape,value,0,0,0,ier) ;
};

extern "C" void F77NAME(cvc_getdefint32)(cVarContainer** self, const char* name, const int* rank, const int* shape, ip_32* value, 
					 const int* drank, const int* dshape, ip_32* dvalue, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    aip_32    f = aip_32(s,value) ;
    aipPtr_32 d = aipPtr_32() ;
    if (drank!=0) {
      if (*drank != *rank) {
	    cerr << "cvc_getDefInt32: default rank differs from input rank" << endl ;
	    *ier=1 ;
	    return ;
      } ;
      for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(dshape[i]) ;
      d = aipPtr_32::build(s,dvalue) ;
    } ;
    (*self)->object()->get(name,f,d) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setint32)(cVarContainer** self, const char* name, const int* rank, const int* shape, ip_32* value, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    aip_32 f = aip_32(s,value) ;
    (*self)->object()->set(name,f) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_addint32)(cVarContainer** self, const char* name, const int* rank, const int* shape, ip_32* avalue, 
				      int* inew, int* ier) {
  *ier=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<ip_32*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    ip_32* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    aipPtr_32 f = aipPtr_32::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      aip_32 g(s,avalue) ;
      fwrapCopy(g,*f) ;
    } ;
    (*self)->object()->add(name,f) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_replaceint32)(cVarContainer** self, const char* name, const int* rank, const int* shape, ip_32* avalue, 
					  int* inew, int* ioption, int* istatus, int* ier) {
  *ier=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<ip_32*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    ip_32* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) { s[i]=static_cast<size_t>(shape[i]) ; } ;
    aipPtr_32 f = aipPtr_32::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      aip_32 g(s,avalue) ;
      fwrapCopy(g,*f) ;
    } ;
    bool istat = (*self)->object()->replace(name,*ioption,f) ;
    *istatus = istat ? 1 : 0 ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_arrayint32)(cVarContainer** self, const char* name, aip_32** you, int* ier) {
  *ier=0 ;
  try {
    aipPtr_32 f = (*self)->object()->getInt32(name) ;
    *you = f.pointer() ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_add0int32)(cVarContainer** self, const char* name, const int* rank, const int* shape, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    (*self)->object()->add0(name,VarContainer::I32,s) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;


//
// ----------------- Real*4 ---------------
//
extern "C" void F77NAME(cvc_getscalarfloat32)(cVarContainer** self, const char* name, fp_32* value, int* ier) {
  *ier=0 ;
  try {
    *value = (*self)->object()->getScalarFloat32(name) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getdefscalarfloat32)(cVarContainer** self, const char* name, fp_32* value, const fp_32* def, int* ier) {
  *ier=0 ;
  try {
    *value = (def==0) ? (*self)->object()->getScalarFloat32(name) : (*self)->object()->getDefScalarFloat32(name,*def) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setscalarfloat32)(cVarContainer** self, const char* name, const fp_32* value, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->set(name,*value) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_addscalarfloat32)(cVarContainer** self, const char* name, const fp_32* value, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->add(name,*value) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getfloat32)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_32* value, 
					int* ier) {
  F77NAME(cvc_getdeffloat32)(self,name,rank,shape,value,0,0,0,ier) ;
};

extern "C" void F77NAME(cvc_getdeffloat32)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_32* value, 
					   const int* drank, const int* dshape, fp_32* dvalue, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    afp_32    f = afp_32(s,value) ;
    afpPtr_32 d = afpPtr_32() ;
    if (drank!=0) {
      if (*drank != *rank) {
	    cerr << "cvc_getDefFloat32: default rank differs from input rank" << endl ;
	    *ier=1 ;
	    return ;
      } ;
      for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(dshape[i]) ;
      d = afpPtr_32::build(s,dvalue) ;
    } ;
    (*self)->object()->get(name,f,d) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setfloat32)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_32* value, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    afp_32 f = afp_32(s,value) ;
    (*self)->object()->set(name,f) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_addfloat32)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_32* avalue, 
					int* inew, int* ier) {
  *ier=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<fp_32*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    fp_32* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    afpPtr_32 f = afpPtr_32::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      afp_32 g(s,avalue) ;
      fwrapCopy(g,*f) ;
    } ;
    (*self)->object()->add(name,f) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_arrayfloat32)(cVarContainer** self, const char* name, afp_32** you, int* ier) {
  *ier=0 ;
  try {
    afpPtr_32 f = (*self)->object()->getFloat32(name) ;
    *you = f.pointer() ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_add0float32)(cVarContainer** self, const char* name, const int* rank, const int* shape, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    (*self)->object()->add0(name,VarContainer::F32,s) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_replacefloat32)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_32* avalue, 
					    int* inew, int* ioption, int* istatus, int* ier) {
  *ier=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<fp_32*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    fp_32* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) { s[i]=static_cast<size_t>(shape[i]) ; } ;
    afpPtr_32 f = afpPtr_32::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      afp_32 g(s,avalue) ;
      fwrapCopy(g,*f) ;
    } ;
    bool istat = (*self)->object()->replace(name,*ioption,f) ;
    *istatus = istat ? 1 : 0 ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;


//
// ----------------- Real*8 ---------------
//
extern "C" void F77NAME(cvc_getscalarfloat64)(cVarContainer** self, const char* name, fp_64* value, int* ier) {
  *ier=0 ;
  try {
    *value = (*self)->object()->getScalarFloat64(name) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getdefscalarfloat64)(cVarContainer** self, const char* name, fp_64* value, const fp_64* def, int* ier) {
  *ier=0 ;
  try {
    *value = (def==0) ? (*self)->object()->getScalarFloat64(name) : (*self)->object()->getDefScalarFloat64(name,*def) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setscalarfloat64)(cVarContainer** self, const char* name, const fp_64* value, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->set(name,*value) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_addscalarfloat64)(cVarContainer** self, const char* name, const fp_64* value, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->add(name,*value) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getfloat64)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_64* value, 
					int* ier) {
  F77NAME(cvc_getdeffloat64)(self,name,rank,shape,value,0,0,0,ier) ;
};

extern "C" void F77NAME(cvc_getdeffloat64)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_64* value, 
					   const int* drank, const int* dshape, fp_64* dvalue, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    afp_64    f = afp_64(s,value) ;
    afpPtr_64 d = afpPtr_64() ;
    if (drank!=0) {
      if (*drank != *rank) {
	    cerr << "cvc_getDefFloat64: default rank differs from input rank" << endl ;
	    *ier=1 ;
	    return ;
      } ;
      for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(dshape[i]) ;
      d = afpPtr_64::build(s,dvalue) ;
    } ;
    (*self)->object()->get(name,f,d) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setfloat64)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_64* value, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    afp_64 f = afp_64(s,value) ;
    (*self)->object()->set(name,f) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_addfloat64)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_64* avalue, 
					int* inew, int* ier) {
  *ier=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<fp_64*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    fp_64* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    afpPtr_64 f = afpPtr_64::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      afp_64 g(s,avalue) ;
      fwrapCopy(g,*f) ;
    } ;
    (*self)->object()->add(name,f) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_arrayfloat64)(cVarContainer** self, const char* name, afp_64** you, int* ier) {
  *ier=0 ;
  try {
    afpPtr_64 f = (*self)->object()->getFloat64(name) ;
    *you = f.pointer() ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_add0float64)(cVarContainer** self, const char* name, const int* rank, const int* shape, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    (*self)->object()->add0(name,VarContainer::F64,s) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_replacefloat64)(cVarContainer** self, const char* name, const int* rank, const int* shape, fp_64* avalue, 
					    int* inew, int* ioption, int* istatus, int* ier) {
  *ier=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<fp_64*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    fp_64* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) { s[i]=static_cast<size_t>(shape[i]) ; } ;
    afpPtr_64 f = afpPtr_64::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      afp_64 g(s,avalue) ;
      fwrapCopy(g,*f) ;
    } ;
    bool istat = (*self)->object()->replace(name,*ioption,f) ;
    *istatus = istat ? 1 : 0 ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;


//
// ----------------- Logical*4 ---------------
//
extern "C" void F77NAME(cvc_settruefalse)(cVarContainer** self, lp_32* truevalue, lp_32* falsevalue, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->setLogicalTrue(*truevalue) ;
    (*self)->object()->setLogicalFalse(*falsevalue) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }    
} ;

extern "C" void F77NAME(cvc_getscalarlogical32)(cVarContainer** self, const char* name, lp_32* value, int* ier) {
  *ier=0 ;
  try {
    *value = (*self)->object()->getScalarLogical32(name) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getdefscalarlogical32)(cVarContainer** self, const char* name, lp_32* value, const lp_32* def, int* ier) {
  *ier=0 ;
  try {
    *value = (def==0) ? (*self)->object()->getScalarLogical32(name) : (*self)->object()->getDefScalarLogical32(name,*def) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setscalarlogical32)(cVarContainer** self, const char* name, const lp_32* value, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->setLogical(name,*value) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_addscalarlogical32)(cVarContainer** self, const char* name, const lp_32* value, int* ier) {
  *ier=0 ;
  try {
    (*self)->object()->addLogical(name,*value) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getlogical32)(cVarContainer** self, const char* name, const int* rank, const int* shape, lp_32* value, 
					  int* ier) {
  F77NAME(cvc_getdeflogical32)(self,name,rank,shape,value,0,0,0,ier) ;
};

extern "C" void F77NAME(cvc_getdeflogical32)(cVarContainer** self, const char* name, const int* rank, const int* shape, lp_32* value, 
					     const int* drank, const int* dshape, lp_32* dvalue, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    alp_32    f = alp_32(s,value) ;
    alpPtr_32 d = alpPtr_32() ;
    if (drank!=0) {
      if (*drank != *rank) {
	    cerr << "cvc_getDefLogical32: default rank differs from input rank" << endl ;
	    *ier=1 ;
	    return ;
      } ;
      for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(dshape[i]) ;
      d = alpPtr_32::build(s,dvalue) ;
    } ;
    (*self)->object()->getLogical(name,f,d) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setlogical32)(cVarContainer** self, const char* name, const int* rank, const int* shape, lp_32* value, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    alp_32 f = alp_32(s,value) ;
    (*self)->object()->setLogical(name,f) ;
  }
    catch (std::exception& e) {
      cerr << e.what() << endl ;
      *ier=1 ;
    }
};

extern "C" void F77NAME(cvc_addlogical32)(cVarContainer** self, const char* name, const int* rank, const int* shape, lp_32* avalue, 
					  int* inew, int* ier) {
  *ier=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<lp_32*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    lp_32* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    alpPtr_32 f = alpPtr_32::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      alp_32 g(s,avalue) ;
      fwrapCopy(g,*f) ;
    } ;
    (*self)->object()->addLogical(name,f) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_arraylogical32)(cVarContainer** self, const char* name, alp_32** you, int* ier) {
  *ier=0 ;
  try {
    alpPtr_32 f = (*self)->object()->getLogical32(name) ;
    *you = f.pointer() ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_add0logical32)(cVarContainer** self, const char* name, const int* rank, const int* shape, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    (*self)->object()->add0(name,VarContainer::L32,s) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_replacelogical32)(cVarContainer** self, const char* name, const int* rank, const int* shape, lp_32* avalue, 
					      int* inew, int* ioption, int* istatus, int* ier) {
  *ier=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<lp_32*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    lp_32* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) { s[i]=static_cast<size_t>(shape[i]) ; } ;
    alpPtr_32 f = alpPtr_32::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      alp_32 g(s,avalue) ;
      fwrapCopy(g,*f) ;
    } ;
    bool istat = (*self)->object()->replaceLogical(name,*ioption,f) ;
    *istatus = istat ? 1 : 0 ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

//
// ----------------- Character ---------------
//
extern "C" void F77NAME(cvc_getscalarstring)(cVarContainer** self, const char* name, 
                        const int* len, cp_char* value, int* ier) {
  *ier=0 ;
  try {
    acp_string s ;                                        // new scalar string as fwrap
    s()=(*self)->object()->getScalarString(name) ;        // fetch as string
    acp_char v(static_cast<size_t>(*len),value) ;         // fwrap as char[len]
    fwrapCopy_char(s,v) ;                                 // copy to v
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getdefscalarstring)(cVarContainer** self, const char* name, 
                        const int*  len, cp_char* value, 
                        const int* dlen, const cp_char* def, int* ier) {
  *ier=0 ;
  try {
    acp_string s ;  // new scalar string as fwrap    
    
    if (def==0) {
      s() = (*self)->object()->getScalarString(name) ;   // fetch as string    
    }
    else {
      const acp_char d(static_cast<size_t>(*dlen),const_cast<cp_char*>(def)) ;  // fwrap default as char[len]
      acp_string ds ;                                     // represent default as a string
      fwrapCopy_char(d,ds) ;                              // copy default to ds 
      
      s() = (*self)->object()->getDefScalarString(name,ds()) ;  // fetch as string    
    }
    acp_char v(static_cast<size_t>(*len),value) ;         // fwrap as char[len]
    fwrapCopy_char(s,v) ;                                 // copy to v
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setscalarstring)(cVarContainer** self, const char* name, 
                const int* len, const cp_char* value, int* ier) {
  *ier=0 ;
  try {
    acp_string s ;                                       // new scalar string as fwrap    
    const acp_char v(static_cast<size_t>(*len),const_cast<cp_char*>(value)) ;  // fwrap as char[len]
    fwrapCopy_char(v,s) ;                                // copy to v

    (*self)->object()->set(name,s) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_addscalarstring)(cVarContainer** self, const char* name, 
                const int* len, const cp_char* value, int* ier) {
  *ier=0 ;
  try {
    acp_string s  ;                                      // new scalar string as fwrap    
    const acp_char v(static_cast<size_t>(*len),const_cast<cp_char*>(value)) ;  // fwrap as char[len]
    fwrapCopy_char(v,s) ;                                // copy to v

    (*self)->object()->add(name,s()) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_getchar)(cVarContainer** self, const char* name, const int* rank, const int* shape, cp_char* value, 
				      int* ier) {
  F77NAME(cvc_getdefchar)(self,name,rank,shape,value,0,0,0,ier) ;
};

extern "C" void F77NAME(cvc_getdefchar)(cVarContainer** self, const char* name, const int* rank, const int* shape, cp_char* value, 
					 const int* drank, const int* dshape, cp_char* dvalue, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    acp_char    f = acp_char(s,value) ;
    acpPtr_char d = acpPtr_char() ;
    if (drank!=0) {
      if (*drank != *rank) {
	    cerr << "cvc_getDefChar: default rank differs from input rank" << endl ;
	    *ier=1 ;
	    return ;
      } ;
      for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(dshape[i]) ;
      d = acpPtr_char::build(s,dvalue) ;
    } ;
    (*self)->object()->get(name,f,d) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_setchar)(cVarContainer** self, const char* name, const int* rank, const int* shape, cp_char* value, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    acp_char f = acp_char(s,value) ;
    (*self)->object()->set(name,f) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_addchar)(cVarContainer** self, const char* name, const int* rank, const int* shape, cp_char* avalue, 
				      int* inew, int* ier) {
  *ier=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<cp_char*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    cp_char* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    acpPtr_char f = acpPtr_char::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      acp_char g(s,avalue) ;
      fwrapCopy_char(g,*f) ;
    } ;
    (*self)->object()->add(name,f) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
};

extern "C" void F77NAME(cvc_replacechar)(cVarContainer** self, const char* name, const int* rank, const int* shape, cp_char* avalue, 
					  int* inew, int* ioption, int* istatus, int* ier) {
  *ier=0 ;
  *istatus=0 ;
  try {
    if (cvc_holdValue!=0) {
      avalue=static_cast<cp_char*>(cvc_holdValue) ;
      cvc_holdValue=0 ;
    } ;
    cp_char* value = (*inew!=0) ? 0 : avalue ;                     // force the creation of a new storage array
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) { s[i]=static_cast<size_t>(shape[i]) ; } ;
    acpPtr_char f = acpPtr_char::build(s,value) ;
    if (avalue!=value && avalue!=0) {
      // copy from avalue to new data structure
      acp_char g(s,avalue) ;
      fwrapCopy_char(g,*f) ;
    } ;
    bool istat = (*self)->object()->replace(name,*ioption,f) ;
    *istatus = istat ? 1 : 0 ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_arraychar)(cVarContainer** self, const char* name, acp_char** you, int* ier) {
  *ier=0 ;
  try {
    acpPtr_char f = (*self)->object()->getChar(name) ;
    *you = f.pointer() ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

extern "C" void F77NAME(cvc_add0char)(cVarContainer** self, const char* name, const int* rank, const int* shape, int* ier) {
  *ier=0 ;
  try {
    size_t srank = static_cast<size_t>(*rank) ;
    Shape s(srank) ;
    for (size_t i=0 ; i<srank ; i++) s[i]=static_cast<size_t>(shape[i]) ;
    (*self)->object()->add0(name,VarContainer::CHAR,s) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
} ;

//
// ----------------- Bytes ---------------
//
extern "C" void F77NAME(cvc_sizescalarbytes)(cVarContainer** self, const char* name, int* olen, 
             int* ier) {
  *olen=0 ;
  *ier=0 ;
  try {
    const Bytes& b = (*self)->object()->getScalarBytes(name) ;      // fetch as bytes
    *olen = static_cast<int>(b.size()) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_getscalarbytes)(cVarContainer** self, const char* name, 
              const int* len, Byte* value, int* olen, int* ier) { 
  *olen=0 ;
  *ier=0 ;
  try {
    const Bytes& b  = (*self)->object()->getScalarBytes(name) ;      // fetch as bytes
    size_t       bs = b.size() ;
    
    *olen = static_cast<int>(bs) ;
    if ((*olen)>(*len)) {
      cerr << "?cvc_getscalarbytes: in container "+(*self)->object()->name()+", Bytes argument length="
           <<(*len)<<" is too small for " << name << " with data length=" << (*olen) << endl ;
      *ier=1 ; return ;
    }
    Byte* p = value ;
    for (size_t i=0 ; i<bs ; i++) *p++ = b[i] ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_getdefscalarbytes)(cVarContainer** self, const char* name,  
               const int* len, Byte* value, const int* dlen, const Byte* def, int* olen, int* ier) { 
  *olen=0 ;
  *ier=0 ;
  try {
    if (def==0) {
      F77NAME(cvc_getscalarbytes)(self, name, len, value, olen, ier) ;
    }
    else {
      size_t ds = static_cast<size_t>(*dlen) ;
      Bytes  d(ds) ;
      const Byte* q = def ;
      for (size_t i=0 ; i<ds ; i++) d[i] = *q++ ;
      
      const Bytes& b  = (*self)->object()->getDefScalarBytes(name,d) ;      // fetch as bytes
      size_t       bs = b.size() ;
    
      *olen = static_cast<int>(bs) ;
      if ((*olen)>(*len)) {
        cerr << "?cvc_getdefscalarbytes: in container "+(*self)->object()->name()+", Byte argument length="
             <<(*len)<<" is too small for " << name << " or default data with length=" << (*olen) << endl ;
        *ier=1 ; return ;
      }
      Byte* p = value ;
      for (size_t i=0 ; i<bs ; i++) *p++ = b[i] ;
    }
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_setscalarbytes)(cVarContainer** self, const char* name, 
               const int* len, const Byte* value, int* ier){ 
  *ier=0 ;
  try {
    size_t vlen = static_cast<size_t>(*len) ;
    Bytes  v(vlen) ;
    const Byte* p = value ;
    for (size_t i=0 ; i<vlen ; i++) v[i] = *p++ ;
    (*self)->object()->set(name,v) ;    
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_addscalarbytes)(cVarContainer** self, const char* name,
                const int* len,  const Byte* value, int* ier){ 
  *ier=0 ;
  try {
    size_t vlen = static_cast<size_t>(*len) ;
    Bytes  v(vlen) ;
    const Byte* p = value ;
    for (size_t i=0 ; i<vlen ; i++) v[i] = *p++ ;
    (*self)->object()->add(name,v) ;    
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_replacescalarbytes)(cVarContainer** self, const char* name,
                 const int* len,  const Byte* value, const int* ioption, int* istatus, int* ier){ 
  *ier=0 ;
  *istatus=0 ;
  try {
    size_t vlen = static_cast<size_t>(*len) ;
    Bytes  v(vlen) ;
    const Byte* p = value ;
    for (size_t i=0 ; i<vlen ; i++) v[i] = *p++ ;
    bool istat = (*self)->object()->replace(name,*ioption,v) ;    
    *istatus = istat ? 1 : 0 ;    
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_getscalarbytestofile)(cVarContainer** self, const char* name, 
                 const char* file, int* ier) { 
  *ier=0 ;
  try {
    (*self)->object()->getScalarBytesToFile(name,file) ;    
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_setscalarbytesfromfile)(cVarContainer** self, const char* name, 
                 const char* file, int* ier){ 
  *ier=0 ;
  try {
    (*self)->object()->setScalarBytesFromFile(name,file) ;    
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_addscalarbytesfromfile)(cVarContainer** self, const char* name, 
                 const char* file, int* ier){ 
  *ier=0 ;
  try {
    (*self)->object()->addScalarBytesFromFile(name,file) ;    
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_replacescalarbytesfromfile)(cVarContainer** self, const char* name, 
                  const char* file, const int* ioption, int* istatus, int* ier){ 
  *ier=0 ;
  *istatus=0 ;
  try {
    bool istat = (*self)->object()->replaceScalarBytesFromFile(name,*ioption,file) ;
    *istatus = istat ? 1 : 0 ;    
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}

extern "C" void F77NAME(cvc_add0bytes)(cVarContainer** self, const char* name, int* ier) {
  *ier=0 ;
  try {
    Shape s(0) ;
    (*self)->object()->add0(name,VarContainer::BYTES,s) ;
  }
  catch (std::exception& e) {
    cerr << e.what() << endl ;
    *ier=1 ;
  }
}


#endif
