Module: CodeRunner::Gs2::GSLVectors

Included in:
CodeRunner::Gs2
Defined in:
lib/gs2crmod/gsl_data.rb

Instance Method Summary collapse

Instance Method Details

#apar2_over_time_gsl_vector(options) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/gs2crmod/gsl_data.rb', line 228

def apar2_over_time_gsl_vector(options)

	Dir.chdir(@directory) do			#Necessary options: ky
		#log 'about to open netcdf file'
		#options.setup_time_window
		phis = netcdf_file.var('apar2').get('start'=>[options[:begin_element]], 'end'=>[options[:end_element]] ).to_a
		log 'about to allocate gsl vector'
		vec = GSL::Vector.alloc(phis)
		log 'finished'
		return fix_norm(vec, 1, options) 
	end 
end

#dt_gsl_vector(options) ⇒ Object

The size of each time step, indexed by time, normalised to a/v_th1.



362
363
364
365
366
367
# File 'lib/gs2crmod/gsl_data.rb', line 362

def dt_gsl_vector(options)
	t = gsl_vector('t', options)
	size = t.size
	# NB t already has norm fixed
  return t.subvector(1, size - 1) - t.subvector(0, size-1)
end

#es_heat_by_kx_over_time_gsl_vector(options) ⇒ Object



415
416
417
418
# File 'lib/gs2crmod/gsl_data.rb', line 415

def es_heat_by_kx_over_time_gsl_vector(options)
	options[:direction] = :kx
	es_heat_by_kxy_over_time_gsl_vector(options)
end

#es_heat_by_ky_over_time_gsl_vector(options) ⇒ Object



419
420
421
422
# File 'lib/gs2crmod/gsl_data.rb', line 419

def es_heat_by_ky_over_time_gsl_vector(options)
	options[:direction] = :ky
	es_heat_by_kxy_over_time_gsl_vector(options)
end

#es_heat_flux_over_time_gsl_vector(options) ⇒ Object



850
851
852
853
854
855
856
# File 'lib/gs2crmod/gsl_data.rb', line 850

def es_heat_flux_over_time_gsl_vector(options)
	Dir.chdir(@directory) do

		options.setup_time_window
		return GSL::Vector.alloc(netcdf_file.var('es_heat_flux').get('start' => [options[:species_index].to_i - 1, options[:begin_element]], 'end' => [options[:species_index].to_i - 1, options[:end_element]]).to_a.flatten)
	end
end

#es_heat_over_kx_gsl_vector(options) ⇒ Object



452
453
454
455
# File 'lib/gs2crmod/gsl_data.rb', line 452

def es_heat_over_kx_gsl_vector(options)
	options[:direction] = :kx
	es_heat_over_kxy_gsl_vector(options)
end

#es_heat_over_kxy_gsl_vector(options) ⇒ Object

This function will output the heat flux as a function of kx or ky. Default behaviour will be to average the heat flux over the time domain.



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/gs2crmod/gsl_data.rb', line 463

def es_heat_over_kxy_gsl_vector(options)
	Dir.chdir(@directory) do
		kxy = options[:direction]
		raise "Please provide species_index " unless options[:species_index]
		if kxy==:ky
			es_heat = (netcdf_file.var('es_heat_by_k').get({'start' => [0,0,options[:species_index]-1, 0], 'end' => [-1,-1,options[:species_index]-1, -1]})) #index = [kx,ky,spec,t]
       #Need to average over time and sum over kx
       shape = es_heat.shape
       es_heat_av = []; temp = [];
       for iy in 0...shape[1]
         for ix in 0...shape[0]
           temp[ix] = es_heat[ix,iy,0,0..-1].sum / shape[3]
         end
         es_heat_av[iy] = temp.sum
       end
			return es_heat_av.to_gslv
		else
			es_heat = (netcdf_file.var('es_heat_by_k').get({'start' => [0,0,options[:species_index]-1, 0], 'end' => [-1,-1,options[:species_index]-1, -1]})) #index = [kx,ky,spec,t]
       shape = es_heat.shape
       es_heat_av = []; temp = [];
       for ix in 0...shape[0]
         for iy in 0...shape[1]
           temp[iy] = es_heat[ix,iy,0,0..-1].sum / shape[3]
         end
         es_heat_av[ix] = temp.sum
       end
			return es_heat_av.to_gslv
		end
	end
end

#es_heat_over_ky_gsl_vector(options) ⇒ Object



456
457
458
459
# File 'lib/gs2crmod/gsl_data.rb', line 456

def es_heat_over_ky_gsl_vector(options)
	options[:direction] = :ky
	es_heat_over_kxy_gsl_vector(options)
end

#es_heat_par_over_time_gsl_vector(options) ⇒ Object Also known as: es_heat_par_gsl_vector



857
858
859
860
861
862
863
# File 'lib/gs2crmod/gsl_data.rb', line 857

