[R] translating IDL to R
Michael Sumner
mdsumner at gmail.com
Tue Jul 24 02:03:05 CEST 2012
R can do all this, but you'll need to get into specifics a little
more. Most of your code is covered by R's ?read.table, ?data.frame,
?array, ?file and ?Extract. See the ncdf (or ncdf4 or RNetCDF)
package for examples that will look quite similar to the IDL code that
opens a NetCDF file.
You could do a more or less direct translation of this IDL code to R,
but for us to help easily we probably need example data files. These
are fairly basic I/O and array manipulation operations, and The
Introduction to R and R Import/Export manuals cover that well.
A small example translation:
nlon = 720 ## number of grid points by longitude
nlat = 360 ## number of grid points by latitude
gfedmly = array(0.0, c(nlon,nlat)) ## array containing monthly emissions
gfeddly = array(0.0, c(nlon,nlat)) ## array containing daily emissions
To start populating those arrays from your files we would need much
more detail about them, and as ever with arrays you need to be aware
of the orientation conventions, and depending on your data you should
explore the spatial support functions in sp and raster and rgdal. See
the R Spatial Task View, you'll find much more integrated support in R
than this IDL code shows and it's probably easier for you to jump
straight into that level.
Cheers, Mike.
On Tue, Jul 24, 2012 at 9:55 AM, Tom Roche <Tom_Roche at pobox.com> wrote:
>
> I would appreciate
>
> * guidance regarding translation of IDL routines to R, generally
> * assistance translating two IDL routines to R, specifically
>
> Why I ask:
>
> I'm slowly learning how to do atmospheric modeling. One language that
> has been been popular in this space is IDL
>
> http://en.wikipedia.org/wiki/IDL_(programming_language)
>
> which unfortunately is proprietary (not to mention butt-ugly, IMHO :-)
> There is an emerging FOSS implementation, GDL
>
> http://en.wikipedia.org/wiki/GNU_Data_Language
>
> but I can't presently install GDL on either my own workstation or the
> cluster on which I do my "real work." And, unfortunately, I need to
> generate some datasets for which instructions are provided only in
> IDL (see listings following my .sig).
>
> However I have R in both work environments, and love it. So if there
> are any experts in IDL-to-R porting out there, I'd appreciate your
> assistance.
>
> TIA, Tom Roche <Tom_Roche at pobox.com>---2 IDL routines follow to EOF---
>
> from ftp://gfed3:dailyandhourly@zea.ess.uci.edu/GFEDv3.1/Readme.pdf
>
> ;++++++++++++++++++++ idl code to generate daily emissions +++++++++
> nlon=720 ; number of grid points by longitude
> nlat=360 ; number of grid points by latitude
> gfedmly=fltarr(nlon,nlat) ; array containing monthly emissions
> gfeddly=fltarr(nlon,nlat) ; array containing daily emissions
>
> ; You must read monthly emissions to generate daily fluxes.
> ; For example, if you want daily emissions for January 21st, 2004,
> ; you need read monthly data in January 2004 first:
> file0_in='GFED3.1_200401_C.txt'
> file0_in=strcompress(file0_in, /REMOVE_ALL)
> gfedmly = read_ascii( file0_in )
> gfedmly = gfedmly.field001
>
> ; reverse the direction of latitude with monthly emissions
> ; to combine with daily fire fractions.
> for j=1, nlat/2 do begin
> tmp = gfedmly[*,j-1]
> gfedmly[*,j-1] = gfedmly[*,nlat-j]
> gfedmly[*,nlat-j] = tmp
> endfor
> undefine, tmp
>
> ; Then, you can read daily fire fractions from the netcdf file.
> file1_in = string('fraction_emissions_20040121.nc')
> file1_in=strcompress(file1_in, /REMOVE_ALL)
> fid=NCDF_OPEN(file1_in)
> varid=NCDF_VARID(fid,'Fraction_of_Emissions')
> NCDF_VARGET, fid, varid, DATA
> NCDF_CLOSE, fid
> gfeddly=gfedmly*DATA
> ;++++++++++++++++++++ the end for daily emissions ++++++++++++++++++
>
> ;++++++++++++++++++++ idl code to generate 3-hourly emissions ++++++
> nlon=720 ; number of grid points by longitude
> nlat=360 ; number of grid points by latitude
> nhor=8 ; numbers of 3-hourly intervals each day
> gfeddly=fltarr(nlon,nlat) ; array containing daily emissions
> gfed3hly=fltarr(nlon,nlat,nhor) ; array containing 3-hourly emissions
>
> file_in = string('fraction_emissions_200401.nc')
> file_in=strcompress(file_in, /REMOVE_ALL)
> fid=NCDF_OPEN(file_in)
> varid=NCDF_VARID(fid,'Fraction_of_Emissions')
> NCDF_VARGET, fid, varid, DATA
> NCDF_CLOSE, fid
>
> for nh=0,nhor-1 do begin
> gfed3hly[*,*,nh]=gfeddly*DATA[*,*,nh]
> endfor
> ;++++++++++++++++++++ the end for 3-hourly emissions +++++++++++++++
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Michael Sumner
Hobart, Australia
e-mail: mdsumner at gmail.com
More information about the R-help
mailing list