Full Examples

The Kameleon Class can be used to access any of Kameleon’s Supported Models.

Kameleon Class Demo

This demo should work on any of Kameleon’s Supported Models.

import sys
from CCMC import _CCMC as ccmc

def main(argv):
	if (len(argv) == 5):
	    filename = argv[0]
	    variable = argv[1]

	    c0 = float(argv[2])
	    c1 = float(argv[3])
	    c2 = float(argv[4])

	    kameleon = ccmc.Kameleon()
	    kameleon.open(filename)
	    kameleon.loadVariable(variable)

	    interpolator = kameleon.createNewInterpolator()
	    var = interpolator.interpolate(variable,c0, c1, c2)

	    print variable, var

	    kameleon.close()
	else:
		print 'Usage: <filename> <variable> x, y, z \n python kameleon_test rho -40 0 0' 

if __name__ == '__main__':
    main(sys.argv[1:])

Tracer Class Demo

Python

#1/usr/bin/python
"""
This program takes as input a kameleon-compatible cdf or hdf5 file, a variable, and a seed position.
It then computes a fieldline and interpolates the variable onto it, plotting the resulting fieldline in 3d
and a graph of the variable as a function of arc length.
"""
import numpy as np
import sys, getopt
import time
from CCMC import _CCMC as ccmc

def main(argv):
    if (len(argv) == 5):
        filename = argv[0]
        variable = argv[1]
        c0 = float(argv[2])
        c1 = float(argv[3])
        c2 = float(argv[4])

        print 'Input file is "', filename
        print 'seed position:', c0, c1, c2
    
        kameleon = ccmc.Kameleon()
    
        kameleon.open(filename)
        kameleon.loadVariable("b")
        kameleon.loadVariable(variable)
        tracer = ccmc.Tracer(kameleon)
        tracer.setMaxIterations(20000)
        tracer.setDn(.2)   
        
        print 'Testing bidirectional trace at seed position:', c0, c1, c2
        start = time.clock()
        fieldline1 = tracer.bidirectionalTrace('b',c0,c1,c2)
        finish = time.clock()    
        elapsed = finish - start
        print 'Bidirectional trace completed. Elapsed time: ', elapsed, 'seconds.'



        interpolator = kameleon.createNewInterpolator()

        if module_exists("matplotlib"):
            import matplotlib.pyplot as plt
            from mpl_toolkits.mplot3d import axes3d
            fig = plt.figure()
            ax = fig.add_subplot(111, projection='3d')

            npts = fieldline1.size()
            x = np.zeros(npts); y = np.zeros(npts); z = np.zeros(npts)
            data = np.zeros(npts); arcLength = np.zeros(npts)

            for i in range(npts):
                p = fieldline1.getPosition(i)
                (x[i], y[i], z[i]) = (p.component1, p.component2, p.component3)
                data[i] = interpolator.interpolate(variable, x[i], y[i], z[i])
                arcLength[i] = fieldline1.getLength(i)

            def plotExtrema(extrema, color = 'r', marker = 'o'):
            #plot min and max values along fieldline
                for i in range(extrema.size()):
                    p  = fieldline1.getPosition(extrema[i])
                    ax.scatter(p.component1, p.component2, p.component3, c = color, marker  = marker)

            fieldline1.minmax()
            plotExtrema(fieldline1.minima)
            plotExtrema(fieldline1.maxima, 'b', '^')

            ax.plot(x, y, z, label='Magnetic field line')
            ax.legend()

            fig = plt.figure()
            plt.plot(arcLength, data, label='parametric data')
            plt.xlabel('arc length [' + kameleon.getVisUnit('x') + ']')
            plt.ylabel(variable + ' [' + kameleon.getVisUnit(variable) + ']')



            plt.show()

        else:
            print 'matplotlib not installed.. no 3d view supported.'

    else: print 'Usage: <filename> <variable> x, y, z \n python kameleon_test rho -40 0 0'
        


        
def module_exists(module_name):
    try:
        __import__(module_name)
    except ImportError:
        return False
    else:
        return True
    
if __name__ =="__main__":
    main(sys.argv[1:])

Fortran

      program tracer_f

c     Five functions to create a Kameleon Object, open the cdf file, load the variable, and close the file

      external f_kameleon_create, f_kameleon_open, f_kameleon_delete
      external f_kameleon_load_vector_variable, f_kameleon_close
      external f_kameleon_create_c_string

c     Three Tracer functions
      external f_tracer_create, f_tracer_bidirectionaltrace, f_tracer_delete

c     Variables to be used for interpolation and data extraction

      character*300 cdf_file_path
      character*301 cdf_file_path_c
      character*50 variable
      character*50 variable_c
c      real*8 x,y,z
c      real*8 interpolated_value
      integer status,kid,tid,s_length,actual_steps
      real x_array(1000)
      real y_array(1000)
      real z_array(1000)
      character*100 c0,c1,c2
      real rc0, rc1, rc2
      integer argc

c      character*50 var_to_read

c     --- set your actual path name here ---
      argc = iargc()
      if (argc.ne.5) then
        print *,"tracer_compat_f <filename> variable c0 c1 c2"
        print *,"    Adapt3D, OpenGGCM, BATSRUS, LFM: x y z"
        print *,"    ENLIL, MAS: r theta(latitude) phi(longitude)"
        print *,"    SWMF Iono: 0 theta(latitude) phi(MLT)"
       call exit(1)
      endif

      call getarg(1, cdf_file_path)
      call getarg(2, variable)
      call getarg(3, c0)
      call getarg(4, c1)
      call getarg(5, c2)

      read(c0,*) rc0
      read(c1,*) rc1
      read(c2,*) rc2

      write(*,*) 'Processing file: ', cdf_file_path

c     Open the cdf file


      kid=0
      tid=0
      status=0
      actual_steps=0
      call f_kameleon_create_c_string(cdf_file_path, cdf_file_path_c)
      call f_kameleon_create_c_string(variable, variable_c)
      call f_kameleon_create(kid)
      call f_kameleon_open(kid,cdf_file_path_c,status)
      call f_kameleon_load_vector_variable(kid,variable_c)
      call f_tracer_create(tid,kid)

      print *, 'c0: ', rc0, ' c1: ', rc1, ' c2: ', rc2
      print *, 'passing variables:', 'tid: ', tid, 'variable ', variable

c     make sure the step size used is appropriate and the target arrays have 2 times
c     the step_max so the arrays have enough for two directions.
      call f_Tracer_bidirectionaltrace(tid,variable,rc0,rc1,rc2,10000, 
     &       .2, actual_steps,x_array,y_array,z_array)
      write(*,*) 'finished tracing'
      write(*,*) 'actual_steps: ',actual_steps
      call f_tracer_delete(tid);
      call f_kameleon_close(kid) 
      call f_kameleon_delete(kid, status)
      end