def es_heat_par_over_time_gsl_vector(options)
	Dir.chdir(@directory) do

		options.setup_time_window
		return GSL::Vector.alloc(netcdf_file.var('es_heat_par').get('start' => [options[:species_index].to_i - 1, options[:begin_element]], 'end' => [options[:species_index].to_i - 1, options[:end_element]]).to_a.flatten)
	end
end

#es_heat_perp_over_time_gsl_vector(options) ⇒ Object Also known as: es_heat_perp_gsl_vector



865
866
867
868
869
870
871
# File 'lib/gs2crmod/gsl_data.rb', line 865

def es_heat_perp_over_time_gsl_vector(options)
	Dir.chdir(@directory) do

		options.setup_time_window
		return GSL::Vector.alloc(netcdf_file.var('es_heat_perp').get('start' => [options[:species_index].to_i - 1, options[:begin_element]], 'end' => [options[:species_index].to_i - 1, options[:end_element]]).to_a.flatten)
	end
end

#es_mom_flux_over_time_gsl_vector(options) ⇒ Object



880
881
882
883
884
885
886
# File 'lib/gs2crmod/gsl_data.rb', line 880

def es_mom_flux_over_time_gsl_vector(options)
	Dir.chdir(@directory) do

		options.setup_time_window
		return GSL::Vector.alloc(netcdf_file.var('es_mom_flux').get('start' => [options[:species_index].to_i - 1, options[:begin_element]], 'end' => [options[:species_index].to_i - 1, options[:end_element]]).to_a.flatten)
	end
end

#frequency_by_kx_over_time_gsl_vector(options) ⇒ Object

The real frequency of the fluctuations, read from the .out file, indexed by time and normalised to vth_1/a. :ky_index or :kx_index must be specified in options.



311
312
313
314
# File 'lib/gs2crmod/gsl_data.rb', line 311

def frequency_by_kx_over_time_gsl_vector(options)
        options[:direction] = :kx
        frequency_by_kxy_over_time_gsl_vector(options)
end

#frequency_by_kxy_over_time_gsl_vector(options) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/gs2crmod/gsl_data.rb', line 321

def frequency_by_kxy_over_time_gsl_vector(options)
    kxy = options[:direction]
    kxy_index = kxy + :_index
    kxys = get_list_of(kxy)
    desired_kxy = kxys[options[kxy_index]]
    raise "No k found at the desired index" if desired_kxy.nil?

    omega_reals = []
    File.open(@run_name+".out",'r') do |fileHandle|
      fileHandle.each_line do |fileLine|
        if fileLine.include?('aky=')  # Only examine the lines of the .out file that contain frequency information.

          index = fileLine.index('akx=')
          raise "akx wasn't found where it was expected in the .out file." if index.nil?
          akx = fileLine[(index+4)..-1].to_f

          index = fileLine.index('aky=')
          raise "aky wasn't found where it was expected in the .out file." if index.nil?
          aky = fileLine[(index+4)..-1].to_f

          index = fileLine.index('om=')
          raise "om wasn't found where it was expected in the .out file." if index.nil?
          omr = fileLine[(index+3)..-1].to_f
          if kxy == :kx
            # You need to be careful when testing equality of the desired k with the k in the .out file
            # since the .out file is only written to ~ 5 significant digits:
            omega_reals << omr if ((desired_kxy - akx).abs/(desired_kxy.abs + 1e-7) < 1e-4)
          else
            omega_reals << omr if ((desired_kxy - aky).abs/(desired_kxy.abs + 1e-7) < 1e-4)
          end
        end
      end
     end
     raise "No real frequencies found in the .out file for the desired k" if (omega_reals.size==0)
     GSL::Vector.alloc(omega_reals)
end

#frequency_by_ky_over_time_gsl_vector(options) ⇒ Object



316
317
318
319
# File 'lib/gs2crmod/gsl_data.rb', line 316

def frequency_by_ky_over_time_gsl_vector(options)
        options[:direction] = :ky
        frequency_by_kxy_over_time_gsl_vector(options)
end

#frequency_over_ky_gsl_vector(options) ⇒ Object

Frequency, indexed over ky, taken direct from the gs2 output file



410
411
412
413
# File 'lib/gs2crmod/gsl_data.rb', line 410

def frequency_over_ky_gsl_vector(options)
	  options.convert_to_index(self, :kx)
		return GSL::Vector.alloc(gsl_vector('ky').to_a.map{|ky| frequency_at_ky_at_kx[ky].values[options[:kx_index]-1]})
end

#growth_rate_by_kx_over_time_gsl_vector(options) ⇒ Object

:kx or :kx_index must be specified in options



282
283
284
285
# File 'lib/gs2crmod/gsl_data.rb', line 282

def growth_rate_by_kx_over_time_gsl_vector(options)
	options[:direction] = :kx
	growth_rate_by_kxy_over_time_gsl_vector(options)
end

#growth_rate_by_kxy_over_time_gsl_vector(options) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/gs2crmod/gsl_data.rb', line 294

