Exemplo n.º 1
0
    def vars2png(self, wrfout_path, dom_id, ts_esmf, vars):
        """
        Postprocess a list of scalar fields into 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
        """
        # 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:
            raise PostprocError("vars2png: Invalid timestamp %s" % ts_esmf)
        tndx = times.index(ts_esmf)

        # build one KMZ per variable
        for var in vars:
            try:
                outpath_base = os.path.join(
                    self.output_path, self.product_name + ("-%02d-" % dom_id) +
                    ts_esmf + "-" + var)
                if is_windvec(var):
                    raster_path, coords = self._vector2png(
                        d, var, tndx, outpath_base)
                    raster_name = osp.basename(raster_path)
                    self._update_manifest(dom_id, ts_esmf, var, {
                        'raster': raster_name,
                        'coords': coords
                    })
                else:
                    raster_path, cb_path, coords = self._scalar2png(
                        d, var, tndx, outpath_base)
                    mf_upd = {
                        'raster': osp.basename(raster_path),
                        'coords': coords
                    }
                    if cb_path is not None:
                        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 into PNG"
                    % (e.message, var, ts_esmf))
                logging.warning(traceback.print_exc())
Exemplo n.º 2
0
def process_vars_tiff(pp, d, wrfout_path, dom_id, times, vars):
    """
    Postprocess a list of scalar or vector fields for a given wrfout file into TIFF files.

    :param pp: Postprocess class
    :param d: the open netCDF file
    :param dom_id: the domain identifier
    :param times: list of times to process
    :param vars: list of variables to process
    """

    logging.info('process_vars_tiff: looking for file %s' % wrfout_path)
    # netCDF WRF metadata
    projection, geotransform_atm, geotransform_fire = ncwrfmeta(d)

    outpath_base = osp.join(pp.output_path, pp.product_name + ("-%02d-" % dom_id))
    # build an output file per variable
    for var in vars:
        logging.info('process_vars_tiff: postprocessing %s' % var)
        try:
            tiff_path, coords, mf_upd = None, None, {}
            wisdom = get_wisdom(var).copy()
            wisdom.update(pp.wisdom_update.get(var, {}))
            if is_fire_var(var):
                geotransform = geotransform_fire
            else:
                geotransform = geotransform_atm

            if is_windvec(var):
                u_name, v_name = wisdom['components']
                uw, vw = get_wisdom(u_name), get_wisdom(v_name)
                uw.update(pp.wisdom_update.get(u_name, {}))
                vw.update(pp.wisdom_update.get(v_name, {}))
                tiff_path = vector2tiffs(outpath_base, d, (wisdom,uw,vw), projection, geotransform, times, var)
            else:
                tiff_path = scalar2tiffs(outpath_base, d, wisdom, projection, geotransform, times, var)

            for idx,time in enumerate(times):
                mf_upd['tiff'] = osp.basename(tiff_path[idx])
                ts_esmf = time.replace('_','T')+'Z'
                pp._update_manifest(dom_id, ts_esmf, var, mf_upd)

        except Exception as e:
            logging.warning("Exception %s while postprocessing %s" % (e, var))
            logging.warning(traceback.print_exc())
Exemplo n.º 3
0
    def vars2kmz(self, wrfout_path, dom_id, ts_esmf, vars):
        """
        Postprocess a list of scalar fields at a given simulation time into 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
        """
        # 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:
            raise PostprocError("vars2kmz: Invalid timestamp %s" % ts_esmf)
        tndx = times.index(ts_esmf)

        # build one KMZ per variable
        for var in vars:
            try:
                outpath_base = os.path.join(
                    self.output_path, self.product_name + ("-%02d-" % dom_id) +
                    ts_esmf + "-" + var)
                kmz_path = None
                if is_windvec(var):
                    kmz_path, _, _ = self._vector2kmz(d, var, tndx, ts_esmf,
                                                      outpath_base)
                else:
                    kmz_path, _, _, _ = self._scalar2kmz(
                        d, var, tndx, ts_esmf, outpath_base)
                kmz_name = osp.basename(kmz_path)
                self._update_manifest(dom_id, ts_esmf, var, {'kml': kmz_name})

            except Exception as e:
                logging.warning(
                    "Exception %s while postprocessing %s for time %s into KMZ"
                    % (e.message, var, ts_esmf))
                logging.warning(traceback.print_exc())
Exemplo n.º 4
0
    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())
Exemplo n.º 5
0
    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()