FUNCTION gen_exinds, vec ; 7-jan-11/MR: coded ; determines the indices of the local extrema in the vector vec if vec(1) ge vec(0) then $ grows=-1 $ else $ grows=1 imax=n_elements(vec)-1 imin=0 s0=1 while imin lt imax do begin ind = (where(grows*(vec(imin:imax-1)-vec(imin+1:imax)) lt 0.))(0) if ind ne -1 then begin ind = ind+imin if s0 then begin exinds = [ind] s0 = 0 endif else $ exinds = [exinds, ind] imin = ind grows = -grows endif else $ imin = imax endwhile if n_elements(exinds) eq 0 then exinds = -1 return, exinds END ;****************************************************************************************** PRO plot_segs, x, y, seginds, styles=styles, colors=colors, overplot=overplot, _extra=extra ; 7-jan-11/MR: coded ; plots y vs x with segemented coloring/linestyling according to the indices of the segment boundaries seginds if not keyword_set(colors) then $ colors = [!p.color,!p.color] $ else if n_elements(colors) eq 1 then begin if colors(0) eq 250 then $ colors = [colors, !p.color] $ else $ colors = [colors, 250] endif if not keyword_set(styles) then begin if colors(0) eq colors(1) then $ styles = [0,3] $ else $ styles = [0,0] endif else if n_elements(styles) eq 1 then begin if styles(0) eq 3 then $ styles = [styles, 0] $ else $ styles = [styles, 3] endif np = n_elements(x) < n_elements(y) if np le 1 then return if not keyword_set(overplot) then $ plot, x(0:np-1), y(0:np-1), /nodata, _extra=extra ia = 0 toggle = 0 for i=0,n_elements(seginds)-1 do begin ie = seginds(i) > 0 oplot, x(ia:ie), y(ia:ie), lines=styles(toggle), color=colors(toggle) ia = ie toggle = 1-toggle endfor if ia lt np-1 then oplot, x(ia:np-1), y(ia:np-1), lines=styles(toggle), color=colors(toggle) END ;****************************************************************************************** PRO alloc_specs, spec, lint_shell, lint_z, lcomplex, fullspec=fullspec common pars, nx, ny, nz, nk, ncomp, nt, Lx, Ly, Lz if lint_shell then begin if lint_z then $ dims='nk' $ else $ dims='nk,nz' endif else if lint_z then $ dims='nx,ny' $ else $ dims='nx,ny,nz' if lcomplex then $ cmd = 'complex' $ else $ cmd = 'flt' cmd = 'spec='+cmd+'arr('+dims ierr = execute(cmd+')') if ~ierr then begin print, 'alloc_specs: Allocation '+cmd+') failed!!!' stop endif if keyword_set(fullspec) then begin ierr = execute('full'+cmd+',ncomp,nt)') if ~ierr then begin print, 'alloc_specs: Allocation '+'full'+cmd+',nt) failed!!!' stop endif endif END ;****************************************************************************************** FUNCTION find_no_wavenos, headline, text, symbol, no opos = strpos(headline,'(') & cpos = strpos(headline,')') len = cpos-opos-1 if len le 0 then begin print, 'Warning - header corrupt: Number of '+strtrim(text,2)+' wavenumbers missing -' print, ' will use '+strtrim(symbol,2)+'='+strtrim(string(no),2)+' which is perhaps by one too large.' print, ' Correct data file by hand (or '+strtrim(symbol,2)+' in code if necessary).' endif else begin nol=no on_ioerror, readerr reads, strmid(headline,opos+1,len), nol no=nol return, cpos+1 readerr: print, ' Error when reading '+strtrim(symbol,2)+': '+!ERROR_STATE.MSG on_ioerror, NULL stop return, -1 endelse END ;****************************************************************************************** FUNCTION read_firstpass, file, lint_shell, lint_z, lcomplex, extr, startpos, fmt=fmt, zpos=zpos common pars, nx, ny, nz, nk, ncomp, nt, Lx, Ly, Lz common wavenrs, kxs, kys, kshell ; ; Looping through all the data to get number of spectral snapshots ; close,2 openr,2, file, error=err if err ne 0 then return, 0 headline = '' readf, 2, headline headline = strlowcase(headline) witheader=strpos(headline,'spectrum') ne -1 if witheader then begin warn = 'Warning: File header of '+strtrim(file,2) if (strpos(headline,'shell-integrated') ne -1) ne lint_shell then begin if lint_shell then $ print, warn+' says "not shell-integrated" - assume that' $ else $ print, warn+' says "shell-integrated" - assume that' lint_shell = ~lint_shell endif if (strpos(headline,'z-integrated') ne -1) ne lint_z then begin if lint_z then $ print, warn+' says "not z-integrated" - assume that' $ else $ print, warn+' says "z-integrated" - assume that' lint_z = ~lint_z endif lcomplex = strpos(headline,'complex') ne -1 if strpos(headline,'componentwise') ne -1 then begin ia = strpos(headline,'(')+1 & ie = strpos(headline,')')-1 ncomp = fix(strmid(headline,ia,ie-ia+1)) endif else $ ncomp = 1 readf, 2, headline if strpos(strlowcase(headline),'wavenumbers') eq -1 then begin print, warn+' corrupt (keyword "wavenumbers" missing!' lcorr=1 endif else $ lcorr=0 if (lint_shell) then begin if not lcorr then $ ind=find_no_wavenos(headline, 'shell', 'nk', nk) kshell = fltarr(nk) readf, 2, kshell endif else begin if not lcorr then begin ind=find_no_wavenos(headline, 'x', 'nx', nx) if ind ge 0 then ind=find_no_wavenos(strmid(headline,ind), 'y', 'ny', ny) endif kxs = fltarr(nx) & kys = fltarr(ny) readf, 2, kxs, kys kxs = shift(kxs,nx/2) kys = shift(kys,ny/2) endelse if not lint_z then begin point_lun, -2, pos readf, 2, headline if strpos(strlowcase(headline),'positions') eq -1 then begin ; ; no z-positions in data file -> spectra given for the whole z grid ; point_lun, 2, pos pc_read_grid,obj=grid,/trim,/quiet zpos=grid.z nz = n_elements(zpos) lzpos_exist=0 ; endif else begin ia = strpos(headline,'(')+1 & ie = strpos(headline,')')-1 nz = fix(strmid(headline,ia,ie-ia+1)) if nz le 0 then begin print, warn+' corrupt! -- no positive number of z positions given!' stop endif else begin zpos=fltarr(nz) readf, 2, zpos endelse lzpos_exist=1 endelse endif endif else begin kxs=(findgen(nx)-nx/2)*2*!pi/Lx ;,(nx+1)/2)*2*!pi/Lx kys=(findgen(ny)-ny/2)*2*!pi/Ly ;,(ny+1)/2)*2*!pi/Ly if lint_shell then begin ; valid for old style files kshell = fltarr(nk) for ix=0,nx-1 do $ for iy=0,ny-1 do begin ik = round( sqrt( kxs(ix)^2+kys(iy)^2 )/(2*!pi/Lx) ) kshell(ik)=ik*2*!pi/Lx endfor endif pos=0L point_lun, 2, pos ; set file position to top of file (i.e., at first time stamp) endelse alloc_specs, spectrum1, lint_shell, lint_z, lcomplex alloc_specs, spectrum1y, lint_shell, lint_z, lcomplex alloc_specs, spectrum1z, lint_shell, lint_z, lcomplex if lcomplex then begin globalmin=1e12 +complexarr(ncomp) globalmax=1e-30+complexarr(ncomp) endif else begin globalmin=1e12 +fltarr(ncomp) globalmax=1e-30+fltarr(ncomp) endelse nt=0L & time=0. & s0=1 & s0form=1 & nseg=0 while not eof(2) do begin if s0 then begin if witheader then begin point_lun, -2, pos readf,2,headline if strpos(strlowcase(headline),'spectrum') eq -1 then $ point_lun, 2, pos $ else begin readf,2,headline readf,2, kxs, kys if lint_shell then begin readf, 2, headline readf, 2, kshell endif if not lint_z and lzpos_exist then begin readf,2,headline readf,2, zpos endif endelse nz = abs(nz) endif endif if witheader then begin on_ioerror, newheader point_lun, -2, timepos endif readf,2,time if witheader then begin on_ioerror, NULL if s0 then begin nseg +=1 if nseg eq 1 then startpos = timepos else startpos = [startpos,timepos] s0=0 endif endif if s0form then begin if lcomplex then begin ; ; derive format for reading from data line (only needed for complex data) ; dataline = '' point_lun, -2, pos readf, 2, dataline point_lun, 2, pos inds = strsplit(dataline,' ',length=lens) num = n_elements(inds) & ends = inds+lens lens = ends-[0,ends(0:num-2)] fmt = '((' for ii=0,num-1,2 do $ fmt += '(e'+strtrim(string(lens(ii)),2)+''+'.1,e'+strtrim(string(lens(ii+1)),2)+'.1),' fmt = strmid(fmt,0,strlen(fmt)-1)+'))' endif else $ fmt=0 s0form = 0 endif for i=0,ncomp-1 do begin readf,2,spectrum1,format=fmt ;readf,2,spectrum1y,format=fmt ;readf,2,spectrum1z,format=fmt globalmax(i)=max(spectrum1) > globalmax(i) globalmin(i)=min(spectrum1) < globalmin(i) endfor nt=nt+1L continue newheader: if eof(2) then exit $ else begin s0=1 point_lun, 2, timepos endelse endwhile close, 2 ; end first reading extr = [globalmin,globalmax] return, nt END ;****************************************************************************************** PRO pc_power_xy,var1,var2,last,w,v1=v1,v2=v2,all=all,wait=wait,k=k,spec1=spec1, $ spec2=spec2,i=i,tt=tt,noplot=noplot,tmin=tmin,tmax=tmax, $ tot=tot,lin=lin,png=png,yrange=yrange,norm=norm,helicity2=helicity2, $ compensate1=compensate1,compensate2=compensate2,datatopdir=datatopdir, $ lint_shell=lint_shell, lint_z=lint_z, print=prnt, obj=obj ; ; $Id$ ; ; This routine reads in the power spectra generated during the run ; (provided dspec is set to a time interval small enough to produce ; enough spectra.) ; By default, spec1 is kinetic energy and spec2 magnetic energy. ; The routine plots the spectra for the last time saved in the file. ; All times are stored in the arrays spec1 and spec2. ; The index of the first time is 1, and of the last time it is i-2. ; ; var1 : Use v1 instead (Kept to be backward compatible) ; var2 : Use v2 instead (Kept to be backward compatible) ; last : Use all instead (Kept to be backward compatible) ; w : Use wait instead (Kept to be backward compatible) ; ; v1 : First variable to be plotted (Ex: 'u') ; v2 : Second variable to be plotted (Ex: 'b') ; all : Plot all snapshots if /all is set, otherwise only last snapshot ; wait : Time to wait between each snapshot (if /all is set) ; k : Returns the wavenumber vector ; spec1 : Returns all the spectral snapshots of the first variable ; spec2 : Returns all the spectral snapshots of the second variable ; i : The index of the last time is i-2 ; tt : Returns the times for the different snapshots (vector) ; noplot: Do not plot if set ; tmin : First time for plotting snapshots (if /all is set) ; tmax : Last time for plotting snapshots (if /all is set) ; tot : Plots total power spectrum if tot=1 ; lin : Plots the line k^lin ; png : to write png file for making a movie ; yrange: y-range for plot ; compensate: exponent for compensating power spectrum (default=0) ; lint_shell: shell-integrated spectrum (default=1) ; lint_z : z-integrated spectrum (default=0) ; print : flag for print into PS file (default=0) ; ; 24-sep-02/nils: coded ; 5-oct-02/axel: comments added ; 29-nov-10/MR : adaptions to different types of spectra (with/without headers), ; keyword parameters lint_shell, lint_z and print added ; common pars, nx, ny, nz, nk, ncomp, nt, Lx, Ly, Lz common wavenrs, kxs, kys, kshell default,var1,'u' default,var2, '' ;'b' default,last,1 default,w,0.1 default,tmin,0 default,tmax,1e34 default,tot,0 default,lin,0 default,dir,'' default,compensate1,0 default,compensate2,compensate1 default,compensate,compensate1 default,datatopdir,'data' default,lint_shell,1 default,lint_z,0 default, prnt, 0 ; ; This is done to make the code backward compatible. ; if keyword_set(all) then begin last=0 end if keyword_set(wait) then begin w=wait end if keyword_set(v1) then begin file1='power'+v1 if keyword_set(v2) then begin file2='power'+v2 end else begin file2='' end end else begin if keyword_set(v2) then begin print,'In order to set v2 you must also set v1!' print,'Exiting........' stop end file1='power'+var1 if var2 ne '' then $ file2='power'+var2 $ else $ file2='' end ;;file1=file1+'_xy' ;;if file2 ne '' then file2=file2+'_xy' file1=file1+'.dat' if file2 ne '' then file2=file2+'.dat' ; ; plot only when iplot=1 (default) ; can be turned off by using /noplot ; if keyword_set(noplot) then iplot=0 else iplot=1 ; ;!p.multi=[0,1,1] ;!p.charsize=2 ; ; Reading number of grid points from 'data/dim.dat' ; pc_read_param,o=param,/quiet Lx=param.Lxyz[0] Ly=param.Lxyz[1] Lz=param.Lxyz[2] pc_read_dim,o=param,/quiet nx=param.nx ny=param.ny nz=param.nz ; ; Calculating some variables ; first='true' kxs = fltarr(nx) & kys = fltarr(ny) k0=2.*!pi/Lz nk0=round( sqrt( ((nx+1)*!pi/Lx)^2+((ny+1)*!pi/Ly)^2)/(2*!pi/Lx) )+1 nk=nk0 ;if keyword_set(v1) then begin ; if ((v1 EQ "_phiu") OR (v1 EQ "_phi_kin") $ ; OR (v1 EQ "hel_phi_kin") OR (v1 EQ "hel_phi_mag") $ ; OR (v1 EQ "_phib") OR (v1 EQ "_phi_mag") ) then begin ; k = k*k0 ; end ;end nt1 = read_firstpass( datatopdir+'/'+file1, lint_shell, lint_z, lcomplex, global_ext1, startpos1, fmt=fmt1, zpos=zpos1 ) if nt1 eq 0 then begin print, 'Error when reading '+datatopdir+'/'+file1+'!' stop endif else $ nk1 = nk default,yrange,[10.0^(floor(alog10(float(global_ext1(0))))),10.0^ceil(alog10(float(global_ext1(1))))] ; ; Reading file 2 if it is defined ; if (file2 ne '') then begin nk = nk0 nt2 = read_firstpass( datatopdir+'/'+file2, lint_shell, lint_z, lcomplex, global_ext2, startpos2, fmt=fmt2, zpos=zpos2 ) if nt2 eq 0 or nk ne nk1 then begin if nt2 eq 0 then $ print, 'Error: No data readable from file '+datatopdir+'/'+file2+'!' $ else $ print, 'Error: Number of shell wavenumbers different in files '+datatopdir+'/'+file1+' and '+datatopdir+'/'+file2+'!' print, ' File2 ignored.' file2 = '' close, 1 nk = nk1 nt = nt1 endif else begin if nt1 ne nt2 then begin print, 'Warning: Number of time steps different in files '+datatopdir+'/'+file1+' and '+datatopdir+'/'+file2+'!' print, ' Adopting minimum.' nt = nt1