예제 #1
0
    def get_open_file_name(self):
        """Ask the user for a filename for saving the data frame"""
        def check_current():
            dirname = osp.dirname(current)
            if osp.exists(dirname) and osp.isdir(dirname):
                return dirname

        current = self.txt_fname.text().strip()
        start = None
        if current:
            start = check_current()
        if start is None:
            for attr in 'project_file', 'image_file':
                try:
                    current = self.stradi.get_attr(attr)
                except KeyError:
                    pass
                else:
                    start = check_current()
                    if start is not None:
                        break
        if start is None:
            start = os.getcwd()
        fname = QFileDialog.getSaveFileName(
            self, 'DataFrame file destination', start,
            'Excel files (*.xlsx *.xls);;'
            'csv files (*.csv);;'
            'All files (*)')
        if with_qt5:  # the filter is passed as well
            fname = fname[0]
        if not fname:
            return
        self.txt_fname.setText(fname)
예제 #2
0
파일: main.py 프로젝트: Chilipp/psyplot-gui
 def _save_preset(self, project, *args, **kwargs):
     fname, ok = QFileDialog.getSaveFileName(
         self, 'Save preset', os.path.join(get_configdir(), 'presets'),
         'YAML file (*.yml *.yaml);;'
         'All files (*)')
     if ok:
         project.save_preset(fname, *args, **kwargs)
예제 #3
0
    def _save_image(self, image, fname=None):
        """Save an image to a file

        Parameters
        ----------
        image: PIL.Image.Image
            The image to save
        fname: str or None
            The path of the target filename where to save the `image`. If None,
            A QFileDialog is opened and we ask the user for a filename"""
        if fname is None or not isinstance(fname, six.string_types):
            fname = QFileDialog.getSaveFileName(
                self.straditizer_widgets, 'Straditizer file destination',
                self._start_directory, 'All images '
                '(*.png *.jpeg *.jpg *.pdf *.tif *.tiff);;'
                'Joint Photographic Experts Group (*.jpeg *.jpg);;'
                'Portable Document Format (*.pdf);;'
                'Portable Network Graphics (*.png);;'
                'Tagged Image File Format(*.tif *.tiff);;'
                'All files (*)')
            if with_qt5:  # the filter is passed as well
                fname = fname[0]
        if not fname:
            return
        ext = osp.splitext(fname)[1]
        if ext.lower() in ['.jpg', '.jpeg', '.pdf'] and image.mode == 'RGBA':
            image = rgba2rgb(image)
        image.save(fname)
예제 #4
0
파일: main.py 프로젝트: Chilipp/psyplot-gui
 def _export_project(self, p, *args, **kwargs):
     fname = QFileDialog.getSaveFileName(
         self, 'Picture destination', os.getcwd(), 'PDF files (*.pdf);;'
         'Postscript file (*.ps);;'
         'PNG image (*.png);;'
         'JPG image (*.jpg *.jpeg);;'
         'TIFF image (*.tif *.tiff);;'
         'GIF image (*.gif);;'
         'All files (*)')
     if with_qt5:  # the filter is passed as well
         fname = fname[0]
     if not fname:
         return
     try:
         p.export(fname, *args, **kwargs)
     except Exception:
         self.error_msg.showTraceback(
             '<b>Could not export the figures!</b>')
예제 #5
0
 def _export_project(self, p, *args, **kwargs):
     fname = QFileDialog.getSaveFileName(
         self, 'Picture destination', os.getcwd(),
         'PDF files (*.pdf);;'
         'Postscript file (*.ps);;'
         'PNG image (*.png);;'
         'JPG image (*.jpg *.jpeg);;'
         'TIFF image (*.tif *.tiff);;'
         'GIF image (*.gif);;'
         'All files (*)'
         )
     if not fname:
         return
     try:
         p.export(fname, *args, **kwargs)
     except:
         self.error_msg.showTraceback(
             '<b>Could not export the figures!</b>')
예제 #6
0
 def _save_project(self, p, new_fname=False, *args, **kwargs):
     if new_fname or 'project_file' not in p.attrs:
         fname = QFileDialog.getSaveFileName(
             self, 'Project destination', os.getcwd(),
             'Pickle files (*.pkl);;'
             'All files (*)'
             )
         if not fname:
             return
     else:
         fname = p.attrs['project_file']
     try:
         p.save_project(fname, *args, **kwargs)
     except:
         self.error_msg.showTraceback('<b>Could not save the project!</b>')
     else:
         p.attrs['project_file'] = fname
         if p.is_main:
             self.update_project_action(p.num)
예제 #7
0
파일: main.py 프로젝트: Chilipp/psyplot-gui
 def _save_project(self, p, new_fname=False, *args, **kwargs):
     if new_fname or 'project_file' not in p.attrs:
         fname = QFileDialog.getSaveFileName(
             self, 'Project destination', os.getcwd(),
             'Pickle files (*.pkl);;'
             'All files (*)')
         if with_qt5:  # the filter is passed as well
             fname = fname[0]
         if not fname:
             return
     else:
         fname = p.attrs['project_file']
     try:
         p.save_project(fname, *args, **kwargs)
     except Exception:
         self.error_msg.showTraceback('<b>Could not save the project!</b>')
     else:
         p.attrs['project_file'] = fname
         if p.is_main:
             self.update_project_action(p.num)
예제 #8
0
    def save_straditizer_as(self, fname=None):
        """Save the straditizer to a file

        Parameters
        ----------
        fname: str or ``None``
            If None, a QFileDialog is opened and the user has to provide a
            filename. The final action then depends on the ending of the
            chose file:

            ``'.pkl'``
                We save the straditizer with :meth:`pickle.dump`
            else
                We use the
                :meth:`straditize.straditizer.Straditizer.to_dataset` method
                and save the resulting dataset using the
                :meth:`xarray.Dataset.to_netcdf` method"""
        if fname is None or not isinstance(fname, six.string_types):
            fname = QFileDialog.getSaveFileName(
                self.straditizer_widgets, 'Straditizer file destination',
                self._start_directory,
                ('NetCDF files (*.nc *.nc4);;Pickle files (*.pkl);;'
                 'All files (*)'))
            if with_qt5:  # the filter is passed as well
                fname = fname[0]
        if not fname:
            return
        ending = os.path.splitext(fname)[1]
        self.straditizer.set_attr('saved', str(dt.datetime.now()))
        self.straditizer.set_attr('project_file', fname)
        if ending == '.pkl':
            self.straditizer.save(fname)
        else:
            ds = self.straditizer.to_dataset()
            # -- Compression with a level of 4. Requires netcdf4 engine
            comp = dict(zlib=True, complevel=4)
            encoding = {var: comp for var in ds.data_vars}

            ds.to_netcdf(fname, encoding=encoding, engine='netcdf4')