def process_file(self, wrfout_path, var_list, skip=1): """ Process an entire file, all timestamps and generate images for var_instr.keys(). :param wrfout_path: the wrfout to process :param var_list: list of variables to process :param skip: only process every skip-th frame """ traceargs() # open the netCDF dataset d = nc4.Dataset(wrfout_path) # extract ESMF string times and identify timestamp of interest times = [''.join(x) for x in d.variables['Times'][:]] # build one KMZ per variable fixed_colorbars = {} for tndx, ts_esmf in enumerate(times[::skip]): print('Processing time %s ...' % ts_esmf) for var in var_list: try: outpath_base = os.path.join(self.output_path, self.product_name + '-' + ts_esmf + '-' + var) if var in ['WINDVEC']: kmz_path,raster_path,coords = self._vector2kmz(d, var, tndx, outpath_base, cleanup=False) raster_name = osp.basename(raster_path) kmz_name = osp.basename(kmz_path) self._update_manifest(dom_id, ts_esmf, var, { 'raster' : raster_name, 'coords' : coords, 'kml' : kmz_name}) else: kmz_path,raster_path,cb_path,coords = self._scalar2kmz(d, var, tndx, outpath_base, cleanup=False) mf_upd = { 'raster' : osp.basename(raster_path), 'coords' : coords, 'kml' : osp.basename(kmz_path) } if cb_path is not None: # optimization for display when we know colorbar has fixed scale # we memoize the colorbar computed at time 0 and use it for all frames # although other colorbars are generated, they are deleted scale = self.wisdom_update.get('scale', get_wisdom(var)['scale']) if type(scale) == list and np.isfinite(scale[0]) and np.isfinite(scale[1]): logging.info("Using fixed colorbar strategy for variable " + var) if tndx == 0: fixed_colorbars[var] = osp.basename(cb_path) else: os.remove(cb_path) mf_upd['colorbar'] = fixed_colorbars[var] else: mf_upd['colorbar'] = osp.basename(cb_path) self._update_manifest(dom_id, ts_esmf, var, mf_upd) except Exception as e: logging.warning("Exception %s while postprocessing %s for time %s" % (e.message, var, ts_esmf)) logging.warning(traceback.print_exc())
def process_vars(self, wrfout_path, dom_id, ts_esmf, vars): """ Postprocess a list of scalar or vector fields at a given simulation time into PNG and KMZ files. :param wrfout_path: WRF file to process :param dom_id: the domain identifier :param ts_esmf: time stamp in ESMF format :param vars: list of variables to process """ traceargs() logging.info('process_vars: looking for time %s in %s' % (ts_esmf,wrfout_path)) # open the netCDF dataset d = nc4.Dataset(wrfout_path) # extract ESMF string times and identify timestamp of interest times = [''.join(x) for x in d.variables['Times'][:]] if ts_esmf not in times: logging.error('process_vars: cannot find time %s in %s' % (ts_esmf,wrfout_path)) logging.info('process_vars: Available times: %s' % times) raise PostprocError("process_vars: Time %s not in %s" % (ts_esmf,osp.basename(wrfout_path))) tndx = times.index(ts_esmf) # build an output file per variable for var in vars: logging.info('process_vars: postprocessing %s for time %s' % (var, ts_esmf)) try: outpath_base = os.path.join(self.output_path, self.product_name + ("-%02d-" % dom_id) + ts_esmf + "-" + var) kmz_path, raster_path, cb_path, coords, mf_upd = None, None, None, None, {} if var in ['WINDVEC']: kmz_path,raster_path,coords = self._vector2kmz(d, var, tndx, outpath_base, cleanup=False) else: kmz_path,raster_path,cb_path,coords = self._scalar2kmz(d, var, tndx, outpath_base, cleanup=False) if cb_path is not None: mf_upd['colorbar'] = osp.basename(cb_path) mf_upd['kml'] = osp.basename(kmz_path) mf_upd['raster'] = osp.basename(raster_path) mf_upd['coords'] = coords self._update_manifest(dom_id, ts_esmf, var, mf_upd) except Exception as e: logging.warning("Exception %s while postprocessing %s for time %s" % (e.message, var, ts_esmf)) logging.warning(traceback.print_exc()) d.close()
def process_file(self, wrfout_path, var_list, skip=1): """ Process an entire file, all timestamps and generate images for var_instr.keys(). :param wrfout_path: the wrfout to process :param var_list: list of variables to process :param skip: only process every skip-th frame """ traceargs() # open the netCDF dataset d = nc4.Dataset(wrfout_path) # extract ESMF string times and identify timestamp of interest times = [''.join(x) for x in d.variables['Times'][:]] # build one KMZ per variable fixed_colorbars = {} for tndx, ts_esmf in enumerate(times[::skip]): print('Processing time %s ...' % ts_esmf) for var in var_list: try: outpath_base = os.path.join( self.output_path, self.product_name + '-' + ts_esmf + '-' + var) if is_windvec(var): kmz_path, raster_path, coords = self._vector2kmz( d, var, tndx, ts_esmf, outpath_base, cleanup=False) raster_name = osp.basename(raster_path) kmz_name = osp.basename(kmz_path) self._update_manifest( dom_id, ts_esmf, var, { 'raster': raster_name, 'coords': coords, 'kml': kmz_name }) else: kmz_path, raster_path, cb_path, coords = self._scalar2kmz( d, var, tndx, ts_esmf, outpath_base, cleanup=False) mf_upd = { 'raster': osp.basename(raster_path), 'coords': coords, 'kml': osp.basename(kmz_path) } if cb_path is not None: # optimization for display when we know colorbar has fixed scale # we memoize the colorbar computed at time 0 and use it for all frames # although other colorbars are generated, they are deleted scale = self.wisdom_update.get( 'scale', get_wisdom(var)['scale']) if type(scale) == list and np.isfinite( scale[0]) and np.isfinite(scale[1]): logging.info( "Using fixed colorbar strategy for variable " + var) if tndx == 0: fixed_colorbars[var] = osp.basename( cb_path) else: os.remove(cb_path) mf_upd['colorbar'] = fixed_colorbars[var] else: mf_upd['colorbar'] = osp.basename(cb_path) self._update_manifest(dom_id, ts_esmf, var, mf_upd) except Exception as e: logging.warning( "Exception %s while postprocessing %s for time %s" % (e.message, var, ts_esmf)) logging.warning(traceback.print_exc())
def process_vars(self, wrfout_path, dom_id, ts_esmf, vars): """ Postprocess a list of scalar or vector fields at a given simulation time into PNG and KMZ files. :param wrfout_path: WRF file to process :param dom_id: the domain identifier :param ts_esmf: time stamp in ESMF format :param vars: list of variables to process """ traceargs() logging.info('process_vars: looking for time %s in %s' % (ts_esmf, wrfout_path)) # open the netCDF dataset d = nc4.Dataset(wrfout_path) # extract ESMF string times and identify timestamp of interest times = [''.join(x) for x in d.variables['Times'][:]] if ts_esmf not in times: logging.error('process_vars: cannot find time %s in %s' % (ts_esmf, wrfout_path)) logging.info('process_vars: Available times: %s' % times) raise PostprocError("process_vars: Time %s not in %s" % (ts_esmf, osp.basename(wrfout_path))) tndx = times.index(ts_esmf) # build an output file per variable for var in vars: logging.info('process_vars: postprocessing %s for time %s' % (var, ts_esmf)) try: outpath_base = os.path.join( self.output_path, self.product_name + ("-%02d-" % dom_id) + ts_esmf + "-" + var) kmz_path, raster_path, cb_path, coords, mf_upd = None, None, None, None, {} if is_windvec(var): kmz_path, raster_path, coords = self._vector2kmz( d, var, tndx, ts_esmf, None, outpath_base, cleanup=False) else: kmz_path, raster_path, cb_path, coords = self._scalar2kmz( d, var, tndx, ts_esmf, None, outpath_base, cleanup=False) if cb_path is not None: mf_upd['colorbar'] = osp.basename(cb_path) mf_upd['kml'] = osp.basename(kmz_path) mf_upd['raster'] = osp.basename(raster_path) mf_upd['coords'] = coords self._update_manifest(dom_id, ts_esmf, var, mf_upd) except Exception as e: logging.warning( "Exception %s while postprocessing %s for time %s" % (e.message, var, ts_esmf)) logging.warning(traceback.print_exc()) d.close()