def _open_file(self, fname): """ Returns netCDF file according to dataset parameters and file name. """ return netcdf('{}/{}'.format(self.params['path'], fname), 'r')
def read_topo_netcdf(self,fnam,subsamp=1): #Read a netcdf formatted topography file from scipy.io import netcdf_file as netcdf topo=netcdf(fnam,'r') x=topo.variables['x'][::subsamp] #vector y=topo.variables['y'][::subsamp] #vector z=topo.variables['z'][::subsamp,::subsamp] #matrix self.subsamp=subsamp self.set_data(x,y,z)
def write(self, t, dat): """Writes data to NetCDF file. PARAMETERS t (float) : Time. dat (dictionary) : Data to be saved. """ # Some parameters attribs = [ 'amip', 'grib', 'missing_value', 'canonical_units', 'description', 'standard_name' ] coordinates = dict(k='height', j='latitude', i='longitude') # Creates NetCDF file and sets its attributes. TM = dates.num2date(t) sname = 'tauxy%04d%02d%02d.nc' % (TM.year, TM.month, TM.day) f = netcdf('%s/%s' % (self.params['path'], sname), 'w') setattr(f, 'name', self.name) setattr(f, 'description', self.name) for attrib, attrib_value in self.attributes.items(): setattr(f, attrib, attrib_value) # Create a dimension and coordinates of the data. f.createDimension('n', 1) for dim, coord in coordinates.items(): if coord == 'n': f.createDimension(dim, 1) fvar = f.createVariable(coord, 'float', (1, )) fvar[:] = t else: f.createDimension(dim, self.dimensions[dim]) fvar = f.createVariable(coord, 'float', (dim, )) fvar[:] = self.variables[coord].data for attrib in attribs: try: setattr(fvar, attrib, getattr(self.variables[coord], attrib)) except: pass # Now saves the data for var, value in dat.items(): fvar = f.createVariable(var, 'float', ( 'n', 'k', 'j', 'i', )) for attrib in attribs: if getattr(self.variables[var], attrib) != None: setattr(fvar, attrib, getattr(self.variables[var], attrib)) Value = value.data.copy() Value[value.mask] = self.variables[var].missing_value fvar[:] = Value[None, None, :, :] # f.close()
def test_surface2netcdf(self): """ Should write a surface to a netcdf file. """ vm = readVM(get_example_file('cranis3d.vm')) surface2netcdf(vm, 0, 'test.grd') grd = netcdf('test.grd', 'r') z = grd.variables['z'][:] self.assertEqual((vm.ny, vm.nx), z.shape) self.assertAlmostEqual(vm.rf[0][0][0] * 1000., z[0][0], 4)
def read_file(self, filename): """Reads zipped NetCDF file and returns its file pointer.""" # # Uncompress NetCDF file. f = gzopen('%s' % (filename), 'rb') g = open('%s_%s.nc' % (self.params['uuid'], 'dump'), 'wb') g.write(f.read()) f.close() g.close() # return netcdf('%s_%s.nc' % (self.params['uuid'], 'dump'), 'r')
def read_topo_netcdf(self, fnam, subsamp=1): """ Read a netcdf formatted topography file """ from scipy.io import netcdf_file as netcdf topo = netcdf(fnam, "r") x = topo.variables["x"][::subsamp] # vector y = topo.variables["y"][::subsamp] # vector z = topo.variables["z"][::subsamp, ::subsamp] # matrix self.subsamp = subsamp self.set_data(x, y, z)
def interface2netcdf(vm, iref, filename, units="km", elevation=False): """ Write interface depths to a netcdf file. Parameters ---------- vm : :class:`rockfish.tomography.model.VM` VM model to extract an interface from. iref : int Index of interface to extract. filename: str Name of the netcdf file to write the interface to. units : 'str', optional Name of distance units in the output file. Must be a unit name understood by :class:`sympy.physics.units`. Distance units in the model are assumed to be 'km'. elevation : bool, optional If ``True``, flip the sign of interface depths. """ # Unit scaling scl = sunits.kilometers / sunits.__getattribute__(units) if elevation: zscl = scl * -1.0 else: zscl = scl # Open file f = netcdf(filename, "w") f.title = "Interface {:} from VM model.".format(iref) f.source = "Created by rockfish.tomography.vm2gmt.surface2netcdf" # x-dimension f.createDimension("x", vm.ny) x = f.createVariable("x", "f", ("x",)) x[:] = vm.y * scl x.units = units # y-dimension f.createDimension("y", vm.nx) y = f.createVariable("y", "f", ("y",)) y[:] = vm.x * scl y.units = units # depths (z) z = f.createVariable("z", "f", ("x", "y")) z[:, :] = vm.rf[iref].transpose() * zscl z.units = units z.scale_factor = 1.0 z.add_offset = 0.0 # depth range f.createDimension("side", 2) z_range = f.createVariable("z_range", "f", ("side",)) z_range[0] = np.min(vm.rf[0]) * zscl z_range[1] = np.min(vm.rf[1]) * zscl f.sync() f.close()
def write(self, t, dat): """Writes data to NetCDF file. PARAMETERS t (float) : Time. dat (dictionary) : Data to be saved. """ # Some parameters attribs = ['amip', 'grib', 'missing_value', 'canonical_units', 'description', 'standard_name'] coordinates = dict(k='height', j='latitude', i='longitude') # Creates NetCDF file and sets its attributes. TM = dates.num2date(t) sname = 'tauxy%04d%02d%02d.nc' % (TM.year, TM.month, TM.day) f = netcdf('%s/%s' % (self.params['path'], sname), 'w') setattr(f, 'name', self.name) setattr(f, 'description', self.name) for attrib, attrib_value in self.attributes.items(): setattr(f, attrib, attrib_value) # Create a dimension and coordinates of the data. f.createDimension('n', 1) for dim, coord in coordinates.items(): if coord == 'n': f.createDimension(dim, 1) fvar = f.createVariable(coord, 'float', (1, )) fvar[:] = t else: f.createDimension(dim, self.dimensions[dim]) fvar = f.createVariable(coord, 'float', (dim, )) fvar[:] = self.variables[coord].data for attrib in attribs: try: setattr(fvar, attrib, getattr(self.variables[coord], attrib)) except: pass # Now saves the data for var, value in dat.items(): fvar = f.createVariable(var, 'float', ('n', 'k', 'j', 'i', )) for attrib in attribs: if getattr(self.variables[var], attrib) != None: setattr(fvar, attrib, getattr(self.variables[var], attrib)) Value = value.data.copy() Value[value.mask] = self.variables[var].missing_value fvar[:] = Value[None, None, :, :] # f.close()
def plotBathymetry(rawDataPred,bathFile = 'ETOPO1_Bed_g_gmt4.grd'): bathData = netcdf(bathFile,'r') bx = bathData.variables['x'][::60] by = bathData.variables['y'][::60] bz = bathData.variables['z'][::60,::60] data = [] for i in range(len(rawDataPred['classif'])): xd = rawDataPred['lon'][i] yd = rawDataPred['lat'][i] for yi in itertools.ifilter(lambda m: abs(by[m] - yd) < 1e-5,range(len(by))): for xi in itertools.ifilter(lambda m: abs(bx[m] - xd) < 1e-5,range(len(bx))): if isOcean(xd,yd): if bz[yi][xi] < 0: data.append([rawDataPred['classif'][i],bz[yi][xi]]) print float(i)/len(rawDataPred['classif']) return data
def interface2grd(vm, iref, filename, **kwargs): """ Write surface depths to a GMT-compatible netcdf file. .. note:: In general, netcdf files produced by :meth:`surface2netcdf` are compatible with GMT. However, zmin and zmax are not set correctly, causing issues with other programs such as Fledermaus. :meth:`surface2grd` is a kludge that uses GMT's grd2xyz and then xyz2grd to create a fully GMT-compatible netcdf file. .. todo:: Replace with a fully functioning version of :meth:`surface2netcdf`. Parameters ---------- vm : :class:`rockfish.tomography.model.VM` VM model to extract an interface from. iref : int Index of interface to extract. filename: str Name of the grd file to write the interface to. kwargs : Arguments for :meth:`surface2netcdf`. """ ncdf = "temp.netcdf" interface2netcdf(vm, iref, ncdf, **kwargs) grd = netcdf(ncdf) y = grd.variables["x"][:] x = grd.variables["y"][:] region = "{:}/{:}/{:}/{:}".format(min(x), max(x), min(y), max(y)) spacing = "{:}/{:}".format(np.diff(x[0:2])[0], np.diff(y[0:2])[0]) tmp = "temp.xyz" gmt = "grd2xyz {:} > {:}\n".format(ncdf, tmp) gmt += "xyz2grd {:} -G{:} -R{:} -I{:}".format(tmp, filename, region, spacing) subprocess.call(gmt, shell=True) os.remove(tmp) os.remove(ncdf)
#Plot a netcdf topo file in python from numpy import * from scipy.io import netcdf_file as netcdf from matplotlib import pyplot as plt from misc_tools import * import scipy.interpolate fn = '/Users/aallam/gmt/100m_poop.grd' flon, flat = load_faults() topo = netcdf(fn, 'r') subsamp = 5 x = topo.variables['x'][::subsamp] #vector y = topo.variables['y'][::subsamp] #vector z = topo.variables['z'][::subsamp, ::subsamp] #matrix itopo = Interface() itopo.read_topo_netcdf(fn) gnn = scipy.interpolate.RectBivariateSpline(itopo.x, itopo.y, itopo.z.transpose()) qq = gnn.__call__(x, y) print 'poo' plt.figure(figsize=(6, 6)) plt.plot(flon, flat, 'k') plt.axis([-118, -115, 32, 35]) plt.pcolor(x, y, z) print 1 plt.figure(figsize=(6, 6)) plt.axis([-118, -115, 32, 35]) plt.pcolor(x, y, qq.transpose())
def read(self, t=None, z=None, y=None, x=None, N=None, K=None, J=None, I=None, var=None, nonan=True, result='full', profile=False, dummy=False): """Reads dataset. PARAMETERS t, z, y, x (array like, optional) : Sets the time, height, latitude and longitude for which the data will be read. N, K, J, I (array like, optional) : Sets the temporal, vertical, meridional and zonal indices for which the data will be read. var (string, optional) : Indicates which variable of the grid will be read. If the parameter is a list of variables, then the data will be returned as a list of arrays. nonan (boolean, optional) : If set to true (default) changes data values containing NaN to zero, preserving the mask. result (string, optional) : Determines wheter all time, height, latitude, longitude and data will be returned ('full', default), if temporal, vertical, meridional and zonal indices are returned instead ('indices'), or if only variable data is returned ('var only'). components (list, optional) : A list containing which components will be included in the calculation. Options are the seasonal cycle ('seasonal'), westward propagating planetary waves ('planetary'), eddy fields ('eddy') and noise ('noise'). profile (boolean, optional) : Sets whether the status is send to screen. dummy (boolean, optional) : If set to true, does not load data and returns the shape of the array that would have been returned. RETURNS t, z, y, x, dat (array like) : If 'result' is set to 'full', then all coordinates and data variables are returned. N, K, J, I, var (array like) : If 'result' is set to 'indices', then all indices and data variables are returned. dat (array like) : If 'result' is set to 'var only', then the data is returned. """ global DEBUG t1 = time() # Checks input variables for consistency. if (t != None) & (N != None): raise ValueError('Both time and temporal index were provided.') if (z != None) & (K != None): raise ValueError('Both height and vertical index were provided.') if (y != None) & (J != None): raise ValueError( 'Both latitude and meridional index were provided.') if (x != None) & (I != None): raise ValueError('Both latitude and zonal index were provided.') if var == None: var = self.params['var_list'] # Checks for variables indices. Intersects desired input values with # dataset dimesion data. In this dataset, since only surface data is # available, the height values are always zero. if t != None: N = flatnonzero(in1d(self.variables['time'].data, t)) elif N == None: N = arange(self.dimensions['n']) if z != None: K = [0] elif K == None: K = [0] elif K != None: K = [0] if y != None: J = flatnonzero(in1d(self.variables['latitude'].data, y)) elif J == None: J = arange(self.dimensions['j']) if x != None: I = flatnonzero(in1d(self.variables['longitude'].data, y)) elif I == None: I = arange(self.dimensions['i']) # Sets the shape of the data array. shape = (len(N), 1, len(J), len(I)) if dummy: return shape # Selects data according to indices. t = self.variables['time'].data[N] z = self.variables['height'].data y = self.variables['latitude'].data[J] x = self.variables['longitude'].data[I] xx, yy = meshgrid(x, y) II, JJ = meshgrid(I, J) # Ressets variables Var = dict() if ('taux' in var) & ('tauy' in var): tauxy = True else: tauxy = False for item in var: if (item == 'taux') & tauxy: Var['tauxy'] = ma.zeros(shape, dtype=complex) elif (item == 'tauy') & tauxy: continue else: Var[item] = ma.zeros(shape) # Walks through every time index and loads data range from maps. for n, T in enumerate(t): t2 = time() if profile: s = '\rLoading data... %s ' % (profiler(shape[0], n + 1, 0, t1, t2),) stdout.write(s) stdout.flush() # Reads NetCDF file data = netcdf('%s/%s' % (self.params['path'], self.params['file_list'][N[n]]), 'r') for item in var: if (('lon_i' in self.params.keys()) & ('lat_j' in self.params.keys())): P = data.variables[item].data[0, 0, self.params['lat_j'], self.params['lon_i']][JJ, II] else: P = data.variables[item].data[0, 0, JJ, II] P[P <= self.variables[item].missing_value] = nan P = ma.masked_where(isnan(P), P) if nonan: P.data[P.mask] = 0 # if (item == 'taux') & tauxy: Var['tauxy'][n, 0, :, :] += P[:, :] elif (item == 'tauy') & tauxy: Var['tauxy'][n, 0, :, :] += 1j * P[:, :] else: Var[item][n, 0, :, :] += P[:, :] data.close() # If result dictionary contains only one item, return only the value # of this item. if len(Var.keys()) == 1: Var = Var[Var.keys()[0]] if profile: stdout.write('\r\n') stdout.flush() if DEBUG: print 't: ', t print 'z: ', z print 'y:', y print 'x:', x print 'var: ', Var print 'N: ', N print 'K: ', K print 'J: ', J print 'I:', I print 'shape: ', shape if result == 'full': return t, z, y, x, Var elif result == 'indices': return N, K, J, I, Var elif result == 'var only': return Var else: raise Warning("Result parameter set imporperly to '%s', " "assuming 'var only'." % (result)) return Var
def open_file(self): """Opens to netCDF file according to dataset parameters.""" return netcdf('%s/%s' % (self.params['path'], self.params['fname']), 'r')
def __init__(self, path=None, mask_file=None, xlim=None, ylim=None): # Initializes the variables to default values. The indices 'n', 'k', # 'j' and 'i' refer to the temporal, height, meridional and zonal # coordinates respectively. If one of these indexes is set to 'None', # then it is assumed infinite size, which is relevant for the 'time' # coordinate. self.attributes = dict() self.dimensions = dict(n=0, k=0, j=0, i=0) self.coordinates = dict(n=None, k=None, j=None, i=None) self.variables = dict() self.params = dict() self.stencil_coeffs = dict() self.stencil_params = dict() # Sets global parameters for grid. if path == None: path = ('/home/sebastian/academia/data/ncdc.noaa/seawinds/stress/' 'daily') self.params['path'] = path self.params['mask_file'] = mask_file self.params['missing_value'] = -9999. # Generates list of files, tries to match them to the pattern and to # extract the time. file_pattern = 'tauxy([0-9]{8}).nc' flist = listdir(self.params['path']) flist, match = reglist(flist, file_pattern) self.params['file_list'] = flist if len(flist) == 0: return # Convert dates to matplotlib format, i.e. days since 0001-01-01 UTC. time_list = array([dates.datestr2num(item) for item in match]) # Reads first file in dataset to determine array geometry and # dimenstions (lon, lat) data = netcdf('%s/%s' % (self.params['path'], self.params['file_list'][0]), 'r') for var in data.variables.keys(): if var in ['latitude', 'lat']: lat = data.variables[var].data elif var in ['longitude', 'lon']: lon = data.variables[var].data # If xlim and ylim are set, calculate how many indices have to be moved # in order for latitude array to start at xlim[0]. if (xlim != None) | (ylim != None): if xlim == None: xlim = (lon.min(), lon.max()) if ylim == None: ylim = (lat.min(), lat.max()) # LON = lon_n(lon, xlim[1]) i = argsort(LON) selx = i[flatnonzero((LON[i] >= xlim[0]) & (LON[i] <= xlim[1]))] sely = flatnonzero((lat >= ylim[0]) & (lat <= ylim[1])) ii, jj = meshgrid(selx, sely) lon = LON[selx] lat = lat[sely] self.params['xlim'] = xlim self.params['ylim'] = ylim self.params['lon_i'] = ii self.params['lat_j'] = jj self.params['dlon'] = lon[1] - lon[0] self.params['dlat'] = lat[1] - lat[0] # Initializes the grid attributes, dimensions, coordinates and # variables. self.name = 'sea_surface_wind_stress' for attr, attr_value in vars(data).iteritems(): if attr in ['mode', 'filename']: continue if type(attr_value) == str: if attr in ['name']: self.name = attr_value elif attr in ['description', 'summary']: self.description = attr_value else: self.attributes[attr.lower()] = attr_value self.dimensions = dict(n=time_list.size, k=1, j=lat.size, i=lon.size) self.coordinates = dict(n='time', k='height', j='latitude', i='longitude') # self.variables = dict( time = atlantis.data.variable( canonical_units='days since 0001-01-01 UTC', data=time_list, ), height = atlantis.data.get_standard_variable('height', data=[0.]), latitude = atlantis.data.get_standard_variable('latitude', data=lat), longitude = atlantis.data.get_standard_variable('longitude', data=lon), xm = atlantis.data.variable( canonical_units = 'km', description = 'Zonal distance.' ), ym = atlantis.data.variable( canonical_units = 'km', description = 'Meridional distance.' ), ) # self.variables['xm'].data, self.variables['ym'].data = ( metergrid(self.variables['longitude'].data, self.variables['latitude'].data, unit='km') ) # self.params['var_list'] = list() for var in data.variables.keys(): if var in ['tau', 'taux', 'tauy', 'tau_div', 'tau_curl']: attribs = dict() for attr, attr_value in vars(data.variables[var]).iteritems(): if attr == '_FillValue': attribs['missing_value'] = attr_value elif attr == 'data': continue elif attr == 'long_name': attribs['description'] = attr_value elif attr == 'units': if attr_value == 'N/m**2': a = 'N m-2' else: a = attr_value attribs['canonical_units'] = a else: attribs[attr] = attr_value self.variables[var] = atlantis.data.variable(**attribs) self.params['var_list'].append(var) if self.variables[var].missing_value == None: self.variables[var].missing_value = ( self.params['missing_value']) # data.close() return
def __init__(self, path=None, mask_file=None, xlim=None, ylim=None): # Initializes the variables to default values. The indices 'n', 'k', # 'j' and 'i' refer to the temporal, height, meridional and zonal # coordinates respectively. If one of these indexes is set to 'None', # then it is assumed infinite size, which is relevant for the 'time' # coordinate. self.attributes = dict() self.dimensions = dict(n=0, k=0, j=0, i=0) self.coordinates = dict(n=None, k=None, j=None, i=None) self.variables = dict() self.params = dict() self.stencil_coeffs = dict() self.stencil_params = dict() # Sets global parameters for grid. if path == None: path = ('/home/sebastian/academia/data/ncdc.noaa/seawinds/stress/' 'daily') self.params['path'] = path self.params['mask_file'] = mask_file self.params['missing_value'] = -9999. # Generates list of files, tries to match them to the pattern and to # extract the time. file_pattern = 'tauxy([0-9]{8}).nc' flist = listdir(self.params['path']) flist, match = reglist(flist, file_pattern) self.params['file_list'] = flist if len(flist) == 0: return # Convert dates to matplotlib format, i.e. days since 0001-01-01 UTC. time_list = array([dates.datestr2num(item) for item in match]) # Reads first file in dataset to determine array geometry and # dimenstions (lon, lat) data = netcdf( '%s/%s' % (self.params['path'], self.params['file_list'][0]), 'r') for var in data.variables.keys(): if var in ['latitude', 'lat']: lat = data.variables[var].data elif var in ['longitude', 'lon']: lon = data.variables[var].data # If xlim and ylim are set, calculate how many indices have to be moved # in order for latitude array to start at xlim[0]. if (xlim != None) | (ylim != None): if xlim == None: xlim = (lon.min(), lon.max()) if ylim == None: ylim = (lat.min(), lat.max()) # LON = lon_n(lon, xlim[1]) i = argsort(LON) selx = i[flatnonzero((LON[i] >= xlim[0]) & (LON[i] <= xlim[1]))] sely = flatnonzero((lat >= ylim[0]) & (lat <= ylim[1])) ii, jj = meshgrid(selx, sely) lon = LON[selx] lat = lat[sely] self.params['xlim'] = xlim self.params['ylim'] = ylim self.params['lon_i'] = ii self.params['lat_j'] = jj self.params['dlon'] = lon[1] - lon[0] self.params['dlat'] = lat[1] - lat[0] # Initializes the grid attributes, dimensions, coordinates and # variables. self.name = 'sea_surface_wind_stress' for attr, attr_value in vars(data).iteritems(): if attr in ['mode', 'filename']: continue if type(attr_value) == str: if attr in ['name']: self.name = attr_value elif attr in ['description', 'summary']: self.description = attr_value else: self.attributes[attr.lower()] = attr_value self.dimensions = dict(n=time_list.size, k=1, j=lat.size, i=lon.size) self.coordinates = dict(n='time', k='height', j='latitude', i='longitude') # self.variables = dict( time=atlantis.data.variable( canonical_units='days since 0001-01-01 UTC', data=time_list, ), height=atlantis.data.get_standard_variable('height', data=[0.]), latitude=atlantis.data.get_standard_variable('latitude', data=lat), longitude=atlantis.data.get_standard_variable('longitude', data=lon), xm=atlantis.data.variable(canonical_units='km', description='Zonal distance.'), ym=atlantis.data.variable(canonical_units='km', description='Meridional distance.'), ) # self.variables['xm'].data, self.variables['ym'].data = (metergrid( self.variables['longitude'].data, self.variables['latitude'].data, unit='km')) # self.params['var_list'] = list() for var in data.variables.keys(): if var in ['tau', 'taux', 'tauy', 'tau_div', 'tau_curl']: attribs = dict() for attr, attr_value in vars(data.variables[var]).iteritems(): if attr == '_FillValue': attribs['missing_value'] = attr_value elif attr == 'data': continue elif attr == 'long_name': attribs['description'] = attr_value elif attr == 'units': if attr_value == 'N/m**2': a = 'N m-2' else: a = attr_value attribs['canonical_units'] = a else: attribs[attr] = attr_value self.variables[var] = atlantis.data.variable(**attribs) self.params['var_list'].append(var) if self.variables[var].missing_value == None: self.variables[var].missing_value = ( self.params['missing_value']) # data.close() return
mz[numpy.isnan(mz)] = 0 mask = (mz == 0) # Loads phase speed of first-mode baroclinic gravity waves and Rossby radius # of deformation as of Chelton et al. (1998). fname = 'rossrad.dat' lat, lon, c, Ro = numpy.loadtxt(fname, unpack=True) lon = klib.common.lon_n(lon, x.max()) # Saves data into temporary file and interpolates it using GMT (Smith & Wessel) numpy.savetxt('dump_%d.xyz' % (nproc), numpy.array([lon, lat, Ro]).T) ret = subprocess.call(['./%s' % (icall), '%s' % nproc], stdout=subprocess.PIPE, stderr=subprocess.PIPE) data = netcdf('%s_%d.grd' % ('output', nproc), 'r') x = data.variables['x'].data y = data.variables['y'].data z = data.variables['z'].data z = numpy.ma.masked_where(mask, z) zx_deg = z / xm zy_deg = z / (ym[1:] - ym[:-1]).mean() fname = 'rossrad_25.xy' klib.file.save_map(x, y, z, '%s/%s.gz' % ('./', fname)) # Cleaning temporary files os.remove('./dump_%d.xyz' % (nproc)) os.remove('./block_%d.xyz' % (nproc)) os.remove('./output_%d.grd' % (nproc))
def read(self, t=None, z=None, y=None, x=None, N=None, K=None, J=None, I=None, var=None, nonan=True, result='full', profile=False, dummy=False): """Reads dataset. PARAMETERS t, z, y, x (array like, optional) : Sets the time, height, latitude and longitude for which the data will be read. N, K, J, I (array like, optional) : Sets the temporal, vertical, meridional and zonal indices for which the data will be read. var (string, optional) : Indicates which variable of the grid will be read. If the parameter is a list of variables, then the data will be returned as a list of arrays. nonan (boolean, optional) : If set to true (default) changes data values containing NaN to zero, preserving the mask. result (string, optional) : Determines wheter all time, height, latitude, longitude and data will be returned ('full', default), if temporal, vertical, meridional and zonal indices are returned instead ('indices'), or if only variable data is returned ('var only'). components (list, optional) : A list containing which components will be included in the calculation. Options are the seasonal cycle ('seasonal'), westward propagating planetary waves ('planetary'), eddy fields ('eddy') and noise ('noise'). profile (boolean, optional) : Sets whether the status is send to screen. dummy (boolean, optional) : If set to true, does not load data and returns the shape of the array that would have been returned. RETURNS t, z, y, x, dat (array like) : If 'result' is set to 'full', then all coordinates and data variables are returned. N, K, J, I, var (array like) : If 'result' is set to 'indices', then all indices and data variables are returned. dat (array like) : If 'result' is set to 'var only', then the data is returned. """ global DEBUG t1 = time() # Checks input variables for consistency. if (t != None) & (N != None): raise ValueError('Both time and temporal index were provided.') if (z != None) & (K != None): raise ValueError('Both height and vertical index were provided.') if (y != None) & (J != None): raise ValueError( 'Both latitude and meridional index were provided.') if (x != None) & (I != None): raise ValueError('Both latitude and zonal index were provided.') if var == None: var = self.params['var_list'] # Checks for variables indices. Intersects desired input values with # dataset dimesion data. In this dataset, since only surface data is # available, the height values are always zero. if t != None: N = flatnonzero(in1d(self.variables['time'].data, t)) elif N == None: N = arange(self.dimensions['n']) if z != None: K = [0] elif K == None: K = [0] elif K != None: K = [0] if y != None: J = flatnonzero(in1d(self.variables['latitude'].data, y)) elif J == None: J = arange(self.dimensions['j']) if x != None: I = flatnonzero(in1d(self.variables['longitude'].data, y)) elif I == None: I = arange(self.dimensions['i']) # Sets the shape of the data array. shape = (len(N), 1, len(J), len(I)) if dummy: return shape # Selects data according to indices. t = self.variables['time'].data[N] z = self.variables['height'].data y = self.variables['latitude'].data[J] x = self.variables['longitude'].data[I] xx, yy = meshgrid(x, y) II, JJ = meshgrid(I, J) # Ressets variables Var = dict() if ('taux' in var) & ('tauy' in var): tauxy = True else: tauxy = False for item in var: if (item == 'taux') & tauxy: Var['tauxy'] = ma.zeros(shape, dtype=complex) elif (item == 'tauy') & tauxy: continue else: Var[item] = ma.zeros(shape) # Walks through every time index and loads data range from maps. for n, T in enumerate(t): t2 = time() if profile: s = '\rLoading data... %s ' % (profiler( shape[0], n + 1, 0, t1, t2), ) stdout.write(s) stdout.flush() # Reads NetCDF file data = netcdf( '%s/%s' % (self.params['path'], self.params['file_list'][N[n]]), 'r') for item in var: if (('lon_i' in self.params.keys()) & ('lat_j' in self.params.keys())): P = data.variables[item].data[0, 0, self.params['lat_j'], self.params['lon_i']][JJ, II] else: P = data.variables[item].data[0, 0, JJ, II] P[P <= self.variables[item].missing_value] = nan P = ma.masked_where(isnan(P), P) if nonan: P.data[P.mask] = 0 # if (item == 'taux') & tauxy: Var['tauxy'][n, 0, :, :] += P[:, :] elif (item == 'tauy') & tauxy: Var['tauxy'][n, 0, :, :] += 1j * P[:, :] else: Var[item][n, 0, :, :] += P[:, :] data.close() # If result dictionary contains only one item, return only the value # of this item. if len(Var.keys()) == 1: Var = Var[Var.keys()[0]] if profile: stdout.write('\r\n') stdout.flush() if DEBUG: print 't: ', t print 'z: ', z print 'y:', y print 'x:', x print 'var: ', Var print 'N: ', N print 'K: ', K print 'J: ', J print 'I:', I print 'shape: ', shape if result == 'full': return t, z, y, x, Var elif result == 'indices': return N, K, J, I, Var elif result == 'var only': return Var else: raise Warning("Result parameter set imporperly to '%s', " "assuming 'var only'." % (result)) return Var
xm, ym = metergrid([1.0], ey, unit="km") mz[numpy.isnan(mz)] = 0 mask = mz == 0 # Loads phase speed of first-mode baroclinic gravity waves and Rossby radius # of deformation as of Chelton et al. (1998). fname = "rossrad.dat" lat, lon, c, Ro = numpy.loadtxt(fname, unpack=True) lon = klib.common.lon_n(lon, x.max()) # Saves data into temporary file and interpolates it using GMT (Smith & Wessel) numpy.savetxt("dump_%d.xyz" % (nproc), numpy.array([lon, lat, Ro]).T) ret = subprocess.call(["./%s" % (icall), "%s" % nproc], stdout=subprocess.PIPE, stderr=subprocess.PIPE) data = netcdf("%s_%d.grd" % ("output", nproc), "r") x = data.variables["x"].data y = data.variables["y"].data z = data.variables["z"].data z = numpy.ma.masked_where(mask, z) zx_deg = z / xm zy_deg = z / (ym[1:] - ym[:-1]).mean() fname = "rossrad_25.xy" klib.file.save_map(x, y, z, "%s/%s.gz" % ("./", fname)) # Cleaning temporary files os.remove("./dump_%d.xyz" % (nproc)) os.remove("./block_%d.xyz" % (nproc)) os.remove("./output_%d.grd" % (nproc))
print now(), 'frame_change_pygplates: Processing user input:' # Get the recon time reconstruction_time = int(sys.argv[1]) print now( ), 'frame_change_pygplates: reconstruction_time = ', reconstruction_time # Get the input grid input_grid_file = sys.argv[2] print now(), 'frame_change_pygplates: input_grid_file = ', input_grid_file # Get the grid -R input_grid_R = sys.argv[3] input_grid = netcdf(input_grid_file, 'r') print now( ), 'frame_change_pygplates: input_grid: variables', input_grid.variables # set output filenames output_xy_name = 'prefix-plate_frame.xy' # check on filenames if input_grid_file.endswith('.nc'): output_xy_name = input_grid_file.replace('.nc', '-plateframe.xy') elif input_grid_file.endswith('.grd'): output_xy_name = input_grid_file.replace('.grd', '-plateframe.xy') # write the output data file with open(output_xy_name, 'w') as output_xy_file:
#Plot a netcdf topo file in python from numpy import * from scipy.io import netcdf_file as netcdf from matplotlib import pyplot as plt from misc_tools import * import scipy.interpolate fn='/Users/aallam/gmt/100m_poop.grd' flon,flat=load_faults() topo=netcdf(fn,'r') subsamp=5 x=topo.variables['x'][::subsamp] #vector y=topo.variables['y'][::subsamp] #vector z=topo.variables['z'][::subsamp,::subsamp] #matrix itopo=Interface() itopo.read_topo_netcdf(fn) gnn=scipy.interpolate.RectBivariateSpline(itopo.x,itopo.y,itopo.z.transpose()) qq=gnn.__call__(x,y) print 'poo' plt.figure(figsize=(6,6)) plt.plot(flon,flat,'k') plt.axis([-118,-115,32,35]) plt.pcolor(x,y,z) print 1 plt.figure(figsize=(6,6)) plt.axis([-118,-115,32,35]) plt.pcolor(x,y,qq.transpose() )
def move_seafloor(home,project_name,run_name,model_name,topo_file,topo_dx_file,topo_dy_file, tgf_file,fault_name,outname,time_epi,tsun_dt,maxt,ymb,dl=2./60,variance=None,static=False): ''' Create moving topography input files for geoclaw ''' import datetime from numpy import genfromtxt,zeros,arange,meshgrid,ones,c_,savetxt,delete from obspy import read from string import rjust from scipy.io import netcdf_file as netcdf from scipy.interpolate import griddata from mudpy.inverse import interp_and_resample,grd2xyz from scipy.ndimage.filters import gaussian_filter #Staright line coordinates m=ymb[0] b=ymb[1] #Get station names sta=genfromtxt(home+project_name+'/data/station_info/'+tgf_file) lon=sta[:,1] lat=sta[:,2] loni=arange(lon.min(),lon.max()+dl,dl) #Fot grid interpolation lati=arange(lat.min(),lat.max()+dl,dl) loni,lati=meshgrid(loni,lati) #Get fault file f=genfromtxt(home+project_name+'/data/model_info/'+fault_name) #Where is the data data_dir=home+project_name+'/output/forward_models/' #Define time deltas td_max=datetime.timedelta(seconds=maxt) td=datetime.timedelta(seconds=tsun_dt) #Maximum tiem to be modeled tmax=time_epi+td_max Nt=(tmax-time_epi)/tsun_dt #Read derivatives bathy_dx=netcdf(topo_dx_file,'r') zdx=bathy_dx.variables['z'][:] bathy_dy=netcdf(topo_dy_file,'r') zdy=bathy_dy.variables['z'][:] #Read slope file kwrite=0 idelete=[] for ksta in range(len(sta)): if ksta%500==0: print '... ... working on seafloor grid point '+str(ksta)+' of '+str(len(sta)) try: #If no data then delete if static==False: #We're reading waveforms e=read(data_dir+run_name+'.'+rjust(str(int(sta[ksta,0])),4,'0')+'.disp.e') n=read(data_dir+run_name+'.'+rjust(str(int(sta[ksta,0])),4,'0')+'.disp.n') u=read(data_dir+run_name+'.'+rjust(str(int(sta[ksta,0])),4,'0')+'.disp.z') e=interp_and_resample(e,1.0,time_epi) n=interp_and_resample(n,1.0,time_epi) u=interp_and_resample(u,1.0,time_epi) #Keep only data between time_epi and tmax e.trim(time_epi,tmax,fill_value=e[0].data[-1],pad=True) n.trim(time_epi,tmax,fill_value=n[0].data[-1],pad=True) u.trim(time_epi,tmax,fill_value=u[0].data[-1],pad=True) #Decimate to original smapling interval #eds[0].decimate(4,no_filter=True) #nds[0].decimate(4,no_filter=True) #uds[0].decimate(4,no_filter=True) #Initalize matrices if ksta==0: emat=zeros((n[0].stats.npts,len(sta))) nmat=emat.copy() umat=emat.copy() #Populate matrix emat[:,kwrite]=e[0].data nmat[:,kwrite]=n[0].data umat[:,kwrite]=u[0].data else: neu=genfromtxt(data_dir+rjust(str(int(sta[ksta,0])),4,'0')+'.static.neu') n=neu[0] e=neu[1] u=neu[2] tsun_dt=1.0 maxt=1.0 if ksta==0: emat=zeros((1,len(sta))) nmat=emat.copy() umat=emat.copy() #Populate matrix emat[:,kwrite]=e nmat[:,kwrite]=n umat[:,kwrite]=u kwrite+=1 except: #Data was missing, delete from lat,lon print 'No data for station '+str(ksta)+', deleting from coordinates list' idelete.append(ksta) #Clean up missing data if len(idelete)!=0: lat=delete(lat,idelete) lon=delete(lon,idelete) emat=emat[:,:-len(idelete)] nmat=nmat[:,:-len(idelete)] umat=umat[:,:-len(idelete)] #Now go one epoch at a time, and interpolate all fields #Get mask for applying horizontal effect mask=zeros(loni.shape) for k1 in range(loni.shape[0]): for k2 in range(loni.shape[1]): if (lati[k1,k2]-b)/m>loni[k1,k2]: #Point is to the left, do not apply horizontal effect mask[k1,k2]=NaN imask1,imask2=where(mask==0)#Points tot he right DO apply horiz. effect print '... interpolating coseismic offsets to a regular grid' nt_iter=umat.shape[0] for kt in range(nt_iter): if kt%20==0: print '... ... working on time slice '+str(kt)+' of '+str(nt_iter) ninterp=griddata((lon,lat),nmat[kt,:],(loni,lati),method='cubic') einterp=griddata((lon,lat),emat[kt,:],(loni,lati),method='cubic') uinterp=griddata((lon,lat),umat[kt,:],(loni,lati),method='cubic') #Output vertical uout=uinterp #Apply effect of topography advection #uout[imask1,imask2]=uout[imask1,imask2]+zdx[imask1,imask2]*einterp[imask1,imask2]+zdy[imask1,imask2]*ninterp[imask1,imask2] uout=uout+zdx*einterp+zdy*ninterp #print 'no horiz' #Filter? if variance!=None: uout=gaussian_filter(uout,variance) #Convert to column format and append xyz=grd2xyz(uout,loni,lati) tvec=(kt*tsun_dt)*ones((len(xyz),1)) if kt==0: #Intialize numel=uout.size #Number of elements in grid kwrite=numel #Where to write the data dtopo=zeros((numel*nt_iter,4)) dtopo[0:kwrite,1:3]=xyz[:,0:2] else: dtopo[kwrite:kwrite+numel,:]=c_[tvec,xyz] kwrite=kwrite+numel if static==True: tvec=ones(tvec.shape) numel=uout.size*2 #Number of elements in grid kwrite=numel/2 #Where to write the data dtopo=zeros((numel*nt_iter,4)) dtopo[0:kwrite,1:3]=xyz[:,0:2] dtopo[kwrite:kwrite+numel,1:3]=xyz[:,0:2] dtopo[kwrite:kwrite+numel,:]=c_[tvec,xyz] kwrite=kwrite+numel print '... writting dtopo files' savetxt(data_dir+outname+'.dtopo',dtopo,fmt='%i\t%.6f\t%.6f\t%.4e')