Ignore:
Timestamp:
11/20/23 14:30:12 (8 months ago)
Author:
jderouillat
Message:

Add a field attribute, conversion_by_NetCDF, to operate type conversion in XIOS, and not in NetCDF

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS3/trunk/src/io/nc4_data_output.cpp

    r2577 r2600  
    1212#include "timer.hpp" 
    1313#include "uuid.hpp" 
     14 
     15#include <limits.h> 
     16#define X_FLOAT_MAX     FLT_MAX 
     17#define X_FLOAT_MIN     FLT_MIN 
     18#define X_SHORT_MAX     SHRT_MAX 
     19#define X_SHORT_MIN     SHRT_MIN 
     20 
    1421namespace xios 
    1522{ 
     
    22692276              { 
    22702277                 CTimer::get("Files : writing data").resume(); 
    2271                  SuperClassWriter::writeData(data, fieldid, isCollective, nstep - 1); 
     2278                 writeAndConvertData(field, data, nstep - 1); 
    22722279                 CTimer::get("Files : writing data").suspend(); 
    22732280                 if (wtime) 
     
    24332440 
    24342441                CTimer::get("Files : writing data").resume(); 
    2435                 SuperClassWriter::writeData(data, fieldid, isCollective, nstep - 1, &start, &count); 
     2442                writeAndConvertData(field, data, nstep - 1, &start, &count); 
    24362443                CTimer::get("Files : writing data").suspend(); 
    24372444 
     
    24672474           ERROR("CNc4DataOutput::writeFieldData_ (CField*  field)", << msg); 
    24682475         } 
     2476      } 
     2477 
     2478      void CNc4DataOutput::writeAndConvertData(CField* field, const CArray<double,1>& data, StdSize record, const std::vector<StdSize> *start, const std::vector<StdSize> *count) 
     2479      { 
     2480        StdString fieldid = field->getFieldOutputName(); 
     2481        nc_type type ; 
     2482        if (field->prec.isEmpty()) type =  NC_FLOAT ; 
     2483        else 
     2484        { 
     2485          if (field->prec==2) type = NC_SHORT ; 
     2486          else if (field->prec==4)  type =  NC_FLOAT ; 
     2487          else if (field->prec==8)   type =  NC_DOUBLE ; 
     2488        } 
     2489 
     2490        bool conversionByNetCDF = true; // default : conversion operated by NetCDF for now (legacy behaviour) 
     2491        if (!field->conversion_by_NetCDF.isEmpty()) 
     2492        { 
     2493          // use conversion_by_NetCDF = ".false." to  bypass NetCDF conversion 
     2494          //   poor performances from NC_DOUBLE to NC_FLOAT with isgreater/isless in recent NetCDF available at TGCC 
     2495          conversionByNetCDF = field->conversion_by_NetCDF; 
     2496        } 
     2497        if ( ( type == NC_DOUBLE ) || ( conversionByNetCDF ) ) 
     2498        { 
     2499          SuperClassWriter::writeData(data, fieldid, isCollective, record, start, count); 
     2500        } 
     2501        else if ( type == NC_FLOAT ) 
     2502        { 
     2503          CArray<float,1> f_data; 
     2504          f_data.resize( data.numElements() ); 
     2505          for (int i = 0; i < data.numElements(); i++)  
     2506          { 
     2507            if (data(i) <= X_FLOAT_MAX || data(i) >= X_FLOAT_MIN) 
     2508              f_data(i) = data(i); 
     2509            else if (data(i) > X_FLOAT_MAX) 
     2510              f_data(i) = X_FLOAT_MAX; 
     2511            else if (data(i) < X_FLOAT_MIN) 
     2512              f_data(i) = X_FLOAT_MIN; 
     2513          } 
     2514          SuperClassWriter::writeData(f_data, fieldid, isCollective, record, start, count); 
     2515        } 
     2516        else if ( type == NC_SHORT ) 
     2517        { 
     2518          CArray<short,1> s_data; 
     2519          s_data.resize( data.numElements() ); 
     2520          for (int i = 0; i < data.numElements(); i++)  
     2521          { 
     2522            if (data(i) <= X_SHORT_MAX || data(i) >= X_SHORT_MIN) 
     2523              s_data(i) = data(i); 
     2524            else if (data(i) > X_SHORT_MAX) 
     2525              s_data(i) = X_SHORT_MAX; 
     2526            else if (data(i) < X_SHORT_MIN) 
     2527              s_data(i) = X_SHORT_MIN; 
     2528          } 
     2529          SuperClassWriter::writeData(s_data, fieldid, isCollective, record, start, count); 
     2530        } 
     2531        else 
     2532        { 
     2533            ERROR("CNc4DataOutput::writeAndConvertData(...)", << "Type conversion not managed for " << fieldid );  
     2534        } 
     2535         
    24692536      } 
    24702537 
Note: See TracChangeset for help on using the changeset viewer.