A few months ago I wanted to use the monthly average values of chlorophyll-a data available from the SeaWiFS project homepage as HDF4 files. Because I already have netCDF4 and HDF5 installed on my computer, I had problems installing HDF4 on the same computer. After a lot of struggling to get that to work, I downloaded HDFView instead. Using HDFView I exported each of the monthly climatology HDF4 files of chlorophyll-a to ASCII format. This left me with 12 ASCII files that I wanted to convert into one netCDF4 file that I could use with my individual-based model. I wrote a python program to facilitate that task (shown below) and I ended up with one netCDF4 file that contains monthly (January to December) climatology (1998-2008) values of SeaWiFS chlorophyll-a. The netCDF4 file is 178MB large and available here
Chlorophyll-a (format NetCDF4) (40)The program I used to create the file is available below:
import datetime as datetime
import string, sys, os
import pylab
import IOseawifs
__author__Â Â = 'Trond Kristiansen'
__created__Â = datetime.datetime(2009, 12, 3)
__modified__ = datetime.datetime(2009, 12, 19)
__version__Â = "0.5.0"
__status__Â Â = "Development"
__revisions__= "3.12.09, 16.12.2009, 19.12.2009"
"""
===================== readSeaWiFS.py ===========================================
This is a script to read SeaWiFS data from files (monthly mean files). These
data was downloaded from the SeaWiFS website as HDF4 files
(http://seadas.gsfc.nasa.gov/), and converted
(exported) to ASCII files using the application HDFview. This was the best
solution after having had problems installing HDF4 alongside HDF5.
The data are read as one big matrix using numpys loadtxt
(np.loadtxt(ascii-file)). One matrix is read for each monthly climatology
(Jan-Dec). Each matrix is stored into a large array called chlo which has the
shape (12,2160,4320). This matrix together with the latitude, longitude
arrays are written to netCDF4 file using IOseawifs.py.
The program plotSeaWiFS.py enables you to extract time series from a
given longitude/latitude position by interpolating the values stored in
the surrounding grid points read from this file.
Uses: IOseawifs.py (and is useful together with plotSeaWifs.py)
"""
def createGrid(minLon,maxLon,minLat,maxLat,delta):
lat=np.zeros((2160), dtype=np.float64)
lon=np.zeros((4320), dtype=np.float64)
k=0
for k in range(2160):
if k==0:
lat[k]=minLat + delta
else:
lat[k]=lat[k-1] + delta
k=0
for k in range(4320):
if k==0:
lon[k]=minLon + delta
else:
lon[k]=lon[k-1] + delta
latitude, longitude= np.meshgrid(lat,lon)
return longitude, latitude
def main(files):
minLat=-90.
maxLat= 90.
minLon=-180.
maxLon= 180.
delta = 180./2160.
"""A one time creation of the longitude/latitude grid"""
longitude, latitude = createGrid(minLon,maxLon,minLat,maxLat,delta)
index=longitude.shape
"""Create the matrix to save data to:"""
chlo = np.zeros((12,index[0],index[1]), dtype=np.float64)
months=[]; month=0
for file in files:
if os.path.exists(file):
print 'Opened file %s (month=%s)'%(file,month)
data=np.loadtxt(file)
months.append(month + 1)
data2=np.rot90(data)
chlo[month,:,:] = np.flipud(np.fliplr(data2[:,:]))
month+=1
else:
print "File %s does not exists"%(file)
sys.exit()
"""Now save the data for all months to a netcdf4 file"""
IOseawifs.writeSeawifsFile(chlo,latitude,longitude,months)
if __name__=="__main__":
try:
import psyco
psyco.log()
psyco.profile()
except ImportError:
pass
files=['l3m_January.txt','l3m_February.txt','l3m_March.txt','l3m_April.txt','l3m_May.txt','l3m_June.txt',
'l3m_July.txt','l3m_August.txt','l3m_September.txt','l3m_October.txt','l3m_November.txt','l3m_December.txt']
main(files)
from netCDF4 import num2date
import numpy as np
import time
import os
def writeSeawifsFile(data, latitude, longitude, months):
outputFile='chlo-seawifs.nc'
if os.path.exists(outputFile): os.remove(outputFile)
print 'Output file is %s'%(outputFile)
print 'months',months
f1 = Dataset(outputFile, mode='w', format='NETCDF4')
f1.description="This is a monthly average global SEAWIFS Chlorophyll-a file"
f1.history = 'Created ' + time.ctime(time.time())
f1.type='NetCDF4 classic created readSeaWifs.py'
f1.createDimension('time', len(months))
f1.createDimension('lat', 2160)
f1.createDimension('lon', 4320)
v_time = f1.createVariable('time', 'd', ('time',),zlib=True)
v_time.long_name = 'Month of the year'
v_time.units = 'month'
v_time.field = 'time, scalar, series'
v_time.calendar='standard'
v=f1.createVariable('latitude', 'd', ('lon','lat',),zlib=True)
v.long_name = "Matrix of latitude"
v.units = "degrees"
v=f1.createVariable('longitude', 'd', ('lon','lat',),zlib=True)
v.long_name = "Matrix of latitude"
v.units = "degrees"
v=f1.createVariable('Chlorophyll-a', 'f', ('time','lon','lat',),zlib=True)
v.long_name = "Monthly climatological chlorophyll-a from Seawifs"
v.units = "mg/m3"
f1.variables['time'][:]Â Â = months
f1.variables['Chlorophyll-a'][:,:,:]Â = data
f1.variables['latitude'][:,:]Â = latitude
f1.variables['longitude'][:,:]Â = longitude
f1.close()

Comments
Leave a comment Trackback