In [1]:
import os
from netCDF4 import Dataset
import numpy as np
from osgeo import gdal
from osgeo import osr
from pylab import rcParams
%matplotlib inline
import matplotlib.pyplot as plt
In [2]:
print "hello"
In [7]:
grid = 'e2n_25'
#file = '/projects/PMESDR/vagrant/brodzik/' + grid + '/FD4a-E2T97-061-061.lis_dump.nc'
#file = '/projects/PMESDR/vagrant/brodzik/' + grid + '/FD4m-E2N97-061-061.lis_dump.nc'
file = '/projects/PMESDR/vagrant/brodzik/sirRSS/' + grid + '/FD4e-E2N97-061-061.lis_dump.nc'
f = Dataset( file, 'r', 'NETCDF4' )
In [9]:
# This should not be necessary, if the data are in the netCDF file properly
# Needs some investigation, could be FORTRAN-C-netCDF nonsense
tb = f.variables[ 'a_image' ][ : ]
cols, rows = shape( tb )
print cols, rows
tb = tb.reshape( rows, cols )
In [10]:
fig, ax = plt.subplots(1,1)
figsize(12,10)
ax.set_title( os.path.basename( file ) + ' (' + grid + ')' )
# Flip the image to display properly in python
plt.imshow(np.flipud(tb), cmap=plt.cm.gray, vmin=100, vmax=320 )
plt.axis('off')
#plt.colorbar( shrink=0.35, label='TB' )
plt.colorbar( shrink=0.95, label='TB' )
Out[10]:
In [11]:
outfile = file + '.new.nc'
driver = gdal.GetDriverByName('netCDF')
In [12]:
dst_ds = driver.Create(outfile, cols, rows, 1, gdal.GDT_UInt16)
In [13]:
proj = osr.SpatialReference()
# ease2_t:
# when we can connect to epsg v8.6, we should be able to do this: proj.SetWellKnownGeogCS("EPSG:6933")
#proj.SetFromUserInput("+proj=cea +lat_0=0 +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m")
# E2N
proj.SetFromUserInput("+proj=laea +lat_0=90 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m")
# E2S
#proj.SetFromUserInput("+proj=laea +lat_0=-90 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m")
dst_ds.SetProjection(proj.ExportToWkt())
Out[13]:
In [14]:
print proj.ExportToPrettyWkt()
In [15]:
# Thanks to web page at:
# http://geoexamples.blogspot.com/2012/01/creating-files-in-ogr-and-gdal-with.html
# The geotransform defines the relation between the raster coordinates x, y and the
# geographic coordinates, using the following definition:
# Xgeo = geotransform[0] + Xpixel*geotransform[1] + Yline*geotransform[2]
# Ygeo = geotransform[3] + Xpixel*geotransform[4] + Yline*geotransform[5]
# The first and fourth parameters define the origin of the upper left pixel
# The second and sixth parameters define the pixels size.
# The third and fifth parameters define the rotation of the raster.
# These values are for EASE2_T25km only, values are meters
#E2T_25
#scale = 25025.26000
#E2T_3
#scale = 3128.15750
#E2N_25 or E2S_25
scale_x = 25000.00000
scale_y = -25000.00000
#E2N_3 or E2S_3
#scale = 3125.00000
# E2T
#map_UL_x = -17367530.44
#map_UL_y = -6756820.20000
# E2N or E2S
map_UL_x = -9000000.
map_UL_y = 9000000.
geotransform = (map_UL_x,scale_x,0.,map_UL_y,0.,scale_y)
dst_ds.SetGeoTransform(geotransform)
Out[15]:
In [16]:
# Convert tb array to UInt16
print tb.dtype
new = (tb + 0.5).astype(uint16)
In [17]:
dst_ds.GetRasterBand(1).WriteArray(new)
Out[17]:
In [18]:
dst_ds = None
In [19]:
print outfile
In [20]:
print "Hello world"
In [34]:
In [ ]: