Re: use of ind function, with a complicated set of tests

From: Donna Cote <d-cote_at_nyahnyahspammersnyahnyah>
Date: Fri, 10 Apr 2009 12:11:31 -0500

Dennis,

Testing for the rank, like you said, is not a problem. What I'm running
into (and maybe this is a question already answered) is that I'm
examining observation data (in a netCDF file), data record by data
record (how do I do that?) and producing a line of text on just those
records which have data (.not.missing()) in those record variables in
our list of variables (which is a subset of the entire 136+ variables in
each file).

I am at the point where I can process the first record and produce what
I want, but can't seem to get to the other records' data.

Donna

; my script
; read input file
f=addfile(readfn,"r")

; start with a 1D array that holds all variable names found in this file
varNames=getfilevarnames(f)
 <snip>
varNamesSize=dimsizes(varNames)
; for each row / record in the file:
; will produce two output string variables, one will contain db column
names, the other will contain the values
; track the number or index of the items put into these strings, if, at
the end, this number is still lt 0, then no output is printed
numColumns=-1
; for each variable in the 1D array of names, test type, dimensions, and
dimension name
; base output on test results
  do i=0, dimsizes(varNames)-1

  ; with tmp holding contents of each file variable, start working
    tmp = f->$varNames(i)$

    j = ind(var1D.eq.varNames(i)) ; where j is the array index where
the current variable name is found in the var1D array
    if (ismissing(j)) then
      delete(j)
      delete(tmp)
      continue ; skip this variable and go to the next in the
varNames array
    end if

    longnametext=""
    unitstext=""
    if (isatt(tmp,"long_name")) then
      longnametext=tmp_at_long_name
    end if
    if (isatt(tmp,"units")) then
      unitstext=tmp_at_units
    end if

    rank = dimsizes(dimsizes(tmp)) ; rank gives me the number
of dimensions of a variable, where dimsizes tells me how many rows in
the var array

      if (rank.ge.1 .and. isdimnamed(tmp,0) .and. (tmp!0 .eq. "recNum")
) then ; (B)
        if (rank.eq.1) then ; (C)
          if (typeof(tmp) .eq. "character") then ; (D)
            if (numColumns.gt.-1) then
              varColumnsNameList=varColumnsNameList+ ", " +col1D(j)
              varColumnsValueList=varColumnsValueList+ ", '"
+chartostring(tmp(i))+ "'"
            else
              varColumnsNameList=col1D(j)
              varColumnsValueList="'" +chartostring(tmp(i))+ "'"
            end if
            numColumns=numColumns+1
          else ; else of if typeof(tmp) is character
            if ( isdouble(tmp) .or. islong(tmp) .or. isfloat(tmp)) then
              tmp@_FillValue = 3.40282e+38
              if ( .not.ismissing(tmp(i))) then ; skip this
variable and go to the next in the varNames array
                if (numColumns.gt.-1) then
                  varColumnsNameList=varColumnsNameList+ ", " +col1D(j)
                  varColumnsValueList=varColumnsValueList+ ", " +tmp(i)
                else
                  varColumnsNameList=col1D(j)
                  varColumnsValueList=tmp(i)
                end if
                numColumns=numColumns+1
              end if
            else
              if (numColumns.gt.-1) then
                varColumnsNameList=varColumnsNameList+ ", " +col1D(j)
                varColumnsValueList=varColumnsValueList+ ", " +tmp(i)
              else
                varColumnsNameList=col1D(j)
                varColumnsValueList="" +tmp(i)+ ""
              end if
              numColumns=numColumns+1
            end if
          end if ; end of if typeof... is character ; (D)
        end if ; (C) end of if rank.eq.1 ...

        if (rank.eq.2) then ; (Z)
          text2= varNames(i)+ " long_name = '" +longnametext+ "' units =
'" +unitstext+ "' "
          ;text2="("+ i +") "+ varNames(i) +" rank = 2, typeof = "+
typeof(tmp)
          if (typeof(tmp) .eq. "character") then
            if (numColumns.gt.-1) then
              varColumnsNameList=varColumnsNameList+ ", " +col1D(j)
              varColumnsValueList=varColumnsValueList+ ", '"
+chartostring(tmp(i,:))+ "'"
            else
              varColumnsNameList=col1D(j)
              varColumnsValueList="'" +chartostring(tmp(i,:))+ "'"
            end if
            numColumns=numColumns+1
          else
;
            if ( isdouble(tmp) .or. islong(tmp) .or. isfloat(tmp)) then
              tmp@_FillValue = 3.40282e+38

              if ( .not.ismissing(tmp(i,0))) then ; skip this
variable and go to the next in the varNames array
                if (numColumns.gt.-1) then
                  varColumnsNameList=varColumnsNameList+ ", " +col1D(j)
                  varColumnsValueList=varColumnsValueList+ ", " +tmp(i,0)
                else
                  varColumnsNameList=col1D(j)
                  varColumnsValueList=tmp(i,0)
                end if
                numColumns=numColumns+1
              end if
            end if
          end if ; end of if typeof...
        end if ; (Z) end of if rank.eq.2 ...

      end if ; (B) end of if rank.ge.1 ... and has a named dimension at
!0 and that dimension name is recNum
    delete(j)
    delete(tmp)
  end do ; end of do each record variable (i)

  if ( numColumns.gt.-1 ) then
    print("(" +varColumnsNameList+ ") VALUES (" +varColumnsValueList+ ")")
  end if
<EOF>

Dennis Shea wrote:
> Don't know if I really understand.
>
> You test for rank of "ge.1" .... Well, all variables
> have a rank of, at least, one. So to me it is
> a puzzling test. I wrote the attached [untested]
> and it returns all variables with rank ".gt.1"
>
> Good luck
> D

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Apr 10 2009 - 11:11:31 MDT

This archive was generated by hypermail 2.2.0 : Sun Apr 12 2009 - 14:28:35 MDT