diff --git a/ChangeLog b/ChangeLog
index 341d530acf3d589f0d725775bd6fd9ccebd13f35..ad51f7e685ee82e056aa174b5525a7e7d655717a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2023-05-19  Uwe Schulzweida
+
+	* cdfDefineAttributes: check filetype for unsigned int attributes (bug fix)
+
 2023-05-16  Uwe Schulzweida
 
 	* vlistChangeGridIndex: remove CDI_KEY_CHUNKSIZE if present
diff --git a/src/cdf_write.c b/src/cdf_write.c
index cad3163ee40780596d56db7cb0983d17fbbb18fc..2b54a1e7db355d90abd98e1573f80b4f0d3b39cb 100644
--- a/src/cdf_write.c
+++ b/src/cdf_write.c
@@ -297,8 +297,30 @@ cdfDefineCellMethods(stream_t *streamptr, int cdiID, int varID, int fileID, int
     }
 }
 
+static nc_type
+int_datatype_to_xtype(int filetype, int datatype)
+{
+  // clang-format off
+  if (filetype == CDI_FILETYPE_NC4 || filetype == CDI_FILETYPE_NC4C || filetype == CDI_FILETYPE_NCZARR)
+    {
+      return (datatype == CDI_DATATYPE_INT8)   ? NC_BYTE :
+             (datatype == CDI_DATATYPE_INT16)  ? NC_SHORT :
+#ifdef  HAVE_NETCDF4
+             (datatype == CDI_DATATYPE_UINT8)  ? NC_UBYTE :
+             (datatype == CDI_DATATYPE_UINT16) ? NC_USHORT :
+             (datatype == CDI_DATATYPE_UINT32) ? NC_UINT :
+#endif
+                                                 NC_INT;
+    }
+
+  return (datatype == CDI_DATATYPE_INT8)   ? NC_BYTE :
+         (datatype == CDI_DATATYPE_INT16)  ? NC_SHORT :
+                                             NC_INT;
+  // clang-format on
+}
+
 void
-cdfDefineAttributes(int cdiID, int varID, int fileID, int ncvarID)
+cdfDefineAttributes(int filetype, int cdiID, int varID, int fileID, int ncvarID)
 {
   int atttype, attlen;
   char attname[CDI_MAX_NAME + 1];
@@ -330,17 +352,7 @@ cdfDefineAttributes(int cdiID, int varID, int fileID, int ncvarID)
           int *attint = (int *) resizeBuf(&attBuf, &attBufSize, attSize);
           cdiInqAttInt(cdiID, varID, attname, attlen, &attint[0]);
           size_t len = (size_t) attlen;
-          // clang-format off
-          nc_type xtype = (atttype == CDI_DATATYPE_INT8)   ? NC_BYTE :
-                          (atttype == CDI_DATATYPE_INT16)  ? NC_SHORT :
-#ifdef  HAVE_NETCDF4
-                          (atttype == CDI_DATATYPE_UINT8)  ? NC_UBYTE :
-                          (atttype == CDI_DATATYPE_UINT16) ? NC_USHORT :
-                          (atttype == CDI_DATATYPE_UINT32) ? NC_UINT :
-#endif
-                                                             NC_INT;
-          // clang-format on
-          cdf_put_att_int(fileID, ncvarID, attname, xtype, len, attint);
+          cdf_put_att_int(fileID, ncvarID, attname, int_datatype_to_xtype(filetype, atttype), len, attint);
         }
       else if (atttype == CDI_DATATYPE_FLT32 || atttype == CDI_DATATYPE_FLT64)
         {
@@ -392,7 +404,7 @@ cdfDefGlobalAtts(stream_t *streamptr)
 
   if (natts > 0 && streamptr->ncmode == 2) cdf_redef(fileID);
 
-  cdfDefineAttributes(vlistID, CDI_GLOBAL, fileID, NC_GLOBAL);
+  cdfDefineAttributes(streamptr->filetype, vlistID, CDI_GLOBAL, fileID, NC_GLOBAL);
 
   if (natts > 0 && streamptr->ncmode == 2) cdf_enddef(fileID, streamptr->self);
 
@@ -1041,7 +1053,7 @@ cdfDefVar(stream_t *streamptr, int varID)
   cdfDefineCellMethods(streamptr, vlistID, varID, fileID, ncvarID);
 
   // Attributes
-  cdfDefineAttributes(vlistID, varID, fileID, ncvarID);
+  cdfDefineAttributes(streamptr->filetype, vlistID, varID, fileID, ncvarID);
 
   // Institute
   if (vlistInqInstitut(vlistID) == CDI_UNDEFID) cdfDefineInstituteName(vlistID, varID, fileID, ncvarID);
diff --git a/src/stream_cdf.h b/src/stream_cdf.h
index bff944cee202a7a6f577ba50e33077a1b43a4155..25576f3d0e74ab9c52da5e8d1b64c827af7f0b67 100644
--- a/src/stream_cdf.h
+++ b/src/stream_cdf.h
@@ -34,7 +34,7 @@ void cdfDefRecord(stream_t *streamptr);
 
 void cdfCopyRecord(stream_t *streamptr2, stream_t *streamptr1);
 
-void cdfDefineAttributes(int vlistID, int varID, int fileID, int ncvarID);
+void cdfDefineAttributes(int filetype, int vlistID, int varID, int fileID, int ncvarID);
 
 void cdf_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss);
 void cdf_write_record(stream_t *streamptr, int memtype, const void *data, size_t nmiss);
