
// Produce a list of all vars in input
// each element of att list  is of type NC_STRING
// nb get_var_in() can take a single argument that can be a var name or regular expression
// lists are  of type NC_STRING and can are in essence ragged arrays
@all=get_vars_in();

*sz=@all.size();
*idx=0;


// sort vars into three buckets     
// DOUBLE, FLOAT, INT
for(idx=0;idx<sz;idx++)
{
  // remember @var_nm is of type NC_STRING
  @var_nm=@all(idx);
  ltype=*@var_nm.type();

  // push is quite clever if it is called with a "&" (reference) att
  // and that att doesnt exist then it is created on the fly
  if(ltype==NC_DOUBLE)
   push(&@dv, @var_nm);
  else if(ltype==NC_FLOAT)
     push(&@fv, @var_nm);     
  else if(ltype==NC_INT)
     push(&@iv, @var_nm);

}



// change missing values
sz=@dv.size();

print("starting doubles\n");

for(idx=0;idx<sz;idx++)
{
  @var_nm=@dv(idx);

  if( *@var_nm.ndims()>0)
  { 
    *@var_nm=*@var_nm;  
    *@var_nm.change_miss(-666.0);
  }
}

print("starting floats\n");

// average floats
sz=@fv.size();

for(idx=0;idx<sz;idx++)
{
  // we wish to append "_avg" to the var_nm. Since we cant easily append to NC_STRING 
  // we convert the  NC_STRING to type NC_CHAR using sprint()
  // then we can easily append to the NC_CHAR using push() again
  @var_nm=sprint(@fv(idx)); 
  @var_nm_avg=push(@var_nm,"_avg");
  
  if(*@var_nm.ndims() >0)
     *@var_nm_avg=*@var_nm.avg();  

}


// permute last two dims of ints
sz=@iv.size();

print("starting ints\n");

for(idx=0;idx<sz;idx++)
{
  @var_nm=@iv(idx);
          
  /// since we dont know the names of the dims -we can use placeholder arguments
  if(*@var_nm.ndims() ==3)
    *@var_nm=*@var_nm.permute($0,$2,$1);    
  else if(*@var_nm.ndims() ==4)
    *@var_nm=*@var_nm.permute($0,$1,$3,$2);    



}






