def createImgFLEXPART(inputPath): driver = gdal.GetDriverByName('GTiff') H = rh.Header(inputPath) #H.fill_backward(nspec=(0,1)) lower_left = [H.outlon0, H.outlat0] upper_right = [ int(H.outlon0 + (H.dxout * H.numxgrid)), int(H.outlat0 + (H.dyout * H.numygrid)) ] heightLevels = H.outheight timestamps = H.available_dates outFileList = [] for t in timestamps: G = rg.read_grid(H, date=t) grid = G[(0, t)] volume = grid.grid[:, :] * 112 / 35 xSize = volume.shape[0] ySize = volume.shape[1] numBands = volume.shape[2] filename = 'FLEXPART_SO2_' + t[:8] + '.' + t[8:] + '.tif' dst_ds = driver.Create(filename, xSize, ySize, numBands, GDT_Float32) for i in range(numBands): band = volume[:, :, i].squeeze().transpose() band = np.flipud(band) dst_ds.GetRasterBand(i + 1).WriteArray(band) dst_ds.GetRasterBand(i + 1).SetNoDataValue(-9999) dst_ds.GetRasterBand(i + 1).ComputeStatistics(False) pixelWidth = abs((lower_left[0] - upper_right[0]) / xSize) pixelHeight = abs((lower_left[1] - upper_right[1]) / ySize) dst_ds.SetGeoTransform( [lower_left[0], pixelWidth, 0, upper_right[1], 0, -pixelHeight]) srs = osr.SpatialReference() srs.SetWellKnownGeogCS('WGS84') dst_ds.SetProjection(srs.ExportToWkt()) time = t[:4] + '-' + t[4:6] + '-' + t[6:8] + 'T' + t[8:10] + ':' + t[ 10:12] + ':' + t[12:14] + 'Z' dst_ds.SetMetadataItem('TIME_START', time) dst_ds.SetMetadataItem('TIME_END', time) dst_ds.SetMetadataItem('GLOBAL_MIN', str(np.amin(volume[:]))) dst_ds.SetMetadataItem('GLOBAL_MAX', str(np.amax(volume[:]))) levels = str() for l in heightLevels: levels += str(l) + ',' levels = levels[:-1] dst_ds.SetMetadataItem('VERTICAL_LEVELS', levels) dst_ds.SetMetadataItem('VERTICAL_LEVELS_NUMBER', str(len(heightLevels))) dst_ds = None outFileList.append(filename) logging.debug('Finished processing for ' + t) return outFileList
import eof_reconstruction as eof_r import matplotlib.pyplot as plt import noisegen as ng # Open displacement maps path = '/home/hipperta/Documents/img/serie_temp/argentiere/s1a/' im = [] n_i = 0 for filename in sorted(os.listdir(path)): fichier = gampy.ouvrir(path + filename, 's') im.append(gampy.reshape(fichier)) n_i += 1 # Create mask with shape defined in text file (Gamma) polygon = rg.read_grid('argentiere_mask.txt', 0, 1, False, False) width = im[0].shape[1] height = im[0].shape[0] img = Image.new('L', (width, height), 0) ImageDraw.Draw(img).polygon(polygon, outline=1, fill=1) mask = np.array(img) im_i = copy.deepcopy(im) n_pts = len(im[0][mask == True]) datai = np.zeros([n_i, n_pts]) dataii = np.zeros([n_i, n_pts]) # Generate cross validation points mask_gaps = np.zeros([n_i, n_pts], dtype=bool) mask_cv = np.zeros([n_i, n_pts], dtype=bool)
def metadata(inpfile='BOUT.inp', path='.', v=False): filepath = path + '/' + inpfile print(filepath) inp = read_inp(path=path, boutinp=inpfile) inp = parse_inp(inp) #inp file print(path) outinfo = file_import(path + '/BOUT.dmp.0.nc') #output data try: print(path) cxxinfo = no_comment_cxx(path=path, boutcxx='physics_code.cxx.ref') #evolved = get_evolved_cxx(cxxinfo) fieldkeys = get_evolved_cxx(cxxinfo) fieldkeys = ['[' + elem + ']' for elem in fieldkeys] except: print('cant find the cxx file') #gridoptions = {'grid':grid,'mesh':mesh} if '[mesh]' in list(inp.keys()): #IC = outinfo IC = read_grid(path + '/BOUT.dmp.0.nc') #output data again elif 'grid' in inp['[main]']: gridname = inp['[main]']['grid'] try: IC = read_grid(gridname) #have to be an ansoulte file path for now print('IC: ', type(IC)) # print IC.variables # print gridname except: #print gridname print('Fail to load the grid file') #print IC #print gridname #print len(IC) #print IC evolved = [] collected = [] ICscale = [] # fieldkeys = ['[Ni]','[Te]','[Ti]','[Vi]','[rho]', # '[Ajpar]','[Apar]','[vEBx]','[vEBy]','[vEBz]', # '[jpar]','[phi]'] #construct fieldkeys from cxx info #fieldkeys = ['['+x+']' for x in evolved] #fieldkeys = evolved #just look ahead and see what 3D fields have been output available = np.array([str(x) for x in outinfo]) a = np.array([(len(outinfo[x].shape) == 4) for x in available]) available = available[a] defaultIC = float(inp['[All]'].get('scale', 0.0)) # print inp.keys() #figure out which fields are evolved print(fieldkeys) for section in list(inp.keys()): #loop over section keys print('section: ', section) if section in fieldkeys: #pick the relevant sections print(section) #print inp[section].get('evolve','True') #rint (inp[section].get('evolve','True')).lower().strip() if (inp[section].get('evolve', 'True').lower().strip() == 'true' ): # and section[1:-1] in available : print('ok reading') evolved.append(section.strip('[]')) ICscale.append(float(inp[section].get('scale', defaultIC))) if inp[section].get('collect', 'False').lower().strip() == 'true': collected.append(section.strip('[]')) try: if inp['[physics]'].get('transport', 'False').lower().strip() == 'true': vEBstr = ['vEBx', 'vEBy', 'vEBz', 'vEBrms'] [collected.append(item) for item in vEBstr] except: print('no [physics] key') meta = OrderedDict() class ValUnit(object): def __init__(self, value=0, units=''): self.u = units self.v = value def todict(self): return {'u': self.u, 'v': self.v} #def decode_valunit(d): def ToFloat(metaString): try: return float(metaString) except ValueError: return metaString #meta['evolved'] = ValUnit(evolved,'') meta['evolved'] = evolved meta['collected'] = collected meta['IC'] = np.array(ICscale) d = {} print('evolved: ', evolved) # read meta data from .inp file, this is whre most metadata get written for section in list(inp.keys()): if (('evolve' not in inp[section]) and ('first' not in inp[section]) ): #hacky way to exclude some less relevant metadata for elem in list(inp[section].keys()): meta[elem] = ValUnit(ToFloat(inp[section][elem])) d[elem] = np.array(ToFloat(inp[section][elem])) #read in some values from the grid(IC) and scale them as needed norms = { 'Ni0': ValUnit(1.e14, 'cm^-3'), 'bmag': ValUnit(1.0e4, 'gauss'), 'Ni_x': ValUnit(1.e14, 'cm^-3'), 'Te_x': ValUnit(1.0, 'eV'), 'Ti_x': ValUnit(1.0, 'eV'), 'Rxy': ValUnit(1, 'm'), 'Bxy': ValUnit(1.0e4, 'gauss'), 'Bpxy': ValUnit(1.0e4, 'gauss'), 'Btxy': ValUnit(1.0e4, 'gauss'), 'Zxy': ValUnit(1, 'm'), 'dlthe': ValUnit(1, 'm'), 'dx': ValUnit(1, 'm'), 'hthe0': ValUnit(1, 'm') } availkeys = np.array([str(x) for x in outinfo]) tmp1 = np.array([x for x in availkeys]) #b = np.array([x if x not in available for x in a]) tmp2 = np.array([x for x in tmp1 if x not in available]) static_fields = np.array([x for x in tmp2 if x in list(norms.keys())]) #static_fields = tmp2 #print availkeys # print meta.keys() #print IC.variables.keys() # print tmp1 # print tmp2 for elem in static_fields: print('elem: ', elem) meta[elem] = ValUnit(IC.variables[elem][:] * norms[elem].v, norms[elem].u) d[elem] = np.array(IC.variables[elem][:] * norms[elem].v) for elem in IC.variables: if elem not in meta: if elem in list(norms.keys()): meta[elem] = ValUnit(IC.variables[elem][:] * norms[elem].v, norms[elem].u) d[elem] = np.array(IC.variables[elem][:] * norms[elem].v) else: meta[elem] = IC.variables[elem][:] d[elem] = IC.variables[elem][:] #print d.keys() #if case some values are missing default = { 'bmag': 1, 'Ni_x': 1, 'NOUT': 100, 'TIMESTEP': 1, 'MZ': 32, 'AA': 1, 'Zeff': ValUnit(1, ''), 'ZZ': 1, 'zlowpass': 0.0, 'transport': False } diff = set(default.keys()).difference(set(d.keys())) for elem in diff: #print 'diff: ',elem meta[elem] = default[elem] d[elem] = np.array(default[elem]) #print meta.keys() #print d.keys() #print meta['zlowpass'] if meta['zlowpass'] != 0: print(meta['MZ'].v, meta['zlowpass'].v) meta['maxZ'] = int(np.floor(meta['MZ'].v * meta['zlowpass'].v)) else: meta['maxZ'] = 5 #meta['nx'] = nx #meta['ny']= ny meta['dt'] = meta['TIMESTEP'] #nx,ny = d['Rxy'].shape #print meta['AA'].v meta['rho_s'] = ValUnit(1.02e2 * np.sqrt(d['AA'] * d['Te_x']) / (d['ZZ'] * d['bmag']), 'cm') # ion gyrorad at T_e, in cm meta['rho_i'] = ValUnit( 1.02e2 * np.sqrt(d['AA'] * d['Ti_x']) / (d['ZZ'] * d['bmag']), 'cm') meta['rho_e'] = ValUnit(2.38 * np.sqrt(d['Te_x']) / (d['bmag']), 'cm') meta['fmei'] = ValUnit(1. / 1836.2 / d['AA']) meta['lambda_ei'] = 24. - np.log(old_div(np.sqrt(d['Ni_x']), d['Te_x'])) meta['lambda_ii'] = 23. - np.log(d['ZZ']**3 * np.sqrt(2. * d['Ni_x']) / (d['Ti_x']**1.5)) # meta['wci'] = 1.0 * 9.58e3 * d['ZZ'] * d['bmag'] / d['AA'] # ion gyrofrteq meta['wpi'] = 1.32e3 * d['ZZ'] * np.sqrt(old_div( d['Ni_x'], d['AA'])) # ion plasma freq meta['wce'] = 1.78e7 * d['bmag'] #electron gyrofreq meta['wpe'] = 5.64e4 * np.sqrt(d['Ni_x']) #electron plasma freq meta['v_the'] = 4.19e7 * np.sqrt(d['Te_x']) #cm/s meta['v_thi'] = 9.79e5 * np.sqrt(old_div(d['Ti_x'], d['AA'])) #cm/s meta['c_s'] = 9.79e5 * np.sqrt( 5.0 / 3.0 * d['ZZ'] * d['Te_x'] / d['AA']) # meta['v_A'] = 2.18e11 * np.sqrt(old_div(1.0, (d['AA'] * d['Ni_x']))) meta['nueix'] = 2.91e-6 * d['Ni_x'] * meta['lambda_ei'] / d['Te_x']**1.5 # meta['nuiix'] = 4.78e-8 * d['ZZ']**4. * d['Ni_x'] * meta['lambda_ii'] / d[ 'Ti_x']**1.5 / np.sqrt(d['AA']) # meta['nu_hat'] = meta['Zeff'].v * meta['nueix'] / meta['wci'] meta['L_d'] = 7.43e2 * np.sqrt(old_div(d['Te_x'], d['Ni_x'])) meta['L_i_inrt'] = 2.28e7 * np.sqrt(old_div( d['AA'], d['Ni_x'])) / d['ZZ'] #ion inertial length in cm meta['L_e_inrt'] = 5.31e5 * np.sqrt(d['Ni_x']) #elec inertial length in cm meta['Ve_x'] = 4.19e7 * d['Te_x'] meta['R0'] = old_div((d['Rxy'].max() + d['Rxy'].min()), 2.0) print(d['Rxy'].mean(1)) print(d['ZMAX']) print(d['ZMIN']) meta['L_z'] = 1e2 * 2 * np.pi * d['Rxy'].mean(1) * ( d['ZMAX'] - d['ZMIN']) # in cm toroidal range meta['dz'] = (d['ZMAX'] - d['ZMIN']) #meta['lbNorm']=meta['L_z']*(d['Bpxy']/d['Bxy']).mean(1) #-binormal coord range [cm] meta['lbNorm'] = meta['L_z'] * (old_div(d['Bxy'], d['Bpxy'])).mean(1) #meta['zPerp']=np.array(meta['lbNorm']).mean*np.array(range(d['MZ']))/(d['MZ']-1) #let's calculate some profile properties dx = np.gradient(d['Rxy'])[0] meta['L'] = 1.0 * 1e2 * dx * (meta['Ni0'].v) / np.gradient( meta['Ni0'].v)[0] / meta['rho_s'].v meta['w_Ln'] = old_div(meta['c_s'], (np.min(abs(meta['L'])) * meta['wci'] * meta['rho_s'].v)) #normed to wci AA = meta['AA'].v ZZ = d['ZZ'] Te_x = d['Te_x'] Ti_x = d['Ti_x'] fmei = meta['fmei'].v meta['lpar'] = 1e2 * ( (old_div(d['Bxy'], d['Bpxy'])) * d['dlthe']).sum(1) / meta[ 'rho_s'].v #-[normed], average over flux surfaces, parallel length #yes dlthe is always the vertical displacement #dlthe = (hthe0*2 pi)/nz #meta['lpar']=1e2*(d['Bxy']/d['Bpxy']).mean(1)*d['dlthe'].mean(1) #function of x meta['sig_par'] = old_div(1.0, (fmei * 0.51 * meta['nu_hat'])) #meta['heat_nue'] = ((2*np.pi/meta['lpar'])**2)/(fmei*meta['nu_hat']) #kz_e = kz_i*(rho_e/rho_i) # kz_s = kz_i*(rho_s/rho_i) # kz_i = (TWOPI/L_z)*(indgen((*current_str).fft.nz+1))*rho_i # knorm = (TWOPI/lbNorm)*(indgen((*current_str).fft.nz+1))*rho_s # for now just translate for elem in meta: if type(meta[elem]).__name__ == 'ValUnit': meta[elem] = {'u': meta[elem].u, 'v': meta[elem].v} print('meta: ', type(meta)) return meta
def metadata(inpfile='BOUT.inp',path ='.',v=False): filepath = path+'/'+inpfile print filepath inp = read_inp(path=path,boutinp=inpfile) inp = parse_inp(inp) #inp file print path outinfo = file_import(path+'/BOUT.dmp.0.nc') #output data try: print path cxxinfo = no_comment_cxx(path=path,boutcxx='physics_code.cxx.ref') #evolved = get_evolved_cxx(cxxinfo) fieldkeys = get_evolved_cxx(cxxinfo) fieldkeys = ['['+elem+']' for elem in fieldkeys] except: print 'cant find the cxx file' #gridoptions = {'grid':grid,'mesh':mesh} if '[mesh]' in inp.keys(): #IC = outinfo IC = read_grid(path+'/BOUT.dmp.0.nc') #output data again elif 'grid' in inp['[main]']: gridname = inp['[main]']['grid'] try: IC = read_grid(gridname) #have to be an ansoulte file path for now print 'IC: ',type(IC) # print IC.variables # print gridname except: #print gridname print 'Fail to load the grid file' #print IC #print gridname #print len(IC) #print IC evolved = [] collected =[] ICscale = [] # fieldkeys = ['[Ni]','[Te]','[Ti]','[Vi]','[rho]', # '[Ajpar]','[Apar]','[vEBx]','[vEBy]','[vEBz]', # '[jpar]','[phi]'] #construct fieldkeys from cxx info #fieldkeys = ['['+x+']' for x in evolved] #fieldkeys = evolved #just look ahead and see what 3D fields have been output available = np.array([str(x) for x in outinfo]) a = np.array([(len(outinfo[x].shape) ==4) for x in available]) available = available[a] defaultIC = float(inp['[All]'].get('scale',0.0)) # print inp.keys() #figure out which fields are evolved print fieldkeys for section in inp.keys(): #loop over section keys print 'section: ', section if section in fieldkeys: #pick the relevant sections print section #print inp[section].get('evolve','True') #rint (inp[section].get('evolve','True')).lower().strip() if (inp[section].get('evolve','True').lower().strip() == 'true'):# and section[1:-1] in available : print 'ok reading' evolved.append(section.strip('[]')) ICscale.append(float(inp[section].get('scale',defaultIC))) if inp[section].get('collect','False').lower().strip() == 'true': collected.append(section.strip('[]')) try: if inp['[physics]'].get('transport','False').lower().strip() == 'true': vEBstr = ['vEBx','vEBy','vEBz','vEBrms'] [collected.append(item) for item in vEBstr] except: print 'no [physics] key' meta = OrderedDict() class ValUnit(object): def __init__(self,value=0,units=''): self.u = units self.v = value def todict(self): return {'u':self.u,'v':self.v} #def decode_valunit(d): def ToFloat(metaString): try: return float(metaString) except ValueError: return metaString #meta['evolved'] = ValUnit(evolved,'') meta['evolved'] = evolved meta['collected'] = collected meta['IC']= np.array(ICscale) d = {} print 'evolved: ',evolved # read meta data from .inp file, this is whre most metadata get written for section in inp.keys(): if (('evolve' not in inp[section]) and ('first' not in inp[section])): #hacky way to exclude some less relevant metadata for elem in inp[section].keys(): meta[elem] = ValUnit(ToFloat(inp[section][elem])) d[elem] = np.array(ToFloat(inp[section][elem])) #read in some values from the grid(IC) and scale them as needed norms = {'Ni0':ValUnit(1.e14,'cm^-3'),'bmag':ValUnit(1.0e4,'gauss'), 'Ni_x':ValUnit(1.e14,'cm^-3'), 'Te_x':ValUnit(1.0,'eV'),'Ti_x':ValUnit(1.0,'eV'),'Rxy':ValUnit(1,'m'), 'Bxy':ValUnit(1.0e4,'gauss'),'Bpxy':ValUnit(1.0e4,'gauss'), 'Btxy':ValUnit(1.0e4,'gauss'),'Zxy':ValUnit(1,'m'), 'dlthe':ValUnit(1,'m'),'dx':ValUnit(1,'m'),'hthe0':ValUnit(1,'m')} availkeys = np.array([str(x) for x in outinfo]) tmp1 = np.array([x for x in availkeys]) #b = np.array([x if x not in available for x in a]) tmp2 = np.array([x for x in tmp1 if x not in available]) static_fields = np.array([x for x in tmp2 if x in norms.keys()]) #static_fields = tmp2 #print availkeys # print meta.keys() #print IC.variables.keys() # print tmp1 # print tmp2 for elem in static_fields: print 'elem: ',elem meta[elem] = ValUnit(IC.variables[elem][:]*norms[elem].v,norms[elem].u) d[elem] = np.array(IC.variables[elem][:]*norms[elem].v) for elem in IC.variables: if elem not in meta: if elem in norms.keys(): meta[elem] = ValUnit(IC.variables[elem][:]*norms[elem].v,norms[elem].u) d[elem] = np.array(IC.variables[elem][:]*norms[elem].v) else: meta[elem] = IC.variables[elem][:] d[elem] = IC.variables[elem][:] #print d.keys() #if case some values are missing default = {'bmag':1,'Ni_x':1,'NOUT':100,'TIMESTEP':1, 'MZ':32,'AA':1,'Zeff':ValUnit(1,''),'ZZ':1, 'zlowpass':0.0,'transport':False} diff = set(default.keys()).difference(set(d.keys())) for elem in diff: #print 'diff: ',elem meta[elem] = default[elem] d[elem] = np.array(default[elem]) #print meta.keys() #print d.keys() #print meta['zlowpass'] if meta['zlowpass'] != 0: print meta['MZ'].v, meta['zlowpass'].v meta['maxZ'] = int(np.floor(meta['MZ'].v*meta['zlowpass'].v)) else: meta['maxZ'] = 5 #meta['nx'] = nx #meta['ny']= ny meta['dt'] = meta['TIMESTEP'] #nx,ny = d['Rxy'].shape #print meta['AA'].v meta['rho_s'] = ValUnit(1.02e2*np.sqrt(d['AA']*d['Te_x'])/(d['ZZ']* d['bmag']),'cm') # ion gyrorad at T_e, in cm meta['rho_i'] = ValUnit(1.02e2*np.sqrt(d['AA']*d['Ti_x'])/(d['ZZ']* d['bmag']),'cm') meta['rho_e'] = ValUnit(2.38*np.sqrt(d['Te_x'])/(d['bmag']),'cm') meta['fmei'] = ValUnit(1./1836.2/d['AA']) meta['lambda_ei'] = 24.-np.log(np.sqrt(d['Ni_x'])/d['Te_x']) ; meta['lambda_ii'] = 23.-np.log(d['ZZ']**3 * np.sqrt(2.*d['Ni_x'])/(d['Ti_x']**1.5)) # meta['wci'] = 1.0*9.58e3*d['ZZ']*d['bmag']/d['AA'] # ion gyrofrteq meta['wpi'] = 1.32e3*d['ZZ']*np.sqrt(d['Ni_x']/d['AA']) # ion plasma freq meta['wce'] = 1.78e7*d['bmag'] #electron gyrofreq meta['wpe'] = 5.64e4*np.sqrt(d['Ni_x'])#electron plasma freq meta['v_the'] = 4.19e7*np.sqrt(d['Te_x'])#cm/s meta['v_thi'] = 9.79e5*np.sqrt(d['Ti_x']/d['AA']) #cm/s meta['c_s'] = 9.79e5*np.sqrt(5.0/3.0 * d['ZZ'] * d['Te_x']/d['AA'])# meta['v_A'] = 2.18e11*np.sqrt(1.0/(d['AA'] * d['Ni_x'])) meta['nueix'] = 2.91e-6*d['Ni_x']*meta['lambda_ei']/d['Te_x']**1.5 # meta['nuiix'] = 4.78e-8*d['ZZ']**4.*d['Ni_x']*meta['lambda_ii']/d['Ti_x']**1.5/np.sqrt(d['AA']) # meta['nu_hat'] = meta['Zeff'].v*meta['nueix']/meta['wci'] meta['L_d'] = 7.43e2*np.sqrt(d['Te_x']/d['Ni_x']) meta['L_i_inrt'] = 2.28e7*np.sqrt(d['AA']/d['Ni_x'])/ d['ZZ'] #ion inertial length in cm meta['L_e_inrt'] = 5.31e5*np.sqrt(d['Ni_x']) #elec inertial length in cm meta['Ve_x'] = 4.19e7*d['Te_x'] meta['R0'] = (d['Rxy'].max()+d['Rxy'].min())/2.0 print d['Rxy'].mean(1) print d['ZMAX'] print d['ZMIN'] meta['L_z'] = 1e2 * 2*np.pi * d['Rxy'].mean(1) *(d['ZMAX'] - d['ZMIN']) # in cm toroidal range meta['dz'] = (d['ZMAX'] - d['ZMIN']) #meta['lbNorm']=meta['L_z']*(d['Bpxy']/d['Bxy']).mean(1) #-binormal coord range [cm] meta['lbNorm']=meta['L_z']*(d['Bxy']/d['Bpxy']).mean(1) #meta['zPerp']=np.array(meta['lbNorm']).mean*np.array(range(d['MZ']))/(d['MZ']-1) #let's calculate some profile properties dx = np.gradient(d['Rxy'])[0] meta['L'] = 1.0*1e2*dx*(meta['Ni0'].v)/np.gradient(meta['Ni0'].v)[0]/meta['rho_s'].v meta['w_Ln'] = meta['c_s']/(np.min(abs(meta['L']))*meta['wci'] *meta['rho_s'].v) #normed to wci AA = meta['AA'].v ZZ = d['ZZ'] Te_x = d['Te_x'] Ti_x = d['Ti_x'] fmei = meta['fmei'].v meta['lpar'] =1e2*((d['Bxy']/d['Bpxy'])*d['dlthe']).sum(1)/meta['rho_s'].v #-[normed], average over flux surfaces, parallel length #yes dlthe is always the vertical displacement #dlthe = (hthe0*2 pi)/nz #meta['lpar']=1e2*(d['Bxy']/d['Bpxy']).mean(1)*d['dlthe'].mean(1) #function of x meta['sig_par'] = 1.0/(fmei*0.51*meta['nu_hat']) #meta['heat_nue'] = ((2*np.pi/meta['lpar'])**2)/(fmei*meta['nu_hat']) #kz_e = kz_i*(rho_e/rho_i) # kz_s = kz_i*(rho_s/rho_i) # kz_i = (TWOPI/L_z)*(indgen((*current_str).fft.nz+1))*rho_i # knorm = (TWOPI/lbNorm)*(indgen((*current_str).fft.nz+1))*rho_s # for now just translate for elem in meta: if type(meta[elem]).__name__ =='ValUnit': meta[elem] = {'u':meta[elem].u,'v':meta[elem].v} print 'meta: ', type(meta) return meta
def metadata(inpfile='BOUT.inp',path ='.',v=False): filepath = path+'/'+inpfile print filepath inp = read_inp(path=path,boutinp=inpfile) inp = parse_inp(inp) try: gridname = inp['[main]']['grid'] IC = read_grid(gridname) #have to be an ansoulte file path for now print 'IC: ',type(IC) # print IC.variables # print gridname except: print gridname print 'Fail to load the grid file' #print gridname #print len(IC) #print IC evolved = [] ICscale = [] fieldkeys = ['[Ni]','[jpar]','[Te]','[Ti]','[Vi]','[rho]'] defaultIC = float(inp['[All]'].get('scale',0.0)) for section in inp.keys(): #loop over section keys #print section if section in fieldkeys: #pick the relevant sections print section #print type(inp[section].get('evolve','True')) print (inp[section].get('evolve','True')).lower().strip() if inp[section].get('evolve','True').lower().strip() == 'true': print 'ok reading' evolved.append(section.strip('[]')) ICscale.append(float(inp[section].get('scale',defaultIC))) meta = OrderedDict() class ValUnit(object): def __init__(self,value=0,units=''): self.u = units self.v = value def todict(self): return {'u':self.u,'v':self.v} #def decode_valunit(d): def ToFloat(metaString): try: return float(metaString) except ValueError: return metaString meta['evolved'] = ValUnit(evolved,'') meta['IC']= np.array(ICscale) d = {} # read meta data from .inp file for section in inp.keys(): if (('evolve' not in inp[section]) and ('first' not in inp[section])): #hacky way to exclude some less relevant metadata for elem in inp[section].keys(): meta[elem] = ValUnit(ToFloat(inp[section][elem])) d[elem] = np.array(ToFloat(inp[section][elem])) #read in some values from the grid(IC) and scale them as needed norms = {'Ni0':ValUnit(1.e14,'cm^-3'),'bmag':ValUnit(1.0e4,'gauss'), 'Ni_x':ValUnit(1.e14,'cm^-3'), 'Te_x':ValUnit(1.0,'eV'),'Ti_x':ValUnit(1.0,'eV'),'Rxy':ValUnit(1,'m'), 'Bxy':ValUnit(1.0e4,'gauss'),'Bpxy':ValUnit(1.0e4,'gauss'), 'Btxy':ValUnit(1.0e4,'gauss'),'Zxy':ValUnit(1,'m'), 'dlthe':ValUnit(1,'m'),'dx':ValUnit(1,'m'),'hthe0':ValUnit(1,'m')} for elem in norms.keys(): #print 'elem: ',elem meta[elem] = ValUnit(IC.variables[elem][:]*norms[elem].v,norms[elem].u) d[elem] = np.array(IC.variables[elem][:]*norms[elem].v) #if case some values are missing default = {'bmag':1,'Ni_x':1,'NOUT':100,'TIMESTEP':1, 'MZ':32,'AA':1,'Zeff':ValUnit(1,''),'ZZ':1} diff = set(default.keys()).difference(set(d.keys())) for elem in diff: meta[elem] = default[elem] d[elem] = np.array(default[elem]) nx,ny = d['Rxy'].shape #compute some quantities that are usefull print meta['AA'].v meta['nx'] = nx meta['ny']= ny meta['dt'] = meta['TIMESTEP'] meta['rho_s'] = ValUnit(1.02e2*np.sqrt(d['AA']*d['Te_x'])/(d['ZZ']* d['bmag']),'cm') # ion gyrorad at T_e, in cm meta['rho_i'] = ValUnit(1.02e2*np.sqrt(d['AA']*d['Ti_x'])/(d['ZZ']* d['bmag']),'cm') meta['rho_e'] = ValUnit(2.38*np.sqrt(d['Te_x'])/(d['bmag']),'cm') meta['fmei'] = ValUnit(1./1836.2/d['AA']) meta['lambda_ei'] = 24.-np.log(np.sqrt(d['Ni_x'])/d['Te_x']) ; meta['lambda_ii'] = 23.-np.log(d['ZZ']**3 * np.sqrt(2.*d['Ni_x'])/(d['Ti_x']**1.5)) # meta['wci'] = 9.58e3*d['ZZ']*d['bmag']/d['AA'] # ion gyrofrteq meta['wpi'] = 1.32e3*d['ZZ']*np.sqrt(d['Ni_x']/d['AA']) # ion plasma freq meta['wce'] = 1.78e7*d['bmag'] #electron gyrofreq meta['wpe'] = 5.64e4*np.sqrt(d['Ni_x'])#electron plasma freq meta['v_the'] = 4.19e7*np.sqrt(d['Te_x'])#cm/s meta['v_thi'] = 9.79e5*np.sqrt(d['Ti_x']/d['AA']) #cm/s meta['c_s'] = 9.79e5*np.sqrt(5.0/3.0 * d['ZZ'] * d['Te_x']/d['AA'])# meta['v_A'] = 2.18e11*np.sqrt(1.0/(d['AA'] * d['Ni_x'])) meta['nueix'] = 2.91e-6*d['Ni_x']*meta['lambda_ei']/d['Te_x']**1.5 # meta['nuiix'] = 4.78e-8*d['ZZ']**4.*d['Ni_x']*meta['lambda_ii']/d['Ti_x']**1.5/np.sqrt(d['AA']) # meta['nu_hat'] = meta['Zeff'].v*meta['nueix']/meta['wci'] meta['L_d'] = 7.43e2*np.sqrt(d['Te_x']/d['Ni_x']) meta['L_i_inrt'] = 2.28e7*np.sqrt(d['AA']/d['Ni_x'])/ d['ZZ'] #ion inertial length in cm meta['L_e_inrt'] = 5.31e5*np.sqrt(d['Ni_x']) #elec inertial length in cm meta['Ve_x'] = 4.19e7*d['Te_x'] meta['R0'] = (d['Rxy'].max()+d['Rxy'].min())/2.0 print d['Rxy'].mean(1) print d['ZMAX'] print d['ZMIN'] meta['L_z'] = 1e2 * 2*np.pi * d['Rxy'].mean(1) *(d['ZMAX'] - d['ZMIN']) # in cm toroidal range meta['dz'] = (d['ZMAX'] - d['ZMIN']) #meta['lbNorm']=meta['L_z']*(d['Bpxy']/d['Bxy']).mean(1) #-binormal coord range [cm] meta['lbNorm']=meta['L_z']*(d['Bxy']/d['Bpxy']).mean(1) #meta['zPerp']=np.array(meta['lbNorm']).mean*np.array(range(d['MZ']))/(d['MZ']-1) #let's calculate some profile properties dx = np.gradient(d['Rxy'])[0] meta['L'] = 1e2*dx*(meta['Ni0'].v)/np.gradient(meta['Ni0'].v)[0]/meta['rho_s'].v meta['w_Ln'] = meta['c_s']/(np.min(abs(meta['L']))*meta['wci'] *meta['rho_s'].v) #normed to wci AA = meta['AA'].v ZZ = d['ZZ'] Te_x = d['Te_x'] Ti_x = d['Ti_x'] fmei = meta['fmei'].v meta['lpar'] =1e2*((d['Bxy']/d['Bpxy'])*d['dlthe']).sum(1)/meta['rho_s'].v #-[normed], average over flux surfaces, parallel length #meta['lpar']=1e2*(d['Bxy']/d['Bpxy']).mean(1)*d['dlthe'].mean(1) #function of x meta['sig_par'] = 1.0/(fmei*0.51*meta['nu_hat']) #meta['heat_nue'] = ((2*np.pi/meta['lpar'])**2)/(fmei*meta['nu_hat']) #kz_e = kz_i*(rho_e/rho_i) # kz_s = kz_i*(rho_s/rho_i) # kz_i = (TWOPI/L_z)*(indgen((*current_str).fft.nz+1))*rho_i # knorm = (TWOPI/lbNorm)*(indgen((*current_str).fft.nz+1))*rho_s # for now just translate for elem in meta: if type(meta[elem]).__name__ =='ValUnit': meta[elem] = {'u':meta[elem].u,'v':meta[elem].v} print 'meta: ', type(meta) return meta
def metadata(inpfile="BOUT.inp", path=".", v=False): filepath = path + "/" + inpfile print(filepath) inp = read_inp(path=path, boutinp=inpfile) inp = parse_inp(inp) # inp file print(path) outinfo = file_import(path + "/BOUT.dmp.0.nc") # output data try: print(path) cxxinfo = no_comment_cxx(path=path, boutcxx="physics_code.cxx.ref") # evolved = get_evolved_cxx(cxxinfo) fieldkeys = get_evolved_cxx(cxxinfo) fieldkeys = ["[" + elem + "]" for elem in fieldkeys] except: print("cant find the cxx file") # gridoptions = {'grid':grid,'mesh':mesh} if "[mesh]" in list(inp.keys()): # IC = outinfo IC = read_grid(path + "/BOUT.dmp.0.nc") # output data again elif "grid" in inp["[main]"]: gridname = inp["[main]"]["grid"] try: IC = read_grid(gridname) # have to be an ansoulte file path for now print("IC: ", type(IC)) # print IC.variables # print gridname except: # print gridname print("Fail to load the grid file") # print IC # print gridname # print len(IC) # print IC evolved = [] collected = [] ICscale = [] # fieldkeys = ['[Ni]','[Te]','[Ti]','[Vi]','[rho]', # '[Ajpar]','[Apar]','[vEBx]','[vEBy]','[vEBz]', # '[jpar]','[phi]'] # construct fieldkeys from cxx info # fieldkeys = ['['+x+']' for x in evolved] # fieldkeys = evolved # just look ahead and see what 3D fields have been output available = np.array([str(x) for x in outinfo]) a = np.array([(len(outinfo[x].shape) == 4) for x in available]) available = available[a] defaultIC = float(inp["[All]"].get("scale", 0.0)) # print inp.keys() # figure out which fields are evolved print(fieldkeys) for section in list(inp.keys()): # loop over section keys print("section: ", section) if section in fieldkeys: # pick the relevant sections print(section) # print inp[section].get('evolve','True') # rint (inp[section].get('evolve','True')).lower().strip() if inp[section].get("evolve", "True").lower().strip() == "true": # and section[1:-1] in available : print("ok reading") evolved.append(section.strip("[]")) ICscale.append(float(inp[section].get("scale", defaultIC))) if inp[section].get("collect", "False").lower().strip() == "true": collected.append(section.strip("[]")) try: if inp["[physics]"].get("transport", "False").lower().strip() == "true": vEBstr = ["vEBx", "vEBy", "vEBz", "vEBrms"] [collected.append(item) for item in vEBstr] except: print("no [physics] key") meta = OrderedDict() class ValUnit(object): def __init__(self, value=0, units=""): self.u = units self.v = value def todict(self): return {"u": self.u, "v": self.v} # def decode_valunit(d): def ToFloat(metaString): try: return float(metaString) except ValueError: return metaString # meta['evolved'] = ValUnit(evolved,'') meta["evolved"] = evolved meta["collected"] = collected meta["IC"] = np.array(ICscale) d = {} print("evolved: ", evolved) # read meta data from .inp file, this is whre most metadata get written for section in list(inp.keys()): if ("evolve" not in inp[section]) and ( "first" not in inp[section] ): # hacky way to exclude some less relevant metadata for elem in list(inp[section].keys()): meta[elem] = ValUnit(ToFloat(inp[section][elem])) d[elem] = np.array(ToFloat(inp[section][elem])) # read in some values from the grid(IC) and scale them as needed norms = { "Ni0": ValUnit(1.0e14, "cm^-3"), "bmag": ValUnit(1.0e4, "gauss"), "Ni_x": ValUnit(1.0e14, "cm^-3"), "Te_x": ValUnit(1.0, "eV"), "Ti_x": ValUnit(1.0, "eV"), "Rxy": ValUnit(1, "m"), "Bxy": ValUnit(1.0e4, "gauss"), "Bpxy": ValUnit(1.0e4, "gauss"), "Btxy": ValUnit(1.0e4, "gauss"), "Zxy": ValUnit(1, "m"), "dlthe": ValUnit(1, "m"), "dx": ValUnit(1, "m"), "hthe0": ValUnit(1, "m"), } availkeys = np.array([str(x) for x in outinfo]) tmp1 = np.array([x for x in availkeys]) # b = np.array([x if x not in available for x in a]) tmp2 = np.array([x for x in tmp1 if x not in available]) static_fields = np.array([x for x in tmp2 if x in list(norms.keys())]) # static_fields = tmp2 # print availkeys # print meta.keys() # print IC.variables.keys() # print tmp1 # print tmp2 for elem in static_fields: print("elem: ", elem) meta[elem] = ValUnit(IC.variables[elem][:] * norms[elem].v, norms[elem].u) d[elem] = np.array(IC.variables[elem][:] * norms[elem].v) for elem in IC.variables: if elem not in meta: if elem in list(norms.keys()): meta[elem] = ValUnit(IC.variables[elem][:] * norms[elem].v, norms[elem].u) d[elem] = np.array(IC.variables[elem][:] * norms[elem].v) else: meta[elem] = IC.variables[elem][:] d[elem] = IC.variables[elem][:] # print d.keys() # if case some values are missing default = { "bmag": 1, "Ni_x": 1, "NOUT": 100, "TIMESTEP": 1, "MZ": 32, "AA": 1, "Zeff": ValUnit(1, ""), "ZZ": 1, "zlowpass": 0.0, "transport": False, } diff = set(default.keys()).difference(set(d.keys())) for elem in diff: # print 'diff: ',elem meta[elem] = default[elem] d[elem] = np.array(default[elem]) # print meta.keys() # print d.keys() # print meta['zlowpass'] if meta["zlowpass"] != 0: print(meta["MZ"].v, meta["zlowpass"].v) meta["maxZ"] = int(np.floor(meta["MZ"].v * meta["zlowpass"].v)) else: meta["maxZ"] = 5 # meta['nx'] = nx # meta['ny']= ny meta["dt"] = meta["TIMESTEP"] # nx,ny = d['Rxy'].shape # print meta['AA'].v meta["rho_s"] = ValUnit( 1.02e2 * np.sqrt(d["AA"] * d["Te_x"]) / (d["ZZ"] * d["bmag"]), "cm" ) # ion gyrorad at T_e, in cm meta["rho_i"] = ValUnit(1.02e2 * np.sqrt(d["AA"] * d["Ti_x"]) / (d["ZZ"] * d["bmag"]), "cm") meta["rho_e"] = ValUnit(2.38 * np.sqrt(d["Te_x"]) / (d["bmag"]), "cm") meta["fmei"] = ValUnit(1.0 / 1836.2 / d["AA"]) meta["lambda_ei"] = 24.0 - np.log(old_div(np.sqrt(d["Ni_x"]), d["Te_x"])) meta["lambda_ii"] = 23.0 - np.log(d["ZZ"] ** 3 * np.sqrt(2.0 * d["Ni_x"]) / (d["Ti_x"] ** 1.5)) # meta["wci"] = 1.0 * 9.58e3 * d["ZZ"] * d["bmag"] / d["AA"] # ion gyrofrteq meta["wpi"] = 1.32e3 * d["ZZ"] * np.sqrt(old_div(d["Ni_x"], d["AA"])) # ion plasma freq meta["wce"] = 1.78e7 * d["bmag"] # electron gyrofreq meta["wpe"] = 5.64e4 * np.sqrt(d["Ni_x"]) # electron plasma freq meta["v_the"] = 4.19e7 * np.sqrt(d["Te_x"]) # cm/s meta["v_thi"] = 9.79e5 * np.sqrt(old_div(d["Ti_x"], d["AA"])) # cm/s meta["c_s"] = 9.79e5 * np.sqrt(5.0 / 3.0 * d["ZZ"] * d["Te_x"] / d["AA"]) # meta["v_A"] = 2.18e11 * np.sqrt(old_div(1.0, (d["AA"] * d["Ni_x"]))) meta["nueix"] = 2.91e-6 * d["Ni_x"] * meta["lambda_ei"] / d["Te_x"] ** 1.5 # meta["nuiix"] = 4.78e-8 * d["ZZ"] ** 4.0 * d["Ni_x"] * meta["lambda_ii"] / d["Ti_x"] ** 1.5 / np.sqrt(d["AA"]) # meta["nu_hat"] = meta["Zeff"].v * meta["nueix"] / meta["wci"] meta["L_d"] = 7.43e2 * np.sqrt(old_div(d["Te_x"], d["Ni_x"])) meta["L_i_inrt"] = 2.28e7 * np.sqrt(old_div(d["AA"], d["Ni_x"])) / d["ZZ"] # ion inertial length in cm meta["L_e_inrt"] = 5.31e5 * np.sqrt(d["Ni_x"]) # elec inertial length in cm meta["Ve_x"] = 4.19e7 * d["Te_x"] meta["R0"] = old_div((d["Rxy"].max() + d["Rxy"].min()), 2.0) print(d["Rxy"].mean(1)) print(d["ZMAX"]) print(d["ZMIN"]) meta["L_z"] = 1e2 * 2 * np.pi * d["Rxy"].mean(1) * (d["ZMAX"] - d["ZMIN"]) # in cm toroidal range meta["dz"] = d["ZMAX"] - d["ZMIN"] # meta['lbNorm']=meta['L_z']*(d['Bpxy']/d['Bxy']).mean(1) #-binormal coord range [cm] meta["lbNorm"] = meta["L_z"] * (old_div(d["Bxy"], d["Bpxy"])).mean(1) # meta['zPerp']=np.array(meta['lbNorm']).mean*np.array(range(d['MZ']))/(d['MZ']-1) # let's calculate some profile properties dx = np.gradient(d["Rxy"])[0] meta["L"] = 1.0 * 1e2 * dx * (meta["Ni0"].v) / np.gradient(meta["Ni0"].v)[0] / meta["rho_s"].v meta["w_Ln"] = old_div(meta["c_s"], (np.min(abs(meta["L"])) * meta["wci"] * meta["rho_s"].v)) # normed to wci AA = meta["AA"].v ZZ = d["ZZ"] Te_x = d["Te_x"] Ti_x = d["Ti_x"] fmei = meta["fmei"].v meta["lpar"] = ( 1e2 * ((old_div(d["Bxy"], d["Bpxy"])) * d["dlthe"]).sum(1) / meta["rho_s"].v ) # -[normed], average over flux surfaces, parallel length # yes dlthe is always the vertical displacement # dlthe = (hthe0*2 pi)/nz # meta['lpar']=1e2*(d['Bxy']/d['Bpxy']).mean(1)*d['dlthe'].mean(1) #function of x meta["sig_par"] = old_div(1.0, (fmei * 0.51 * meta["nu_hat"])) # meta['heat_nue'] = ((2*np.pi/meta['lpar'])**2)/(fmei*meta['nu_hat']) # kz_e = kz_i*(rho_e/rho_i) # kz_s = kz_i*(rho_s/rho_i) # kz_i = (TWOPI/L_z)*(indgen((*current_str).fft.nz+1))*rho_i # knorm = (TWOPI/lbNorm)*(indgen((*current_str).fft.nz+1))*rho_s # for now just translate for elem in meta: if type(meta[elem]).__name__ == "ValUnit": meta[elem] = {"u": meta[elem].u, "v": meta[elem].v} print("meta: ", type(meta)) return meta