GPCC analysis project

* Download the following data from the Global precipitation Climatology Center (GPCC)

ncols 720
nrows 360
xllcorner -180.0
yllcorner -90.0
cellsize 0.5
NODATA_value -99999.99
 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99 -99999.99

   1 #/////////////////////////////
   2 # PROGRAM to read GPCC data and store it as netCDF file
   3 
   4 from pylab import *
   5 import Nio
   6 
   7 close('all')
   8 def read_arcascii(fname):
   9     """
  10     reads ArcGIS like ASCII file and returns data
  11     and coordinate vectors
  12     
  13     f: filename
  14     """
  15     
  16     f=open(fname,'r')
  17     ncol=float(f.readline().split()[1])
  18     nrow=float(f.readline().split()[1])
  19     xll=float(f.readline().split()[1])
  20     yll=float(f.readline().split()[1])
  21     cellsize=float(f.readline().split()[1])
  22     nodata=float(f.readline().split()[1])
  23     
  24     d=f.read() #contains now a string
  25     d=d.split()
  26     
  27     #fill array (could be probably done more efficiently)
  28     o=zeros((nrow,ncol)); o[:]=nodata
  29     c=0
  30     for i in range(nrow):
  31         for j in range(ncol):
  32             o[i,j]=float(d[c])
  33             c += 1
  34     
  35     f.close()
  36     
  37     #/// coordinate vectors ///
  38     xvec=arange(ncol)*cellsize+xll+cellsize*0.5
  39     yvec=arange(nrow)*cellsize+yll+cellsize*0.5
  40     yvec=flipud(yvec)
  41     
  42     return xvec,yvec,o   
  43 
  44 
  45 #///////////////////////////////////////////////////
  46 
  47 fname='data.dat'
  48 lon,lat,D=read_arcascii(fname)
  49 D=D*12. #multiply by 12, as the GPCC data comes as mm/month --> get yearly sums
  50 
  51 #/// store the data in a netCDF file
  52 # implement your own code here!, the following code is not tested yet!
  53 # IT MIGHT THEREFORE BE BUGGY !!!
  54 
  55 oname=fname[:-4]+'.nc'
  56 F=Nio.open_file(oname,w)        #open netcdf file
  57 
  58 #create file dimensions
  59 F.create_dimensions('lat',len(lat))
  60 F.create_dimensions('lon',len(lon))
  61 
  62 #create variables
  63 F.create_variable('lat','d','lat')
  64 F.create_variable('lon','d','lon')
  65 F.create_variable('precip','d',('lat,lon'))
  66 
  67 #store data
  68 F.variables['lat']=lat
  69 F.variables['lat'].units='degree'
  70 F.variables['lon']=lon
  71 F.variables['lon'].units='degree'
  72 F.variables['precip'][:,:]=D
  73 F.variables['precip'].long_name = 'yearly precipitation sum'
  74 F.variables['precip'].units = 'mm/year'
  75 
  76 F.close()

MpiMetWiki: GpccAnalysis (last edited 2009-10-03 10:52:54 by AlexanderLoew)