Skip to content

Instantly share code, notes, and snippets.

@pwolfram
Created December 11, 2018 22:25
Show Gist options
  • Save pwolfram/836a398dc8f39bbdf2a93e8c324ac3fd to your computer and use it in GitHub Desktop.
Save pwolfram/836a398dc8f39bbdf2a93e8c324ac3fd to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import numpy as np
import scipy as sp
import xarray as xr
import netCDF4
forcing = xr.open_dataset('forcing.nc')
# build xtime
xtime = ['0001-01-01_{:02d}:00:00'.format(atime) for atime in np.arange(24)]
xtime = ['{:64s}'.format(astr) for astr in xtime]
# linear ramp scaling
scaling = np.linspace(0,1.0,len(xtime)).T
# build fields
def make_data_array(values, scaling, forcing):
adjusted = np.sign(values)*np.sqrt(scaling[np.newaxis,:].T*1e3*abs(values))
return adjusted
windSpeedU = make_data_array(forcing.windStressZonal.values, scaling, forcing)
windSpeedV = make_data_array(forcing.windStressMeridional.values, scaling, forcing)
ncds = netCDF4.Dataset('atmosphere_forcing.nc', 'w')
ncds.createDimension('nCells', len(forcing.nCells))
ncds.createDimension('StrLen', 64)
ncds.createDimension('Time', None)
time = ncds.createVariable('xtime','S1', ('Time', 'StrLen'))
time[:] = netCDF4.stringtochar(np.asarray(xtime))
time = ncds.dimensions['Time'].name
ncells = ncds.dimensions['nCells'].name
ncds.createVariable('windSpeedU', np.float64,(time, ncells))
ncds.createVariable('windSpeedV', np.float64,(time, ncells))
ncds.variables['windSpeedU'][:,:] = windSpeedU[:,:]
ncds.variables['windSpeedV'][:,:] = windSpeedV[:,:]
ncds.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment