def run(self, latidx, lonidx): try: inputfile_dir = self.config.get_dict(self.translator_type, 'inputfile_dir', default=os.path.join( '..', '..')) inputfiles = self.config.get_dict(self.translator_type, 'inputfile', default='1.soil.tile.nc4') inputfiles = param_to_list(inputfiles) latdelta, londelta = [ double(d) / 60 for d in self.config.get('delta').split(',') ] # convert to degrees nco = Nco() outputfiles = self.config.get_dict( self.translator_type, 'outputfile', default=inputs_to_outputs(inputfiles)) outputfiles = param_to_list(outputfiles) inputfiles = apply_prefix(inputfiles, inputfile_dir) for i in range(len(inputfiles)): inputfile = inputfiles[i] outputfile = outputfiles[i] with nc(inputfile) as f: variables = f.variables.keys() soil_id = f.getncattr('soil_id') # get latitude, longitude limits minlat = 90 - latdelta * latidx maxlat = minlat + latdelta minlon = -180 + londelta * (lonidx - 1) maxlon = minlon + londelta # additional options options = '-h -a lat,lon -d lat,%f,%f -d lon,%f,%f --rd' % ( minlat, maxlat, minlon, maxlon) if 'cropland' in variables: options += ' -w cropland' # perform aggregation nco.ncwa(input=inputfile, output=outputfile, options=options) # add degenerate profile dimension nco.ncecat(input=outputfile, output=outputfile, options='-O -h -u profile') nco.ncap2(input=outputfile, output=outputfile, options='-O -h -s profile[profile]=1') # add soil_id variable nco.ncap2(input=outputfile, output=outputfile, options='-O -h -s soil_id[profile,lat,lon]=1') nco.ncatted(input=outputfile, output=outputfile, options='-O -h -a units,soil_id,c,c,"mapping"') nco.ncatted(input=outputfile, output=outputfile, options='-O -h -a long_name,soil_id,c,c,"%s"' % str(soil_id)) nco.ncatted(input=outputfile, output=outputfile, options='-O -h -a soil_id,global,d,c,""') # change latitude, longitude to simulated point with nc(outputfile, 'a') as f: latv = f.variables['lat'] latv[:] = 90 - latdelta * (latidx - 0.5) lonv = f.variables['lon'] lonv[:] = -180 + londelta * (lonidx - 0.5) return True except: print "[%s]: %s" % (os.path.basename(__file__), traceback.format_exc()) return False
def write_means_to_netcdf_file(files, ens_means, variables, start_date, end_date, argv, test=False): """ Write means computed in netcdf files :param files: initial files :param ens_means: ensemble means calculated calling function compute_average :param variables: list of variables :param start_date: start date list in [day, month, year] format :param end_date: end date list in [day, month, year] format :param argv: string containing command line arguments used :param test: if test is true, make some changes specific to files on my pc :return: None, files created in folder analysis/ensemble_means """ # Initialise Nco nco = Nco() # Start and end date string start_end_str = str(start_date[0]) + "-" + str(start_date[1]) + "-" + str(start_date[2]) + " and " + \ str(end_date[0]) + "-" +str(end_date[1]) + "-" + str(end_date[2]) # Get path path = directories.CLIMATE_DATA # Get normal and files in absolute path saved in ensemble groups ens_files = [[] for _ in range(len(ens_means))] abs_files = [[] for _ in range(len(ens_means))] # Get absolute path of each file for i in range(len(files)): # Get file ensemble number index ens_indx = ens_to_indx(get_ens_num(files[i])) # save in ens_files ens_files[ens_indx].append(files[i]) # Get absolute path joined = os.path.abspath(os.path.join(path, files[i])) if test: joined = joined.replace("Adanna Akwataghibe", "Adanna") # save in ens_files abs_files[ens_indx].append(joined) # Get folder to store ensemble means results = directories.ANALYSIS mean_folder = os.path.abspath(os.path.join(results, directories.MEANS)) if test: mean_folder = mean_folder.replace("Adanna Akwataghibe", "Adanna") # Go through ensembles, merge files to get output and write to output for i in range(len(ens_means)): # Get first file name in specific ensemble and add last year to name - use as output file name output_file = "" if ens_files[i][0].endswith(".nc"): output_file = ens_files[i][0][:-3] + '_' + str(end_date[2]) + '.nc' output_file = os.path.join(mean_folder, output_file) # Merge files in ensemble in output_file nco.ncecat(input=abs_files[i], output=output_file) # Write means to file with Dataset(abs_files[i][0], 'r') as src, Dataset(output_file, 'a') as dest: for var in variables: # create dataset identical to original variable in file mean_var_name = var + '_mean' datatype = src.variables[var].datatype # Get dimensions without time dims = src.variables[var].dimensions[1:] mean_var = dest.createVariable(mean_var_name, datatype, dims) # save means in variable mean_var[:] = ens_means[i][var][:] mean_var.setncatts(src[var].__dict__) mean_var.long_name = mean_var.long_name + ' averaged between ' + start_end_str # Write to description and history of file desc_str = "Added averages of variables " + ', '.join(variables) + " within time period " + \ start_end_str if 'description' in dest.ncattrs(): dest.description = desc_str + ' \n' + dest.description else: dest.description = desc_str dest.history = time.ctime(time.time()) + ': Commands used to produce file: ' + argv + ' \n' + \ time.ctime(time.time()) + ': Functions used: extract_data, compute_average,' \ ' write_means_to_netcdf_file' + ' \n' + dest.history print("Mean ensemble files created in " + os.path.join(directories.ANALYSIS, directories.MEANS) + "folder.")