Ignore:
Timestamp:
11/20/23 14:07:25 (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
  • XIOS2/trunk/src/io/nc4_data_output.cpp

    r2515 r2599  
    1313#include "uuid.hpp" 
    1414#include "mesh_values.hpp" 
     15 
     16#include <limits.h> 
     17#define X_FLOAT_MAX     FLT_MAX 
     18#define X_FLOAT_MIN     FLT_MIN 
     19#define X_SHORT_MAX     SHRT_MAX 
     20#define X_SHORT_MIN     SHRT_MIN 
     21 
    1522namespace xios 
    1623{ 
     
    25152522              { 
    25162523                 CTimer::get("Files : writing data").resume(); 
    2517                  SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1); 
     2524                 writeAndConvertData(field, fieldData, field->getNStep() - 1); 
    25182525                 CTimer::get("Files : writing data").suspend(); 
    25192526                 if (wtime) 
     
    26552662 
    26562663                CTimer::get("Files : writing data").resume(); 
    2657                 SuperClassWriter::writeData(fieldData, fieldid, isCollective, field->getNStep() - 1, &start, &count); 
     2664                writeAndConvertData(field, fieldData, field->getNStep() - 1, &start, &count); 
    26582665                CTimer::get("Files : writing data").suspend(); 
    26592666 
     
    26882695           ERROR("CNc4DataOutput::writeFieldData_ (CField*  field)", << msg); 
    26892696         } 
     2697      } 
     2698 
     2699      void CNc4DataOutput::writeAndConvertData(CField* field, const CArray<double,1>& data, StdSize record, const std::vector<StdSize> *start, const std::vector<StdSize> *count) 
     2700      { 
     2701        StdString fieldid = field->getFieldOutputName(); 
     2702        nc_type type ; 
     2703        if (field->prec.isEmpty()) type =  NC_FLOAT ; 
     2704        else 
     2705        { 
     2706          if (field->prec==2) type = NC_SHORT ; 
     2707          else if (field->prec==4)  type =  NC_FLOAT ; 
     2708          else if (field->prec==8)   type =  NC_DOUBLE ; 
     2709        } 
     2710 
     2711        bool conversionByNetCDF = true; // default : conversion operated by NetCDF for now (legacy behaviour) 
     2712        if (!field->conversion_by_NetCDF.isEmpty()) 
     2713        { 
     2714          // use conversion_by_NetCDF = ".false." to  bypass NetCDF conversion 
     2715          //   poor performances from NC_DOUBLE to NC_FLOAT with isgreater/isless in recent NetCDF available at TGCC 
     2716          conversionByNetCDF = field->conversion_by_NetCDF; 
     2717        } 
     2718        if ( ( type == NC_DOUBLE ) || ( conversionByNetCDF ) ) 
     2719        { 
     2720          SuperClassWriter::writeData(data, fieldid, isCollective, record, start, count); 
     2721        } 
     2722        else if ( type == NC_FLOAT ) 
     2723        { 
     2724          CArray<float,1> f_data; 
     2725          f_data.resize( data.numElements() ); 
     2726          for (int i = 0; i < data.numElements(); i++)  
     2727          { 
     2728            if (data(i) <= X_FLOAT_MAX || data(i) >= X_FLOAT_MIN) 
     2729              f_data(i) = data(i); 
     2730            else if (data(i) > X_FLOAT_MAX) 
     2731              f_data(i) = X_FLOAT_MAX; 
     2732            else if (data(i) < X_FLOAT_MIN) 
     2733              f_data(i) = X_FLOAT_MIN; 
     2734          } 
     2735          SuperClassWriter::writeData(f_data, fieldid, isCollective, record, start, count); 
     2736        } 
     2737        else if ( type == NC_SHORT ) 
     2738        { 
     2739          CArray<short,1> s_data; 
     2740          s_data.resize( data.numElements() ); 
     2741          for (int i = 0; i < data.numElements(); i++)  
     2742          { 
     2743            if (data(i) <= X_SHORT_MAX || data(i) >= X_SHORT_MIN) 
     2744              s_data(i) = data(i); 
     2745            else if (data(i) > X_SHORT_MAX) 
     2746              s_data(i) = X_SHORT_MAX; 
     2747            else if (data(i) < X_SHORT_MIN) 
     2748              s_data(i) = X_SHORT_MIN; 
     2749          } 
     2750          SuperClassWriter::writeData(s_data, fieldid, isCollective, record, start, count); 
     2751        } 
     2752        else 
     2753        { 
     2754            ERROR("CNc4DataOutput::writeAndConvertData(...)", << "Type conversion not managed for " << fieldid );  
     2755        } 
     2756         
    26902757      } 
    26912758 
Note: See TracChangeset for help on using the changeset viewer.