def load_generic(satscene, options, calibrate=True): """Read sar data from file and load it into *satscene*. """ os.environ["PPP_CONFIG_DIR"] = CONFIG_PATH LOG.debug("Channels to load from %s: %s"%(satscene.instrument_name, satscene.channels_to_load)) # Compulsory global attribudes satscene.info["title"] = (satscene.satname.capitalize() + satscene.number + " satellite, " + satscene.instrument_name.capitalize() + " instrument.") satscene.info["institution"] = "No institution." satscene.add_to_history("SAR data read by mipp/mpop.") satscene.info["references"] = "No reference." satscene.info["comments"] = "No comment." for chn in satscene.channels_to_load: try: metadata, data = xsar.sat.load(satscene.fullname, satscene.time_slot, chn, mask=True, calibrate=calibrate) except CalibrationError: LOG.warning("Loading non calibrated data since calibration failed.") metadata, data = xsar.sat.load(satscene.fullname, satscene.time_slot, chn, mask=True, calibrate=False) except ReaderError: # if channel can't be found, go on with next channel continue satscene[chn] = data satscene[chn].info['units'] = metadata.calibration_unit if is_pyresample_loaded: # Build an area on the fly from the mipp metadata proj_params = getattr(metadata, "proj4_params").split() proj_dict = {} for param in proj_params: key, val = [i.strip() for i in param.split("=")] proj_dict[key] = val # Build area_def on-the-fly satscene[chn].area = geometry.AreaDefinition( satscene.satname + satscene.instrument_name + str(metadata.area_extent) + str(data.shape), "On-the-fly area", proj_dict["proj"], proj_dict, data.shape[1], data.shape[0], metadata.area_extent) else: LOG.info("Could not build area, pyresample missing...")
def project(self, coverage): """Remaps the Modis EOS-HDF level2 ocean products to cartographic map-projection on a user defined area. """ LOG.info("Projecting product %s..."%(self.name)) #print("Inside project...") retv = ModisEosHdfLevel2(self.name) retv.data = coverage.project_array(self.data) retv.area = coverage.out_area retv.shape = retv.data.shape retv.resolution = self.resolution retv.info = self.info retv.filled = True valid_min = retv.data.min() valid_max = retv.data.max() retv.info['valid_range'] = np.array([valid_min, valid_max]) retv.info['var_data'] = retv.data return retv
def load(satscene, **kwargs): """Read data from file and load it into *satscene*. Load data into the *channels*. *Channels* is a list or a tuple containing channels we will load data into. If None, all channels are loaded. """ del kwargs conf = ConfigParser() conf.read(os.path.join(CONFIG_PATH, satscene.fullname + ".cfg")) options = {} for option, value in conf.items(satscene.instrument_name+"-level3", raw = True): options[option] = value pathname = os.path.join(options["dir"], options['filename']) filename = satscene.time_slot.strftime(pathname) for prodname in GEO_PHYS_PRODUCTS + FLAGS_QUALITY: if prodname in satscene.channels_to_load: prod_chan = ModisEosHdfLevel2(prodname) prod_chan.read(filename) prod_chan.satid = satscene.satname.capitalize() prod_chan.resolution = 1000.0 prod_chan.shape = prod_chan.data.shape # All this for the netCDF writer: prod_chan.info['var_name'] = prodname prod_chan.info['var_data'] = prod_chan.data resolution_str = str(int(prod_chan.resolution))+'m' prod_chan.info['var_dim_names'] = ('y'+resolution_str, 'x'+resolution_str) prod_chan.info['long_name'] = prod_chan.attr['long_name'][:-1] try: prod_chan.info['standard_name'] = prod_chan.attr['standard_name'][:-1] except KeyError: pass valid_min = np.min(prod_chan.data) valid_max = np.max(prod_chan.data) prod_chan.info['valid_range'] = np.array([valid_min, valid_max]) prod_chan.info['resolution'] = prod_chan.resolution if prodname == 'l2_flags': # l2 flags definitions for i in range(1, 33): key = "f%02d_name"%i prod_chan.info[key] = prod_chan.attr[key][:-1] satscene.channels.append(prod_chan) if prodname in CHANNELS: satscene[prodname].info['units'] = '%' else: satscene[prodname].info['units'] = prod_chan.attr['units'][:-1] LOG.info("Loading modis lvl2 product '%s' done"%prodname) # Check if there are any bands to load: channels_to_load = False for bandname in CHANNELS: if bandname in satscene.channels_to_load: channels_to_load = True break if channels_to_load: #print "FILE: ", filename eoshdf = SD(filename) # Get all the Attributes: # Common Attributes, Data Time, # Data Structure and Scene Coordinates info = {} for key in eoshdf.attributes().keys(): info[key] = eoshdf.attributes()[key] dsets = eoshdf.datasets() selected_dsets = [] for bandname in CHANNELS: if (bandname in satscene.channels_to_load and bandname in dsets): value = eoshdf.select(bandname) selected_dsets.append(value) # Get only the selected datasets attr = value.attributes() band = value.get() nodata = attr['bad_value_scaled'] mask = np.equal(band, nodata) satscene[bandname] = (np.ma.masked_where(mask, band) * attr['slope'] + attr['intercept']) satscene[bandname].info['units'] = '%' satscene[bandname].info['long_name'] = attr['long_name'][:-1] for dset in selected_dsets: dset.endaccess() LOG.info("Loading modis lvl2 Remote Sensing Reflectances done") eoshdf.end() lat, lon = get_lat_lon(satscene, None) from pyresample import geometry satscene.area = geometry.SwathDefinition(lons=lon, lats=lat) #print "Variant: ", satscene.variant satscene.variant = 'regional' # Temporary fix! LOG.info("Loading modis data done.")
def get_channels(self, channels, calib_type): """Get calibrated channel data. *calib_type* = 0: Counts *calib_type* = 1: Reflectances and brightness temperatures *calib_type* = 2: Radiances """ if calib_type == 0: raise ValueError('calibrate=0 is not supported! ' + 'This reader cannot return counts') elif calib_type != 1 and calib_type != 2: raise ValueError('calibrate=' + str(calib_type) + 'is not supported!') if ("3a" in channels or "3A" in channels or "3b" in channels or "3B" in channels): three_a = ((self["FRAME_INDICATOR"] & 2**16) == 2**16) three_b = ((self["FRAME_INDICATOR"] & 2**16) == 0) chans = {} for chan in channels: if chan not in ["1", "2", "3a", "3A", "3b", "3B", "4", "5"]: LOG.info("Can't load channel in eps_l1b: " + str(chan)) if chan == "1": if calib_type == 1: chans[chan] = np.ma.array( to_refl(self["SCENE_RADIANCES"][:, 0, :], self["CH1_SOLAR_FILTERED_IRRADIANCE"])) else: chans[chan] = np.ma.array( self["SCENE_RADIANCES"][:, 0, :]) if chan == "2": if calib_type == 1: chans[chan] = np.ma.array( to_refl(self["SCENE_RADIANCES"][:, 1, :], self["CH2_SOLAR_FILTERED_IRRADIANCE"])) else: chans[chan] = np.ma.array( self["SCENE_RADIANCES"][:, 1, :]) if chan.lower() == "3a": if calib_type == 1: chans[chan] = np.ma.array( to_refl(self["SCENE_RADIANCES"][:, 2, :], self["CH2_SOLAR_FILTERED_IRRADIANCE"])) else: chans[chan] = np.ma.array(self["SCENE_RADIANCES"][:, 2, :]) chans[chan][three_b, :] = np.nan chans[chan] = np.ma.masked_invalid(chans[chan]) if chan.lower() == "3b": if calib_type == 1: chans[chan] = np.ma.array( to_bt(self["SCENE_RADIANCES"][:, 2, :], self["CH3B_CENTRAL_WAVENUMBER"], self["CH3B_CONSTANT1"], self["CH3B_CONSTANT2_SLOPE"])) else: chans[chan] = self["SCENE_RADIANCES"][:, 2, :] chans[chan][three_a, :] = np.nan chans[chan] = np.ma.masked_invalid(chans[chan]) if chan == "4": if calib_type == 1: chans[chan] = np.ma.array( to_bt(self["SCENE_RADIANCES"][:, 3, :], self["CH4_CENTRAL_WAVENUMBER"], self["CH4_CONSTANT1"], self["CH4_CONSTANT2_SLOPE"])) else: chans[chan] = np.ma.array( self["SCENE_RADIANCES"][:, 3, :]) if chan == "5": if calib_type == 1: chans[chan] = np.ma.array( to_bt(self["SCENE_RADIANCES"][:, 4, :], self["CH5_CENTRAL_WAVENUMBER"], self["CH5_CONSTANT1"], self["CH5_CONSTANT2_SLOPE"])) else: chans[chan] = np.ma.array(self["SCENE_RADIANCES"][:, 4, :]) return chans
def load_generic(satscene, options, calibrate=True, area_extent=None): """Read seviri data from file and load it into *satscene*. """ os.environ["PPP_CONFIG_DIR"] = CONFIG_PATH LOG.debug("Channels to load from %s: %s" % (satscene.instrument_name, satscene.channels_to_load)) # Compulsory global attribudes satscene.info["title"] = ( satscene.satname.capitalize() + satscene.number + " satellite, " + satscene.instrument_name.capitalize() + " instrument." ) satscene.info["institution"] = "Original data disseminated by EumetCast." satscene.add_to_history("HRIT/LRIT data read by mipp/mpop.") satscene.info["references"] = "No reference." satscene.info["comments"] = "No comment." from_area = False if area_extent is None and satscene.area is not None: if not satscene.area_def: satscene.area = get_area_def(satscene.area_id) area_extent = satscene.area.area_extent from_area = True for chn in satscene.channels_to_load: if from_area: try: metadata = xrit.sat.load(satscene.fullname, satscene.time_slot, chn, only_metadata=True) if ( satscene.area_def.proj_dict["proj"] != "geos" or float(satscene.area_def.proj_dict["lon_0"]) != metadata.sublon ): raise ValueError( "Slicing area must be in " "geos projection, and lon_0 should match the" " satellite's position." ) except SatReaderError: # if channel can't be found, go on with next channel continue try: image = xrit.sat.load(satscene.fullname, satscene.time_slot, chn, mask=True, calibrate=calibrate) if area_extent: metadata, data = image(area_extent) else: metadata, data = image() except CalibrationError: LOG.warning("Loading non calibrated data since calibration failed.") image = xrit.sat.load(satscene.fullname, satscene.time_slot, chn, mask=True, calibrate=False) if area_extent: metadata, data = image(area_extent) else: metadata, data = image() except SatReaderError: # if channel can't be found, go on with next channel continue satscene[chn] = data satscene[chn].info["units"] = metadata.calibration_unit # Build an area on the fly from the mipp metadata proj_params = getattr(metadata, "proj4_params").split(" ") proj_dict = {} for param in proj_params: key, val = param.split("=") proj_dict[key] = val if is_pyresample_loaded: # Build area_def on-the-fly satscene[chn].area = geometry.AreaDefinition( satscene.satname + satscene.instrument_name + str(metadata.area_extent) + str(data.shape), "On-the-fly area", proj_dict["proj"], proj_dict, data.shape[1], data.shape[0], metadata.area_extent, ) else: LOG.info("Could not build area, pyresample missing...")