def parse_vars(lines): i=-1 i0=-1 i1=-1 while i<len(lines)-1: i+=1 if lines[i].lstrip().find('variables:')==0: i0=i+1 elif (lines[i].lstrip().find('// global attributes:')==0 or\ lines[i].lstrip().find('group:')==0 or\ lines[i].lstrip().find('} // group ')==0 or\ lines[i].strip()=='}') and i0>-1: i1=i break res=cbt.odict() for j in range(i0,i1): if lines[j].strip(): if lines[j].find('=')==-1 and lines[j].strip()[0]!='"': varname=lines[j][:-2].strip().split()[1].split('(')[0] res[varname]=cbt.odict() else: if lines[j].strip()[0]!='"': # multiline !! name=lines[j].split(':')[0].strip() vatt=lines[j].split(':')[1].split('=')[0].strip() res[name][vatt]={} return res
def parse_vars(lines): i = -1 i0 = -1 i1 = -1 while i < len(lines) - 1: i += 1 if lines[i].lstrip().find('variables:') == 0: i0 = i + 1 elif (lines[i].lstrip().find('// global attributes:')==0 or\ lines[i].lstrip().find('group:')==0 or\ lines[i].lstrip().find('} // group ')==0 or\ lines[i].strip()=='}') and i0>-1: i1 = i break res = cbt.odict() for j in range(i0, i1): if lines[j].strip(): if lines[j].find('=') == -1 and lines[j].strip()[0] != '"': varname = lines[j][:-2].strip().split()[1].split('(')[0] res[varname] = cbt.odict() else: if lines[j].strip()[0] != '"': # multiline !! name = lines[j].split(':')[0].strip() vatt = lines[j].split(':')[1].split('=')[0].strip() res[name][vatt] = {} return res
def ncdump_info(f): ''' List of dimensions, variables and attributes from ncdump Works with netcdf files version <= 4 ''' ncdump=cbt.search('ncdump') if ncdump: #try : out=cbt.run(ncdump+' -h '+f+' 2>/dev/null') #except: out=cbt.run(ncdump+' -h '+f) out=cbt.run(ncdump,'-h',f) else: return if not out: res=cbt.odict() res['groups'] = cbt.odict() res['name'] = f res['dimensions'] = cbt.odict() res['variables'] = cbt.odict() res['attributes'] = cbt.odict() else: res=parse_once(out,True) return res
def parse_once(lines, root=False): name = lines[0].split()[1] res = cbt.odict() res['groups'] = cbt.odict() res['name'] = name res['dimensions'] = parse_dims(lines) res['variables'] = parse_vars(lines) res['attributes'] = parse_atts(lines) j0 = False k = 0 for j in range(1, len(lines)): k += 1 l = lines[j] if l.lstrip().find('group:') == 0 and j0 is False: j0 = j name = l.split()[1] if l.strip() == '} // group ' + name and not j0 is False: res['groups'][name] = parse_once(lines[j0:j + 1]) j0 = False return res
def ncdump_info(f): ''' List of dimensions, variables and attributes from ncdump Works with netcdf files version <= 4 ''' ncdump = cbt.search('ncdump') if ncdump: #try : out=cbt.run(ncdump+' -h '+f+' 2>/dev/null') #except: out=cbt.run(ncdump+' -h '+f) out = cbt.run(ncdump, '-h', f) else: return if not out: res = cbt.odict() res['groups'] = cbt.odict() res['name'] = f res['dimensions'] = cbt.odict() res['variables'] = cbt.odict() res['attributes'] = cbt.odict() else: res = parse_once(out, True) return res
def parse_once(lines,root=False): name=lines[0].split()[1] res=cbt.odict() res['groups'] = cbt.odict() res['name'] = name res['dimensions'] = parse_dims(lines) res['variables'] = parse_vars(lines) res['attributes'] = parse_atts(lines) j0=False k=0 for j in range(1,len(lines)): k+=1 l=lines[j] if l.lstrip().find('group:')==0 and j0 is False: j0=j name=l.split()[1] if l.strip() == '} // group '+name and not j0 is False: res['groups'][name]=parse_once(lines[j0:j+1]) j0=False return res
def parse_atts(lines): i = -1 i0 = -1 i1 = -1 while i < len(lines) - 1: i += 1 if lines[i].lstrip().find('// global attributes:') == 0: i0 = i + 1 elif (lines[i].lstrip().find('// global attributes:')==0 or\ lines[i].lstrip().find('group:')==0 or\ lines[i].lstrip().find('} // group ')==0) and i0>-1: i1 = i break res = cbt.odict() for j in range(i0, i1): if lines[j].strip() and lines[j].strip()[0] == ':': s = lines[j] res[s[s.index(':') + 1:s.index('=')].strip()] = {} return res
def parse_atts(lines): i=-1 i0=-1 i1=-1 while i<len(lines)-1: i+=1 if lines[i].lstrip().find('// global attributes:')==0: i0=i+1 elif (lines[i].lstrip().find('// global attributes:')==0 or\ lines[i].lstrip().find('group:')==0 or\ lines[i].lstrip().find('} // group ')==0) and i0>-1: i1=i break res=cbt.odict() for j in range(i0,i1): if lines[j].strip() and lines[j].strip()[0]==':': s=lines[j] res[s[s.index(':')+1:s.index('=')].strip()]={} return res
def parse_dims(lines): i = -1 i0 = -1 i1 = -1 while i < len(lines) - 1: i += 1 if lines[i].lstrip().find('dimensions:') == 0: i0 = i + 1 elif (lines[i].lstrip().find('variables:')==0 or\ lines[i].lstrip().find('group:')==0 or\ lines[i].lstrip().find('} // group ')==0) and i0>-1: i1 = i break res = cbt.odict() for j in range(i0, i1): tmp = lines[j].split('=') dimname = tmp[0].strip() dimvalue = tmp[1][:-1].strip() res[dimname] = dimvalue return res
def parse_dims(lines): i=-1 i0=-1 i1=-1 while i<len(lines)-1: i+=1 if lines[i].lstrip().find('dimensions:')==0: i0=i+1 elif (lines[i].lstrip().find('variables:')==0 or\ lines[i].lstrip().find('group:')==0 or\ lines[i].lstrip().find('} // group ')==0) and i0>-1: i1=i break res=cbt.odict() for j in range(i0,i1): tmp=lines[j].split('=') dimname=tmp[0].strip() dimvalue=tmp[1][:-1].strip() res[dimname]=dimvalue return res