def readParameters(self, rootFile): # Read inverted parameters ff = nf(rootFile+'.parameters', 'r') pars = ff.variables['map'][:] ff.close() # Read errors ff = nf(rootFile+'.errors', 'r') errors = ff.variables['map'][:] ff.close() return pars, errors
def readParameters(self, rootFile): # Read inverted parameters ff = nf(rootFile + '.parameters', 'r') pars = ff.variables['map'][:] ff.close() # Read errors ff = nf(rootFile + '.errors', 'r') errors = ff.variables['map'][:] ff.close() return pars, errors
def write(self, filename): nc_fid = nf(filename, 'w') # Create dims nc_fid.createDimension('x', self.nx) nc_fid.createDimension('y', self.ny) nc_fid.createDimension('stokes', self.ns) nc_fid.createDimension('wav', self.nw) nc_fid.createDimension('time') if (self.dat.dtype == 'float64'): prec = 'f8' else: prec = 'f4' prof = nc_fid.createVariable('profiles', prec, ('time', 'y', 'x', 'wav', 'stokes')) wav = nc_fid.createVariable('wav', 'f8', ('wav')) wei = nc_fid.createVariable('weights', 'f4', ('wav', 'stokes')) pwe = nc_fid.createVariable('pixel_weights', 'f4', ('time', 'y', 'x')) print("profile::write: saving data [{0}]".format(filename)) # Write data for ii in range(self.nt): prof[ii, :, :, :, :] = self.dat[ii, :, :, :, :] pwe[ii] = self.pweights[ii, :, :] wav[:] = self.wav wei[:] = self.weights nc_fid.close()
def _getContent(self, file, verb=False): inam = "model::_getContent: " nc_fid = nf(file, 'r') nc_dims = [dim for dim in nc_fid.dimensions] nc_vars = np.asarray([var for var in nc_fid.variables]) if (verb): dstr = '' dvar = ' ' ivar = '' ii = 0 for dim in nc_dims: dstr += str(dim) + '={0} '.format(len(nc_fid.dimensions[dim])) print(inam + 'Dimensions:') print(' ' + dstr) print(' ') print(inam + 'Variables:') for var in nc_vars: ivar = ' ' tmp = nc_fid.variables[var].dimensions for d in tmp: ivar += str(d) + ' ' print(' [{0}] '.format(ii) + '{:>10}'.format(var) + ' [' + ivar + ']') ii += 1 print(" ") nc_fid.close() return (nc_dims, nc_vars)
def readProfiles(self, rootFile): # Read inverted profiles ff = nf(rootFile+'.inversion', 'r') synthProf = ff.variables['map'][:] ff.close() return synthProf
def _getContent(self, filename): nc_fid = nf(filename, 'r') nc_dims = [dim for dim in nc_fid.dimensions] nc_vars = np.asarray([var for var in nc_fid.variables]) nc_fid.close() return (nc_dims, nc_vars)
def readProfiles(self, rootFile): # Read inverted profiles ff = nf(rootFile + '.inversion', 'r') synthProf = ff.variables['map'][:] ff.close() return synthProf
def readObservations(self, rootFile): # Read inverted profiles ff = nf(rootFile+'.nc', 'r') sizeMask = ff.variables['mask'].shape sizeMap = ff.variables['map'].shape obsProf = ff.variables['map'][:].reshape((sizeMask[0],sizeMask[1],sizeMap[-2],sizeMap[-1])) ff.close() return obsProf
def _readVar1(self, file, vname, x0, x1, y0, y1, t0, t1): nc_fid = nf(file, 'r') nc_vars = np.asarray([var for var in nc_fid.variables]) inam = "model::_readVar1: " dum = np.where(nc_vars == vname) res = None if (len(dum[0]) == 1): res = nc_fid.variables[vname][t0:t1, y0:y1, x0:x1, :] nc_fid.close() return (res)
def readObservations(self, rootFile): # Read inverted profiles ff = nf(rootFile + '.nc', 'r') sizeMask = ff.variables['mask'].shape sizeMap = ff.variables['map'].shape obsProf = ff.variables['map'][:].reshape( (sizeMask[0], sizeMask[1], sizeMap[-2], sizeMap[-1])) ff.close() return obsProf
def writeInstProf(oname, var, pref=[]): ncfile1 = nf(oname, 'w', format='NETCDF4') ncfile1.createDimension('wav', var.size) par1 = ncfile1.createVariable('iprof', 'f8', ('wav')) par1[:] = var if (len(pref) == 3): ncfile1.createDimension('np', len(pref)) par2 = ncfile1.createVariable('pref', 'f8', ('np')) par2[:] = np.float32(pref) ncfile1.close()
def _getDimensions(self, file, atype=0): nc_fid = nf(file, 'r') if (atype == 0): return (0, 0, 0, 0) elif (atype == 1 or atype == 2): nx = len(nc_fid.dimensions['x']) ny = len(nc_fid.dimensions['y']) ndep = len(nc_fid.dimensions['ndep']) nt = len(nc_fid.dimensions['time']) nc_fid.close() return (nx, ny, ndep, nt)
def _getType(self, file): nc_fid = nf(file, 'r') inam = "model::getType: " nc_dims = [dim for dim in nc_fid.dimensions] nc_vars = np.asarray([var for var in nc_fid.variables]) nc_fid.close() dum = np.where(nc_vars == 'temp') if (len(dum[0]) == 1): return 1 dum = np.where(nc_vars == 'model') if (len(dum[0]) == 1): return 2 return 0
def read(self, filename): # Get content of file nc_dims, nc_vars = self._getContent(filename) nc_fid = nf(filename, 'r') # Set dims nx = 0 ny = 0 nw = 0 ns = 0 nt = 0 for ii in nc_dims: if (ii == 'x'): nx = len(nc_fid.dimensions['x']) elif (ii == 'y'): ny = len(nc_fid.dimensions['y']) elif (ii == 'wav'): nw = len(nc_fid.dimensions['wav']) elif (ii == 'stokes'): ns = len(nc_fid.dimensions['stokes']) elif (ii == 'time'): nt = len(nc_fid.dimensions['time']) elif (ii == 'vtype'): self.nder = len(nc_fid.dimensions['vtype']) self.setsize(nx, ny, nw, ns, nt) read = "" print(nc_vars) # Readvars if (len(np.where(nc_vars == 'profiles')[0]) == 1): self.dat[:, :, :, :, :] = nc_fid.variables['profiles'][:] read += "[profiles]" if (len(np.where(nc_vars == 'wav')[0]) == 1): self.wav[:] = nc_fid.variables['wav'][:] read += "[wav]" if (len(np.where(nc_vars == 'weights')[0]) == 1): self.weights[:, :] = nc_fid.variables['weights'][:] read += "[weights]" if (len(np.where(nc_vars == 'pixel_weights')[0]) == 1): self.pweights[:, :, :] = nc_fid.variables['pixel_weights'][:] read += "[pixel_weights]" if (len(np.where(nc_vars == 'derivatives')[0]) == 1): self.rf = nc_fid.variables['derivatives'][:] read += '[derivatives]' if (len(np.where(nc_vars == 'derivatives')[0]) == 1): self.rf_type = np.int32(nc_fid.variables['vidx'][:]) read += '[vidx]' print("profile::read: " + read) nc_fid.close()
def write(self, filename, write_all=True, t0 = 0, \ t1 = -1, x0=0, x1=-1, y0=0, y1=-1, z0 = 0, z1 = -1): ncfile = nf(filename, 'w', format='NETCDF4', clobber=True) # Check ranges if (t1 == -1): t1 = self.nt if (t1 > self.nt): t1 = self.nt if (x1 == -1): x1 = self.nx if (y1 == -1): y1 = self.ny if (x1 > self.nx): x1 = self.nx if (y1 > self.ny): y1 = self.ny if (z1 == -1): z1 = self.ndep if (z1 > self.ndep): z1 = self.ndep nx = x1 - x0 ny = y1 - y0 nz = z1 - z0 nt = t1 - t0 print("model::write: {4} -> nx={0}, ny={1}, ndep={2}, nt={3}".\ format(nx, ny, nz, nt, filename)) sys.stdout.flush() # Create dimensions ncfile.createDimension('y', ny) ncfile.createDimension('x', nx) ncfile.createDimension('ndep', nz) ncfile.createDimension('time') # Create vars and write par0 = ncfile.createVariable('ltau500', 'f4', ('time', 'y', 'x', 'ndep')) par1 = ncfile.createVariable('z', 'f4', ('time', 'y', 'x', 'ndep')) par2 = ncfile.createVariable('temp', 'f4', ('time', 'y', 'x', 'ndep')) par3 = ncfile.createVariable('pgas', 'f4', ('time', 'y', 'x', 'ndep')) par4 = ncfile.createVariable('vlos', 'f4', ('time', 'y', 'x', 'ndep')) par5 = ncfile.createVariable('blong', 'f4', ('time', 'y', 'x', 'ndep')) par6 = ncfile.createVariable('bhor', 'f4', ('time', 'y', 'x', 'ndep')) par7 = ncfile.createVariable('azi', 'f4', ('time', 'y', 'x', 'ndep')) par8 = ncfile.createVariable('vturb', 'f4', ('time', 'y', 'x', 'ndep')) par9 = ncfile.createVariable('cmass', 'f4', ('time', 'y', 'x', 'ndep')) if (write_all): par10 = ncfile.createVariable('nne', 'f4', ('time', 'y', 'x', 'ndep')) par11 = ncfile.createVariable('rho', 'f4', ('time', 'y', 'x', 'ndep')) for tt in range(t0, t1): par0[tt] = self.ltau[tt, y0:y1, x0:x1, z0:z1] par1[tt] = self.z[tt, y0:y1, x0:x1, z0:z1] par2[tt] = self.temp[tt, y0:y1, x0:x1, z0:z1] par3[tt] = self.pgas[tt, y0:y1, x0:x1, z0:z1] par4[tt] = self.vlos[tt, y0:y1, x0:x1, z0:z1] par5[tt] = self.Bln[tt, y0:y1, x0:x1, z0:z1] par6[tt] = self.Bho[tt, y0:y1, x0:x1, z0:z1] par7[tt] = self.azi[tt, y0:y1, x0:x1, z0:z1] par8[tt] = self.vturb[tt, y0:y1, x0:x1, z0:z1] par9[tt] = self.cmass[tt, y0:y1, x0:x1, z0:z1] if (write_all): par10[tt] = self.nne[tt, y0:y1, x0:x1, z0:z1] par11[tt] = self.rho[tt, y0:y1, x0:x1, z0:z1] ncfile.close()
from netCDF4 import Dataset as nf ff = nf('test.nc', 'r') wave = ff.variables['lambda1'] res = ff.variables['stokes1'][:]