[cmor-tables] 12/83: 2010-05-24 : changed the way the final file name is constructed, uses time bounds if present, otherwise as before uses time values
Alastair McKinstry
mckinstry at moszumanska.debian.org
Sun Aug 30 08:05:58 UTC 2015
This is an automated email from the git hooks/post-receive script.
mckinstry pushed a commit to branch master
in repository cmor-tables.
commit e9149be3623ce20df78b59cfb9184eb147f923c2
Author: Charles Doutriaux <doutriaux1 at llnl.gov>
Date: Mon May 24 12:35:38 2010 -0700
2010-05-24 : changed the way the final file name is constructed, uses time bounds if present, otherwise as before uses time values
---
Src/cmor.c | 17 +++++++++--
Src/cmor_axes.c | 12 +++++++-
Src/cmor_variables.c | 67 ++++++++++++++++++++++++++++---------------
Test/test_python_jamie_3hr.py | 51 ++++++++++++++++++++++++++++++++
include/cmor.h | 2 ++
5 files changed, 122 insertions(+), 27 deletions(-)
diff --git a/Src/cmor.c b/Src/cmor.c
index 1da9849..570c6fd 100644
--- a/Src/cmor.c
+++ b/Src/cmor.c
@@ -490,6 +490,8 @@ void cmor_reset_variable(int var_id) {
cmor_vars[var_id].values = NULL;
cmor_vars[var_id].first_time=-999.;
cmor_vars[var_id].last_time=-999.;
+ cmor_vars[var_id].first_bound=1.e20;
+ cmor_vars[var_id].last_bound=1.e20;
cmor_vars[var_id].base_path[0]='\0';
cmor_vars[var_id].current_path[0]='\0';
cmor_vars[var_id].suffix[0]='\0';
@@ -3520,8 +3522,12 @@ int cmor_close_variable(int var_id, char *file_name, int *preserve)
}
/* ok makes a comptime out of input */
- cdRel2Comp(icalo,msg,cmor_vars[var_id].first_time,&comptime);
-
+ if (cmor_vars[var_id].first_bound!=1.e20) {
+ cdRel2Comp(icalo,msg,cmor_vars[var_id].first_bound,&comptime);
+ }
+ else {
+ cdRel2Comp(icalo,msg,cmor_vars[var_id].first_time,&comptime);
+ }
/* need to figure out the approximate interval */
interval = cmor_convert_interval_to_seconds(cmor_tables[cmor_axes[cmor_vars[var_id].axes_ids[0]].ref_table_id].interval,cmor_tables[cmor_axes[cmor_vars[var_id].axes_ids[0]].ref_table_id].axes[cmor_axes[cmor_vars[var_id].axes_ids[0]].ref_axis_id].units);
@@ -3553,7 +3559,12 @@ int cmor_close_variable(int var_id, char *file_name, int *preserve)
}
- cdRel2Comp(icalo,msg,cmor_vars[var_id].last_time,&comptime);
+ if (cmor_vars[var_id].last_bound!=1.e20) {
+ cdRel2Comp(icalo,msg,cmor_vars[var_id].last_bound,&comptime);
+ }
+ else {
+ cdRel2Comp(icalo,msg,cmor_vars[var_id].last_time,&comptime);
+ }
/* separator between first and last time */
strncat(outname,"-",CMOR_MAX_STRING-strlen(outname));
diff --git a/Src/cmor_axes.c b/Src/cmor_axes.c
index 6a2931a..ed69d26 100644
--- a/Src/cmor_axes.c
+++ b/Src/cmor_axes.c
@@ -1019,7 +1019,17 @@ int cmor_check_interval(int axis_id, char *interval, double *values, int nvalues
tmp = (double)fabs(diff-interv);
tmp = tmp/interv;
if (tmp>.2) { /* more than 20% diff issues an error */
- snprintf(ctmp,CMOR_MAX_STRING,"approximate time axis interval is defined as %f seconds (%s), for value %i we got a difference of %f, which is %f %% , seems too big, check your values", interv, interval, i+1, diff, tmp*100.);
+ if (isbounds==1) {
+ snprintf(ctmp,CMOR_MAX_STRING,"approximate time axis interval is defined as %f seconds (%s), for value %i we got a difference (based on bounds) of %f, which is %f %% , seems too big, check your values", interv, interval, i+1, diff, tmp*100.);
+ }
+ else {
+ if (isbounds==1) {
+ snprintf(ctmp,CMOR_MAX_STRING,"approximate time axis interval is defined as %f seconds (%s), for value %i we got a difference (based on bounds) of %f, which is %f %% , seems too big, check your values", interv, interval, i+1, diff, tmp*100.);
+ }
+ else {
+ snprintf(ctmp,CMOR_MAX_STRING,"approximate time axis interval is defined as %f seconds (%s), for value %i we got a difference of %f, which is %f %% , seems too big, check your values", interv, interval, i+1, diff, tmp*100.);
+ }
+ }
cmor_handle_error(ctmp,CMOR_CRITICAL);
}
else if (tmp>.1) { /* more than 10% diff issues a warning */
diff --git a/Src/cmor_variables.c b/Src/cmor_variables.c
index 268b2f1..188f4d2 100644
--- a/Src/cmor_variables.c
+++ b/Src/cmor_variables.c
@@ -1448,38 +1448,45 @@ int cmor_write_var_to_file(int ncid,cmor_var_t *avar,void *data,char itype, int
snprintf(msg,CMOR_MAX_STRING,"in cmor_write: variable '%s' you are passing time values but you already defined them via cmor_axis, this is not allowed",avar->id);
cmor_handle_error(msg,CMOR_CRITICAL);
}
-
+
if (time_bounds!=NULL) {
-
+
counts2[0]=counts[0];
counts2[1]=2;
cmor_get_axis_attribute(avar->axes_ids[0],"units",'c',&msg);
cmor_get_cur_dataset_attribute("calendar",msg2);
-
-
-/* else { */
-/* /\* checking manually that time is within bounds *\/ */
-/* if ((time_vals[0]<time_bounds[0]) || (time_vals[0]>time_bounds[1])) { */
-/* snprintf(msg,CMOR_MAX_STRING, "Time point: %lf is not within the bounds you passed: [%lf, %lf]\n",time_vals[0],time_bounds[0],time_bounds[1]); */
-/* cmor_handle_error(msg,CMOR_CRITICAL); */
-/* } */
-/* } */
+
+
+ /* else { */
+ /* /\* checking manually that time is within bounds *\/ */
+ /* if ((time_vals[0]<time_bounds[0]) || (time_vals[0]>time_bounds[1])) { */
+ /* snprintf(msg,CMOR_MAX_STRING, "Time point: %lf is not within the bounds you passed: [%lf, %lf]\n",time_vals[0],time_bounds[0],time_bounds[1]); */
+ /* cmor_handle_error(msg,CMOR_CRITICAL); */
+ /* } */
+ /* } */
tmp_vals = malloc(ntimes_passed*2*sizeof(double));
if (tmp_vals == NULL) {
snprintf(msg,CMOR_MAX_STRING,"cmor_write: cannot malloc %i tmp bounds time vals for variable '%s'",ntimes_passed*2,avar->id);
cmor_handle_error(msg,CMOR_CRITICAL);
}
-
+
ierr = cmor_convert_time_values(time_vals,'d',ntimes_passed,&tmp_vals[0],cmor_axes[avar->axes_ids[0]].iunits,msg,msg2,msg2);
ierr = cmor_check_monotonic(&tmp_vals[0],ntimes_passed,"time",0,avar->axes_ids[0]);
-
+
ierr = cmor_convert_time_values(time_bounds,'d',ntimes_passed*2,&tmp_vals[0],cmor_axes[avar->axes_ids[0]].iunits,msg,msg2,msg2);
ierr = cmor_check_monotonic(&tmp_vals[0],ntimes_passed*2,"time",1,avar->axes_ids[0]);
ierr = cmor_check_values_inside_bounds(&time_vals[0],&time_bounds[0], ntimes_passed, "time");
ierr = nc_put_vara_double(ncid,avar->time_bnds_nc_id,starts,counts2,tmp_vals);
if (ierr != NC_NOERR) {snprintf(msg,CMOR_MAX_STRING,"NetCDF error (%i) writing time bounds for variable '%s', already written in file: %i",ierr,avar->id,avar->ntimes_written);cmor_handle_error(msg,CMOR_CRITICAL);}
-
+ /* ok first time around the we need to store bounds */
+ if (avar->ntimes_written == 0) {
+ /* ok first time we're putting data in */
+ avar->first_bound = tmp_vals[0];
+ }
+ avar->last_bound = tmp_vals[ntimes_passed*2-1];
+
+
/* ok since we have bounds we need to set time in the middle */
/* but only do this in case of none climato */
if (cmor_tables[cmor_axes[avar->axes_ids[0]].ref_table_id].axes[cmor_axes[avar->axes_ids[0]].ref_axis_id].climatology==0) {
@@ -1492,10 +1499,10 @@ int cmor_write_var_to_file(int ncid,cmor_var_t *avar,void *data,char itype, int
ierr = cmor_convert_time_values(time_vals,'d',ntimes_passed,&tmp_vals[0],cmor_axes[avar->axes_ids[0]].iunits,msg,msg2,msg2);
}
-
+
ierr = nc_put_vara_double(ncid,avar->time_nc_id,starts,counts,&tmp_vals[0]);
if (ierr != NC_NOERR) {snprintf(msg,CMOR_MAX_STRING,"NetCDF error (%i) writing time values for variable '%s'",ierr,avar->id);cmor_handle_error(msg,CMOR_CRITICAL);}
-
+
if (cmor_tables[cmor_axes[avar->axes_ids[0]].ref_table_id].axes[cmor_axes[avar->axes_ids[0]].ref_axis_id].climatology==0) {
/* all good in that case */
}
@@ -1513,9 +1520,9 @@ int cmor_write_var_to_file(int ncid,cmor_var_t *avar,void *data,char itype, int
cmor_handle_error(msg,CMOR_CRITICAL);
}
}
-/* printf("setting last time to: %lf\n",tmp_vals[ntimes_passed-1]); */
+ /* printf("setting last time to: %lf\n",tmp_vals[ntimes_passed-1]); */
avar->last_time = tmp_vals[ntimes_passed-1];
-
+
free(tmp_vals);
}
else {
@@ -1524,7 +1531,8 @@ int cmor_write_var_to_file(int ncid,cmor_var_t *avar,void *data,char itype, int
snprintf(msg,CMOR_MAX_STRING,"in cmor_write, time axis must have bounds, please pass them to cmor_write along with time values");
cmor_handle_error(msg,CMOR_CRITICAL);
}
-
+ avar->first_bound=1.e20;
+ avar->last_bound=1.e20;
cmor_get_axis_attribute(avar->axes_ids[0],"units",'c',&msg);
cmor_get_cur_dataset_attribute("calendar",msg2);
tmp_vals = malloc(ntimes_passed*sizeof(double));
@@ -1534,13 +1542,13 @@ int cmor_write_var_to_file(int ncid,cmor_var_t *avar,void *data,char itype, int
}
ierr = cmor_convert_time_values(time_vals,'d',ntimes_passed,&tmp_vals[0],cmor_axes[avar->axes_ids[0]].iunits,msg,msg2,msg2);
ierr = nc_put_vara_double(ncid,avar->time_nc_id,starts,counts,tmp_vals);
-/* printf("setting last time to (4): %lf\n",tmp_vals[ntimes_passed-1]); */
+ /* printf("setting last time to (4): %lf\n",tmp_vals[ntimes_passed-1]); */
if (avar->ntimes_written == 0) {
/* ok first time we're putting data in */
avar->first_time = tmp_vals[0];
}
avar->last_time = tmp_vals[ntimes_passed-1];
-/* printf("setting last time to: %f\n",avar->last_time); */
+ /* printf("setting last time to: %f\n",avar->last_time); */
free(tmp_vals);
if (ierr != NC_NOERR) {snprintf(msg,CMOR_MAX_STRING,"NetCDF error (%i) writing times for variable '%s', already written in file: %i",ierr,avar->id,avar->ntimes_written);cmor_handle_error(msg,CMOR_CRITICAL);}
}
@@ -1556,6 +1564,11 @@ int cmor_write_var_to_file(int ncid,cmor_var_t *avar,void *data,char itype, int
counts2[1]=2;
ierr = nc_put_vara_double(ncid,avar->time_bnds_nc_id,starts,counts2,&cmor_axes[avar->axes_ids[0]].bounds[starts[0]*2]);
if (ierr != NC_NOERR) {snprintf(msg,CMOR_MAX_STRING,"NCError (%i) writting time bounds values for variable '%s'",ierr,avar->id);cmor_handle_error(msg,CMOR_CRITICAL);}
+ /* ok we need to store first and last bounds */
+ if (avar->ntimes_written==0) {
+ avar->first_bound=cmor_axes[avar->axes_ids[0]].bounds[starts[0]*2];
+ }
+ avar->last_bound=cmor_axes[avar->axes_ids[0]].bounds[(starts[0]+counts[0])*2];
}
else {
/* checks wether you need bounds or not */
@@ -1563,12 +1576,16 @@ int cmor_write_var_to_file(int ncid,cmor_var_t *avar,void *data,char itype, int
snprintf(msg,CMOR_MAX_STRING,"in cmor_write, time axis must have bounds, you defined it w/o");
cmor_handle_error(msg,CMOR_CRITICAL);
}
+ avar->first_bound=1.e20;
+ avar->last_bound=1.e20;
}
ierr = nc_put_vara_double(ncid,avar->time_nc_id,starts,counts,&cmor_axes[avar->axes_ids[0]].values[starts[0]]);
if (ierr != NC_NOERR) {snprintf(msg,CMOR_MAX_STRING,"NCError (%i) writting time values for variable '%s'",ierr,avar->id);cmor_handle_error(msg,CMOR_CRITICAL);}
/* ok now we need to stroe first and last stuff */
- avar->first_time = cmor_axes[avar->axes_ids[0]].values[0];
-/* printf("setting last time (2) to: %lf\n",cmor_axes[avar->axes_ids[0]].values[starts[0]+counts[0]-1]); */
+ if (avar->ntimes_written==0) {
+ avar->first_time = cmor_axes[avar->axes_ids[0]].values[starts[0]];
+ }
+ /* printf("setting last time (2) to: %lf\n",cmor_axes[avar->axes_ids[0]].values[starts[0]+counts[0]-1]); */
avar->last_time = cmor_axes[avar->axes_ids[0]].values[starts[0]+counts[0]-1];
}
}
@@ -1586,6 +1603,8 @@ int cmor_write_var_to_file(int ncid,cmor_var_t *avar,void *data,char itype, int
snprintf(msg,CMOR_MAX_STRING,"in cmor_write: variable '%s' you are passing %i times but no values and you did not define them via cmor_axis",avar->id,ntimes_passed);
cmor_handle_error(msg,CMOR_CRITICAL);
}
+ avar->first_bound=1.e20;
+ avar->last_bound=1.e20;
if (cmor_axes[avar->axes_ids[ierr]].bounds!=NULL) {
/* ok at that stage the recentering must already be done so we just need to write the bounds */
counts2[0]=counts[0];
@@ -1594,6 +1613,8 @@ int cmor_write_var_to_file(int ncid,cmor_var_t *avar,void *data,char itype, int
starts[1]=0;
ierr = nc_put_vara_double(ncid,avar->time_bnds_nc_id,starts,counts2,&cmor_axes[avar->axes_ids[0]].bounds[starts[0]*2]);
if (ierr != NC_NOERR) {snprintf(msg,CMOR_MAX_STRING,"NCError (%i) writting time bounds values for variable '%s'",ierr,avar->id);cmor_handle_error(msg,CMOR_CRITICAL);}
+ avar->first_bound=cmor_axes[avar->axes_ids[0]].bounds[0];
+ avar->last_bound=cmor_axes[avar->axes_ids[0]].bounds[counts[0]*2];
}
ierr = nc_put_vara_double(ncid,avar->time_nc_id,starts,counts,&cmor_axes[avar->axes_ids[0]].values[starts[0]]);
if (ierr != NC_NOERR) {snprintf(msg,CMOR_MAX_STRING,"NCError (%i) writting time values for variable '%s'",ierr,avar->id);cmor_handle_error(msg,CMOR_CRITICAL);}
diff --git a/Test/test_python_jamie_3hr.py b/Test/test_python_jamie_3hr.py
new file mode 100644
index 0000000..762ab13
--- /dev/null
+++ b/Test/test_python_jamie_3hr.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+import cmor
+import numpy
+
+def main():
+
+ cmor.setup(inpath='Tables',
+ netcdf_file_action = cmor.CMOR_REPLACE_3)
+ cmor.dataset('pre-industrial control', 'ukmo', 'HadCM3', '360_day',
+ institute_id = 'ukmo',
+ model_id = 'HadCM3',
+ history = 'some global history',
+ forcing = 'N/A',
+ parent_experiment_id = 'N/A',
+ branch_time = 0.,
+ contact = 'bob')
+
+ table = 'CMIP5_3hr'
+ cmor.load_table(table)
+ axes = [ {'table_entry': 'time1',
+ 'units': 'hours since 2000-01-01 00:00:00',
+ },
+ {'table_entry': 'latitude',
+ 'units': 'degrees_north',
+ 'coord_vals': [0],
+ 'cell_bounds': [-1, 1]},
+ {'table_entry': 'longitude',
+ 'units': 'degrees_east',
+ 'coord_vals': [90],
+ 'cell_bounds': [89, 91]},
+ ]
+
+ values = numpy.array([1.], numpy.float32)+200
+ axis_ids = list()
+ for axis in axes:
+ axis_id = cmor.axis(**axis)
+ axis_ids.append(axis_id)
+
+ varid = cmor.variable('tas',
+ 'K',
+ axis_ids,
+ history = 'variable history',
+ missing_value = -99,
+ )
+ cmor.write(varid, values, time_vals = [0.], time_bnds = [ [0,3.] ])
+
+ cmor.close()
+
+if __name__ == '__main__':
+
+ main()
diff --git a/include/cmor.h b/include/cmor.h
index 8a70d40..624b712 100644
--- a/include/cmor.h
+++ b/include/cmor.h
@@ -216,6 +216,8 @@ typedef struct cmor_var_ {
double *values;
double first_time;
double last_time;
+ double first_bound;
+ double last_bound;
char base_path[CMOR_MAX_STRING];
char current_path[CMOR_MAX_STRING];
char suffix[CMOR_MAX_STRING];
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/cmor-tables.git
More information about the debian-science-commits
mailing list