Created
April 30, 2014 15:04
-
-
Save jdherman/28d3ed93b19e0ccd6f14 to your computer and use it in GitHub Desktop.
Convert a full lat-lon NetCDF dataset to a "list" representation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
from netCDF4 import Dataset | |
import numpy as np | |
import os | |
import math | |
PATH = '.' | |
LL = np.loadtxt('%s/global_soils_default.txt' % PATH); | |
LL = LL[:,2:4] | |
OBS = np.loadtxt('%s/VIC_GRDC_Monthly_Climatology.txt' % PATH, delimiter=',', skiprows=1) | |
old_root= Dataset('vic_LHS_climatology.nc', 'r', format='NETCDF4') | |
old_vic_runoff = old_root.variables['vic_runoff'] | |
old_obs_runoff = old_root.variables['obs_runoff'] | |
# New file | |
root_grp = Dataset('vic_LHS_climatology_cells.nc', 'w', format='NETCDF4') | |
root_grp.description = '(Cells only) Results from VIC 10K Latin Hypercube ensemble, 60-year simulation on Blue Waters' | |
# dimensions | |
root_grp.createDimension('ncells', 15836) | |
root_grp.createDimension('month', 12) | |
ensemble = root_grp.createDimension('ensemble', 10000) | |
# variables | |
latitudes = root_grp.createVariable('latitude', 'f4', ('ncells',)) | |
longitudes = root_grp.createVariable('longitude', 'f4', ('ncells',)) | |
vic_runoff = root_grp.createVariable('vic_runoff', 'f4', ('ncells', 'ensemble', 'month',), fill_value=-9999.0) | |
obs_runoff = root_grp.createVariable('obs_runoff', 'f4', ('ncells', 'month'), fill_value=-9999.0) | |
vic_runoff.units = 'mm/month' | |
obs_runoff.units = 'mm/month' | |
cellsdone = 0 | |
latiter = np.arange(-90.5, 89.5, 1.0) | |
loniter = np.arange(0.5, 360.5, 1.0) | |
for lati, lat in enumerate(latiter): | |
for loni, lon in enumerate(loniter): | |
# grab the index of the 0-15836 list of grid cells | |
i = np.where((np.floor(LL[:,0]) == np.floor(lat)) & (np.floor(LL[:,1]) == np.floor(lon))) | |
# if this is one of our land surface grid cells... | |
if(np.size(i) > 0): | |
i = i[0] | |
print i | |
latitudes[i] = LL[i,0] | |
longitudes[i] = LL[i,1] | |
obs_runoff[i,:] = old_obs_runoff[lati,loni,:] | |
vic_runoff[i,:,:] = old_vic_runoff[lati,loni,:,:] | |
root_grp.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear Sir
I have had a difficult time trying to find an example script to write a netcdf file from real data(.csv) rather than random number generators.
I have tried Rnetcdf, r ncdf , ncdf4 and xarray but the example from your blog lools like the best. I would like to test it out with the data you used but can't find them.
Thanks
Dave Casciotti