Convert netcdf to image - python

I have a netcdf file that I would like to convert to an image (joed, png, gif) using a command line tool.
Is someone could please help me with the library name and possibly a link to how it is done.
Regards
David

Others have mentioned commercial solutions with ArcGIS, IDL and Matlab, but here's one way to do it using Python, using the netCDF4 module to read the netcdf file, and matplotlib to create the image. The netCDF4 module will read both NetCDF3, NetCDF4 files, and also read remote NetCDF (or other files) served via the OPeNDAP service. Below I read topography data using the OPeNDAP service, so you should be able to run the program without changes. The netCDF4 module can be a bit difficult to build, but it's included in the Python(x,y), Enthought Canopy and Continuum Anaconda distributions.
import matplotlib.pyplot as plt
import netCDF4
# open a local NetCDF file or remote OPeNDAP URL
url = 'http://www.ngdc.noaa.gov/thredds/dodsC/relief/ETOPO1/thredds/ETOPO1_Bed_g_gmt4.nc'
nc = netCDF4.Dataset(url)
# examine the variables
print nc.variables.keys()
print nc.variables['z']
# sample every 10th point of the 'z' variable
topo = nc.variables['z'][::10,::10]
# make image
plt.figure(figsize=(10,10))
plt.imshow(topo,origin='lower')
plt.title(nc.title)
plt.savefig('image.png', bbox_inches=0)
which produces this image:

Here is the official list of software for manipulating NetCDF.
http://www.unidata.ucar.edu/software/netcdf/software.html
If there is anything it is probably there. But I have a couple of comments on the subject if you are interested.
What platform are you using? Linux, Windows etc? Whatever the answer I think the answer is you will be struggling to find a command line tool with out creating it yourself.
It is probably relatively easy to create something using Java or Python and some GDAL libraries or the like. I have made something similar using ArcGIS if you have this but it is not command line because that is quite difficult to achieve.
Part of the problem you will face is that in using a command line you will need to have additional information as to how it is exported setup before hand but this information does not lend itself to a non-GUI environment.
Questions such as is it going to be grey scale or colour. If colour which colours because these will need to be defined. Say we use blue to red colour ramp then is red a high value or a low value. How is the colour going to be assigned to the values. Will it be gradual or will it be stepped e.g. values 0 - 10 correspond to a single colour then 10 - 20 to another colour.
It's not command line but 'NcView' may work for you.
http://meteora.ucsd.edu/~pierce/ncview_home_page.html

This can be done using my new package ncplot (available through pypi or conda-forge). It works as either a command line tool or in Python, and will generate interactive plots from the NetCDF files.
On the command line just do the following:
ncplot infile.nc
In Python (preferably in a Jupyter notebook). Just do the following:
from ncplot import ncplot
ncplot("infile.nc")

IDV is a good visualization tool for NetCDF, but, as far as I know, there is no command line interface.
I would recommend Matlab. It has read and write functions for NetCDF as well as an extensive plotting library...probably one of the best. You can then compile the matlab code and run it from the command line.

this may help:
http://gmt.soest.hawaii.edu/gmt/html/man/grdimage.html
here some examples:
web.ics.purdue.edu/~ecalais/teaching/gmt/GMT_6.pdf

GDAL can do this and you can control the output colour palettes in a variety of ways, and the rgdal package provides an R interface to some of this.
R has support to read NetCDF files and write to image files via a number of contributed packages, namely ncdf, RNetCDF and ncdf4.

If you want a commandline facility to enable to produce a simple plot without needing to do any coding there is
1. ncview
ncview file.nc
which can produce a ps plot with "print to file" but it is not very helpful as the colorbar legend is not included
2. Panoply
A second point and click method is to use
panoply
which is available here: https://www.giss.nasa.gov/tools/panoply/
3. CDO
The third method is to use CDO from the command line, (but make sure you have compiled it with MAGICS++ support included).
For example, you can make a simple PNG map of data in a netcdf file with the following command:
cdo shaded,device="png" in.nc plot
which will provide a file called "plot_variablename.png"
See the documentation here: https://code.mpimet.mpg.de/projects/cdo/wiki/Tutorial#Plotting

Related

How to pass an R variable to a Python variable in Pycharm?

