Re: Function similar to python "try"

From: Wei Huang <huangwei_at_nyahnyahspammersnyahnyah>
Date: Mon Aug 05 2013 - 09:53:07 MDT

I guess "isfilepresent" plus "addfile" itself can (almost) serve the purpose of "try" in python.

1. first use "isfilepresent" to see if the file exist.

2. use "addfile" to open the file, and check the file status.

Here is a script which check few files:

 files = (/"ECMWF_Tb_IASI_20050501_12z+20050801_00z.nc", \
           "MOP02J-20080101-L2V16.2.3.he5", \
           "file.h5", \
           "ozone.master.nc", \
           "no_such_file.nc", \
           "tst.ncl"/)

;print(files)

 do n = 0, dimsizes(files) - 1
    file_good = True;

   ;print("Test file No. " + n + ": <" + files(n) + ">")
    file_exists = isfilepresent(files(n))
    if(.not. file_exists) then
       ;print("File <" + files(n) + "> does not exist.")
        file_good = False;
    end if

    if(file_exists) then
        f = addfile(files(n), "r")
        if(ismissing(f)) then
          ;print("File <" + files(n) + "> can not be read by ncl")
           file_good = False;
        end if
    end if

    if(file_good) then
        print("file No. " + n + ": <" + files(n) + "> can be handled by ncl");
    end if

    if(.not. file_good) then
        print("file No. " + n + ": <" + files(n) + "> can not be handled by ncl");
    end if

 end do

 print("NCL session still valid.")

Which produce:
(0) file No. 0: <ECMWF_Tb_IASI_20050501_12z+20050801_00z.nc> can be handled by ncl
(0) file No. 1: <MOP02J-20080101-L2V16.2.3.he5> can be handled by ncl
(0) file No. 2: <file.h5> can be handled by ncl
(0) file No. 3: <ozone.master.nc> can be handled by ncl
(0) file No. 4: <no_such_file.nc> can not be handled by ncl
warning:["FileSupport.c":3698]:NCL does not know anything about file suffix <ncl>.
But NCL will try its best to figure out the file format.

fatal:An internal error has occurred. The file format requested does not appear to be supported, could not open (tst.ncl)
(0) file No. 5: <tst.ncl> can not be handled by ncl
(0) NCL session still valid.

Where the first 4 files exist on my machine, 5th does not exist.
6th is the above ncl script, which ncl executable can not open it as NetCDF, of HDF files.

The last line show that we can go beyond the check.

Certainly, there is one issue, which, if the file is corrupted, the above script might not work properly.

Hope this helps.

Wei
 

huangwei@ucar.edu
VETS/CISL
National Center for Atmospheric Research
P.O. Box 3000 (1850 Table Mesa Dr.)
Boulder, CO 80307-3000 USA
(303) 497-8924

On Aug 5, 2013, at 9:25 AM, Marston Johnston <shejo284@gmail.com> wrote:

> Hi Mary,
>
> That sounds like a good idea, but instead of a new function, you could also modify the "addfile" to either throw up a warning if the file cannot be open, or fatal if not present. Perhaps this can be controlled by an additional "Opt" argument to the addfile function. I cannot edit my NCL codes and I'm not a great programmer, but if I were, that would be my first option.
>
> /M
>
>
> On Mon, Aug 5, 2013 at 4:26 PM, Mary Haley <haley@ucar.edu> wrote:
> Marston,
>
> Perhaps we can add an "isvalidfile" function. I actually thought "isfilepresent" did this, but apparently it just checks for the existence.
>
> Wei or Dave (Brown), would it be possible to have an "isvalidfile" type of function? I know it's not always obvious what type of file you are opening, so what about an "isvalid_netcdf_file"?
>
> --Mary
>
> On Aug 5, 2013, at 2:34 AM, Marston Johnston wrote:
>
> > Hi guys,
> >
> > Thanks for the interesting solutions but...in Mary's suggestion, the file is present but corrupted so checking if the file is present is not a viable solution. Dave's solution is advance, using shell commands and making system calls, but he's right, it is not very fast and needs to read all files, all 8000 files, twice. The problem here is that my files are hdf-eos files and not netcdf. There is no h4dump, or equivalent program, installed on the server. Nevertheless, I'll keep these suggestions in my archive of NCL code tidbits.
> >
> > Appreciate the help guys,
> > I'll just have to delete the files when the program crashes.
> >
> > /M
> >
> > _______________________________________________
> > ncl-talk mailing list
> > List instructions, subscriber options, unsubscribe:
> > http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
>
>
> --
> Only the fruitful thing is true!
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Aug 5 09:53:24 2013

This archive was generated by hypermail 2.1.8 : Wed Aug 07 2013 - 13:59:00 MDT