Skip to content

Commit

Permalink
Add the attribute "coordinates" to output netCDF variables
Browse files Browse the repository at this point in the history
- Currently the domain and progress variables have the attribute "coordinates" this commit adds this to the output variables

- `SW_NC_create_full_var()` gains new parameter to know if it should add to the provided "coordinates" attribute string
	* The provided string should contain at the least "lat lon" or "lat lon site" (or the user-provided names for these coordinates/dimensions)
- Update the call to `SW_NC_create_full_var()` to give the index -1, denoting that there is no need to add to the coordinate string
	* This should not happen anyways here even without a -1 due to only being added to when the coordinates "time", "vertical", and/or "pft" is present (which don't exist for progress)

- `create_output_file()` gains new attribute string for "coordinates" attribute
	* Add target for "goto" instead of immediately returning from the function to close the open file
  • Loading branch information
N1ckP3rsl3y committed Sep 12, 2024
1 parent fad27ea commit bef362c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/SW_netCDF_General.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ void SW_NC_create_full_var(
int deflateLevel,
const char *latName,
const char *lonName,
const int coordAttIndex,
LOG_INFO *LogInfo
);

Expand Down
37 changes: 36 additions & 1 deletion src/SW_netCDF_General.c
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ void SW_NC_get_str_att_val(
int attCallRes;
int attLenCallRes;
size_t attLen = 0;

SW_NC_get_var_identifier(ncFileID, varName, &varID, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
Expand Down Expand Up @@ -914,6 +915,9 @@ and writing attributes
variable
@param[in] latName User-provided latitude name
@param[in] lonName User-provided longitude name
@param[in] coordAttIndex Specifies the coordinate attribute location
within the provided `attNames`/`attVals` (if there isn't an attribute
of this name, it's value should be -1)
@param[in,out] LogInfo Holds information dealing with logfile output
*/
void SW_NC_create_full_var(
Expand All @@ -936,6 +940,7 @@ void SW_NC_create_full_var(
int deflateLevel,
const char *latName,
const char *lonName,
const int coordAttIndex,
LOG_INFO *LogInfo
) {

Expand All @@ -953,7 +958,11 @@ void SW_NC_create_full_var(
unsigned int numTimeVertVegVals = 3;
unsigned int varVal = 0;
size_t chunkSizes[MAX_NUM_DIMS] = {1, 1, 1, 1, 1};

char coordValBuf[MAX_FILENAMESIZE] = "";
char *writePtr = coordValBuf;
char *tempCoordPtr;
int writeSize = MAX_FILENAMESIZE;
char finalCoordVal[MAX_FILENAMESIZE];

for (index = 0; index < numConstDims; index++) {
SW_NC_get_dim_identifier(
Expand Down Expand Up @@ -994,10 +1003,36 @@ void SW_NC_create_full_var(
return; // Exit function prematurely due to error
}

tempCoordPtr = sw_memccpy(writePtr, " ", '\0', writeSize);
writeSize--;
writePtr = tempCoordPtr - 1;

tempCoordPtr = sw_memccpy(
writePtr, (char *) timeVertVegNames[index], '\0', writeSize
);
writeSize -= (int) (tempCoordPtr - coordValBuf - 1);
writePtr = tempCoordPtr - 1;

dimArrSize++;
}
}

if (coordAttIndex > -1 && !isnull(attVals[coordAttIndex])) {
snprintf(
finalCoordVal,
MAX_FILENAMESIZE,
"%s%s",
attVals[coordAttIndex],
coordValBuf
);

free((void *) ((char *) attVals[coordAttIndex]));
attVals[coordAttIndex] = Str_Dup(finalCoordVal, LogInfo);
if (LogInfo->stopRun) {
return;
}
}

for (index = numConstDims; index < MAX_NUM_DIMS; index++) {
if (index - numConstDims < 3) {
varVal = timeVertVegVals[index - numConstDims];
Expand Down
1 change: 1 addition & 0 deletions src/SW_netCDF_Input.c
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,7 @@ void SW_NCIN_create_progress(SW_DOMAIN *SW_Domain, LOG_INFO *LogInfo) {
SW_Domain->OutDom.netCDFOutput.deflateLevel,
readinGeoYName,
readinGeoXName,
-1,
LogInfo
);

Expand Down
48 changes: 44 additions & 4 deletions src/SW_netCDF_Output.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ into one location to write out
variable information
@param[out] resAtts Resulting attributes to write out
@param[in] sumType Sum type of the output key
@param[in] siteDom Specifies if the domain is site-oriented
@param[in] readinYName User-provided geographical y-axis name
@param[in] readinXName User-provided geographical x-axis name
@param[out] LogInfo Holds information on warnings and errors
*/
static int gather_var_attributes(
Expand All @@ -597,13 +600,17 @@ static int gather_var_attributes(
int varNum,
char *resAtts[],
OutSum sumType,
Bool siteDom,
const char *readinYName,
const char *readinXName,
LOG_INFO *LogInfo
) {
int fillSize = 0;
int varIndex;
int resSNP;
char cellRedef[MAX_FILENAMESIZE];
char establOrginName[MAX_FILENAMESIZE];
char coordsAtt[MAX_FILENAMESIZE];

// Determine attribute 'original_name'
if (key == eSW_Estab) {
Expand Down Expand Up @@ -665,6 +672,29 @@ static int gather_var_attributes(
}
}

/* Fill coordinates attribute */
snprintf(
coordsAtt,
MAX_FILENAMESIZE,
(siteDom) ? "%s %s site" : "%s %s",
readinYName,
readinXName
);
if (resSNP < 0 || (unsigned) resSNP >= (sizeof coordsAtt)) {
LogError(
LogInfo,
LOGWARN,
"attribute 'coordinates' of variable '%s' was truncated.",
varInfo[VARNAME_INDEX]
);
}

resAtts[fillSize] = Str_Dup(coordsAtt, LogInfo);
if (LogInfo->stopRun) {
return 0; // Exit function prematurely due to error
}
fillSize++;

if (key == eSW_Temp || key == eSW_SoilTemp) {
resAtts[fillSize] = (char *) "temperature: on_scale";
fillSize++;
Expand Down Expand Up @@ -923,13 +953,16 @@ static void create_output_file(
"comment",
"units",
"cell_method",
"coordinates",
"units_metadata"
};
char *attVals[MAX_NATTS] = {NULL};
OutSum sumType = OutDom->sumtype[key];
Bool siteDom = (Bool) (strcmp(domType, "s") == 0);

int numAtts = 0;
const int nameAtt = 0;
const int coordAttInd = 5;

int newFileID = -1; // Default to not created
int cellMethAttInd = 0;
Expand Down Expand Up @@ -1001,15 +1034,16 @@ static void create_output_file(
deflateLevel,
latName,
lonName,
coordAttInd,
LogInfo
);

if (pd > eSW_Day) {
if (newFileID > -1) {
// new file was created
cellMethAttInd = (key == eSW_Temp || key == eSW_SoilTemp) ?
numAtts - 2 :
numAtts - 1;
numAtts - 3 :
numAtts - 2;
}
if (!isnull(attVals[cellMethAttInd])) {
free(attVals[cellMethAttInd]);
Expand All @@ -1018,19 +1052,25 @@ static void create_output_file(
}
if (key == eSW_Estab && !isnull(attVals[nameAtt])) {
free(attVals[nameAtt]);
attVals[nameAtt] = NULL;
}
if (!isnull(attVals[coordAttInd])) {
free(attVals[coordAttInd]);
attVals[coordAttInd] = NULL;
}
if (LogInfo->stopRun && FileExists(newFileName)) {
nc_close(newFileID);
return; // Exit function prematurely due to error
goto closeFile;
}
}
}

closeFile: {
// Only close the file if it was created
if (newFileID > -1) {
nc_close(newFileID);
}
}
}

/* =================================================== */
/* Global Function Definitions */
Expand Down

0 comments on commit bef362c

Please sign in to comment.