def load(self): ''' Retrieves the grid and data for this Ferret variable from Ferret. This method is automatically called before returning the grid or data for the first time for this variable. This can be called to update the grid or data in this FerVar after any change in the definition of the variable. Alternatively, cleardata can be called to clear any stored grid and data, delaying the update from Ferret until the grid or data is requested. Raises a ValueEror if problems occur. ''' fername = self.fername() datadict = pyferret.getdata(fername, False) feraxes = [] for (axistype, axcoords, axunit, axname) in zip(datadict["axis_types"], datadict["axis_coords"], datadict["axis_units"], datadict["axis_names"]): feraxes.append( pyferret.FerAxis(coords=axcoords, axtype=axistype, unit=axunit, name=axname)) self._datagrid = pyferret.FerGrid(axes=feraxes, name=fername) self._dataarray = datadict["data"] self._dataunit = datadict["data_unit"] self._missingvalue = datadict["missing_value"]
def load_mnc_profile(sgid,dive_number): # Wipe any previously loaded data and variables in # Ferret. These lines allow for multiple reuse of # this function in a given kernel session. (e_v, e_m) = pyferret.run('cancel data /all') (e_v, e_m) = pyferret.run('cancel variables /all') # Set a shorter variable name for number of dives. # If the glider data has climbs and dives, mult # by 2 and subtract 1 to index just the dives. dn = dive_number*2 - 1 # Load the requested data into the notebook (e_v, e_m) = pyferret.run( 'use /mnt/courses/eos1505/sg'+str(sgid)+'/sg'+str(sgid)+'_m03.nc') # Assign subsets of the data in Ferret - we want to pull out # just the data for this particular dive, not the whole mission. (e_v, e_m) = pyferret.run('let temp = theta[l='+str(dn)+']') (e_v, e_m) = pyferret.run('let salt = salinity[l='+str(dn)+']') (e_v, e_m) = pyferret.run('let dens = density[l='+str(dn)+']') (e_v, e_m) = pyferret.run('let dept = ctd_depth[l='+str(dn)+']') # Bring the data from Ferret into the Notebook temp = np.squeeze(pyferret.getdata('temp',False)['data']) salt = np.squeeze(pyferret.getdata('salt',False)['data']) dens = np.squeeze(pyferret.getdata('dens',False)['data']) dept = np.squeeze(pyferret.getdata('dept',False)['data']) # Filter out missing values (usually the placeholder is # a very large negative number, 1e-34) temp[temp<-4.0] = np.nan salt[salt<0] = np.nan dens[dens<900] = np.nan return dept, temp, salt, dens
def load_chl(lon, lat, dt): # Load the data set and store as a Python variable (e_v, e_m) = pyferret.run('cancel data /all') (e_v, e_m) = pyferret.run('cancel variables /all') (e_v, e_m) = pyferret.run( 'use /mnt/courses/eos1505/MODIS/MODIS_'+dt+'.cdf') chl_dict = pyferret.getdata('CHL_FILL[x='+str(lon)+', y='+str(lat)+']',False) # Put the data into Python arrays chl = np.squeeze(chl_dict['data']) # Mask out fill values chl[chl < 0] = np.nan ymdhms = np.zeros(np.shape(chl_dict['axis_coords'][3])).astype(int) ymdhms[:,0] = chl_dict['axis_coords'][3][:,2].astype(int) ymdhms[:,1] = chl_dict['axis_coords'][3][:,1].astype(int) ymdhms[:,2] = chl_dict['axis_coords'][3][:,0].astype(int) chl_dates = [datetime.datetime(*dd) for dd in ymdhms] return chl, chl_dates
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
def load_topo(): # Load the data set and store as a Python variable # Python dictionaries are a type of variable that # stores the data along with its metadata. # # The pyferret.getdata commands are accessing data # by variable name in the given file. If you ever # need to explore the available variables in a # NetCDF file, use the following command at the # *terminal*: # # ncdump -h /mnt/courses/eos2680/ETOPO1/topo_tenthdeg_ice_gline.nc # # and all the information about the NetCDF file # will be displayed. (e_v, e_m) = pyferret.run('cancel data /all') (e_v, e_m) = pyferret.run('cancel variables /all') (error_value, error_message) = pyferret.run( 'use /mnt/courses/eos2680/ETOPO1/topo_tenthdeg_ice_gline.nc') lon_dict = pyferret.getdata('lon1',False) lat_dict = pyferret.getdata('lat1',False) topo_dict = pyferret.getdata('z1',False) # The "keys" are the names of the entries in the # dictionary - its pieces. You can access the values # associated with a dictionary's keys with # dict_name['key_name']. #print(topo_dict.keys()) # Put the data into Python arrays lon = lon_dict['data'] lat = lat_dict['data'] topo = topo_dict['data'] # And you can see the size of the data array # which is a grid in the x, y, directions but # the z (depth), time, and other dimensions # are placeholder dimensions in that they have # only a length of 1. #print('Array size:') #print(np.shape(topo)) # To cut out these singleton dimensions, use # the squeeze command: lon = np.squeeze(lon) lat = np.squeeze(lat) topo = np.squeeze(topo) #print('Array size after squeeze:') #print(np.shape(topo)) # Note that all of the above can be condensed # into one line, but it's much harder to # understand and verify that the code is working # in the condensed version (commented out below). #lon = np.squeeze(pyferret.getdata('lon1',False)['data']) #lat = np.squeeze(pyferret.getdata('lat1',False)['data']) #topo = np.squeeze(pyferret.getdata('z1',False)['data']) # Finally, lon and lat are given as # vectors, for plotting it is easier # to expand them into 2D arrays to # have the same size as topo. [y,x] = np.meshgrid(lat,lon) return x, y, topo