from netCDF4 import Dataset
import numpy as np

def   create_file(name,field_name,time_axis,glam,gphi,dep_axis,data):  


      root_grp = Dataset(name, 'w', format='NETCDF4')
      
   
      
      ndimx=len(glam[:])
      
      ndimy=len(gphi[:])
     
      ndimz=len(dep_axis)
      ntime=len(time_axis)     

      data=np.reshape(data, (ntime,ndimz,ndimy,ndimx))

      print(np.shape(glam))
      print(np.shape(gphi))
      print(np.shape(dep_axis))
      print(np.shape(data))
      
      # dimensions
      root_grp.createDimension('time' ,None)
      root_grp.createDimension('x'    ,ndimx)
      root_grp.createDimension('y'    ,ndimy)
      root_grp.createDimension('z'    ,ndimz)      

      # variables
      time = root_grp.createVariable('time', 'f8', ('time',))
      lon  = root_grp.createVariable('lon', 'f4', ('x'))
      lat  = root_grp.createVariable('lat', 'f4', ('y'))
      depth= root_grp.createVariable('depth', 'f4', ('z',))
      field= root_grp.createVariable(field_name, 'f8', ('time','z','y','x',))
   
      time[:]       = np.copy(time_axis)
      lon[:]        = np.copy(glam[:])
      lat[:]        = np.copy(gphi[:])
      depth[:]      = np.copy(dep_axis)
      field[:,:,:,:]      = np.copy(data)
      root_grp.close()


