solstice-pp

Post-processing utilities for the solstice app
git clone git://git.meso-star.com/solstice-pp.git
Log | Files | Refs | README | LICENSE

solmaps.c (2329B)


      1 /* Copyright (C) 2017, 2018, 2025 |Méso|Star>
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "solpp.h"
     17 
     18 int
     19 main(int argc, char** argv)
     20 {
     21   char s[128];
     22   buf_char_T buf = BUF_NULL;
     23   FILE* input = stdin;
     24   char* line = NULL;
     25   double azim = -1;
     26   double elev = -1;
     27 
     28   if(argc > 1 && !(input = fopen(argv[1], "r"))) {
     29     fprintf(stderr, "Could not open the file `%s'.\n", argv[1]);
     30     return 1;
     31   }
     32 
     33   line = read_line(&buf, input);
     34   while(line) {
     35     if(!strncmp(line, "#--- Sun direction:", 19)) {
     36       /* Get the solar direction */
     37       CHK(sscanf(line+19, "%lf %lf (%*f %*f %*f)", &azim, &elev)==2);
     38       line = read_line(&buf, input);
     39     } else if(!strncmp(line, "# vtk", 5)) {
     40       char* header = NULL;
     41       char* rcv_name = NULL;
     42       FILE* output;
     43 
     44       CHK(azim >= 0 && elev >= 0);
     45 
     46       CHK(header = strdup(line)); /* Duplicate the current line */
     47       CHK(line = read_line(&buf, input));
     48       CHK(rcv_name = strdup(line)); /* Duplicate the line of the receiver name */
     49 
     50       /* Create the name of the destination file */
     51       CHK(snprintf(s, sizeof(s), "%g-%g-%s.vtk", azim, elev, rcv_name) < sizeof(s));
     52       printf("Writing `%s'\n", s);
     53       CHK(output = fopen(s, "w"));
     54 
     55       /* Write the map data into `output' */
     56       fprintf(output, "%s\n", header);
     57       fprintf(output, "%s\n", rcv_name);
     58       while((line = read_line(&buf, input)) && line[0] != '#') {
     59         fprintf(output, "%s\n", line);
     60       }
     61 
     62       /* Clean up temporary variable and close the destination file */
     63       fclose(output);
     64       free(header);
     65       free(rcv_name);
     66     } else {
     67       line = read_line(&buf, input);
     68     }
     69   }
     70 
     71   BUF_RELEASE(buf);
     72   if(input && input!=stdin) fclose(input);
     73   return 0;
     74 }
     75