Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageMichael 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 > [url]http://www.unidata.ucar.edu/packages/netcdf/f90/Documentation/f90-html-docs/[/ur l] > > 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 by tes) -- 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 fir st 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 sur e 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 wri ting is longer than what you've defined, weird things can happen. (To be fair I've only not iced it with writing attributes which don't require this rigamarole but once bitten....) All the above goes for higher dimensioned character string arrays. Just reme mber to always declare the string length as the first dimension in the "DimIDs" array in NF 90_DEF_VAR. Hope this helps. cheers, paulv
Post Follow-up to this messageHi 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 ;-)
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.