def growth_rate_by_kxy_over_time_gsl_vector(options)
	# i.e. time_dependent_gr_by_ky_vs_time or phi2_by_kx_vs_time
	
	kxy = options[:direction]

	phi = gsl_vector("phi2_by_#{kxy}_over_time", options).log / 2.0

	size = phi.size
	dphi = phi.subvector(1, size - 1) - phi.subvector(0, size-1)
	# NB dt already has norm fixed, dphi is dimensionless
	return fix_norm(dphi/gsl_vector('dt'), 0, options)
end

#growth_rate_by_ky_over_time_gsl_vector(options) ⇒ Object

:ky or :ky_index must be specified in options



290
291
292
293
# File 'lib/gs2crmod/gsl_data.rb', line 290

def growth_rate_by_ky_over_time_gsl_vector(options)
	options[:direction] = :ky
	growth_rate_by_kxy_over_time_gsl_vector(options)
end

#growth_rate_over_kx_gsl_vector(options) ⇒ Object

The growth rate, calculated from the potential, indexed by kx. Only makes sense in linear calculations.



370
371
372
373
# File 'lib/gs2crmod/gsl_data.rb', line 370

def growth_rate_over_kx_gsl_vector(options)
	options[:direction] = :kx
	growth_rate_over_kxy_gsl_vector(options)
end

#growth_rate_over_kx_slice_gsl_vector(options) ⇒ Object

The growth rate, calculated from the potential, indexed by kx. Only makes sense in linear calculations.



391
392
393
394
395
396
397
398
# File 'lib/gs2crmod/gsl_data.rb', line 391

def growth_rate_over_kx_slice_gsl_vector(options)
	Dir.chdir(@directory) do
		slice_of_growth_rates = send(:growth_rate_at_ky_at_kx)[options[:ky]].values
		raise "Something went wrong: slice of growth rates seems empty" if slice_of_growth_rates.nil?
		return GSL::Vector.alloc(slice_of_growth_rates)
		#return GSL::Vector.alloc(send(:growth_rate_at_ky_at_kx[ky]).values)
	end
end

#growth_rate_over_ky_gsl_vector(options) ⇒ Object

The growth rate, calculated from the potential, indexed by ky. Only makes sense in linear calculations.



375
376
377
378
# File 'lib/gs2crmod/gsl_data.rb', line 375

def growth_rate_over_ky_gsl_vector(options)
	options[:direction] = :ky
	growth_rate_over_kxy_gsl_vector(options)
end

#growth_rate_over_ky_slice_gsl_vector(options) ⇒ Object

The growth rate, calculated from the potential, indexed by ky. Only makes sense in linear calculations.



401
402
403
404
405
406
407
# File 'lib/gs2crmod/gsl_data.rb', line 401

def growth_rate_over_ky_slice_gsl_vector(options)
	Dir.chdir(@directory) do
		slice_of_growth_rates = send(:growth_rate_at_ky_at_kx).values.map{|h| h[options[:kx]]}
		raise "Something went wrong: slice of growth rates seems empty" if slice_of_growth_rates.nil?
		return GSL::Vector.alloc(slice_of_growth_rates)
	end
end

#hflux_tot_over_time_gsl_vector(options) ⇒ Object Also known as: hflux_tot_gsl_vector



839
840
841
842
843
844
845
846
847
848
# File 'lib/gs2crmod/gsl_data.rb', line 839

def hflux_tot_over_time_gsl_vector(options)
	Dir.chdir(@directory) do
		options.setup_time_window
		narr = netcdf_file.var('hflux_tot').get('start' => [options[:begin_element]], 'end' => [options[:end_element]])
			#eputs 'Got narr'
		#ep 'hflux_tot', hflux
		#eputs "fixing norm"
		return fix_heat_flux_norm(GSL::Vector.alloc(narr.to_a), options)
	end
end

#kpar_gsl_vector(options) ⇒ Object



710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/gs2crmod/gsl_data.rb', line 710

def kpar_gsl_vector(options)

	Dir.chdir(@directory) do	
		if agk? or (@s_hat_input||@shat).abs	< 1.0e-5
			dk = 1
			phi = list(:theta).values
		else
			kxe = gsl_vector('linked_kx_elements', options)
			dk = 1.0/kxe.size
		phi = gsl_vector_complex('phi_along_field_line', options)
		end
		case phi.size%2
		when 0
			kpar = GSL::Vector.indgen(phi.size-1, -((phi.size-3)/2))*dk
		when 1
			kpar = GSL::Vector.indgen(phi.size-1, -((phi.size-2)/2))*dk
		end
     #ep 'kpar', kpar, 'phi.size', phi.size

		#ep 'kpar.class', kpar.class
		return kpar

	end
end

#linked_kx_elements_gsl_vector(options) ⇒ Object



618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/gs2crmod/gsl_data.rb', line 618

def linked_kx_elements_gsl_vector(options)
	Dir.chdir(@directory) do		
		return GSL::Vector.alloc([0]) if @grid_option == "single" or agk?
		if agk? or (@s_hat_input or @shat).abs < 1.0e-5
			#p 'op1', options
			options.convert_to_index(self, :ky, :kx)
			#p 'op2', options
			#eputs "No Magnetic Shear"

