def __init__(self, app, options=None): # Start pyferret pyferret.start(journal=False, unmapped=True, quiet=True, verify=False) master_pid = os.getpid() print('---------> gunicorn master pid: ', master_pid) instance_WMS_Client = Template(template_WMS_client()) instance_NW_Package = Template(template_nw_package()) with open(tmpdir + '/index.html', 'wb') as f: f.write(instance_WMS_Client.render(gunicornPID=master_pid, mapWidth=mapWidth, mapHeight=mapHeight, mapCenter=mapCenter, mapZoom=mapZoom, port=port)) with open(tmpdir + '/package.json', 'wb') as f: f.write(instance_NW_Package.render()) # Copy icon path = os.path.dirname(os.path.realpath(__file__)) shutil.copy(path + "/icon.png", tmpdir) # Launch NW.js proc = subprocess.Popen(['nw', tmpdir]) print('Client nw process: ', proc.pid) self.options = options or {} self.application = app super(StandaloneApplication, self).__init__()
def __init__(self, app, options=None): # Start pyferret pyferret.start(journal=False, unmapped=True, quiet=True, verify=False) master_pid = os.getpid() print('---------> gunicorn master pid: ', master_pid) if not serverOnly: # nw will be launched listSynchroMapsToSet = list(itertools.permutations(range(1,nbMaps+1), 2)) instance_WMS_Client = Template(template_WMS_client()) instance_NW_Package = Template(template_nw_package()) with open(tmpdir + '/index.html', 'wb') as f: f.write(instance_WMS_Client.render(cmdArray=cmdArray, gunicornPID=master_pid, listSynchroMapsToSet=listSynchroMapsToSet, mapWidth=mapWidth, mapHeight=mapHeight, mapCenter=mapCenter, mapZoom=mapZoom, port=port)) with open(tmpdir + '/package.json', 'wb') as f: f.write(instance_NW_Package.render(nbMaps=nbMaps, mapWidth=mapWidth, mapHeight=mapHeight)) # Launch NW.js proc = subprocess.Popen(['nw', tmpdir]) print('Client nw process: ', proc.pid) self.options = options or {} self.application = app super(StandaloneApplication, self).__init__()
def __init__(self, shell): """ Parameters ---------- shell : IPython shell """ super(ferretMagics, self).__init__(shell) try: pyferret.start(memsize=(_DEFAULT_MEMSIZE/8.0), verify=False, journal=False, unmapped=True, quiet=True) except ExceptionPexpect: raise ferretMagicError('pyferret cannot be started') self._shell = shell self._shell.ferret_locked = False
def __init__(self, app, options=None): # Start pyferret pyferret.start(journal=False, unmapped=True, quiet=True, verify=False, memsize=50) master_pid = os.getpid() print('---------> gunicorn master pid: ', master_pid) self.options = options or {} self.application = app super(StandaloneApplication, self).__init__()
def app3z(nc_file='', var_name='tmn', k=1, l=1, outDir=''): # init_ferret import pyferret as pf pf.start(quiet=True, unmapped=True) pf.run('set memory/size=500') pf.run('cancel mode logo') pf.run('cancel mode journal') temp1 = 'use "%s"' % nc_file print(temp1) pf.run(temp1) temp1 = 'show data' print(temp1) pf.run(temp1) #temp1 = 'shade %s[k=%d,l=%d]\ngo land'%(var_name, k, l) temp1 = 'shade %s[l=%d]' % (var_name, l) print(temp1) pf.run(temp1) temp1 = 'go land' print(temp1) pf.run(temp1) t1 = outDir.find('static') outDir1 = outDir[t1:] figFile1 = '%s/plot.png' % outDir1 print(figFile1) figFile0 = '%s/plot.png' % outDir temp1 = 'frame/file="%s"' % figFile0 print(temp1) pf.run(temp1) mess = 'Plot var %s of file:\n%s' % (nc_file, var_name) figFile = 'http://ec2-13-56-153-11.us-west-1.compute.amazonaws.com:5003/%s' % figFile1 return mess, figFile, None
import os import pyferret pyferret.start(quiet=True) import shutil import iris_sample_data fname = 'A1B_north_america.nc' srcpath = os.path.join(iris_sample_data.path, fname) ncpath = os.path.join('/tmp', fname) shutil.copy(srcpath, ncpath) ds = pyferret.FerDSet(ncpath) assert (ds['height'].unit == 'm') assert (ds['air_temperature'].data.shape == (49, 37, 1, 240, 1, 1)) os.remove(ncpath) print('ran pyferret test')
def regrid_once_primitive(var, ref_var, axis, verbose=False, prerun=None, transform='@ave'): ''' A generic function that regrids a variable without the dependence of geodat.nc.Variable Args: var (dict) : arguments for num2fer Required keys: data,coords,dimunits ref_var (dict) : arguments for num2fer. This supplies the grid for regridding Required keys: coords,dimunits axis (str) : the axis for regridding e.g. 'X'/'Y'/'XY'/"YX" verbose (bool) : whether to print progress (default: False) prerun (a list of str) : commands to be run at the start (default: None) transform (str): "@ave" (Conserve area average), "@lin" (Linear interpolation),...see Ferret doc Returns: dict ''' if not PYFERRET_INSTALLED: raise _IMPORT_PYFERRET_ERROR pyferret.start(quiet=True, journal=verbose, verify=False, server=True) # commands to run before regridding if prerun is not None: if type(prerun) is str: pyferret.run(prerun) elif type(prerun) is list: for s in prerun: if type(s) is str: pyferret.run(prerun) else: raise Exception("prerun has to be either a string or "+\ "a list of string") else: raise Exception("prerun has to be either a string or a list of "+\ "string") assert isinstance(axis, str) axis = axis.upper() # Make sure axis is a string denoting X or Y axis #if axis not in ['X', 'Y', 'XY', 'YX']: # raise Exception("Currently axis can only be X/Y/XY") # Construct the source data read by pyferret.putdata source_fer = num2fer(**var) source_fer["name"] = "source" # Fill in unnecessary input for Ferret if "data" not in ref_var: ref_var['data'] = numpy.zeros((1,)*len(ref_var['coords'])) # Construct the destination data read by pyferret.putdata dest_fer = num2fer(**ref_var) dest_fer["name"] = "dest" if verbose: print source_fer print dest_fer pyferret.putdata(source_fer, axis_pos=source_fer['axis_pos']) if verbose: print "Put source variable" pyferret.run('show grid source') pyferret.putdata(dest_fer, axis_pos=dest_fer['axis_pos']) if verbose: print "Put destination variable" pyferret.run('show grid dest') pyfer_command = 'let result = source[g'+axis.lower()+'=dest'+transform+']' pyferret.run(pyfer_command) if verbose: print "Regridded in FERRET" pyferret.run('show grid result') # Get results result_ref = pyferret.getdata('result') if verbose: print "Get data from FERRET" # Convert from ferret data structure to geodat.nc.Variable tmp_result = fer2num(result_ref) if 'varname' in var: tmp_result['varname'] = var['varname'] tmp_caxes = [geodat.units.assign_caxis(dimunit) for dimunit in tmp_result['dimunits']] var_caxes = [geodat.units.assign_caxis(dimunit) for dimunit in var['dimunits']] # Preserve dimension order (Ferret reverts the order) neworder = [tmp_caxes.index(cax) for cax in var_caxes] # Change the dimension order of the result to match with the input tmp_result['coords'] = [tmp_result['coords'][iax] for iax in neworder] tmp_result['dimunits'] = [tmp_result['dimunits'][iax] for iax in neworder] if 'dimnames' in tmp_result: tmp_result['dimnames'] = [tmp_result['dimnames'][iax] for iax in neworder] tmp_result['data'] = tmp_result['data'].transpose(neworder).astype( var['data'].dtype) # Return the input var with the data and dimensions replaced by # the regridded ones var.update(tmp_result) result = var status = pyferret.stop() if verbose: if status: print "PyFerret stopped." else: print "PyFerret failed to stop." return result
#========================================= # Dependencies #================================================================= # Initialize access to PyFerret. # Understanding the following lines of code is not part of the course. # Acknowlegements: # -> Pyferret: NOAA, https://ferret.pmel.noaa.gov/Ferret/ # -> ferretmagic: Patrick Brockmann, https://pypi.org/project/ferretmagic/ # -> These exact lines for running PyFerret on the Bowdoin HPC Grid: DJ Merrill #================================================================= import sys sys.path.append('/mnt/local/pyferret/lib/python3.6/site-packages') import pyferret pyferret.addenv(FER_DIR='/mnt/local/pyferret', FER_DAT='/mnt/local/FerretDatasets') pyferret.start(journal=False, quiet=True, unmapped=True) import numpy as np import pandas as pd import datetime #========================================== # This is a very simple function for the # purposes of testing. #========================================== def add_one(x): y = x + 1 return y #========================================== # Function to load glider data from mission # NetCDF files
def app3(nc_file='', var_name=None, k=1, l=1, outDir=''): # checkNc import checkNc2 dict1 = {} dict1['fileName'] = nc_file dict1['source'] = 'online' dict1['message'] = '' ok1 = checkNc2.checkNc(nc_file, dict1, allowOverwrite=0) mes1 = dict1['message'] mes1 += '\n\n' + dict1['check'] print(ok1) print('dict1.key:') for kk in dict1.keys(): print(kk) varList = dict1['varList'] var_name1 = None if var_name is None: for v in varList: d2 = dict1['varDict'][v]['dim2'] if (len(d2) > 1) and (len(d2) < 5): if (d2[-1] == 'lon') and (d2[-2] == 'lat'): var_name1 = v nD = len(d2) else: var_name1 = var_name try: d2 = dict1['varDict'][var_name]['dim2'] nD = len(d2) except: text1 = 'Variable %s does not exist in the file.' % var_name print(text1) mes1 += '\n%s' % text1 if var_name1 is None: mes1 += '\n\n## Cannot figure out which variable to plot.##' # plotting k = int(k) l = int(l) # use_ferret if 1: # init_ferret import pyferret as pf pf.start(quiet=True, unmapped=True) pf.run('set memory/size=1000') pf.run('cancel mode logo') pf.run('cancel mode journal') temp1 = 'use "%s"' % nc_file print(temp1) pf.run(temp1) temp1 = 'show data' print(temp1) pf.run(temp1) if nD == 2: temp1 = 'shade %s' % (var_name1) elif nD == 3: temp1 = 'shade %s[l=%d]' % (var_name1, l) elif nD == 4: temp1 = 'shade %s[k=%d, l=%d]' % (var_name1, k, l) print(temp1) pf.run(temp1) temp1 = 'go land' print(temp1) pf.run(temp1) t1 = outDir.find('static') outDir1 = outDir[t1:] figFile1 = '%s/plot.png' % outDir1 print(figFile1) figFile0 = '%s/plot.png' % outDir temp1 = 'frame/file="%s"' % figFile0 print(temp1) pf.run(temp1) if 0: # use basemap from netCDF4 import Dataset import matplotlib as mpl mpl.use('Agg') # for interactive. Work on svm3 import matplotlib.pylab as Mat from mpl_toolkits.basemap import Basemap mpl.rcParams['image.cmap'] = 'jet' nc = Dataset(nc_file) ncVar = nc.variables[var_name1] dims1 = ncVar.dimensions print(dims1) print(ncVar.shape) if 0: if len(dims1) == 3: data1 = ncVar[k - 1, ::-1, :] elif len(dims1) == 4: data1 = ncVar[k - 1, l - 1, ::-1, :] elif len(dims1) == 2: data1 = ncVar[::-1, :] if 1: if len(dims1) == 3: data1 = ncVar[k - 1, :, :] elif len(dims1) == 4: data1 = ncVar[k - 1, l - 1, :, :] elif len(dims1) == 2: data1 = ncVar[:, :] lon2 = nc.variables[dims1[-1]][:] lat2 = nc.variables[dims1[-2]][:] m = Basemap(lon2[0], lat2[0], lon2[-1], lat2[-1], resolution='c', suppress_ticks=False) #m.pcolor(lon2, lat2, pattern1[i, ::-1, :], vmin=min2, vmax=max2, shading='flat') #data1 = data1[::-1, :] m.pcolor(lon2, lat2, data1) m.drawcoastlines(color=(.7, .7, .7)) m.colorbar() #Mat.title('EOF %d'%(i+1)) figFile0 = '%s/plot.png' % outDir Mat.savefig(figFile0, dpi=100) #figFile = 'http://localhost:5003/%s'%figFile0 #figFile = 'http://ec2-13-56-67-192.us-west-1.compute.amazonaws.com:8080/%s'%figFile0 # for ec20 t1 = outDir.find('static') outDir1 = outDir[t1:] figFile1 = '%s/plot.png' % outDir1 print(figFile1) figFile = 'http://ec2-13-56-153-11.us-west-1.compute.amazonaws.com:5003/%s' % figFile1 return mes1, figFile, None