aperezhortal / SkewTplus

Atmospheric Profile Plotting and Diagnostics
Other
9 stars 8 forks source link

Save a plot straight forward into file without X requirement #2

Closed peters77 closed 5 years ago

peters77 commented 5 years ago

Problem: When the X server loaded, saving into a file is not working:

import matplotlib
matplotlib.use('Agg') # Needed when no X server is present.
import matplotlib.pyplot as plt

fig = mySounding.quickPlot()
fig.savefig('skewt_idar-oberstein.png')

System:

Solution: If no X window system is installed there should be an option/command to save the plot into a file.

aperezhortal commented 5 years ago

Hi Peters, thanks for the suggestion!

The quickPlot function was only designed to show the plots. Also, it didn't return the figure instance.

I'm working in the solution. I'll add a new keyword to quickPlot for saving to a file. By default, it shows the plot. If a filename is passed to it, it will attempt to save the file.

I found some problems with the figure manager that I need to solve to implement this so it will take some time.

peters77 commented 5 years ago

Great! I noticed there is even a mySkewT_Figure.show() function. So I don' t know where to implement the save option preferably...in quickPlot or better in mySkewT_Figure.show()?

aperezhortal commented 5 years ago

I fixed it in the last commit a25dd5f3d0cac16da9cb50656a9ecf82e7832978

The quickPlot function has an additional optional argument: output_file If it is not set, the plot is showed. If a path is given, it saves the plot to a file.

Also I added to more methods to skewT.figure class. show_plot and save_fig

Check the updated examples.

The minimal example to work without the X server is:

#Load the sounding datas
mySounding = sounding("./exampleSounding.txt")

# If no X server is available to visualize the plot, save it to file
import matplotlib
matplotlib.use('Agg')

# Save the SkewT diagram of the Sounding
mySounding.quickPlot(output_file="quick_sounding.pdf")
peters77 commented 5 years ago

I have some trouble to update to the latest git version. I tried: pip install -U git+https://github.com/aperezhortal/SkewTplus.git

but got:

Collecting git+https://github.com/aperezhortal/SkewTplus.git
  Cloning https://github.com/aperezhortal/SkewTplus.git to /tmp/pip-req-build-tueb3fxs
Requirement already satisfied, skipping upgrade: matplotlib in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from SkewTplus==1.1.2) (2.2.3)
Requirement already satisfied, skipping upgrade: numpy in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from SkewTplus==1.1.2) (1.15.0)
Requirement already satisfied, skipping upgrade: netCDF4 in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from SkewTplus==1.1.2) (1.4.1)
Requirement already satisfied, skipping upgrade: requests in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from SkewTplus==1.1.2) (2.19.1)
Requirement already satisfied, skipping upgrade: six in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from SkewTplus==1.1.2) (1.11.0)
Requirement already satisfied, skipping upgrade: cycler>=0.10 in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from matplotlib->SkewTplus==1.1.2) (0.10.0)
Requirement already satisfied, skipping upgrade: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from matplotlib->SkewTplus==1.1.2) (2.2.2)
Requirement already satisfied, skipping upgrade: python-dateutil>=2.1 in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from matplotlib->SkewTplus==1.1.2) (2.7.3)
Requirement already satisfied, skipping upgrade: pytz in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from matplotlib->SkewTplus==1.1.2) (2018.5)
Requirement already satisfied, skipping upgrade: kiwisolver>=1.0.1 in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from matplotlib->SkewTplus==1.1.2) (1.0.1)
Requirement already satisfied, skipping upgrade: cftime in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from netCDF4->SkewTplus==1.1.2) (1.0.1)
Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from requests->SkewTplus==1.1.2) (3.0.4)
Requirement already satisfied, skipping upgrade: idna<2.8,>=2.5 in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from requests->SkewTplus==1.1.2) (2.7)
Requirement already satisfied, skipping upgrade: urllib3<1.24,>=1.21.1 in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from requests->SkewTplus==1.1.2) (1.23)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from requests->SkewTplus==1.1.2) (2018.11.29)
Requirement already satisfied, skipping upgrade: setuptools in /home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages (from kiwisolver>=1.0.1->matplotlib->SkewTplus==1.1.2) (40.4.0)
Building wheels for collected packages: SkewTplus
  Running setup.py bdist_wheel for SkewTplus ... error
  Complete output from command /home/cpeters/anaconda2/envs/pytroll/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-req-build-tueb3fxs/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-96p4oc_y --python-tag cp36:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.6
  creating build/lib.linux-x86_64-3.6/SkewTplus
  copying SkewTplus/skewT.py -> build/lib.linux-x86_64-3.6/SkewTplus
  copying SkewTplus/errorHandling.py -> build/lib.linux-x86_64-3.6/SkewTplus
  copying SkewTplus/thermodynamics.py -> build/lib.linux-x86_64-3.6/SkewTplus
  copying SkewTplus/sounding.py -> build/lib.linux-x86_64-3.6/SkewTplus
  copying SkewTplus/__init__.py -> build/lib.linux-x86_64-3.6/SkewTplus
  running build_ext
  building 'SkewTplus._thermodynamics' extension
  creating build/temp.linux-x86_64-3.6
  creating build/temp.linux-x86_64-3.6/SkewTplus
  gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -m64 -fPIC -m64 -fPIC -fPIC -I/home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages/numpy/core/include -I/home/cpeters/anaconda2/envs/pytroll/include/python3.6m -c SkewTplus/_thermodynamics.cpp -o build/temp.linux-x86_64-3.6/SkewTplus/_thermodynamics.o -fopenmp
  gcc: error: SkewTplus/_thermodynamics.cpp: No such file or directory
  gcc: fatal error: no input files
  compilation terminated.
  error: command 'gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for SkewTplus
  Running setup.py clean for SkewTplus
