Re: Function similar to python "try"

From: Dave Allured - NOAA Affiliate <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Fri Aug 02 2013 - 16:53:12 MDT

Also, you could use systemfunc and an external command to scan files
in advance of opening them in NCL. I like ncdump for Netcdf files.
Nccopy might be faster, but I am not sure how to do the redirect.
There are similar command tools for other formats.

This is inefficient because you are reading each file twice, but it
may be necessary for some situations. Also I found that ncdump -k is
just as good as plain ncdump for finding truncated netcdf-4 files,
without reading the entire file.

begin
  files = (/ "test.nc", "not-found.nc", \
             "not-netcdf.nc ", "corrupted.nc" /)

  do j = 0, 3
    cmd = "(ncdump " + files(j) + " > /dev/null) 2>&1 ; echo $?"
    out = systemfunc (cmd)
    nlines = dimsizes (out)
    test_status = (out(nlines-1) .eq. "0") ; True = no errors

    print ("")
    print (files(j)+"")

    if (test_status) then
      print ("File is okay")
    else
      print ("File error:")
      print (" " + out(0:nlines-2))
    end if

    delete (out)
  end do
end

(0) test.nc
(0) File is okay
(0)
(0) not-found.nc
(0) File error:
(0) ncdump: not-found.nc: No such file or directory
(0)
(0) not-netcdf.nc
(0) File error:
(0) ncdump: not-netcdf.nc: NetCDF: Unknown file format
(0)
(0) corrupted.nc
(0) File error:
(0) ncdump: corrupted.nc: NetCDF: HDF error

--Dave

On Fri, Aug 2, 2013 at 2:16 PM, Mary Haley <haley@ucar.edu> wrote:
> Hi Marston,
>
> I'm afraid there is no such thing as the "try" concept in NCL, but
> if you want to test for a valid file, you might try "isfilepresent".
>
> if(isfilepresent(url+filename)) then
> do something
> else
> print error
> end if
>
> I'm not sure if the "isfilepresent" will work in your case, but it's worth a try.
>
> --Mary
>
>
> On Aug 1, 2013, at 8:53 AM, Marston Johnston wrote:
>
>> Hi,
>>
>> I'm looping over many files and some may be corrupted. I'm looking for a way to handle a failure to open in NCL without causing a fatal error.
>> In Python, this can be done using:
>>
>> try:
>> command
>> except:
>> command
>>
>> Is there something similar in NCL?
>>
>> /M
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Aug 2 16:53:25 2013

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