This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Zane, > It is a fortran 90 program. I am trying to read a netCDF file so I that can > manipulate the data from it and write it back to the netCDF file. Everything > goes fine until the point where I try to read the _Fillvalue ( _Fillvalue = > -32767s), missing values (missing_value = -32767s) and rainfall offset > (add_offset = 650.f). The problem is that I have trouble reading in the value > since there are characters and integers mixed. I can only read it in as a > character, but then when writing these back to the netCDF file it puts the > value in "..." (eg "-32767s"). So how can I read and write these attribute > values? > > This is what I want: (what I can get but that doesnt work) > > short rnd24(time, lat, lon) ; > rnd24:long_name = "24hr precipitation" ; > rnd24:units = "mm" ; > rnd24:add_offset = 650.f ; (rnd24:add_offset = "650.f") > rnd24:scale_factor = 0.0203125f ; (rnd24:scale_factor = "0.0203125f" ) > rnd24:valid_min = -32500 ; > rnd24:valid_max = 32500 ; > rnd24:cell_methods = "time: mean" ; > rnd24:_FillValue = -32767s ; (rnd24:_FillValue = "-32767s") > rnd24:missing_value = -32767s ; (rnd24:missing_value = "-32767s") > > > Here is an example where I tried writing a netCDF file: (I have highlighted > the areas where I am struggling to define the attributes) OK, I've marked the changes you need below. You just have to declare the attribute values to be the type intended ... --Russ > PROGRAM test > use netcdf > implicit none > > character (len = *), parameter :: FN="1979RR.nc", FN2="ccam_data.nc" > character (len = *), parameter :: LAT="lat", Y_AXIS="Y", LAT_LONG="latitude" > character (len = *), parameter :: LON="lon", X_AXIS="X", LON_LONG="longitude" > character (len = *), parameter :: TIM="time", TIM_NAME="****", T_AXIS="T" > character (len = *), parameter :: L_NAME="long_name", S_NAME="standard_name" > character (len = *), parameter :: RR="rnd24", RR_LONG="24h precipitation" > character (len = *), parameter :: OFF_NAME="add_offset", > SF_NAME="scale_factor", VMN_NAME="valid_min", VMX_NAME="valid_max" > character (len = *), parameter :: FV_NAME="_FillValue", > MV_NAME="missing_value" > character (len = *), parameter :: GS_NAME="source", SOURCE="CSIRO....." > character (len = *), parameter :: GA_il="il", GA_kl="kl", GA_lon="rlong0", > GA_lat="rlat0", GA_shm="schmidt" > character (len = *), parameter :: GA_lon_val="0.f", > GA_lat_val="0.f",GA_shm_val="1.f" > > !*** THESE strings should be VALUES (conversion needed) > character (len = *), parameter :: RR_OFFSET="650.f", SF="0.0203125f" > character (len = *), parameter :: MV="-32767s" , FV="-32767s" real, parameter :: RR_OFFSET=650.0, SF=0.0203125 integer*2, parameter :: MV=-32767 , FV=-32767 > character (len = *), parameter :: UNITS="units", AXIS="axis" > character (len = *), parameter :: LAT_UNITS="degrees_north" > character (len = *), parameter :: LON_UNITS="degrees_east" > character (len = *), parameter :: RR_UNITS="mm" > > integer, parameter :: NLATS=31, NLONS=51 > integer, parameter :: NDIMS=3, NTIM=1, VMN=-32500, VMX=32500 > integer, parameter :: GA_il_val=48, GA_kl_val=18 > integer, parameter :: lon_START=-20, lon_END=80, lat_START=-50, lat_END=10 > integer, parameter :: tim_START=1440, tim_END=43200 > > !real, dimension(:,:,:), allocatable :: RR24 > real :: RR24(NLONS,NLATS) > integer :: ncid, ncid2, lat_dimid, lat_varid, latc > integer :: lon_dimid, lon_varid, lonc > integer :: tim_dimid, tim_varid, timc > integer :: rr_varid, rr2_varid, i > integer :: dimids(NDIMS), start(NDIMS), count(NDIMS) > > !real , parameter :: FV = '-32323s' > > real :: lats(NLATS), lons(NLONS), tims(NTIM) > > open(1,file='1979_RRTOT.txt') > > > do latc = 1, NLATS > lats(latc)= lat_START + (latc - 1)*2 > end do > > do lonc = 1, NLONS > lons(lonc)= lon_START + (lonc - 1)*2 > end do > > do timc = 1, NTIM > tims(timc)= tim_START + (timc - 1)*1440 > end do > > > do latc = 1, NLATS > read(1,*) (RR24(lonc,latc),lonc=1,51) > do lonc = 1, NLONS > RR24(lonc,latc)=RR24(lonc,latc)/12 > end do > end do > > !create netCDF file > call check(nf90_create(FN, NF90_CLOBBER, ncid)) > call check(nf90_open(FN2, NF90_NOWRITE, ncid2)) > > !define dimentions > call check(nf90_def_dim(ncid, LON, NLONS, lon_dimid)) > call check(nf90_def_dim(ncid, LAT, NLATS, lat_dimid)) > call check(nf90_def_dim(ncid, TIM, NF90_UNLIMITED, tim_dimid)) > > !define variables > dimids = (/ lon_dimid, lat_dimid, tim_dimid /) > call check(nf90_def_var(ncid, LON, NF90_REAL, lon_dimid, lon_varid)) > call check(nf90_def_var(ncid, LAT, NF90_REAL, lat_dimid, lat_varid)) > call check(nf90_def_var(ncid, TIM, NF90_REAL, tim_dimid, tim_varid)) > call check(nf90_def_var(ncid, RR, NF90_SHORT, dimids, rr_varid)) > > !add attributes of the variables > call check(nf90_put_att(ncid, lon_varid, L_NAME, LON_LONG)) > call check(nf90_put_att(ncid, lon_varid, S_NAME, LON_LONG)) > call check(nf90_put_att(ncid, lon_varid, AXIS, X_AXIS)) > call check(nf90_put_att(ncid, lon_varid, UNITS, LON_UNITS)) > > call check(nf90_put_att(ncid, lat_varid, L_NAME, LAT_LONG)) > call check(nf90_put_att(ncid, lat_varid, S_NAME, LAT_LONG)) > call check(nf90_put_att(ncid, lat_varid, AXIS, Y_AXIS)) > call check(nf90_put_att(ncid, lat_varid, UNITS, LAT_UNITS)) > > > > call check(nf90_put_att(ncid, tim_varid, UNITS, TIM_NAME)) > call check(nf90_put_att(ncid, tim_varid, S_NAME, TIM)) > call check(nf90_put_att(ncid, tim_varid, AXIS, T_AXIS)) > > call check(nf90_put_att(ncid, rr_varid, L_NAME, RR_LONG)) > call check(nf90_put_att(ncid, rr_varid, UNITS, RR_UNITS)) > > !call check(nf90_put_att(ncid, rr_varid, OFF_NAME, RR_OFFSET)) > !call check(nf90_put_att(ncid, rr_varid, SF_NAME, SF)) > !call check(nf90_put_att(ncid, rr_varid, VMN_NAME, VMN)) > !call check(nf90_put_att(ncid, rr_varid, VMX_NAME, VMX)) > call check(nf90_put_att(ncid, rr_varid, FV_NAME, FV)) > !call check(nf90_put_att(ncid, rr_varid, MV_NAME, MV)) call check(nf90_put_att(ncid, rr_varid, OFF_NAME, RR_OFFSET)) call check(nf90_put_att(ncid, rr_varid, SF_NAME, SF)) call check(nf90_put_att(ncid, rr_varid, VMN_NAME, VMN)) call check(nf90_put_att(ncid, rr_varid, VMX_NAME, VMX)) call check(nf90_put_att(ncid, rr_varid, FV_NAME, FV)) call check(nf90_put_att(ncid, rr_varid, MV_NAME, MV)) > !***Global Attributes > call check(nf90_put_att(ncid, NF90_GLOBAL, GS_NAME, SOURCE)) > call check(nf90_put_att(ncid, NF90_GLOBAL, GA_il, GA_il_val)) > call check(nf90_put_att(ncid, NF90_GLOBAL, GA_kl, GA_kl_val)) > call check(nf90_put_att(ncid, NF90_GLOBAL, GA_lon, GA_lon_val)) > call check(nf90_put_att(ncid, NF90_GLOBAL, GA_lat, GA_lat_val)) > call check(nf90_put_att(ncid, NF90_GLOBAL, GA_shm, GA_shm_val)) > > !end def > call check(nf90_enddef(ncid)) > > !add values to attributes > call check(nf90_put_var(ncid, lon_varid, lons)) > call check(nf90_put_var(ncid, lat_varid, lats)) > call check(nf90_put_var(ncid, tim_varid, tims)) > > .............. > > Regards, > Zane > > > > > > > > > > >>> "Unidata netCDF Support" <address@hidden> 10/30/2012 12:08 AM >>> > Hi Zane, > > > I am trying to copy an attribute, Fillvalue, but I get errors or > > segmantation fault in the declaration of a fillvalue that is the following > > value for eg. -11111s. The same goes for offset values for eg 650.f or > > 0.34344f. How do I declare these? > > Sorry, we need more information. > > Are you trying to define the _FillValue attribute in a C program, a Fortran > program, or a CDL file? > > Do you have a small example that demonstrates the problem, so we can reproduce > it here and determine what is going on? > > Or do you just need a small example in C or Fortran to show how to define > variable attributes? > > --Russ > > Russ Rew UCAR Unidata Program > address@hidden http://www.unidata.ucar.edu > > > > Ticket Details > =================== > Ticket ID: HMG-489739 > Department: Support netCDF > Priority: Normal > Status: Closed > > > -- > This message is subject to the CSIR's copyright terms and conditions, e-mail > legal notice, and implemented Open Document Format (ODF) standard. > The full disclaimer details can be found at > http://www.csir.co.za/disclaimer.html. > > This message has been scanned for viruses and dangerous content by > MailScanner, > and is believed to be clean. > > Please consider the environment before printing this email. > > > -- > This message is subject to the CSIR's copyright terms and conditions, e-mail > legal notice, and implemented Open Document Format (ODF) standard. > The full disclaimer details can be found at > http://www.csir.co.za/disclaimer.html. > > This message has been scanned for viruses and dangerous content by > MailScanner, > and is believed to be clean. > > Please consider the environment before printing this email. > > > Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu Ticket Details =================== Ticket ID: HMG-489739 Department: Support netCDF Priority: Normal Status: Closed