# 				begin
# 					options.convert_to_index(:kx)
# 				rescue
# 					raise "Must specify kx or kx_index if no magnetics shear"
# 				end
# # 				theta0 = (options[:theta0] || 0)
# # 				theta0 += jump(options) if @g_exb

			#theta0 = (options[:kx_index])
			#if @g_exb and @g_exb.abs > 0.0
         #theta0 += jump(options)
         #theta0 = theta0%((list(:kx).size-1)/2) if list(:kx).size > 1
       #end

			return GSL::Vector.alloc([options[:kx_index] - 1])
		end
		
		options.convert_to_index(self, :ky, :kx)
		nkx = netcdf_file.var('kx').dims[0].length
# 			p nkx
		stride = @jtwist * (options[:ky_index] -1 )
		#stride = 3
		nlinks = [(nkx / stride).floor, 1].max 
		theta0 = options[:kx_index] % @jtwist  #(options[:theta0] || 0)
		log 'stride', stride, 'nlinks', nlinks, 'theta0', theta0
		#if @g_exb and @jtwist > 1 #and options[:t_index]
# 				kx_shift = list(:ky)[options[:ky_index]]  * @g_exb
# 				p list(:t)[options[:t_index]], options[:t_index], kx_shift
			
# 				kx_shift *=  list(:t)[(options[:t_index] or list(:t).keys.max)]
# 				jump = (kx_shift / list(:kx)[2]).round
			#theta0  += (@jtwist - jump(options) % @jtwist) % @jtwist
			
# 				else
# 					jump = 0	
		#end
		ep 'stride', stride, 'nlinks', nlinks, 'theta0', theta0
		ep GSL::Vector.indgen(nlinks / 2,  nkx + theta0 - nlinks / 2 * stride, stride).connect(GSL::Vector.indgen(nlinks / 2, theta0, stride)).reverse if nlinks > 1
		#return [7,5,3,1,34].to_gslv
		return GSL::Vector.alloc([theta0 % jtwist]) if nlinks ==1
		return GSL::Vector.indgen(nlinks / 2,  nkx + theta0 - nlinks / 2 * stride, stride).connect(GSL::Vector.indgen(nlinks / 2, theta0, stride)).reverse

	end
end

#lpc_energy_gsl_vector(options) ⇒ Object

Velocity space diagnostics: fraction of dist func in higher energy harmonics



896
897
898
899
900
# File 'lib/gs2crmod/gsl_data.rb', line 896

def lpc_energy_gsl_vector(options)
	raise "Velocity space lpc diagnostics not found" unless FileTest.exist? "#@directory/#@run_name.lpc"
	lpc = GSL::Vector.filescan("#@directory/#@run_name.lpc")
	return lpc[2]
end

#lpc_pitch_angle_gsl_vector(options) ⇒ Object

Velocity space diagnostics: fraction of dist func in higher pitch angle harmonics



889
890
891
892
893
# File 'lib/gs2crmod/gsl_data.rb', line 889

def lpc_pitch_angle_gsl_vector(options)
	raise "Velocity space lpc diagnostics not found" unless FileTest.exist? "#@directory/#@run_name.lpc"
	lpc = GSL::Vector.filescan("#@directory/#@run_name.lpc")
	return lpc[1]
end

#par_mom_flux_over_time_gsl_vector(options) ⇒ Object



915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# File 'lib/gs2crmod/gsl_data.rb', line 915

def par_mom_flux_over_time_gsl_vector(options)
Dir.chdir(@directory) do

	options.setup_time_window
	# This is a hack... one day some one will put it in the NetCDF file (haha).
	momlines = `grep parmom #@run_name.out`
	mom = []
	momlines.scan(Regexp.new("#{LongRegexen::FLOAT.to_s}$")) do 
		mom.push $~[:float].to_f
	end
	options[:end_element] = (mom.size + options[:end_element]) if options[:end_element] < 0
# 			p options
	return GSL::Vector.alloc(mom).subvector(options[:begin_element], options[:end_element] - options[:begin_element] + 1)
end
end

#perp_mom_flux_over_time_gsl_vector(options) ⇒ Object



931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'lib/gs2crmod/gsl_data.rb', line 931

def perp_mom_flux_over_time_gsl_vector(options)

	Dir.chdir(@directory) do
		options.setup_time_window
		# This is a hack... one day some one will put it in the NetCDF file (haha).
		momlines = `grep perpmom #@run_name.out`
		mom = []
		momlines.scan(Regexp.new("#{LongRegexen::FLOAT.to_s}$")) do 
			mom.push $~[:float].to_f
		end
		options[:end_element] = (mom.size + options[:end_element]) if options[:end_element] < 0
	# 			p options
		return GSL::Vector.alloc(mom).subvector(options[:begin_element], options[:end_element] - options[:begin_element] + 1)
	end
