For Programmers: Free Programming Magazines  


Home > Archive > Fortran > March 2004 > how to put a F90 string array into netCDF file?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to put a F90 string array into netCDF file?
Michael Kiefer

2004-03-27, 12:17 am

Hello,

We want to put a character array like e.g.

character (30) :: err_string(35)

via the netCDF F90 function nf90_put_var(ncFileID, err_list_ID, err_string)

into a nc-file.

After several fruitless attempts (e.g. copying err_string into
a character(1), dimension=30x35 variable) we got completely stuck...
The online documentation http://www.unidata.ucar.edu/package.../f90-html-docs/
is not too specific on that topic...

Does anybody have a short example code how to do this.
Even a hint of how to write a simple string would be helpful.

Thanks in advance!

Michael Kiefer & Udo Grabowski
Paul Van Delst

2004-03-27, 12:17 am

Michael Kiefer wrote:
> Hello,
>
> We want to put a character array like e.g.
>
> character (30) :: err_string(35)
>
> via the netCDF F90 function nf90_put_var(ncFileID, err_list_ID, err_string)
>
> into a nc-file.
>
> After several fruitless attempts (e.g. copying err_string into
> a character(1), dimension=30x35 variable) we got completely stuck...
> The online documentation
> http://www.unidata.ucar.edu/package.../f90-html-docs/
>
> is not too specific on that topic...
>
> Does anybody have a short example code how to do this.
> Even a hint of how to write a simple string would be helpful.
>
> Thanks in advance!


Hello,

Unfortunately it's not a trivial thing to do, but it's not too hard either.

The important thing is that netCDF really doesn't understand the concept of a "string" (as
far as I can tell). Think of it more as an array of single characters (or bytes) -- so you
were on the rigth track.

The important step is to define the (maximum) length of the string (in your case, 30) as a
dimension:

NF90_Status = NF90_DEF_DIM( NC_FileID, &
'StrLen', &
LEN(err_string(1)), &
StrLen_DimID )

Same as for the "regular" dimension:

NF90_Status = NF90_DEF_DIM( NC_FileID, &
'n_Strings', &
SIZE(err_string), &
n_Strings_DimID )

Then you define your string array variable with the string length as the first dimension:

NF90_Status = NF90_DEF_VAR( NC_fileID, &
'StringArray, &
NF90_CHAR, &
DimIDs = (/ StrLen_DimID, n_Strings_DimID /), &
VarID = StringArray_VarID )

and after putting the file into data mode, you write the 2-d character array:

NF90_Status = NF90_PUT_VAR( NC_FileID, &
StringArray_VarID, &
err_string )

Same for reading the string array,

NF90_Status = NF90_GET_VAR( NC_FileID, &
StringArray_VarID, &
err_string_to_read )


Typically when doing this I inquire the string length dimension and make sure it matches
what I'm read/writing, e.g.

NF90_Status = NF90_INQUIRE_DIMENSION( NC_FileID, &
StrLen_DimID, &
Len = String_Length )

IF ( String_Length /= LEN( err_string(1) ) ) THEN
...issue error message...
END IF

I issue an error, but in most cases a warning would probably suffice. I have found,
though, that in writing character string to netCDF files, if what you're writing is longer
than what you've defined, weird things can happen. (To be fair I've only noticed it with
writing attributes which don't require this rigamarole but once bitten....)

All the above goes for higher dimensioned character string arrays. Just remember to always
declare the string length as the first dimension in the "DimIDs" array in NF90_DEF_VAR.

Hope this helps.

cheers,

paulv

Michael Kiefer

2004-03-27, 12:17 am

Hi Paul,

thanks for your sound advice! Now it works as expected.

The crucial point indeed is to declare the F90 variable
"as usual", e.g. character (30) :: err_string(35)
and then "make the netCDF believe" it is a two dimensional
array of single characters.

Cheers,

Michael & Udo, happy again ;-)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com