Failed to build SkewTplus
Installing collected packages: SkewTplus
  Found existing installation: SkewTplus 1.1.2
    Uninstalling SkewTplus-1.1.2:
      Successfully uninstalled SkewTplus-1.1.2
  Running setup.py install for SkewTplus ... error
    Complete output from command /home/cpeters/anaconda2/envs/pytroll/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-req-build-tueb3fxs/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-vallz05o/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    creating build/lib.linux-x86_64-3.6/SkewTplus
    copying SkewTplus/skewT.py -> build/lib.linux-x86_64-3.6/SkewTplus
    copying SkewTplus/errorHandling.py -> build/lib.linux-x86_64-3.6/SkewTplus
    copying SkewTplus/thermodynamics.py -> build/lib.linux-x86_64-3.6/SkewTplus
    copying SkewTplus/sounding.py -> build/lib.linux-x86_64-3.6/SkewTplus
    copying SkewTplus/__init__.py -> build/lib.linux-x86_64-3.6/SkewTplus
    running build_ext
    building 'SkewTplus._thermodynamics' extension
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/SkewTplus
    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -m64 -fPIC -m64 -fPIC -fPIC -I/home/cpeters/anaconda2/envs/pytroll/lib/python3.6/site-packages/numpy/core/include -I/home/cpeters/anaconda2/envs/pytroll/include/python3.6m -c SkewTplus/_thermodynamics.cpp -o build/temp.linux-x86_64-3.6/SkewTplus/_thermodynamics.o -fopenmp
    gcc: error: SkewTplus/_thermodynamics.cpp: No such file or directory
    gcc: fatal error: no input files
    compilation terminated.
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
  Rolling back uninstall of SkewTplus
Command "/home/cpeters/anaconda2/envs/pytroll/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-req-build-tueb3fxs/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-vallz05o/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-req-build-tueb3fxs/

Is there another way to upgrade to the recent version?! Sorry...I'm not very skilled!

aperezhortal commented 5 years ago

There was a missing file in the installer. Probably you don't have Cython installed. In that case the installer will try to use the cpp sources provided in the installer.

I fixed the issue. Otherwise, try doing pip install cython before installing the package.

peters77 commented 5 years ago

Error fixed,

Save the SkewT diagram of the Sounding
mySounding.quickPlot(output_file="quick_sounding.pdf")

works very well now!

Thanks for the quick implementation. Will try the more enhanced examples.