comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » how to search if a variable exists in a file
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
how to search if a variable exists in a file [message #92002] Mon, 28 September 2015 15:25 Go to next message
Xiaohua Pan is currently offline  Xiaohua Pan
Messages: 2
Registered: September 2015
Junior Member
Hi all,
I am dealing with a set of stations which unfortunately contain different variables (netcdf formate). For example, Some stations have the variables, say 'temperature' and 'pressure', while some stations only have 'temperature'. When I read data with DO loop the stations, I would like to search the variable first, so I can skip those variables not existing to avoid the crash of program. I found a function of file_search(), but can't find a similar function for a variable. Is there anyone know how to do it? Thanks.

DO i_station = 0, n-1 do begin
DO i_var = 0, 1 do begin
; How to skip the variable if it doesn't exit ????
ENDFOR
ENDFOR

Thanks.
Xiaohua
Re: how to search if a variable exists in a file [message #92003 is a reply to message #92002] Mon, 28 September 2015 16:25 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Xiaohua Pan writes:

> I am dealing with a set of stations which unfortunately contain
different variables (netcdf formate). For example, Some stations have
the variables, say 'temperature' and 'pressure', while some stations
only have 'temperature'. When I read data with DO loop the stations, I
would like to search the variable first, so I can skip those variables
not existing to avoid the crash of program. I found a function of
file_search(), but can't find a similar function for a variable. Is
there anyone know how to do it? Thanks.
>
> DO i_station = 0, n-1 do begin
> DO i_var = 0, 1 do begin
> ; How to skip the variable if it doesn't exit ????
> ENDFOR
> ENDFOR

If you were to use the netCDF programs in the Coyote Library to work
with your netCDF files, you could use the method HasVar to tell you if a
file contains a variable with a particular name:

http://www.idlcoyote.com/fileio_tips/ncdf_browser.html

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: how to search if a variable exists in a file [message #92004 is a reply to message #92002] Tue, 29 September 2015 08:27 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
On 09/28/15 18:25, Xiaohua Pan wrote:
> Hi all,
> I am dealing with a set of stations which unfortunately contain
> different variables (netcdf formate). For example, Some stations have
> the variables, say 'temperature' and 'pressure', while some stations
> only have 'temperature'. When I read data with DO loop the stations, I
> would like to search the variable first, so I can skip those variables
> not existing to avoid the crash of program. I found a function of
> file_search(), but can't find a similar function for a variable. Is
> there anyone know how to do it? Thanks.
>
> DO i_station = 0, n-1 do begin
> DO i_var = 0, 1 do begin
> ; How to skip the variable if it doesn't exit ????
> ENDFOR
> ENDFOR

See:
http://www.exelisvis.com/docs/NCDF_VARID.html
Re: how to search if a variable exists in a file [message #92006 is a reply to message #92003] Tue, 29 September 2015 12:05 Go to previous message
Xiaohua Pan is currently offline  Xiaohua Pan
Messages: 2
Registered: September 2015
Junior Member
On Monday, September 28, 2015 at 7:25:32 PM UTC-4, David Fanning wrote:
> Xiaohua Pan writes:
>
>> I am dealing with a set of stations which unfortunately contain
> different variables (netcdf formate). For example, Some stations have
> the variables, say 'temperature' and 'pressure', while some stations
> only have 'temperature'. When I read data with DO loop the stations, I
> would like to search the variable first, so I can skip those variables
> not existing to avoid the crash of program. I found a function of
> file_search(), but can't find a similar function for a variable. Is
> there anyone know how to do it? Thanks.
>>
>> DO i_station = 0, n-1 do begin
>> DO i_var = 0, 1 do begin
>> ; How to skip the variable if it doesn't exit ????
>> ENDFOR
>> ENDFOR
>
> If you were to use the netCDF programs in the Coyote Library to work
> with your netCDF files, you could use the method HasVar to tell you if a
> file contains a variable with a particular name:
>
> http://www.idlcoyote.com/fileio_tips/ncdf_browser.html
>
> Cheers,
>
> David
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
> Sepore ma de ni thue. ("Perhaps thou speakest truth.")

Hi David,
It works. Thank you so much!
Xiaohua

For those who may need this information as well,
here is an example:

filename = 'data.nc'
var_name = 'sst'
ncdfObj = Obj_New('NCDF_FILE', filename)
hasVar = ncdfObj -> HasVar(var_name, OBJECT=varObj)
IF hasVar THEN BEGIN
print, ' find the variable ' + var_name
ENDIF ELSE BEGIN
print, " Can't find the variable "+var_name
ENDELSE
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: PLOT and LEGEND Function Graphic
Next Topic: Job Posting: Computer Research Specialist / Research Software Engineer / University of Florida

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 07:15:03 PDT 2025

Total time taken to generate the page: 0.00458 seconds