/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/sedflux/run_data_dump.c

Go to the documentation of this file.
00001 //---
00002 //
00003 // This file is part of sedflux.
00004 //
00005 // sedflux is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // sedflux is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with sedflux; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //
00019 //---
00020 
00021 #define SED_DATA_DUMP_PROC_NAME "data dump"
00022 #define EH_LOG_DOMAIN SED_DATA_DUMP_PROC_NAME
00023 
00024 #include <string.h>
00025 #include <utils/utils.h>
00026 #include <sed/sed_sedflux.h>
00027 #include "my_processes.h"
00028 
00029 Sed_process_info
00030 run_data_dump( Sed_process proc , Sed_cube prof )
00031 {
00032    Data_dump_t*     data = sed_process_user_data(proc);
00033    Sed_process_info info = SED_EMPTY_INFO;
00034    int i;
00035    char str[S_NAMEMAX], *filename;
00036    gchar* cube_name;
00037    Sed_property_file fp;
00038    Sed_property_file_attr attr;
00039    Sed_property property;
00040 
00041    data->count++;
00042    sprintf(str,"%04d",data->count);
00043 
00044    cube_name = sed_cube_name( prof );
00045    for (i=0; data->property && i<data->property->len ;i++)
00046    {
00047       property = sed_property_dup( g_array_index( data->property , Sed_property , i ) );
00048 
00049       filename = g_strconcat( data->output_dir  ,
00050                               G_DIR_SEPARATOR_S ,
00051                               cube_name         ,
00052                               str ,
00053                               "." ,
00054                               sed_property_extension(property) , NULL );
00055 
00056       attr = sed_property_file_attr_new( );
00057 
00058 eh_warning( "property file attributes are not being used." );
00059 /*
00060       sed_set_sed_file_attr_y_res( attr , data->vertical_resolution );
00061       sed_set_sed_file_attr_x_res( attr , data->horizontal_resolution );
00062       sed_set_sed_file_attr_y_lim( attr , data->y_lim_min , data->y_lim_max );
00063       sed_set_sed_file_attr_x_lim( attr , data->x_lim_min , data->x_lim_max );
00064 */
00065 
00066       fp = sed_property_file_new( filename , property , NULL );
00067 
00068       sed_property_file_write( fp , prof );
00069       sed_property_file_attr_destroy( attr );
00070       sed_property_file_destroy( fp );
00071 
00072       eh_message( "time                           : %f" ,
00073                   sed_cube_age_in_years(prof) );
00074       eh_message( "filename                       : %s" , filename        );
00075       eh_message( "vertical resolution (0=full)   : %f" ,
00076                   data->vertical_resolution );
00077       eh_message( "horizontal resolution (0=full) : %f" ,
00078                   data->horizontal_resolution );
00079 
00080       eh_free(filename);
00081    }
00082    eh_free( cube_name );
00083 
00084    return info;
00085 }
00086 
00087 #define DATA_DUMP_KEY_DIR       "output directory"
00088 #define DATA_DUMP_KEY_VRES      "vertical resolution"
00089 #define DATA_DUMP_KEY_HRES      "horizontal resolution"
00090 #define DATA_DUMP_KEY_Y_LIM     "vertical limits"
00091 #define DATA_DUMP_KEY_X_LIM     "horizontal limits"
00092 #define DATA_DUMP_KEY_PROPERTY  "property"
00093 
00094 static gchar* data_dump_req_labels[] =
00095 {
00096    DATA_DUMP_KEY_DIR      ,
00097    DATA_DUMP_KEY_VRES     ,
00098    DATA_DUMP_KEY_HRES     ,
00099    DATA_DUMP_KEY_Y_LIM    ,
00100    DATA_DUMP_KEY_X_LIM    ,
00101    DATA_DUMP_KEY_PROPERTY ,
00102    NULL
00103 };
00104 
00105 gboolean
00106 init_data_dump( Sed_process p , Eh_symbol_table tab , GError** error )
00107 {
00108    Data_dump_t* data    = sed_process_new_user_data( p , Data_dump_t );
00109    GError*      tmp_err = NULL;
00110    gboolean     is_ok   = TRUE;
00111 
00112    eh_return_val_if_fail( error==NULL || *error==NULL , FALSE );
00113    
00114    if ( eh_symbol_table_require_labels( tab , data_dump_req_labels , &tmp_err ) )
00115    {
00116       data->property   = g_array_new( FALSE , FALSE , sizeof(Sed_property) );
00117       data->output_dir = eh_symbol_table_value( tab , DATA_DUMP_KEY_DIR );
00118 
00119       // ---
00120       // read the vertical and horizontal resolutions.  if the key word, 'full'
00121       // is given for either resolution, use the full resolution for that 
00122       // direction.
00123       // ---
00124       if ( strcasecmp( eh_symbol_table_lookup( tab , DATA_DUMP_KEY_VRES ) , "full" ) == 0 )
00125          data->vertical_resolution = -1;
00126       else
00127          data->vertical_resolution = eh_symbol_table_dbl_value( tab , DATA_DUMP_KEY_VRES );
00128 
00129       if ( strcasecmp( eh_symbol_table_lookup( tab , DATA_DUMP_KEY_HRES ) , "full" ) == 0 )
00130          data->horizontal_resolution = -1;
00131       else
00132          data->horizontal_resolution = eh_symbol_table_dbl_value( tab , DATA_DUMP_KEY_HRES );
00133 
00134       // ---
00135       // read the x and y limits of the output file.  if the key word, 'tight'
00136       // is given for either limits, use limits that will encompass all of the
00137       // data in that direction.
00138       // ---
00139       if ( strcasecmp( eh_symbol_table_lookup( tab , DATA_DUMP_KEY_Y_LIM ) , "tight") == 0 ) {
00140          data->y_lim_min = -G_MAXDOUBLE;
00141          data->y_lim_max =  G_MAXDOUBLE;
00142       }
00143       else
00144       {
00145          gchar** y_lim_str = g_strsplit( eh_symbol_table_lookup( tab , DATA_DUMP_KEY_Y_LIM ) , "," , 2 );
00146          data->y_lim_min = strtod( y_lim_str[0] , NULL );
00147          data->y_lim_max = strtod( y_lim_str[1] , NULL );
00148       }
00149 
00150       if ( strcasecmp( eh_symbol_table_lookup( tab , DATA_DUMP_KEY_X_LIM ) , "tight" ) == 0 )
00151       {
00152          data->x_lim_min = -G_MAXDOUBLE;
00153          data->x_lim_max =  G_MAXDOUBLE;
00154       }
00155       else
00156       {
00157          gchar** x_lim_str = g_strsplit( eh_symbol_table_lookup( tab , DATA_DUMP_KEY_X_LIM ) , "," , 2 );
00158          data->x_lim_min = strtod( x_lim_str[0] , NULL );
00159          data->x_lim_max = strtod( x_lim_str[1] , NULL );
00160       }
00161 
00162       // ---
00163       // read the property labels.  they are comma delimited so we cycle through
00164       // the list of them.
00165       // ---
00166       {
00167          gint         i;
00168          gchar**      property = g_strsplit( eh_symbol_table_lookup( tab , DATA_DUMP_KEY_PROPERTY ) , "," , -1 );
00169          Sed_property property_val;
00170 
00171          for ( i=0 ; property[i] ; i++ )
00172          {
00173             property_val = sed_property_new( property[i] );
00174             g_array_append_val( data->property , property_val );
00175          }
00176 
00177          g_strfreev(property);
00178       }
00179    
00180 //   if ( !try_dir(data->output_dir) )
00181       if ( !tmp_err ) eh_open_dir( data->output_dir , &tmp_err );
00182    }
00183 
00184    if ( tmp_err )
00185    {
00186       g_propagate_error( error , tmp_err );
00187       is_ok = FALSE;
00188    }
00189 
00190    return is_ok;
00191 }
00192 
00193 gboolean
00194 destroy_data_dump( Sed_process p )
00195 {
00196    if ( p )
00197    {
00198       Data_dump_t* data = sed_process_user_data( p );
00199 
00200       if ( data )
00201       {
00202          if ( data->property ) g_array_free( data->property , TRUE );
00203          eh_free( data->output_dir );
00204          eh_free( data             );
00205       }
00206    }
00207 
00208    return TRUE;
00209 }
00210 
00211 gboolean dump_data_dump_data( gpointer ptr , FILE *fp )
00212 {
00213    guint len;
00214    Data_dump_t *data = (Data_dump_t*)ptr;
00215 
00216    fwrite( &(data->vertical_resolution) , sizeof(double) , 1 , fp );
00217    fwrite( &(data->horizontal_resolution) , sizeof(double) , 1 , fp );
00218    fwrite( &(data->y_lim_min) , sizeof(double) , 1 , fp );
00219    fwrite( &(data->y_lim_max) , sizeof(double) , 1 , fp );
00220    fwrite( &(data->x_lim_min) , sizeof(double) , 1 , fp );
00221    fwrite( &(data->x_lim_max) , sizeof(double) , 1 , fp );
00222 
00223    len = strlen( data->output_dir );
00224    fwrite( &len , sizeof(guint) , 1 , fp );
00225    fwrite( data->output_dir , sizeof(char) , len , fp );
00226 
00227    len = data->property->len;
00228    fwrite( &len  , sizeof(guint) , 1 , fp );
00229    fwrite( &(data->property->data) , sizeof(guint) , len , fp );
00230 
00231    fwrite( &(data->count) , sizeof(int) , 1 , fp );
00232 
00233    return TRUE;
00234 }
00235 
00236 gboolean load_data_dump_data( gpointer ptr , FILE *fp )
00237 {
00238    Data_dump_t *data = (Data_dump_t*)ptr;
00239    Sed_property *property;
00240    guint len;
00241 
00242    fread( &(data->vertical_resolution)   , sizeof(double) , 1 , fp );
00243    fread( &(data->horizontal_resolution) , sizeof(double) , 1 , fp );
00244    fread( &(data->y_lim_min)             , sizeof(double) , 1 , fp );
00245    fread( &(data->y_lim_max)             , sizeof(double) , 1 , fp );
00246    fread( &(data->x_lim_min)             , sizeof(double) , 1 , fp );
00247    fread( &(data->x_lim_max)             , sizeof(double) , 1 , fp );
00248 
00249    fread( &len                           , sizeof(guint)  , 1 , fp );
00250    data->output_dir = eh_new( char , len );
00251    fread( data->output_dir , sizeof(char) , len , fp );
00252 
00253    data->property = g_array_new( FALSE , FALSE , sizeof(Sed_property) );
00254    fread( &len , sizeof(guint) , 1 , fp );
00255    property = eh_new( Sed_property , len );
00256    fread( property , sizeof(Sed_property) , len , fp );
00257    g_array_append_vals( data->property , property , len );
00258 
00259    fread( &(data->count) , sizeof(int) , 1 , fp );
00260 
00261    return TRUE;
00262 }
00263 

Generated on Fri Jan 4 18:04:16 2008 for sedflux by  doxygen 1.5.2