def hemco_to_netCDF( folder, hemco_file_list=None, remake=False ): """ Conbine HEMCO diagnostic output files to a single NetCDF file. Parameters ---------- remake (boolean): overwrite existing NetCDF file """ from bpch2netCDF import get_folder folder = get_folder(folder) output_file = os.path.join(folder, 'hemco.nc') # If the hemco netCDF file already exists then quit unless remake=True if not remake: if os.path.exists(output_file): logging.warning( output_file + ' already exists, not remaking') return logging.info("Combining hemco diagnostic files") # By default look for any files that look like hemco diagnostic files: # Look for all hemco netcdf files then remove the restart files. if hemco_file_list==None: hemco_files = glob.glob(folder + '/*HEMCO*.nc') for filename in hemco_files: if "restart" in filename: hemco_files.remove(filename) else: file_list = [] for hemco_file in hemco_file_list: full_path = os.path.join(folder, hemco_file) if not os.path.exists(full_path): logging.error(full_path + " could not be found") raise IOError("{path} could not be found".format(path=full_path)) file_list.append(full_path) hemco_files = file_list if len(hemco_files)==0: logging.warning("No hemco diagnostic files found in {_dir}"\ .format(_dir=folder)) else: logging.debug( "The following hemco files were found:") logging.debug( str(hemco_files) ) # Use iris cubes to combine the data into an output file hemco_data = iris.load(hemco_files) # Concatanate the times. hemco_data = hemco_data.concatenate() iris.save( hemco_data, output_file) logging.info( str(hemco_data) ) logging.info("Hecmo file created at {file}".format(file=output_file)) return
def bpch_to_netCDF(folder='none', filename='ctm.nc',\ bpch_file_list=None, remake=False, verbose=True): """ Converts GEOS-Chem ctm.bpch output file(s) to NetCDF """ # Check if file already exists and warn about remaking from bpch2netCDF import get_folder folder = get_folder(folder) output_file = os.path.join(folder, filename) # If the netCDf file already exists dont overwrite it without remake=True. if not remake: if os.path.exists(output_file): logging.warning(output_file + ' already exists. Not recreating.') return # By default look inside the folder for any files if bpch_file_list == None: bpch_files = glob.glob(folder + '/*.bpch*') if len(bpch_files) == 0: bpch_files = glob.glob(folder + '/*trac_avg*') if len(bpch_files) == 0: logging.error("No bpch files found in " + folder) raise IOError(folder + " contains no bpch files.") # Confirm the specified bpch files are there. else: file_list = [] for bpch_file in bpch_file_list: full_path = folder + '/' + bpch_file if not os.path.exists(full_path): logging.error(full_path + " could not be found") raise IOError("Full path could not be found") file_list.append(full_path) bpch_files = file_list # Open the bpch files logging.debug("The following bpch files were found:") logging.debug(str(bpch_files)) if verbose: print "Creating a netCDF file. This can take some time..." bpch_data = datasets.load(bpch_files) # Save the netCDF file # iris.fileformats.netcdf.save(data, output_file) datasets.save(bpch_data, output_file) logging.info("A netCDF file has been created with the name {ctm}".format( ctm=output_file)) return
def hemco_to_netCDF(folder, hemco_file_list, remake=False): from bpch2netCDF import get_folder folder = get_folder(folder) output_file = os.path.join(folder, 'hemco.nc') # If the hemco netCDF file already exists then quit unless remake=True if not remake: if os.path.exists(output_file): logging.warning(output_file + ' already exists, not remaking') return logging.info("Combining hemco diagnostic files") # By default look for any files that look like hemco diagnostic files: # Look for all hemco netcdf files then remove the restart files. if hemco_file_list == None: hemco_files = glob.glob(folder + '/*HEMCO*.nc') for filename in hemco_files: if "restart" in filename: hemco_files.remove(filename) if len(hemco_files) == 0: logging.warning("No hemco diagnostic files found in wd") else: logging.debug("The following hemco files were found:") logging.debug(str(hemco_files)) # Use iris cubes to combine the data into an output file hemco_data = iris.load(hemco_files) # Concatanate the times. hemco_data = hemco_data.concatenate() iris.save(hemco_data, output_file) logging.info(str(hemco_data)) logging.info("Hecmo file created at {file}".format(file=output_file)) return
def convert_to_netCDF(folder='none', filename='ctm.nc'): from bpch2netCDF import get_folder folder = get_folder(folder) # Get ctm.bpch ( inc. if named *trac_avg* ) import glob bpch_files = glob.glob(folder + '/*ctm.bpch*') if len(bpch_files) == 0: bpch_files = glob.glob(folder + '/*trac_avg*') # Open the bpch files try: from pygchem import datasets except: import pygchem.datafields as datasets data = datasets.load(bpch_files) # Save the netCDF file output_file = folder + '/' + filename import iris iris.fileformats.netcdf.save(data, output_file) return
def bpch_to_netCDF(folder=None, filename='ctm.nc', bpch_file_list=None, \ remake=False, filetype="*ctm.bpch*", verbose=False, **kwargs): """ Converts GEOS-Chem ctm.bpch output file(s) to NetCDF Parameters ---------- folder (str): working directory for data files filename (str): name to give created NetCDF bpch_file_list (list): list of files to convert remake (boolean): overwrite existing NetCDF file filetype (str): string with wildcards to match filenames ( e.g. *ctm.bpch*,*ts*bpch* ) verbose (boolean): print (minor) logging to screen Returns ------- (None) saves a NetCDF file to disk """ # Check if file already exists and warn about remaking from bpch2netCDF import get_folder folder = get_folder(folder) output_file = os.path.join(folder, filename) # If the netCDf file already exists dont overwrite it without remake=True. if not remake: if os.path.exists(output_file): logging.warning(output_file + ' already exists. Not recreating.') return # Look for files if file list is not provided. if isinstance( bpch_file_list, type(None) ): logging.debug("Searching for the following bpch filetype: {filetype}"\ .format(filetype=filetype)) bpch_files = glob.glob( folder + '/' + filetype ) if len(bpch_files) == 0: logging.error("No bpch files found in "+folder) raise IOError(folder + " contains no bpch files.") # use the specified files. else: file_list = [] for bpch_file in bpch_file_list: full_path = folder + '/' + bpch_file if not os.path.exists(full_path): logging.error(full_path + " could not be found") raise IOError("Full path could not be found") file_list.append(full_path) bpch_files = file_list # Open the bpch files logging.debug( "The following bpch files were found:") logging.debug( str(bpch_files) ) if verbose: print "Creating a netCDF file. This can take some time..." bpch_data = datasets.load(bpch_files) # Save the netCDF file # iris.fileformats.netcdf.save(data, output_file) datasets.save( bpch_data, output_file ) logging.info( "A netCDF file has been created with the name {ctm}".format(ctm=output_file)) return