#!/usr/bin/env python
# coding: utf-8

import numpy as np
from netCDF4 import Dataset  # http://code.google.com/p/netcdf4-python/
import matplotlib.pyplot as plt
import time
import sys

times = (np.arange(41)*10)+0
filebase = "/nobackupp18/jleake/fe01_v4_SMD_stab_nc_files/fe01_v4_SMD_stab_"
print(filebase)
print(times)
for itimes in times:
    stime = time.time()
    filename = filebase+format(itimes,'04d')+".nc"
    print(filename)
    print("got data "+str(time.time()-stime))
    nc_fid = Dataset(filename)

    X = nc_fid.variables['x'][:]
    Y = nc_fid.variables['y'][:]
    Z = nc_fid.variables['z'][:]
    
    Bx = np.transpose(nc_fid.variables['bx'][:,:,:])
    By = np.transpose(nc_fid.variables['by'][:,:,:])
    Bz = np.transpose(nc_fid.variables['bz'][:,:,:])

    Jx = np.transpose(nc_fid.variables['jx'][:,:,:])
    Jy = np.transpose(nc_fid.variables['jy'][:,:,:])
    Jz = np.transpose(nc_fid.variables['jz'][:,:,:])

    nx,ny,nz = np.shape(Jx)

    print("zrange: ", Z[0], Z[-1])
    index = np.min(np.where(Z >= 0.0))
    print("surface at Z= ", Z[index])

    plt.figure()

    plt.contourf(X,Y,Bz[:,:,index], 101)
    plt.colorbar()
    plt.savefig('Bz_time_'+str(itimes).zfill(5)+'.png')

    
