import vcs import MV import Numeric import cdutil import genutil cdms.axis.latitude_aliases.append('Y') cdms.axis.longitude_aliases.append('X') cdms.axis.time_aliases.append('T') f=cdms.open('../example/data2.cdf') s=f('ssta',time=slice(0,120)) print s.shape SP=spanlib.SpAn(s) eof,pc,ev = SP.pca() print 'Done PCA, doing phases w/o mssa' print 'Reconstructing no phases' out = SP.reconstruct() ## phases = spanlib.phases(ffrec) x=vcs.init() for i in range(0,out.shape[0],out.shape[0]/10): x.plot(out[i]) raw_input('map out %i/%i ok?' % ( i+1 , out.shape[0])) x.clear()
cdms.axis.latitude_aliases.append('Y') cdms.axis.longitude_aliases.append('X') cdms.axis.time_aliases.append('T') f = cdms.open('../example/data2.cdf') s2 = f('ssta', latitude=(-10, 10), longitude=(110, 180)) s1 = f('ssta', latitude=(-15, 15), longitude=(210, 250)) print 'Data in:', s1.shape, s2.shape res = spanlib.stackData(s1, s2) print res[0].shape SP = spanlib.SpAn(MV.array(res[0]), weights=MV.array(res[1])) eof, pc, ev = SP.pca() ## for ax in res[3]: ## ax[0]=eof.getAxis(0) ## res2 = spanlib.unStackData(eof,res[1],res[2],res[3]) res3 = steof, stpc, stev = SP.mssa(pca=True) ffrec = SP.reconstruct() res4 = spanlib.unStackData(ffrec, res[1], res[2], res[3]) print ffrec import vcs
# Simply open the netcdf file print "Open file" f = cdms.open('data2.cdf') # Retrieve data print "Read the whole dataset" s = f('ssta') ############################################### # Use spanlib ############################################### # Create the analysis object print "Creating SpAn object" npca = 4 SP = spanlib.SpAn(s, npca=npca) # Perform a preliminary PCA+MSSA # (equivalent to simple use SP.mssa(pca=True) later) print "PCA..." SP.pca() eof = SP.pca_eof() pc = SP.pca_pc() ev = SP.pca_ev() f = cdms.open('out.nc', 'w') f.write(s, extend=False) f.write(eof) f.write(pc) f.write(ev) f.close()
time = cdms.createAxis(tt,id='time') time.units = 'months since 2000' time.designateTime() var = MV.array(var,copy=0,id='var') var.units = 'm' var.long_name = 'My variable' var.setAxisList([time,lon]) f=cdms.open('var.nc','w') f.write(var) f.close() nmode = 10 span = spanlib.SpAn(var,npca=nmode) eof = span.pca_eof() pc = span.pca_pc() ev = span.pca_ev() rec= span.pca_rec(imode=-2) print '+ ev[:3]:',ev[:3] #print '+ eof sums:',[(thiseof.filled()**2).sum() for thiseof in eof] print '+ max,min eof:',npy.max(eof[0]),npy.min(eof[0]) print '+ ev sum:',span.pca_ev(sum=True) print '+ eof[0, :3]:', eof[0, :3] f=cdms.open(__file__[:-2]+'nc','w') f.write(var,extend=False) f.write(eof)