#! /usr/bin/perl

use warnings;
use strict;

use lib "scripts/netcdf";
use abinit_readtype;

my ($dim,%dims);

%dims  = extract_variables("Src_defs/defs_netcdf.F90","ncdims_type");

#print "Writing restore routine...";

open(OUT,">Src_3ionetcdf/get_dims_dtset_ncdf.F90")
or die "Could not open Src_3ionetcdf/get_dims_dtset_ncdf.F90 for writing";

print OUT <<EOF
!{\\src2tex{textfont=tt}}
!!****f* ABINIT/get_dims_dtset_ncdf
!! NAME
!! get_dims_dtset_ncdf
!!
!! FUNCTION
!!  Reads the dimensions of the arrays present in a dataset from
!!  a specified NetCDF file.
!!
!! COPYRIGHT
!!  Copyright (C) 2005 ABINIT group (YP)
!!  This file is distributed under the terms of the
!!  GNU General Public License, see ~ABINIT/Infos/copyright
!!  or http://www.gnu.org/copyleft/gpl.txt .
!!  For the initials of contributors, see ~ABINIT/Infos/contributors .
!!
!! INPUTS
!!  ncid = identifier of an open NetCDF file
!!
!! OUTPUT
!!  dims <type(ncdims_type)> = dimensions of dataset variables
!!
!! NOTES
!!  Please do not edit this file. Since it is automatically generated (see
!!  the makencdf script), your changes would be systematically overwritten.
!!  Instead you should directly modify the generating script itself
!!  (~ABINIT/scripts/netcdf/get_dims_dtset_ncdf.pl) to suit your needs.
!!
!! PARENTS
!!
!! CHILDREN
!!
!! SOURCE

subroutine get_dims_dtset_ncdf(ncid,dims)

 use defs_basis
#if defined NETCDF
 use netcdf
 use defs_netcdf
#endif

 implicit none

!Arguments ------------------------------------
#if defined NETCDF
 type(ncdims_type),intent(out) :: dims
#else
 integer          ,intent(in)  :: dims
#endif
!scalars
 integer,intent(in) :: ncid

!Local variables-------------------------------
!scalars
 integer :: ncdim,ncerr,ncvar
 character(len=500) :: message
!arrays
 integer,allocatable :: ncdims(:)

! ***************************************************************************
#if defined NETCDF
EOF
;

foreach $dim (sort keys %dims)
{
	$dim =~ s/\(.*//;

	print OUT " ! Get $dim\n";
	print OUT " ncerr = nf90_inq_dimid(ncid,name='$dim',dimid=ncdim)\n";
	print OUT " call handle_ncerr_new('INQ','DIM','$dim','get_dims_dtset_ncdf',ncerr)\n";
	print OUT " ncerr = nf90_inquire_dimension(ncid,dimid=ncdim,len=dims\%$dim)\n";
	print OUT " call handle_ncerr_new('GET','DIM','$dim','get_dims_dtset_ncdf',ncerr)\n";
}

print OUT <<EOF
#else
 write(message,'(4a)') ch10,' get_dims_dtset_ncdf - ERROR',ch10,&
&  'This version has not been compiled with NetCDF support'
 call wrtout(std_out,message,'COLL')
 call leave_new('COLL')
#endif

end subroutine get_dims_dtset_ncdf
!!***
EOF
;

close(OUT);

#print "done.\n\n";