I am new to Pycharm; however, I want to take advantage of my R and Python knowledge. I am a big fan of both languages, and I am constantly learning more about them.
I am hoping to pass an R variable to a Python variable, similar to Jupyter Notebook.
I could not find any example code anywhere of doing this.
R code
x <- 5
python code
# Some conversion method needs to be added
print(x)
Python Console
>>>5
This is possible because Jupyter provides its own Kernel that code runs in. This kernel is able to translate variables between languages.
Pycharm does not provide a kernel, and instead executes Python code directly through an interpreter on your system. It's similar to doing python my_script.py. AFAIK vanilla Pycharm does not execute R at all.
There are plugins for Pycharm that support R and Jupyter notebooks. You might be able to find one that does what you want.
I usually solve this problem by simply adhering to the Unix philosophy:
Rscript foo.R | python bar.py
where foo.R prints the data to STDOUT and bar.py expects to read it from STDIN (you could also read/write from files). This approach of course requires you to define a clear interface of data between the scripts, rather than passing all the variables indiscriminately, which I don't personally mind because I do that anyway as a matter of design best practices.
There are also packages like reticulate which will allow you to call one language from the other. This might be more useful if you like to switch languages a lot.
Thanks for the above answer! That is great. I did find solution that could help other people that use pycharm.
If you have installed the R plug in option, you can use the following code.
Python code to save to feather file:
```
import pyarrow.feather as feather
data = pd.read_csv('C:/Users/data.csv')
feather.write_feather(data, 'C:/Users/data.feather')
```
R code to retrieve feather file
```
library(tidyverse)
library(feather)
library(arrow)
data <- arrow::read_arrow('C:/Users/data.feather')
print(data)
```
However, this process seems very similar to writing a file to csv in Python, then uploading the csv into R. There could be some type of lightweight storage difference, speed processing, and language variable agnostic/class translation. Below is the official documentation: Git: https://github.com/wesm/feather
Apache Arrow: https://arrow.apache.org/docs/python/install.html
RStudio: https://www.rstudio.com/blog/feather/

Writing PSD files with python

I have a PSD file with 3 layers. What I want to do is change the 3rd, deepest layer's image and save the whole PSD file as PNG.
Is this possible with python or the ubuntu command line?
I've already looked at psd tools but it's only good for exporting images as PNG in my case, I've looked at this blog post but not sure if it will allow me to change layer's image and where to find some documentation
If you really need Python, try to use photoshop-python-api
But even this not support all the functionalities of Photoshop.
Win32com API can give a vast range of functionalities but it's for windows.
Have you ever tried ExtendScript JS for Phothoshop? This is a built-in script editor for Adobe and a script listener is also available so you can easily make functions you need.
Scripting Guide
Scripting Listener

Add a datafile type reader to paraview using pvpython

I'm aware that you can add readers for different datafile types to paraview, however, that all talks about doing a bunch of VTK stuff in c++ and (maybe worse) re-compiling paraview to make it aware of your datafile format. On the other hand, paraview also supports scripting in python. Maybe it is my lack of familiarity with VTK, but, to me it looks like I can only manipulate VTK objects from pvpython. Is there any way to dynamically add a reader to paraview using pvpython?
What you want is a "Python Programmable Filter. Refer to ParaView Wiki. This wiki page shows how to write a CSV reader in Python.
As time has passed, ParaView has new options to achieve this.
It's worth looking at the recently added Python Algorithm feature in ParaView.
Examples are given in the source code.

Creating excel chart from text file

I am running a simulation that saves some result in a file from which I take the data I need and save it in
result.dat
like so:
SAN.statisticsCollector.Network Response Time
Min: 0.210169
Max: 8781.55
average: 346.966666667
I do all this using python, and it was easy to convert result.dat into an excel file using xlwt. The problem is that creating charts using xlwt is not possible. I than came across Jpype, but installation on my ubuntu 12.04 machine was a headache. I'm probably just being lazy but still - is there any other way, not necessarily python-related, to convert result.dat into an excel file with charts?
Thanks
P.s the file I want to create is a spreadsheet, not Microsoft's Excel!
there is a now a new possibility: http://pythonhosted.org/openpyxl/charts.html
and http://xlsxwriter.readthedocs.org/chart.html
The main problem is that currently there's no Python library that implements MS Excel chart creation, and, obviously, they will not appear due to lack of good chart format documentation (as python-excel.org guys told) and its huge complecity.
There are two other options though:
Another option is to use 3-rd party tools (like JPype that you've mentioned) combining them with Python scripts. As far as I know, except Java smartXML there's no libraries that are capable of creating excel charts (or course, there are ones for .NET, e.g. expertXLS) and I'm not sure it will run on Mono + IronPython, though you can try.
The third option is Win32 COM API, e.g. like described in this SO post, which is not quite an option for you due to your working operating system.

Templatizing images using gimp

My wife recently started a business making soap bars and the soap labels have quickly spiraled out of control into tons of diverging Gimp .xcf files. The only difference between the files are the names of the product, description, and ingredients. I'd like to make a template and produce the labels from a .xcf file and maybe a .xml or .ini file that has the text data. I've been eyeing python-fu, but I'm not quite sure if it does what I'd like. I'd like to stick to python where possible because it's simple.
Are there facilities in python + gimp to do what I'd like? Could something similar be done in anything other than gimp?
Edit: Additionally, I have to make pages of stickers to print, so some means of optimizing the number of stickers that can fit on a page (2d bin pack?) would be a big plus.
The Python Imaging Library (PIL) can quite easily read in an image, put some text on it, and write it back out. Use the Image module to read and write, and the ImageDraw module to add the text.
I doubt that it can use the .xcf format though, you'll probably want to convert to .png.
You can script GIMP in Python, and pretty much everything you can do o n the prgoram can be done via the API -- you can check for the available API functions in help->procedure browser.
To enable Python scripting in gimp 2.6 under Windows, you have to google for it -- Python, python gtk and one other package have to be installed before GIMP.
Python's PIL is fine for simple images, but it is weak in arttistic pixel manipulation, which is available with GIMP.

Categories

Resources