[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

951005: Why don't this work?



Bill,

> Subject: Why don't this work?
> To:       address@hidden
> From: Bill Roberts <address@hidden>
> Organization: UCAR/NCAR/HAO
> Keywords: 199510042248.AA01153

In the above message you wrote:
 
> What should be simple ain't.  Sorry but I can't figure out
> how to do this in Fortran.  Works fine in C.  
>   I'm trying to write characters into a netCDF file.
> 
> Here is the complete code - executive and subroutine
> (The output of running this code (ncdump) is below).
> 
> Program...
>         program fred
>  
>         common / x / ack(2), pht(2)
>  
>         character*2 ack
>         character*6 pht
>  
>         ack(1)= 'A'//char(0)
>         ack(2)= 'B'//char(0)
>         pht(1)= 'line1'//char(0)
>         pht(2)= 'line2'//char(0)
>  
>         call blee()
>  
>         end
> 
> Subroutine ...
>         subroutine blee()
>         include '/opt/local/include/netcdf.inc'
>  
>         common / x / ack(2), pht(2)
>  
>         character*2 ack
>         character*6 pht
>  
>         integer base(2), count(2), ncid, rcode
>         integer ackid, phtid
>  
>         ncid= nccre("fred.nc", NCCLOB, rcode)
>  
> c dimension defs and stuff...
>         base(1)= ncddef(ncid, "pht_dim", 6, rcode)
>         base(2)= ncddef(ncid, "list_size", 2, rcode)
>         phtid= ncvdef(ncid, "pht", NCCHAR, 2, base, rcode)
>         base(1)= ncddef(ncid, "ack_dim", 2, rcode)
>         ackid= ncvdef(ncid, "ack", NCCHAR, 2, base, rcode)
>  
> c kludge thang...
>         call ncclos(ncid,rcode)
>         ncid= ncopn("fred.nc", NCWRITE, rcode)
>  
> c now the attempt...
>         base(1)= 1
>         base(2)= 1
>         count(1)= 1
>         count(2)= 2
>         call ncvptc (ncid, ackid, base, count, ack, 2, rcode)
>         call ncvptc (ncid, phtid, base, count, pht, 6, rcode)
>  
>         return
>         end

Try changing this last part to the following:

    c now the attempt...
             base(1)= 1
             base(2)= 1
             count(1)= 2
             count(2)= 2
             call ncvptc (ncid, ackid, base, count, ack, 2*2, rcode)
             count(1)= 6
             call ncvptc (ncid, phtid, base, count, pht, 6*2, rcode)

             call ncclos (ncid, rcode)
      
             return
             end

The 6th argument to ncvptc() is the *total* size of the 5th argument
(the string array).  It's used for sanity checking.

The ncclos() is necessary in order to flush the output buffers.

I changed count(1) in the above in order to write out all the characters
in each array.

--------
Steve Emmerson   <address@hidden>