def add(id, description, projection_id, extent, xsize, ysize, xscale, yscale, filename=AREA_REGISTRY): reg = _arearegistry.load(filename) reg.removeByName(id) # Is silent if entry doesn't exist a = _area.new() a.id, a.description, a.pcsid = id, description, projection_id a.extent = extent a.xsize, a.ysize = xsize, ysize a.xscale, a.yscale = xscale, yscale p = rave_projection.pcs(a.pcsid) pid = p.id pname = p.name if isinstance(pid, bytes): pid = pid.decode() if isinstance(pname, bytes): pname = pname.decode() a.projection = _projection.new(pid, pname, " ".join(p.definition)) reg.add(a) reg.write(filename)
def register(A): A.validate( ["Id", "name", "pcs", "extent", "xsize", "ysize", "xscale", "yscale"]) # Switch to strings if isinstance(A.pcs, bytes): A.pcs = A.pcs.decode() if isinstance(A.name, bytes): A.name = A.name.decode() if isinstance(A.Id, bytes): A.Id = A.Id.decode() if isinstance(A.xsize, bytes): A.xsize = A.xsize.decode() if isinstance(A.ysize, bytes): A.ysize = A.ysize.decode() if isinstance(A.xscale, bytes): A.xscale = A.xscale.decode() if isinstance(A.yscale, bytes): A.yscale = A.yscale.decode() if isinstance(A.extent, bytes): A.extent = A.extent.decode() A.pcs = A.pcs.replace("\n", "").lstrip(" ").rstrip(" ") A.name = A.name.replace("\n", "").lstrip(" ").rstrip(" ") # Likewise, rave_simplexml.c doesn't write argument types, so we must enforce them here. if isinstance(A.xsize, str): A.xsize = int(A.xsize) if isinstance(A.ysize, str): A.ysize = int(A.ysize) if isinstance(A.xscale, str): A.xscale = float(A.xscale) if isinstance(A.yscale, str): A.yscale = float(A.yscale) if isinstance(A.extent, str): A.extent = make_tuple(A.extent) A.pcs = rave_projection.pcs(A.pcs) _registry[A.Id] = A
def s2c(indata, pcs_id): import rave_projection p = rave_projection.pcs(pcs_id) outdata = [] for ll in indata: outdata.append(r2d(p.invproj(ll))) return outdata
def c2s(indata, pcs_id): import rave_projection p = rave_projection.pcs(pcs_id) # pcs_id = "ps60n", for example outdata = [] for ll in indata: outdata.append(p.proj(d2r(ll))) return outdata
def _generate(self, dd, dt, area=None): self._debug_generate_info(area) if self.verbose: self.logger.info("Fetching objects and applying quality plugins") self.logger.debug( "Generating composite with date and time %sT%s for area %s", dd, dt, area) objects, nodes, how_tasks, all_files_malfunc = self.fetch_objects() if all_files_malfunc: self.logger.info( "Content of all provided files were marked as 'malfunc'. Since option 'ignore_malfunc' is set, no composite is generated!" ) return None objects, algorithm, qfields = self.quality_control_objects(objects) self.logger.debug("Quality controls for composite generation: %s", (",".join(qfields))) if len(objects) == 0: self.logger.info( "No objects provided to the composite generator. No composite will be generated!" ) return None objects = list(objects.values()) if self.dump: self._dump_objects(objects) generator = _pycomposite.new() if area is not None: if _area.isArea(area): pyarea = area else: pyarea = my_area_registry.getarea(area) else: if self.verbose: self.logger.info("Determining best fit for area") A = rave_area.MakeAreaFromPolarObjects(objects, self.pcsid, self.xscale, self.yscale) pyarea = _area.new() pyarea.id = "auto-generated best-fit" pyarea.xsize = A.xsize pyarea.ysize = A.ysize pyarea.xscale = A.xscale pyarea.yscale = A.yscale pyarea.extent = A.extent pcs = rave_projection.pcs(A.pcs) pcsname = pcs.name if not is_py27: pcsname = pcsname.decode() pyarea.projection = _projection.new(pcs.id, pcsname, ' '.join(pcs.definition)) if len(objects) == 1: try: tmpid = odim_source.NODfromSource(objects[0]) pyarea.id = "auto_%s_%s" % (A.pcs, tmpid) except: pass generator.addParameter(self.quantity, self.gain, self.offset, self.minvalue) generator.product = self.product if algorithm is not None: generator.algorithm = algorithm for o in objects: generator.add(o) # We want to ensure that we get a proper indexing of included radar sourceid = o.source try: osource = odim_source.ODIM_Source(o.source) if osource.wmo: sourceid = "WMO:%s" % osource.wmo elif osource.rad: sourceid = "RAD:%s" % osource.rad elif osource.nod: sourceid = "NOD:%s" % osource.nod except: pass if not sourceid in self.radar_index_mapping.keys(): self.radar_index_mapping[sourceid] = self.get_next_radar_index( ) generator.selection_method = self.selection_method generator.interpolation_method = self.interpolation_method generator.date = o.date if dd is None else dd generator.time = o.time if dt is None else dt generator.height = self.height generator.elangle = self.elangle generator.range = self.range if self.qitotal_field is not None: generator.quality_indicator_field_name = self.qitotal_field if self.prodpar is not None: self._update_generator_with_prodpar(generator) if self.verbose: self.logger.info("Generating cartesian composite") generator.applyRadarIndexMapping(self.radar_index_mapping) result = generator.generate(pyarea, qfields) if self.applyctfilter: if self.verbose: self.logger.debug("Applying ct filter") rave_ctfilter.ctFilter(result, self.quantity) if self.applygra: if not "se.smhi.composite.distance.radar" in qfields: self.logger.info( "Trying to apply GRA analysis without specifying a quality plugin specifying the se.smhi.composite.distance.radar q-field, disabling..." ) else: if self.verbose: self.logger.info( "Applying GRA analysis (ZR A = %f, ZR b = %f)" % (self.zr_A, self.zr_b)) grafield = self._apply_gra(result, dd, dt) if grafield: result.addParameter(grafield) else: self.logger.warn("Failed to generate gra field....") # Hack to create a BRDR field if the qfields contains se.smhi.composite.index.radar if "se.smhi.composite.index.radar" in qfields: bitmapgen = _bitmapgenerator.new() brdr_field = bitmapgen.create_intersect( result.getParameter(self.quantity), "se.smhi.composite.index.radar") brdr_param = result.createParameter("BRDR", _rave.RaveDataType_UCHAR) brdr_param.setData(brdr_field.getData()) if self.applygapfilling: if self.verbose: self.logger.debug("Applying gap filling") t = _transform.new() gap_filled = t.fillGap(result) result.getParameter(self.quantity).setData( gap_filled.getParameter(self.quantity).getData()) # Fix so that we get a valid place for /what/source and /how/nodes plc = result.source result.source = "%s,CMT:%s" % (CENTER_ID, plc) result.addAttribute('how/nodes', nodes) if self.use_site_source and len(objects) == 1: try: result.source = objects[0].source if result.source.find("NOD:") == -1: tmpid = odim_source.NODfromSource(objects[0]) result.source = "%s,NOD:%s,CMT:%s" % ( self.remove_CMT_from_source(result.source), tmpid, plc) else: result.source = "%s,CMT:%s" % (self.remove_CMT_from_source( result.source), plc) except: self.logger.exception("Failed to get source from object") if how_tasks != "": result.addAttribute('how/task', how_tasks) if self.verbose: self.logger.debug("Returning resulting composite image") return result
def _generate(self, dd, dt, area=None): self._debug_generate_info(area) if self.verbose: self.logger.info("Fetching objects and applying quality plugins") self.logger.debug( "Generating composite with date and time %sT%s for area %s", dd, dt, area) objects, nodes, how_tasks = self.fetch_objects() objects, algorithm, qfields = self.quality_control_objects(objects) objects = objects.values() if self.dump: self._dump_objects(objects) generator = _pycomposite.new() if area is not None: if _area.isArea(area): pyarea = area else: pyarea = my_area_registry.getarea(area) else: if self.verbose: self.logger.info("Determining best fit for area") A = rave_area.MakeAreaFromPolarObjects(objects, self.pcsid, self.xscale, self.yscale) pyarea = _area.new() pyarea.id = "auto-generated best-fit" pyarea.xsize = A.xsize pyarea.ysize = A.ysize pyarea.xscale = A.xscale pyarea.yscale = A.yscale pyarea.extent = A.extent pcs = rave_projection.pcs(A.pcs) pyarea.projection = _projection.new( pcs.id, pcs.name, string.join(pcs.definition, ' ')) if len(objects) == 1: try: tmpid = odim_source.NODfromSource(objects[0]) pyarea.id = "auto_%s_%s" % (A.pcs, tmpid) except: pass if type(self.quantity) is types.StringType: generator.addParameter(self.quantity, self.gain, self.offset) else: for quantity in self.quantity: generator.addParameter(quantity, self.gain, self.offset) generator.product = self.product if algorithm is not None: generator.algorithm = algorithm if len(objects) == 0: self.logger.info("No objects provided to the composite generator.") if dd is None or dt is None: self.logger.error( "Can not create a composite without specifying a valid date / time when no objects are provided." ) raise Exception, "Can not create a composite without specifying a valid date / time when no objects are provided." for o in objects: generator.add(o) generator.selection_method = self.selection_method generator.date = o.date if dd is None else dd generator.time = o.time if dt is None else dt generator.height = self.height generator.elangle = self.elangle generator.range = self.range if self.qitotal_field is not None: generator.quality_indicator_field_name = self.qitotal_field if self.prodpar is not None: self._update_generator_with_prodpar(generator) if self.verbose: self.logger.info("Generating cartesian composite") result = generator.nearest(pyarea, qfields) if self.applyctfilter: if self.verbose: self.logger.debug("Applying ct filter") ret = rave_ctfilter.ctFilter(result, self.quantity) if self.applygra: if not "se.smhi.composite.distance.radar" in qfields: self.logger.info( "Trying to apply GRA analysis without specifying a quality plugin specifying the se.smhi.composite.distance.radar q-field, disabling..." ) else: if self.verbose: self.logger.info( "Applying GRA analysis (ZR A = %f, ZR b = %f)" % (self.zr_A, self.zr_b)) grafield = self._apply_gra(result, dd, dt) if grafield: result.addParameter(grafield) else: self.logger.warn("Failed to generate gra field....") if self.applygapfilling: if self.verbose: self.logger.debug("Applying gap filling") t = _transform.new() gap_filled = t.fillGap(result) result.getParameter(self.quantity).setData( gap_filled.getParameter(self.quantity).getData()) # Fix so that we get a valid place for /what/source and /how/nodes plc = result.source result.source = "%s,CMT:%s" % (CENTER_ID, plc) result.addAttribute('how/nodes', nodes) if self.use_site_source and len(objects) == 1: try: result.source = objects[0].source if result.source.find("NOD:") == -1: tmpid = odim_source.NODfromSource(objects[0]) result.source = "%s,NOD:%s,CMT:%s" % ( self.remove_CMT_from_source(result.source), tmpid, plc) else: result.source = "%s,CMT:%s" % (self.remove_CMT_from_source( result.source), plc) except: self.logger.exception("Failed to get source from object") if how_tasks != "": result.addAttribute('how/task', how_tasks) if self.verbose: self.logger.debug("Returning resulting composite image") return result