diff --git a/src/stream_cdf_o.c b/src/stream_cdf_o.c
index ee148433df58e6f15abd900d204d89459f2ae5c5..82d1aa57c2b5efaee53e58619a8426c48b9f24e0 100644
--- a/src/stream_cdf_o.c
+++ b/src/stream_cdf_o.c
@@ -1633,7 +1633,7 @@ cdfDefZaxisChar(stream_t *streamptr, int zaxisID, char *axisname, int *dimID, si
 
       cdfPutGridStdAtts(fileID, ncvarID, zaxisID, 'Z');
       cdf_put_att_text(fileID, ncvarID, "axis", 1, "Z");
-      cdfDefineAttributes(zaxisID, CDI_GLOBAL, fileID, ncvarID);
+      cdfDefineAttributes(streamptr->filetype, zaxisID, CDI_GLOBAL, fileID, ncvarID);
 
       streamptr->nczvarID[zaxisindex] = ncvarID;
       cdf_enddef(fileID, streamptr->self);
@@ -1732,7 +1732,7 @@ cdfDefZaxis(stream_t *streamptr, int zaxisID)
 
       int natts;
       cdiInqNatts(zaxisID, CDI_GLOBAL, &natts);
-      cdfDefineAttributes(zaxisID, CDI_GLOBAL, fileID, ncvarid);
+      cdfDefineAttributes(streamptr->filetype, zaxisID, CDI_GLOBAL, fileID, ncvarid);
     }
 #ifndef USE_MPI
   else if (type == ZAXIS_CHAR)
@@ -1807,7 +1807,7 @@ cdfDefZaxis(stream_t *streamptr, int zaxisID)
                   }
                 }
             }
-          cdfDefineAttributes(zaxisID, CDI_GLOBAL, fileID, ncvarid);
+          cdfDefineAttributes(streamptr->filetype, zaxisID, CDI_GLOBAL, fileID, ncvarid);
         }
 
       if (switchNCMode)
@@ -1850,7 +1850,7 @@ cdf_def_mapping(stream_t *streamptr, int gridID)
 
   int ncvarid;
   int ncerrcode = nc_def_var(fileID, gmapvarname, gmapvartype, 0, NULL, &ncvarid);
-  if (ncerrcode == NC_NOERR) cdfDefineAttributes(gridID, CDI_GLOBAL, fileID, ncvarid);
+  if (ncerrcode == NC_NOERR) cdfDefineAttributes(streamptr->filetype, gridID, CDI_GLOBAL, fileID, ncvarid);
 
   if (switchNCMode)
     {