end

#phi0_by_kx_by_ky_over_time_gsl_vector(options) ⇒ Object



609
610
611
612
613
614
615
616
# File 'lib/gs2crmod/gsl_data.rb', line 609

def phi0_by_kx_by_ky_over_time_gsl_vector(options)
	Dir.chdir(@directory) do		
		options.convert_to_index(self, :kx, :ky)
		phi0_array = netcdf_file.var('phi0').get.to_a.map{|arr| arr[options[:kx_index] - 1][options[:ky_index] - 1][options[:ri]]}
		return GSL::Vector.alloc(phi0_array)

	end
end

#phi2_by_kx_over_time_gsl_vector(options) ⇒ Object



493
494
495
496
# File 'lib/gs2crmod/gsl_data.rb', line 493

def phi2_by_kx_over_time_gsl_vector(options)
	options[:direction] = :kx
 	phi2_by_kxy_over_time_gsl_vector(options)
end

#phi2_by_ky_over_time_gsl_vector(options) ⇒ Object



497
498
499
500
# File 'lib/gs2crmod/gsl_data.rb', line 497

def phi2_by_ky_over_time_gsl_vector(options)
	options[:direction] = :ky
 	phi2_by_kxy_over_time_gsl_vector(options)
end

#phi2_by_mode_over_time_gsl_vector(options) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/gs2crmod/gsl_data.rb', line 537

def phi2_by_mode_over_time_gsl_vector(options)
	Dir.chdir(@directory) do			#Necessary options: :ky and :kx
		#Optional options: :t_index_window
		# 		eputs "got here"
		#options[:begin_element], options[:end_element] = (options[:t_index_window] ? options[:t_index_window].map{|ind| ind -1} : [0, -1])
		options.setup_time_window			
		phi_t_array=nil
		if @grid_option == "single"
			phi_t_array = netcdf_file.var('phi2').get('start' => [options[:begin_element]], 'end' => [options[:end_element]]).to_a.flatten
		else
# 				value = options[:ky]
# 				eputs value
# 				get_list_of(:ky)
# 				index = @ky_list.find{|index,val| (val-value).abs < Float::EPSILON}[0]
			options.convert_to_index(self, :kx, :ky)
# 				p options
			phi_t_array = netcdf_file.var("phi2_by_mode").get('start' => [options[:kx_index] - 1, options[:ky_index] - 1, options[:begin_element]], 'end' => [options[:kx_index] - 1, options[:ky_index] - 1, options[:end_element]]).to_a.flatten
# 				eputs 'phi_t_array.size', phi_t_array.size
		end
		return GSL::Vector.alloc(phi_t_array)

	end
end

#phi2tot_over_time_gsl_vector(options) ⇒ Object

The square of the potential summed over all wave numbers, indexed by time, normalised to (e/T)(rho_1/a).



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/gs2crmod/gsl_data.rb', line 216

def phi2tot_over_time_gsl_vector(options)

	Dir.chdir(@directory) do			#Necessary options: ky
		#log 'about to open netcdf file'
		#options.setup_time_window
		phis = netcdf_file.var('phi2').get('start'=>[options[:begin_element]], 'end'=>[options[:end_element]] ).to_a
		log 'about to allocate gsl vector'
		vec = GSL::Vector.alloc(phis)
		log 'finished'
		return fix_norm(vec, 1, options) 
	end 
end

#phi_along_field_line_gsl_vector(options) ⇒ Object



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/gs2crmod/gsl_data.rb', line 735

def phi_along_field_line_gsl_vector(options)
	Dir.chdir(@directory) do	
		complex_phi_vector= gsl_vector_complex('phi_along_field_line', options)
		case options[:imrc]
		when :im
			phi_vector = complex_phi_vector.imag
		when :mag
			mag = true
			phi_vector = complex_phi_vector.abs2
		when :corr
			thetas = gsl_vector('theta_along_field_line', options)
			min = thetas.abs.to_a.index(thetas.abs.min)
			at_0 = complex_phi_vector[min]
# 				ep at_0.class
			phi_vector = (complex_phi_vector * (at_0 / at_0.mag).conj).real
# 				gsl_complex('correcting_phase', options)).real
		when :real
			phi_vector = complex_phi_vector.real
		else
			raise CRError.new("options[:imrc] was: #{options[:irmc]}")
		end
		phi_vector *= -1.0 if options[:flip]
		(phi_vector /= phi_vector.abs.max; phi_vector *= (options[:height] || 1.0)) if options[:norm]
		phi_vector = phi_vector.reverse if options[:rev]
		return phi_vector

	end
end

#phi_for_eab_movie_gsl_vector(options) ⇒ Object



828
829
830
831
832
833
834
835
836
837
# File 'lib/gs2crmod/gsl_data.rb', line 828

def phi_for_eab_movie_gsl_vector(options)
	Dir.chdir(@directory) do			#options required are x_index, y_index and tm_index (Time)
		mvf_name = @run_name + '.movie.nc'
		raise CRError.new("cannot find file #{mvf_name}") unless FileTest.exist? mvf_name
		ncf = NumRu::NetCDF.open(mvf_name)
