/****************************************************************************
**  SCALASCA    http://www.scalasca.org/                                   **
*****************************************************************************
**  Copyright (c) 1998-2012                                                **
**  Forschungszentrum Juelich GmbH, Juelich Supercomputing Centre          **
**                                                                         **
**  Copyright (c) 2003-2008                                                **
**  University of Tennessee, Innovative Computing Laboratory               **
**                                                                         **
**  See the file COPYRIGHT in the package base directory for details       **
****************************************************************************/

void FSUB(F_elg_cart_create)(elg_ui4* dim0, 
			     elg_ui4* dim1, 
			     elg_ui4* dim2,
			     elg_ui4* period0, 
			     elg_ui4* period1, 
			     elg_ui4* period2 )
{
  elg_ui4  *dimv, *temp_dimv; 
  elg_ui1  *periodv, *temp_periodv; 
  int      i;

  /* allocate space for temporary arrays to store 
     the required parameters */
  temp_dimv = calloc(3, sizeof(elg_ui4));
  if ( temp_dimv == NULL ) 
    elg_error();
  
  temp_periodv = calloc(3, sizeof(elg_ui1));
  if ( temp_periodv == NULL ) 
    elg_error();
 
  /* check the parameters and calculate ndims */
  if ( *dim0 == 0 ) 
    elg_error_msg("Incorrect dimension specification");
  else if ( *dim0 > 0 && *dim1 == 0 && *dim2 == 0 )
    {
      ndims = 1;
      temp_dimv[0] = *dim0; temp_periodv[0] = *period0;      
    }
  else if ( *dim0 > 0 && *dim1 > 0 && *dim2 == 0 ) 
    {
      ndims = 2;
      temp_dimv[0] = *dim0; temp_periodv[0] = *period0;
      temp_dimv[1] = *dim1; temp_periodv[1] = *period1;
    }
  else
    {
      ndims = 3;
      temp_dimv[0] = *dim0; temp_periodv[0] = *period0;
      temp_dimv[1] = *dim1; temp_periodv[1] = *period1;
      temp_dimv[2] = *dim2; temp_periodv[2] = *period2;
    }
  
  /* allocate space for dimv and periodv depending on ndims */
  dimv = calloc(ndims, sizeof(elg_ui4));
  if ( dimv == NULL ) 
    elg_error();
  
  periodv = calloc(ndims, sizeof(elg_ui1));
  if ( periodv == NULL ) 
    elg_error();

  /* fill the dimv and periodv arrays */
  for ( i = 0 ; i < ndims ; i++ ) 
    {
      dimv[i] = temp_dimv[i];
      periodv[i] = temp_periodv[i];
    }
  
  topid = esd_def_cart(cid, ndims, dimv, periodv);

  /* free up the space used by arrays */
  free(dimv); free(temp_dimv);
  free(periodv); free(temp_periodv);
}

void FSUB(F_elg_cart_coords)(elg_ui4* coord0, 
			     elg_ui4* coord1, 
			     elg_ui4* coord2)
{
  elg_ui4 *coordv;
  
  /* allocate space for coordv */
  coordv = calloc(ndims, sizeof(elg_ui4));
  if ( coordv == NULL ) 
    elg_error();

  /* fill in coordv  */
  if ( ndims == 1 ) 
    coordv[0] = *coord0;
  else if ( ndims == 2 )
    {
      coordv[0] = *coord0; coordv[1] = *coord1;
    }
  else
    {
       coordv[0] = *coord0; coordv[1] = *coord1; coordv[2] = *coord2;
    }

  esd_def_coords(topid, ndims, coordv);

  /* free up the space used by array */
  free(coordv);
}
