def _check_not_valid_xml_csw_url(self): """Check not valid xml csw connections resources urls""" self._parse_xml() not_valid_csw_urls = [] gscript.warning( _( "Non valid csw urls going to be removed from connections " "resources xml file {xml}".format( xml=self._conns_resrs_xml, ), ), ) gscript.message(_(self._progress_message)) n = len(self._xml_root) for i, csw in enumerate(self._xml_root): if not self._validate_url(url=csw.attrib["url"], add_url=True): not_valid_csw_urls.append( "{name}{separator}{url}\n".format( name=csw.attrib["name"], separator=self._separator, url=csw.attrib["url"], ), ) self._xml_root.remove(csw) percent(i, n, 1) percent(1, 1, 1) self._not_valid_csw_urls = [] if not_valid_csw_urls: self._write_xml() # Length of printed string 60 not_valid_csw_urls.append( "Number of non valid csw urls{count:.>32}\n".format( count=len(not_valid_csw_urls), ), ) sys.stdout.write("".join(not_valid_csw_urls))
def _check_active_xml_csw_url(self): """Check active xml csw connections resources""" self._parse_xml() not_active_csw = [] gscript.warning( _( "Non active csw items going to be removed from connections " "resources xml file {xml}".format( xml=self._conns_resrs_xml, ), ), ) gscript.message(_(self._progress_message)) n = len(self._xml_root) for i, csw in enumerate(self._xml_root): if not self._is_active_csw_url(url=csw.attrib["url"]): not_active_csw.append( "{name}{separator}{url}\n".format( name=csw.attrib["name"], separator=self._separator, url=csw.attrib["url"], ), ) self._xml_root.remove(csw) percent(i, n, 1) percent(1, 1, 1) if not_active_csw: self._write_xml() # Length of printed string 60 not_active_csw.append( "Number of non active csw{count:.>35}\n".format( count=len(not_active_csw), ), ) sys.stdout.write("".join(not_active_csw))
def timeMsg(self,msg,end=False,step=1): self.set_counter += 1 if self.start is None: self.start = time.time() self.startLast = self.start self.logger.info("Measuring time - START: %s " % str(datetime.now())) self.logger.info(msg) else: self.end = time.time() elapsedTotal = self.end - self.start elapsedLast = self.end-self.startLast self.startLast = self.end grass.percent(self.set_counter,self.total_count_step,1) self.logger.info("TOTAL TIME < %s > : %s" % (msg,elapsedTotal)) self.logger.info("LAST PART TIME< %s > : %s" % (msg,elapsedLast)) if end: grass.percent(self.total_count_step,self.total_count_step,1) self.logger.info("TOTAL TIME e: %s" % (elapsedTotal)) self.logger.info("Measuring time - END: %s " % str(datetime.now()))
def list_available_modules(): mlist = list() grass.message(_('Fetching list of modules from GRASS-Addons SVN (be patient)...')) pattern = re.compile(r'(<li><a href=".+">)(.+)(</a></li>)', re.IGNORECASE) i = 0 prefix = ['d', 'db', 'g', 'i', 'm', 'ps', 'p', 'r', 'r3', 's', 'v'] nprefix = len(prefix) for d in prefix: if flags['g']: grass.percent(i, nprefix, 1) i += 1 modclass = expand_module_class_name(d) grass.verbose(_("Checking for '%s' modules...") % modclass) url = '%s/%s' % (options['svnurl'], modclass) grass.debug("url = %s" % url, debug = 2) f = urllib.urlopen(url) if not f: grass.warning(_("Unable to fetch '%s'") % url) continue for line in f.readlines(): # list modules sline = pattern.search(line) if not sline: continue name = sline.group(2).rstrip('/') if name.split('.', 1)[0] == d: print_module_desc(name, url) mlist.append(name) mlist += list_wxgui_extensions() if flags['g']: grass.percent(1, 1, 1) return mlist
def get_data(self): """ Get csw list of candidates """ if not self._print_info: self._parse_xml() if self._data_theme == "All": gscript.message(_(self._progress_message)) n = len(self._data[self._file_data_key]) for i, row in enumerate(self._data[self._file_data_key]): if row: # Url validation self._handle_multiple_url(url=row[5]) self._print_or_write_data(row) percent(i, n, 1) percent(1, 1, 1) else: gscript.message(_(self._progress_message)) n = len(self._data[self._file_data_key]) for i, row in enumerate(self._data[self._file_data_key]): if row: if row[8] == self._data_theme: # Url validation self._handle_multiple_url(url=row[5]) self._print_or_write_data(row) percent(i, n, 1) percent(1, 1, 1) if self._print_info: sys.stdout.write(self._print_result) else: self._write_xml() if self._print_summary_info: self._print_summary_stats()
def main(): developments = options['development'].split(',') observed_popul_file = options['observed_population'] projected_popul_file = options['projected_population'] sep = gutils.separator(options['separator']) subregions = options['subregions'] methods = options['method'].split(',') plot = options['plot'] simulation_times = [ float(each) for each in options['simulation_times'].split(',') ] for each in methods: if each in ('exp_approach', 'logarithmic2'): try: from scipy.optimize import curve_fit except ImportError: gcore.fatal( _("Importing scipy failed. Method '{m}' is not available"). format(m=each)) # exp approach needs at least 3 data points if len(developments) <= 2 and ('exp_approach' in methods or 'logarithmic2' in methods): gcore.fatal(_("Not enough data for method 'exp_approach'")) if len(developments) == 3 and ('exp_approach' in methods and 'logarithmic2' in methods): gcore.warning( _("Can't decide between 'exp_approach' and 'logarithmic2' methods" " because both methods can have exact solutions for 3 data points resulting in RMSE = 0" )) observed_popul = np.genfromtxt(observed_popul_file, dtype=float, delimiter=sep, names=True) projected_popul = np.genfromtxt(projected_popul_file, dtype=float, delimiter=sep, names=True) year_col = observed_popul.dtype.names[0] observed_times = observed_popul[year_col] year_col = projected_popul.dtype.names[0] projected_times = projected_popul[year_col] if len(developments) != len(observed_times): gcore.fatal( _("Number of development raster maps doesn't not correspond to the number of observed times" )) # gather developed cells in subregions gcore.info(_("Computing number of developed cells...")) table_developed = {} subregionIds = set() for i in range(len(observed_times)): gcore.percent(i, len(observed_times), 1) data = gcore.read_command('r.univar', flags='gt', zones=subregions, map=developments[i]) for line in data.splitlines(): stats = line.split('|') if stats[0] == 'zone': continue subregionId, developed_cells = stats[0], int(stats[12]) subregionIds.add(subregionId) if i == 0: table_developed[subregionId] = [] table_developed[subregionId].append(developed_cells) gcore.percent(1, 1, 1) subregionIds = sorted(list(subregionIds)) # linear interpolation between population points population_for_simulated_times = {} for subregionId in table_developed.keys(): population_for_simulated_times[subregionId] = np.interp( x=simulation_times, xp=np.append(observed_times, projected_times), fp=np.append(observed_popul[subregionId], projected_popul[subregionId])) # regression demand = {} i = 0 if plot: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt n_plots = np.ceil(np.sqrt(len(subregionIds))) fig = plt.figure(figsize=(5 * n_plots, 5 * n_plots)) for subregionId in subregionIds: i += 1 rmse = dict() predicted = dict() simulated = dict() coeff = dict() for method in methods: # observed population points for subregion reg_pop = observed_popul[subregionId] simulated[method] = np.array( population_for_simulated_times[subregionId]) if method in ('exp_approach', 'logarithmic2'): # we have to scale it first y = np.array(table_developed[subregionId]) magn = float( np.power( 10, max(magnitude(np.max(reg_pop)), magnitude(np.max(y))))) x = reg_pop / magn y = y / magn if method == 'exp_approach': initial = ( 0.5, np.mean(x), np.mean(y) ) # this seems to work best for our data for exp_approach elif method == 'logarithmic2': popt, pcov = curve_fit(logarithmic, x, y) initial = (popt[0], popt[1], 0) with np.errstate( invalid='warn' ): # when 'raise' it stops every time on FloatingPointError try: popt, pcov = curve_fit(globals()[method], x, y, p0=initial) if np.isnan(popt).any(): raise RuntimeError # would result in nans in predicted if method == 'logarithmic2' and np.any( simulated[method] / magn <= popt[-1]): raise RuntimeError except (FloatingPointError, RuntimeError): rmse[ method] = sys.maxsize # so that other method is selected gcore.warning( _("Method '{m}' cannot converge for subregion {reg}" .format(m=method, reg=subregionId))) if len(methods) == 1: gcore.fatal( _("Method '{m}' failed for subregion {reg}," " please select at least one other method"). format(m=method, reg=subregionId)) else: predicted[method] = globals()[method]( simulated[method] / magn, *popt) * magn r = globals()[method]( x, *popt) * magn - table_developed[subregionId] coeff[method] = popt if len(reg_pop) > 3: rmse[method] = np.sqrt( (np.sum(r * r) / (len(reg_pop) - 3))) else: rmse[method] = 0 else: if method == 'logarithmic': reg_pop = np.log(reg_pop) if method == 'exponential': y = np.log(table_developed[subregionId]) else: y = table_developed[subregionId] A = np.vstack((reg_pop, np.ones(len(reg_pop)))).T npversion = [int(x) for x in np.__version__.split('.')] if npversion >= [1, 14, 0]: rcond = None else: rcond = -1 m, c = np.linalg.lstsq(A, y, rcond=rcond)[0] # y = mx + c coeff[method] = m, c if method == 'logarithmic': with np.errstate(invalid='ignore', divide='ignore'): predicted[method] = np.where( simulated[method] > 1, np.log(simulated[method]) * m + c, 0) predicted[method] = np.where(predicted[method] > 0, predicted[method], 0) r = (reg_pop * m + c) - table_developed[subregionId] elif method == 'exponential': predicted[method] = np.exp(m * simulated[method] + c) r = np.exp(m * reg_pop + c) - table_developed[subregionId] else: # linear predicted[method] = simulated[method] * m + c r = (reg_pop * m + c) - table_developed[subregionId] # RMSE if len(reg_pop) > 2: rmse[method] = np.sqrt( (np.sum(r * r) / (len(reg_pop) - 2))) else: rmse[method] = 0 method = min(rmse, key=rmse.get) gcore.verbose( _("Method '{meth}' was selected for subregion {reg}").format( meth=method, reg=subregionId)) # write demand demand[subregionId] = predicted[method] demand[subregionId] = np.diff(demand[subregionId]) if np.any(demand[subregionId] < 0): gcore.warning( _("Subregion {sub} has negative numbers" " of newly developed cells, changing to zero".format( sub=subregionId))) demand[subregionId][demand[subregionId] < 0] = 0 if coeff[method][0] < 0: # couldn't establish reliable population-area # project by number of developed pixels in analyzed period range_developed = table_developed[subregionId][ -1] - table_developed[subregionId][0] range_times = observed_times[-1] - observed_times[0] dev_per_step = math.ceil(range_developed / float(range_times)) # this assumes demand is projected yearly demand[subregionId].fill(dev_per_step if dev_per_step > 0 else 0) gcore.warning( _("For subregion {sub} population and development are inversely proportional," " demand will be interpolated based on prior change in development only." .format(sub=subregionId))) # draw if plot: ax = fig.add_subplot(n_plots, n_plots, i) ax.set_title("{sid}, RMSE: {rmse:.3f}".format(sid=subregionId, rmse=rmse[method])) ax.set_xlabel('population') ax.set_ylabel('developed cells') # plot known points x = np.array(observed_popul[subregionId]) y = np.array(table_developed[subregionId]) ax.plot(x, y, marker='o', linestyle='', markersize=8) # plot predicted curve x_pred = np.linspace( np.min(x), np.max(np.array(population_for_simulated_times[subregionId])), 30) cf = coeff[method] if method == 'linear': line = x_pred * cf[0] + cf[1] label = "$y = {c:.3f} + {m:.3f} x$".format(m=cf[0], c=cf[1]) elif method == 'logarithmic': line = np.log(x_pred) * cf[0] + cf[1] label = "$y = {c:.3f} + {m:.3f} \ln(x)$".format(m=cf[0], c=cf[1]) elif method == 'exponential': line = np.exp(x_pred * cf[0] + cf[1]) label = "$y = {c:.3f} e^{{{m:.3f}x}}$".format(m=cf[0], c=np.exp(cf[1])) elif method == 'exp_approach': line = exp_approach(x_pred / magn, *cf) * magn label = "$y = (1 - e^{{-{A:.3f}(x-{B:.3f})}}) + {C:.3f}$".format( A=cf[0], B=cf[1], C=cf[2]) elif method == 'logarithmic2': line = logarithmic2(x_pred / magn, *cf) * magn label = "$y = {A:.3f} + {B:.3f} \ln(x-{C:.3f})$".format( A=cf[0], B=cf[1], C=cf[2]) ax.plot(x_pred, line, label=label) ax.plot(simulated[method], predicted[method], linestyle='', marker='o', markerfacecolor='None') plt.legend(loc=0) labels = ax.get_xticklabels() plt.setp(labels, rotation=30) if plot: plt.tight_layout() fig.savefig(plot) # write demand with open(options['demand'], 'w') as f: header = observed_popul.dtype.names # the order is kept here header = [header[0] ] + [sub for sub in header[1:] if sub in subregionIds] f.write(sep.join(header)) f.write('\n') i = 0 for time in simulation_times[1:]: f.write(str(int(time))) f.write(sep) # put 0 where there are more counties but are not in region for sub in header[1:]: # to keep order of subregions f.write(str(int(demand[sub][i]))) if sub != header[-1]: f.write(sep) f.write('\n') i += 1
def main(): options, flags = grass.parser() elevation_input = options['elevation'] aspect_input = options['aspect'] slope_input = options['slope'] linke = options['linke'] linke_value = options['linke_value'] albedo = options['albedo'] albedo_value = options['albedo_value'] coeff_bh = options['coeff_bh'] coeff_bh_strds = options['coeff_bh_strds'] coeff_dh = options['coeff_dh'] coeff_dh_strds = options['coeff_dh_strds'] lat = options['lat'] long_ = options['long'] beam_rad_basename = beam_rad_basename_user = options['beam_rad_basename'] diff_rad_basename = diff_rad_basename_user = options['diff_rad_basename'] refl_rad_basename = refl_rad_basename_user = options['refl_rad_basename'] glob_rad_basename = glob_rad_basename_user = options['glob_rad_basename'] incidout_basename = options['incidout_basename'] beam_rad = options['beam_rad'] diff_rad = options['diff_rad'] refl_rad = options['refl_rad'] glob_rad = options['glob_rad'] has_output = any([ beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename, incidout_basename, beam_rad, diff_rad, refl_rad, glob_rad ]) if not has_output: grass.fatal(_("No output specified.")) start_time = float(options['start_time']) end_time = float(options['end_time']) time_step = float(options['time_step']) nprocs = int(options['nprocs']) day = int(options['day']) civil_time = float( options['civil_time']) if options['civil_time'] else None distance_step = float( options['distance_step']) if options['distance_step'] else None solar_constant = float( options['solar_constant']) if options['solar_constant'] else None if solar_constant: # check it's newer version of r.sun if not module_has_parameter('r.sun', 'solar_constant'): grass.warning( _("This version of r.sun lacks solar_constant option, " "it will be ignored. Use newer version of r.sun.")) solar_constant = None temporal = flags['t'] binary = flags['b'] mode1 = True if options['mode'] == 'mode1' else False mode2 = not mode1 tmpName = 'tmp' year = int(options['year']) rsun_flags = '' if flags['m']: rsun_flags += 'm' if flags['p']: rsun_flags += 'p' if not is_grass_7() and temporal: grass.warning(_("Flag t has effect only in GRASS 7")) # check: start < end if start_time > end_time: grass.fatal(_("Start time is after end time.")) if time_step >= end_time - start_time: grass.fatal(_("Time step is too big.")) if mode2 and incidout_basename: grass.fatal(_("Can't compute incidence angle in mode 2")) if flags['c'] and mode1: grass.fatal( _("Can't compute cumulative irradiation rasters in mode 1")) if mode2 and flags['b']: grass.fatal(_("Can't compute binary rasters in mode 2")) if any((beam_rad, diff_rad, refl_rad, glob_rad)) and mode1: grass.fatal(_("Can't compute irradiation raster maps in mode 1")) if beam_rad and not beam_rad_basename: beam_rad_basename = create_tmp_map_name('beam_rad') MREMOVE.append(beam_rad_basename) if diff_rad and not diff_rad_basename: diff_rad_basename = create_tmp_map_name('diff_rad') MREMOVE.append(diff_rad_basename) if refl_rad and not refl_rad_basename: refl_rad_basename = create_tmp_map_name('refl_rad') MREMOVE.append(refl_rad_basename) if glob_rad and not glob_rad_basename: glob_rad_basename = create_tmp_map_name('glob_rad') MREMOVE.append(glob_rad_basename) # here we check all the days if not grass.overwrite(): check_time_map_names(beam_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, tmpName, mode1) check_time_map_names(diff_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, tmpName, mode1) check_time_map_names(refl_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, tmpName, mode1) check_time_map_names(glob_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, tmpName, mode1) # check for slope/aspect if not aspect_input or not slope_input: params = {} if not aspect_input: aspect_input = create_tmp_map_name('aspect') params.update({'aspect': aspect_input}) REMOVE.append(aspect_input) if not slope_input: slope_input = create_tmp_map_name('slope') params.update({'slope': slope_input}) REMOVE.append(slope_input) grass.info(_("Running r.slope.aspect...")) grass.run_command('r.slope.aspect', elevation=elevation_input, quiet=True, **params) grass.info(_("Running r.sun in a loop...")) count = 0 # Parallel processing proc_list = [] proc_count = 0 suffixes = [] suffixes_all = [] if mode1: times = list(frange1(start_time, end_time, time_step)) else: times = list(frange2(start_time, end_time, time_step)) num_times = len(times) core.percent(0, num_times, 1) for time in times: count += 1 core.percent(count, num_times, 10) coeff_bh_raster = coeff_bh if coeff_bh_strds: coeff_bh_raster = get_raster_from_strds(year, day, time, strds=coeff_bh_strds) coeff_dh_raster = coeff_dh if coeff_dh_strds: coeff_dh_raster = get_raster_from_strds(year, day, time, strds=coeff_dh_strds) suffix = '_' + format_time(time) proc_list.append( Process(target=run_r_sun, args=(elevation_input, aspect_input, slope_input, day, time, civil_time, linke, linke_value, albedo, albedo_value, coeff_bh_raster, coeff_dh_raster, lat, long_, beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename, incidout_basename, suffix, binary, tmpName, None if mode1 else time_step, distance_step, solar_constant, rsun_flags))) proc_list[proc_count].start() proc_count += 1 suffixes.append(suffix) suffixes_all.append(suffix) if proc_count == nprocs or proc_count == num_times or count == num_times: proc_count = 0 exitcodes = 0 for proc in proc_list: proc.join() exitcodes += proc.exitcode if exitcodes != 0: core.fatal(_("Error while r.sun computation")) # Empty process list proc_list = [] suffixes = [] if beam_rad: sum_maps(beam_rad, beam_rad_basename, suffixes_all) set_color_table([beam_rad]) if diff_rad: sum_maps(diff_rad, diff_rad_basename, suffixes_all) set_color_table([diff_rad]) if refl_rad: sum_maps(refl_rad, refl_rad_basename, suffixes_all) set_color_table([refl_rad]) if glob_rad: sum_maps(glob_rad, glob_rad_basename, suffixes_all) set_color_table([glob_rad]) if not any([ beam_rad_basename_user, diff_rad_basename_user, refl_rad_basename_user, glob_rad_basename_user ]): return 0 # cumulative sum if flags['c']: copy_tmp = create_tmp_map_name('copy') REMOVE.append(copy_tmp) for each in (beam_rad_basename_user, diff_rad_basename_user, refl_rad_basename_user, glob_rad_basename_user): if each: previous = each + suffixes_all[0] for suffix in suffixes_all[1:]: new = each + suffix grass.run_command('g.copy', raster=[new, copy_tmp], quiet=True) grass.mapcalc("{new} = {previous} + {current}".format( new=new, previous=previous, current=copy_tmp), overwrite=True) previous = new # add timestamps either via temporal framework in 7 or r.timestamp in 6.x if is_grass_7() and temporal: core.info(_("Registering created maps into temporal dataset...")) import grass.temporal as tgis def registerToTemporal(basename, suffixes, mapset, start_time, time_step, title, desc): maps = ','.join( [basename + suf + '@' + mapset for suf in suffixes]) tgis.open_new_stds(basename, type='strds', temporaltype='absolute', title=title, descr=desc, semantic='mean', dbif=None, overwrite=grass.overwrite()) tgis.register_maps_in_space_time_dataset(type='raster', name=basename, maps=maps, start=start_time, end=None, increment=time_step, dbif=None, interval=False) # Make sure the temporal database exists tgis.init() mapset = grass.gisenv()['MAPSET'] if mode2: start_time += 0.5 * time_step absolute_time = datetime.datetime(year, 1, 1) + \ datetime.timedelta(days=day - 1) + \ datetime.timedelta(hours=start_time) start = absolute_time.strftime("%Y-%m-%d %H:%M:%S") step = datetime.timedelta(hours=time_step) step = "%d seconds" % step.seconds if beam_rad_basename_user: registerToTemporal( beam_rad_basename_user, suffixes_all, mapset, start, step, title="Beam irradiance" if mode1 else "Beam irradiation", desc="Output beam irradiance raster maps [W.m-2]" if mode1 else "Output beam irradiation raster maps [Wh.m-2]") if diff_rad_basename_user: registerToTemporal( diff_rad_basename_user, suffixes_all, mapset, start, step, title="Diffuse irradiance" if mode1 else "Diffuse irradiation", desc="Output diffuse irradiance raster maps [W.m-2]" if mode1 else "Output diffuse irradiation raster maps [Wh.m-2]") if refl_rad_basename_user: registerToTemporal( refl_rad_basename_user, suffixes_all, mapset, start, step, title="Reflected irradiance" if mode1 else "Reflected irradiation", desc="Output reflected irradiance raster maps [W.m-2]" if mode1 else "Output reflected irradiation raster maps [Wh.m-2]") if glob_rad_basename_user: registerToTemporal( glob_rad_basename_user, suffixes_all, mapset, start, step, title="Total irradiance" if mode1 else "Total irradiation", desc="Output total irradiance raster maps [W.m-2]" if mode1 else "Output total irradiation raster maps [Wh.m-2]") if incidout_basename: registerToTemporal(incidout_basename, suffixes_all, mapset, start, step, title="Incidence angle", desc="Output incidence angle raster maps") else: absolute_time = datetime.datetime(year, 1, 1) + \ datetime.timedelta(days=day - 1) for i, time in enumerate(times): grass_time = format_grass_time(absolute_time + datetime.timedelta(hours=time)) if beam_rad_basename_user: set_time_stamp(beam_rad_basename_user + suffixes_all[i], time=grass_time) if diff_rad_basename_user: set_time_stamp(diff_rad_basename_user + suffixes_all[i], time=grass_time) if refl_rad_basename_user: set_time_stamp(refl_rad_basename_user + suffixes_all[i], time=grass_time) if glob_rad_basename_user: set_time_stamp(glob_rad_basename_user + suffixes_all[i], time=grass_time) if incidout_basename: set_time_stamp(incidout_basename + suffixes_all[i], time=grass_time) if beam_rad_basename_user: maps = [beam_rad_basename_user + suf for suf in suffixes_all] set_color_table(maps, binary) if diff_rad_basename_user: maps = [diff_rad_basename_user + suf for suf in suffixes_all] set_color_table(maps, binary) if refl_rad_basename_user: maps = [refl_rad_basename_user + suf for suf in suffixes_all] set_color_table(maps, binary) if glob_rad_basename_user: maps = [glob_rad_basename_user + suf for suf in suffixes_all] set_color_table(maps, binary) if incidout_basename: maps = [incidout_basename + suf for suf in suffixes_all] set_color_table(maps)
def main(): options, flags = grass.parser() elevation_input = options['input'] pca_shade_output = options['output'] altitude = float(options['altitude']) number_of_azimuths = int(options['nazimuths']) z_exaggeration = float(options['zscale']) scale = float(options['scale']) units = options['units'] shades_basename = options['shades_basename'] pca_basename = pca_basename_user = options['pca_shades_basename'] nprocs = int(options['nprocs']) full_circle = 360 # let's use floats here and leave the consequences to the user smallest_angle = float(full_circle) / number_of_azimuths azimuths = list(frange(0, full_circle, smallest_angle)) if not shades_basename: shades_basename = create_tmp_map_name('shade') MREMOVE.append(shades_basename) if not pca_basename: pca_basename = pca_shade_output + '_pca' pca_maps = [ pca_basename + '.' + str(i) for i in range(1, number_of_azimuths + 1) ] if not pca_basename_user: REMOVE.extend(pca_maps) # here we check all the posible if not grass.overwrite(): check_map_names(shades_basename, grass.gisenv()['MAPSET'], suffixes=azimuths) check_map_names(pca_basename, grass.gisenv()['MAPSET'], suffixes=range(1, number_of_azimuths)) grass.info(_("Running r.relief in a loop...")) count = 0 # Parallel processing proc_list = [] proc_count = 0 suffixes = [] all_suffixes = [] core.percent(0, number_of_azimuths, 1) for azimuth in azimuths: count += 1 core.percent(count, number_of_azimuths, 10) suffix = '_' + str(azimuth) proc_list.append( Process(target=run_r_shaded_relief, args=(elevation_input, shades_basename, altitude, azimuth, z_exaggeration, scale, units, suffix))) proc_list[proc_count].start() proc_count += 1 suffixes.append(suffix) all_suffixes.append(suffix) if proc_count == nprocs or proc_count == number_of_azimuths \ or count == number_of_azimuths: proc_count = 0 exitcodes = 0 for proc in proc_list: proc.join() exitcodes += proc.exitcode if exitcodes != 0: core.fatal(_("Error during r.relief computation")) # Empty process list proc_list = [] suffixes = [] # FIXME: how percent really works? # core.percent(1, 1, 1) shade_maps = [shades_basename + suf for suf in all_suffixes] grass.info(_("Running r.pca...")) # not quiet=True to get percents grass.run_command('i.pca', input=shade_maps, output=pca_basename, overwrite=core.overwrite()) grass.info( _("Creating RGB composite from " "PC1 (red), PC2 (green), PC3 (blue) ...")) grass.run_command('r.composite', red=pca_maps[0], green=pca_maps[1], blue=pca_maps[2], output=pca_shade_output, overwrite=core.overwrite(), quiet=True) grass.raster_history(pca_shade_output) if pca_basename_user: set_color_table(pca_maps, map_=shade_maps[0])
def main(): """main function, called at execution time""" # parse arguments temp_maps = options['temp'].split(',') prec_maps = options['prec'].split(',') stdv_maps = options['stdv'].split(',') # check that we have compatible number of input maps ntemp = len(temp_maps) nprec = len(prec_maps) nstdv = len(stdv_maps) if nprec not in (1, ntemp): grass.fatal('Got %i prec maps, expected 1 (constant) or %i (as temp)' % (nprec, ntemp)) if nstdv not in (1, ntemp): grass.fatal('Got %i stdv maps, expected 1 (constant) or %i (as temp)' % (nstdv, ntemp)) # exit if no output is requested out_vars = ['pdd', 'accu', 'snow_melt', 'ice_melt', 'melt', 'runoff', 'smb'] out_maps = {var: options[var] for var in out_vars if options[var] != ''} if len(out_maps) == 0: grass.fatal('No output required. Please inform at least one of ' + ', '.join(out_vars) + '.') # read temperature maps grass.info('reading temperature maps...') temp = [garray.array() for m in temp_maps] for i, m in enumerate(temp_maps): temp[i].read(m) grass.percent(i, ntemp, 1) temp = np.asarray(temp) # read precipitation maps grass.info('reading precipitation maps...') prec = [garray.array() for m in temp_maps] for i, m in enumerate(prec_maps): prec[i].read(m) grass.percent(i, nprec, 1) prec = np.asarray(prec) # read standard deviation maps if stdv_maps != ['']: grass.info('reading standard deviation maps...') stdv = [garray.array() for m in stdv_maps] for i, m in enumerate(stdv_maps): stdv[i].read(m) grass.percent(i, nstdv, 1) stdv = np.asarray(stdv) else: stdv = 0.0 # initialize PDD model pdd = PDDModel() for param in ('pdd_factor_snow', 'pdd_factor_ice', 'refreeze_snow', 'refreeze_ice', 'temp_snow', 'temp_rain', 'interpolate_rule', 'interpolate_n'): if options[param]: setattr(pdd, param, float(options[param])) for param in ('interpolate_rule',): if options[param]: setattr(pdd, param, str(options[param])) # run PDD model grass.info('running PDD model...') smb = pdd(temp, prec, stdv) # write output maps grass.info('writing output maps...') for varname in ['pdd', 'accu', 'snow_melt', 'ice_melt', 'melt', 'runoff', 'smb']: if options[varname]: a = garray.array() a[:] = smb[varname] a.write(mapname=options[varname])
def main(): """main function, called at execution time""" # parse arguments temp_maps = options['temp'].split(',') prec_maps = options['prec'].split(',') stdv_maps = options['stdv'].split(',') # check that we have compatible number of input maps ntemp = len(temp_maps) nprec = len(prec_maps) nstdv = len(stdv_maps) if nprec not in (1, ntemp): grass.fatal('Got %i prec maps, expected 1 (constant) or %i (as temp)' % (nprec, ntemp)) if nstdv not in (1, ntemp): grass.fatal('Got %i stdv maps, expected 1 (constant) or %i (as temp)' % (nstdv, ntemp)) # exit if no output is requested out_vars = [ 'pdd', 'accu', 'snow_melt', 'ice_melt', 'melt', 'runoff', 'smb' ] out_maps = {var: options[var] for var in out_vars if options[var] != ''} if len(out_maps) == 0: grass.fatal('No output required. Please inform at least one of ' + ', '.join(out_vars) + '.') # read temperature maps grass.info('reading temperature maps...') temp = [garray.array() for m in temp_maps] for i, m in enumerate(temp_maps): temp[i].read(m) grass.percent(i, ntemp, 1) temp = np.asarray(temp) # read precipitation maps grass.info('reading precipitation maps...') prec = [garray.array() for m in temp_maps] for i, m in enumerate(prec_maps): prec[i].read(m) grass.percent(i, nprec, 1) prec = np.asarray(prec) # read standard deviation maps if stdv_maps != ['']: grass.info('reading standard deviation maps...') stdv = [garray.array() for m in stdv_maps] for i, m in enumerate(stdv_maps): stdv[i].read(m) grass.percent(i, nstdv, 1) stdv = np.asarray(stdv) else: stdv = 0.0 # initialize PDD model pdd = PDDModel() for param in ('pdd_factor_snow', 'pdd_factor_ice', 'refreeze_snow', 'refreeze_ice', 'temp_snow', 'temp_rain', 'interpolate_rule', 'interpolate_n'): if options[param]: setattr(pdd, param, float(options[param])) for param in ('interpolate_rule', ): if options[param]: setattr(pdd, param, str(options[param])) # run PDD model grass.info('running PDD model...') smb = pdd(temp, prec, stdv) # write output maps grass.info('writing output maps...') for varname in [ 'pdd', 'accu', 'snow_melt', 'ice_melt', 'melt', 'runoff', 'smb' ]: if options[varname]: a = garray.array() a[:] = smb[varname] a.write(mapname=options[varname])
def main(): options, flags = grass.parser() # required elevation_input = options['elevation'] aspect_input = options['aspect'] slope_input = options['slope'] # optional latitude = options['lat'] longitude = options['long'] linke_input = options['linke'] linke_value = options['linke_value'] albedo_input = options['albedo'] albedo_value = options['albedo_value'] horizon_basename = options['horizon_basename'] horizon_step = options['horizon_step'] # outputs beam_rad = options['beam_rad'] diff_rad = options['diff_rad'] refl_rad = options['refl_rad'] glob_rad = options['glob_rad'] beam_rad_basename = beam_rad_basename_user = options['beam_rad_basename'] diff_rad_basename = diff_rad_basename_user = options['diff_rad_basename'] refl_rad_basename = refl_rad_basename_user = options['refl_rad_basename'] glob_rad_basename = glob_rad_basename_user = options['glob_rad_basename'] # missing output? if not any([ beam_rad, diff_rad, refl_rad, glob_rad, beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename ]): grass.fatal(_("No output specified.")) start_day = int(options['start_day']) end_day = int(options['end_day']) day_step = int(options['day_step']) if day_step > 1 and (beam_rad or diff_rad or refl_rad or glob_rad): grass.fatal( _("Day step higher then 1 would produce" " meaningless cumulative maps.")) # check: start < end if start_day > end_day: grass.fatal(_("Start day is after end day.")) if day_step >= end_day - start_day: grass.fatal(_("Day step is too big.")) step = float(options['step']) nprocs = int(options['nprocs']) solar_constant = float( options['solar_constant']) if options['solar_constant'] else None if solar_constant: # check it's newer version of r.sun if not module_has_parameter('r.sun', 'solar_constant'): grass.warning( _("This version of r.sun lacks solar_constant option, " "it will be ignored. Use newer version of r.sun.")) solar_constant = None if beam_rad and not beam_rad_basename: beam_rad_basename = create_tmp_map_name('beam_rad') MREMOVE.append(beam_rad_basename) if diff_rad and not diff_rad_basename: diff_rad_basename = create_tmp_map_name('diff_rad') MREMOVE.append(diff_rad_basename) if refl_rad and not refl_rad_basename: refl_rad_basename = create_tmp_map_name('refl_rad') MREMOVE.append(refl_rad_basename) if glob_rad and not glob_rad_basename: glob_rad_basename = create_tmp_map_name('glob_rad') MREMOVE.append(glob_rad_basename) # check for existing identical map names if not grass.overwrite(): check_daily_map_names(beam_rad_basename, grass.gisenv()['MAPSET'], start_day, end_day, day_step) check_daily_map_names(diff_rad_basename, grass.gisenv()['MAPSET'], start_day, end_day, day_step) check_daily_map_names(refl_rad_basename, grass.gisenv()['MAPSET'], start_day, end_day, day_step) check_daily_map_names(glob_rad_basename, grass.gisenv()['MAPSET'], start_day, end_day, day_step) # check for slope/aspect if not aspect_input or not slope_input: params = {} if not aspect_input: aspect_input = create_tmp_map_name('aspect') params.update({'aspect': aspect_input}) REMOVE.append(aspect_input) if not slope_input: slope_input = create_tmp_map_name('slope') params.update({'slope': slope_input}) REMOVE.append(slope_input) grass.info(_("Running r.slope.aspect...")) grass.run_command('r.slope.aspect', elevation=elevation_input, quiet=True, **params) if beam_rad: grass.mapcalc('{beam} = 0'.format(beam=beam_rad), quiet=True) if diff_rad: grass.mapcalc('{diff} = 0'.format(diff=diff_rad), quiet=True) if refl_rad: grass.mapcalc('{refl} = 0'.format(refl=refl_rad), quiet=True) if glob_rad: grass.mapcalc('{glob} = 0'.format(glob=glob_rad), quiet=True) rsun_flags = '' if flags['m']: rsun_flags += 'm' if flags['p']: rsun_flags += 'p' grass.info(_("Running r.sun in a loop...")) count = 0 # Parallel processing proc_list = [] proc_count = 0 suffixes_all = [] days = range(start_day, end_day + 1, day_step) num_days = len(days) core.percent(0, num_days, 1) for day in days: count += 1 core.percent(count, num_days, 10) suffix = '_' + format_order(day) proc_list.append( Process(target=run_r_sun, args=(elevation_input, aspect_input, slope_input, latitude, longitude, linke_input, linke_value, albedo_input, albedo_value, horizon_basename, horizon_step, solar_constant, day, step, beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename, suffix, rsun_flags))) proc_list[proc_count].start() proc_count += 1 suffixes_all.append(suffix) if proc_count == nprocs or proc_count == num_days or count == num_days: proc_count = 0 exitcodes = 0 for proc in proc_list: proc.join() exitcodes += proc.exitcode if exitcodes != 0: core.fatal(_("Error while r.sun computation")) # Empty process list proc_list = [] if beam_rad: sum_maps(beam_rad, beam_rad_basename, suffixes_all) if diff_rad: sum_maps(diff_rad, diff_rad_basename, suffixes_all) if refl_rad: sum_maps(refl_rad, refl_rad_basename, suffixes_all) if glob_rad: sum_maps(glob_rad, glob_rad_basename, suffixes_all) # FIXME: how percent really works? # core.percent(1, 1, 1) # set color table if beam_rad: set_color_table([beam_rad]) if diff_rad: set_color_table([diff_rad]) if refl_rad: set_color_table([refl_rad]) if glob_rad: set_color_table([glob_rad]) if not any([ beam_rad_basename_user, diff_rad_basename_user, refl_rad_basename_user, glob_rad_basename_user ]): return 0 # add timestamps and register to spatio-temporal raster data set temporal = flags['t'] if temporal: core.info(_("Registering created maps into temporal dataset...")) import grass.temporal as tgis def registerToTemporal(basename, suffixes, mapset, start_day, day_step, title, desc): """ Register daily output maps in spatio-temporal raster data set """ maps = ','.join( [basename + suf + '@' + mapset for suf in suffixes]) tgis.open_new_stds(basename, type='strds', temporaltype='relative', title=title, descr=desc, semantic='sum', dbif=None, overwrite=grass.overwrite()) tgis.register_maps_in_space_time_dataset(type='rast', name=basename, maps=maps, start=start_day, end=None, unit='days', increment=day_step, dbif=None, interval=False) # Make sure the temporal database exists tgis.init() mapset = grass.gisenv()['MAPSET'] if beam_rad_basename_user: registerToTemporal( beam_rad_basename, suffixes_all, mapset, start_day, day_step, title="Beam irradiation", desc="Output beam irradiation raster maps [Wh.m-2.day-1]") if diff_rad_basename_user: registerToTemporal( diff_rad_basename, suffixes_all, mapset, start_day, day_step, title="Diffuse irradiation", desc="Output diffuse irradiation raster maps [Wh.m-2.day-1]") if refl_rad_basename_user: registerToTemporal( refl_rad_basename, suffixes_all, mapset, start_day, day_step, title="Reflected irradiation", desc="Output reflected irradiation raster maps [Wh.m-2.day-1]") if glob_rad_basename_user: registerToTemporal( glob_rad_basename, suffixes_all, mapset, start_day, day_step, title="Total irradiation", desc="Output total irradiation raster maps [Wh.m-2.day-1]") # just add timestamps, don't register else: for i, day in enumerate(days): if beam_rad_basename_user: set_time_stamp(beam_rad_basename + suffixes_all[i], day=day) if diff_rad_basename_user: set_time_stamp(diff_rad_basename + suffixes_all[i], day=day) if refl_rad_basename_user: set_time_stamp(refl_rad_basename + suffixes_all[i], day=day) if glob_rad_basename_user: set_time_stamp(glob_rad_basename + suffixes_all[i], day=day) # set color table for daily maps if beam_rad_basename_user: maps = [beam_rad_basename + suf for suf in suffixes_all] set_color_table(maps) if diff_rad_basename_user: maps = [diff_rad_basename + suf for suf in suffixes_all] set_color_table(maps) if refl_rad_basename_user: maps = [refl_rad_basename + suf for suf in suffixes_all] set_color_table(maps) if glob_rad_basename_user: maps = [glob_rad_basename + suf for suf in suffixes_all] set_color_table(maps)
def main(): options, flags = grass.parser() elevation_input = options['elevation'] aspect_input = options['aspect'] slope_input = options['slope'] linke = options['linke'] linke_value = options['linke_value'] albedo = options['albedo'] albedo_value = options['albedo_value'] horizon_basename = options['horizon_basename'] horizon_step = options['horizon_step'] beam_rad_basename = options['beam_rad_basename'] diff_rad_basename = options['diff_rad_basename'] refl_rad_basename = options['refl_rad_basename'] glob_rad_basename = options['glob_rad_basename'] incidout_basename = options['incidout_basename'] if not any([beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename, incidout_basename]): grass.fatal(_("No output specified.")) start_time = float(options['start_time']) end_time = float(options['end_time']) time_step = float(options['time_step']) nprocs = int(options['nprocs']) day = int(options['day']) temporal = flags['t'] binary = flags['b'] binaryTmpName = 'binary' year = int(options['year']) if not is_grass_7() and temporal: grass.warning(_("Flag t has effect only in GRASS 7")) # check: start < end if start_time > end_time: grass.fatal(_("Start time is after end time.")) if time_step >= end_time - start_time: grass.fatal(_("Time step is too big.")) # here we check all the days if not grass.overwrite(): check_time_map_names(beam_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, binaryTmpName) check_time_map_names(diff_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, binaryTmpName) check_time_map_names(refl_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, binaryTmpName) check_time_map_names(glob_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, binaryTmpName) # check for slope/aspect if not aspect_input or not slope_input: params = {} if not aspect_input: aspect_input = create_tmp_map_name('aspect') params.update({'aspect': aspect_input}) TMP.append(aspect_input) if not slope_input: slope_input = create_tmp_map_name('slope') params.update({'slope': slope_input}) TMP.append(slope_input) grass.info(_("Running r.slope.aspect...")) grass.run_command('r.slope.aspect', elevation=elevation_input, quiet=True, **params) grass.info(_("Running r.sun in a loop...")) count = 0 # Parallel processing proc_list = [] proc_count = 0 suffixes = [] suffixes_all = [] times = list(frange(start_time, end_time, time_step)) num_times = len(times) core.percent(0, num_times, 1) for time in times: count += 1 core.percent(count, num_times, 10) suffix = '_' + format_time(time) proc_list.append(Process(target=run_r_sun, args=(elevation_input, aspect_input, slope_input, day, time, linke, linke_value, albedo, albedo_value, horizon_basename, horizon_step, beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename, incidout_basename, suffix, binary, binaryTmpName))) proc_list[proc_count].start() proc_count += 1 suffixes.append(suffix) suffixes_all.append(suffix) if proc_count == nprocs or proc_count == num_times or count == num_times: proc_count = 0 exitcodes = 0 for proc in proc_list: proc.join() exitcodes += proc.exitcode if exitcodes != 0: core.fatal(_("Error while r.sun computation")) # Empty process list proc_list = [] suffixes = [] # FIXME: how percent really works? # core.percent(1, 1, 1) # add timestamps either via temporal framework in 7 or r.timestamp in 6.x if is_grass_7() and temporal: core.info(_("Registering created maps into temporal dataset...")) import grass.temporal as tgis def registerToTemporal(basename, suffixes, mapset, start_time, time_step, title, desc): maps = ','.join([basename + suf + '@' + mapset for suf in suffixes]) tgis.open_new_stds(basename, type='strds', temporaltype='absolute', title=title, descr=desc, semantic='mean', dbif=None, overwrite=grass.overwrite()) tgis.register_maps_in_space_time_dataset( type='raster', name=basename, maps=maps, start=start_time, end=None, increment=time_step, dbif=None, interval=False) # Make sure the temporal database exists tgis.init() mapset = grass.gisenv()['MAPSET'] absolute_time = datetime.datetime(year, 1, 1) + \ datetime.timedelta(days=day - 1) + \ datetime.timedelta(hours=start_time) start = absolute_time.strftime("%Y-%m-%d %H:%M:%S") step = datetime.timedelta(hours=time_step) step = "%d seconds" % step.seconds if beam_rad_basename: registerToTemporal(beam_rad_basename, suffixes_all, mapset, start, step, title="Beam irradiance", desc="Output beam irradiance raster maps [Wh.m-2]") if diff_rad_basename: registerToTemporal(diff_rad_basename, suffixes_all, mapset, start, step, title="Diffuse irradiance", desc="Output diffuse irradiance raster maps [Wh.m-2]") if refl_rad_basename: registerToTemporal(refl_rad_basename, suffixes_all, mapset, start, step, title="Reflected irradiance", desc="Output reflected irradiance raster maps [Wh.m-2]") if glob_rad_basename: registerToTemporal(glob_rad_basename, suffixes_all, mapset, start, step, title="Total irradiance", desc="Output total irradiance raster maps [Wh.m-2]") if incidout_basename: registerToTemporal(incidout_basename, suffixes_all, mapset, start, step, title="Incidence angle", desc="Output incidence angle raster maps") else: absolute_time = datetime.datetime(year, 1, 1) + \ datetime.timedelta(days=day - 1) for i, time in enumerate(times): grass_time = format_grass_time(absolute_time + datetime.timedelta(hours=time)) if beam_rad_basename: set_time_stamp(beam_rad_basename + suffixes_all[i], time=grass_time) if diff_rad_basename: set_time_stamp(diff_rad_basename + suffixes_all[i], time=grass_time) if refl_rad_basename: set_time_stamp(refl_rad_basename + suffixes_all[i], time=grass_time) if glob_rad_basename: set_time_stamp(glob_rad_basename + suffixes_all[i], time=grass_time) if incidout_basename: set_time_stamp(incidout_basename + suffixes_all[i], time=grass_time) if beam_rad_basename: maps = [beam_rad_basename + suf for suf in suffixes_all] set_color_table(maps, binary) if diff_rad_basename: maps = [diff_rad_basename + suf for suf in suffixes_all] set_color_table(maps, binary) if refl_rad_basename: maps = [refl_rad_basename + suf for suf in suffixes_all] set_color_table(maps, binary) if glob_rad_basename: maps = [glob_rad_basename + suf for suf in suffixes_all] set_color_table(maps, binary) if incidout_basename: maps = [incidout_basename + suf for suf in suffixes_all] set_color_table(maps)
def main(): options, flags = grass.parser() elevation_input = options['elevation'] aspect_input = options['aspect'] slope_input = options['slope'] linke = options['linke'] linke_value = options['linke_value'] albedo = options['albedo'] albedo_value = options['albedo_value'] beam_rad_basename = options['beam_rad_basename'] diff_rad_basename = options['diff_rad_basename'] refl_rad_basename = options['refl_rad_basename'] glob_rad_basename = options['glob_rad_basename'] incidout_basename = options['incidout_basename'] if not any([beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename, incidout_basename]): grass.fatal(_("No output specified.")) start_time = float(options['start_time']) end_time = float(options['end_time']) time_step = float(options['time_step']) nprocs = int(options['nprocs']) day = int(options['day']) temporal = flags['t'] binary = flags['b'] binaryTmpName = 'binary' year = int(options['year']) if not is_grass_7() and temporal: grass.warning(_("Flag t has effect only in GRASS 7")) # check: start < end if start_time > end_time: grass.fatal(_("Start time is after end time.")) if time_step >= end_time - start_time: grass.fatal(_("Time step is too big.")) # here we check all the days if not grass.overwrite(): check_time_map_names(beam_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, binaryTmpName) check_time_map_names(diff_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, binaryTmpName) check_time_map_names(refl_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, binaryTmpName) check_time_map_names(glob_rad_basename, grass.gisenv()['MAPSET'], start_time, end_time, time_step, binary, binaryTmpName) # check for slope/aspect if not aspect_input or not slope_input: params = {} if not aspect_input: aspect_input = create_tmp_map_name('aspect') params.update({'aspect': aspect_input}) TMP.append(aspect_input) if not slope_input: slope_input = create_tmp_map_name('slope') params.update({'slope': slope_input}) TMP.append(slope_input) grass.info(_("Running r.slope.aspect...")) grass.run_command('r.slope.aspect', elevation=elevation_input, quiet=True, **params) grass.info(_("Running r.sun in a loop...")) count = 0 # Parallel processing proc_list = [] proc_count = 0 suffixes = [] suffixes_all = [] times = list(frange(start_time, end_time, time_step)) num_times = len(times) core.percent(0, num_times, 1) for time in times: count += 1 core.percent(count, num_times, 10) suffix = '_' + format_time(time) proc_list.append(Process(target=run_r_sun, args=(elevation_input, aspect_input, slope_input, day, time, linke, linke_value, albedo, albedo_value, beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename, incidout_basename, suffix, binary, binaryTmpName))) proc_list[proc_count].start() proc_count += 1 suffixes.append(suffix) suffixes_all.append(suffix) if proc_count == nprocs or proc_count == num_times or count == num_times: proc_count = 0 exitcodes = 0 for proc in proc_list: proc.join() exitcodes += proc.exitcode if exitcodes != 0: core.fatal(_("Error while r.sun computation")) # Empty process list proc_list = [] suffixes = [] # FIXME: how percent really works? # core.percent(1, 1, 1) # add timestamps either via temporal framework in 7 or r.timestamp in 6.x if is_grass_7() and temporal: core.info(_("Registering created maps into temporal dataset...")) import grass.temporal as tgis def registerToTemporal(basename, suffixes, mapset, start_time, time_step, title, desc): maps = ','.join([basename + suf + '@' + mapset for suf in suffixes]) tgis.open_new_stds(basename, type='strds', temporaltype='absolute', title=title, descr=desc, semantic='mean', dbif=None, overwrite=grass.overwrite()) tgis.register_maps_in_space_time_dataset( type='raster', name=basename, maps=maps, start=start_time, end=None, increment=time_step, dbif=None, interval=False) # Make sure the temporal database exists tgis.init() mapset = grass.gisenv()['MAPSET'] absolute_time = datetime.datetime(year, 1, 1) + \ datetime.timedelta(days=day - 1) + \ datetime.timedelta(hours=start_time) start = absolute_time.strftime("%Y-%m-%d %H:%M:%S") step = datetime.timedelta(hours=time_step) step = "%d seconds" % step.seconds if beam_rad_basename: registerToTemporal(beam_rad_basename, suffixes_all, mapset, start, step, title="Beam irradiance", desc="Output beam irradiance raster maps [Wh.m-2]") if diff_rad_basename: registerToTemporal(diff_rad_basename, suffixes_all, mapset, start, step, title="Diffuse irradiance", desc="Output diffuse irradiance raster maps [Wh.m-2]") if refl_rad_basename: registerToTemporal(refl_rad_basename, suffixes_all, mapset, start, step, title="Reflected irradiance", desc="Output reflected irradiance raster maps [Wh.m-2]") if glob_rad_basename: registerToTemporal(glob_rad_basename, suffixes_all, mapset, start, step, title="Total irradiance", desc="Output total irradiance raster maps [Wh.m-2]") if incidout_basename: registerToTemporal(incidout_basename, suffixes_all, mapset, start, step, title="Incidence angle", desc="Output incidence angle raster maps") else: absolute_time = datetime.datetime(year, 1, 1) + \ datetime.timedelta(days=day - 1) for i, time in enumerate(times): grass_time = format_grass_time(absolute_time + datetime.timedelta(hours=time)) if beam_rad_basename: set_time_stamp(beam_rad_basename + suffixes_all[i], time=grass_time) if diff_rad_basename: set_time_stamp(diff_rad_basename + suffixes_all[i], time=grass_time) if refl_rad_basename: set_time_stamp(refl_rad_basename + suffixes_all[i], time=grass_time) if glob_rad_basename: set_time_stamp(glob_rad_basename + suffixes_all[i], time=grass_time) if incidout_basename: set_time_stamp(incidout_basename + suffixes_all[i], time=grass_time) if beam_rad_basename: maps = [beam_rad_basename + suf for suf in suffixes_all] set_color_table(maps, binary) if diff_rad_basename: maps = [diff_rad_basename + suf for suf in suffixes_all] set_color_table(maps, binary) if refl_rad_basename: maps = [refl_rad_basename + suf for suf in suffixes_all] set_color_table(maps, binary) if glob_rad_basename: maps = [glob_rad_basename + suf for suf in suffixes_all] set_color_table(maps, binary) if incidout_basename: maps = [incidout_basename + suf for suf in suffixes_all] set_color_table(maps)
def main(): options, flags = grass.parser() elevation_input = options["elevation"] aspect_input = options["aspect"] slope_input = options["slope"] linke = options["linke"] linke_value = options["linke_value"] albedo = options["albedo"] albedo_value = options["albedo_value"] coeff_bh = options["coeff_bh"] coeff_bh_strds = options["coeff_bh_strds"] coeff_dh = options["coeff_dh"] coeff_dh_strds = options["coeff_dh_strds"] lat = options["lat"] long_ = options["long"] beam_rad_basename = beam_rad_basename_user = options["beam_rad_basename"] diff_rad_basename = diff_rad_basename_user = options["diff_rad_basename"] refl_rad_basename = refl_rad_basename_user = options["refl_rad_basename"] glob_rad_basename = glob_rad_basename_user = options["glob_rad_basename"] incidout_basename = options["incidout_basename"] beam_rad = options["beam_rad"] diff_rad = options["diff_rad"] refl_rad = options["refl_rad"] glob_rad = options["glob_rad"] has_output = any([ beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename, incidout_basename, beam_rad, diff_rad, refl_rad, glob_rad, ]) if not has_output: grass.fatal(_("No output specified.")) start_time = float(options["start_time"]) end_time = float(options["end_time"]) time_step = float(options["time_step"]) nprocs = int(options["nprocs"]) day = int(options["day"]) civil_time = float( options["civil_time"]) if options["civil_time"] else None distance_step = (float(options["distance_step"]) if options["distance_step"] else None) solar_constant = (float(options["solar_constant"]) if options["solar_constant"] else None) temporal = flags["t"] binary = flags["b"] mode1 = True if options["mode"] == "mode1" else False mode2 = not mode1 tmpName = "tmp" year = int(options["year"]) rsun_flags = "" if flags["m"]: rsun_flags += "m" if flags["p"]: rsun_flags += "p" # check: start < end if start_time > end_time: grass.fatal(_("Start time is after end time.")) if time_step >= end_time - start_time: grass.fatal(_("Time step is too big.")) if mode2 and incidout_basename: grass.fatal(_("Can't compute incidence angle in mode 2")) if flags["c"] and mode1: grass.fatal( _("Can't compute cumulative irradiation rasters in mode 1")) if mode2 and flags["b"]: grass.fatal(_("Can't compute binary rasters in mode 2")) if any((beam_rad, diff_rad, refl_rad, glob_rad)) and mode1: grass.fatal(_("Can't compute irradiation raster maps in mode 1")) if beam_rad and not beam_rad_basename: beam_rad_basename = create_tmp_map_name("beam_rad") MREMOVE.append(beam_rad_basename) if diff_rad and not diff_rad_basename: diff_rad_basename = create_tmp_map_name("diff_rad") MREMOVE.append(diff_rad_basename) if refl_rad and not refl_rad_basename: refl_rad_basename = create_tmp_map_name("refl_rad") MREMOVE.append(refl_rad_basename) if glob_rad and not glob_rad_basename: glob_rad_basename = create_tmp_map_name("glob_rad") MREMOVE.append(glob_rad_basename) # here we check all the days if not grass.overwrite(): check_time_map_names( beam_rad_basename, grass.gisenv()["MAPSET"], start_time, end_time, time_step, binary, tmpName, mode1, ) check_time_map_names( diff_rad_basename, grass.gisenv()["MAPSET"], start_time, end_time, time_step, binary, tmpName, mode1, ) check_time_map_names( refl_rad_basename, grass.gisenv()["MAPSET"], start_time, end_time, time_step, binary, tmpName, mode1, ) check_time_map_names( glob_rad_basename, grass.gisenv()["MAPSET"], start_time, end_time, time_step, binary, tmpName, mode1, ) # check for slope/aspect if not aspect_input or not slope_input: params = {} if not aspect_input: aspect_input = create_tmp_map_name("aspect") params.update({"aspect": aspect_input}) REMOVE.append(aspect_input) if not slope_input: slope_input = create_tmp_map_name("slope") params.update({"slope": slope_input}) REMOVE.append(slope_input) grass.info(_("Running r.slope.aspect...")) grass.run_command("r.slope.aspect", elevation=elevation_input, quiet=True, **params) grass.info(_("Running r.sun in a loop...")) count = 0 # Parallel processing proc_list = [] proc_count = 0 suffixes = [] suffixes_all = [] if mode1: times = list(frange1(start_time, end_time, time_step)) else: times = list(frange2(start_time, end_time, time_step)) num_times = len(times) core.percent(0, num_times, 1) for time in times: count += 1 core.percent(count, num_times, 10) coeff_bh_raster = coeff_bh if coeff_bh_strds: coeff_bh_raster = get_raster_from_strds(year, day, time, strds=coeff_bh_strds) coeff_dh_raster = coeff_dh if coeff_dh_strds: coeff_dh_raster = get_raster_from_strds(year, day, time, strds=coeff_dh_strds) suffix = "_" + format_time(time) proc_list.append( Process( target=run_r_sun, args=( elevation_input, aspect_input, slope_input, day, time, civil_time, linke, linke_value, albedo, albedo_value, coeff_bh_raster, coeff_dh_raster, lat, long_, beam_rad_basename, diff_rad_basename, refl_rad_basename, glob_rad_basename, incidout_basename, suffix, binary, tmpName, None if mode1 else time_step, distance_step, solar_constant, rsun_flags, ), )) proc_list[proc_count].start() proc_count += 1 suffixes.append(suffix) suffixes_all.append(suffix) if proc_count == nprocs or proc_count == num_times or count == num_times: proc_count = 0 exitcodes = 0 for proc in proc_list: proc.join() exitcodes += proc.exitcode if exitcodes != 0: core.fatal(_("Error while r.sun computation")) # Empty process list proc_list = [] suffixes = [] if beam_rad: sum_maps(beam_rad, beam_rad_basename, suffixes_all) set_color_table([beam_rad]) if diff_rad: sum_maps(diff_rad, diff_rad_basename, suffixes_all) set_color_table([diff_rad]) if refl_rad: sum_maps(refl_rad, refl_rad_basename, suffixes_all) set_color_table([refl_rad]) if glob_rad: sum_maps(glob_rad, glob_rad_basename, suffixes_all) set_color_table([glob_rad]) if not any([ beam_rad_basename_user, diff_rad_basename_user, refl_rad_basename_user, glob_rad_basename_user, ]): return 0 # cumulative sum if flags["c"]: copy_tmp = create_tmp_map_name("copy") REMOVE.append(copy_tmp) for each in ( beam_rad_basename_user, diff_rad_basename_user, refl_rad_basename_user, glob_rad_basename_user, ): if each: previous = each + suffixes_all[0] for suffix in suffixes_all[1:]: new = each + suffix grass.run_command("g.copy", raster=[new, copy_tmp], quiet=True) grass.mapcalc( "{new} = {previous} + {current}".format( new=new, previous=previous, current=copy_tmp), overwrite=True, ) previous = new # add to temporal framework if temporal: core.info(_("Registering created maps into temporal dataset...")) import grass.temporal as tgis def registerToTemporal(basename, suffixes, mapset, start_time, time_step, title, desc): maps = ",".join( [basename + suf + "@" + mapset for suf in suffixes]) tgis.open_new_stds( basename, type="strds", temporaltype="absolute", title=title, descr=desc, semantic="mean", dbif=None, overwrite=grass.overwrite(), ) tgis.register_maps_in_space_time_dataset( type="raster", name=basename, maps=maps, start=start_time, end=None, increment=time_step, dbif=None, interval=False, ) # Make sure the temporal database exists tgis.init() mapset = grass.gisenv()["MAPSET"] if mode2: start_time += 0.5 * time_step absolute_time = (datetime.datetime(year, 1, 1) + datetime.timedelta(days=day - 1) + datetime.timedelta(hours=start_time)) start = absolute_time.strftime("%Y-%m-%d %H:%M:%S") step = datetime.timedelta(hours=time_step) step = "%d seconds" % step.seconds if beam_rad_basename_user: registerToTemporal( beam_rad_basename_user, suffixes_all, mapset, start, step, title="Beam irradiance" if mode1 else "Beam irradiation", desc="Output beam irradiance raster maps [W.m-2]" if mode1 else "Output beam irradiation raster maps [Wh.m-2]", ) if diff_rad_basename_user: registerToTemporal( diff_rad_basename_user, suffixes_all, mapset, start, step, title="Diffuse irradiance" if mode1 else "Diffuse irradiation", desc="Output diffuse irradiance raster maps [W.m-2]" if mode1 else "Output diffuse irradiation raster maps [Wh.m-2]", ) if refl_rad_basename_user: registerToTemporal( refl_rad_basename_user, suffixes_all, mapset, start, step, title="Reflected irradiance" if mode1 else "Reflected irradiation", desc="Output reflected irradiance raster maps [W.m-2]" if mode1 else "Output reflected irradiation raster maps [Wh.m-2]", ) if glob_rad_basename_user: registerToTemporal( glob_rad_basename_user, suffixes_all, mapset, start, step, title="Total irradiance" if mode1 else "Total irradiation", desc="Output total irradiance raster maps [W.m-2]" if mode1 else "Output total irradiation raster maps [Wh.m-2]", ) if incidout_basename: registerToTemporal( incidout_basename, suffixes_all, mapset, start, step, title="Incidence angle", desc="Output incidence angle raster maps", ) else: absolute_time = datetime.datetime(year, 1, 1) + datetime.timedelta(days=day - 1) for i, time in enumerate(times): grass_time = format_grass_time(absolute_time + datetime.timedelta(hours=time)) if beam_rad_basename_user: set_time_stamp(beam_rad_basename_user + suffixes_all[i], time=grass_time) if diff_rad_basename_user: set_time_stamp(diff_rad_basename_user + suffixes_all[i], time=grass_time) if refl_rad_basename_user: set_time_stamp(refl_rad_basename_user + suffixes_all[i], time=grass_time) if glob_rad_basename_user: set_time_stamp(glob_rad_basename_user + suffixes_all[i], time=grass_time) if incidout_basename: set_time_stamp(incidout_basename + suffixes_all[i], time=grass_time) if beam_rad_basename_user: maps = [beam_rad_basename_user + suf for suf in suffixes_all] set_color_table(maps, binary) if diff_rad_basename_user: maps = [diff_rad_basename_user + suf for suf in suffixes_all] set_color_table(maps, binary) if refl_rad_basename_user: maps = [refl_rad_basename_user + suf for suf in suffixes_all] set_color_table(maps, binary) if glob_rad_basename_user: maps = [glob_rad_basename_user + suf for suf in suffixes_all] set_color_table(maps, binary) if incidout_basename: maps = [incidout_basename + suf for suf in suffixes_all] set_color_table(maps)