Next: , Previous: Where statement, Up: ncap2 netCDF Arithmetic Processor


4.1.14 Loops

ncap supplies for() loops and while() loops. They are completely unoptimized so use them only with RAM variables unless you want thrash your disk to death. To break out of a loop use the break command. To iterate to the next cycle use the continue command.

     // Follwing sets elements in variable double temp(time,lat)
     // If element < 0 set to 0, if element > 100 set to 100
     *sz_idx=$time.size;
     *sz_jdx=$lat.size;
     
     for(*idx=0;idx<sz_idx;idx++)
       for(*jdx=0;jdx<sz_jdx;jdx++)
         if(temp(idx,jdx) > 100) temp(idx,jdx)=100.0;
         else if(temp(idx,jdx) < 0) temp(idx,jdx)=0.0;
     
     // Are values of co-ordinate variable double lat(lat) monotonic?
        *sz=$lat.size;
     
        for(*idx=1;idx<sz;idx++)
          if(lat(idx)-lat(idx-1) < 0.0)
            break;
     
        if(idx == sz)
          print("lat co-ordinate is monotonic\n");
        else
          print("lat co-ordinate is NOT monotonic\n");
     
     // Sum odd elements
     *idx=0;
     *sz=$lat_nw.size;
     *sum=0.0;
       while(idx<sz){
        if(lat(idx)%2) sum+=lat(idx);
        idx++;
       }
     
     ram_write(sum);
     print("Total of odd elements ");print(sum);print("\n");