Re: Type conversion error: Unable to convert given STRING to Float. [message #76759] |
Tue, 05 July 2011 12:56  |
Zac
Messages: 3 Registered: July 2011
|
Junior Member |
|
|
Here's the solution, convert the integer to a string before inserting
into the file name:
layr=strcompress(string(lay +1), /REMOVE_ALL)
Open_Device, /ps, /Color, filename=''+outdir+'/emissions_'+sim
+'_air_level'+layr+'_'+pol+'.ps'
tvmap, air_pl, lon, lat, BLACK=1, NColors=225, Bottom = 20, /coasts, $
Title='Layer '+layr+' Aircraft '+pol+' Emissions - '+simtitle,$
|
|
|
Re: Type conversion error: Unable to convert given STRING to Float. [message #76762 is a reply to message #76759] |
Tue, 05 July 2011 08:55   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 7/5/11 8:36 AM, Zac wrote:
> Can anyone tell me why IDL thinks that the variable in the for loop
> below is a string?
See below, but I think it is the opposite, you have an integer and are
treating it like a string.
> Here's the offending part of the script. After I've opened the input
> file, I'm trying to loop over the one of the dimensions in the file
> (in this case the vertical layers of a file that contains gridded, 3-d
> aircraft emissions). I want to plot the emissions at each layer. I
> can hard code the layer number in the script and it works fine. When
> I try to implement the for loop, IDL is complaining that "lay" is a
> string.
>
> In addition, I tried to take the for loop out altogether and just add
> in a variable for the layer number, something like:
>
> lay = 2
>
> This gives the same type conversion error
>
> What am I doing wrong here?
>
> Thanks in advance, Zac
> ...
>
> ni=n_elements(lon)
> nj=n_elements(lat)
> nz=n_elements(lev)
>
> for lay=0,nz-1 do begin
> air_pl=air[*,*,lay]
>
> myct, 1, range=[0.4,1], /reverse
>
> !P.font(0)=0
>
> minvar=0
> maxvar=1E9
>
> layr=lay+1
>
> Open_Device, /ps, /Color, filename=''+outdir+'/emissions_'+sim
> +'_air_level_'+layr+'_'+pol+'.ps'
You are doing string operations above, but layr is an int.
> tvmap, air_pl, lon, lat, BLACK=1, NColors=225, Bottom = 20, /coasts, $
> Title='Layer '+layr+' Aircraft '+pol+' Emissions - '+simtitle,$
> /Continents,/Cylindrical,/SAMPLE,/isotropic, $
> /CBAR, Divisions=3, min_valid=minvar, $
> mindata=minvar, maxdata=maxvar, $
> CBFORMAT='(e12.2)', CBMAX=maxvar, CBMIN=minvar,CBUNIT='molec/cm2-
> sec',$
> /landscape, /grid, /countries, xthick=4,$
> ythick=4, thick=1.2, charthick=2, charsize=1, csfac=0.7,
> tcsfac=0.
> close_device
>
> endfor ; layer loop
>
> endfor ; pollutant loop
>
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL, A Guide to Learning IDL: http://modernidl.idldev.com
Research Mathematician
Tech-X Corporation
|
|
|
Re: Type conversion error: Unable to convert given STRING to Float. [message #76854 is a reply to message #76762] |
Tue, 05 July 2011 14:37  |
R.G.Stockwell
Messages: 163 Registered: October 2004
|
Senior Member |
|
|
> "Michael Galloy" wrote in message news:iuvc6e$ckm$1@dont-email.me...
>> Open_Device, /ps, /Color, filename=''+outdir+'/emissions_'+sim
>> +'_air_level_'+layr+'_'+pol+'.ps'
> You are doing string operations above, but layr is an int.
just a quick fyi to the OP. IDL is trying to convert '_air_level_' to an
integer, so it can be added to layr (hence the cannot convert STRING to
float message). It's trying to convert all the strings to integers in the
above line.
|
|
|
Re: Type conversion error: Unable to convert given STRING to Float. [message #76857 is a reply to message #76759] |
Tue, 05 July 2011 13:28  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 7/5/11 1:56 PM, Zac wrote:
> Here's the solution, convert the integer to a string before inserting
> into the file name:
>
> layr=strcompress(string(lay +1), /REMOVE_ALL)
>
> Open_Device, /ps, /Color, filename=''+outdir+'/emissions_'+sim
> +'_air_level'+layr+'_'+pol+'.ps'
> tvmap, air_pl, lon, lat, BLACK=1, NColors=225, Bottom = 20, /coasts, $
> Title='Layer '+layr+' Aircraft '+pol+' Emissions - '+simtitle,$
>
Here's the two ways I end up using to do this. The quick and easy way:
layr = strtrim(lay + 1, 2)
STRTRIM will convert a number type to a string and also remove
whitespace, here the "2" indicates to remove leading and trailing
whitespace.
Or I might do something like this:
filename = string(outdir, sim, lay + 1, pol, $
format='(%"%s/emissions_%s_air_level%d_%s.ps")')
You can also use Fortran style format codes, I am just more familiar
with the C ones.
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL, A Guide to Learning IDL: http://modernidl.idldev.com
Research Mathematician
Tech-X Corporation
|
|
|
Re: Type conversion error: Unable to convert given STRING to Float. [message #76858 is a reply to message #76762] |
Tue, 05 July 2011 12:32  |
Zac
Messages: 3 Registered: July 2011
|
Junior Member |
|
|
Ok, I see where the problem is now, but I don't know IDL syntax well
enough to solve. Can you tell me how I can embed an int into a
string? I would like my loop counter, which is the layer number I'm
looping over, to get written to the file name.
I'm clearly not using '+layr+' correctly. So how do I get an integer
type into the string file name?
filename=''+outdir+'/emissions_'+sim+'_air_level_'+layr+'_'+ pol+'.ps'
|
|
|