# 			p ncf.var('phi_by_xmode').get.to_a[0][0][0]
		return GSL::Vector.alloc(ncf.var('phi_by_xmode').get.to_a[options[:tm_index] - 1].map{|xy_arr| xy_arr[options[:x_index] - 1][options[:y_index] - 1]})

	end
end

#scan_parameter_value_gsl_vector(options) ⇒ Object



947
948
949
# File 'lib/gs2crmod/gsl_data.rb', line 947

def scan_parameter_value_gsl_vector(options)
	return GSL::Vector.alloc(netcdf_file.var('scan_parameter_value').get.to_a)
end

#spectrum_over_kpar_gsl_vector(options) ⇒ Object



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# File 'lib/gs2crmod/gsl_data.rb', line 672

def spectrum_over_kpar_gsl_vector(options)
	Dir.chdir(@directory) do
# , /kpar_spectrum/
		#ep 'zero?', (@s_hat_input||@shat)==0.0
		unless agk? or (@s_hat_input||@shat||0.0).abs<1.0e-5
			phi = gsl_vector_complex('phi_along_field_line', options)
       phi = phi.subvector(0,phi.size-1)
			#i = 0
			#phi = phi.collect{|re,im| 
				#i+=1; GSL::Complex.alloc(Math.sin(0.1*i), Math.cos(0.1*i))+ 
				#GSL::Complex.alloc(Math.sin(0.4*i), Math.cos(0.4*i)) 

			#}
			##GraphKit.quick_create([phi.square]).gnuplot
			phi_k = phi.forward
			phi_kr = phi_k.square
			case phi_kr.size%2
			when 0
				spec = phi_kr.subvector((phi_kr.size+2)/2, (phi_kr.size-2)/2).connect(phi_kr.subvector(0, (phi_kr.size+2)/2))
			when 1
				spec = phi_kr.subvector((phi_kr.size + 1)/2, (phi_kr.size-1)/2).connect(phi_kr.subvector(0, (phi_kr.size+1)/2))
			end
			##spec = phi_kr
			#ep 'spec.class', spec.class
			return spec
		else

			gm = gsl_matrix('spectrum_over_ky_over_kpar', options)
			vec = GSL::Vector.alloc(gm.shape[1])
			vec.set_all(0.0)
			for ky_element in 0...gm.shape[0]
				vec+= gm.row(ky_element)
			end
			vec = vec/gm.shape[0]
			return vec
		end
	end
end

#spectrum_over_kx_gsl_vector(options) ⇒ Object



950
951
952
953
# File 'lib/gs2crmod/gsl_data.rb', line 950

def spectrum_over_kx_gsl_vector(options)
	options[:direction] = :kx
	spectrum_over_kxy_gsl_vector(options)
end

#spectrum_over_kxy_gsl_vector(options) ⇒ Object



958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/gs2crmod/gsl_data.rb', line 958

def spectrum_over_kxy_gsl_vector(options)
	Dir.chdir(@directory) do
		# i.e. spectrum_over_ky or spectrum_over_kx
		kxy = options[:direction]
	# 			eputs options[:t_index]
		raise "Spectrum makes no sense for single modes" if @grid_option == "single"

		options.convert_to_index(:t) if options[:t] or options[:t_element]
	# 			eputs options[:t_index]

		options[:t_index] ||= list(:t).keys.max
	# 			eputs options[:t_index]
		phi_array = netcdf_file.var("phi2_by_#{kxy}").get('start' => [0, options[:t_index] - 1], 'end' => [-1, options[:t_index] - 1]).to_a.flatten
		v = GSL::Vector.alloc(phi_array)
		v = v.from_box_order if kxy == :kx
		v = v.mul(gsl_vector(kxy).square) unless options[:phi2_only]
		return v
	end
end

#spectrum_over_ky_gsl_vector(options) ⇒ Object



954
955
956
957
# File 'lib/gs2crmod/gsl_data.rb', line 954

def spectrum_over_ky_gsl_vector(options)
	options[:direction] = :ky
	spectrum_over_kxy_gsl_vector(options)
end

#theta_along_field_line_gsl_vector(options) ⇒ Object



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/gs2crmod/gsl_data.rb', line 764

def theta_along_field_line_gsl_vector(options)
	Dir.chdir(@directory) do
		case @grid_option
		when "single", "range"
			theta_vector = gsl_vector(:theta)
		when "box"
			#eputs "Start theta_along_field_line"

			kx_elements = gsl_vector('linked_kx_elements', options).to_a
			#if @grid_option == "range"
				#kx_elements = kx_elements.to_gslv.from_box_order.to_a
			#end
			ep 'kx_elements', kx_elements.to_a
# 				ep list(:kx).keys.max
# 				ep kx_elements[0], list(:kx)[(kx_elements[0] + 1).to_i]
# 				ep kx_elements[-1], list(:kx)[(kx_elements[-1] + 1).to_i]
			thetas = gsl_vector(:theta)
