[ncl-talk] check if a directory exists

Karin Meier-Fleischer meier-fleischer at dkrz.de
Thu Jan 19 05:34:10 MST 2017


Hi Marston,

you can use NCLs 'systemfunc' function with the shell function 'test' to 
check if a directory exists.

test -d /directory/; echo $?        returns 0 if directory exist or 1 if not

E.g.

   dirnames = (/"/tmp", "dirB", "$HOME"/)
   ndirs = dimsizes(dirnames)

   do i=0,ndirs-1
      ret = systemfunc("test -d "+dirnames(i)+"; echo $?")
      if(ret .eq. 0) then
         print("--> "+dirnames(i)+": exists")                  ;-- do 
what you want to do
      else if(ret .eq.1) then
         print("--> "+dirnames(i)+": doesn't exists")          ;-- just 
continue
         continue
      end if
      end if
   end do

Bye,
Karin


Am 19.01.17 um 12:53 schrieb Marston Johnston:
> Hi,
>
> I’m checking to see if there is a way for NCL to check if a directory 
> is present before running a systemfunc for files to process? For 
> example, I’m trying to process a series of MHS satellite granules but 
> some satellites, e.g., metopa, noaa19, etc. have different record 
> lengths for various reasons. Some records overlap and some do not.
> So I will parallel process the satellite data with a single script 
> with the satellite name as a command-line argument and loop over the 
> years, months, and days available (see code snip below). I would like 
> to check the presence of the directory before calling  files := 
> systemfunc ("ls "+cmd) so that if the record does not exist, NCL will 
> not crash. I’ve looked at isfilepresent, file exist, but these will 
> cause NCL to crash or return something that I’m not easily checked, 
> i.e., e.g., an empty array of length 0. I did a search for this FAQ in 
> NCL but there does not seem to be one. Any clever ideas out there?
>
> Thanks in advance!
> /M
>
> ; *******  Code *************
> do y = 0, nyears - 1
>     print("Processing year: "+years(y))
>     do m = 0, nmonths - 1
>       print("Processing month: "+months(m))
>       ndays := days_in_month(toint(years(y)),toint(months(m)))
>       days := ispan(1,ndays,1)
>       ;print(days)
>       do d = 0, ndays - 1
>         print("Processing day: "+(d+1))
>         day = sprinti("%0.2i",days(d))
>         cmd := idir+years(y)+"/"+months(m)+"/"+day+"/NSS*.nc"
>         dayfile = idir+years(y)+"/"+months(m)+"/"+day+"/daymean.nc"
>         ;print(dayfile)
>         ;exit
>         files := systemfunc ("ls "+cmd)
>         ;print(files)
>         nfiles = dimsizes(files)
> snip
>
> ;****************** End of Code snip *************************
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20170119/af4c481f/attachment.html 


More information about the ncl-talk mailing list