def main(): import optparse global xmlplot parser = optparse.OptionParser() parser.add_option('-q','--quiet',action='store_false',dest='verbose',help='suppress progress messages') parser.set_defaults(verbose=True) (options, args) = parser.parse_args() pathout = args.pop(-1) minc,maxc,data,vars = None,None,[],[] for path in args: varname = os.path.splitext(os.path.basename(path))[0] dimname = 'dimension1' diminfo = {dimname:{'datatype':'datetime'}} vars.append((varname,varname,'')) # Create observations file mat = xmlplot.data.LinkedMatrix(dimensions=diminfo,dimensionorder=(dimname,),variables=[vars[-1]]) if options.verbose: print 'Reading %s...' % (path) mat.loadFromFile(path) curdata = mat.getData() data.append(curdata) curmin,curmax = curdata[0].min(),curdata[0].max() if minc is None or curmin>minc: minc = curmin if maxc is None or curmax<maxc: maxc = curmax if options.verbose: if diminfo[dimname]['datatype']=='datetime': if diminfo[dimname]['datatype']=='datetime': curmin,curmax = xmlplot.common.num2date(curmin),xmlplot.common.num2date(curmax) print '"%s" ranges between %s and %s.' % (varname,xmlstore.util.formatDateTime(curmin),xmlstore.util.formatDateTime(curmax)) else: print '"%s" ranges between %.8g and %.8g.' % (varname,curmin,curmax) if maxc<minc: print 'The data series from the different files do not overlap. Unable to combine them into a usable observation file.' sys.exit(1) coords = numpy.unique(numpy.concatenate([d[0] for d in data],0)) coords.sort() newdata = numpy.empty((coords.shape[0],sum([d[1].shape[1] for d in data])),numpy.float) pos = 0 for d in data: newdata[:,pos:pos+d[1].shape[1]] = xmlplot.common.interp1(d[0],d[1],coords) pos += d[1].shape[1] mat = xmlplot.data.LinkedMatrix(dimensions=diminfo,dimensionorder=(dimname,),variables=vars) mat.setData([coords,newdata]) if options.verbose: print 'Saving %s...' % (pathout,) mat.saveToFile(pathout) return 0
def extractncdata_parts(paths,*args,**kwargs): coords,data = [],[] basepath = kwargs.pop('basepath','') for i,path in enumerate(paths): res = extractncdata(os.path.join(basepath,path),*args,**kwargs) if res is None: return None c,d = res c.shape = (-1,) d.shape = (-1,) if coords: ilast = coords[-1].searchsorted(c[0]) print 'Overlap between files %s and %i is %i rows.' % (i-1,i,len(coords[-1])-ilast) coords[-1] = coords[-1][:ilast] data [-1] = data [-1][:ilast] coords.append(c) data.append(d) return numpy.concatenate(coords),numpy.concatenate(data)