# 				ep thetas
			#eputs "End theta_along_field_line"
			return thetas if agk? or (@s_hat_input or @shat).abs < 1.0e-5
			if gryfx?
				theta_list = ((1..kx_elements.size).to_a.map do |i|
					thetas * i
				end)
				thetas = theta_list.inject{|o,n| o.connect(n)}
				thetas -= Math::PI*(kx_elements.size-1)
				return thetas

			end
			theta_list = (kx_elements.map do |element|
			        
				kx = list(:kx)[(element + 1).to_i]
# 				             ep element
				             #ep 'kx', kx, 'shat', (@s_hat_input or @shat), 'ky',   list(:ky)[options[:ky_index]]
				thetas - 1.0 / (@s_hat_input or @shat) / list(:ky)[options[:ky_index]] * kx
			end).inject{|old, new| old.connect(new)}
# 				thetas = gsl_vector(:theta) - 1.0 / @shat / list(:ky)[options[:ky_index]] * list(:kx)[(kx_elements[0] + 1).to_i] #- Math::PI*(kx_elements.size  - 1)
# 				get_list_of(:ky, :t)
# 				if @g_exb #and options[:t_index]

				if options[:moving]
					theta_list = theta_list  -  Math::PI * 2.0 * (jump(options) / @jtwist)
				else
# 						ep 'jump % jtwist is!!', jump(options) % @jtwist
					theta_list = theta_list - Math::PI * 2.0 / @nx.to_f * ((jump(options) % @jtwist).to_f / @jtwist.to_f)
				end
# 					jump = 0	
# 				end
# 				theta_list = thetas.dup #gsl_vector(:theta) - Math::PI*kx_elements.size
# 				(kx_elements.size - 1).times do 
# 					thetas = thetas + Math::PI * 2.0
# 					theta_list = theta_list.connect(thetas)
# 				end
# 				pp theta_list.to_a.values_at(0, theta_list.size - 1)
# 				pp theta_list.to_a.max
			theta_vector = theta_list
		end
# 			theta_vector = theta_vector.reverse if options[:rev]
		theta_vector *= (@shat) if options[:z]
		return theta_vector

	end
end

#tpar2_by_mode_over_time_gsl_vector(options) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/gs2crmod/gsl_data.rb', line 561

def tpar2_by_mode_over_time_gsl_vector(options)
	Dir.chdir(@directory) do			#Necessary options: :ky and :kx
		#Optional options: :t_index_window
		# 		eputs "got here"
		#options[:begin_element], options[:end_element] = (options[:t_index_window] ? options[:t_index_window].map{|ind| ind -1} : [0, -1])
		options.setup_time_window			
		tpar_t_array=nil
		if @grid_option == "single"
			tpar_t_array = netcdf_file.var('tpar2').get('start' => [options[:begin_element]], 'end' => [options[:end_element]]).to_a.flatten
		else
# 				value = options[:ky]
# 				eputs value
# 				get_list_of(:ky)
# 				index = @ky_list.find{|index,val| (val-value).abs < Float::EPSILON}[0]
			options.convert_to_index(self, :kx, :ky, :species)
# 				p options
			tpar_t_array = netcdf_file.var("tpar2_by_mode").get('start' => [options[:kx_index] - 1, options[:ky_index] - 1, options[:species_index] - 1, options[:begin_element]], 'end' => [options[:kx_index] - 1, options[:ky_index] - 1, options[:species_index] - 1, options[:end_element]]).to_a.flatten
# 				eputs 'tpar_t_array.size', tpar_t_array.size
		end
		return GSL::Vector.alloc(tpar_t_array)

	end
end

#tperp2_by_mode_over_time_gsl_vector(options) ⇒ Object



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/gs2crmod/gsl_data.rb', line 585

def tperp2_by_mode_over_time_gsl_vector(options)
	Dir.chdir(@directory) do			#Necessary options: :ky and :kx
		#Optional options: :t_index_window
		# 		eputs "got here"
		#options[:begin_element], options[:end_element] = (options[:t_index_window] ? options[:t_index_window].map{|ind| ind -1} : [0, -1])
		options.setup_time_window			
		tperp_t_array=nil
		if @grid_option == "single"
			tperp_t_array = netcdf_file.var('tperp2').get('start' => [options[:begin_element]], 'end' => [options[:end_element]]).to_a.flatten
		else
# 				value = options[:ky]
# 				eputs value
# 				get_list_of(:ky)
# 				index = @ky_list.find{|index,val| (val-value).abs < Float::EPSILON}[0]
			options.convert_to_index(self, :kx, :ky, :species)
# 				p options
			tperp_t_array = netcdf_file.var("tperp2_by_mode").get('start' => [options[:kx_index] - 1, options[:ky_index] - 1, options[:species_index] - 1, options[:begin_element]], 'end' => [options[:kx_index] - 1, options[:ky_index] - 1, options[:species_index] - 1, options[:end_element]]).to_a.flatten
