Ignore:
Timestamp:
11/20/23 13:05:46 (6 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/branches/xios-3.0-beta/src/io/nc4_data_output.cpp

    r2530 r2598  
    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{ 
     
    22652272              { 
    22662273                 CTimer::get("Files : writing data").resume(); 
    2267                  SuperClassWriter::writeData(data, fieldid, isCollective, nstep - 1); 
     2274                 writeAndConvertData(field, data, nstep - 1); 
    22682275                 CTimer::get("Files : writing data").suspend(); 
    22692276                 if (wtime) 
     
    24292436 
    24302437                CTimer::get("Files : writing data").resume(); 
    2431                 SuperClassWriter::writeData(data, fieldid, isCollective, nstep - 1, &start, &count); 
     2438                writeAndConvertData(field, data, nstep - 1, &start, &count); 
    24322439                CTimer::get("Files : writing data").suspend(); 
    24332440 
     
    24632470           ERROR("CNc4DataOutput::writeFieldData_ (CField*  field)", << msg); 
    24642471         } 
     2472      } 
     2473 
     2474      void CNc4DataOutput::writeAndConvertData(CField* field, const CArray<double,1>& data, StdSize record, const std::vector<StdSize> *start, const std::vector<StdSize> *count) 
     2475      { 
     2476        StdString fieldid = field->getFieldOutputName(); 
     2477        nc_type type ; 
     2478        if (field->prec.isEmpty()) type =  NC_FLOAT ; 
     2479        else 
     2480        { 
     2481          if (field->prec==2) type = NC_SHORT ; 
     2482          else if (field->prec==4)  type =  NC_FLOAT ; 
     2483          else if (field->prec==8)   type =  NC_DOUBLE ; 
     2484        } 
     2485 
     2486        bool conversionByNetCDF = true; // default : conversion operated by NetCDF for now (legacy behaviour) 
     2487        if (!field->conversion_by_NetCDF.isEmpty()) 
     2488        { 
     2489          // use conversion_by_NetCDF = ".false." to  bypass NetCDF conversion 
     2490          //   poor performances from NC_DOUBLE to NC_FLOAT with isgreater/isless in recent NetCDF available at TGCC 
     2491          conversionByNetCDF = field->conversion_by_NetCDF; 
     2492        } 
     2493        if ( ( type == NC_DOUBLE ) || ( conversionByNetCDF ) ) 
     2494        { 
     2495          SuperClassWriter::writeData(data, fieldid, isCollective, record, start, count); 
     2496        } 
     2497        else if ( type == NC_FLOAT ) 
     2498        { 
     2499          CArray<float,1> f_data; 
     2500          f_data.resize( data.numElements() ); 
     2501          for (int i = 0; i < data.numElements(); i++)  
     2502          { 
     2503            if (data(i) <= X_FLOAT_MAX || data(i) >= X_FLOAT_MIN) 
     2504              f_data(i) = data(i); 
     2505            else if (data(i) > X_FLOAT_MAX) 
     2506              f_data(i) = X_FLOAT_MAX; 
     2507            else if (data(i) < X_FLOAT_MIN) 
     2508              f_data(i) = X_FLOAT_MIN; 
     2509          } 
     2510          SuperClassWriter::writeData(f_data, fieldid, isCollective, record, start, count); 
     2511        } 
     2512        else if ( type == NC_SHORT ) 
     2513        { 
     2514          CArray<short,1> s_data; 
     2515          s_data.resize( data.numElements() ); 
     2516          for (int i = 0; i < data.numElements(); i++)  
     2517          { 
     2518            if (data(i) <= X_SHORT_MAX || data(i) >= X_SHORT_MIN) 
     2519              s_data(i) = data(i); 
     2520            else if (data(i) > X_SHORT_MAX) 
     2521              s_data(i) = X_SHORT_MAX; 
     2522            else if (data(i) < X_SHORT_MIN) 
     2523              s_data(i) = X_SHORT_MIN; 
     2524          } 
     2525          SuperClassWriter::writeData(s_data, fieldid, isCollective, record, start, count); 
     2526        } 
     2527        else 
     2528        { 
     2529            ERROR("CNc4DataOutput::writeAndConvertData(...)", << "Type conversion not managed for " << fieldid );  
     2530        } 
     2531         
    24652532      } 
    24662533 
Note: See TracChangeset for help on using the changeset viewer.