Xmipp  v3.23.11-Nereus
spider.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 
27 #include "spider.h"
28 
29 #include <core/args.h>
30 #include <core/geometry.h>
31 
32 // Generate Count File -----------------------------------------------------
33 void generate_Spider_count(int imax, DocFile &DF_out)
34 {
35  Matrix1D<double> aux(1);
36 
37  DF_out.clear();
38  DF_out.append_comment((std::string)"Count for Spider up to " + integerToString(imax));
39 
40  for (aux(0) = 1; aux(0) <= imax; aux(0)++)
41  DF_out.append_data_line(aux);
42 }
43 
44 // Translate to Spider selfile ---------------------------------------------
45 void translate_to_Spider_sel(MetaData &SF_in, DocFile &DF_out, bool new_style)
46 {
47  Matrix1D<double> aux(1);
48  //int selline = 1;
49 
50  DF_out.clear();
51  DF_out.append_comment((std::string)"Translation for Spider of " + SF_in.getFilename());
52  int i=1;
53  for (size_t objId : SF_in.ids())
54  {
55  bool store = true;
56  //if (!SF_in.Is_COMMENT())
57  {
58  int enabled;
59 
60  SF_in.getValue(MDL_ENABLED, enabled, objId);
61 
62  if ( enabled ==1)
63  {
64  if (!new_style)
65  aux(0) = 1;
66  //else aux(0) = ((FileName)SF_in.get_current_file()).get_number();
67  else
68  aux(0) = i++;
69  }
70  else
71  {
72  if (!new_style)
73  aux(0) = 0;
74  else
75  {
76  store = false;
77  i++;
78  }
79  }
80  if (store)
81  DF_out.append_data_line(aux);
82  }
83  }
84 
85 }
86 
87 // Extract angles ----------------------------------------------------------
88 void extract_angles(MetaData &SF_in, DocFile &DF_out,
89  const std::string &ang1, const std::string &ang2,
90  const std::string &ang3, bool fromMetadata)
91 {
92  checkAngle(ang1);
93  checkAngle(ang2);
94  checkAngle(ang3);
95 
96  DF_out.clear();
97 
98  FileName auxFn;
99 
100  SF_in.getValue( MDL_IMAGE, auxFn, SF_in.firstRowId() );
101 
102  DF_out.append_comment((std::string)"Angles for " + auxFn +
103  ". Angle order: " + ang1 + " " + ang2 + " " + ang3);
104 
105  int i = 0;
106  time_config();
107  std::cerr << "Extracting angles ...\n";
108  init_progress_bar(SF_in.size());
109  Image<double> P;
110  FileName fn_img;
111  ApplyGeoParams params;
112  params.datamode = HEADER;
113 
114  for (size_t objId: SF_in.ids())
115  {
116  if (fromMetadata)
117  {
118  double rot;
119  SF_in.getValue(MDL_ANGLE_ROT, rot, objId);
120  double tilt;
121  SF_in.getValue(MDL_ANGLE_TILT, tilt, objId);
122  double psi;
123  SF_in.getValue(MDL_ANGLE_PSI, psi, objId);
124  DF_out.append_angles(rot, tilt, psi,
125  ang1, ang2, ang3);
126  }
127  else
128  {
129  // Read image
130  SF_in.getValue(MDL_IMAGE, fn_img, objId);
131  if (fn_img=="")
132  break;
133  P.readApplyGeo(fn_img, SF_in, objId, params);
134  DF_out.append_angles(P.rot(), P.tilt(), P.psi(),
135  ang1, ang2, ang3);
136  }
137  i++;
138  if (i % 10 == 0)
139  progress_bar(i);
140  }
141 
142  progress_bar(SF_in.size());
143 }
144 
145 // Rename for Spider -------------------------------------------------------
146 void rename_for_Spider(MetaData &SF_in, MetaData &SF_out, const FileName &fn_root,
147  const FileName &out_ext)
148 {
149  FileName fn_in, fn_out;
150  int counter = 1;
151  size_t id;
152 
153  for (size_t objId : SF_out.ids())
154  {
155  SF_in.getValue( MDL_IMAGE, fn_in, objId);
156  if (fn_in=="")
157  break;
158  fn_out = fn_root + integerToString(counter, 5);
159  if (out_ext == "")
160  fn_out = fn_out.addExtension(fn_in.getExtension());
161  else
162  fn_out = fn_out.addExtension(out_ext);
163  id = SF_out.addObject();
164  SF_out.setValue( MDL_IMAGE, fn_out, id);
165  SF_out.setValue( MDL_ENABLED, 1, id);
166 
167  std::cout << "Renaming " << fn_in << " as " << fn_out << std::endl;
168  std::string command = (std::string)"cp " + fn_in + " " + fn_out;
169  if (!system(command.c_str()))
170  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot copy files");
171 
172  counter++;
173  }
174 
175 }
Just to locate unclassified errors.
Definition: xmipp_error.h:192
void init_progress_bar(long total)
Rotation angle of an image (double,degrees)
virtual size_t addObject()=0
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void generate_Spider_count(int imax, DocFile &DF_out)
Definition: spider.cpp:33
Tilting angle of an image (double,degrees)
virtual size_t firstRowId() const =0
int append_angles(double rot, double tilt, double psi, const std::string &ang1, const std::string &ang2, const std::string &ang3)
Definition: docfile.cpp:1014
FileName addExtension(const String &ext) const
Special label to be used when gathering MDs in MpiMetadataPrograms.
String integerToString(int I, int _width, char fill_with)
void rename_for_Spider(MetaData &SF_in, MetaData &SF_out, const FileName &fn_root, const FileName &out_ext)
Definition: spider.cpp:146
virtual IdIteratorProxy< false > ids()
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0
#define i
Is this image enabled? (int [-1 or 1])
void time_config()
String getExtension() const
void checkAngle(const String &str)
int append_data_line(int no_lines_to_append=1)
Definition: docfile.cpp:975
void extract_angles(MetaData &SF_in, DocFile &DF_out, const std::string &ang1, const std::string &ang2, const std::string &ang3, bool fromMetadata)
Definition: spider.cpp:88
void progress_bar(long rlen)
void clear()
Definition: docfile.cpp:181
void translate_to_Spider_sel(MetaData &SF_in, DocFile &DF_out, bool new_style)
Definition: spider.cpp:45
virtual size_t size() const =0
bool setValue(const MDLabel label, const T &valueIn, size_t id)
double psi(const double x)
void append_comment(const std::string &comment)
Definition: docfile.cpp:1005
Name of an image (std::string)
virtual FileName getFilename() const