# 				eputs 'tperp_t_array.size', tperp_t_array.size
		end
		return GSL::Vector.alloc(tperp_t_array)

	end
end

#transient_amplification_over_kx_gsl_vector(options) ⇒ Object



261
262
263
264
# File 'lib/gs2crmod/gsl_data.rb', line 261

def transient_amplification_over_kx_gsl_vector(options)
	options[:direction] = :kx
	transient_amplification_over_kxy_gsl_vector(options)
end

#transient_amplification_over_ky_gsl_vector(options) ⇒ Object



265
266
267
268
# File 'lib/gs2crmod/gsl_data.rb', line 265

def transient_amplification_over_ky_gsl_vector(options)
	options[:direction] = :ky
	transient_amplification_over_kxy_gsl_vector(options)
end

#transient_es_heat_flux_amplification_over_kx_gsl_vector(options) ⇒ Object



241
242
243
244
# File 'lib/gs2crmod/gsl_data.rb', line 241

def transient_es_heat_flux_amplification_over_kx_gsl_vector(options)
	options[:direction] = :kx
	transient_es_heat_flux_amplification_over_kxy_gsl_vector(options)
end

#transient_es_heat_flux_amplification_over_kxy_gsl_vector(options) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/gs2crmod/gsl_data.rb', line 250

def transient_es_heat_flux_amplification_over_kxy_gsl_vector(options)
	Dir.chdir(@directory) do			# i.e. phi2_by_ky_vs_time or phi2_by_kx_vs_time
		kxy = options[:direction].to_sym
		
# 			ep :growth_rate_at_ + kxy
		p send(:transient_es_heat_flux_amplification_at_species_at_ + kxy)
		return GSL::Vector.alloc(send(:transient_es_heat_flux_amplification_at_species_at_ + kxy)[options[:species_index]-1].values)

	end
end

#transient_es_heat_flux_amplification_over_ky_gsl_vector(options) ⇒ Object



246
247
248
249
# File 'lib/gs2crmod/gsl_data.rb', line 246

def transient_es_heat_flux_amplification_over_ky_gsl_vector(options)
	options[:direction] = :ky
	transient_es_heat_flux_amplification_over_kxy_gsl_vector(options)
end

#vres_energy_gsl_vector(options) ⇒ Object

Velocity space diagnostics: integral error due to energy resolution



910
911
912
913
914
# File 'lib/gs2crmod/gsl_data.rb', line 910

def vres_energy_gsl_vector(options)
	raise "Velocity space vres diagnostics not found" unless FileTest.exist? "#@directory/#@run_name.vres"
	vres = GSL::Vector.filescan("#@directory/#@run_name.vres")
	return vres[2]
end

#vres_pitch_angle_gsl_vector(options) ⇒ Object

Velocity space diagnostics: integral error due to pitch angle resolution



903
904
905
906
907
# File 'lib/gs2crmod/gsl_data.rb', line 903

def vres_pitch_angle_gsl_vector(options)
	raise "Velocity space vres diagnostics not found" unless FileTest.exist? "#@directory/#@run_name.vres"
	vres = GSL::Vector.filescan("#@directory/#@run_name.vres")
	return vres[1]
end

#x_gsl_vector(options) ⇒ Object



977
978
979
980
981
982
983
984
# File 'lib/gs2crmod/gsl_data.rb', line 977

def x_gsl_vector(options)
	raise "options nakx and interpolate_x are incompatible" if options[:nakx] and options[:interpolate_x]
	kx = gsl_vector(:kx, options)
	lx = 2*Math::PI/kx.to_box_order[1]
	#ep 'lx', lx
	nx = options[:nakx]||kx.size
	GSL::Vector.indgen(nx, 0, lx/nx)
end

#y_gsl_vector(options) ⇒ Object



985
986
987
988
989
990
991
992
# File 'lib/gs2crmod/gsl_data.rb', line 985

def y_gsl_vector(options)
	raise "options naky and interpolate_y are incompatible" if options[:naky] and options[:interpolate_y]
	ky = gsl_vector(:ky, options)
	ly = 2*Math::PI/ky[1]
	ny = options[:naky]||ky.size
	ysize = ny*2-2+ny%2
	GSL::Vector.indgen(ysize, 0, ly/ysize)
end

#zonal_spectrum_gsl_vector(options) ⇒ Object



993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/gs2crmod/gsl_data.rb', line 993

def zonal_spectrum_gsl_vector(options)
	Dir.chdir(@directory) do

     gmzf = gsl_matrix('spectrum_over_ky_over_kx',options)
                   veczf = GSL::Vector.alloc(gmzf.shape[1])
                 # p gmzf.get_row(0).size
                 # p gmzf.get_row(0)
     gmzf.shape[1].times{|i| veczf[i] = gmzf[0,i]}
     return veczf
#else
	#raise CRError.new("Unknown gsl_vector requested: #{name}")
	end
	#  			eputs data; gets
end