def test_norotation_yaml(self): """No rotation specified in yaml format.""" from pyresample.area_config import load_area yamlDef = """regionB: description: regionB projection: a: 6378137.0 b: 6378137.0 lon_0: -34 proj: merc x_0: 0 y_0: 0 k_0: 1 shape: height: 548 width: 800 area_extent: lower_left_xy: [-7761424.714818418, -4861746.639279127] upper_right_xy: [11136477.43264252, 8236799.845095873] units: m""" with NamedTemporaryFile(mode="w", suffix='.yaml', delete=False) as f: f.write(yamlDef) test_area = load_area(f.name, 'regionB') self.assertEqual(test_area.rotation, 0) os.remove(f.name)
def get_sectors_from_yamls(sectorfnames, sectornames): ''' Get AreaDefinition objects with custom "sector_info" dictionary from YAML area definition Args: sectorfnames (list) : list of string full paths to YAML area definition files sectornames (list) : list of strings of desired sector names to retrieve from YAML files Returns: (list) : List of pyresample AreaDefinition objects, with arbitrary additional YAML entries added as attributes to each area def (this is to allow specifying "sector_info" metadata dictionary within the YAML file) ''' from pyresample import load_area import yaml area_defs = [] for sectorfname in sectorfnames: with open(sectorfname) as sectorfobj: ydict = yaml.safe_load(sectorfobj) for sectorname in sectornames: if sectorname in ydict.keys(): try: area_def = load_area(sectorfname, sectorname) except TypeError: area_def = create_areadefinition_from_yaml(sectorfname,sectorname) for key in ydict[sectorname].keys(): if not hasattr(area_def, key) and key not in ['description', 'projection']: area_def.__setattr__(key, ydict[sectorname][key]) area_defs += [area_def] return area_defs
def test_load_area(self): from pyresample import load_area ease_nh = load_area(os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg'), 'ease_nh') nh_str = """Area ID: ease_nh Description: Arctic EASE grid Projection ID: ease_nh Projection: {'a': '6371228.0', 'lat_0': '90.0', 'lon_0': '0.0', 'proj': 'laea', 'units': 'm'} Number of columns: 425 Number of rows: 425 Area extent: (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)""" self.assertEqual(nh_str, ease_nh.__str__())
def test_norotation_legacy(self): """No rotation specified in legacy format.""" from pyresample.area_config import load_area legacyDef = """REGION: regionB { NAME: regionB PCS_ID: regionB PCS_DEF: proj=merc, lon_0=-34, k=1, x_0=0, y_0=0, a=6378137, b=6378137 XSIZE: 800 YSIZE: 548 AREA_EXTENT: (-7761424.714818418, -4861746.639279127, 11136477.43264252, 8236799.845095873) };""" with NamedTemporaryFile(mode="w", suffix='.cfg', delete=False) as f: f.write(legacyDef) test_area = load_area(f.name, 'regionB') self.assertEqual(test_area.rotation, 0) os.remove(f.name)
def test_load_area(self): from pyresample import load_area ease_nh = load_area( os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg'), 'ease_nh') # pyproj 2.0+ adds some extra parameters projection = ("{'R': '6371228', 'lat_0': '90', 'lon_0': '0', " "'no_defs': 'None', 'proj': 'laea', 'type': 'crs', " "'units': 'm', 'x_0': '0', 'y_0': '0'}") nh_str = """Area ID: ease_nh Description: Arctic EASE grid Projection ID: ease_nh Projection: {} Number of columns: 425 Number of rows: 425 Area extent: (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)""".format( projection) self.assertEqual(nh_str, ease_nh.__str__())
def get_time_start_end(isat, ds): if isat == 0: orbit_time = np.datetime64( ds.attrs['time_coverage_start']) - np.timedelta64( 24, 'h') #changed to 24 hr for sss orbit_time2 = np.datetime64( ds.attrs['time_coverage_end']) + np.timedelta64(24, 'h') if isat == 1: orbit_time = ds.time[0].data - np.timedelta64(12, 'h') orbit_time2 = ds.time[-1].data + np.timedelta64(12, 'h') return orbit_time, orbit_time2 area_def = load_area('areas.cfg', 'pc_world') rlon = np.arange(-180, 180, .1) rlat = np.arange(90, -90, -.1) #for name in data_dict: for iname in range(input_iusv_start, input_iusv_end): #g,name in enumerate(data_dict): area_def = load_area('areas.cfg', 'pc_world') rlon = np.arange(-180, 180, .1) rlat = np.arange(90, -90, -.1) for isat in range(1, 2): ds_usv, name_usv = read_one_usv(files[iname])
def run(ipath, ifile, opath, dnts, satzs, year, month, dataset, chunksize=100000): # if dnts is single string convert to list if isinstance(dnts, str): dnts = [dnts] # if satzs is single string/int/float convert to list if isinstance(satzs, str) or isinstance(satzs, int) or isinstance( satzs, float): satzs = [satzs] if dataset not in ['CCI', 'CLAAS3']: raise Exception('Dataset {} not available!'.format(dataset)) ofile_cma = 'CMA_SEVIRI_CALIOP_{}{}_DNT-{}_SATZ-{}.png' ofile_cph = 'CPH_SEVIRI_CALIOP_{}{}_DNT-{}_SATZ-{}.png' ofile_ctth = 'CTTH_SEVIRI_CALIOP_{}{}_DNT-{}_SATZ-{}.png' ofile_scat = 'SCATTER_SEVIRI_CALIOP_{}{}_DNT-{}_SATZ-{}.png' # iterate over satzen limitations for satz_lim in satzs: # if satz_lim list item is string convert it to float if satz_lim is not None: if isinstance(satz_lim, str): try: satz_lim = float(satz_lim) except ValueError: msg = 'Cannot convert {} to float' raise Exception(msg.format(satz_lim)) for dnt in dnts: dnt = dnt.upper() if dnt not in ['ALL', 'DAY', 'NIGHT', 'TWILIGHT']: raise Exception('DNT {} not recognized'.format(dnt)) # set output filenames for CPH and CMA plot ofile_cma = ofile_cma.format(year, month, dnt, satz_lim) ofile_cph = ofile_cph.format(year, month, dnt, satz_lim) ofile_ctth = ofile_ctth.format(year, month, dnt, satz_lim) ofile_scat = ofile_scat.format(year, month, dnt, satz_lim) # get matchup data data, latlon = get_collocated_file_info(os.path.join(ipath, ifile), chunksize, dnt, satz_lim, dataset) adef = load_area('areas.yaml', 'pc_world') # for each input pixel get target pixel index resampler = BucketResampler(adef, latlon['lon'], latlon['lat']) idxs = resampler.idxs # get output grid size/lat/lon out_size = adef.size lon, lat = adef.get_lonlats() # do validation cma_scores = do_cma_validation(data, adef, out_size, idxs) cph_scores = do_cph_validation(data, adef, out_size, idxs) ctth_scores = do_ctth_validation(data, resampler, thrs=10) # get crs for plotting crs = adef.to_cartopy_crs() # get cos(lat) filed for weighted average on global regular grid cosfield = get_cosfield(lat) # do plotting make_plot(cma_scores, os.path.join(opath, ofile_cma), crs, dnt, 'CMA', cosfield) make_plot(cph_scores, os.path.join(opath, ofile_cph), crs, dnt, 'CPH', cosfield) make_plot_CTTH(ctth_scores, os.path.join(opath, ofile_ctth), crs, dnt, 'CTTH', cosfield) make_scatter(data, os.path.join(opath, ofile_scat), dnt, dataset)
def run(opath, year, month, dataset, ifilepath_calipso=None, ifilepath_amsr=None, ifilepath_dardar=None, satzs=[None], dnts=['ALL', 'DAY', 'NIGHT', 'TWILIGHT'], chunksize=100000, plot_area='pc_world', FILTER_STRATOSPHERIC_CLOUDS=False): """ Main function to be called in external script to run atrain_plot. opath (str): Path where figures should be saved. year (str): String of year. month (str): String of month. dataset (str): Dataset validated: Available: CCI, CLAAS. ifilepath_calipso (str): Path to CALIPSO HDF5 atrain_match matchup file. ifilepath_amsr (str): Path to CALIPSO HDF5 atrain_match matchup file. ifilepath_dardar (str): Path to CALIPSO HDF5 atrain_match matchup file. satzs (list): List of satellite zenith limitations to be processed. Use [None] if no limitation required. dnts (list): List of illumination scenarios to be processed. Available: ALL, DAY, NIGHT, TWILIGHT chunksize (int): Size of data chunks to reduce memory usage. plot_area (str): Name of area definition in areas.yaml file to be used. FILTER_STRATOSPHERIC_CLOUDS(bool): filter calipso data with CTH>tropopause height """ global filter_stratospheric filter_stratospheric = FILTER_STRATOSPHERIC_CLOUDS if ifilepath_calipso is None and ifilepath_amsr is None and ifilepath_dardar is None: raise Exception( 'All input files (CALIOP/AMSR/DARDAR) are None. Not running validation!' ) _print_opts(ifilepath_calipso, ifilepath_amsr, ifilepath_dardar, opath, dnts, satzs, dataset, filter_stratospheric) # if dnts is single string convert to list if isinstance(dnts, str): dnts = [dnts] # if satzs is single string/int/float convert to list if isinstance(satzs, str) or isinstance(satzs, int) or isinstance( satzs, float): satzs = [satzs] if dataset not in ['CCI', 'CLAAS']: raise Exception('Dataset {} not available!'.format(dataset)) # define plotting area module_path = os.path.dirname(__file__) adef = load_area(os.path.join(module_path, 'areas.yaml'), plot_area) # get output grid size/lat/lon out_size = adef.size lon, lat = adef.get_lonlats() # get crs for plotting crs = adef.to_cartopy_crs() ofile_cma = 'CMA_{}_CALIOP_{}{}_DNT-{}_SATZ-{}.png' ofile_cph = 'CPH_{}_CALIOP_{}{}_DNT-{}_SATZ-{}.png' ofile_ctth = 'CTTH_{}_CALIOP_{}{}_DNT-{}_SATZ-{}.png' ofile_scat = 'CTX_SCATTER_{}_CALIOP_{}{}_DNT-{}_SATZ-{}.png' # iterate over satzen limitations for satz_lim in satzs: # if satz_lim list item is string convert it to float if satz_lim is not None: if isinstance(satz_lim, str): try: satz_lim = float(satz_lim) except ValueError: msg = 'Cannot convert {} to float' raise Exception(msg.format(satz_lim)) print('------------- SATZ = {} -------------'.format(satz_lim)) for dnt in dnts: dnt = dnt.upper() if dnt not in ['ALL', 'DAY', 'NIGHT', 'TWILIGHT']: raise Exception('DNT {} not recognized'.format(dnt)) print('---------------------------') print('DNT = {}'.format(dnt)) print('---------------------------') if ifilepath_calipso is not None: # set output filenames for CPH and CMA plot ofile_cma_tmp = ofile_cma.format(dataset, year, month, dnt, satz_lim) ofile_cph_tmp = ofile_cph.format(dataset, year, month, dnt, satz_lim) ofile_ctth_tmp = ofile_ctth.format(dataset, year, month, dnt, satz_lim) ofile_scat_tmp = ofile_scat.format(dataset, year, month, dnt, satz_lim) # get matchup data data, latlon = _get_calipso_matchup_file_content( ifilepath_calipso, chunksize, dnt, satz_lim, dataset) # for each input pixel get target pixel index resampler = BucketResampler(adef, latlon['lon'], latlon['lat']) idxs = resampler.idxs # do validation cma_scores = _do_cma_cph_validation(data, adef, out_size, idxs, 'cma') cph_scores = _do_cma_cph_validation(data, adef, out_size, idxs, 'cph') ctth_scores = _do_ctth_validation(data, adef, out_size, idxs, resampler, thrs=10) # get cos(lat) filed for weighted average on global regular grid cosfield = _get_cosfield(lat) # do plotting mp.make_plot_cma_cph(cma_scores, os.path.join(opath, ofile_cma_tmp), crs, dnt, 'CMA', cosfield) mp.make_plot_cma_cph(cph_scores, os.path.join(opath, ofile_cph_tmp), crs, dnt, 'CPH', cosfield) mp.make_plot_CTTH(ctth_scores, os.path.join(opath, ofile_ctth_tmp), crs, dnt, 'CTTH', cosfield) scat.make_scatter_ctx(data, os.path.join(opath, ofile_scat_tmp), dnt, dataset) if ifilepath_amsr is not None: # LWP plots amsr_lwp, imager_lwp, amsr_lat, amsr_lon = ga.read_amsr_lwp( ifilepath_amsr, dataset) ofile_lwp = 'LWP_{}_AMSR2_{}{}.png'.format(dataset, year, month) ofile_lwp_scatter = 'LWP_SCATTER_{}_AMSR2_{}{}.png'.format( dataset, year, month) results = _process_cer_or_lwp_or_iwp(amsr_lwp, imager_lwp, amsr_lat, amsr_lon, adef, 'AMSR2 LWP', dataset, chunksize) mp.make_plot_lwp(results, crs, os.path.join(opath, ofile_lwp)) scat.make_scatter_lwp(amsr_lwp, imager_lwp, os.path.join(opath, ofile_lwp_scatter)) if ifilepath_dardar is not None: # IWP plots dardar_iwp, imager_iwp, dardar_lat, dardar_lon = gd.read_dardar_iwp( ifilepath_dardar, dataset) ofile_iwp = 'IWP_{}_DARDAR_{}{}.png'.format(dataset, year, month) ofile_iwp_scatter = 'IWP_SCATTER_{}_DARDAR_{}{}.png'.format( dataset, year, month) results = _process_cer_or_lwp_or_iwp(dardar_iwp, imager_iwp, dardar_lat, dardar_lon, adef, 'DARDAR IWP', dataset, chunksize) mp.make_plot_iwp(results, crs, os.path.join(opath, ofile_iwp)) scat.make_scatter_iwp(dardar_iwp, imager_iwp, os.path.join(opath, ofile_iwp_scatter)) # CER plots dardar_cer, imager_cer, dardar_lat, dardar_lon = gd.read_dardar_cer( ifilepath_dardar, dataset) ofile_cer = 'CER_{}_DARDAR_{}{}.png'.format(dataset, year, month) ofile_cer_scatter = 'CER_SCATTER_{}_DARDAR_{}{}.png'.format( dataset, year, month) results = _process_cer_or_lwp_or_iwp(dardar_cer, imager_cer, dardar_lat, dardar_lon, adef, 'DARDAR CER', dataset, chunksize) mp.make_plot_cer(results, crs, os.path.join(opath, ofile_cer)) scat.make_scatter_cer(dardar_cer, imager_cer, os.path.join(opath, ofile_cer_scatter))
end_time=datetime(2021, 7, 25, 11, 25), base_dir=sen3_data_l2, reader='olci_l2', sensor='olci', ) """ Create Scene object """ scn = Scene(filenames=filenames) """ Load selected datasets Available OLCI Level 2 datasets are: 'chl_nn.nc','chl_oc4me.nc', 'iop_nn.nc','iwv.nc','par.nc','trsp.nc','tsm_nn.nc','w_aer.nc', 'Oa01_reflectance.nc','Oa02_reflectance.nc','Oa03_reflectance.nc','Oa04_reflectance.nc', 'Oa05_reflectance.nc','Oa06_reflectance.nc','Oa07_reflectance.nc','Oa08_reflectance.nc', 'Oa09_reflectance.nc','Oa10_reflectance.nc','Oa11_reflectance.nc','Oa12_reflectance.nc', 'Oa16_reflectance.nc','Oa17_reflectance.nc','Oa18_reflectance.nc','Oa21_reflectance.nc', 'wqsf.nc' """ datasets = ['Oa08', 'Oa06', 'chl_nn', 'chl_oc4me', 'mask'] scn.load(datasets) my_area = load_area( os.path.join(settings.base_directory, 'etc/areas/local_areas.yaml'), 'baws300_sweref99tm') scn = scn.resample(my_area, radius_of_influence=800) """ Chlorophyll data are stored as logarithmic values. Convert to real values: """ scn['chl_nn'] = np.power(10, scn['chl_nn']) plt.imshow(scn['chl_nn']) plt.colorbar() plt.clim(0, 10)
def __call__(self, message): urlobj = urlparse(message.data['uri']) if 'start_time' in message.data: start_time = message.data['start_time'] else: raise InconsistentMessage("No start time in message!") if message.data['instruments'] == self.instrument: path, fname = os.path.split(urlobj.path) LOG.debug("path " + str(path) + " filename = " + str(fname)) instrument = str(message.data['instruments']) LOG.debug("Instrument %r supported!", instrument) platform_name = METOPS.get(message.data['satellite'], message.data['satellite']) filepath = os.path.join(path, fname) else: LOG.debug("Scene is not supported") raise SceneNotSupported("platform and instrument: " + str(message.data['platform_name']) + " " + str(message.data['instruments'])) if 'end_time' in message.data: end_time = message.data['end_time'] else: LOG.warning("No end time in message!") end_time = start_time + timedelta(seconds=self.passlength_seconds) LOG.info("End time set to: %s", str(end_time)) # Check that the input file really exists: if not os.path.exists(filepath): #LOG.error("File %s does not exist. Don't do anything...", filepath) raise IOError("File %s does not exist. Don't do anything...", filepath) LOG.info("Sat and Instrument: %s %s", platform_name, instrument) if not isinstance(self.tle_dirs, list): tle_dirs = [self.tle_dirs] tle_files = [] for tledir in tle_dirs: tle_files = tle_files + glob( os.path.join(tledir, globify(self.tlefilename))) tlep = Parser(self.tlefilename) time_thr = timedelta(days=5) utcnow = datetime.utcnow() valid_tle_file = None for tlefile in tle_files: fname = os.path.basename(tlefile) res = tlep.parse(fname) dtobj = res['time'] delta_t = abs(utcnow - dtobj) if delta_t < time_thr: time_thr = delta_t valid_tle_file = tlefile if not valid_tle_file: raise NoValidTles("Failed finding a valid tle file!") else: LOG.debug("Valid TLE file: %s", valid_tle_file) if not isinstance(self.areaids, list): self.areaids = [self.areaids] inside = False for areaid in self.areaids: area_def = load_area(self.area_def_file, areaid) inside = self.granule_inside_area(start_time, end_time, platform_name, area_def, valid_tle_file) if inside: return True return False
import os import sys import numpy as np import xarray as xr from pyresample import image, geometry, load_area, save_quicklook, SwathDefinition, area_def2basemap from pyresample.geometry import GridDefinition from pyresample.kd_tree import resample_nearest from scipy import spatial from read_routines import read_all_usv, get_filelist_slstr_l2p,get_orbital_data_slstr_l2p,rectangle_overlap # Area for PyResample # ------------------- '''some definitions''' area_def = load_area('areas.cfg', 'pc_world_1km') rlon = np.arange(-180, 180, .01) rlat = np.arange(90, -90, -.01) # Enter paths etc # --------------- # input_iusv_start = int(input("Enter start cruise processing number 0-10: ")) # input_iusv_end = int(input("Enter stop cruise processing number 0-10: ")) # adir_usv = str(input("Enter directory for USV data: ")) # adir_l1r = str(input("Enter directory for L1R data: ")) input_iusv_start = 0 input_iusv_end = 1 # Path where matchup files will be saved: adir_out = '/data/users/ssk/saildrone_data/matchup_SLSTR/' # Path pattern to select campaigns (Enter full path if you want one file):
# YSIZE: 548 # ROTATION: -45 # AREA_EXTENT: (-7761424.714818418, -4861746.639279127, 11136477.43264252, 8236799.845095873) #}; #REGION: world_plat_5400_2700 { # NAME: world_plat_5400_2700 # PCS_ID: world_plat_5400_2700 # PCS_DEF: proj=eqc,ellps=WGS84 # XSIZE: 5400 # YSIZE: 2700 # AREA_EXTENT: (-20037508.342789244, -10018754.171394622, 20037508.342789244, 10018754.171394622) #}; bkg_file = "world.topo.small.png" outfile = 'regionB.jpg' background = Image.open(bkg_file) #area_bkg = load_area('/opt/users/cll/PyTroll/etc/areas.def', 'world_plat_5400_2700') #area_target = load_area('/opt/users/cll/PyTroll/etc/areas.def', 'regionB') area_bkg = load_area( '/opt/users/cll/official/PyTroll/bluemarble-pyresample/areas.yaml', 'world_plat_5400_2700') area_target = load_area(os.path.join(CONFIG_PATH, "areas.def"), 'EuropeCanaryS95') data = background.split() data = np.dstack((np.array(data[0]), np.array(data[1]), np.array(data[2]))) nn = image.ImageContainerNearest(data, area_bkg, radius_of_influence=50000) area_con_quick = nn.resample(area_target) scipy.misc.imsave(outfile, area_con_quick.image_data)
# https://pyresample.readthedocs.org/en/latest/plot.html#displaying-data-quickly import numpy as np import matplotlib.pyplot as plt import pyresample as pr lons = np.zeros(1000) tb37v = np.arange(1000) #lats = np.arange(-80, -90, -0.01) # orginal example -> shows north pole #area_def = pr.utils.load_area('../cfg_test/areas.def', 'ease_sh') # orginal example -> shows north pole lats = np.arange(80, 90, 0.01) # ??? need to use positive lats for south pole ??? area = 'ease_nh' #area="EuropeCanary95" # this does not produce correct result, shows SEVIRI full disk projection for blue marble !!! area_def = pr.load_area('/data/OWARNA/hau/pytroll/cfg_test/areas.def', area) # ??? need to use nh to show south pole ??? swath_def = pr.geometry.SwathDefinition(lons, lats) result = pr.kd_tree.resample_nearest(swath_def, tb37v, area_def, radius_of_influence=20000, fill_value=None) bmap = pr.plot.area_def2basemap(area_def) bmng = bmap.bluemarble() col = bmap.imshow(result, origin='upper') filename = 'test_basemap_' + area + '.png' plt.savefig('./' + filename, bbox_inches='tight') print("display " + filename + " &")