Advertisement
xdenisx

nc2txt

Oct 1st, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. from netCDF4 import Dataset
  2. import glob
  3. import os
  4.  
  5. #ffiles = '*.nc'
  6. ffiles = glob.glob('*.nc')
  7. ff0 = Dataset(ffiles[0])
  8. latt = ff0.variables['latitude'][:]
  9. lonn = ff0.variables['longitude'][:]
  10.  
  11. for xx in range(len(lonn[:,1])):
  12.         for yy in range(len(lonn[1,:])):
  13.             if lonn[xx,yy]>=45 and lonn[xx,yy]<=60 and latt[xx,yy]>=67 and latt[xx,yy]<=72:
  14.                 ff_txt=open('txt\\%.2f-%.2f.txt' % (latt[xx,yy],lonn[xx,yy]),'w')
  15.                 for ifile in ffiles:
  16.                     iifile = Dataset(ifile)
  17.                     u = iifile.variables['meridional_motion'][0,xx,yy]
  18.                     v = iifile.variables['zonal_motion'][0,xx,yy]
  19.                     if u<>32767 and u<>'':                      
  20.                         ff_txt.write('%s;%.0f;%.0f\n' % (ifile.split('.')[0],u,v))
  21.                     else:
  22.                         ff_txt.write('%s;999.;999.\n' % ifile.split('.')[0])
  23.                 try:
  24.                     ff_txt.close()
  25.                 except:
  26.                     pass
  27.  
  28.  
  29. txt_files = glob.glob('txt\\*.txt')
  30. for ifile in txt_files:
  31.     statinfo = os.stat(ifile)
  32.     if statinfo.st_size==0:
  33.         os.remove(ifile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement