Subtracting Images

Post any questions, ideas, or topics related to Jython and Python scripting.
User avatar
abhiram
Posts: 7
Joined: Thu Oct 23, 2014 12:35 pm

Subtracting Images

Post by abhiram »

Hello,

I am trying to find out a way to subtract Ch4 image - Ch2 image ( temperature values) for each day and save the temperature difference as a new area file.I am hoping that it is possible to script this job using Jython. I would really appreciate your inputs on the problem.

I have two area files from GOES13 one for Chan2 and Chan4. At a basic level I would like to load these two area files using Jython and then compute the difference in terms of temperature values.

Any inputs are appreciated.

Thanks!
User avatar
bobc
Posts: 988
Joined: Mon Nov 15, 2010 5:57 pm

Re: Subtracting Images

Post by bobc »

Hello abhiram -

As of right now, there's no way of generating AREA files in McIDAS-V through scripting. However, you are able to capture images as well as netCDF files. Here is a sample script that you can use to create a local dataset, pull in the data, subtract the bands, display the data, and save off the display as an image and a netCDF file. Note that you will need to change dataPath to point to the directory storing your data, and outputPath to the directory where you want your image/netCDF file to be saved to.

Code: Select all

# Directory path where AREAs are stored and for output
dataPath = 'path/to/GOES/AREA/files/'
outputPath = 'path/to/output/directory/'

# Dictionary to pass to makeLocalADDEEntry
goesDict = dict(
    save = True,
    mask = dataPath,
    dataset = 'GOES',
    imageType = 'GOES AREA files',
    format = 'AREA',
    )

# Creating a local dataset
goesArea = makeLocalADDEEntry(**goesDict)

# Pulling in data for bands 2 and 4
desc = getLocalADDEEntry(dataset='GOES',imageType='AREA')
metadata2,data2 = getADDEImage(localEntry=desc,band=2,unit='TEMP')
metadata4,data4 = getADDEImage(localEntry=desc,band=4,unit='TEMP')

# Creating a layer that subtracts band 2 Temps from band 4 Temps
subData = sub(data4,data2)

# Build window, display data, and set enhancement and range values
panel = buildWindow()
layer = panel[0].createLayer('Image Display',subData)
layer.setEnhancement('Inverse Gray Scale',range=(-20,20))

# Capture an image to the outputPath directory
panel[0].captureImage(outputPath+'bandsub.png')

# Export the image as a netCDF file
exportGridToNetcdf(subData,outputPath+'bandsub.nc')

You can find documentation on makeLocalADDEEntry, getLocalADDEEntry, and getADDEImage on the Create an Object scripting page in the McIDAS-V User's Guide.

I will search through our inquiry system to see if anyone has requested the ability to write AREA files in McIDAS-V. If one does not exist, I will write one up as an inquiry for development.

Thanks -
Bob Carp
McIDAS Help Desk
User avatar
bobc
Posts: 988
Joined: Mon Nov 15, 2010 5:57 pm

Re: Subtracting Images

Post by bobc »

Hi again abhiram -

One of our users noted to me that there are some issues with the exportGirdToNetcdf function. Currently, the netCDF file is created from the actual image and not the data, and this can lead to some navigation issues in the netCDF file. This is written up as an inquiry for development. One of our programmers has commented that this function is more intended for gridded data than satellite imagery. If you choose to create netCDF files, please keep the potential navigation issues in mind.

Thanks -
Bob Carp
User avatar
abhiram
Posts: 7
Joined: Thu Oct 23, 2014 12:35 pm

Re: Subtracting Images

Post by abhiram »

Hello Bob,

Firstly, thanks for responding quickly.

From your reply, it seems like the netcdf files won't be having any navigational data in them ( Unlike the area files with their NAV block). Is that a fair understanding?

My aim for computing the difference is that at a later stage I plan to write down at a script that can give me the difference value at a specific location (using gps co-ordinates)

Thanks!
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: Subtracting Images

Post by tomw »

You could pretty easily create a stand-alone Jython script to read and compute the differences on-the-fly using just Jython code and without having to start McIDAS-V ... assuming that for this function, you don't need the selection or display power it provides.

The ADDE servers can return a very small region (say, 1x1 -- although the servers will return a 1x4 set of data, since the number of elements must be a mulitple of 4) and you can request the data at a particular latitude / longitude coordinate so your program/script would not have to do any navigation itself.

Although I'm not completely certain what your needs are, an approach like this could be efficient to get a single value of the difference at a given geographic location....

I would be concerned that if you wrote a netCDF or AREA file with the differences, you would have to a) read a large file and b) use the navigation routines in the McV/VisAD libarary to get the desired data value.

...just a thought...
User avatar
abhiram
Posts: 7
Joined: Thu Oct 23, 2014 12:35 pm

Re: Subtracting Images

Post by abhiram »

tomw wrote:The ADDE servers can return a very small region (say, 1x1 -- although the servers will return a 1x4 set of data, since the number of elements must be a mulitple of 4) and you can request the data at a particular latitude / longitude coordinate so your program/script would not have to do any navigation itself.

I kind of arrived at the same idea. It seems simpler than creating another area file. Btw, how do I grab the data-values at a specific location?

Thanks!
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: Subtracting Images

Post by tomw »

abhiram wrote:I kind of arrived at the same idea. It seems simpler than creating another area file. Btw, how do I grab the data-values at a specific location?


The key is to request the data from the server with the desired lat/lon coordinates -- that way your code does not need to do any "navigation" at all....just pull out the first data value. You'll want a "coordinate system" of LATLON and a "place" of ULEFT. For the getADDEImage, look at:
http://www.ssec.wisc.edu/mcidas/doc/mcv_guide/1.4/index.php?page=misc/Scripting/CreateObject.html
I will have to leave it to others to comment on how to get the data values from the returned object...although
it is likely to be "getFloats()" [I'm just not sure what the type of the object is...sorry]

If you want to use AreaFile, then see the info at:
http://www.ssec.wisc.edu/visad-docs/javadoc/edu/wisc/ssec/mcidas/AreaFile.html
and use the "getData()" method to return an array of pointers. The URL request string to use is detailed at:
http://www.ssec.wisc.edu/visad-docs/javadoc/edu/wisc/ssec/mcidas/adde/AddeURLConnection.html

You would then have something like this:

Code: Select all

from edu.wisc.ssec.mcidas import AreaFile as af
url = "adde://your.server.edu/imagedata?group=MYDATA&type=GOES&DAY=2010123&TIME=17:45 17:45 I &BAND=4 &LATLON=45 90&SIZE=1 1&USER=abcd&PROJ=1111"
a = af(url);
value = a.getData();
User avatar
bobc
Posts: 988
Joined: Mon Nov 15, 2010 5:57 pm

Re: Subtracting Images

Post by bobc »

Hi abhiram -

Another approach you can consider to get data values at a specific location would be the Data Probe/Time Series display. On the McIDAS-V Documentation page, there is a Scripting tutorial. One of the scripts included in the tutorial is classify-movie.py. This script creates a local dataset and creates a loop of subtracted bands. You should be able to use this script as a template.

From here, you could tack on a line like:

Code: Select all

probeLayer = panel[0].createLayer('Data Probe/Time Series',myLoop)

This places a data probe in the Main Display window, with the probe output in the Layer Controls tab of the Data Explorer. You can adjust the location of the probe by left-click+dragging it in the Main Display window, or by typing Lat/Lon values at the bottom of the Layer Controls tab. From here, you can export the data values at each timestep using 'File > Save > Export All Times as CSV' in the Layer Controls tab of the Data Explorer.

- Bob
User avatar
dl08
Posts: 2
Joined: Fri Oct 31, 2014 6:13 pm

Re: Subtracting Images

Post by dl08 »

We are testing the use of the fog product in detecting fog in Florida using GOES-13 area files. We have 366 image pairs for the year 2012 (one scan time per day) and want to subtract ch4(temperatures)-ch2(temperatures) to create a fog product.

We have a text file with the lat/lon locations of 75 AWOS/ASOS sites in Florida. Potentially, we would like to create a loop that extracts the temp difference at each of the station lat/lon locations and writes these values to an output file for all 366 fog images. Is it possible to request data from the server for multiple lat/lon coordinates at once?
User avatar
jayh
Posts: 424
Joined: Thu Jan 15, 2009 10:34 pm

Re: Subtracting Images

Post by jayh »

Hello dl08-

I queried the programmer and he said that Jython scripts can easily read and write text files because Python is fairly rich in that regard. It does not matter if the script is running "inside" McV or not.

He included this link:
https://docs.python.org/2/tutorial/inputoutput.html

You could use these constructs to read in the lat/lons and then incorporate that with some of the Data Probe methods above to possibly get what you want.

Thanks, Jay
McIDAS Help Desk
Post Reply